P3391 文艺平衡树(Splay)
题目背景
这是一道经典的Splay模板题——文艺平衡树。
题目描述
您需要写一种数据结构(可参考题目标题),来维护一个有序数列,其中需要提供以下操作:翻转一个区间,例如原有序序列是5 4 3 2 1,翻转区间是[2,4]的话,结果是5 2 3 4 1
输入格式:
第一行为n,m n表示初始序列有n个数,这个序列依次是 (1,2, \cdots n-1,n)(1,2,⋯n−1,n) m表示翻转操作次数
接下来m行每行两个数 [l,r][l,r] 数据保证 1 \leq l \leq r \leq n 1≤l≤r≤n
输出格式:
输出一行n个数字,表示原始序列经过m次变换后的结果
输入样例#1:
5 3
1 3
1 3
1 4
输出样例#1:
4 3 2 1 5
说明
n,m≤100000
我就想水一篇,你要咋地QAQ?
```c++
include<bits/stdc++.h>
using namespace std;
const int maxn = 1e5 + 5, L = 0, R = 1;
struct lpl{
int data, size, tag, fa, son[2];
}node[maxn];
int n, m, cnt, root;
namespace Splay{
inline void pushdown(int t)
{
if(!node[t].tag) return; node[t].tag = 0;
swap(node[t].son[L], node[t].son[R]);
node[node[t].son[L]].tag ^= 1; node[node[t].son[R]].tag ^= 1;
}
inline void update(int t){node[t].size = node[node[t].son[L]].size + node[node[t].son[R]].size + 1;}
inline void rotate(int t)
{
int fa = node[t].fa, grdfa = node[fa].fa, which = (node[fa].son[R] == t);
node[grdfa].son[node[grdfa].son[R] == fa] = t; node[t].fa = grdfa;
node[node[t].son[which ^ 1]].fa = fa;
node[fa].son[which] = node[t].son[which ^ 1];
node[t].son[which ^ 1] = fa; node[fa].fa = t;
update(fa); update(t);
}
inline void splay(int t, int k)
{
while(node[t].fa != k){
int fa = node[t].fa, grdfa = node[fa].fa;
if(grdfa != k){
if((node[fa].son[R] == t) ^ (node[grdfa].son[R] == fa)) rotate(t);
else rotate(fa);
}
rotate(t);
}
if(!k) root = t;
}
inline void insert(int t)
{
int now = root, fa = 0;
while(now){
fa = now;
if(node[now].data < t) now = node[now].son[R];
else now = node[now].son[L];
}
node[fa].son[t > node[fa].data] = ++cnt; node[cnt].data = t;
node[cnt].fa = fa; node[cnt].size = 1;
splay(cnt, 0);
}
int Find(int t, int k)
{
pushdown(t);
if(k == node[node[t].son[L]].size + 1) return t;
if(k <= node[node[t].son[L]].size) return Find(node[t].son[L], k);
return Find(node[t].son[R], k - (node[node[t].son[L]].size + 1));
}
void print(int t)
{
pushdown(t);
if(node[t].son[L]) print(node[t].son[L]);
if(2 <= node[t].data && node[t].data <= n + 1) printf("%d ", node[t].data - 1);
if(node[t].son[R]) print(node[t].son[R]);
}
}
inline void workk(int l, int r)
{
int LL, RR;
LL = Splay::Find(root, l - 1), RR = Splay::Find(root, r + 1);
Splay::splay(LL, 0);
Splay::splay(RR, LL);
node[node[node[root].son[R]].son[L]].tag ^= 1;
}
int main()
{
scanf("%d%d", &n, &m);
for(int i = 1; i <= n + 2; ++i) Splay::insert(i);
while(m--){
int l, r;
scanf("%d%d", &l, &r); l++; r++;
workk(l, r);
}
Splay::print(root);
return 0;
}
P3391 文艺平衡树(Splay)的更多相关文章
- [洛谷P3391] 文艺平衡树 (Splay模板)
初识splay 学splay有一段时间了,一直没写...... 本题是splay模板题,维护一个1~n的序列,支持区间翻转(比如1 2 3 4 5 6变成1 2 3 6 5 4),最后输出结果序列. ...
- 【阶梯报告】洛谷P3391【模板】文艺平衡树 splay
[阶梯报告]洛谷P3391[模板]文艺平衡树 splay 题目链接在这里[链接](https://www.luogu.org/problemnew/show/P3391)最近在学习splay,终于做对 ...
- [luogu P3391] 文艺平衡树
[luogu P3391] 文艺平衡树 题目背景 这是一道经典的Splay模板题——文艺平衡树. 题目描述 您需要写一种数据结构(可参考题目标题),来维护一个有序数列,其中需要提供以下操作:翻转一个区 ...
- luoguP3391[模板]文艺平衡树(Splay) 题解
链接一下题目:luoguP3391[模板]文艺平衡树(Splay) 平衡树解析 这里的Splay维护的显然不再是权值排序 现在按照的是序列中的编号排序(不过在这道题目里面就是权值诶...) 那么,继续 ...
- BZOJ3223: Tyvj 1729 文艺平衡树 [splay]
3223: Tyvj 1729 文艺平衡树 Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 3595 Solved: 2029[Submit][Sta ...
- Tyvj P1729 文艺平衡树 Splay
题目: http://tyvj.cn/p/1729 P1729 文艺平衡树 时间: 1000ms / 空间: 131072KiB / Java类名: Main 背景 此为平衡树系列第二道:文艺平衡树 ...
- BZOJ 3223: Tyvj 1729 文艺平衡树(splay)
速度居然进前十了...第八... splay, 区间翻转,用一个类似线段树的lazy标记表示是否翻转 ------------------------------------------------- ...
- BZOJ3223/洛谷P3391 - 文艺平衡树
BZOJ链接 洛谷链接 题意 模板题啦~2 代码 //文艺平衡树 #include <cstdio> #include <algorithm> using namespace ...
- bzoj3223Tyvj 1729 文艺平衡树 splay
3223: Tyvj 1729 文艺平衡树 Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 5644 Solved: 3362[Submit][Sta ...
随机推荐
- vue项目一个页面使用多个轮播图详解
1.html代码: <div v-for="(item,index) in arrDataList.Floor"> // 根据后台数据循环渲染多个轮播图组件 <d ...
- UIGestureRecognizer 手势
- (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view. self.v ...
- jquery+html实现前端的上传图片预览
就是这样的一个功能,点击加号,出现图片选择,然后选择好以后生成预览. input那么丑,UI看不惯,一定要改成加号 我就用了fa的图标,外部套一个bootstrap4中的class:borde ...
- shell命令结果重定向
- ThreadPoolExecuotor源码参考
Executor --> ExecutorService --> AbstractExecutorService --> ThreadPoolExecuotor Executor接口 ...
- Go 使用 append 向切片增加元素
1.// 创建一个整型切片 // 其长度和容量都是 5 个元素 slice := []int{10, 20, 30, 40, 50} // 创建一个新切片 // 其长度为 2 个元素,容量为 4 个元 ...
- canvas 绘制二次贝塞尔曲线
代码: <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8 ...
- Codeforces Round #584 - Dasha Code Championship - Elimination Round (rated, open for everyone, Div. 1 + Div. 2)
怎么老是垫底啊. 不高兴. 似乎 A 掉一道题总比别人慢一些. A. Paint the Numbers 贪心,从小到大枚举,如果没有被涂色,就新增一个颜色把自己和倍数都涂上. #include< ...
- CentOS7.5 安装部署Apache+Mysql+Php
系统:CentOS7.5 安装Apache 安装 yum -y install httpd 开启apache服务 systemctl start httpd.service 设置apache服务开机启 ...
- 如何在Ubuntu下搭建tftp服务器
远程桌面连接工具 今天开始调试arm的板子,要通过tftp下载到板子上,所以又要配置tftp服务器,真的烦死了… (本人酷爱装系统,所以经常都要搞配置) 因为之前已经在Ubuntu下搭建过很多次t ...