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号节点和首尾的两个虚拟 ...
随机推荐
- web元素定位和appium-app元素定位
一.web页面元素定位工具介绍 1.打开google浏览器,按F12进入开发者模式,如下图: 2.用鼠标点击下图红色框中的箭头--然后鼠标移动到web页面的元素上(此处为百度框),会自动定位到对应的h ...
- openstack octavia的实现与分析(一)openstack负载均衡的现状与发展以及lvs,Nginx,Haproxy三种负载均衡机制的基本架构和对比
[负载均衡] 大量用户发起请求的情况下,服务器负载过高,导致部分请求无法被响应或者及时响应. 负载均衡根据一定的算法将请求分发到不同的后端,保证所有的请求都可以被正常的下发并返回. [主流实现-LVS ...
- 利用DES,C#加密,Java解密代码
//C#加密 /// <summary> /// 进行DES加密. /// </summary> /// <param name="pToEncrypt&quo ...
- PMP知识领域
· 十大知识领域 整合-项目整合管理 识别.定义.组合.统一和协调个项目管理过程组的各种过程和活动而展开的活动与过程. 整合:统一.合并.沟通和简历联系:贯穿项目始终 七个过程组 一.制定项目章程(启 ...
- Ubuntu 18.04.4 LTS 更换国内系统源
Ubuntu 18.04.4 LTS 更换国内系统源 1.1) 好习惯先做备份在干活: mv /etc/apt/sources.list /etc/apt/sources.list.bak 1.2) ...
- Java JDK8下载 (jdk-8u251-windows-x64和jdk-8u271-linux-x64.tar)
jdk-8u251-windows-x64 和 jdk-8u271-linux-x64.tar 链接:https://pan.baidu.com/s/1gci6aSIFhEhjY8F48qH39Q 提 ...
- bat批处理积累
1 ::所有命令不回显,包含echo off自身也不回显 2 @echo off 3 4 ::rem或双冒号都为注释行 5 6 rem 变量赋值,注意变量和等号之间不能有空格,等号后的空格会作为变量值 ...
- [Usaco2008 Mar]Cow Travelling游荡的奶牛
题目描述 奶牛们在被划分成N行M列(2 <= N <= 100; 2 <= M <= 100)的草地上游走,试图找到整块草地中最美味的牧草.Farmer John在某个时刻看见 ...
- three.js cannon.js物理引擎地形生成器和使用指针锁定控件
今天郭先生说一说使用cannon.js物理引擎绘制地形和使用指针锁定控件.效果如下图.线案例请点击博客原文. 这里面的生成地形的插件和指针锁定控件也是cannon.js的作者schteppe封装的,当 ...
- 攻击JWT的一些方法
JWT安全隐患之绕过访问控制 https://mp.weixin.qq.com/s/xe8vOVhaysmgvxl-A3nkBA 记录一次JWT的越权渗透测试 https://mp.weixin.qq ...