bzoj1500: [NOI2005]维修数列 (Splay+变态题)
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
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
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+变态题)的更多相关文章
- BZOJ1500: [NOI2005]维修数列[splay ***]
1500: [NOI2005]维修数列 Time Limit: 10 Sec Memory Limit: 64 MBSubmit: 12278 Solved: 3880[Submit][Statu ...
- BZOJ1500: [NOI2005]维修数列 [splay序列操作]【学习笔记】
以前写过这道题了,但我把以前的内容删掉了,因为现在感觉没法看 重写! 题意: 维护一个数列,支持插入一段数,删除一段数,修改一段数,翻转一段数,查询区间和,区间最大子序列 splay序列操作裸题 需要 ...
- BZOJ1500 [NOI2005]维修数列(Splay tree)
[Submit][Status][Discuss] Description 请写一个程序,要求维护一个数列,支持以下 6 种操作: 请注意,格式栏 中的下划线‘ _ ’表示实际输入文件中的空格 Inp ...
- [bzoj1500][NOI2005]维修数列——splay
题目 题解 这道题可以说是数列问题的大BOSS,也算是这一周来学习splay等数据结构的一个总结. 我们一个一个地看这些操作. 对于操作1,我们首先建一棵子树,直接接上原树即可. 对于操作2,我们找到 ...
- [bzoj1500][NOI2005 维修数列] (splay区间操作)
Description Input 输入的第1 行包含两个数N 和M(M ≤20 000),N 表示初始时数列中数的个数,M表示要进行的操作数目. 第2行包含N个数字,描述初始时的数列. 以下M行,每 ...
- [BZOJ1500][NOI2005]维修数列 解题报告 Splay
Portal Gun:[BZOJ1500][NOI2005]维修数列 有一段时间没写博客了,最近在刚数据结构......各种板子背得简直要起飞,题目也是一大堆做不完,这里就挑一道平衡树的题来写写好了 ...
- [BZOJ1500][NOI2005]维修数列---解题报告
Portal Gun:[BZOJ1500][NOI2005]维修数列 有一段时间没写博客了,最近在刚数据结构......各种板子背得简直要起飞,题目也是一大堆做不完,这里就挑一道平衡树的题来写写好了 ...
- 【BZOJ1500】[NOI2005]维修数列 Splay
[BZOJ1500][NOI2005]维修数列 Description Input 输入的第1 行包含两个数N 和M(M ≤20 000),N 表示初始时数列中数的个数,M表示要进行的操作数目.第2行 ...
- bzoj千题计划221:bzoj1500: [NOI2005]维修数列(fhq treap)
http://www.lydsy.com/JudgeOnline/problem.php?id=1500 1.覆盖标记用INF表示无覆盖标记,要求可能用0覆盖 2.代表空节点的0号节点和首尾的两个虚拟 ...
随机推荐
- 【C++】《C++ Primer 》第十章
第十章 泛型算法 一.概述 因为它们实现共同的操作,所以称之为"算法".而"泛型",指的是它们可以操作在多种容器类型上. 泛型算法并不直接操作容器,而是遍历由两 ...
- wpf 通过为DataGrid所绑定的数据源类型的属性设置Attribute改变DataGrid自动生成列的顺序
环境Win10 VS2019 .Net Framework4.8 在wpf中,如果为一个DataGrid绑定到一个数据源,默认情况下DataGrid会为数据源类型的每个属性生成一个列(Column)对 ...
- Can't locate CPAN.pm in @INC
[root@test]# perl -MCPAN -e 'install DBD::mysql'Can't locate CPAN.pm in @INC (@INC contains: /usr/lo ...
- SpringBoot Logback无法获取配置中心属性
SpringBoot Logback无法获取配置中心属性 前言 最近在做项目中,需要把项目中的日志信息通过RabbitMQ将规定格式的消息发送到消息队列中,然后ELK系统通过消息队列拿日志并且保存起来 ...
- 开发中经常使用到的Xcode快捷键
工欲善其事必先利其器. 有了这些快捷键加持,你写代码不仅很6而且还很好看. 这些快捷键都是平时使用频率非常高的,今天整理出来分享给大家了. 左缩进:Cmd + [ 右缩进:Cmd + ] 代码格式化/ ...
- Dubbo的设计理念原来就藏在这三张图中
Dubbo在众多的微服务框架中脱颖而出,占据RPC服务框架的半壁江山,非常具有普适性,熟练掌握 Dubbo的应用技巧后深刻理解其内部实现原理,让大家能更好的掌控工作,助力职场,特别能让大家在面试中脱颖 ...
- Linux下安装配置rocketmq (单个Master、双Master)
一.环境: centos7(2台虚拟机):192.168.64.123:192.168.64.125 apache-maven-3.2.5(官网要求maven版本是3.2.x,版本不同,编译rocke ...
- Sgu149 Computer Network
Sgu149 Computer Network 题目描述 给你一棵N(N<=10000)个节点的树,求每个点到其他点的最大距离. 不难想到一个节点到其他点的最大距离为:max(以它为根的子树的最 ...
- 前端知识(一)03 初识 ECMAScript 6-谷粒学院
目录 一.ECMAScript 6 1.什么是 ECMAScript 6 2.ECMAScript 和 JavaScript 的关系 二.基本语法 1.let声明变量 2.const声明常量(只读变量 ...
- pytest:conftest.py文件
一.fixture scope 为session 级别是可以跨 .py模块调用的,也就是当我们有多个 .py文件的用例时,如果多个用例只需调用一次fixture,可以将scope='session', ...