Time Limit: 10 Sec  Memory Limit: 64 MB
Submit: 11353  Solved: 3553

[Submit][Status][Discuss]

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

思路:最后一个操作一开始做的时候我是直接用dp[i]=max(dp[i-1]+a[i],a[i])做的,这样每次操作都要花O(n)的复杂度,一直T,后来看了别人的思路,发现这个也是要记录的(= =还是太稚嫩)。除了在splay上面维护pre[],sz[],ch[][2]等一些基本的东西外,还要维护zhi[](这个节点的值),sum[](这个节点代表的区间的和),rev[](旋转标记),same[](区间内都为同一个数的标记),lx[](这个节点所代表的区间从以左端点为起点的最大和),rx[](~从以右端点为起点的最大和),mx[](这个区间子区间的最大和)。然后就是splay经典操作了,这题一开始写的时候没有用内存池,即没有回收删除的点,一直T,应该是内存不够导致的吧。后来看了kuangbin大神的模板,回收废的节点后才过了。ps:kuangbin大神的splay模板太劲了!

#include<iostream>
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<math.h>
#include<vector>
#include<map>
#include<set>
#include<string>
#include<bitset>
#include<algorithm>
using namespace std;
typedef long long ll;
typedef long double ldb;
#define inf 99999999
#define pi acos(-1.0)
#define Key_value ch[ch[rt][1]][0]
#define maxn 1000010
int n;
int cnt,rt;
int pre[maxn],ch[maxn][2],sz[maxn],rev[maxn],zhi[maxn],sum[maxn],same[maxn];
int lx[maxn],rx[maxn],mx[maxn];
int b[maxn],tot2;//内存池和容量
int a[maxn]; void update_same(int x,int c)
{
if(!x)return;
zhi[x]=c;
sum[x]=c*sz[x];
lx[x]=rx[x]=mx[x]=max(c,c*sz[x]);
same[x]=1;
} void update_rev(int x)
{
if(!x)return; //!!!
rev[x]^=1;
swap(ch[x][0],ch[x][1]);
swap(lx[x],rx[x]);
}
void pushdown(int x)
{
int y;
if(rev[x]){
update_rev(ch[x][0]);
update_rev(ch[x][1]);
rev[x]=0;
}
if(same[x]){
update_same(ch[x][0],zhi[x]);
update_same(ch[x][1],zhi[x]);
same[x]=0;
}
}
void pushup(int x)
{
int lson=ch[x][0],rson=ch[x][1];
sz[x]=sz[lson]+sz[rson]+1;
sum[x]=sum[lson]+sum[rson]+zhi[x];
lx[x]=max(lx[lson],sum[lson]+zhi[x]+max(0,lx[rson]) ); //!!!max(0,lx[rson])
rx[x]=max(rx[rson],sum[rson]+zhi[x]+max(0,rx[lson]) );
mx[x]=max(mx[lson],mx[rson]);
mx[x]=max(mx[x],max(0,rx[lson])+zhi[x]+max(0,lx[rson]) );
}
void Treavel(int x)
{
if(x)
{
pushdown(x);
Treavel(ch[x][0]);
printf("结点:%2d: 左儿子 %2d 右儿子 %2d 父结点 %2d size = %2d zhi = %2d sum=%2d\n",x,ch[x][0],ch[x][1],pre[x],sz[x],zhi[x],sum[x]);
Treavel(ch[x][1]);
}
}
void debug()
{
printf("root:%d\n",rt);
Treavel(rt);
} void newnode(int &x,int father,int value)
{
if(tot2)x=b[tot2--];
else x=++cnt;
pre[x]=father;ch[x][0]=ch[x][1]=0;sz[x]=1;rev[x]=0;zhi[x]=value;sum[x]=value;same[x]=0;
lx[x]=rx[x]=mx[x]=value;
} void build(int &x,int l,int r,int father)
{
if(l>r)return;
int mid=(l+r)/2;
newnode(x,father,a[mid]);
build(ch[x][0],l,mid-1,x);
build(ch[x][1],mid+1,r,x);
pushup(x);
} void init()
{
cnt=rt=0;
pre[rt]=ch[rt][0]=ch[rt][1]=sz[rt]=0;
rev[rt]=zhi[rt]=sum[rt]=same[rt]=0;
lx[rt]=rx[rt]=mx[rt]=-inf; newnode(rt,0,-10000);
newnode(ch[rt][1],rt,-10000);
build(Key_value,1,n,ch[rt][1]);
pushup(ch[rt][1]);
pushup(rt);
} void rotate(int x,int p)
{
int y=pre[x];
pushdown(y);pushdown(x);
ch[y][!p]=ch[x][p];
pre[ch[x][p] ]=y;
if(pre[y])ch[pre[y] ][ch[pre[y] ][1]==y ]=x;
pre[x]=pre[y];
ch[x][p]=y;
pre[y]=x;
pushup(y);pushup(x);
}
void splay(int x,int goal)
{
pushdown(x);
while(pre[x]!=goal){
if(pre[pre[x] ]==goal){
pushdown(pre[x]);pushdown(x);
rotate(x,ch[pre[x]][0]==x);
}
else{
int y=pre[x];int z=pre[y];
pushdown(z);pushdown(y);pushdown(x);
int p=ch[pre[y] ][0]==y;
if(ch[y][p]==x )rotate(x,!p);
else rotate(y,p);
rotate(x,p);
}
}
if(goal==0)rt=x;
pushup(x);
} int get_kth(int x,int k)
{
int i,j;
pushdown(x);
int t=sz[ch[x][0] ]+1;
if(t==k)return x;
if(t<k)return get_kth(ch[x][1],k-t);
return get_kth(ch[x][0],k);
} void Insert(int pos,int tot)
{
int i,j;
for(i=1;i<=tot;i++){
scanf("%d",&a[i]);
}
splay(get_kth(rt,pos+1),0);
splay(get_kth(rt,pos+2),rt);
build(Key_value,1,tot,ch[rt][1]);
pushup(ch[rt][1]);
pushup(rt);
} void erase(int x)
{
if(x==0)return; //!!!
b[++tot2]=x;
erase(ch[x][0]);
erase(ch[x][1]);
} void Delete(int pos,int tot)
{
splay(get_kth(rt,pos),0);
splay(get_kth(rt,pos+tot+1),rt);
erase(Key_value);
Key_value=0;
pushup(ch[rt][1]);
pushup(rt);
} void Make_same(int pos,int tot,int c)
{
splay(get_kth(rt,pos),0);
splay(get_kth(rt,pos+tot+1),rt);
update_same(Key_value,c);
pushup(ch[rt][1]);
pushup(rt);
} void Reverse(int pos,int tot)
{
splay(get_kth(rt,pos),0);
splay(get_kth(rt,pos+tot+1),rt);
update_rev(Key_value);
pushup(ch[rt][1]);
pushup(rt);
} int Get_sum(int pos,int tot)
{
splay(get_kth(rt,pos),0);
splay(get_kth(rt,pos+tot+1),rt);
return sum[Key_value];
} int Get_maxsum(int pos,int tot)
{
splay(get_kth(rt,pos),0);
splay(get_kth(rt,pos+tot+1),rt);
return mx[Key_value];
}
int main()
{
int m,i,j,pos,tot,c;
char s[10];
while(scanf("%d%d",&n,&m)!=EOF)
{
for(i=1;i<=n;i++){
scanf("%d",&a[i]);
}
init();
while(m--){
scanf("%s",s);
if(s[0]=='I'){
scanf("%d%d",&pos,&tot);
Insert(pos,tot);
}
if(s[0]=='D'){
scanf("%d%d",&pos,&tot);
Delete(pos,tot);
}
if(s[0]=='M' && s[2]=='K'){
scanf("%d%d%d",&pos,&tot,&c);
Make_same(pos,tot,c);
}
if(s[0]=='R'){
scanf("%d%d",&pos,&tot);
Reverse(pos,tot);
}
if(s[0]=='G'){
scanf("%d%d",&pos,&tot);
printf("%d\n",Get_sum(pos,tot));
}
if(s[0]=='M' && s[2]=='X'){
printf("%d\n",Get_maxsum(1,sz[rt]-2) );
}
}
}
return 0;
}

bzoj1500: [NOI2005]维修数列 (Splay+变态题)的更多相关文章

  1. BZOJ1500: [NOI2005]维修数列[splay ***]

    1500: [NOI2005]维修数列 Time Limit: 10 Sec  Memory Limit: 64 MBSubmit: 12278  Solved: 3880[Submit][Statu ...

  2. BZOJ1500: [NOI2005]维修数列 [splay序列操作]【学习笔记】

    以前写过这道题了,但我把以前的内容删掉了,因为现在感觉没法看 重写! 题意: 维护一个数列,支持插入一段数,删除一段数,修改一段数,翻转一段数,查询区间和,区间最大子序列 splay序列操作裸题 需要 ...

  3. BZOJ1500 [NOI2005]维修数列(Splay tree)

    [Submit][Status][Discuss] Description 请写一个程序,要求维护一个数列,支持以下 6 种操作: 请注意,格式栏 中的下划线‘ _ ’表示实际输入文件中的空格 Inp ...

  4. [bzoj1500][NOI2005]维修数列——splay

    题目 题解 这道题可以说是数列问题的大BOSS,也算是这一周来学习splay等数据结构的一个总结. 我们一个一个地看这些操作. 对于操作1,我们首先建一棵子树,直接接上原树即可. 对于操作2,我们找到 ...

  5. [bzoj1500][NOI2005 维修数列] (splay区间操作)

    Description Input 输入的第1 行包含两个数N 和M(M ≤20 000),N 表示初始时数列中数的个数,M表示要进行的操作数目. 第2行包含N个数字,描述初始时的数列. 以下M行,每 ...

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

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

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

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

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

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

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

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

随机推荐

  1. oracle出现未选定行

    初学oracle,在SQLplus输入查询命令 出现了以下情况.. 后来了解到oracle的SQL语句其中有些词必须大写才会有效. 在这个语句中将username后面的值改为大写就可以了. 还有一种就 ...

  2. [WPF] 在单元测试中使用 Prism 的 EventAggregator,订阅到 ThreadOption.UIThread 会报错

    1. 问题 [TestClass] public class UnitTest1 { [TestMethod] public void TestMethod1() { ContainerLocator ...

  3. oracle_fdw的安装和使用

    1.下载instant oracle client 下载网址:https://www.oracle.com/technetwork/topics/linuxx86-64soft-092277.html ...

  4. go语言循环变量

    阅读go语言圣经第五章第六节介绍到了捕获迭代变量 package main import ( "fmt" ) func main() { var lis []func() for ...

  5. vs code配置vue自动格式化

     vs code配置vue自动格式化 我他妈的要被这个vs code的格式化逼疯了.我在网上看了很多的文章,不是太老就是不好使,遇到太多坑了.在这贴出自己的配置,虽然有多余的代码,虽然可能在未来的更新 ...

  6. 自翻------Office 2013 RT 使用说明

    Office Home and Student 2013 RT Preview的更新 介绍 Microsoft已发布Microsoft Office Home and Student 2013 RT ...

  7. 大数据系列2:Hdfs的读写操作

    在前文大数据系列1:一文初识Hdfs中,我们对Hdfs有了简单的认识. 在本文中,我们将会简单的介绍一下Hdfs文件的读写流程,为后续追踪读写流程的源码做准备. Hdfs 架构 首先来个Hdfs的架构 ...

  8. nfs samba文件共享服务

    (注意:实验之前强关闭selinux和防火墙) 一丶nfs ① 1.服务端 启动服务 systemctl start nfs.service   配置文件 vim /etc/exports share ...

  9. 前端中的script标签

    script标签中的重要属性! . 浏览器解析行内脚本的方式决定了它在看到字符串时,会将其当成结束的 标签.想避免这个问题,只需要转义字符"\" ①即可: 要包含外部文件中的 Ja ...

  10. Linux系统使用lvm扩展根分区

    Linux系统使用lvm扩展根分区 背景:买的云主机虚拟机封装镜像是40G的系统盘,后期适用不规范或者其他需求需要扩展系统盘,而非挂载在一个盘至新建目录. 1.原本目录磁盘等信息: 2.使用vgdis ...