文艺平衡树(区间翻转)(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 ...
随机推荐
- OpenJudge百炼习题解答(C++)--题2704:竞赛评分
题: 总时间限制: 1000ms 内存限制: 65536kB 描写叙述 现举行一次小竞赛,參赛的3支队伍,编号为1,2,3.每支队列轮流回答问题,假设回答正确,加10分;回答错误,扣10分;放弃 ...
- muduo库源码剖析(一) reactor模式
一. Reactor模式简介 Reactor释义“反应堆”,是一种事件驱动机制.和普通函数调用的不同之处在于:应用程序不是主动的调用某个API完成处理,而是恰恰相反,Reactor逆置了事件处理流程, ...
- 友盟 个推 微信sdk spring boot
友盟 个推 微信sdk spring boot ngix mongodb memory cache
- [Vue @Component] Dynamic Vue.js Components with the component element
You can dynamically switch between components in a template by using the reserved <component> ...
- 安卓下载文件怎样更新UI进度
曾经写过几篇关于下载的文章.总的来说是下面几点: 1.维护一个下载进程的Hashmap,key:使用Md5进行处理后的文件下载地址,value为下载的Task. 以防止下载反复.并将信息保存至数据库. ...
- [WebGL入门]十二,模型数据和顶点属性
注:文章译自http://wgld.org/,原作者杉本雅広(doxas),文章中假设有我的额外说明,我会加上[lufy:].另外.鄙人webgl研究还不够深入,一些专业词语.假设翻译有误.欢迎大家指 ...
- 辛星浅谈mysql中的数据碎片以及引擎为MyISAM下的操作
对于mysql中的数据碎片,事实上和我们删除数据是息息相关的,删除数据的时候必定会在数据文件里造成不连续的空白空间,对于少量的数据的删除,并不会产生多少的空白空间.假设在一段时间内的大量的删除操作,会 ...
- LeetCode 970. Powerful Integers (强整数)
题目标签:HashMap 题目让我们找出所有独一的powerful integers 小于bound的情况下. 把 x^i 看作 a:把 y^j 看作b, 代入for loop,把所有的情况都遍历一遍 ...
- LeetCode 961. N-Repeated Element in Size 2N Array (重复 N 次的元素)
题目标签:HashMap 题目给了我们一个size 为 2N 的int array,其中有 N + 1 个唯一的 数字,让我们找出那个重复的数字. 利用hashset,把每一个数字存入,一旦发现有重复 ...
- B. Case of Fake Numbers( Codeforces Round #310 (Div. 2) 简单题)
B. Case of Fake Numbers time limit per test 2 seconds memory limit per test 256 megabytes input stan ...