解题关键:splay模板题。

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cstdlib>
#include<iostream>
#include<cmath>
using namespace std;
typedef long long ll;
const int N = ; int ch[N][],par[N],val[N],cnt[N],size[N],rev[N],root,ncnt;
int n,m,x,y; bool chk(int x){
return ch[par[x]][]==x;
} void pushup(int x){
size[x]=size[ch[x][]]+size[ch[x][]]+cnt[x];
} void pushdown(int x){
if(rev[x]){
swap(ch[x][],ch[x][]);//反转就是交换左右子树即可成立。
rev[ch[x][]]^=;
rev[ch[x][]]^=;
rev[x]=;
}
} void rotate(int x){
int y=par[x],z=par[y],k=chk(x),w=ch[x][k^];
ch[y][k]=w;par[w]=y;
ch[z][chk(y)]=x;par[x]=z;
ch[x][k^]=y;par[y]=x;
pushup(y);pushup(x);
} void splay(int x,int goal=){
while(par[x]!=goal){
int y=par[x],z=par[y];
if(z!=goal){
if(chk(x)==chk(y)) rotate(y);
else rotate(x);
}
rotate(x);
}
if(!goal) root=x;
} void insert(int x){
int cur=root,p=;
while(cur&&val[cur]!=x){
p=cur;
cur=ch[cur][x>val[cur]];
}
if(cur){
cnt[cur]++;
}else{
cur=++ncnt;
if(p) ch[p][x>val[p]]=cur;
ch[cur][]=ch[cur][]=;
par[cur]=p;val[cur]=x;
cnt[cur]=size[cur]=;
}
splay(cur);
} void find(int x){
int cur=root;
if(!cur) return;
while(ch[cur][x>val[cur]]&&x!=val[cur]){
cur=ch[cur][x>val[cur]];
}
splay(cur);
}
//从1开始计数
int kth(int k){
k++;
int cur=root;
while(){
pushdown(cur);
if(ch[cur][]&&k<=size[ch[cur][]]){
cur=ch[cur][];
}else if(k>size[ch[cur][]]+cnt[cur]){
k-=size[ch[cur][]]+cnt[cur];
cur=ch[cur][];
}else{
return cur;
}
}
} int rnk(int x){
find(x);
if(val[root]>=x) return size[ch[root][]];
else return size[ch[root][]]+cnt[root];
} void reverse(int l,int r){
int x=kth(l-),y=kth(r+);
splay(x);splay(y,x);
rev[ch[y][]]^=;
} int pre(int x){
find(x);
if(val[root]<x) return root;
int cur=ch[root][];
while(ch[cur][]) cur=ch[cur][];
return cur;
} int succ(int x) {
find(x);
if(val[root]>x) return root;
int cur=ch[root][];
while(ch[cur][]) cur=ch[cur][];
return cur;
} void output(int x){
pushdown(x);
if(ch[x][]) output(ch[x][]);
if(val[x]<=n&&val[x]>=) printf("%d ",val[x]);
if(ch[x][]) output(ch[x][]);
} void init(){
insert(-2e9);
insert(2e9);
} int main(){
init();
scanf("%d%d",&n,&m);
for(int i=;i<=n;i++) insert(i);
while(m--){
scanf("%d%d",&x,&y);
reverse(x,y);
}
output(root);
return ;
}

[bzoj3223]文艺平衡树(splay区间反转模板)的更多相关文章

  1. BZOJ3223 文艺平衡树(splay)

    题目背景 这是一道经典的Splay模板题——文艺平衡树. 题目描述 您需要写一种数据结构(可参考题目标题),来维护一个有序数列,其中需要提供以下操作:翻转一个区间,例如原有序序列是5 4 3 2 1, ...

  2. JZYZOJ1998 [bzoj3223] 文艺平衡树 splay 平衡树

    http://172.20.6.3/Problem_Show.asp?id=1998 平衡树区间翻转的板子,重新写一遍,给自己码一个板子. #include<iostream> #incl ...

  3. [bzoj3223]文艺平衡树——splay

    题意 你应当编写一个数据结构,支持以下操作: 反转一个区间 题解 我们把在数组中的位置当作权值,这样原序列就在这种权值意义下有序,我们考虑使用splay维护. 对于操作rev[l,r],我们首先把l- ...

  4. bzoj 3223 文艺平衡树 splay 区间翻转

    Tyvj 1728 普通平衡树 Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 17715  Solved: 7769[Submit][Status][ ...

  5. 算法模板——splay区间反转 2

    实现功能:同splay区间反转 1(基于BZOJ3223 文艺平衡树) 这次改用了一个全新的模板(HansBug:琢磨了我大半天啊有木有),大大简化了程序,同时对于splay的功能也有所完善 这里面没 ...

  6. 【阶梯报告】洛谷P3391【模板】文艺平衡树 splay

    [阶梯报告]洛谷P3391[模板]文艺平衡树 splay 题目链接在这里[链接](https://www.luogu.org/problemnew/show/P3391)最近在学习splay,终于做对 ...

  7. luoguP3391[模板]文艺平衡树(Splay) 题解

    链接一下题目:luoguP3391[模板]文艺平衡树(Splay) 平衡树解析 这里的Splay维护的显然不再是权值排序 现在按照的是序列中的编号排序(不过在这道题目里面就是权值诶...) 那么,继续 ...

  8. BZOJ 3223: Tyvj 1729 文艺平衡树-Splay树(区间翻转)模板题

    3223: Tyvj 1729 文艺平衡树 Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 6881  Solved: 4213[Submit][Sta ...

  9. BZOJ3223: Tyvj 1729 文艺平衡树 [splay]

    3223: Tyvj 1729 文艺平衡树 Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 3595  Solved: 2029[Submit][Sta ...

随机推荐

  1. Golang Printf、Sprintf 、Fprintf 格式化

    /* %v 输出结构体 {10 30} %+v 输出结构体显示字段名 {one:10 tow:30} %#v 输出结构体源代码片段 main.Point{one:10, tow:30} %T 输出值的 ...

  2. (十七)js bom/dom

    window 是所有BOM中所有对象的核心. window 的属性 window.self代表自己本身,相当于window. eg: console.log(window.self === windo ...

  3. TCP, Scoket, HTTP(转)

    1.TCP连接 要想明白Socket连接,先要明白TCP连接.手机能够使用联网功能是因为手机底层实现了TCP/IP协议,可以使手机终端通过无线网络建立TCP连接.TCP协议可以对上层网络提供接口,使上 ...

  4. HDU - 6041:I Curse Myself(Tarjan求环&K路归并)

    There is a connected undirected graph with weights on its edges. It is guaranteed that each edge app ...

  5. HDU - 6433: H. Pow (简答题,输出大数)

    There are n numbers 3^0, 3^1, . . . , 3^n-1. Each time you can choose a subset of them (may be empty ...

  6. hibernate正向工程生成数据库

    hibernate正向工程生成数据库 hibernate.cfg.xml ? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 2 ...

  7. #51单片机#蓝牙模块(ATK-SPP-HC06从机串口)的使用方法

    #include <AT89X51.H> #include <intrins.h> // 函数原形定义 #define uchar unsigned char #define ...

  8. (转)java.lang.RuntimeException: Missing type parameter

    java.lang.RuntimeException: Missing type parameter (2015-04-07 14:35:51)   分类: 技术 程序中用到了gson的new typ ...

  9. composer安装特别慢的解决方案

    在项目开发的时候 有许多新的依赖要按照 就需要用到composer 但是由于国内安装下载速度贼慢(国外的网站连接速度太慢,并且随时可能被墙) 安装一个excel扩展(composer require ...

  10. SQL2005 如何在没有日志文件的情况下如何恢复MDF数据库文件?

    第一步:先建立一个同名数据库,停止SQL SERVER2005,将没有日志的的.mdf数据库文件覆盖刚新建的.mdf数据库文件,重新启动数据库. 第二步:在查询分析器中运行如下代码(将数据库名修改为您 ...