码力不行啊。。。
错误记录:
171行后面对find2的使用错误,原来写的是p=find2(rt,p1),然后再加上一句能过样例但很假的特判
事实上,现在是要寻找最大的j,使得d2[1..j-1]=p1-1
而find2返回的是最大的j,使得d2[1..j]<=p1,因此要这么用
1 #pragma GCC optimize("Ofast") 2 #include<cstdio> 3 #include<algorithm> 4 #include<cstring> 5 #include<vector> 6 #include<set> 7 using namespace std; 8 #define fi first 9 #define se second 10 #define mp make_pair 11 #define pb push_back 12 typedef long long ll; 13 typedef unsigned long long ull; 14 typedef pair<int,int> pii; 15 namespace S 16 { 17 const int N=200100; 18 int rand1() 19 { 20 static int x=233; 21 return x=(48271LL*x+1)%2147483647; 22 } 23 struct Node 24 { 25 Node *ch[2],*fa; 26 int sz,d,r; 27 int d1,d2;ll sum; 28 }nds[N]; 29 int gsz(Node *o) {return o?o->sz:0;} 30 int gd1(Node *o) {return o?o->d1:0;} 31 int gd2(Node *o) {return o?o->d2:0;} 32 ll gsum(Node *o) {return o?o->sum:0;} 33 void upd(Node *o) 34 { 35 o->sz=gsz(o->ch[0])+gsz(o->ch[1])+1; 36 o->d1=gd1(o->ch[0])+gd1(o->ch[1])+(o->d>0); 37 o->d2=gd2(o->ch[0])+gd2(o->ch[1])+(o->d<0); 38 o->sum=ll(gsum(o->ch[0]))+gsum(o->ch[1])+o->d; 39 } 40 int mem; 41 Node *getnode() 42 { 43 Node *t=nds+(mem++); 44 t->ch[0]=t->ch[1]=t->fa=0; 45 t->d1=t->d2=t->d=0;t->sz=1;t->r=rand1(); 46 return t; 47 } 48 Node *merge(Node *a,Node *b) 49 { 50 if(!a) return b; 51 if(!b) return a; 52 if(a->r<b->r) 53 { 54 a->ch[1]=merge(a->ch[1],b);upd(a); 55 if(a->ch[1]) a->ch[1]->fa=a; 56 return a; 57 } 58 else 59 { 60 b->ch[0]=merge(a,b->ch[0]);upd(b); 61 if(b->ch[0]) b->ch[0]->fa=b; 62 return b; 63 } 64 } 65 typedef pair<Node*,Node*> pnn; 66 pnn split(Node *a,int k) 67 { 68 if(!a) return pnn(0,0); 69 if(!k) return pnn(0,a); 70 int ls=gsz(a->ch[0]);pnn y; 71 if(k<=ls) 72 { 73 //if(a->ch[0]) a->ch[0]->fa=0; 74 y=split(a->ch[0],k); 75 a->ch[0]=y.se; 76 if(a->ch[0]) a->ch[0]->fa=a; 77 upd(a);y.se=a; 78 } 79 else 80 { 81 //if(a->ch[1]) a->ch[1]->fa=0; 82 y=split(a->ch[1],k-ls-1); 83 a->ch[1]=y.fi; 84 if(a->ch[1]) a->ch[1]->fa=a; 85 upd(a);y.fi=a; 86 } 87 return y; 88 } 89 void insert(Node *&a,int p,int x) 90 { 91 pnn t1=split(a,p-1);Node *t=getnode(); 92 t->d=x;upd(t); 93 a=merge(merge(t1.fi,t),t1.se); 94 } 95 void remove(Node *&a,int p) 96 { 97 pnn t1=split(a,p-1),t2=split(t1.se,1); 98 a=merge(t1.fi,t2.se); 99 } 100 int find1(Node *&a,int k) 101 { 102 pnn t1=split(a,k);int ans=t1.fi->d1; 103 a=merge(t1.fi,t1.se); 104 return ans; 105 } 106 int find2(Node *a,int k) 107 { 108 if(!a) return 0; 109 int ls=gd2(a->ch[0]); 110 if(k<ls) return find2(a->ch[0],k); 111 else if(k<ls+(a->d<0)) return gsz(a->ch[0]); 112 else return gsz(a->ch[0])+1+find2(a->ch[1],k-ls-(a->d<0)); 113 } 114 int find3(Node *&a,int k) 115 { 116 pnn t1=split(a,k);int ans=t1.fi->d2; 117 a=merge(t1.fi,t1.se); 118 return ans; 119 } 120 ll qsum(Node *&a,int l,int r) 121 {if(l>r) swap(l,r); 122 pnn t1=split(a,l-1),t2=split(t1.se,r-l+1); 123 ll ans=t2.fi->sum; 124 a=merge(t1.fi,merge(t2.fi,t2.se)); 125 return ans; 126 } 127 Node *rt; 128 Node *find(Node *a,int k) 129 { 130 if(!a) return 0; 131 int ls=gsz(a->ch[0]); 132 if(k<=ls) return find(a->ch[0],k); 133 else if(k<=ls+1) return a; 134 else return find(a->ch[1],k-ls-1); 135 } 136 bool gson(Node *a) {return a==a->fa->ch[1];} 137 int findx(Node *a) 138 { 139 int ans=gsz(a->ch[0])+1; 140 for(;a!=rt;a=a->fa) 141 if(gson(a)) 142 ans+=gsz(a->fa->ch[0])+1; 143 return ans; 144 } 145 } 146 set<int> s; 147 char tmp[10]; 148 int n; 149 using S::rt; 150 S::pnn ttt[200100]; 151 int main() 152 { 153 int p,x,i,p1,TT=0; 154 while(scanf("%d",&n)==1) 155 { 156 S::mem=0; 157 s.clear();rt=0; 158 for(i=1;i<=n;i++) s.insert(i); 159 ++TT; 160 printf("Case #%d: ",TT); 161 for(i=1;i<=n;i++) 162 { 163 scanf("%s",tmp); 164 if(tmp[0]=='i') 165 { 166 scanf("%d",&p);p++; 167 x=*s.begin();s.erase(x); 168 S::insert(rt,p,x);ttt[x].fi=S::find(rt,p); 169 //printf("a%d %d ",p,x); 170 //printf("%d ",S::findx(ttt[x].fi)); 171 p1=S::find1(rt,p);p=S::find2(rt,p1-1)+1; 172 S::insert(rt,p,-x);ttt[x].se=S::find(rt,p); 173 //printf("a%d %d ",p,-x); 174 } 175 else if(tmp[0]=='r') 176 { 177 scanf("%d",&x); 178 //printf("b%d ",S::findx(ttt[x].fi)); 179 S::remove(rt,S::findx(ttt[x].fi)); 180 //printf("%d ",S::findx(ttt[x].se)); 181 S::remove(rt,S::findx(ttt[x].se)); 182 s.insert(x); 183 } 184 else 185 { 186 scanf("%d",&x); 187 printf("%lld ",S::qsum(rt,S::findx(ttt[x].fi),S::findx(ttt[x].se))); 188 } 189 } 190 } 191 return 0; 192 }