模板题 link

Splay 区间翻转,存个代码

旋转时,要注意goal是引用 , 并记得修改 , 有标记的一定记得标记下放 , 还有清空

#include<iostream>
#include<cstdio>
using namespace std;
const int N = 101000;
inline int read()
{
register int x = 0 , f = 0; register char c = getchar();
while(c < '0' || c > '9') f |= c == '-' , c = getchar();
while(c >= '0' && c <= '9') x = (x << 3) + (x << 1) + c - '0' , c = getchar();
return f ? -x : x;
}
#define RQ puts("RQ");
int n , m , sz , root;
int tr[N][2] , val[N] , rev[N] , fa[N] , siz[N];
inline int Newnode() { return ++sz; }
inline void update(int x) { siz[x] = siz[tr[x][0]] + siz[tr[x][1]] + 1; }
inline int witch(int x) { return x == tr[fa[x]][1]; }
inline int rk(int x) { return 1 + siz[tr[x][0]]; }
void rot(int x , int &goal) // 忘记了修改 goal
{
int y = fa[x] , z = fa[y] , k = witch(x) , w = tr[x][!k];
if(y == goal) goal = x; else tr[z][witch(y)] = x;
fa[x] = z; tr[y][k] = w; if(w) fa[w] = y;
tr[x][!k] = y; fa[y] = x;
update(y); update(x);
} void down(int x)
{
if(!x || !rev[x]) return ; // 没用判断标记
swap(tr[x][0] , tr[x][1]);
if(tr[x][0]) rev[tr[x][0]] ^= 1;
if(tr[x][1]) rev[tr[x][1]] ^= 1;
rev[x] = 0; // 还没有清空
return ;
} void Splay(int x , int &goal) // 引用
{
while(x != goal)
{
if(fa[x] != goal) rot(witch(x) == witch(fa[x]) ? fa[x] : x , goal);
rot(x , goal);
}
return ;
} void build(int &k , int l , int r , int f)
{
if(l > r) return ; int mid = (l + r) >> 1;
if(!k) k = Newnode(); siz[k] = 1; fa[k] = f; val[k] = mid;
build(tr[k][0] , l , mid - 1 , k);
build(tr[k][1] , mid + 1 , r , k);
update(k); return ;
} int kth(int k) // 求的是第k大的是谁 , 而不是k的排名
{
int p = root;
while(down(p) , rk(p) != k)
{
if(rk(p) < k) k -= rk(p) , p = tr[p][1];
else p = tr[p][0];
}
Splay(p , root);
return p;
} void reverse(int l , int r)
{
int x = kth(l) , y = kth(r);
Splay(x , root); Splay(y , tr[x][1]);
rev[tr[y][0]] ^= 1; return ;
} void dfs(int x)
{
if(!x) return ; down(x);
dfs(tr[x][0]);
if(val[x] >= 1 && val[x] <= n) printf("%d " , val[x]);
dfs(tr[x][1]);
return ;
} int main()
{
n = read(); m = read();
build(root , 0 , n+1 , 0);
for(int i = 1 ; i <= m ; ++i)
{
int l = read() , r = read();
reverse(l , r + 2);
}
dfs(root);
return 0;
}

P3391 【模板】文艺平衡树的更多相关文章

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

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

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

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

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

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

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

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

  5. P3391 【模板】文艺平衡树FHQ treap

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

  6. P3391 【模板】文艺平衡树(Splay)新板子

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

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

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

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

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

  9. 洛谷 P3391 【模板】文艺平衡树

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

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

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

随机推荐

  1. request.getParameterMap获取不到数据问题

    最近在做javaweb项目的过程中发现使用request.getParameterMap( )方法获取jsp页面中的表单数据的时候发现获取不到,检查了好长时间最后发现问题是在jsp页面中. reque ...

  2. CSP2019第二轮-划水游记

    又是 颓废的 一年 眨眼间已经初三了 到了NOIPCSP的时节 Day0 学校(没错,全校事件)抽风地把二晚停了,于是也就有了机会早早回家van♂耍 和母上大人简单地收拾收拾东西,回了姥姥家 以&qu ...

  3. LeetCode 面试题 02.06. 回文链表

    题目链接:https://leetcode-cn.com/problems/palindrome-linked-list-lcci/ 编写一个函数,检查输入的链表是否是回文的. 示例 1: 输入: 1 ...

  4. YARN安装和使用

    简介 Yet Another Resource Negotiator ,负责整个集群资源的调度,和管理,支持多框架资源统一调度(HIVE spark flink) 开启yarn 安装hadoop,可以 ...

  5. css3基础-选择器+边框与圆角+背景与渐变

    Css3选择器相关: section > div直接子元素选择器 div + article相邻兄弟选择器(在元素之后出现) div ~ article通用兄弟选择器(在元素之后出现) 属性选择 ...

  6. layui 弹出层layer中from初始化 ,并在btn中返回from.data

    1.弹出对话框 layer.open() 来初始化弹层 // 监听添加操作 $(".data-add-btn").on("click", function () ...

  7. 使用shell程序备份crontab中的.sh脚本文件

    需求 线上环境有一些定时脚本(用crontab -l可查看当前用户的),有时我们可能会改这些定时任务的脚本内容.为避免改错无后悔药,需用shell实现一个程序,定时备份crontab中的.sh脚本文件 ...

  8. 使用nginx代理gogs遇到推送代码错误的问题(RPC failed; HTTP 413 curl 22 The requested URL returned error: 413)

    前提 代码管理我是用Gogs.Git,前些阵子使用Nginx将git.balabiu.com反向代理到了Gogs的默认端口,其他二级域名准备做其他使用, 导致上报代码出现了错误. 问题 推送代码报错误 ...

  9. linux基础之磁盘管理与文件系统

    上面一篇文章(https://www.cnblogs.com/ckh2014/p/7224851.html)介绍了硬盘的基本结构,那么一块磁盘如何应用取存储数据呢? 它的步骤是这样的: 识别硬盘 -- ...

  10. leetcode四道组合总和问题总结(39+40+216+377)

    39题目: 链接:https://leetcode-cn.com/problems/combination-sum/ 给定一个无重复元素的数组 candidates 和一个目标数 target ,找出 ...