1 #include <cstdio> 2 #include <algorithm> 3 using namespace std; 4 #define lson l,m,rt+1 5 #define rson m+1,r,rt+1 6 #define maxn 100005 7 struct{ 8 char s[10]; 9 int l,r,num; 10 }mes[maxn<<2]; 11 struct node{ 12 int num,val; 13 }setree[18][maxn]; 14 int sorted[maxn]; 15 void build(int l,int r,int rt) 16 { 17 if(l==r) 18 return; 19 int m=(l+r)>>1; 20 int same=m-l+1; 21 for(int i=l;i<=r;i++) 22 if(sorted[i]<sorted[m]) 23 same--; 24 int ln=l,rn=m+1; 25 for(int i=l;i<=r;i++){ 26 if(i==l) 27 setree[rt][i].num=0; 28 else 29 setree[rt][i].num=setree[rt][i-1].num; 30 if(setree[rt][i].val<sorted[m]){ 31 setree[rt+1][ln++].val=setree[rt][i].val; 32 setree[rt][i].num++; 33 } 34 else if(setree[rt][i].val>sorted[m]){ 35 setree[rt+1][rn++].val=setree[rt][i].val; 36 } 37 else{ 38 if(same){ 39 setree[rt+1][ln++].val=setree[rt][i].val; 40 setree[rt][i].num++; 41 same--; 42 } 43 else{ 44 setree[rt+1][rn++].val=setree[rt][i].val; 45 } 46 } 47 } 48 build(lson); 49 build(rson); 50 } 51 int query(int l,int r,int rt,int L,int R,int num) 52 { 53 if(l==r) 54 return setree[rt][l].val; 55 int m=(l+r)>>1; 56 int inleft1,inleft2,inright1,inright2; 57 if(L==l){ 58 inleft1=0; 59 inleft2=setree[rt][R].num; 60 inright1=0; 61 inright2=R-l+1-inleft2; 62 } 63 else{ 64 inleft1=setree[rt][L-1].num; 65 inleft2=setree[rt][R].num; 66 inright1=L-l-inleft1; 67 inright2=R-l+1-inleft2; 68 } 69 if(inleft2-inleft1>=num) 70 return query(lson,l+inleft1,l+inleft2-1,num); 71 else 72 return query(rson,m+inright1+1,m+inright2,num-inleft2+inleft1); 73 } 74 int binsearch(int l,int r,int num,int n) 75 { 76 int L=1,R=r-l+1; 77 while(R>=L){ 78 int m=(R+L)>>1; 79 int num1=query(1,n,0,l,r,m); 80 if(num==num1) 81 return m; 82 else if(num<num1) 83 R=m-1; 84 else 85 L=m+1; 86 } 87 //return R; 88 } 89 int main() 90 { 91 int m,cas=1; 92 while(~scanf("%d",&m)){ 93 int n=0; 94 for(int i=1;i<=m;i++){ 95 scanf("%s",&mes[i].s); 96 if(mes[i].s[0]=='I'){ 97 scanf("%d",&mes[i].num); 98 sorted[++n]=mes[i].num; 99 setree[0][n].val=mes[i].num; 100 } 101 else if(mes[i].s[6]=='1') 102 scanf("%d%d%d",&mes[i].l,&mes[i].r,&mes[i].num); 103 else 104 scanf("%d",&mes[i].num); 105 } 106 sort(sorted+1,sorted+n+1); 107 build(1,n,0); 108 int k=0; 109 __int64 ans1=0,ans2=0,ans3=0; 110 printf("Case %d:\n",cas++); 111 for(int i=1;i<=m;i++){ 112 if(mes[i].s[0]=='I') 113 k++; 114 else if(mes[i].s[6]=='1') 115 ans1+=query(1,n,0,mes[i].l,mes[i].r,mes[i].num); 116 else if(mes[i].s[6]=='3') 117 ans3+=query(1,n,0,1,k,mes[i].num); 118 else{ 119 int ans=binsearch(1,k,mes[i].num,n); 120 ans2+=ans; 121 } 122 } 123 printf("%I64d\n%I64d\n%I64d\n",ans1,ans2,ans3); 124 } 125 return 0; 126 }