GSS系列有一丝丝像…

只不过那个是线段树 这个是splay

翻转 插入 删除啥的就是普通的splay

合在一起了而已

//By SiriusRen
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
struct Tree{
int v,rev,sum,lmax,rmax,maxx,ch[2],fa,size,same;
void init(){v=rev=sum=lmax=rmax=maxx=ch[0]=ch[1]=fa=0;size=1;same=-1001;}
}tr[505000];
int rubbish[500050],top,n,m,root;
char op[105];
void push_up(int x){
tr[0].init();tr[0].size=0,tr[0].maxx=-0x3f3f3f3f;
int lson=tr[x].ch[0],rson=tr[x].ch[1];
tr[x].size=tr[lson].size+tr[rson].size+1;
tr[x].sum=tr[lson].sum+tr[rson].sum+tr[x].v;
tr[x].lmax=max(max(tr[lson].lmax,tr[lson].sum+tr[rson].lmax+tr[x].v),0);
tr[x].rmax=max(max(tr[rson].rmax,tr[rson].sum+tr[lson].rmax+tr[x].v),0);
tr[x].maxx=max(tr[lson].maxx,max(tr[rson].maxx,tr[lson].rmax+tr[rson].lmax+tr[x].v));
}
void push_down(int x){
int lson=tr[x].ch[0],rson=tr[x].ch[1];
if(tr[x].same!=-1001){
tr[lson].same=tr[rson].same=tr[x].same;
tr[lson].v=tr[rson].v=tr[x].same;
tr[lson].sum=tr[lson].size*tr[x].same;
tr[rson].sum=tr[rson].size*tr[x].same;
if(tr[x].same>0){
tr[lson].maxx=tr[lson].lmax=tr[lson].rmax=tr[lson].sum;
tr[rson].maxx=tr[rson].lmax=tr[rson].rmax=tr[rson].sum;
}
else{
tr[lson].lmax=tr[lson].rmax=tr[rson].lmax=tr[rson].rmax=0;
tr[lson].maxx=tr[rson].maxx=tr[x].same;
}
tr[x].same=-1001;
}
if(tr[x].rev){
tr[lson].rev^=1,tr[rson].rev^=1;
swap(tr[x].ch[0],tr[x].ch[1]);
swap(tr[lson].lmax,tr[lson].rmax);
swap(tr[rson].lmax,tr[rson].rmax);
tr[x].rev=0;
}
}
void rotate(int p){
int q=tr[p].fa,y=tr[q].fa,f=(tr[q].ch[1]==p);
tr[q].ch[f]=tr[p].ch[!f];tr[tr[q].ch[f]].fa=q;
tr[p].ch[!f]=q;tr[q].fa=p;tr[p].fa=y;
if(y)tr[y].ch[tr[y].ch[1]==q]=p;
push_up(q);
}
void splay(int x,int tp){
for(int y;y=tr[x].fa;rotate(x)){
if(y==tp)break;
if(tr[y].fa!=tp){
if((tr[y].ch[0]==x)^(tr[tr[y].fa].ch[0]==y))rotate(x);
else rotate(y);
}
}push_up(x);
if(!tp)root=x;
}
int newnode(){
int temp=rubbish[top--];tr[temp].init();
return temp;
}
int build(int l,int r,int fa){
if(l>r)return 0;
if(l==r){
int temp=newnode();
scanf("%d",&tr[temp].v);
tr[temp].maxx=tr[temp].sum=tr[temp].v;
tr[temp].lmax=tr[temp].rmax=max(0,tr[temp].v);
tr[temp].fa=fa;
return temp;
}
int mid=(l+r)>>1;
int temp=newnode();
tr[temp].ch[0]=build(l,mid-1,temp);
scanf("%d",&tr[temp].v);
tr[temp].maxx=tr[temp].sum=tr[temp].v;
tr[temp].lmax=tr[temp].rmax=max(0,tr[temp].v);
tr[temp].fa=fa;
tr[temp].ch[1]=build(mid+1,r,temp);
push_up(temp);
return temp;
}
void check(int x){
push_down(x);
printf("x=%d lson=%d rson=%d sum=%d size=%d maxx=%d v=%d lmax=%d rmax=%d\n",x,tr[x].ch[0],tr[x].ch[1],tr[x].sum,tr[x].size,tr[x].maxx,tr[x].v,tr[x].lmax,tr[x].rmax);
if(tr[x].ch[0])check(tr[x].ch[0]);
if(tr[x].ch[1])check(tr[x].ch[1]);
}
int find(int x,int y){
push_down(x);
if(tr[tr[x].ch[0]].size+1==y)return x;
if(tr[tr[x].ch[0]].size>=y)return find(tr[x].ch[0],y);
else return find(tr[x].ch[1],y-tr[tr[x].ch[0]].size-1);
}
void dfs_del(int x){
if(x)rubbish[++top]=x;
if(tr[x].ch[0])dfs_del(tr[x].ch[0]);
if(tr[x].ch[1])dfs_del(tr[x].ch[1]);
}
int main(){
top=500000;
for(int i=1;i<=500000;i++)rubbish[i]=i;
scanf("%d%d",&n,&m);
root=build(1,n,0);
int tempa=find(root,1),tempb=newnode();
splay(tempa,0);
tr[tempa].ch[0]=tempb;tr[tempb].fa=tempa;
push_up(tempa);
tempa=find(root,n+1),tempb=newnode();
splay(tempa,0);
tr[tempa].ch[1]=tempb,tr[tempb].fa=tempa;
push_up(tempa);
while(m--){
scanf("%s",op);
int xx,yy,zz;
if(op[0]=='I'){
scanf("%d%d",&xx,&yy);
tempa=find(root,xx+1);
splay(tempa,0);
tempb=find(root,xx+2);
splay(tempb,tempa);
int newroot=build(1,yy,tempb);
tr[tempb].ch[0]=newroot;
push_up(tempb);push_up(root);
}
else if(op[0]=='D'){
scanf("%d%d",&xx,&yy);
tempa=find(root,xx);
splay(tempa,0);
tempb=find(root,xx+1+yy);
splay(tempb,tempa);
dfs_del(tr[tempb].ch[0]);
tr[tempb].ch[0]=0;push_up(tempb);push_up(tempa);
}
else if(op[2]=='K'){
scanf("%d%d%d",&xx,&yy,&zz);
tempa=find(root,xx);
splay(tempa,0);
tempb=find(root,xx+1+yy);
splay(tempb,tempa);
tr[tr[tempb].ch[0]].same=tr[tr[tempb].ch[0]].v=zz;
push_down(tr[tempb].ch[0]),push_up(tr[tempb].ch[0]);
push_up(tempb),push_up(tempa);
}
else if(op[0]=='R'){
scanf("%d%d",&xx,&yy);
tempa=find(root,xx);
splay(tempa,0);
tempb=find(root,xx+1+yy);
splay(tempb,tempa);
tr[tr[tempb].ch[0]].rev^=1;
swap(tr[tr[tempb].ch[0]].lmax,tr[tr[tempb].ch[0]].rmax);
push_up(tempb);
}
else if(op[0]=='G'){
scanf("%d%d",&xx,&yy);
tempa=find(root,xx);
splay(tempa,0);
tempb=find(root,xx+1+yy);
splay(tempb,tempa);
printf("%d\n",tr[tr[tempb].ch[0]].sum);
}
else{
tempa=find(root,1);
splay(tempa,0);
tempb=find(root,tr[root].size);
splay(tempb,tempa);
printf("%d\n",tr[tr[tempb].ch[0]].maxx);
}
}
}

BZOJ 1500 splay终结版...的更多相关文章

  1. BZOJ 1500 Splay 全操作

    好久没写splay了,写一发(写了一节课,调了一节课) #include <iostream> #include <cstring> #include <cstdio&g ...

  2. [BZOJ 1500] [NOI2005] 维修数列

    题目链接:BZOJ - 1500 题目分析 我要先说一下,这道题我写了一晚上,然后Debug了一整个白天..........再一次被自己的蒟蒻程度震惊= = 这道题是传说中的Splay维护数列的Bos ...

  3. [BZOJ 1500] 维护序列

    Link: BZOJ 1500 传送门 Solution: 可能平衡树维护序列的所有操作都在这了吧…… 对序列的维护$fhq treap$和$Splay$都能做 有几个注意点: 1.维护序列时始终记得 ...

  4. IOS之UI--小实例项目--添加商品和商品名(使用xib文件终结版) + xib相关知识点总结

    添加商品和商品名小项目(使用xib文件终结版) 小贴士:博文末尾有项目源码在百度云备份的下载链接. xib相关知识点总结 01-基本使用 一开始使用xib的时候,如果要使用自定义view的代码,就需要 ...

  5. Qt Windows下链接子系统与入口函数(终结版)(可同时存在main和WinMain函数)

    Qt Windows下链接子系统与入口函数(终结版) 转载自:http://blog.csdn.net/dbzhang800/article/details/6358996 能力所限,本讨论仅局限于M ...

  6. coursera_poj_魔兽世界终结版

    五个下午的时间!!!!终于过了!!有史以来做的最复杂的一个题这是我迄今为止做的最复杂也最具有挑战的一个oj作业.虽然之前做过比这个规模一些作业项目,但是往往有简单的模块框架,模块之前的关系也只是有些简 ...

  7. 蜻蜓特派员 Windows XP SP3 纯净终结版

    蜻蜓特派员Windows XP SP3 纯净安装版 终结版,系统纯净无广告.无插件,网卡等驱动和运行库齐全,安全更新补丁全网最新!微软停止了 Windows XP 的支持之后还是偶尔为 WinXP 提 ...

  8. [转]springcloud(九):配置中心和消息总线(配置中心终结版)

    https://www.cnblogs.com/ityouknow/p/6931958.html springcloud(九):配置中心和消息总线(配置中心终结版) 我们在springcloud(七) ...

  9. SQL Server数据全同步及价值分析[终结版]

    SQL Server数据全同步[终结版] 版权全部.转载请注明出处.谢谢! 经过两天的同步编写和測试.出了第一个Release版本号: 1. 本函数仅支持单向同步.即从一个主数据库想多个从数据库同步 ...

随机推荐

  1. PythonOpenCV--Rtrees随机森林

    360确实很个性,哈哈,你个貔貅,只吃不吐! Rtrees介绍!参考链接:http://docs.opencv.org/modules/ml/doc/random_trees.html 原文链接:Py ...

  2. ie6 中文字符编码 出现的问题解决

    0 前言   公司要求网页要支持ie6.7.8,结果我的设备在ie7.8.9均工作正常,而在ie6时就出现各种奇怪的错误,在这个背景下,开始动手解决网页不兼容ie6的情况.   1 搭建ie6的实验平 ...

  3. sql server 的 isnull 函数

  4. 转载:html特殊字符 编码css3 content:&amp;quot;我是特殊符号&amp;quot;

    项目中用到的一些特殊字符和图标 html代码 <div class="cross"></div> css代码 .cross{ width: 20px; he ...

  5. 解决vue中模态框内数据和外面的数据绑定的问题

    1.做表格的修改,把整条数据传到模态框做修改,但是出现模态框改变数据没有保存时,表格的数据也会跟着改变,我想实现保存以后表格数据才会改变的功能. html:使用item整条数据都上传过去了,在upda ...

  6. 【深入理解Java虚拟机】自动内存管理机制——内存区域划分

      Java与C++之间有一堵有内存动态分配和垃圾收集技术所围成的"高墙",墙外面的人想进去,墙里面的人却想出来.C/C++程序员既拥有每一个对象的所有权,同时也担负着每一个对象生 ...

  7. POJ 2376 Cleaning Shifts (线段树优化DP)

    题目大意:给你很多条线段,开头结尾是$[l,r]$,让你覆盖整个区间$[1,T]$,求最少的线段数 题目传送门 线段树优化$DP$裸题.. 先去掉所有能被其他线段包含的线段,这种线段一定不在最优解里 ...

  8. [SPOJ1716] GSS3 - Can you answer these queries III

    线段树操作. 维护一个区间最大连续子段和,左最大连续子段和,右最大连续子段和即可. 最后不知道怎么搞,query的时候返回了个结构体. #include <cstdio> #include ...

  9. ubuntu 下jrtplib编译

    我本来想用最新的 jrtplib-3.9.1和jthread-1.3.1,可是最新的那两个版本中都是用cmake进行配置的,我搞了好久,查了好多资料也没有解决交叉编译的问题,无奈,只能选用比较老的两个 ...

  10. Hibernate之HQL基本用法

    关于HQL HQL与SQL非常类似,只不过SQL的操作对象是数据表,列等对象,而HQL操作的是持久化类,实例,属性等. HQL是完全面向对象的查询语言,因此也具有面向对象的继承,多态等特性. 使用HQ ...