题意:刚开始有一个空的序列,有三种操作
1.insert p 在p位置插入最小的未被使用的正整数,然后找到另外一个位置插入一个负数,
该负数左边负数的个数和该数的相反数的左边的正数的个数一样多且最为靠右
2.query x 查询-x所在位置到x所在位置之间的和
3.remove x 删除x和-x
思路:最小的正整数我们可以用一个优先队列位置,负数位置的确定,我们可以每个节点维护一个其左边的负数个数和其左边的正整数个数
x和-x的位置的确定,插入的时候我们记录一下x和-x在Splay中的下标就可以了
#include<bits/stdc++.h>
using namespace std;
#define key_value ch[ch[root][1]][0]
const int Add=1e5+100;
const int N=3e5+10;
int postive[N],unpostive[N],root,tot,Belong[N],fa[N],size[N];
long long sumv[N];
int ch[N][2],key[N];
priority_queue<int,vector<int>,greater<int> >Q;
void Newnode(int &now,int father,int k){
now=++tot;
fa[now]=father,ch[now][0]=ch[now][1]=0,size[now]=1,sumv[now]=k;
key[now]=k,postive[now]=0,unpostive[now]=0;
if(k>0) postive[now]++;
else unpostive[now]++;
Belong[k+Add]=now;
}
char str[10];
void pushup(int x){
sumv[x]=sumv[ch[x][0]]+sumv[ch[x][1]]+key[x];
size[x]=size[ch[x][0]]+size[ch[x][1]]+1;
postive[x]=postive[ch[x][0]]+postive[ch[x][1]]+(key[x]>0 ? 1:0);
unpostive[x]=unpostive[ch[x][0]]+unpostive[ch[x][1]]+(key[x]<0 ? 1:0);
}
void init(){
tot=root=0;
ch[root][0]=ch[root][1]=0;
Newnode(root,0,-100005);
Newnode(ch[root][1],root,-100005);
pushup(ch[root][1]),pushup(root);
}
//旋转,kind为1为右旋,kind为0为左旋
void Rotate(int x,int kind){
int y=fa[x];
ch[y][!kind]=ch[x][kind];
fa[ch[x][kind]]=y;
//如果父节点不是根结点,则要和父节点的父节点连接起来
if(fa[y])
ch[fa[y]][ch[fa[y]][1]==y]=x;
fa[x]=fa[y];
ch[x][kind]=y;
fa[y]=x;
pushup(y);
}
//Splay调整,将根为now的子树调整为goal
void Splay(int now,int goal){
while(fa[now]!=goal){
if(fa[ fa[now] ]==goal)
Rotate(now,ch[ fa[now] ][0]==now);
else{
int pre=fa[now],kind=ch[ fa[pre] ][0]==pre; //左儿子为1,右儿子为0
if(ch[pre][kind]==now){ //两个方向不同
Rotate(now,!kind);
Rotate(now,kind);
}
else{ //两个方向相同
Rotate(pre,kind);
Rotate(now,kind);
}
}
}
if(goal==0) root=now;
pushup(now);
}
int get_kth(int x,int k){
int num=size[ch[x][0]]+1;
if(num==k)
return x;
if(num>k) return get_kth(ch[x][0],k);
return get_kth(ch[x][1],k-num);
}
int get_Rank(int x,int k){
int num=unpostive[ch[x][0]]+(key[x]<0 ? 1:0);
if(num==k&&key[x]<0) return x;
if(num>=k) return get_Rank(ch[x][0],k);
return get_Rank(ch[x][1],k-num);
}
int get_min(int x){
x=ch[x][1];
while(ch[x][0])
x=ch[x][0];
return x;
}
int get_max(int x){
x=ch[x][0];
while(ch[x][1])
x=ch[x][1];
return x;
}
void Insert(int x,int k){
//将正x插入
Splay(get_kth(root,k+1),0);
Splay(get_kth(root,k+2),root);
Newnode(key_value,ch[root][1],x);
pushup(ch[root][1]),pushup(root);
Splay(key_value,0);
//将负x插入
int cnt=postive[ch[root][0]]; //之前有多少个正数
k=get_Rank(root,cnt+2);//因为要找到最右边,所以先多找一个
int tmp=k;
Splay(k,0);
k=get_max(k);
Splay(k,0);
Splay(tmp,root);
Newnode(key_value,ch[root][1],-x);
pushup(ch[root][1]),pushup(root);
}
void Query(int num){
Splay(Belong[num+Add],0);
Splay(Belong[-num+Add],root);
pushup(ch[root][1]),pushup(root);
printf("%lld\n",sumv[key_value]);
}
void Remove(){
int m=get_max(root);
Splay(m,root);
ch[m][1]=ch[root][1];
fa[ch[root][1]]=m;
root=m;
fa[root]=0;
pushup(root);
}
void remove(int num){
Splay(Belong[num+Add],0);
Remove();
Splay(Belong[-num+Add],0);
Remove();
}
void travel(int x){
if(!x) return ;
travel(ch[x][0]);
printf("%d ",key[x]);
travel(ch[x][1]);
}
int main(){
int n,x,case1=1;
while(scanf("%d",&n)!=EOF){
printf("Case #%d:\n",case1++);
while(!Q.empty())
Q.pop();
for(int i=1;i<=n;i++)
Q.push(i);
init();
for(int i=1;i<=n;i++){
scanf("%s%d",str,&x);
if(str[0]=='i'){
int num=Q.top();
Q.pop(),Insert(num,x);
}
else if(str[0]=='q')
Query(x);
else
remove(x),Q.push(x);
}
}
return 0;
}