这里大部分操作都和poj3580没有本质的区别,稍微有变化的是插入
插入一段区间可以先把插入部分建成平衡树,然后把pos旋到根,pos+1旋到根节点下面
然后插入到pos+1的左子树即可
注意这题会爆空间,要垃圾回收
我的程序又喜闻乐见的TLE了,求开O2,实在搞不动
1 const inf=-200000007; 2 var son:array[-1..600010,1..2] of longint; 3 fa,lmax,rmax,maxx,cov,size,sum,a,q:array[-1..600010] of longint; 4 rev:array[-1..600010] of boolean; 5 st:array[0..4000010] of longint; 6 s,j,root,head,tail,x,y,i,n,m,c,t:longint; 7 ch:char; 8 sc:string; 9 10 procedure swap(var a,b:longint); 11 var c:longint; 12 begin 13 c:=a; 14 a:=b; 15 b:=c; 16 end; 17 18 function max(a,b:longint):longint; 19 begin 20 if a>b then exit(a) else exit(b); 21 end; 22 23 procedure update(x:longint); 24 var l,r:longint; 25 begin 26 l:=son[x,1]; r:=son[x,2]; 27 size[x]:=size[l]+size[r]+1; 28 sum[x]:=sum[l]+sum[r]+a[x]; 29 lmax[x]:=max(max(lmax[l],sum[l]+a[x]),sum[l]+a[x]+lmax[r]); 30 rmax[x]:=max(max(rmax[r],sum[r]+a[x]),sum[r]+a[x]+rmax[l]); 31 maxx[x]:=max(max(maxx[l],maxx[r]),max(max(rmax[l]+a[x],rmax[l]+a[x]+lmax[r]),max(a[x],a[x]+lmax[r]))); 32 end; 33 34 procedure reverse(x:longint); 35 begin 36 rev[x]:=not rev[x]; 37 swap(lmax[x],rmax[x]); 38 end; 39 40 procedure same(x,c:longint); 41 begin 42 cov[x]:=c; a[x]:=c; 43 sum[x]:=c*size[x]; 44 if c>0 then 45 begin 46 lmax[x]:=sum[x]; 47 rmax[x]:=sum[x]; 48 maxx[x]:=sum[x]; 49 end 50 else begin 51 lmax[x]:=c; 52 rmax[x]:=c; 53 maxx[x]:=c; 54 end; 55 end; 56 57 procedure push(x:longint); 58 begin 59 if rev[x] then 60 begin 61 if son[x,1]<>-1 then reverse(son[x,1]); 62 if son[x,2]<>-1 then reverse(son[x,2]); 63 swap(son[x,1],son[x,2]); 64 rev[x]:=false; 65 end; 66 if cov[x]<>inf then 67 begin 68 if son[x,1]<>-1 then same(son[x,1],cov[x]); 69 if son[x,2]<>-1 then same(son[x,2],cov[x]); 70 cov[x]:=inf; 71 end; 72 end; 73 74 function find(k:longint):longint; 75 var p:longint; 76 begin 77 p:=root; 78 while true do 79 begin 80 push(p); 81 if size[son[p,1]]+1=k then exit(p); 82 if size[son[p,1]]+1>k then p:=son[p,1] 83 else begin 84 k:=k-size[son[p,1]]-1; 85 p:=son[p,2]; 86 end; 87 end; 88 end; 89 90 procedure rotate(x,w:longint); 91 var y:longint; 92 begin 93 y:=fa[x]; 94 if fa[y]<>-1 then 95 begin 96 if son[fa[y],1]=y then son[fa[y],1]:=x 97 else son[fa[y],2]:=x; 98 end 99 else root:=x; 100 fa[x]:=fa[y]; 101 son[y,3-w]:=son[x,w]; 102 if son[x,w]<>-1 then fa[son[x,w]]:=y; 103 son[x,w]:=y; 104 fa[y]:=x; 105 update(y); 106 end; 107 108 procedure splay(x,f:longint); 109 var i,t,y:longint; 110 begin 111 if fa[x]=f then exit; 112 push(x); 113 while fa[x]<>f do 114 begin 115 y:=fa[x]; 116 if fa[y]=f then 117 begin 118 if son[y,1]=x then rotate(x,2) 119 else rotate(x,1); 120 end 121 else begin 122 if son[fa[y],1]=y then 123 begin 124 if son[y,1]=x then rotate(y,2) 125 else rotate(x,1); 126 rotate(x,2); 127 end 128 else begin 129 if son[y,1]=x then rotate(x,2) 130 else rotate(y,1); 131 rotate(x,1); 132 end; 133 end; 134 end; 135 update(x); 136 end; 137 138 function range(x,y:longint):longint; 139 begin 140 x:=find(x); 141 splay(x,-1); 142 y:=find(y+2); 143 splay(y,x); 144 exit(y); 145 end; 146 147 procedure delete(x,y:longint); 148 var p:longint; 149 begin 150 p:=range(x,y); 151 if son[p,1]<>-1 then 152 begin 153 inc(tail); 154 st[tail]:=son[p,1]; 155 end; 156 fa[son[p,1]]:=-1; 157 son[p,1]:=-1; 158 update(p); 159 update(root); 160 end; 161 162 procedure change(x,y:longint); 163 var p:longint; 164 begin 165 p:=range(x,y); 166 reverse(son[p,1]); 167 update(p); 168 update(root); 169 end; 170 171 procedure make(x,y,c:longint); 172 var p:longint; 173 begin 174 p:=range(x,y); 175 same(son[p,1],c); 176 update(p); 177 update(root); 178 end; 179 180 function ask(x,y:longint):longint; 181 var p:longint; 182 begin 183 p:=range(x,y); 184 exit(sum[son[p,1]]); 185 end; 186 187 function recycle:longint; 188 var i:longint; 189 begin 190 if head>=tail then 191 begin 192 head:=0; 193 tail:=0; 194 inc(t); 195 exit(t); 196 end 197 else begin 198 inc(head); 199 for i:=1 to 2 do 200 begin 201 if son[st[head],i]<>-1 then 202 begin 203 inc(tail); 204 st[tail]:=son[st[head],i]; 205 end; 206 son[st[head],i]:=-1; 207 end; 208 fa[st[head]]:=-1; 209 rev[st[head]]:=false; 210 exit(st[head]); 211 end; 212 end; 213 214 function build(l,r:longint):longint; 215 var m,p:longint; 216 begin 217 p:=recycle; 218 m:=(l+r) shr 1; 219 a[p]:=q[m]; cov[p]:=inf; 220 if l<m then 221 begin 222 son[p,1]:=build(l,m-1); 223 fa[son[p,1]]:=p; 224 end; 225 if m<r then 226 begin 227 son[p,2]:=build(m+1,r); 228 fa[son[p,2]]:=p; 229 end; 230 update(p); 231 exit(p); 232 end; 233 234 begin 235 readln(n,m); 236 fillchar(fa,sizeof(fa),255); 237 fillchar(son,sizeof(son),255); 238 maxx[-1]:=inf; lmax[-1]:=inf; rmax[-1]:=inf; 239 a[-1]:=inf; 240 for i:=1 to n do 241 read(q[i]); 242 q[0]:=inf; q[n+1]:=inf; 243 readln; 244 t:=-1; 245 root:=build(0,n+1); 246 for i:=1 to m do 247 begin 248 sc:=''; 249 read(ch); 250 while ch<>' ' do 251 begin 252 sc:=sc+ch; 253 if sc='MAX-SUM' then break; 254 read(ch); 255 end; 256 if sc='INSERT' then 257 begin 258 read(x,s); 259 for j:=1 to s do 260 read(q[j]); 261 c:=build(1,s); 262 y:=find(x+2); 263 x:=find(x+1); 264 splay(x,-1); 265 splay(y,x); 266 son[y,1]:=c; 267 fa[c]:=y; 268 splay(c,-1); 269 readln; 270 end 271 else if sc='DELETE' then 272 begin 273 readln(x,y); 274 y:=x+y-1; 275 delete(x,y); 276 end 277 else if sc='MAKE-SAME' then 278 begin 279 readln(x,y,c); 280 y:=x+y-1; 281 make(x,y,c); 282 end 283 else if sc='REVERSE' then 284 begin 285 readln(x,y); 286 y:=x+y-1; 287 change(x,y); 288 end 289 else if sc='GET-SUM' then 290 begin 291 readln(x,y); 292 y:=x+y-1; 293 writeln(ask(x,y)); 294 end 295 else begin 296 writeln(maxx[root]); 297 readln; 298 end; 299 end; 300 end.
UPD:转了c++就过了……,顺便我发现c++读入优化对于大数据能快不少……
1 #include<iostream> 2 #include<cstdio> 3 #include<stdlib.h> 4 #include<cstring> 5 #define N 600010 6 using namespace std; 7 const int inf=200000007; 8 bool rev[N]; 9 int q[4000010],cov[N],lm[N],rm[N],mx[N],fa[N],a[N],b[N],c[N],s[N],son[N][2]; 10 int root,n,m,tot,pos,t,h,r; 11 int read() 12 { 13 int x=0,f=1;char ch=getchar(); 14 while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();} 15 while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();} 16 return x*f; 17 } 18 void mid(int x) 19 { 20 if (son[x][0]) mid(son[x][0]); 21 cout <<a[x]<<" "; 22 if (son[x][1]) mid(son[x][1]); 23 } 24 25 void update(int x) 26 { 27 int l=son[x][0],r=son[x][1]; 28 c[x]=c[l]+c[r]+1; 29 s[x]=s[l]+s[r]+a[x]; 30 lm[x]=max(lm[l],s[l]+max(a[x],a[x]+lm[r])); 31 rm[x]=max(rm[r],s[r]+max(a[x],a[x]+rm[l])); 32 mx[x]=max(max(mx[r],mx[l]),a[x]+max(max(lm[r],rm[l]),max(0,lm[r]+rm[l]))); 33 } 34 35 void reverse(int x) 36 { 37 rev[x]^=1; 38 swap(lm[x],rm[x]); 39 } 40 void same(int x,int w) 41 { 42 cov[x]=a[x]=w; s[x]=c[x]*w; 43 if (w>0) lm[x]=rm[x]=mx[x]=s[x]; 44 else lm[x]=rm[x]=mx[x]=w; 45 } 46 47 void push(int x) 48 { 49 if (rev[x]) 50 { 51 if (son[x][0]) reverse(son[x][0]); 52 if (son[x][1]) reverse(son[x][1]); 53 swap(son[x][0],son[x][1]); 54 rev[x]^=1; 55 } 56 if (cov[x]!=-inf) 57 { 58 if (son[x][0]) same(son[x][0],cov[x]); 59 if (son[x][1]) same(son[x][1],cov[x]); 60 cov[x]=-inf; 61 } 62 } 63 64 int rotate(int x,int w) 65 { 66 int y=fa[x]; 67 if (fa[y]) 68 { 69 if (son[fa[y]][0]==y) son[fa[y]][0]=x; 70 else son[fa[y]][1]=x; 71 } 72 else root=x; 73 fa[x]=fa[y]; 74 son[y][w^1]=son[x][w]; if (son[x][w]) fa[son[x][w]]=y; 75 son[x][w]=y; fa[y]=x; 76 update(y); 77 } 78 79 int recycle() 80 { 81 if (h>=r) 82 { 83 h=r=0; 84 return ++t; 85 } 86 else { 87 h++; 88 for (int i=0;i<=1;i++) 89 if (son[q[h]][i]) 90 { 91 q[++r]=son[q[h]][i]; 92 son[q[h]][i]=0; 93 } 94 fa[q[h]]=0; 95 return q[h]; 96 } 97 } 98 99 int build(int l,int r) 100 { 101 int p=recycle(),m=(l+r)>>1; 102 a[p]=b[m]; cov[p]=-inf; rev[p]=0; 103 if (l<m) 104 { 105 son[p][0]=build(l,m-1); 106 fa[son[p][0]]=p; 107 } 108 if (m<r) 109 { 110 son[p][1]=build(m+1,r); 111 fa[son[p][1]]=p; 112 } 113 update(p); return p; 114 } 115 116 void splay(int x,int f) 117 { 118 push(x); 119 while (fa[x]!=f) 120 { 121 int y=fa[x]; 122 if (fa[y]==f) 123 if (son[y][0]==x) rotate(x,1);else rotate(x,0); 124 else { 125 if (son[fa[y]][0]==y) 126 { 127 if (son[y][0]==x) rotate(y,1); else rotate(x,0); 128 rotate(x,1); 129 } 130 else { 131 if (son[y][0]==x) rotate(x,1); else rotate(y,0); 132 rotate(x,0); 133 } 134 } 135 } 136 update(x); 137 } 138 139 int find(int p,int k) 140 { 141 if (p==0) return 0; 142 push(p); 143 if (k==c[son[p][0]]+1) return p; 144 else if (k>c[son[p][0]]+1) return find(son[p][1],k-c[son[p][0]]-1); 145 else return find(son[p][0],k); 146 } 147 148 int range(int l,int r) 149 { 150 int x=find(root,l),y=find(root,r+2); 151 splay(x,0); splay(y,x); 152 return y; 153 } 154 155 void insert() 156 { 157 for (int i=1; i<=tot;i++) 158 b[i]=read(); 159 int r0=build(1,tot); 160 int x=find(root,pos+1),y=find(root,pos+2); 161 splay(x,0); splay(y,x); 162 son[y][0]=r0; fa[r0]=y; 163 update(y);update(x); 164 } 165 166 void del() 167 { 168 int wh=range(pos,pos+tot-1); 169 q[++r]=son[wh][0]; 170 fa[son[wh][0]]=0; son[wh][0]=0; 171 update(wh); update(root); 172 } 173 174 void make(int w) 175 { 176 int wh=range(pos,pos+tot-1); 177 same(son[wh][0],w); 178 update(wh);update(root); 179 } 180 181 void change() 182 { 183 int wh=range(pos,pos+tot-1); 184 reverse(son[wh][0]); 185 update(wh);update(root); 186 } 187 188 int sum() 189 { 190 int wh=range(pos,pos+tot-1); 191 return s[son[wh][0]]; 192 } 193 194 int main() 195 { 196 n=read();m=read(); 197 b[1]=b[n+2]=a[0]=mx[0]=lm[0]=rm[0]=-inf; 198 for (int i=1; i<=n; i++) 199 b[i+1]=read(); 200 root=build(1,n+2); 201 char ch[10]; 202 while (m--) 203 { 204 scanf("%s",ch); 205 if (ch[0]=='M'&&ch[2]=='X') 206 { 207 printf("%d ",mx[root]); 208 continue; 209 } 210 pos=read();tot=read(); 211 if (ch[0]=='I') insert(); 212 if (ch[0]=='D') del(); 213 if (ch[0]=='M') 214 { 215 int w; w=read(); 216 make(w); 217 } 218 if (ch[0]=='R') change(); 219 if (ch[0]=='G') printf("%d ",sum()); 220 } 221 system("pause"); 222 return 0; 223 }