NOI2005维修数列

剧恶心的splay……

为什么在bzoj上是超时,在自己的电脑上测的是栈溢出……

  1 const maxn=500010;
  2       maxc=11000;
  3 var
  4   n,m,i,j,y,root,x,posi,t,head:longint;
  5   ch:char;
  6   op:array[0..maxn] of boolean;
  7   l,r,a,next,c,mc,s,ma,mb,sum:array[0..maxn] of longint;
  8 function max(x,y:longint):longint;
  9  begin
 10   if x>y then exit(x) else exit(y);
 11  end;
 12 procedure update(x:longint);
 13  var ls,rs:longint;
 14    begin
 15     ls:=l[x];rs:=r[x];
 16     sum[x]:=sum[ls]+sum[rs]+a[x];
 17     ma[x]:=max(ma[ls],sum[ls]+ma[rs]+a[x]);
 18     mb[x]:=max(mb[rs],sum[rs]+mb[ls]+a[x]);
 19     mc[x]:=max(mc[ls],mc[rs]);
 20     mc[x]:=max(mc[x],mb[ls]+ma[rs]+a[x]);
 21     s[x]:=s[ls]+s[rs]+1;
 22    end;
 23 procedure r_rotate(var x:longint);
 24  var y:longint;
 25   begin
 26    y:=l[x];l[x]:=r[y];r[y]:=x;update(x);x:=y;
 27   end;
 28 procedure l_rotate(var x:longint);
 29  var y:longint;
 30   begin
 31    y:=r[x];r[x]:=l[y];l[y]:=x;update(x);x:=y;
 32   end;
 33 procedure put(x,y:longint;flag:boolean);
 34  var tmp:longint;
 35    begin
 36     if x=0 then exit;
 37     if flag then
 38      begin
 39       tmp:=l[x];l[x]:=r[x];r[x]:=tmp;
 40       tmp:=ma[x];ma[x]:=mb[x];mb[x]:=tmp;
 41       op[x]:=not op[x];
 42      end;
 43     if y<1001 then
 44      begin
 45       c[x]:=y;a[x]:=y;sum[x]:=s[x]*y;
 46       mc[x]:=max(0,sum[x]);
 47       ma[x]:=mc[x];mb[x]:=mc[x];
 48       if mc[x]=0 then mc[x]:=y;
 49      end;
 50    end;
 51 procedure splay(var x:longint;y:longint);
 52  var tmp:longint;
 53    begin
 54     put(l[x],c[x],op[x]);put(r[x],c[x],op[x]);
 55     op[x]:=false;c[x]:=maxc;tmp:=s[l[x]]+1;
 56     if y=tmp then exit;
 57     if y>tmp then
 58       begin
 59        splay(r[x],y-tmp);l_rotate(x);
 60       end
 61     else
 62       begin
 63        splay(l[x],y);r_rotate(x);
 64       end;
 65    end;
 66 procedure build(var x:longint;lx,rx:longint);
 67  begin
 68   if lx>rx then exit;
 69   x:=(lx+rx)>>1;
 70   build(l[x],lx,x-1);
 71   build(r[x],x+1,rx);
 72   update(x);
 73  end;
 74 procedure release(x:longint);
 75  begin
 76   if x=0 then exit;
 77   next[x]:=head;head:=x;
 78   release(l[x]);release(r[x]);
 79  end;
 80 procedure init;
 81  begin
 82   readln(n,m);root:=0;
 83   fillchar(c,sizeof(c),1);
 84   a[1]:=-10000;a[n+2]:=-10000;mc[0]:=-10000;
 85   for i:=2 to n+1 do read(a[i]);readln;
 86   head:=n+3;
 87   for i:=head+1 to maxn do next[i-1]:=i;
 88   build(root,1,n+2);
 89  end;
 90 procedure main;
 91  begin
 92   for m:=1 to m do
 93    begin
 94     read(ch);
 95     case ch of
 96     'I':begin
 97          while ch<>' ' do read(ch);
 98          read(posi,t);
 99          splay(root,posi+2);
100          for t:=1 to t do
101            begin
102             read(y);x:=head;head:=next[head];
103             a[x]:=y;l[x]:=l[root];l[root]:=x;
104             r[x]:=0;c[x]:=maxc;op[x]:=false;
105             update(x);
106            end;
107          update(root);
108         end;
109     'D':begin
110          while ch<>' ' do read(ch);
111          read(posi,t);
112          splay(root,posi);
113          splay(root,posi+t+1);
114          release(r[l[root]]);r[l[root]]:=0;
115          update(l[root]);update(root);
116         end;
117     'M':begin
118          read(ch);read(ch);
119          case ch of
120          'K':begin
121               while ch<>' ' do read(ch);
122               read(posi,t,x);
123               splay(root,posi);
124               splay(root,posi+t+1);
125               put(r[l[root]],x,false);
126               update(l[root]);update(root);
127              end;
128          'X':writeln(mc[root]);
129          end;
130         end;
131     'R':begin
132          while ch<>' ' do read(ch);
133          read(posi,t);
134          splay(root,posi);
135          splay(root,posi+t+1);
136          put(r[l[root]],maxc,true);
137          update(l[root]);update(root);
138         end;
139     'G':begin
140          while ch<>' ' do read(ch);
141          read(posi,t);
142          splay(root,posi);
143          splay(root,posi+t+1);
144          update(root);
145          writeln(sum[r[l[root]]]);
146         end;
147     end;
148     readln;
149    end;
150   end;
151 begin
152   init;
153   main;
154 end.       
View Code

声亦香的题解:http://blog.sina.com.cn/s/blog_86942b1401016dhb.html

可是他的两个程序交上去也是一样的结果……

原文地址:https://www.cnblogs.com/zyfzyf/p/3788405.html