模板题。

注意标记即可,另外,涉及区间翻转操作,记得设立首尾哨兵。

  1 #include<bits/stdc++.h>
2 using namespace std;
3 const int Maxn=0x3f3f3f3f;
4 const int N=1e5+5;
5 int lc[N],rc[N],fa[N],sze[N],vi[N],pos[N],a[N];
6 int n,m,x,y,rt,T;
7
8 inline int get(){//快读
9 char ch;bool f=false;int res=0;
10 while(((ch=getchar())<'0'||ch>'9')&&ch!='-');
11 if(ch=='-') f=true;
12 else res=ch-'0';
13 while((ch=getchar())>='0'&&ch<='9')
14 res=(res<<3)+(res<<1)+ch-'0';
15 return f?~res+1:res;
16 }
17
18 inline bool Wrt(const int&x)
19 {
20 return rc[fa[x]]==x;
21 }
22
23 inline void down(const int &x){//下传标记
24 if(x&&pos[x]){
25 pos[lc[x]]^=1;
26 pos[rc[x]]^=1;
27 swap(lc[x],rc[x]);
28 pos[x]=0;
29 }
30 }
31
32 inline void push(int x){
33 sze[x]=sze[lc[x]]+sze[rc[x]]+1;
34 }
35
36 inline int build(int lst,int l,int r){//lst该节点的祖先编号
37 if(l>r) return 0;
38 int mid=(l+r)>>1,x=++T;
39 fa[x]=lst;pos[x]=0;vi[x]=a[mid];
40 lc[x]=build(x,l,mid-1);
41 rc[x]=build(x,mid+1,r);
42 push(x);
43 return x;
44 }
45
46 inline void rot(int x){//单旋
47 int y=fa[x],z=fa[y];
48 down(y);down(x);//下传标记
49 int b=(lc[y]==x)?rc[x]:lc[x];
50 fa[x]=z;fa[y]=x;
51 if(b) fa[b]=y;
52 if(z) (y==lc[z]?lc[z]:rc[z])=x;
53 if(x==lc[y]) rc[x]=y,lc[y]=b;
54 else lc[x]=y,rc[y]=b;
55 push(y);push(x);//更新
56 }
57
58 inline void splay(int x,int tar){
59 while(fa[x]!=tar){
60 if(fa[fa[x]]!=tar)
61 Wrt(x)==Wrt(fa[x])?rot(fa[x]):rot(x);
62 rot(x);
63 }
64 if(!tar) rt=x;
65 }
66
67 inline int Find(int k){//查找第k个数
68 int x=rt,y=k;
69 while(x){
70 down(x);
71 if(y<=sze[lc[x]]) x=lc[x];
72 else{
73 y-=sze[lc[x]]+1;
74 if(!y) return x;
75 x=rc[x];
76 }
77 }
78 }
79
80 /*inline void put(int x){//快输
81 if(x<0){
82 x=~x+1;
83 putchar('-');
84 }
85 if(x>9) put(x/10);
86 putchar(x%10+48);
87 }*/
88
89 inline void Print(int x){
90 down(x);
91 if(lc[x]) Print(lc[x]);
92 if(vi[x]!=-Maxn&&vi[x]!=Maxn)
93 // put(vi[x]),putchar(' ');//快输
94 cout<<vi[x]<<" ";
95 if(rc[x]) Print(rc[x]);
96 }
97
98 int main(){
99 n=get();m=get();
100 a[1]=-Maxn;a[n+2]=Maxn;
101 for(int i=1;i<=n;i++) a[i+1]=i;
102 rt=build(0,1,n+2);
103 while(m--){
104 int tx=Find(get());
105 int ty=Find(get()+2);
106 splay(tx,0);
107 splay(ty,tx);
108 pos[lc[rc[rt]]]^=1;
109 }
110 return Print(rt),0;
111 }

洛谷P3391 文艺平衡树 (Splay模板)的更多相关文章

  1. [洛谷P3391] 文艺平衡树 (Splay模板)

    初识splay 学splay有一段时间了,一直没写...... 本题是splay模板题,维护一个1~n的序列,支持区间翻转(比如1 2 3 4 5 6变成1 2 3 6 5 4),最后输出结果序列. ...

  2. 洛谷P3391文艺平衡树(Splay)

    题目传送门 转载自https://www.cnblogs.com/yousiki/p/6147455.html,转载请注明出处 经典引文 空间效率:O(n) 时间效率:O(log n)插入.查找.删除 ...

  3. BZOJ3223/洛谷P3391 - 文艺平衡树

    BZOJ链接 洛谷链接 题意 模板题啦~2 代码 //文艺平衡树 #include <cstdio> #include <algorithm> using namespace ...

  4. BZOJ3224/洛谷P3391 - 普通平衡树(Splay)

    BZOJ链接 洛谷链接 题意简述 模板题啦~ 代码 //普通平衡树(Splay) #include <cstdio> int const N=1e5+10; int rt,ndCnt; i ...

  5. 洛谷 P3391 文艺平衡树

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

  6. 洛谷.3391.文艺平衡树(fhq Traep)

    题目链接 //注意反转时先分裂r,因为l,r是针对整棵树的排名 #include<cstdio> #include<cctype> #include<algorithm& ...

  7. 洛谷 P3391 【模板】文艺平衡树(Splay)

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

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

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

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

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

随机推荐

  1. 使用Pure Pipes来替换HTML里面的纯函数

    <ul *ngFor="let item of list"> <li>{{show(item.label)}}</li> </ul> ...

  2. 整数分解和for循环

    整数的分解: 一个整数是由多位数字组成的,那么如何能分解出整数的各个位上的数字呢 对一个整数做%10的操作,就可以得到它的个位数 对一个整数做/10的操作,就去掉了他的个位数 然后再对2的结果做%10 ...

  3. java关键字的概念与特征和标识符的概念和规则

    什么是关键字 比如说邮箱地址 abc@qq.com  123abc@qq.com 这样的只要没有人占用都是和发布的 那么这样呢 hahah@enen@itcast.cn呢 @是电子邮箱当中有特殊含义的 ...

  4. BACnet MS/TP转MQTT网关金鸽BL103

    BACnet MS/TP转MQTT网关金鸽BL103BL103是一款BACnet路由器,实现 BACnet MS/TP 总线和以太网 BACnetIP 之间通信路由功能,同时也是一款Modbus RT ...

  5. SQL 字符串去除空格函数汇总

    SQL 中使用ltrim()去除左边空格 ,rtrim()去除右边空格 ,没有同时去除左右空格的函数,要去除所有空格可以用replace(字符串,' ',''),将字符串里的空格替换为空 . 例:去除 ...

  6. Python 懂车帝综合口碑数据

    本文所有教程及源码.软件仅为技术研究.不涉及计算机信息系统功能的删除.修改.增加.干扰,更不会影响计算机信息系统的正常运行.不得将代码用于非法用途,如侵立删! Python 懂车帝综合口碑数据 需求 ...

  7. CMAKE编译时如何自动下载第三方库并解压、安装到指定目录

    GreatSQL社区原创内容未经授权不得随意使用,转载请联系小编并注明来源. 导语 在日常开发过程中难免会使用到第三方库或者需要将部分库分离另外存储,如果将库与代码放在一起难免会造成工程庞大,此时就可 ...

  8. mysql 8.0.28 查询语句执行顺序实测结果

    TL;NRs 根据实测结果,MySQL8.0.28 中 SQL 语句的执行顺序为: (8) SELECT (5) DISTINCT <select_list> (1) FROM <l ...

  9. Git 02 安装

    参考源 https://www.bilibili.com/video/BV1FE411P7B3?spm_id_from=333.999.0.0 版本 本文章基于 Git 2.35.1.2 这里以安装 ...

  10. numa 自动balance 的bug分析

    关于migrate_swap() 和 active_balance()之间的hardlock 背景:这个是在3.10.0-957.el7.x86_64 遇到的一例crash 下面列一下我们是怎么排查并 ...