1 #include<cstdio>
  2 #include<cstring>
  3 #include<algorithm>
  4 #define MAXN 500010
  5 using namespace std;
  6 int x[MAXN];
  7 bool vis[MAXN];
  8 struct cmd
  9 {
 10     char s[6];
 11     int val;
 12 };
 13 struct node
 14 {
 15     int left,right,val;
 16 };
 17 node tree[MAXN<<2];
 18 cmd p[MAXN];
 19 void Build(int L,int R,int rt)
 20 {
 21     tree[rt].left=tree[rt].right=-1;
 22     tree[rt].val=0;
 23     if(L!=R)
 24     {
 25         int mid=(L+R)>>1;
 26         Build(L,mid,rt<<1);
 27         Build(mid+1,R,rt<<1|1);
 28     }
 29 }
 30 inline int MIN(int a,int b)
 31 {
 32     if(!a&&!b)
 33         return 0;
 34     if(!a)
 35         return b;
 36     if(!b)
 37         return a;
 38     return a>b?b:a;
 39 }
 40 inline void PushUp(int L,int R,int rt)
 41 {
 42     if(tree[rt<<1].left>=0)
 43         tree[rt].left=tree[rt<<1].left;
 44     else
 45         tree[rt].left=tree[rt<<1|1].left;
 46     if(tree[rt<<1|1].right>=0)
 47         tree[rt].right=tree[rt<<1|1].right;
 48     else
 49         tree[rt].right=tree[rt<<1].right;
 50     tree[rt].val=MIN(tree[rt<<1].val,tree[rt<<1|1].val);
 51     if(tree[rt].left>=0&&tree[rt].right>=0)
 52         tree[rt].val=MIN(tree[rt].val,x[tree[rt].right]-x[tree[rt].left]);
 53     if(tree[rt<<1].right>=0&&tree[rt<<1|1].left>=0)
 54         tree[rt].val=MIN(tree[rt].val,x[tree[rt<<1|1].left]-x[tree[rt<<1].right]);
 55 }
 56 void Update(int pos,bool flag,int L,int R,int rt)
 57 {
 58     if(L==R)
 59     {
 60         if(flag)
 61             tree[rt].left=tree[rt].right=L;
 62         else
 63             tree[rt].left=tree[rt].right=-1;
 64     }
 65     else
 66     {
 67         int mid=(L+R)>>1;
 68         if(pos<=mid)
 69             Update(pos,flag,L,mid,rt<<1);
 70         else
 71             Update(pos,flag,mid+1,R,rt<<1|1);
 72         PushUp(L,R,rt);
 73     }
 74 }
 75 int main()
 76 {
 77     char s[6];
 78     int n,i,cnt,size,pos;
 79     while(~scanf("%d",&n))
 80     {
 81         memset(vis,false,sizeof(vis));
 82         for(i=cnt=0;i<n;i++)
 83         {
 84             scanf(" %s",p[i].s);
 85             if(p[i].s[0]!='m')
 86             {
 87                 scanf("%d",&p[i].val);
 88                 x[cnt++]=p[i].val;
 89             }
 90         }
 91         sort(x,x+cnt);
 92         cnt=unique(x,x+cnt)-x;
 93         Build(0,cnt-1,1);
 94         for(i=size=0;i<n;i++)
 95         {
 96             if(p[i].s[0]=='a')
 97             {
 98                 pos=lower_bound(x,x+cnt,p[i].val)-x;
 99                 if(!vis[pos])
100                 {
101                     vis[pos]=true;
102                     size++;
103                     Update(pos,true,0,cnt-1,1);
104                 }
105             }
106             else if(p[i].s[0]=='d')
107             {
108                 pos=lower_bound(x,x+cnt,p[i].val)-x;
109                 if(vis[pos])
110                 {
111                     vis[pos]=false;
112                     size--;
113                     Update(pos,false,0,cnt-1,1);
114                 }
115             }
116             else
117             {
118                 if(size<2)
119                     puts("0");
120                 else
121                     printf("%d\n",tree[1].val);
122             }
123         }
124     }
125     return 0;
126 }
posted on 2012-06-21 13:24  DrunBee  阅读(385)  评论(0编辑  收藏  举报