文艺平衡树(bzoj 3223)
Description
您需要写一种数据结构(可参考题目标题),来维护一个有序数列,其中需要提供以下操作:翻转一个区间,例如原有序序列是5 4 3 2 1,翻转区间是[2,4]的话,结果是5 2 3 4 1
Input
第一行为n,m n表示初始序列有n个数,这个序列依次是(1,2……n-1,n) m表示翻转操作次数
接下来m行每行两个数[l,r] 数据保证 1<=l<=r<=n
Output
输出一行n个数字,表示原始序列经过m次变换后的结果
Sample Input
1 3
1 3
1 4
Sample Output
HINT
N,M<=100000
/*区间翻转*/
#include<cstdio>
#include<iostream>
#define N 100010
using namespace std;
int fa[N],son[N][],id[N],sz[N],rev[N];
int n,m,size,rt;
void pushup(int k){
int l=son[k][],r=son[k][];
sz[k]=sz[l]+sz[r]+;
}
void pushdown(int k){
int l=son[k][],r=son[k][];
if(rev[k]){
swap(son[k][],son[k][]);
rev[l]^=;rev[r]^=;
rev[k]=;
}
}
void rotate(int x,int &k){
int y=fa[x],z=fa[y],l,r;
if(son[y][]==x)l=;else l=;r=l^;
if(y==k)k=x;
else {
if(son[z][]==y) son[z][]=x;
else son[z][]=x;
}
fa[x]=z;fa[y]=x;fa[son[x][r]]=y;
son[y][l]=son[x][r];son[x][r]=y;
pushup(y);pushup(x);
}
void splay(int x,int &k){
while(x!=k){
int y=fa[x],z=fa[y];
if(y!=k){
if((son[y][]==x)^(son[z][]==y)) rotate(x,k);
else rotate(y,k);
}
rotate(x,k);
}
}
int find(int k,int rank){
pushdown(k);
int l=son[k][],r=son[k][];
if(sz[l]+==rank)return k;
else if(sz[l]>=rank) return find(l,rank);
else return find(r,rank-sz[l]-);
}
void rever(int l,int r){
int x=find(rt,l),y=find(rt,r+);
splay(x,rt);splay(y,son[x][]);
int z=son[y][];rev[z]^=;
}
void build(int l,int r,int f){
if(l>r)return;
int now=id[l],last=id[f];
if(l==r){
fa[now]=last;sz[now]=;
if(l<f)son[last][]=now;
else son[last][]=now;
return;
}
int mid=l+r>>;now=id[mid];
build(l,mid-,mid);build(mid+,r,mid);
fa[now]=last;pushup(mid);
if(mid<f)son[last][]=now;
else son[last][]=now;
}
int main(){
scanf("%d%d",&n,&m);
for(int i=;i<=n+;i++)
id[i]=++size;
build(,n+,);rt=(n+)>>;
for(int i=;i<=m;i++){
int l,r;scanf("%d%d",&l,&r);
rever(l,r);
}
for(int i=;i<=n+;i++)
printf("%d ",find(rt,i)-);
return ;
}
文艺平衡树(bzoj 3223)的更多相关文章
- 【splay】文艺平衡树 BZOJ 3223
Description 您需要写一种数据结构(可参考题目标题),来维护一个有序数列,其中需要提供以下操作:翻转一个区间,例如原有序序列是5 4 3 2 1,翻转区间是[2,4]的话,结果是5 2 3 ...
- 3223: Tyvj 1729 文艺平衡树 - BZOJ
Description 您需要写一种数据结构(可参考题目标题),来维护一个有序数列,其中需要提供以下操作:翻转一个区间,例如原有序序列是5 4 3 2 1,翻转区间是[2,4]的话,结果是5 2 3 ...
- [题解]bzoj 3223 文艺平衡树
3223: Tyvj 1729 文艺平衡树 Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 3884 Solved: 2235[Submit][Sta ...
- bzoj 3223 文艺平衡树 - Splay
3223: Tyvj 1729 文艺平衡树 Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 3884 Solved: 2235[Submit][Sta ...
- BZOJ 3223: Tyvj 1729 文艺平衡树
3223: Tyvj 1729 文艺平衡树 Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 3628 Solved: 2052[Submit][Sta ...
- 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]文艺平衡树 & [CodeVS 3243]区间翻转
题目不说了,就是区间翻转 传送门:BZOJ 3223 和 CodeVS 3243 第一道题中是1~n的区间翻转,而第二道题对于每个1~n还有一个附加值 实际上两道题的思路是一样的,第二题把值对应到位置 ...
- fhq_treap || BZOJ 3223: Tyvj 1729 文艺平衡树 || Luogu P3391 【模板】文艺平衡树(Splay)
题面: [模板]文艺平衡树(Splay) 题解:无 代码: #include<cstdio> #include<cstring> #include<iostream> ...
随机推荐
- openstack No valid host was found. There are not enough hosts available.
root@dell-PowerEdge-T30:~# gedit /var/log/nova/nova-conductor.logroot@dell-PowerEdge-T30:~# gedit /v ...
- DataModel doesn't have preference values
mahout和hadoop实现简单的智能推荐系统的时候,出现了一下几个方面的错误 DataModel doesn't have preference values 意思是DataModel中没有找到初 ...
- select a.no,a.name,b.subid,b.subname,c.score
select a.no,a.name,b.subid,b.subname,c.score from a,b,c where a.no = c.no and b.subid = c.subid ;
- GUI进化--数据与界面分离
http://blog.csdn.net/doon/article/details/5946862 1.何谓数据和界面分离? GUI,即Graphic User Interface,人机交换界面.连接 ...
- MINST手写数字识别(三)—— 使用antirectifier替换ReLU激活函数
这是一个来自官网的示例:https://github.com/keras-team/keras/blob/master/examples/antirectifier.py 与之前的MINST手写数字识 ...
- django 2.0 + pycharm2017 出现的问题
在创建完成app之后,在models文件里创建两个类:BlogType , Blog, 创建超级用户,注册admin,在登陆admin之后发现,发现 BlogType , Blog,并没有导入到adm ...
- GYM 101604 || 20181010
看着前面咕咕咕的国庆集训 难受 十月十日要萌一天哇www A.字符串 题意:给定一个字符串 问能否交换两个字符或者不交换字符,使其成为回文串 之前写的太丑 重写一遍加一堆 if 竟然过了w 思路:求出 ...
- Python基础篇 -- if while 语句
2.7 if语句 # 单纯if if 条件: 代码块 当条件成立,执行代码块 # 二选一 if 条件: 代码块1 else: 代码块2 #当条件为真,执行代码块1,否则执行代码块2 # 多选一 没有e ...
- thinkphp 结合phpexcel实现excel导入
控制器文件: class ExcelAction extends Action { public function __construct() { import('ORG.Util.ExcelToAr ...
- javascript设计模式(张容铭) 第14章 超值午餐-组合模式 学习笔记
JS 组合模式更常用于创建表单上,比如注册页面可能有不同的表单提交模块.对于这些需求我们只需要有基本的个体,然后通过一定的组合即可实现,比如下面这个页面样式(如图14-2所示),我们来用组合模式实现. ...