[bzoj1500][luogu2042][cogs339][codevs1758]维修数列(维护数列)

先给自己立一个flag

我希望上午能写完

再立一个flag

我希望下午能写完。

再立一个flag

我希望晚上能写完。。。

 我终于A了。。。

6700+ms...(6728)

我成功地立了3个flag。。。

  1 // It is made by XZZ
  2 #include<cstdio>
  3 #include<algorithm>
  4 #include<ctime>
  5 #include<cstdlib>
  6 using namespace std;
  7 #define rep(a,b,c) for(rg int a=b;a<=c;a++)
  8 #define drep(a,b,c) for(rg int a=b;a>=c;a--)
  9 #define erep(a,b) for(rg int a=fir[b];a;a=nxt[a])
 10 #define il inline
 11 #define rg register
 12 #define vd void
 13 #define mp make_pair
 14 typedef long long ll;
 15 typedef pair<int,int> pr;
 16 il int gi(){
 17     rg int x=0,f=1;rg char ch=getchar();
 18     while(ch<'0'||ch>'9')f=ch=='-'?-1:f,ch=getchar();
 19     while(ch>='0'&&ch<='9')x=x*10+ch-'0',ch=getchar();
 20     return x*f;
 21 }
 22 #define Now t[now]
 23 struct node{
 24     int ls,rs,rand,size;
 25     int data,sum,lmax,rmax,maxx;
 26     bool rev,set;
 27     int setnum;
 28     il vd setdata(){data=setnum,sum=setnum*size,lmax=rmax=maxx=data>0?sum:data,set=0;}
 29 }t[500002];
 30 int stack[500002],top;
 31 int root;
 32 il int newnode(int data){
 33     int now=stack[top];
 34     Now.setnum=data,Now.setdata();
 35     Now.rev=Now.set=0;
 36     Now.ls=Now.rs=0;
 37     Now.size=1;
 38     return stack[top--];
 39 }
 40 il vd down(int now){
 41     if(Now.rev){
 42         swap(Now.ls,Now.rs),swap(Now.lmax,Now.rmax);
 43         t[Now.ls].rev^=1,t[Now.rs].rev^=1,Now.rev=0;
 44     }
 45     if(Now.set){
 46         Now.setdata();
 47         t[Now.ls].set=1,t[Now.ls].setnum=Now.setnum;
 48         t[Now.rs].set=1,t[Now.rs].setnum=Now.setnum;
 49     }
 50 }
 51 il vd reset(int now){
 52     if(Now.ls)down(Now.ls);
 53     if(Now.rs)down(Now.rs);
 54     Now.size=t[Now.ls].size+t[Now.rs].size+1;
 55     Now.sum=t[Now.ls].sum+t[Now.rs].sum+Now.data;
 56     Now.lmax=t[Now.ls].sum+Now.data+max(t[Now.rs].lmax,0);
 57     Now.rmax=t[Now.rs].sum+Now.data+max(t[Now.ls].rmax,0);
 58     if(Now.ls)Now.lmax=max(Now.lmax,t[Now.ls].lmax);
 59     if(Now.rs)Now.rmax=max(Now.rmax,t[Now.rs].rmax);
 60     Now.maxx=max(t[Now.ls].rmax,0)+max(t[Now.rs].lmax,0)+Now.data;
 61     if(Now.ls)Now.maxx=max(t[Now.ls].maxx,Now.maxx);
 62     if(Now.rs)Now.maxx=max(Now.maxx,t[Now.rs].maxx);
 63 }
 64 il int merge(int a,int b){
 65     if(!a||!b)return a|b;
 66     if(t[a].rand<t[b].rand){down(a),t[a].rs=merge(t[a].rs,b),reset(a);return a;}
 67     else {down(b),t[b].ls=merge(a,t[b].ls),reset(b);return b;}
 68 }
 69 il pr split(int now,int num){
 70     if(!now)return mp(0,0);
 71     down(now);
 72     int ls=Now.ls,rs=Now.rs;
 73     if(num==t[Now.ls].size){Now.ls=0,reset(now);return mp(ls,now);}
 74     if(num==t[Now.ls].size+1){Now.rs=0,reset(now);return mp(now,rs);}
 75     if(num<t[Now.ls].size){
 76         pr T=split(Now.ls,num);
 77         Now.ls=T.second,reset(now);
 78         return mp(T.first,now);
 79     }else{
 80         pr T=split(Now.rs,num-t[Now.ls].size-1);
 81         Now.rs=T.first,reset(now);
 82         return mp(now,T.second);
 83     }
 84 }
 85 il int build(int n){
 86     int last;
 87     int stk[n],tp=0;
 88     rep(i,1,n){
 89         int now=newnode(gi());last=0;
 90         while(tp&&t[stk[tp]].rand>Now.rand)reset(stk[tp]),last=stk[tp--];
 91         if(tp)t[stk[tp]].rs=now;
 92         Now.ls=last;
 93         stk[++tp]=now;
 94     }
 95     while(tp)reset(stk[tp--]);
 96     return stk[1];
 97 }
 98 il vd rec(int now){
 99     if(!now)return;
100     rec(Now.ls),rec(Now.rs),stack[++top]=now;
101 }
102 int main(){
103     int n=gi(),m=gi();char opt[10];
104     rep(i,1,500000)stack[500000-i+1]=i;
105     top=500000;
106     srand(23336666);
107     rep(i,1,500000)t[i].rand=i;
108     rep(i,1,500000)swap(t[rand()%500000+1].rand,t[rand()%500000+1].rand);
109     root=build(n);
110     rep(i,1,m){
111         scanf("%s",opt);
112         if(opt[2]=='X')printf("%d\n",t[root].maxx);
113         else if(opt[2]=='S'){
114             pr T=split(root,gi());
115             root=merge(merge(T.first,build(gi())),T.second);
116         }else{
117             pr T=split(root,gi()-1),TT=split(T.second,gi());
118             if(opt[2]=='K')t[TT.first].set=1,t[TT.first].setnum=gi();
119             else if(opt[2]=='V')t[TT.first].rev^=1;
120             else if(opt[2]=='T')printf("%d\n",t[TT.first].sum);
121             if(opt[2]!='L')root=merge(T.first,merge(TT.first,TT.second));
122             else root=merge(T.first,TT.second),rec(TT.first);
123         }
124     }
125     return 0;
126 }
127 /*
128 9 8
129 2 -6 3 5 1 -5 -3 6 3 
130 GET-SUM 5 4
131 MAX-SUM
132 INSERT 8 3 -5 7 2
133 DELETE 12 1
134 MAKE-SAME 3 3 2
135 REVERSE 3 6
136 GET-SUM 5 4
137 MAX-SUM
138 */
View Code

解题报告请看这里

posted @ 2017-08-01 08:51  菜狗xzz  阅读(329)  评论(0编辑  收藏  举报