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. stm32之ADC学习

    1.stm32中采用的是逐次逼近型模拟数字方式,那么什么是逐次逼近呢? 逐次逼近的方式类似于二分法,以8位数据为例:当输入一个模拟量的时候,首先取这8位数的一半,即1000 0000,与模拟量比较,大 ...

  2. android模拟器打开时比较慢,Run As就找不到模拟器

    1.运行中输入cmd,然后回车,调出command窗口 2.用cd,将目录切换到adb所在目录,然后输入adb kill-server ,adb start-server 3.adb devices就 ...

  3. WinForm调试输出数据

    在调试Winfrom时想知道其中的数据输出 1.单击运行按钮 2.选择调试->窗口->输出 3.单击Winform中要执行的按钮  在输出栏中显示输出数据 4.Ctrl  K  S  能够 ...

  4. 【BZOJ 1834】 [ZJOI2010]network 网络扩容

    Description 给定一张有向图,每条边都有一个容量C和一个扩容费用W.这里扩容费用是指将容量扩大1所需的费用.求: 1. 在不扩容的情况下,1到N的最大流: 2. 将1到N的最大流增加K所需的 ...

  5. owa Your request can't be completed right now. Please try again later.

    Your request can't be completed right now. Please try again later.

  6. maven3.1安装及配置

    1.首先下载maven3,并安装 2.环境变量配置与JAVA_HOME的配置一样 3.打开MyEclipse preferences>>Maven4MyEclipse>>Mav ...

  7. Xcode 合并分支报错

    原理和操作步骤见如下转载的两篇文章, 我所使用的 svn 客户端软件是 Mac 下面的 Versions.app v1.06 这个版本包含一个多人开发的bug bug 的解决方案见我之前转载的两篇文章 ...

  8. @Autowired获取配置文件中被注入实例的两种方式

    一.说明 二.那么在JavaBean中如何通过@Autowired获取该实例呢?有两种方式: 1.直接获取 @RunWith(SpringJUnit4ClassRunner.class) @Conte ...

  9. HTTP协议的几个重要概念

    转自:http://ice-cream.iteye.com/blog/77549 1.连接(Connection):一个传输层的实际环流,它是建立在两个相互通讯的应用程序之间. 2.消息(Messag ...

  10. Log4net Level

    ILog logger = LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType); l ...