【BZOJ1500】【NOI2005】维修数列(Splay)
【BZOJ1500】【NOI2005】维修数列(Splay)
题面
题解
Splay良心模板题
真的很简单
我一言不发
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<set>
#include<map>
#include<vector>
#include<queue>
using namespace std;
#define ll long long
#define MAX 555555
#define INF 1000000000
#define lson (t[x].ch[0])
#define rson (t[x].ch[1])
inline int read()
{
int x=0,t=1;char ch=getchar();
while((ch<'0'||ch>'9')&&ch!='-')ch=getchar();
if(ch=='-')t=-1,ch=getchar();
while(ch<='9'&&ch>='0')x=x*10+ch-48,ch=getchar();
return x*t;
}
int root,n,m;
int A[MAX];
queue<int> Q;
int Qsize;
struct Node
{
int ch[2],ff;
ll sum,v,size;
int rev,tag;
ll lm,rm,mm;
void init(int w)
{
sum=v=w;size=1;
rev=tag=0;
lm=rm=max(0,w);
mm=w;
}
}t[MAX];
void pushup(int x)
{
t[x].sum=t[lson].sum+t[rson].sum+t[x].v;
t[x].size=t[lson].size+t[rson].size+1;
t[x].lm=max(t[lson].lm,t[lson].sum+t[x].v+t[rson].lm);
t[x].rm=max(t[rson].rm,t[rson].sum+t[x].v+t[lson].rm);
t[x].mm=max(max(t[lson].mm,t[rson].mm),t[lson].rm+t[x].v+t[rson].lm);
}
void rotate(int x)
{
int y=t[x].ff,z=t[y].ff;
int k=t[y].ch[1]==x;
t[z].ch[t[z].ch[1]==y]=x;t[x].ff=z;
t[y].ch[k]=t[x].ch[k^1];t[t[x].ch[k^1]].ff=y;
t[x].ch[k^1]=y;t[y].ff=x;
pushup(y);pushup(x);
}
void pushdown(int x)
{
if(t[x].tag)//区间覆盖
{
t[x].tag=t[x].rev=0;
if(lson)t[lson].tag=1,t[lson].v=t[x].v,t[lson].sum=t[lson].size*t[x].v;
if(rson)t[rson].tag=1,t[rson].v=t[x].v,t[rson].sum=t[rson].size*t[x].v;
if(t[x].v>=0)
{
if(lson)t[lson].lm=t[lson].rm=t[lson].mm=t[lson].sum;
if(rson)t[rson].lm=t[rson].rm=t[rson].mm=t[rson].sum;
}
else
{
if(lson)t[lson].lm=t[lson].rm=0,t[lson].mm=t[x].v;
if(rson)t[rson].lm=t[rson].rm=0,t[rson].mm=t[x].v;
}
}
if(t[x].rev)
{
t[x].rev^=1;
if(lson)t[lson].rev^=1;
if(rson)t[rson].rev^=1;
swap(t[lson].lm,t[lson].rm);
swap(t[rson].rm,t[rson].lm);
swap(t[lson].ch[0],t[lson].ch[1]);
swap(t[rson].ch[0],t[rson].ch[1]);
}
}
void Splay(int x,int goal)
{
while(t[x].ff!=goal)
{
int y=t[x].ff,z=t[y].ff;
if(z!=goal)
(t[y].ch[1]==x)^(t[z].ch[1]==y)?rotate(x):rotate(y);
rotate(x);
}
if(!goal)root=x;
}
int Rank(int k)
{
int x=root;
while(x)
{
pushdown(x);
if(t[lson].size+1==k)return x;
if(t[lson].size+1<k)k-=t[lson].size+1,x=rson;
else x=lson;
}
}
int Build(int l,int r,int ff)
{
if(l>r)return 0;
int mid=(l+r)>>1;
int x=Q.front();Q.pop();Qsize--;
//printf("Qsize=====%d\n",Qsize);
if(l==r)
{
t[x].ff=ff;
t[x].init(A[mid]);
return x;
}
t[x].v=t[x].mm=A[mid];t[x].ff=ff;
lson=Build(l,mid-1,x);
rson=Build(mid+1,r,x);
pushup(x);
return x;
}
void Insert()
{
int pos=read(),tt=read();
for(int i=1;i<=tt;++i)A[i]=read();
if(!tt)return;
int L=Rank(pos+1),R=Rank(pos+2);
Splay(L,0);Splay(R,L);
t[R].ch[0]=Build(1,tt,R);
pushup(R);pushup(L);
}
void Clear(int x)
{
t[x].ch[0]=t[x].ch[1]=t[x].ff=t[x].size=t[x].sum=t[x].lm=t[x].rm=0;
t[x].mm=-INF;
t[x].rev=t[x].tag=0;
}
void Reuse(int x)
{
if(!x)return;
Q.push(x);Qsize++;
if(lson)Reuse(lson);
if(rson)Reuse(rson);
Clear(x);
}
void Delete()
{
int pos=read(),tt=read();
if(!tt)return;
int L=Rank(pos),R=Rank(pos+tt+1);
Splay(L,0);Splay(R,L);
Reuse(t[R].ch[0]);
t[R].ch[0]=0;
pushup(R);pushup(L);
}
void SetVal()
{
int pos=read(),tt=read();
int vv=read();
int L=Rank(pos),R=Rank(pos+tt+1);
Splay(L,0);Splay(R,L);
int x=t[R].ch[0];
t[x].v=vv;
t[x].tag=1;
t[x].sum=t[x].size*vv;
if(vv>=0)t[x].lm=t[x].rm=t[x].mm=t[x].sum;
else t[x].lm=t[x].rm=0,t[x].mm=vv;
pushup(R);pushup(L);
}
void Reverse()
{
int pos=read(),tt=read();
int L=Rank(pos),R=Rank(pos+tt+1);
Splay(L,0);Splay(R,L);
int x=t[R].ch[0];
if(!t[x].tag)
{
t[x].rev^=1;
swap(lson,rson);
swap(t[x].lm,t[x].rm);
pushup(R);pushup(L);
}
}
void Query_Sum()
{
int pos=read(),tt=read();
int L=Rank(pos),R=Rank(pos+tt+1);
Splay(L,0);Splay(R,L);
printf("%lld\n",t[t[R].ch[0]].sum);
}
void Query_Max_Sum()
{
printf("%lld\n",t[root].mm);
}
int main()
{
n=read();m=read();
A[1]=A[n+2]=-INF;
Clear(0);
for(int i=1;i<=550000;++i)Q.push(i);Qsize=550000;
for(int i=1;i<=n;++i)A[i+1]=read();
root=Build(1,n+2,0);
char ch[50];
while(m--)
{
scanf("%s",ch);
if(ch[0]=='G')Query_Sum();
if(ch[0]=='D')Delete();
if(ch[0]=='R')Reverse();
if(ch[0]=='M'&&ch[2]=='K')SetVal();
if(ch[0]=='M'&&ch[2]=='X')Query_Max_Sum();
if(ch[0]=='I')Insert();
}
}
【BZOJ1500】【NOI2005】维修数列(Splay)的更多相关文章
- BZOJ1500: [NOI2005]维修数列[splay ***]
1500: [NOI2005]维修数列 Time Limit: 10 Sec Memory Limit: 64 MBSubmit: 12278 Solved: 3880[Submit][Statu ...
- [bzoj1500][NOI2005]维修数列——splay
题目 题解 这道题可以说是数列问题的大BOSS,也算是这一周来学习splay等数据结构的一个总结. 我们一个一个地看这些操作. 对于操作1,我们首先建一棵子树,直接接上原树即可. 对于操作2,我们找到 ...
- BZOJ1500: [NOI2005]维修数列 [splay序列操作]【学习笔记】
以前写过这道题了,但我把以前的内容删掉了,因为现在感觉没法看 重写! 题意: 维护一个数列,支持插入一段数,删除一段数,修改一段数,翻转一段数,查询区间和,区间最大子序列 splay序列操作裸题 需要 ...
- [bzoj1500][NOI2005 维修数列] (splay区间操作)
Description Input 输入的第1 行包含两个数N 和M(M ≤20 000),N 表示初始时数列中数的个数,M表示要进行的操作数目. 第2行包含N个数字,描述初始时的数列. 以下M行,每 ...
- BZOJ1500 [NOI2005]维修数列(Splay tree)
[Submit][Status][Discuss] Description 请写一个程序,要求维护一个数列,支持以下 6 种操作: 请注意,格式栏 中的下划线‘ _ ’表示实际输入文件中的空格 Inp ...
- bzoj1500: [NOI2005]维修数列 (Splay+变态题)
Time Limit: 10 Sec Memory Limit: 64 MBSubmit: 11353 Solved: 3553 [Submit][Status][Discuss] Descrip ...
- [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-1500】维修数列 Splay
1500: [NOI2005]维修数列 Time Limit: 10 Sec Memory Limit: 64 MBSubmit: 11047 Solved: 3460[Submit][Statu ...
随机推荐
- S5PV210时钟,看门狗定时器
晶振:时钟源(操作主要有两个,倍频,分频) A8的时钟源: 时钟域,每个时钟域(不同的最高频率和最低频率)管理着不同的电路模块: 不同的时钟域对应不同电路模块表 时钟电路:懂得看时钟电路(时钟源选择开 ...
- c++多态性---虚函数
虚函数与纯虚函数的区别: 1.拥有虚函数的类可以声明对象,但拥有纯虚函数的类不可以声明对象(只能声明一个指针,并且不能给其分配内存),并且将这个类称为抽象类 特点: 1.虚函数是动态绑定的基础. 2. ...
- 分布式服务dubbo使用
SOA 服务框架dubbo 节点角色说明: Provider: 暴露服务的服务提供方. Consumer: 调用远程服务的服务消费方. Registry: 服务注册与发现的注册中心. Monitor: ...
- yii2 源码分析Behavior类分析 (四)
Behavior类是所有事件类的基类,它继承自object类 Behavior类的前面注释描述大概意思: * Behavior类是所有事件类的基类 * * 一个行为可以用来增强现有组件的功能,而不需要 ...
- mysql 在一个实例运行情况下再搭建一个实例
配置mysql服务 详细步骤,请参考(http://study.lishiming.net/chapter17.html#mysql), 阿铭只把简单步骤写一下. 根据阿铭提供的地址,假如你已经搭建好 ...
- 使用matlab生成sine波mif文件
使用matlab生成sine波mif文件 作者:lee神 在使用altera 的FPGA中的rom过程中常常会使用到.mif文件或.hex文件.对于初学者,无论mif还是hex都是很令人疑惑的东西,这 ...
- js的继承实现
1.原型链继承 1.创建父类对象2.创建子类函数对象3.将父类的实例对象赋值给子类的原型4.将子类的原型属性的构造函数设置为 子类本身 function Person(name) { this.nam ...
- Smarty3.1.3安装使用
p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Verdana } span.s1 { } Smarty简介 Smarty是一个PHP的模板引 ...
- 关于UIButton嵌入到UIView点击无反应问题的解决方法和delegate的简单用法示例(转载)
做项目封装UIView的时候碰到的问题,没想到有个哥们儿还写成博客,特此收藏! 问题是这样的,几个界面用到同一个自定义返回按钮,于是就想着把这个按钮单独封装起来,添加一个UIView类,在里面自定义U ...
- 看eShopOnContainers学一个EventBus
最近在看微软eShopOnContainers 项目,看到EventBus觉得不错,和大家分享一下 看完此文你将获得什么? eShop中是如何设计事件总线的 实现一个InMemory事件总线eShop ...