luogu1486 [NOI2004]郁闷的出纳员 (平衡树)
2024-10-20 07:51:05
加的注释,都流着泪。。。
胡了一种做法,样例都没过,翻题解发现一神仙Remove操作,妙啊!
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#define R(a,b,c) for(register int a = (b); a <= (c); ++ a)
#define nR(a,b,c) for(register int a = (b); a >= (c); -- a)
#define Max(a,b) ((a) > (b) ? (a) : (b))
#define Min(a,b) ((a) < (b) ? (a) : (b))
#define Fill(a,b) memset(a, b, sizeof(a))
#define Abs(a) ((a) < 0 ? -(a) : (a))
#define Swap(a,b) a^=b^=a^=b
#define ll long long
#define ON_DEBUG
#ifdef ON_DEBUG
#define D_e_Line printf("\n\n----------\n\n")
#define D_e(x) cout << #x << " = " << x << endl
#define Pause() system("pause")
#define FileOpen() freopen("in.txt","r",stdin);
#else
#define D_e_Line ;
#define D_e(x) ;
#define Pause() ;
#define FileOpen() ;
#endif
struct ios{
template<typename ATP>ios& operator >> (ATP &x){
x = 0; int f = 1; char c;
for(c = getchar(); c < '0' || c > '9'; c = getchar()) if(c == '-') f = -1;
while(c >= '0' && c <= '9') x = x * 10 + (c ^ '0'), c = getchar();
x*= f;
return *this;
}
}io;
using namespace std;
const int N = 100007;
int minLimit, totLeave;
struct Treap{
int ch[2], fa, val, tot, siz;
}t[N];
int root, treeIndex;
inline void Pushup(int rt){
t[rt].siz = t[t[rt].ch[0]].siz + t[t[rt].ch[1]].siz + t[rt].tot;
}
inline int Ident(int x){
return t[t[x].fa].ch[1] == x;
}
inline void Rotate(int x){
int y = t[x].fa, z = t[y].fa, k = Ident(x);
t[z].ch[Ident(y)] = x, t[x].fa = z;
t[y].ch[k] = t[x].ch[k ^ 1], t[t[x].ch[k ^ 1]].fa = y;
t[x].ch[k ^ 1] = y, t[y].fa = x;
Pushup(y), Pushup(x);
}
inline void Splay(int x, int pos){
while(t[x].fa != pos){
int y = t[x].fa, z = t[y].fa;
if(z != pos){
Ident(x) == Ident(y) ? Rotate(y) : Rotate(x);
}
Rotate(x);
}
if(!pos) root = x;
}
inline void Insert(int x){
if(x < minLimit) return;
int u = root, fa = 0;
while(u && t[u].val != x){ // !
fa = u;
u = t[u].ch[x > t[u].val];
}
if(u){
++t[u].tot;
}
else{
u = ++treeIndex;
t[u].ch[0] = t[u].ch[1] = 0;
t[u].siz = t[u].tot = 1;
t[u].fa = fa;
t[u].val = x;
if(fa) t[fa].ch[x > t[fa].val] = u;
}
Splay(u, 0);
}
inline void Find(int x){
int u = root;
if(!u) return;
while(t[u].val != x && t[u].ch[x > t[u].val]) u = t[u].ch[x > t[u].val]; // !
Splay(u, 0); // !
return;
}
inline void Add(int x){
R(i,1,treeIndex) t[i].val += x;
}
inline void Sub(int x){
R(i,1,treeIndex) t[i].val -= x;
}
inline int Next(int x, int type){ // the minimu number >= x
Find(x);
int u = root;
if((t[u].val >= x && type) || (t[u].val < x && !type)) return u; // >= ?
u = t[u].ch[type];
while(t[u].ch[type ^ 1]) u = t[u].ch[type ^ 1];
return u;
}
inline void Remove(int x){ // %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
int rt = Next(x + minLimit, 1);
Splay(rt, 0);
totLeave += t[t[rt].ch[0]].siz;
t[rt].ch[0] = 0;
Pushup(rt);
Sub(x);
}
inline int Kth(int x){
int u = root;
if(t[u].siz <= x) return -1;
++x;
while(1){
int v = t[u].ch[1];
if(t[v].siz + t[u].tot < x){
x -= t[v].siz + t[u].tot;
u = t[u].ch[0];
}
else if(x <= t[v].siz){ // !
u = v;
}
else
return t[u].val;
}
}
int main(){
//FileOpen();
//freopen("my.txt","w",stdout);
int m;
io >> m >> minLimit;
Insert(2147483647);
// Insert(-2147483647);
while(m --){
char opt = getchar();
while(opt != 'I' && opt != 'S' && opt != 'A' && opt != 'F') opt = getchar();
int x;
io >> x;
if(opt == 'I'){
Insert(x);
}
else if(opt == 'A'){
Add(x);
}
else if(opt == 'S'){
Remove(x);
}
else{
printf("%d\n",Kth(x));
}
}
printf("%d", totLeave);
return 0;
}
最新文章
- DNS-2
- mysql 5.5多实例部署【图解】
- android获取状态栏高度
- [SVN(ubuntu)] svn 文件状态标记含义
- NodeJS 各websocket框架性能分析
- HOG特征(Histogram of Gradient)总结(转载)
- ### Theano
- Java单实例的最佳写法
- jni cocos2d-x移植到android:helloworld
- delphi 功能函数大全-备份用
- LeetCode(2) || Add Two Numbers &;&; Longest Substring Without Repeating Characters
- C++学习笔记33 转换操作符
- 安卓手机微信页面position: fixed位置错误
- virtualbox, vt-s, rmmod kvm-intel
- angr进阶(5)内存操作
- 使用Intellij中的Spring Initializr来快速构建Spring Boot/Cloud工程(十五)
- ansible经常使用模块使用方法
- [加密]展讯secureboot方案
- JS 中的布尔运算符 &;&; 和 ||
- 九九乘法表的python复习