您的位置:首页 > 其它

bzoj 1503: [NOI2004]郁闷的出纳员 splay

2017-11-29 16:16 423 查看
题目链接

【想说的话】

数据结构可真是难调啊...

无比的心累

【题解】

平衡树都能过

几天前的我一定会用pbds来做

但是现在

我!不!一!样!

对于增加和减少工资,不用打标记什么的,因为询问时询问的是相对排名,所以记录一下变化量,然后对于新加入的工资减去那个变化量就好了

注意:他找的是第k大的工资!

【代码】

#include<bits/stdc++.h>

#define MAXN 100010
#define inf 1000000000

using namespace std;

inline int rd(){
int x=0,y=1;char c=getchar();
while(c<'0' || c>'9'){if(c=='-')y=-y;c=getchar();}
while(c>='0'&&c<='9')x=x*10+c-'0',c=getchar();
return x*y;
}

int son[MAXN][2],fa[MAXN],key[MAXN],cnt[MAXN],size[MAXN],root=0,tot=0,totval[MAXN],val[MAXN],Max[MAXN],add[MAXN];
int det=0;
bool rev[MAXN];

inline void pushdown(int x);

inline int is_right(int x){
return son[fa[x]][1]==x;
}

inline void pushup(int x){
if(x){
size[x]=1;
totval[x]=val[x]*cnt[x];
Max[x]=val[x];
if(son[x][0])size[x]+=size[son[x][0]],totval[x]+=totval[son[x][0]],Max[x]=max(Max[x],Max[son[x][0]]);
if(son[x][1])size[x]+=size[son[x][1]],totval[x]+=totval[son[x][1]],Max[x]=max(Max[x],Max[son[x][1]]);
}
}

inline void Rotate(int x){
int f=fa[x],ff=fa[f],which=is_right(x);
son[f][which]=son[x][which^1];
fa[son[f][which]]=f;
fa[f]=x;
son[x][which^1]=f;
fa[x]=ff;
if(ff)son[ff][son[ff][1]==f]=x;
pushup(f);
pushup(x);
}

inline void splay(int x,int to){
int f=fa[x];
pushdown(x);
while(f!=to){
if(fa[f]!=to)Rotate((is_right(x)==is_right(f)?f:x));
Rotate(x);
f=fa[x];
}
if(to==0)root=x;
}

inline void ins(int v){
if(root==0){
tot++;
son[tot][0]=son[tot][1]=fa[tot]=0;
key[tot]=v;
val[tot]=v;
totval[tot]=v;
Max[tot]=v;
cnt[tot]=1;
size[tot]=1;
root=tot;
return;
}
int now=root,f=0;
while(1){
f=now;
now=son[now][key[now]<v];
if(now==0){
tot++;
son[tot][0]=son[tot][1]=0;
key[tot]=v;
val[tot]=v;
totval[tot]=v;
Max[tot]=v;
size[tot]=1;
cnt[tot]=1;
fa[tot]=f;
son[f][key[f]<v]=tot;
pushup(f);
splay(tot,0);
break;
}
}
}

inline void uprev(int x){
if(!x)return;
swap(son[x][0],son[x][1]);
rev[x]^=1;
}

inline void pushdown(int x){
if(!x)return;
if(rev[x]){
uprev(son[x][0]);
uprev(son[x][1]);
rev[x]^=1;
}
}

inline int getkth(int x,int k){
pushdown(x);
int num=size[son[x][1]];
if(num+1==k)return x;
if(num>=k)return getkth(son[x][1],k);
return getkth(son[x][0],k-num-1);
}

int n,m;
int ans=0;

inline int Delete(int &x,int f){
if(!x)return 0;
int ret;
if(val[x]+det<m){
ret=Delete(son[x][1],x)+size[son[x][0]]+1;
size[son[x][1]]=size[x]-ret;
x=son[x][1];
fa[x]=f;
}
else{
ret=Delete(son[x][0],x);
size[x]-=ret;
}
return ret;
}

int main(){
n=rd(),m=rd();
while(n--){
char c[10];
scanf("%s",c);
int x;
x=rd();
if(c[0]=='I' && x>=m)ins(x-det);
else if(c[0]=='A')det+=x;
else if(c[0]=='S')det-=x,ans+=Delete(root,0);
else if(c[0]=='F')printf("%d\n",x<=size[root]?val[getkth(root,x)]+det:-1);
}
printf("%d\n",ans);

return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: