洛谷P3391文艺平衡树(Splay)
转载自https://www.cnblogs.com/yousiki/p/6147455.html,转载请注明出处
经典引文
Tree Rotation

Splaying
- 节点x是父节点p的左孩子还是右孩子
- 节点p是不是根节点,如果不是
- 节点p是父节点g的左孩子还是右孩子
Zig Step

Zig-Zig Step

Zig-Zag Step

应用
最后附上自己写的洛谷的模板题的代码:
- #include<bits/stdc++.h>
- using namespace std;
- const int N=1e5+;
- int n,m,root,tot;
- struct Node{
- int ch[],size;
- int fa,mark,val;
- void add(int x,int y){
- ch[]=ch[]=mark=;
- val=x;fa=y;size=;}
- }t[N];
- inline int read()
- {
- char ch=getchar();int num=;bool flag=false;
- while(ch<''||ch>''){if(ch=='-')flag=true;ch=getchar();}
- while(ch>=''&&ch<=''){num=num*+ch-'';ch=getchar();}
- return flag?-num:num;
- }
- inline void pushup(int x)
- {
- int l=t[x].ch[],r=t[x].ch[];
- t[x].size=t[l].size+t[r].size+;
- }
- inline void pushdown(int x)
- {
- if(t[x].mark){
- t[t[x].ch[]].mark^=;
- t[t[x].ch[]].mark^=;
- swap(t[x].ch[],t[x].ch[]);
- t[x].mark=;}
- }
- inline void rotate(int x)
- {
- int y=t[x].fa;
- int z=t[y].fa;
- int k=(t[y].ch[]==x);
- t[z].ch[t[z].ch[]==y]=x;
- t[x].fa=z;
- t[t[x].ch[k^]].fa=y;
- t[y].ch[k]=t[x].ch[k^];
- t[x].ch[k^]=y;
- t[y].fa=x;
- pushup(y);pushup(x);
- }
- inline void splay(int x,int tag)
- {
- while(t[x].fa!=tag){
- int y=t[x].fa;
- int z=t[y].fa;
- if(z!=tag)
- (t[y].ch[]==x)^(t[z].ch[]==y)?
- rotate(x):rotate(y);
- rotate(x);
- }
- if(tag==)root=x;
- }
- inline void insert(int x)
- {
- int now=root,fa=;
- while(now)
- fa=now,now=t[now].ch[x>t[now].val];
- now=++tot;
- if(fa)t[fa].ch[x>t[fa].val]=now;
- t[now].add(x,fa);
- splay(now,);
- }
- inline int find(int x)
- {
- int now=root;
- while(){
- pushdown(now);
- if(t[t[now].ch[]].size>=x)now=t[now].ch[];
- else if(t[t[now].ch[]].size+==x)return now;
- else x-=(t[t[now].ch[]].size+),now=t[now].ch[];
- }
- }
- inline void work(int l,int r)
- {
- l=find(l);r=find(r+);
- splay(l,);splay(r,l);
- t[t[t[root].ch[]].ch[]].mark^=;
- }
- inline void print(int x)
- {
- pushdown(x);
- if(t[x].ch[])print(t[x].ch[]);
- if(t[x].val>&&t[x].val<n+)
- printf("%d ",t[x].val-);
- if(t[x].ch[])print(t[x].ch[]);
- }
- int main()
- {
- n=read();m=read();
- for(int i=;i<=n+;i++)
- insert(i);
- for(int i=;i<=m;i++){
- int l=read();int r=read();
- work(l,r);}
- print(root);
- return ;
- }
洛谷P3391文艺平衡树(Splay)的更多相关文章
- [洛谷P3391] 文艺平衡树 (Splay模板)
初识splay 学splay有一段时间了,一直没写...... 本题是splay模板题,维护一个1~n的序列,支持区间翻转(比如1 2 3 4 5 6变成1 2 3 6 5 4),最后输出结果序列. ...
- BZOJ3223/洛谷P3391 - 文艺平衡树
BZOJ链接 洛谷链接 题意 模板题啦~2 代码 //文艺平衡树 #include <cstdio> #include <algorithm> using namespace ...
- BZOJ3224/洛谷P3391 - 普通平衡树(Splay)
BZOJ链接 洛谷链接 题意简述 模板题啦~ 代码 //普通平衡树(Splay) #include <cstdio> int const N=1e5+10; int rt,ndCnt; i ...
- 洛谷 P3391 文艺平衡树
题目描述 您需要写一种数据结构(可参考题目标题),来维护一个有序数列,其中需要提供以下操作:翻转一个区间,例如原有序序列是5 4 3 2 1,翻转区间是[2,4]的话,结果是5 2 3 4 1 --b ...
- 洛谷P3391 文艺平衡树 (Splay模板)
模板题. 注意标记即可,另外,涉及区间翻转操作,记得设立首尾哨兵. 1 #include<bits/stdc++.h> 2 using namespace std; 3 const int ...
- 洛谷.3391.文艺平衡树(fhq Traep)
题目链接 //注意反转时先分裂r,因为l,r是针对整棵树的排名 #include<cstdio> #include<cctype> #include<algorithm& ...
- 洛谷 P3391 【模板】文艺平衡树(Splay)
题目背景 这是一道经典的Splay模板题——文艺平衡树. 题目描述 您需要写一种数据结构(可参考题目标题),来维护一个有序数列,其中需要提供以下操作:翻转一个区间,例如原有序序列是5 4 3 2 1, ...
- 【阶梯报告】洛谷P3391【模板】文艺平衡树 splay
[阶梯报告]洛谷P3391[模板]文艺平衡树 splay 题目链接在这里[链接](https://www.luogu.org/problemnew/show/P3391)最近在学习splay,终于做对 ...
- [luogu P3391] 文艺平衡树
[luogu P3391] 文艺平衡树 题目背景 这是一道经典的Splay模板题——文艺平衡树. 题目描述 您需要写一种数据结构(可参考题目标题),来维护一个有序数列,其中需要提供以下操作:翻转一个区 ...
随机推荐
- [LeetCode] 2. Add Two Numbers ☆☆
You are given two non-empty linked lists representing two non-negative integers. The digits are stor ...
- python实现堆栈、队列
一.利用python列表实现堆栈和队列 堆栈: 堆栈是一个后进先出的数据结构,其工作方式就像生活中常见到的直梯,先进去的人肯定是最后出. 我们可以设置一个类,用列表来存放栈中的元素的信息,利用列表的a ...
- gitlab通过api创建组、项目、成员
前戏 获取gitlab中admin用户的private_token Groups API 获取某个组的详细 curl --header "PRIVATE-TOKEN: *********&q ...
- CAS单点登录原理
转自 https://www.cnblogs.com/lihuidu/p/6495247.html 1.基于Cookie的单点登录的回顾 基于Cookie的单点登录核心原理: 将用户名密 ...
- 深入理解微服务架构spring的各个知识点(面试必问知识点)
什么是spring spring是一个开源框架,spring为简化企业级开发而生,使用spring可以使简单的java bean 实现以前只有EJG才能实现的功能. Spring是一个轻量级的控制反转 ...
- bzoj 1004 burnside 引理+DP
对于burnside引理需要枚举染色,这道题属于burnside的一种简单求解的方法,就是polya,我们可以使每一种置换中的循环节中的元素的颜色都相同,那么这样的话就可以直接DP了,我们可以将m个置 ...
- Python面向对象学习2(面向对象的语法和特性,待更新)
上一个内容我们介绍了面向对象和面向对象场景现在我们来学习下语法和特性 1,面向对象基本语法: # -*- coding:utf-8 -*- # Author: Colin Yao class Dog( ...
- ftrace 的使用【转】
转自:http://blog.csdn.net/wang6077160/article/details/7814279 ftrace 的使用 ftrace 在内核态工作,用户通过 debugfs 接口 ...
- sicily 1036. Crypto Columns
Constraints Time Limit: 1 secs, Memory Limit: 32 MB Description The columnar encryption scheme scram ...
- 设计模式之笔记--组合模式(Composite)
组合模式(Composite) 定义 组合模式(Composite),将对象组合成树形结构以表示“部分-整体”的层次结构.组合模式使得用户对单个对象和组合对象的使用具有一致性. 组合模式有 ...