【HDU 4441】 Queue Sequence 【Splay】

题意:

 给你一个初始为空的序列,让你支持以下操作

1.插入+x,-x到位置p,其中x是序列中没出现过的最小正整数,其中插入后+x在位置p,-x的位置是左边负数的个数和+x左边正数的个数一样且最靠右的位置

2.删除+x,-x 保证+x,-x在序列中存在

3.询问+x,-x之间的数之和

题解:

这题是做的几个splay题目里面比较烦的一个,所以写个博客记录下

首先插入操作是最烦的,求序列中没出现过的最小正整数用一个优先队列维护下就行,对于+x,我们先Get_kth(root,p)找到第p个数是那个数,然后把这个数splay到根然后把+x插到根的前面去,对于-x,由于我们要知道+x前面有几个正数,这里我是用了cnt[x][0]和cnt[x][1]来维护正数和负数在x的子树中的个数,这样我们就可以像计算siz一样计算正数与负数的个数,设+x前面有num个正数,我们就把第num+1个负数splay到根然后把-x插到根前面去,就满足了最靠右的要求,这里插入查找时注意没有这么多数的情况需要特判下

然后是删除操作,这个我们只要在插入的时候用一个数组维护下数x的下标是哪个,把下标对应的节点转到根删了就行

然后输求和操作,这和splay经典的区间操作一样把l-1splay到根,把r+1splay到根的右儿子,这样根的右儿子的左儿子就是我们需要操作的区间[l,r],在一般题目中我们需要新建两个节点来防止越界,这题就不需要了

#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
using namespace std;
#define PB push_back
#define MP make_pair
#define ll long long
#define MS(a,b) memset(a,b,sizeof(a))
#define LL (rt<<1)
#define RR (rt<<1|1)
#define lson l,mid,LL
#define rson mid+1,r,RR
#define pii pair
#define pll pair
#define lb(x) (x&(-x))
void In(){freopen("in.in","r",stdin);}
void Out(){freopen("out.out","w",stdout);}
const int N=2e6+10;
const int M=3e5+10;
const int zero=1e6+5;
const int Mbit=1e6+10;
const int inf=0x3f3f3f3f;
const ll mod=1e9+7;
int root,tot;
int ch[N][2],cnt[N][2],siz[N],pre[N],pos[N],key[N];
ll sum[N];
void init()
{root=tot=0;ch[root][0]=ch[root][1]=siz[root]=pre[root]=cnt[root][0]=cnt[root][1]=0;sum[root]=0ll;
}
void newnode(int &r,int k,int fa)
{r=++tot;pos[k+zero]=r;pre[r]=fa;siz[r]=1;ch[r][0]=ch[r][1]=cnt[r][0]=cnt[r][1]=0;sum[r]=k;if(k>0)cnt[r][0]=1;else cnt[r][1]=1;key[r]=k;
}
void up(int r)
{siz[r]=siz[ch[r][0]]+siz[ch[r][1]]+1;sum[r]=0ll+key[r]+sum[ch[r][0]]+sum[ch[r][1]];cnt[r][0]=cnt[ch[r][0]][0]+cnt[ch[r][1]][0]+(key[r]>0);cnt[r][1]=cnt[ch[r][0]][1]+cnt[ch[r][1]][1]+(key[r]<0);
}
int Insert(int &r,int i,int fa)
{if(r==0){newnode(r,i,fa);return 0;}int t;t=Insert(ch[r][1],i,r);up(r);return cnt[ch[r][0]][0]+t+(key[r]>0);
}
void Rotate(int x,int kind){int y=pre[x];ch[y][!kind]=ch[x][kind];pre[ch[x][kind]]=y;if(pre[y])ch[pre[y]][ch[pre[y]][1]==y]=x;pre[x]=pre[y];ch[x][kind]=y;pre[y]=x;up(y);
}
void splay(int r,int goal){while(pre[r]!=goal){if(pre[pre[r]]==goal)Rotate(r,ch[pre[r]][0]==r);else{int y=pre[r];int kind=(ch[pre[y]][0]==y);if(ch[y][kind]==r){Rotate(r,!kind);Rotate(r,kind);}else{Rotate(y,kind);Rotate(r,kind);}}}up(r);if(goal==0) root=r;
}
int Min(int r){while(ch[r][0])r=ch[r][0];return r;
}
void Delete(){if(ch[root][0]==0||ch[root][1]==0){root=ch[root][0]+ch[root][1];pre[root]=0;return;}int k=Min(ch[root][1]);splay(k,root);ch[ch[root][1]][0]=ch[root][0];root=ch[root][1];pre[ch[root][0]]=root;pre[root]=0;up(root);
}
int Get_kth(int r,int k){int t=siz[ch[r][0]];if(t>=k)return Get_kth(ch[r][0],k);else if(t+1==k) return r;else return Get_kth(ch[r][1],k-t-1);
}
int Get_kthneg(int r,int k)
{int t=cnt[ch[r][0]][1];if(t>=k)return Get_kthneg(ch[r][0],k);else if((t+1==k)&&(key[r]<0))return r;else return Get_kthneg(ch[r][1],k-t-(key[r]<0));
}
char op[10];
int main()
{//In();//Out();int T,kase=0,n;while(~scanf("%d",&n)){printf("Case #%d:\n",++kase);init();priority_queue,greater>pq;for(int i=1;i<=n;i++)pq.push(i);//维护最小可用while(n--){scanf("%s",op);//assert(siz[0]==0);if(op[0]=='i'){int p;scanf("%d",&p);p++;int i=pq.top();pq.pop();int num;if(siz[root]



本文来自互联网用户投稿,文章观点仅代表作者本人,不代表本站立场,不承担相关法律责任。如若转载,请注明出处。 如若内容造成侵权/违法违规/事实不符,请点击【内容举报】进行投诉反馈!

相关文章

立即
投稿

微信公众账号

微信扫一扫加关注

返回
顶部