splay(1区间翻转区间最值与区间修改)
直接上板子,这个要好好体会
操作是最经典的。
#include <algorithm>
#include <iostream>
#include <cstring>
#include <cstdlib>
#include <cstdio>
#include <vector>
#include <cmath>
#include <queue>
#include <map>
#include <set>
using namespace std;
#define file(x) freopen(x".in","r",stdin),freopen(x".out","w",stdout)
inline void read(int &ans) {
ans=;char x=getchar();bool f=;
while(x<''||x>'') {if(x=='-')f=;x=getchar();}
while(x>=''&&x<='')ans=ans*+x-'',x=getchar();
f?ans=-ans:;
}
const int MAXN=1e5+;
const int INF=2e9;
struct splay_tree{
struct node{
int val,max,add,size,son[];
bool rev;//区间是否翻转
void init(int _val){
val=max=_val ,size=;
add=rev=son[]=son[]=;
}
}T[MAXN];
int fa[MAXN],root; void pushup(int x){
T[x].max=T[x].val ,T[x].size=;
for(int k=;k<;k++)
if(T[x].son[k]){
T[x].max=max(T[x].max,T[T[x].son[k]].max);
T[x].size+=T[T[x].son[k]].size;
}
} void pushdown(int x){
if(x==)return;
if(T[x].add){
for(int k=;k<;k++)
if(T[x].son[k]){
T[T[x].son[k]].val+=T[x].add;
T[T[x].son[k]].max+=T[x].add;
T[T[x].son[k]].add+=T[x].add;
}
T[x].add=;
}
if(T[x].rev){
for(int k=;k<;k++)
if(T[x].son[k]) T[T[x].son[k]].rev^=;
swap(T[x].son[],T[x].son[]);
T[x].rev=;
}
} void rotate(int x,int k){
int y=fa[x] ,z=fa[y];
T[y].son[!k]=T[x].son[k] ,fa[T[x].son[k]]=y;
T[x].son[k]=y ,fa[y]=x;
T[z].son[T[z].son[]==y]=x ,fa[x]=z;
pushup(y);
} void splay(int x,int goal){
if(x==goal)return;
while(fa[x]!=goal){
int y=fa[x] ,z=fa[y];
pushdown(z) ,pushdown(y) ,pushdown(x);
int rx=T[y].son[]==x ,ry=T[z].son[]==y;
if(z==goal)rotate(x,rx);
else{
if(rx==ry)rotate(y,ry);
else rotate(x,rx);
rotate(x,ry);
}
}
pushup(x);
if(goal==)root=x;
} int select(int p){
int u=root;
pushdown(u);
while(p!=T[T[u].son[]].size){
if(p<T[T[u].son[]].size)u=T[u].son[];
else{
p-=T[T[u].son[]].size+;
u=T[u].son[];
}
pushdown(u);
}
return u;
} void updata(int L,int R,int val){
int u=select(L-) ,v=select(R+);
splay(u,);
splay(v,u);
T[T[v].son[]].max+=val;
T[T[v].son[]].val+=val;
T[T[v].son[]].add+=val;
} void reverse(int l,int r){
int u=select(l-) ,v=select(r+);
splay(u,);
splay(v,u);
T[T[v].son[]].rev^=;
} int query(int l,int r){
int u=select(l-) ,v=select(r+);
splay(u,);
splay(v,u);
return T[T[v].son[]].max;
} int build(int l,int r){
if(l>r) return ;
if(l==r) return l;
int mid=(l+r)>> ,ls, rs;
ls=T[mid].son[]=build(l,mid-);
rs=T[mid].son[]=build(mid+,r);
fa[ls]=fa[rs]=mid;
pushup(mid);
return mid;
} void init(int n){
T[].init(-INF) ,T[].init(-INF) ,T[n+].init(-INF);
for(int i=;i<=n+;i++)T[i].init();
root=build(,n+) ,fa[root]=;
fa[]= ,T[].son[]=root ,T[].size=;
}
}; splay_tree bzoj1251;
int n,m,a,b,c,d;
int main() {
read(n),read(m);
bzoj1251.init(n);
for(int i=;i<m;i++){
read(a),read(b),read(c);
if(a==)read(d),bzoj1251.updata(b,c,d);
else if(a==)bzoj1251.reverse(b,c);
else printf("%d\n",bzoj1251.query(b,c));
}
return ;
}
splay(1区间翻转区间最值与区间修改)的更多相关文章
- bzoj 1251序列终结者 splay 区间翻转,最值,区间更新
序列终结者 Time Limit: 20 Sec Memory Limit: 162 MBSubmit: 4594 Solved: 1939[Submit][Status][Discuss] De ...
- 【poj1901-求区间第k大值(带修改)】树状数组套主席树
901: Zju2112 Dynamic Rankings Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 7025 Solved: 2925[Sub ...
- CODEVS 4655 序列终结者-splay(区间更新、区间翻转、区间最值)
4655 序列终结者 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 大师 Master 题解 题目描述 Description 网上有许多题,就是给定一个序列,要 ...
- splay区间翻转
原题P3391 [模板]文艺平衡树(Splay) 题目背景 这是一道经典的Splay模板题——文艺平衡树. 题目描述 您需要写一种数据结构(可参考题目标题),来维护一个有序数列,其中需要提供以下操作: ...
- hdu-1890-Robotic Sort splay区间翻转
题意: 依次找第i大的数下标pos[i],然后将区间[i,pos[i]]翻转 分析: splay树区间翻转 // File Name: ACM/HDU/1890.cpp // Author: Zlbi ...
- bzoj3223 Tyvj 1729 文艺平衡树(Splay Tree+区间翻转)
3223: Tyvj 1729 文艺平衡树 Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 2202 Solved: 1226[Submit][Sta ...
- hdu1890 splay维护区间翻转
这题的建模有点不太一样,是按结点横坐标赋予键值的 同时每次rotate和splay时都要注意下往上往下更新 /* 先建立好splay tree,将结点按num/输入顺序排序,遍历时每次将当前结点提到根 ...
- BZOJ 3223: Tyvj 1729 文艺平衡树-Splay树(区间翻转)模板题
3223: Tyvj 1729 文艺平衡树 Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 6881 Solved: 4213[Submit][Sta ...
- bzoj 3223 文艺平衡树 splay 区间翻转
Tyvj 1728 普通平衡树 Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 17715 Solved: 7769[Submit][Status][ ...
- BZOJ 3223 Splay区间翻转
思路: 区间翻转的裸题 终于tm理解splay了-- //By SiriusRen #include <cstdio> #include <cstring> #include ...
随机推荐
- C语言 Char* 和Char 用法
分类专栏: C语言 版权声明:本文为博主原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明. 本文链接:https://blog.csdn.net/imxlw00/ar ...
- Gauss消元模板
; //高斯消元模板 //----------------------------------------------------------------------------------- //把 ...
- java自动化测试-json返回值校验
参考: https://blog.csdn.net/xkhgnc_6666/article/details/50250283 实现举例:
- How to write a paper in a weekend - by Prof. Pete Carr
Key points: don't procrastinate; review the notes and renew the literature search; determine who you ...
- phpstorm更换主题
打开PhpStorm,File -- Settings -- Editor -- Color Scheme --General选择你喜欢的风格进行更改,选择完成后单击Apply
- vue报错 [Vue warn]: Avoid mutating a prop directly since the value will be overwritten whenever the parent component re-renders. Instead, use a data or computed property based on the prop's
[Vue warn]: Avoid mutating a prop directly since the value will be overwritten whenever the parent c ...
- java 学习(day1)
之前学java没好好听课,会一点又不熟练,于是准备重新开始学一些细节,记录每日所学新知识. a+b java的a+b很有意思,当你输出的是" "+a+b,先假设a=2,b=3.然后 ...
- docker部署java应用程序
https://docs.docker.com/get-started/ 安装docker 1.安装docker apt install docker 2.配置docker加速器 安装完成后在 ...
- Python入门6 —— 流程控制 - if判断
代码块: 1.代码块指的是同一级别的代码,在python中用缩进相同的空格数(除了顶级代码块无任何缩进之外,其余代码块都是在原有的基础上缩进4个空格)来标识同一级的代码块 2.同一级别的代码块会按照自 ...
- Art Union
A well-known art union called "Kalevich is Alive!" manufactures objects d'art (pictures). ...