文艺平衡树(区间翻转)(Splay模板)
#include<cstdio>
#include<iostream>
#include<cstring>
using namespace std;
const int N=1e5+;
int fa[N],cnt[N],son[N][],size[N],key[N],v[N],type,root;
int n,m,a[N];
inline int read()
{
int x=,f=;char ch=getchar();
while(ch<''||ch>'')
{if(ch=='-')f=-;ch=getchar();}
while(ch>=''&&ch<='')
{x=(x<<)+(x<<)+ch-'';ch=getchar();}
return x*f;
}
bool judge(int x)
{
return son[fa[x]][]==x;
}
void up(int x)
{
size[x]=size[son[x][]]+size[son[x][]]+;
}
void down(int x)
{
if(x&&v[x])
{
v[son[x][]]^=;
v[son[x][]]^=;
swap(son[x][],son[x][]);
v[x]=;
}
}
void rotate(int x)
{
int old=fa[x],oldf=fa[old],lr=judge(x);
down(old);down(x);
son[old][lr]=son[x][lr^];fa[son[old][lr]]=old;
son[x][lr^]=old;fa[old]=x;
fa[x]=oldf;
if(oldf)son[oldf][son[oldf][]==old]=x;
up(old);up(x);
}
void splay(int x,int goal)
{
for(int f;(f=fa[x])!=goal;rotate(x))
if(fa[f]!=goal)
rotate(judge(x)==judge(f)?f:x);
if(!goal)root=x;
}
int build(int f,int l,int r)
{
if(l>r)return ;
int mid=l+r>>,now=++type;
key[now]=a[mid];fa[now]=f;
v[now]=;
son[now][]=build(now,l,mid-);
son[now][]=build(now,mid+,r);
up(now);
return now;
}
int getrank(int x)
{
int now=root;
while()
{
down(now);
if(x<=size[son[now][]])now=son[now][];
else
{
x-=size[son[now][]]+;
if(!x)return now;
now=son[now][];
}
}
}
void rev(int l,int r)
{
l=getrank(l);
r=getrank(r+);
splay(l,);
splay(r,l);
down(root);
v[son[son[root][]][]]^=;
}
void print(int now)
{
down(now);
if(son[now][])print(son[now][]);
if(key[now]!=-0x3f3f3f3f&&key[now]!=0x3f3f3f3f)printf("%d ",key[now]);
if(key[son[now][]])print(son[now][]);
}
int main()
{
n=read();m=read();
for(int i=;i<=n;i++)a[i+]=i;
a[]=-0x3f3f3f3f;a[n+]=0x3f3f3f3f;
root=build(,,n+);
for(int i=;i<=m;i++)
{
int x=read(),y=read();
rev(x,y);
}
print(root);
return ;
}
文艺平衡树(区间翻转)(Splay模板)的更多相关文章
- Splay(区间翻转) 模板
洛谷:P3391 [模板]文艺平衡树(Splay) #include<cstdio> #include<iostream> #include<algorithm> ...
- fhq_treap || BZOJ 3223: Tyvj 1729 文艺平衡树 || Luogu P3391 【模板】文艺平衡树(Splay)
题面: [模板]文艺平衡树(Splay) 题解:无 代码: #include<cstdio> #include<cstring> #include<iostream> ...
- bzoj3223 文艺平衡树 codevs3303 翻转区间
splay模版题吧 只有区间翻转 至于为什么要把须翻转区间旋到根 因为查找一个区间可以先找出他左端点左边第一个点和右端点x右边第一个点y 然后将x旋到根节点 y旋到x的右儿子 这样x的右边的点就是所有 ...
- bzoj3223 文艺平衡树 (treap or splay分裂+合并)
3223: Tyvj 1729 文艺平衡树 Time Limit: 10 Sec Memory Limit: 128 MB Submit: 3313 Solved: 1883 [Submit][S ...
- 文艺平衡树 lg3391(splay维护区间入门)
splay是支持区间操作的,先做这道题入个门 大多数操作都和普通splay一样,就不多解释了,只解释一下不大一样的操作 #include<bits/stdc++.h> using name ...
- BZOJ 3223 文艺平衡树 [codevs3303翻转区间]
AC通道:http://www.lydsy.com/JudgeOnline/problem.php?id=3223 通道2:http://codevs.cn/problem/3303/ 题目分析: 我 ...
- 【BZOJ3223】 Tyvj 1729 文艺平衡树 Splay
Description 您需要写一种数据结构(可参考题目标题),来维护一个有序数列,其中需要提供以下操作:翻转一个区间,例如原有序序列是5 4 3 2 1,翻转区间是[2,4]的话,结果是5 2 ...
- JZYZOJ1998 [bzoj3223] 文艺平衡树 splay 平衡树
http://172.20.6.3/Problem_Show.asp?id=1998 平衡树区间翻转的板子,重新写一遍,给自己码一个板子. #include<iostream> #incl ...
- BZOJ 3223: Tyvj 1729 文艺平衡树-Splay树(区间翻转)模板题
3223: Tyvj 1729 文艺平衡树 Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 6881 Solved: 4213[Submit][Sta ...
- bzoj3223 Tyvj 1729 文艺平衡树(Splay Tree+区间翻转)
3223: Tyvj 1729 文艺平衡树 Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 2202 Solved: 1226[Submit][Sta ...
随机推荐
- 察看linux 发行版
好像没有太通用的方法. 看一下/etc/redhat-release. redhat 系列(包括centos) 会有如下内容 [root@localhost ~]# cat /etc/redhat- ...
- one troubleshooting case about em access issue
Today when trying to open my Oracle EM url, I can not open it. So I thought may be the network is ha ...
- Node.js 博客实例(三)添加文件上传功能
原教程 https://github.com/nswbmw/N-blog/wiki/_pages的第三章 上传文件眼下有三种方法: 使用 Express 自带的文件上传功能,不涉及数据库 使用 For ...
- Js跨一级域名同步cookie
1. 纯Js同步两个域名下的cookie document.cookie = "name=" + "value;" + "expires=" ...
- 浅析Linux字符设备驱动程序内核机制
前段时间在学习linux设备驱动的时候,看了陈学松著的<深入Linux设备驱动程序内核机制>一书. 说实话.这是一本非常好的书,作者不但给出了在设备驱动程序开发过程中的所须要的知识点(如对 ...
- Java遍历一个文件夹下的全部文件
Java工具中为我们提供了一个用于管理文件系统的类,这个类就是File类,File类与其它流类不同的是,流类关心的是文件的内容.而File类关心的是磁盘上文件的存储. 一,File类有多个构造器,经常 ...
- Codesys——限定符的使用方法[来自Codesys的Help]
Qualifier for actions in SFC In order to configure in which way the actions should be associated to ...
- Codeforces--14D--Two Paths(树的直径)
Two Paths Time Limit: 2000MS Memory Limit: 65536KB 64bit IO Format: %I64d & %I64u Submit ...
- 【SCOI 2011】 糖果
[题目链接] 点击打开链接 [算法] 当x = 1时,连边(a,b,0)和(b,a,0) 当x = 2时,连边(a,b,1) 当x = 3时,连边(b,a,0) 当x = 4时,连边(b,a,1) 当 ...
- PCB 脱离IIS的Web应用
在用.net Web编程中,我们写好的Web应用首选会挂在IIS上面,因为它足稳定并且功能齐全,但这不是我们唯一的选择,微软给我们提供了Owin组件,Web应该的宿主可以不再是IIS了,有了Owin后 ...