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还有一个附加值 实际上两道题的思路是一样的,第二题把值对应到位置 ...
随机推荐
- android 应用开发对大图片的处理
一,下载 android下载大图片(例如微博长图片)会出现OOM down掉问题 解决这个问题的办法是下载图片时先得到图片的宽度和高度,如果超出规定限制则对图片进行缩放 关键参数 1. BitmapF ...
- android玩耍(-) adbshell安装
一 什么是adbshell http://adbshell.com/ Android Debug Bridge (adb) is a command line tool that lets you c ...
- Python之父Guido在最近一次采访的最后说了啥
Python之父Guido在最近一次采访的最后说了啥? 在前些天的一次采访中,被问到Python未来发展方向的时候原文在infoworld,咱们可爱的python老爹Guido是这样说的: One t ...
- [Python]从豆瓣电影批量获取看过这部电影的用户列表
前言 由于之后要做一个实验,需要用到大量豆瓣用户的电影数据,因此想到了从豆瓣电影的“看过这部电影 的豆瓣成员”页面上来获取较为活跃的豆瓣电影用户. 链接分析 这是看过"模仿游戏"的 ...
- VS2012编译错误信息,错误列表却没显示
今天在写代码的时候,发现VS有编译错误,在错误列表里面却没有显示错误信息,百思不得其解. 后来终于发现,错误列表弄了个筛选,所以就看不到错误信息了,晕死.有遇到该问题的,可以参考下.
- Android系统Root原理初探(转)
http://www.imooc.com/learn/126 chkconfig setup 解压update.zip这个文件,可发现它一般打包了如下这几个文件: 或者没有updates而是syste ...
- 新装docker 从本地仓库下载
docker:/root# docker images Get http:///var/run/docker.sock/v1.19/images/json: dial unix /var/run/do ...
- error: ld returned 1 exit status 和 error:undefined reference
undefined reference 往往是链接时出现错误,无法解析引用.这篇文章总结的很好undefined reference问题总结 error: ld returned 1 exit sta ...
- 为Spring添加REST功能
1 关于REST 我的理解,REST就是将资源以最合适的形式在服务端和客户端之间传递. 系统中资源采用URL进行标识(可以理解为URL路径中带参数) 使用HTTP方法进行资源的管理(GET,PUT,P ...
- Linux网桥介绍
网桥的功能类似于二层交换机,作用都是划分冲突域,它们之前且一些细微的差别,此处不展开. Linux网桥作为一个特殊的网桥的实现,有一些自己的特点,因为没有看代码,只能从功能上简单分析一下.个人认为,L ...