https://www.luogu.org/problem/P3391

使用无旋Treap维护序列,注意的是按顺序插入的序列,所以Insert实际上简化成直接root和Merge合并,但是假如要在序列中插入某个数,则要SplitRank到正确的位置。

注意SplitRank的写法以及Pushdown的东西。

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
#define ls(p) ch[p][0]
#define rs(p) ch[p][1] const int MAXN = 200000 + 5;
int val[MAXN], ch[MAXN][2], rnd[MAXN], siz[MAXN], tot, root;
bool rev[MAXN]; void Init() {
root = 0, tot = 0;
} inline void PushUp(int p) {
siz[p] = siz[ls(p)] + siz[rs(p)] + 1;
} inline void PushDown(int p) {
if(rev[p]) {
swap(ls(p), rs(p));
if(ls(p))
rev[ls(p)] ^= 1;
if(rs(p))
rev[rs(p)] ^= 1;
rev[p] = 0;
}
} void SplitRank(int p, int rk, int &x, int &y) {
if(!p) {
x = y = 0;
return;
}
PushDown(p);
if(rk <= siz[ls(p)]) {
y = p;
SplitRank(ls(p), rk, x, ls(p));
PushUp(y);
} else {
x = p;
SplitRank(rs(p), rk - siz[ls(p)] - 1, rs(p), y);
PushUp(x);
}
} int Merge(int x, int y) {
if(!x || !y)
return x | y;
if(rnd[x] < rnd[y]) {
PushDown(x);
rs(x) = Merge(rs(x), y);
PushUp(x);
return x;
} else {
PushDown(y);
ls(y) = Merge(x, ls(y));
PushUp(y);
return y;
}
} int NewNode(int v) {
int p = ++tot;
ch[p][0] = ch[p][1] = 0;
val[p] = v, rnd[p] = rand();
siz[p] = 1;
return p;
} void Insert(int &root, int v) {
root = Merge(root, NewNode(v));
} void Reverse(int &root, int l, int r) {
int x = 0, y = 0, z = 0;
SplitRank(root, l - 1, x, y);
SplitRank(y, r + 1 - l, y, z);
rev[y] ^= 1;
root = Merge(Merge(x, y), z);
} vector<int> ans; void Show(int p) {
PushDown(p);
if(ls(p))
Show(ls(p));
ans.push_back(val[p]);
if(rs(p))
Show(rs(p));
} int d[MAXN], f[MAXN], a[MAXN]; int u2topo[MAXN], cnttopo, topo2u[MAXN]; struct Query {
int u, k, ans, id;
bool operator<(const Query& q)const {
return u2topo[u] < u2topo[q.u];
}
} que[MAXN]; struct cmp {
bool operator()(const Query& q1, const Query& q2)const {
return q1.id < q2.id;
}
}; int que2[MAXN];
int front, back; int main() {
#ifdef Yinku
freopen("Yinku.in", "r", stdin);
#endif // Yinku
int n, q;
while(~scanf("%d%d", &n, &q)) {
Init();
for(int i = 1; i <= n; ++i) {
Insert(root, i);
}
for(int i = 1; i <= q; ++i) {
int l, r;
scanf("%d%d", &l, &r);
Reverse(root, l, r);
}
ans.clear();
Show(root);
for(int i = 0; i < ans.size(); ++i)
printf("%d%c", ans[i], " \n"[i == ans.size() - 1]);
}
return 0;
}

洛谷 - P3391 【模板】文艺平衡树(Splay) - 无旋Treap的更多相关文章

  1. [Bzoj3223][Tyvj1729] 文艺平衡树(splay/无旋Treap)

    题目链接:https://www.lydsy.com/JudgeOnline/problem.php?id=3223 平衡树处理区间问题的入门题目,普通平衡树那道题在维护平衡树上是以每个数的值作为维护 ...

  2. BZOJ - 3223 Tyvj 1729 文艺平衡树 (splay/无旋treap)

    题目链接 splay: #include<bits/stdc++.h> using namespace std; typedef long long ll; ,inf=0x3f3f3f3f ...

  3. 洛谷.3391.[模板]文艺平衡树(Splay)

    题目链接 //注意建树 #include<cstdio> #include<algorithm> const int N=1e5+5; //using std::swap; i ...

  4. 【洛谷P3391】文艺平衡树——Splay学习笔记(二)

    题目链接 Splay基础操作 \(Splay\)上的区间翻转 首先,这里的\(Splay\)维护的是一个序列的顺序,每个结点即为序列中的一个数,序列的顺序即为\(Splay\)的中序遍历 那么如何实现 ...

  5. [Bzoj3224][Tyvj1728] 普通平衡树(splay/无旋Treap)

    题目链接:https://www.lydsy.com/JudgeOnline/problem.php?id=3224 平衡树入门题,学习学习. splay(学习yyb巨佬) #include<b ...

  6. 洛谷.3369.[模板]普通平衡树(Splay)

    题目链接 第一次写(2017.11.7): #include<cstdio> #include<cctype> using namespace std; const int N ...

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

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

  8. 【序列操作V】平衡树(无旋treap)

    题目描述 维护一个队列,初始为空.依次加入 n(1≤n≤105)个数 ai(-109≤ai≤109),第 i(1≤i≤n)个数加入到当前序列第 bi(0≤bi≤当前序列长度)个数后面.输出最终队列. ...

  9. 模板 - 数据结构 - 可持久化无旋Treap/PersistentFHQTreap

    有可能当树中有键值相同的节点时,貌似是要对Split和Merge均进行复制的,本人实测:只在Split的时候复制得到了一个WA,但只在Merge的时候复制还是AC,可能是恰好又躲过去了.有人说假如确保 ...

  10. [模板] 平衡树: Splay, 非旋Treap, 替罪羊树

    简介 二叉搜索树, 可以维护一个集合/序列, 同时维护节点的 \(size\), 因此可以支持 insert(v), delete(v), kth(p,k), rank(v)等操作. 另外, prev ...

随机推荐

  1. 数据结构--排序--快排and冒泡(python)

    听说大厂面试,限时两分钟写出来快排... 闲着没事,写了一下... def Partition(L,low,high): pivotkey = L[low] while low<high: wh ...

  2. 牛客网 珂学送分( 期望DP )

    题意 : 题目链接 分析 : 听队友说一般概率从前往后推.期望从后往前推......... #include<bits/stdc++.h> using namespace std; ; d ...

  3. springboot集成mongoDB需要认证

    报错: Mon Nov 25 01:09:48 CST 2019 There was an unexpected error (type=Internal Server Error, status=5 ...

  4. RabbitMQ 工作图解

    (转网上的图) (原文地址 ,http://www.cnblogs.com/knowledgesea/p/5296008.html)

  5. 小程序封装wx.request,以及调用

    1.新建一个api目录,与pages同级 2.在api目录下新建一个api.js文件 3.编写代码 const host = 'http://test.test.cn' const wxRequest ...

  6. es关闭不使用的index、真正删除文档

    因为只要索引处于open状态,就会占用内存+磁盘: 如果将索引close,只会占用磁盘 Curl -XPOST ‘hadoop01:9200/index/_close’ ------ 在es中删除文档 ...

  7. POJ 1434 Fill the Cisterns! (模拟 or 二分)

    Fill the Cisterns! 题目链接: http://acm.hust.edu.cn/vjudge/contest/129783#problem/F Description During t ...

  8. window下启动redis服务

    ---恢复内容开始--- 在windows环境下启动redis服务,前提是你安装好了,启动如下: 一,进入redis的安装目录下,在地址栏输入“cmd”,回车 二,然后会进入cmd界面,直接运行命令r ...

  9. influxDB 1.3 中文文档

    influxDB是一个旨在处理高并发写入和查询负载的时序数据库,它是TICK框架的第二部分,influxdb用于任何包含大量时序数据应用的后台存储,包括Devops监控.应用指标数据.物联网传感器数据 ...

  10. characteristics of competent communicators

    https://www.universalclass.com/articles/business/communication-studies/be-a-competent-communicator.h ...