模板题 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. 教你用python爬虫监控教务系统,查成绩快人一步!

    教你用python爬虫监控教务系统,查成绩快人一步!这几天考了大大小小几门课,教务系统又没有成绩通知功能,为了急切想知道自己挂了多少门,于是我写下这个脚本. 设计思路:设计思路很简单,首先对已有的成绩 ...

  2. Oracle修改用户Profile SESSIONS_PER_USER 限制

    一.Profile目的: Oracle系统中的profile可以用来对用户所能使用的数据库资源进行限制,使用Create Profile命令创建一个Profile,用它来实现对数据库资源的限制使用,如 ...

  3. python中class的定义及使用

    #类(Class): 用来描述具有相同的属性和方法的对象的集合.它定义了该集合中每个对象所共有的属性和方法. #对象:它是类的实例化. #方法:类中定义的函数. #类(Class) 由3个部分构成: ...

  4. Hadoop学习之路(8)Yarn资源调度系统详解

    文章目录 1.Yarn介绍 2.Yarn架构 2.1 .ResourceManager 2.2 .ApplicationMaster 2.3 .NodeManager 2.4 .Container 2 ...

  5. nginx 反向代理及 https 证书配置

    nginx 反向代理及 https 证书配置 author: yunqimg(ccxtcxx0) 1. 编译安装nginx 从官网下载 nginx源码, 并编译安装. ./configure --pr ...

  6. 浅谈python的第三方库——pandas(一)

    pandas作为python进行数据分析的常用第三方库,它是基于numpy创建的,使得运用numpy的程序也能更好地使用pandas. 1 pandas数据结构 1.1 Series 注:由于pand ...

  7. Zabbbix之十二------Zabbix实现微信报警通知及创建聚合图形

    实战一:实现zabbix监控微信报警 1.在企业微信上注册账号 1.注册企业微信,管理员需要写上自己的真实姓名,扫描以下的二维码,与微信关联真实姓名. 2.登陆企业微信,然后创建一个微信故障通知应用 ...

  8. ag.百家下三路怎么看,如何玩好百家了

    \/Q同号3908914.百家作为风靡全球的一款游戏,这么多年长盛不衰,是世界各地玩家的心头所好,但是你真的知道怎么玩好百家吗?第一点呢就是心态问题,我个人认为心态好一切都好,光是心态就占了百分之五十 ...

  9. CentOS 7 版本配置salt-master salt-minion

    下载saltshaker_api.git [root@linux-node1 salt]# cd $HOME [root@linux-node1 salt]# git clone https://gi ...

  10. Verilog-case、casez和casex的区别

    参考博客:https://www.cnblogs.com/guolongnv/articles/6906929.html 1.基本概念 1)?表示z,而不是“dont care” 2)区分: case ...