luogu1486 [NOI2004]郁闷的出纳员 (平衡树)
加的注释,都流着泪。。。
胡了一种做法,样例都没过,翻题解发现一神仙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;
}