1500: [NOI2005]维修数列

Description

Input

输入的第1 行包含两个数N 和M(M ≤20 000),N 表示初始时数列中数的个数,M表示要进行的操作数目。
第2行包含N个数字,描述初始时的数列。
以下M行,每行一条命令,格式参见问题描述中的表格。
任何时刻数列中最多含有500 000个数,数列中任何一个数字均在[-1 000, 1 000]内。
插入的数字总数不超过4 000 000个,输入文件大小不超过20MBytes。

Output

对于输入数据中的GET-SUM和MAX-SUM操作,向输出文件依次打印结果,每个答案(数字)占一行。

Sample Input

9 8
2 -6 3 5 1 -5 -3 6 3
GET-SUM 5 4
MAX-SUM
INSERT 8 3 -5 7 2
DELETE 12 1
MAKE-SAME 3 3 2
REVERSE 3 6
GET-SUM 5 4
MAX-SUM

Sample Output

-1
10
1
10

HINT

 

 
模板题。。。
  1 #include<iostream>
  2 #include<cstdlib>
  3 #include<cmath>
  4 #include<cstring>
  5 #include<cstdio>
  6 #include<algorithm>
  7 #include<string>
  8 #include<map>
  9 #include<queue>
 10 #include<vector>
 11 #include<set>
 12 #define inf 1000000000
 13 #define maxn 1000000+5
 14 #define maxm 10000+5
 15 #define eps 1e-10
 16 #define ll long long
 17 #define for0(i,n) for(int i=0;i<=(n);i++)
 18 #define for1(i,n) for(int i=1;i<=(n);i++)
 19 #define for2(i,x,y) for(int i=(x);i<=(y);i++)
 20 #define for3(i,x,y) for(int i=(x);i>=(y);i--)
 21 #define for4(i,x) for(int i=head[x],y=e[i].go;i;i=e[i].next,y=e[i].go)
 22 using namespace std;
 23 int read(){
 24     int x=0,f=1;char ch=getchar();
 25     while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
 26     while(ch>='0'&&ch<='9'){x=10*x+ch-'0';ch=getchar();}
 27     return x*f;
 28 }
 29 int n,m,rt,cnt;
 30 int a[maxn],id[maxn],fa[maxn],c[maxn][2];
 31 int sum[maxn],s[maxn],v[maxn],mx[maxn],lx[maxn],rx[maxn];
 32 bool tag[maxn],rev[maxn];
 33 queue<int> q;
 34 void update(int x){
 35     int l=c[x][0],r=c[x][1];
 36     sum[x]=sum[l]+sum[r]+v[x];
 37     s[x]=s[l]+s[r]+1;
 38     mx[x]=max(mx[l],max(mx[r],rx[l]+v[x]+lx[r]));
 39     lx[x]=max(lx[l],sum[l]+v[x]+lx[r]);
 40     rx[x]=max(rx[r],sum[r]+v[x]+rx[l]);
 41 }
 42 void pushdown(int x){
 43     int l=c[x][0],r=c[x][1];
 44     if(tag[x]){
 45         rev[x]=tag[x]=0;
 46         if(l)tag[l]=1,v[l]=v[x],sum[l]=v[x]*s[l];
 47         if(r)tag[r]=1,v[r]=v[x],sum[r]=v[x]*s[r];
 48         if(v[x]>=0){
 49             if(l)lx[l]=rx[l]=mx[l]=sum[l];
 50             if(r)lx[r]=rx[r]=mx[r]=sum[r];
 51         }
 52         else{
 53             if(l)lx[l]=rx[l]=0,mx[l]=v[x];
 54             if(r)lx[r]=rx[r]=0,mx[r]=v[x];
 55         }
 56     }
 57     if(rev[x]){
 58         rev[x]^=1;rev[l]^=1;rev[r]^=1;
 59         swap(lx[l],rx[l]);swap(rx[r],lx[r]);
 60         swap(c[l][0],c[l][1]);swap(c[r][0],c[r][1]);
 61     }
 62 }
 63 void rotate(int x,int &k){
 64     int y=fa[x],z=fa[y],l=(c[y][1]==x),r=l^1;
 65     if(y==k)k=x;
 66     else c[z][(c[z][1]==y)]=x;
 67     fa[x]=z;fa[y]=x;fa[c[x][r]]=y;
 68     c[y][l]=c[x][r];c[x][r]=y;
 69     update(y);update(x);
 70 }
 71 void splay(int x,int &k){
 72     while(x!=k){
 73         int y=fa[x],z=fa[y];
 74         if(y!=k){
 75             if(c[y][0]==x^c[z][0]==y)rotate(x,k);
 76             else rotate(x,k);
 77         }
 78         else rotate(x,k);
 79     }
 80 }
 81 int find(int x,int rk){
 82     pushdown(x);
 83     int l=c[x][0],r=c[x][1];
 84     if(s[l]+1==rk)return x;
 85     if(s[l]>=rk)return find(l,rk);
 86     else return find(r,rk-s[l]-1);
 87 }
 88 void rec(int x){
 89     if(!x)return ;
 90     int l=c[x][0],r=c[x][1];
 91     rec(l);rec(r);q.push(x);
 92     fa[x]=c[x][0]=c[x][1]=0;
 93     tag[x]=rev[x]=0; 
 94 }
 95 int split(int k,int tot){
 96     int x=find(rt,k),y=find(rt,k+tot+1);
 97     splay(x,rt);splay(y,c[x][1]);
 98     return c[y][0];
 99 }
100 void query(int k,int tot){
101     int x=split(k,tot);
102     printf("%d\n",sum[x]);
103 }
104 void modify(int k,int tot,int val){
105     int x=split(k,tot),y=fa[x];
106     v[x]=val;tag[x]=1;sum[x]=s[x]*val;
107     if(val>=0)lx[x]=rx[x]=mx[x]=sum[x];
108     else lx[x]=rx[x]=0,mx[x]=val;
109     update(y);update(fa[y]);
110 }
111 void rever(int k,int tot){
112     int x=split(k,tot),y=fa[x];
113     if(!tag[x]){
114         rev[x]^=1;
115         swap(c[x][0],c[x][1]);
116         swap(lx[x],rx[x]);
117         update(y);update(fa[y]);
118     }
119 }
120 void erase(int k,int tot){
121     int x=split(k,tot),y=fa[x];
122     rec(x);c[y][0]=0;
123     update(y);update(fa[y]);
124 }
125 void build(int l,int r,int f){
126     if(l>r)return ;
127     int mid=(l+r)>>1,now=id[mid],last=id[f];
128     if(l==r){
129         sum[now]=a[l];s[now]=1;
130         tag[now]=rev[now]=0;
131         if(a[l]>=0)lx[now]=rx[now]=mx[now]=a[l];
132         else lx[now]=rx[now]=0,mx[now]=a[l];
133     }
134     else build(l,mid-1,mid),build(mid+1,r,mid);
135     v[now]=a[mid];fa[now]=last;update(now);
136     c[last][mid>=f]=now;
137 }
138 void insert(int k,int tot){
139     for1(i,tot)a[i]=read();
140     for1(i,tot)
141         if(!q.empty())
142             id[i]=q.front(),q.pop();
143         else id[i]=++cnt;
144     build(1,tot,0);int z=id[(1+tot)>>1];
145     int x=find(rt,k+1),y=find(rt,k+2);
146     splay(x,rt);splay(y,c[x][1]);
147     fa[z]=y;c[y][0]=z;
148     update(y);update(x);
149 }
150 int main(){
151     //freopen("input.txt","r",stdin);
152     //freopen("output.txt","w",stdout);
153     n=read();m=read();
154     mx[0]=a[1]=a[n+2]=-inf;
155     for1(i,n)a[i+1]=read();
156     for1(i,n+2)id[i]=i;
157     build(1,n+2,0);
158     rt=(n+3)>>1;cnt=n+2;
159     int k,tot,val;
160     char ch[10];
161     while(m--){
162         scanf("%s",ch);
163         if(ch[0]!='M'||ch[2]!='X')k=read(),tot=read();
164         if(ch[0]=='I')insert(k,tot);
165         if(ch[0]=='D')erase(k,tot);
166         if(ch[0]=='M'){
167             if(ch[2]=='X')printf("%d\n",mx[rt]);
168             else val=read(),modify(k,tot,val);
169         }
170         if(ch[0]=='R')rever(k,tot);
171         if(ch[0]=='G')query(k,tot);
172     }
173     return 0;
174 }
View Code

 

posted @ 2016-07-07 22:38  HTWX  阅读(122)  评论(0编辑  收藏  举报