BZOJ 3223 Tyvj 1729 文艺平衡树(Splay)
【题目链接】 http://www.lydsy.com/JudgeOnline/problem.php?id=3223
【题目大意】
给出一数列,问m次区间翻转后的结果。
【题解】
Splay 区间翻转裸题
【代码】
#include <cstdio>
#include <algorithm>
using namespace std;
const int N=200005;
int n,m,a[N],val[N],mn[N],tag[N],size[N],x,y;
int son[N][2],f[N],tot,root;bool rev[N];
void rev1(int x){if(!x)return;swap(son[x][0],son[x][1]);rev[x]^=1;}
void add1(int x,int p){if(!x)return;val[x]+=p;mn[x]+=p;tag[x]+=p;}
void pb(int x){
if(rev[x]){
rev1(son[x][0]);
rev1(son[x][1]);
rev[x]=0;
}
if(tag[x]){
add1(son[x][0],tag[x]);
add1(son[x][1],tag[x]);
tag[x]=0;
}
}
void up(int x){
size[x]=1,mn[x]=val[x];
if(son[x][0]){
size[x]+=size[son[x][0]];
if(mn[x]>mn[son[x][0]])mn[x]=mn[son[x][0]];
}
if(son[x][1]){
size[x]+=size[son[x][1]];
if(mn[x]>mn[son[x][1]])mn[x]=mn[son[x][1]];
}
}
int build(int l,int r,int fa){
int x=++tot,mid=(l+r)>>1;
f[x]=fa;val[x]=a[mid];
if(l<mid)son[x][0]=build(l,mid-1,x);
if(r>mid)son[x][1]=build(mid+1,r,x);
up(x);
return x;
}
void rotate(int x){
int y=f[x],w=son[y][1]==x;
son[y][w]=son[x][w^1];
if(son[x][w^1])f[son[x][w^1]]=y;
if(f[y]){
int z=f[y];
if(son[z][0]==y)son[z][0]=x;
if(son[z][1]==y)son[z][1]=x;
}f[x]=f[y];son[x][w^1]=y;f[y]=x;up(y);
}
void splay(int x,int w){
int s=1,i=x,y;a[1]=x;
while(f[i])a[++s]=i=f[i];
while(s)pb(a[s--]);
while(f[x]!=w){
y=f[x];
if(f[y]!=w){if((son[f[y]][0]==y)^(son[y][0]==x))rotate(x);else rotate(y);}
rotate(x);
}if(!w)root=x;
up(x);
}
int kth(int k){
int x=root,tmp;
while(1){
pb(x);
tmp=size[son[x][0]]+1;
if(k==tmp)return x;
if(k<tmp)x=son[x][0];else k-=tmp,x=son[x][1];
}
}
int main(){
scanf("%d",&n);
for(int i=1;i<=n;i++)a[i]=i;
root=build(0,n+1,0);
scanf("%d",&m);
while(m--){
scanf("%d%d",&x,&y);
x=kth(x),y=kth(y+2);
splay(x,0),splay(y,x),rev1(son[y][0]);
}for(int i=1;i<=n;i++){
x=kth(i+1);
printf("%d ",val[x]);
}return 0;
}
BZOJ 3223 Tyvj 1729 文艺平衡树(Splay)的更多相关文章
- BZOJ 3223: Tyvj 1729 文艺平衡树(splay)
速度居然进前十了...第八... splay, 区间翻转,用一个类似线段树的lazy标记表示是否翻转 ------------------------------------------------- ...
- bzoj 3223: Tyvj 1729 文艺平衡树 (splay)
链接: https://www.lydsy.com/JudgeOnline/problem.php?id=3223 题面: 3223: Tyvj 1729 文艺平衡树 Time Limit: 10 S ...
- BZOJ 3223: Tyvj 1729 文艺平衡树-Splay树(区间翻转)模板题
3223: Tyvj 1729 文艺平衡树 Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 6881 Solved: 4213[Submit][Sta ...
- bzoj 3223/tyvj 1729 文艺平衡树 splay tree
原题链接:http://www.tyvj.cn/p/1729 这道题以前用c语言写的splay tree水过了.. 现在接触了c++重写一遍... 只涉及区间翻转,由于没有删除操作故不带垃圾回收,具体 ...
- BZOJ - 3223 Tyvj 1729 文艺平衡树 (splay/无旋treap)
题目链接 splay: #include<bits/stdc++.h> using namespace std; typedef long long ll; ,inf=0x3f3f3f3f ...
- BZOJ 3223 Tyvj 1729 文艺平衡树 | Splay 维护序列关系
题解: 每次reverse(l,r) 把l-1转到根,r+1变成他的右儿子,给r+1的左儿子打个标记就是一次反转操作了 每次find和dfs输出的时候下放标记,把左儿子和右儿子换一下 记得建树的时候建 ...
- BZOJ 3223: Tyvj 1729 文艺平衡树
3223: Tyvj 1729 文艺平衡树 Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 3628 Solved: 2052[Submit][Sta ...
- fhq_treap || BZOJ 3223: Tyvj 1729 文艺平衡树 || Luogu P3391 【模板】文艺平衡树(Splay)
题面: [模板]文艺平衡树(Splay) 题解:无 代码: #include<cstdio> #include<cstring> #include<iostream> ...
- [BZOJ 3223 & Tyvj 1729]文艺平衡树 & [CodeVS 3243]区间翻转
题目不说了,就是区间翻转 传送门:BZOJ 3223 和 CodeVS 3243 第一道题中是1~n的区间翻转,而第二道题对于每个1~n还有一个附加值 实际上两道题的思路是一样的,第二题把值对应到位置 ...
随机推荐
- spring sts 从数据库中反向生成实体类
首先我们要在sts中建立mysql的数据库连接 1. 当点击ok之后,如果没有报错的话就应该是建立好了,我们可以点击查看这个数据库中所有的表 我们就可以再sts进行数据库操作了,具体如下: 点击如下按 ...
- bootstrap 3 のcheckbox-inline
<div class="form-group"> <p class="control-label"><b> ...
- LPC基础教程-Lpc程序和编程环境 mudos 加载原理
编程环境 通常我们所见到的Mud大多是LpMud.LpMuds使用Unix的指令和文件结构.如果你对Unix有所了解,那么LpMud中的一些指令和它的文件结构与普通的Unix基本一样.如果你从未使用过 ...
- php 数据结构 hash表
hash表 定义 hash表定义了一种将字符组成的字符串转换为固定长度(一般是更短长度)的数值或索引值的方法,称为散列法,也叫哈希法.由于通过更短的哈希值比用原始值进行数据库搜索更快,这种方法一般用来 ...
- html5 OPOA
1.0 one page one application 一个应用只有一个页面,对用户的表现来说的. 2.0 起源于web的MIS系统 MIS(管理信息系统) 3.0 银行客户端 4. ...
- RenderPartial: No overload for method 'Write' takes 0 arguments
如下方法调用RenderPartial: 报“No overload for method 'Write' takes 0 arguments”的错误: @if (@Model != null &am ...
- Log4j的应用实例(转)
转自:http://www.cnblogs.com/eflylab/archive/2007/01/12/618080.html 在Log4J使用笔记中没有怎么写实例,那么在这篇中我将Log4j的一个 ...
- USB 3.1 Type-C
[時報記者任珮云台北報導]微軟.英特爾.蘋果今年將新款PC介面升級至USB 3.1規格,Wintel陣營今年新款PC產品亦將全面採用USB 3.1介面,里昂證出具最新的報告指出,在新趨勢帶動下,台廠的 ...
- org.springframework.context.event.AbstractApplicationEventMulticaster
private Collection<ApplicationListener<?>> retrieveApplicationListeners(ResolvableType e ...
- Gradient boosting
Gradient boosting gradient boosting 是一种boosting(组合弱学习器得到强学习器)算法中的一种,可以把学习算法(logistic regression,deci ...