模板题。

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

  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. 自建docker仓库

    一.仓库安装 1.系统:CentOS7.9,采用yum安装方式 [root@master ~]# yum install docker-distribution -y ... ... [root@ma ...

  2. c语言中的gets和fgets的使用差别

    gets和fgets的差别 2022年6月30日 #include<stdio.h> #include<string.h> #define STLEN 8 int main(i ...

  3. JavaWeb--基本概念、Web服务器与Tomcat

    前言 Java Web 其实就是一个技术的总和,把Web看成一个容器而已主要使用JavaEE技术来实现.在加上各种中间件. 整个javaWeb阶段的内容通过实际的案例贯穿学习, 所涉及到的技术知识点会 ...

  4. 前端监控系列1| 字节的前端监控SDK是怎样设计的

    作者:彭莉,火山引擎 APM 研发工程师,2020年加入字节,负责前端监控 SDK 的开发维护.平台数据消费的探索和落地. 摘要 字节内部应用环境多样( Web 应用.小程序.Electron 应用. ...

  5. BootStrap详解

    1. bootstrap的安装和使用 官网: https://getbootstrap.com/ 中文网: https://www.bootcss.com/ 菜鸟驿站教程网: https://www. ...

  6. EMAS Serverless到底有多便利?

    EMAS Serverless 简介 EMAS Serverless 是阿里云提供的基于Serverless技术的一站式后端开发平台,为开发者提供高可用.弹性伸缩的云开发服务,包含云函数.云数据库.云 ...

  7. 操作系统学习笔记4 | CPU管理 && 多进程图像

    操作系统的核心功能就是管理计算机硬件,而CPU就是计算机中最核心的硬件.而通过学习笔记3的简史回顾,操作系统通过多进程图像实现对CPU的管理.所以多进程图像是操作系统的核心图像. 参考资料: 课程:哈 ...

  8. Flutter 开启 Windows、macOS 平台支持的命令

    Flutter 的多平台支持除了 Android 和 iOS 是默认开启的以外,比如 Windows.Linux 平台的支持需要手动开启. Flutter config 命令集中,有以下参数是对于平台 ...

  9. Python小游戏——外星人入侵(保姆级教程)第一章 06让飞船移动

    系列文章目录 第一章:武装飞船 06:让飞船移动 一.驾驶飞船 下面来让玩家能够左右移动飞船.我们将编写代码,在用户按左或右箭头键时做出响应.我们将首先专注于向右移动,再使用同样的原理来控制向左移动. ...

  10. mac M1通过homebrew安装python3报错Error: Command failed with exit 128: git

    fatal: not in a git directoryError: Command failed with exit 128: git 只需要运行 git config --global --ad ...