再写一次毒瘤题,改变代码风格改用了指针,以为会不太适应,事实上不会撒。。

对拍数据要等20分钟才出来= =

  1 //#include<bits/stdc++.h>
  2 #include<cstdio>
  3 #include<cstring>
  4 #include<algorithm>
  5 #include<iostream>
  6 #include<queue>
  7 #define inc(i,l,r) for(int i=l;i<=r;i++)
  8 #define dec(i,l,r) for(int i=l;i>=r;i--)
  9 #define link(x) for(edge *j=h[x];j;j=j->next)
 10 #define mem(a) memset(a,0,sizeof(a))
 11 #define inf 10000
 12 #define ll long long
 13 #define succ(x) (1<<x)
 14 #define lowbit(x) (x&(-x))
 15 #define NM 1000000+5
 16 using namespace std;
 17 int read(){
 18     int x=0,f=1;char ch=getchar();
 19     while(!isdigit(ch)){if(ch=='-')f=-1;ch=getchar();}
 20     while(isdigit(ch))x=x*10+ch-'0',ch=getchar();
 21     return x*f;
 22 }
 23 struct node* null;
 24 struct node{
 25     int s,sm,lm,rm,tag,rev,size,v;
 26     node *c[2],*f;
 27     void clr(){
 28         s=sm=lm=rm=v=size=rev=0;c[0]=c[1]=0;tag=inf;
 29     }
 30     void in(int x){
 31         v=s=sm=lm=rm=x;
 32     }
 33     int d(){return f->c[1]==this;}
 34     void setc(node *r,int d){
 35         c[d]=r;r->f=this;
 36     }
 37     void Rev(){rev^=1;swap(c[0],c[1]);swap(lm,rm);}
 38     void Tag(int x){if(this==null)return;v=tag=x;lm=rm=sm=s=size*tag;if(tag<0)lm=rm=sm=tag;}
 39     void push(){
 40         if(tag<inf){
 41             inc(i,0,1)if(c[i])c[i]->Tag(tag);
 42             tag=inf;rev=0;
 43         }
 44         if(rev){
 45             inc(i,0,1)if(c[i])c[i]->Rev();
 46             rev=0;
 47         }
 48     }
 49     void upd(){
 50         node *l=c[0];node*r=c[1];
 51         s=l->s+r->s+v;
 52         sm=max(l->sm,r->sm);
 53         sm=max(sm,max(l->rm,0)+max(r->lm,0)+v);
 54         lm=max(l->lm,l->s+v+max(r->lm,0));
 55         rm=max(r->rm,r->s+v+max(l->rm,0));
 56         size=l->size+r->size+1;
 57     }
 58 }T[NM],*o=T,*root;
 59 int n,m,_x,_y,_t;
 60 queue<node*>q;
 61 char st[20];
 62 node* newnode(){
 63     node *r;
 64     if(q.empty())r=o++;else{
 65         r=q.front();q.pop();r->push();
 66         inc(i,0,1)if(r->c[i]!=null)q.push(r->c[i]);
 67     }
 68     r->clr();r->size=1;r->c[0]=r->c[1]=r->f=null;return r;
 69 }
 70 node *build(int x,int y){
 71     if(x>y)return null;
 72     int t=x+y>>1;node *r=newnode();
 73     if(x==y){
 74         r->in(read());
 75         return r;
 76     }
 77     r->setc(build(x,t-1),0);
 78     r->in(read());
 79     r->setc(build(t+1,y),1);
 80     r->upd();
 81     return r;
 82 }
 83 void rot(node *x){
 84     node *y=x->f;int d=!x->d();
 85     y->push();x->push();
 86     y->f->setc(x,y->d());
 87     y->setc(x->c[d],!d);
 88     x->setc(y,d);
 89     y->upd();
 90 }
 91 void splay(node *x,node *f=null){
 92     for(node *y=x->f;y!=f;y=x->f){
 93         if(y->f!=f)x->d()==y->d()?rot(y):rot(x);
 94         rot(x);
 95     }
 96     x->upd();
 97     if(f==null)root=x;
 98 }
 99 node *find(int x){
100     for(node *r=root;x;r->push())
101     if(x<=r->c[0]->size)r=r->c[0];
102     else{
103         x-=r->c[0]->size;x--;
104         if(!x)return r;
105         r=r->c[1];
106     }
107 }
108 node *pick(int x,int y){
109     node *r=find(x);node *t=find(y);
110     splay(r);splay(t,r);
111     return t;
112 }
113 void out(node *r){
114     if(r==null)return;r->push();
115     out(r->c[0]);printf("%d ",r->v);out(r->c[1]);
116 }
117 int main(){
118 //    freopen("data.in","r",stdin);
119 //    freopen("test.out","w",stdout);
120     null=o++;null->clr();null->in(-inf);null->v=null->s=0;
121     root=newnode();root->setc(newnode(),1);
122     root->in(-inf);root->c[1]->in(-inf);
123     root->upd();
124     n=read();m=read();
125     root->c[1]->setc(build(1,n),0);splay(root->c[1]->c[0]);
126     while(m--){
127         scanf("%s",st);
128         if(st[2]=='X')printf("%d\n",root->sm);
129         else if(st[2]=='S'){
130             _x=read();_t=read();
131             node *r=pick(_x+1,_x+2);
132             r->setc(build(1,_t),0);
133             splay(r->c[0]);
134         }else{
135             _x=read();_y=_x+1+read();
136             node *r=pick(_x,_y);
137             if(st[2]=='L'){
138                 if(r->c[0]!=null)q.push(r->c[0]);
139                 r->c[0]->f=null;r->c[0]=null;splay(r);
140             }else if(st[2]=='K') r->c[0]->Tag(read()),splay(r->c[0]);
141             else if(st[2]=='V') r->c[0]->Rev(),splay(r->c[0]);
142             else printf("%d\n",r->c[0]->s);
143         }
144 //        out(root);putchar('\n');
145     }
146     return 0;
147 }
View Code

 

posted on 2016-03-08 20:45  onlyRP  阅读(193)  评论(0编辑  收藏  举报