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. phpcms v9 自定义分页 带下拉跳转

    <?php function new_pages($num, $curr_page, $perpage = 20, $urlrule = '', $array = array(),$setpag ...

  2. Python 信号量

    信号的概念 信号(signal)--     进程之间通讯的方式,是一种软件中断.一个进程一旦接收到信号就会打断原来的程序执行流程来处理信号. 几个常用信号: SIGINT     终止进程  中断进 ...

  3. Linux学习系列之Linux入门(一)linux安装与入门

    第一篇:安装并配置Linux开发环境 一.安装linux: 主要安装Linux的发行版,到目前为之,主要的发行版有: 比较常用的是Ubuntu.redhat和centOS,主要的安装方法详细: Ubu ...

  4. if...else..的错误用法

    1.最近在写js代码完成一个前段DOM操作的函数时,自己错误的使用了if..else..控制体.为什么是错误的呢?看看我的 代码你就明白了: document.getElementsByClassNa ...

  5. Arcgis 10.1 求面里面所包含的点的平均值

    空间链接的作用:将面上的所有点的值加起来取平均值。赋值给面属性。(我们可以定义右击——定义合并规则 连接要素的字段映射参数中指定的合并规则仅适用于连接要素中的属性,且仅适用于多个要素与目标要素匹配   ...

  6. DB天气app冲刺第九天

    今天已经是冲刺的第九天了.应该算是进入了冲刺的后半期了.今天的android界面UI设计基本完成了.但还是有一些的bug,自己怎么也调不好,明天需要请教一下大神去. 今天已经把昨天布置的任务完成了.昨 ...

  7. 把Ubuntu打造成Mac Macbuntu

    Macbuntu:把 Ubuntu 一键打造成完整的 Mac 风格 23八 2010 # 作者:riku/ 本文采用CC BY-NC-SA 2.5协议授权,转载请注明本文链接. Macbuntu 是一 ...

  8. PHP运算符及php取整函数

    ceil -- 进一法取整 说明 float ceil ( float value ) 返回不小于 value 的下一个整数,value 如果有小数部分则进一位.ceil() 返回的类型仍然是 flo ...

  9. MVC 文件上传和图片剪辑

    http://www.cnblogs.com/hinton/archive/2012/03/01/2375465.html http://gallery.kissyui.com/uploader/1. ...

  10. xcode 把cocos2d-x 以源码的形式包含进自己的项目适合, 性能分析问题的错误

    性能分析:出现如下错误: xcode profile  Variable has incomplete type   class “CC_DLL” 解决办法:在 xcode的Build Setting ...