维修数列
Time Limit: 10 Sec
Memory Limit: 64 MB
Submit: 11839
Solved: 3724
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
在数列上的各种操作。。。
注意最大字段和要维护 lx rx mx 分别更新
注意内存 64M , 可以使用内存池回收!
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cmath>
#include<vector>
#include<queue>
#include<stack>
#include<string>
#include<iomanip>
#include<ctime>
#include<climits>
#include<cctype>
#include<algorithm>
using namespace std;
const int maxn=1000005;
void read(int &x)
{
x=0;
int flag=1;char ch=getchar();
if(ch==EOF) return;
while((ch<'0'||ch>'9')&&ch!='-') ch=getchar();
if(ch=='-') flag=-1,ch=getchar();
while(ch>='0'&&ch<='9')
{
x*=10;
x+=ch-'0';
ch=getchar();
}
x*=flag;
}
/******************splay****************************/
queue <int> que;//recycling
int fa[maxn];
int ch[maxn][2];
int data[maxn];//data_range
int sum[maxn];
int size[maxn];
bool rev[maxn],lazy[maxn];
int mx[maxn],lx[maxn],rx[maxn];
int maxnode,root;
inline void pushdown(int rt)
{
int l=ch[rt][0],r=ch[rt][1];
if(lazy[rt])
{
lazy[rt]=rev[rt]=false;
if(l) lazy[l]=true,data[l]=data[rt],sum[l]=size[l]*data[l];
if(r) lazy[r]=true,data[r]=data[rt],sum[r]=size[r]*data[r];
if(data[rt]>=0)
{
if(l) mx[l]=lx[l]=rx[l]=sum[l];
if(r) mx[r]=lx[r]=rx[r]=sum[r];
}
else
{
if(l) mx[l]=data[rt],lx[l]=rx[l]=0;
if(r) mx[r]=data[rt],lx[r]=rx[r]=0;//!! the mx must include sth, while lx,rx don't
}
}
if(rev[rt])
{
rev[rt]^=1;rev[l]^=1;rev[r]^=1;
swap(ch[l][0],ch[l][1]);swap(ch[r][0],ch[r][1]);
swap(lx[l],rx[l]);swap(lx[r],rx[r]);//writen errored twice!!!!! must be very careful!! lx and rx !!
}
}
inline void update(int rt)
{
int l=ch[rt][0],r=ch[rt][1];
size[rt]=size[l]+size[r]+1;
sum[rt]=sum[l]+sum[r]+data[rt];
mx[rt]=max(mx[l],mx[r]);
mx[rt]=max(mx[rt],lx[r]+rx[l]+data[rt]);
lx[rt]=max(lx[l],sum[l]+data[rt]+lx[r]);
rx[rt]=max(rx[r],sum[r]+data[rt]+rx[l]);
}
inline void rotate(int x,int &to)
{
int y=fa[x],z=fa[y],l,r;
l= ch[y][1]==x ; r=l^1;
if(y==to) to=x;
else ch[z][ch[z][1]==y]=x;
fa[ch[x][r]]=y;fa[y]=x;fa[x]=z;
ch[y][l]=ch[x][r];ch[x][r]=y;
update(y);update(x);
}
inline void splay(int x,int &to)
{
while(x!=to)
{
int y=fa[x],z=fa[y];
if(y!=to)
{
if(ch[y][0]==x^ch[z][0]==y) rotate(x,to);
else rotate(y,to);
}
rotate(x,to);
}
}
/**********************BST*****************************/
int a[maxn],id[maxn];
void build(int l,int r,int f)
{
if(l>r) return;
int m=(l+r)>>1,now=id[m],last=id[f];
if(l==r)
{
sum[now]=a[m];size[now]=1;
rev[now]=lazy[now]=false;
if(a[m]>=0) lx[now]=rx[now]=a[m];
else lx[now]=rx[now]=0;
mx[now]=a[m];
}
else build(l,m-1,m),build(m+1,r,m);
data[now]=a[m];fa[now]=last;ch[last][m>f]=now;
update(now);
}
/**********************function************************/
int find(int rt,int rank)
{
pushdown(rt);
if(rank==size[ch[rt][0]]+1) return rt;
if(rank<=size[ch[rt][0]]) return find(ch[rt][0],rank);
else return find(ch[rt][1],rank-(size[ch[rt][0]]+1));
}
inline void insert(int pos,int tot)
{
for(int i=1;i<=tot;i++) read(a[i]);
for(int i=1;i<=tot;i++)
if(!que.empty()) id[i]=que.front(),que.pop();
else id[i]=++maxnode;
build(1,tot,0);
int rt=id[(1+tot)>>1],y=find(root,pos+1),x=find(root,pos+2);
splay(y,root);splay(x,ch[y][1]);
fa[rt]=x;ch[x][0]=rt;
update(x);update(y);
}
void recycle(int root)
{
if(root==0) return;
fa[root]=sum[root]=data[root]=size[root]=rev[root]=lazy[root]=0;
que.push(root);
recycle(ch[root][0]);recycle(ch[root][1]);
ch[root][0]=ch[root][1]=0;
}
int split(int pos,int tot)
{
int y=find(root,pos),x=find(root,pos+tot+1);
splay(y,root);splay(x,ch[y][1]);
return ch[x][0];
}
inline void erase(int pos,int tot)
{
int rt=split(pos,tot),x=fa[rt],y=fa[x];
recycle(rt);ch[x][0]=0;
update(x);update(y);
}
inline void modify(int pos,int tot,int num)
{
int rt=split(pos,tot),x=fa[rt],y=fa[x];
lazy[rt]=true;data[rt]=num;sum[rt]=size[rt]*num;
if(num>=0) mx[rt]=lx[rt]=rx[rt]=sum[rt];
else mx[rt]=num,lx[rt]=rx[rt]=0;
update(x);update(y);
}
inline void reverse(int pos,int tot)
{
int rt=split(pos,tot),x=fa[rt],y=fa[x];
if(!lazy[rt])
{
rev[rt]^=1;
swap(ch[rt][0],ch[rt][1]);swap(lx[rt],rx[rt]);
update(x);update(y);
}
}
inline int query(int pos,int tot)
{
return sum[split(pos,tot)];
}
/***********************main*****************************/
int n,m;
int main()
{
freopen("sequence.in","r",stdin);
freopen("sequence.out","w",stdout);
read(n),read(m);
mx[0]=a[1]=a[n+2]=INT_MIN/3;
for(int i=1;i<=n;i++) read(a[i+1]);
for(int i=1;i<=n+2;i++) id[i]=i;
maxnode=n+2;
build(1,n+2,0);
root=(n+3)>>1;
char cmd[10];int pos,tot,num;
while(m--)
{
scanf("%s",cmd);
if(cmd[0]!='M'||cmd[2]!='X') read(pos),read(tot);
if(cmd[0]=='I') insert(pos,tot);
if(cmd[0]=='D') erase(pos,tot);
if(cmd[0]=='R') reverse(pos,tot);
if(cmd[0]=='G') printf("%d\n",query(pos,tot));
if(cmd[0]=='M')
if(cmd[3]=='E') read(num),modify(pos,tot,num);
else printf("%d\n",mx[root]);
}
return 0;
}