题目大意:略

splay维护区间翻转裸题,为了减少不必要的麻烦,多插入两个点,分别是0和n+1

每次找区间的第K个值,就在splay上二分即可

顺便学了一下splay的完美建树,而且splay有一些小函数可以宏定义或者用inline,跑得飞快

最后跑一遍中序遍历即可

 #include <cstdio>
#include <cstring>
#include <algorithm>
#define N 100100
#define il inline
#define ll long long
#define root d[0].ch[1]
#define con(x,ff,p) d[x].fa=ff,d[ff].ch[p]=x
#define idf(x) d[d[x].fa].ch[0]==x?0:1
#define lb(x) (x&(-x))
using namespace std; int n,m,cnt;
struct SPLAY{
int fa,ch[],id,sum,mrk;
}d[N<<];
il void pushup(int x) {d[x].sum=d[d[x].ch[]].sum+d[d[x].ch[]].sum+;}
il void pushdown(int x)
{
if(!d[x].mrk) return;
swap(d[x].ch[],d[x].ch[]);
d[x].mrk=;
d[d[x].ch[]].mrk^=;
d[d[x].ch[]].mrk^=;
}
il void rot(int x)
{
int y=d[x].fa;int ff=d[y].fa;
int px=idf(x);int py=idf(y);
con(d[x].ch[px^],y,px);
con(y,x,px^);
con(x,ff,py);
pushup(y),pushup(x);
}
void splay(int x,int to)
{
to=d[to].fa;
int y,px,py;
while(d[x].fa!=to)
{
y=d[x].fa;
px=idf(y),py=idf(x);
if(d[y].fa==to) rot(x);
else if(py==px){
rot(y);
rot(x);
}else{
rot(x);
rot(x);
}
}
}
int Find(int w)
{
int x=root;
while(x)
{
pushdown(x);
if(d[d[x].ch[]].sum>=w)
{
x=d[x].ch[];
continue;
}
w-=d[d[x].ch[]].sum;
if(w==) return x;
w--,x=d[x].ch[];
}
return ;
}
int build(int ff,int l,int r)
{
if(l>r) return ;
int x=++cnt;
int mid=(l+r)>>;
d[x].id=mid-,d[x].fa=ff,d[x].sum=;
d[x].ch[]=build(x,l,mid-);
d[x].ch[]=build(x,mid+,r);
pushup(x);
return x;
}
void Print(int x)
{
pushdown(x);
if(d[x].ch[]) Print(d[x].ch[]);
if(d[x].id!=&&d[x].id!=n+) printf("%d ",d[x].id);
if(d[x].ch[]) Print(d[x].ch[]);
}
int main()
{
//freopen("testdata.in","r",stdin);
scanf("%d%d",&n,&m);
int x,y;
root=build(,,n+);
for(int i=;i<=m;i++){
scanf("%d%d",&x,&y);
if(x==y) continue;
int xx=Find(x);
splay(xx,root);
int yy=Find(y+);
splay(yy,d[root].ch[]);
d[d[d[root].ch[]].ch[]].mrk^=;
}
Print(root);
return ;
}

splay 文艺平衡树 (数据结构)的更多相关文章

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

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

  2. 【Splay】bzoj3223-Tyvj1729文艺平衡树

    一.题目 Description 您需要写一种数据结构(可参考题目标题),来维护一个有序数列,其中需要提供以下操作:翻转一个区间,例如原有序序列是5 4 3 2 1,翻转区间是[2,4]的话,结果是5 ...

  3. bzoj3223 文艺平衡树 (treap or splay分裂+合并)

    3223: Tyvj 1729 文艺平衡树 Time Limit: 10 Sec  Memory Limit: 128 MB Submit: 3313  Solved: 1883 [Submit][S ...

  4. Tyvj P1729 文艺平衡树 Splay

    题目: http://tyvj.cn/p/1729 P1729 文艺平衡树 时间: 1000ms / 空间: 131072KiB / Java类名: Main 背景 此为平衡树系列第二道:文艺平衡树 ...

  5. bzoj3223 Tyvj 1729 文艺平衡树(Splay Tree+区间翻转)

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

  6. BZOJ 3223: Tyvj 1729 文艺平衡树(splay)

    速度居然进前十了...第八... splay, 区间翻转,用一个类似线段树的lazy标记表示是否翻转 ------------------------------------------------- ...

  7. bzoj3223Tyvj 1729 文艺平衡树 splay

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

  8. bzoj 3223: Tyvj 1729 文艺平衡树 (splay)

    链接: https://www.lydsy.com/JudgeOnline/problem.php?id=3223 题面: 3223: Tyvj 1729 文艺平衡树 Time Limit: 10 S ...

  9. bzoj 3223 文艺平衡树 - Splay

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

随机推荐

  1. Project Euler 16 Power digit sum( 大数乘法 )

    题意: 215 = 32768,而32768的各位数字之和是 3 + 2 + 7 + 6 + 8 = 26. 21000的各位数字之和是多少? 思路:大数乘法,计算 210 × 100 可加速计算,每 ...

  2. [Papers] Semantic Segmentation Papers(1)

    目录 FCN Abstract Introduction Related Work FCN Adapting classifiers for dense prediction Shift-and-st ...

  3. rtsp://192.168.1.198:554/PSIA/streaming/channels/101

    rtsp://192.168.1.198:554/PSIA/streaming/channels/101 Playing rtsp://192.168.1.198:554/PSIA/streaming ...

  4. 【codeforces 803D】Magazine Ad

    [题目链接]:http://codeforces.com/contest/803/problem/D [题意] 给你一个字符串; 其中的空格和连字符表示可以折叠的部分 (就是说能在那个位置把字符串分成 ...

  5. 警告: The APR based Apache Tomcat Native library failed to load.

    警告: The APR based Apache Tomcat Native library failed to load. The error reported was [C:\apache-tom ...

  6. rabbitMQ学习笔记(五) 消息路由

    生产者会生产出很多消息 , 但是不同的消费者可能会有不同的需求,只需要接收指定的消息,其他的消息需要被过滤掉. 这时候就可以对消息进行过滤了. 在消费者端设置好需要接收的消息类型. 如果不使用默认的E ...

  7. Python爬虫之正則表達式

    1.经常使用符号 .  :匹配随意字符,换行符 \n 除外 *  :匹配前一个字符0次或无限次 ? :匹配前一个字符0次或1次 .*  :贪心算法.尽可能的匹配多的字符 .*?  :非贪心算法 () ...

  8. 2016.03.10,英语,《Vocabulary Builder》Unit 05

    mal: means bad. malpractice [ˌmæl'præktɪs] n. 失职, 行为不当; malady ['mælədi] n. 病, 疾病, 弊病; malodorous [ˌ ...

  9. 英语发音规则---D字母

    英语发音规则---D字母 一.总结 一句话总结: 1.D发[d]音? doctor ['dɒktə] n. 医生:博士 bread [bred] n. 面包:生计 hand [hænd] n. 手,手 ...

  10. 随机森林算法demo python spark

    关键参数 最重要的,常常需要调试以提高算法效果的有两个参数:numTrees,maxDepth. numTrees(决策树的个数):增加决策树的个数会降低预测结果的方差,这样在测试时会有更高的accu ...