1500: [NOI2005]维修数列

Time Limit: 10 Sec  Memory Limit: 64 MB
Submit: 8397  Solved: 2530

Description

Input

输入文件的第1行包含两个数N和M,N表示初始时数列中数的个数,M表示要进行的操作数目。第2行包含N个数字,描述初始时的数列。以下M行,每行一条命令,格式参见问题描述中的表格。

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

#include<iostream>
#include<cstring>
#include<cstdio>
using namespace std;
#define N 500010 int n,m,num[N];
struct SplayTree
{
int sz[N],ch[N][],pre[N],que[N],sta[N];
int top1,top2,root; int sum[N],maxm[N],maxl[N],maxr[N],val[N];
bool ro[N],same[N]; inline void SameNode(int x,int c)
{
if(x==) return;
val[x]=c;
sum[x]=sz[x]*val[x];
maxm[x]=maxl[x]=maxr[x]=max(sum[x],val[x]);
same[x]=;
}
inline void ReverseNode(int x)
{
if(x==) return;
ch[x][]^=ch[x][]^=ch[x][]^=ch[x][];
maxl[x]^=maxr[x]^=maxl[x]^= maxr[x];
ro[x]^=;
}
inline void PushDown(int x)
{
if(x==) return;
if(ro[x])
{
ReverseNode(ch[x][]);
ReverseNode(ch[x][]);
ro[x]^=;
}
if(same[x])
{
SameNode(ch[x][],val[x]);
SameNode(ch[x][],val[x]);
same[x]=;
}
}
inline void PushUp(int x)
{
if(x==) return;
sz[x]=+sz[ch[x][]]+sz[ch[x][]];
sum[x]=val[x]+sum[ch[x][]]+sum[ch[x][]];
maxl[x]=max(maxl[ch[x][]],sum[ch[x][]]+val[x]+max(,maxl[ch[x][]]));
maxr[x]=max(maxr[ch[x][]],sum[ch[x][]]+val[x]+max(,maxr[ch[x][]]));
maxm[x]=max(max(maxl[ch[x][]],)+val[x]+max(maxr[ch[x][]],),max(maxm[ch[x][]],maxm[ch[x][]]));
}
inline void Rotate(int x,int c)
{
int y=pre[x];
PushDown(y);
PushDown(x);
ch[y][!c]=ch[x][c];
if(ch[x][c]) pre[ch[x][c]]=y;
pre[x]=pre[y];
if(pre[y]) ch[pre[y]][ch[pre[y]][]==y]=x;
ch[x][c]=y;
pre[y]=x;
PushUp(y);
if(y==root) root=x;
}
inline void Splay(int x,int f)
{
PushDown(x);
while(pre[x]!=f)
{
PushDown(pre[pre[x]]);
PushDown(pre[x]);
PushDown(x);
if(pre[pre[x]]==f) Rotate(x,ch[pre[x]][]==x);
else
{
int y=pre[x],z=pre[y];
int c=(ch[z][]==y);
if(ch[y][c]==x) Rotate(x,!c),Rotate(x,c);
else Rotate(y,c),Rotate(x,c);
}
}
PushUp(x);
if(f==) root=x;
}
inline void SplayKth(int k,int f)
{
int x=root;
k+=;
while()
{
PushDown(x);
if(k==sz[ch[x][]]+) break;
else if(k<=sz[ch[x][]]) x=ch[x][];
else k-=sz[ch[x][]]+,x=ch[x][];
}
Splay(x,f);
}
inline void Erase(int x)
{
int head=,rear=;
que[head]=x;
while(head<=rear)
{
sta[++top2]=que[head];
if(ch[que[head]][]) que[++rear]=ch[que[head]][];
if(ch[que[head]][]) que[++rear]=ch[que[head]][];
head++;
}
}
inline void NewNode(int &x,int c)
{
if(top2) x=sta[top2--];
else x=++top1;
ch[x][]=ch[x][]=pre[x]=;
sz[x]=;
ro[x]=same[x]=;
val[x]=sum[x]=maxm[x]=maxl[x]=maxr[x]=c;
}
inline void Build(int &x,int l,int r,int f)
{
if(l>r) return;
int m=(l+r)>>;
NewNode(x,num[m]);
Build(ch[x][],l,m-,x);
Build(ch[x][],m+,r,x);
pre[x]=f;
PushUp(x);
}
inline void Init(int n)
{
ch[][]=ch[][]=pre[]=sz[]=;
ro[]=same[]=;
maxr[]=maxl[]=maxm[]=-;
sum[]=;
root=top1=top2=;
for(int i=;i<=n;i++) scanf("%d",&num[i]);
Build(root,,n+,);
}
void GetSum()
{
int a,b;
scanf("%d%d",&a,&b);
SplayKth(a-,);
SplayKth(a+b,root);
printf("%d\n", sum[ch[ch[root][]][]]);
}
void MaxSum()
{
SplayKth(, );
SplayKth(sz[root]-,root);
printf("%d\n",maxm[ch[ch[root][]][]]);
}
void Insert()
{
int pos,tot;
scanf("%d%d",&pos,&tot);
for(int i=;i<=tot;i++) scanf("%d", &num[i]);
SplayKth(pos,);
SplayKth(pos+,root);
Build(ch[ch[root][]][],,tot,ch[root][]);
PushUp(ch[root][]);
PushUp(root);
}
void Delete()
{
int pos,tot;
scanf("%d%d",&pos,&tot);
SplayKth(pos-,);
SplayKth(pos+tot,root);
Erase(ch[ch[root][]][]);
ch[ch[root][]][]=;
PushUp(ch[root][]);
PushUp(root);
}
void MakeSame()
{
int pos,tot,c;
scanf("%d%d%d",&pos,&tot,&c);
SplayKth(pos-,);
SplayKth(pos+tot,root);
SameNode(ch[ch[root][]][],c);
}
void Reverse()
{
int pos,tot;
scanf("%d%d",&pos,&tot);
SplayKth(pos-,);
SplayKth(pos+tot,root);
ReverseNode(ch[ch[root][]][]);
}
}t;
int main()
{
char op[];
scanf("%d%d",&n,&m);
t.Init(n);
while(m--)
{
scanf("%s",&op);
if(op[]=='G') t.GetSum();
else if(op[]=='M' && op[]=='X') t.MaxSum();
else if(op[]=='I') t.Insert();
else if(op[]=='D') t.Delete();
else if(op[]=='M' && op[]=='K') t.MakeSame();
else if(op[]=='R') t.Reverse();
}
return ;
}

[NOI2005] 维修数列的更多相关文章

  1. bzoj 1500: [NOI2005]维修数列 splay

    1500: [NOI2005]维修数列 Time Limit: 10 Sec  Memory Limit: 64 MBSubmit: 6556  Solved: 1963[Submit][Status ...

  2. [BZOJ1500][NOI2005]维修数列---解题报告

    Portal Gun:[BZOJ1500][NOI2005]维修数列 有一段时间没写博客了,最近在刚数据结构......各种板子背得简直要起飞,题目也是一大堆做不完,这里就挑一道平衡树的题来写写好了 ...

  3. BZOJ_1500_[NOI2005]维修数列_splay

    BZOJ_1500_[NOI2005]维修数列_splay 题意: 分析: 节点维护从左开始的最大连续子段和,从右开始的最大连续子段和,区间的最大连续子段和 插入:重新建一棵树,把pos旋到根,把po ...

  4. bzoj千题计划221:bzoj1500: [NOI2005]维修数列(fhq treap)

    http://www.lydsy.com/JudgeOnline/problem.php?id=1500 1.覆盖标记用INF表示无覆盖标记,要求可能用0覆盖 2.代表空节点的0号节点和首尾的两个虚拟 ...

  5. [BZOJ1500][NOI2005]维修数列 解题报告 Splay

    Portal Gun:[BZOJ1500][NOI2005]维修数列 有一段时间没写博客了,最近在刚数据结构......各种板子背得简直要起飞,题目也是一大堆做不完,这里就挑一道平衡树的题来写写好了 ...

  6. BZOJ 1500: [NOI2005]维修数列 (splay tree)

    1500: [NOI2005]维修数列 Time Limit: 10 Sec  Memory Limit: 64 MBSubmit: 4229  Solved: 1283[Submit][Status ...

  7. 【BZOJ1500】[NOI2005]维修数列 Splay

    [BZOJ1500][NOI2005]维修数列 Description Input 输入的第1 行包含两个数N 和M(M ≤20 000),N 表示初始时数列中数的个数,M表示要进行的操作数目.第2行 ...

  8. [bzoj1500][NOI2005]维修数列_非旋转Treap

    维修数列 bzoj-1500 NOI-2005 题目大意:给定n个数,m个操作,支持:在指定位置插入一段数:删除一个数:区间修改:区间翻转.查询:区间和:全局最大子序列. 注释:$1\le n_{ma ...

  9. 【BZOJ】1500: [NOI2005]维修数列

    [算法]splay [题解]数据结构 感谢Occult的模板>_<:HYSBZ 1500 维修数列 #include<cstdio> #include<cctype> ...

随机推荐

  1. 医学影像工作站程序ProDicom的说明

    转载 http://blog.csdn.net/prodicom/article/details/4015064 注意:以下内容为转载,但保留了第一人称,请注意,以免造成不必要的麻烦. 医网联影像工作 ...

  2. RDD操作

    RDD操作 1.对一个数据为{1,2,3,3}的RDD进行基本的RDD转化操作 函数名 目的 示例 结果 map() 函数应用于RDD中的每个元素 rdd.map(x=>x+1) {2,3,4, ...

  3. 关于location

    ---恢复内容开始--- window.location跳转+替换+刷新 一.最外层top跳转页面,适合用于iframe框架集 top.window.location.href("${pag ...

  4. 二、记一次失败的 CAS 搭建 之 证书配置

    ==================================================================================================== ...

  5. 【CSLA】Component-based,Scalable,LogicalArchitecture

    我能说我没看懂吗 ? http://www.cnblogs.com/lonely7345/archive/2010/02/06/1665171.html

  6. SC命令详解

    我们知道在MStools SDK,也就是在Resource Kit有一个很少有人知道的命令行软件,SC.exe,这个软件向所有的Windows NT和Windows 2000要求控制他们的API函数. ...

  7. bnuoj 1071 拼图++(BFS+康拓展开)

    http://www.bnuoj.com/bnuoj/problem_show.php?pid=1071 [题意]:经过四个点的顺逆时针旋转,得到最终拼图 [题解]:康拓展开+BFS,注意先预处理,得 ...

  8. bnuoj 4357 传送阵

    http://www.bnuoj.com/bnuoj/problem_show.php?pid=4357 [题意]:在1000个数中选择3个之和是m的倍数,可能有多种选择方案,请输出标号排序最小的一组 ...

  9. 1028: [JSOI2007]麻将 - BZOJ

    Description 麻将是中国传统的娱乐工具之一.麻将牌的牌可以分为字牌(共有东.南.西.北.中.发.白七种)和序数牌(分为条子.饼子.万子三种花色,每种花色各有一到九的九种牌),每种牌各四张.在 ...

  10. SpringMVC注解@RequestParam(转)

    鸣谢:http://shawnccx.iteye.com/blog/730239 -------------------------------------------------- 在SpringM ...