bryce1010专题训练——Splay树
| Prob | Hint |
|---|---|
| BZOJ 3323 文艺平衡树 | 区间翻转 |
| BZOJ 1251 序列终结者 | 区间翻转,询问最值 |
| BZOJ 1895 supermemo | 区间加,翻转,剪切,询问最值。点插入,删除。 |
| BZOJ 1056 排名系统 | 专治操作完不伸展 |
| BZOJ 1552 robotic sort | 区间反转,清除标记,splay 的灵活运用 |
| BZOJ 3224 普通平衡树 | 像普通平衡树一样使用 Splay |
1、查找区间第K大+排序
http://acm.hdu.edu.cn/showproblem.php?pid=1890【HDU1890】
#include <stdio.h>
#include <string.h>
#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <string>
#include <math.h>
#include <stdlib.h>
#include <time.h>
using namespace std;
#define Key_value ch[ch[root][1]][0]
const int MAXN = 100010;
int pre[MAXN],ch[MAXN][2],root,tot1;
int size[MAXN];//子树规模
int rev[MAXN];//反转标记
int s[MAXN],tot2;//内存池和容量
//debug部分**********************************
void Treavel(int x)
{
if(x)
{
Treavel(ch[x][0]);
printf("结点:%2d: 左儿子 %2d 右儿子 %2d 父结点 %2d size = %2d rev = %2d\n",x,ch[x][0],ch[x][1],pre[x],size[x],rev[x]);
Treavel(ch[x][1]);
}
}
void debug()
{
printf("root:%d\n",root);
Treavel(root);
}
//以上是debug部分**************************************
void NewNode(int &r,int father,int k)
{
r = k;
pre[r] = father;
ch[r][0] = ch[r][1] = 0;
size[r] = 1;
rev[r] = 0;
}
//反转的更新
void Update_Rev(int r)
{
if(!r)return;
swap(ch[r][0],ch[r][1]);
rev[r] ^= 1;
}
inline void push_up(int r)
{
size[r] = size[ch[r][0]] + size[ch[r][1]] + 1;
}
inline void push_down(int r)
{
if(rev[r])
{
Update_Rev(ch[r][0]);
Update_Rev(ch[r][1]);
rev[r] = 0;
}
}
void Build(int &x,int l,int r,int father)
{
if(l > r)return;
int mid = (l+r)/2;
NewNode(x,father,mid);
Build(ch[x][0],l,mid-1,x);
Build(ch[x][1],mid+1,r,x);
push_up(x);
}
int n;
void Init()
{
root = tot1 = tot2 = 0;
ch[root][0] = ch[root][1] = pre[root] = size[root] = rev[root] = 0;
NewNode(root,0,n+1);
NewNode(ch[root][1],root,n+2);
Build(Key_value,1,n,ch[root][1]);
push_up(ch[root][1]);
push_up(root);
}
//旋转,0为左旋,1为右旋
inline void Rotate(int x,int kind)
{
int y = pre[x];
push_down(y);
push_down(x);//先把y的标记下传,在把x的标记下传
ch[y][!kind] = ch[x][kind];
pre[ch[x][kind]] = y;
if(pre[y])
ch[pre[y]][ch[pre[y]][1]==y] = x;
pre[x] = pre[y];
ch[x][kind] = y;
pre[y] = x;
push_up(y);
}
//Splay调整,将r结点调整到goal下面
inline void Splay(int r,int goal)
{
push_down(r);
while(pre[r] != goal)
{
if(pre[pre[r]] == goal)
{
//有反转操作,需要先push_down,再判断左右孩子
push_down(pre[r]);
push_down(r);
Rotate(r,ch[pre[r]][0]==r);
}
else
{
//有反转操作,需要先push_down
push_down(pre[pre[r]]);
push_down(pre[r]);
push_down(r);
int y = pre[r];
int kind = ch[pre[y]][0]==y;
if(ch[y][kind] == r)
{
Rotate(r,!kind);
Rotate(r,kind);
}
else
{
Rotate(y,kind);
Rotate(r,kind);
}
}
}
push_up(r);
if(goal == 0) root = r;
}
//得到第k个结点(需要push_down)
inline int Get_kth(int r,int k)
{
push_down(r);
int t = size[ch[r][0]] + 1;
if(t == k)return r;
if(t > k)return Get_kth(ch[r][0],k);
else return Get_kth(ch[r][1],k-t);
}
//找前驱(需要push_down)
inline int Get_pre(int r)
{
push_down(r);
if(ch[r][0] == 0)return -1;//不存在
r = ch[r][0];
while(ch[r][1])
{
r = ch[r][1];
push_down(r);
}
return r;
}
//找后继(需要push_down)
inline int Get_next(int r)
{
push_down(r);
if(ch[r][1] == 0)return -1;
r = ch[r][1];
while(ch[r][0])
{
r = ch[r][0];
push_down(r);
}
return r;
}
struct Node
{
int id,val;
}node[MAXN];
bool cmp(Node a,Node b)
{
if(a.val != b.val)return a.val < b.val;
else return a.id < b.id;
}
int main()
{
//freopen("in.txt","r",stdin);
//freopen("out.txt","w",stdout);
while(scanf("%d",&n) == 1 && n)
{
for(int i = 1;i <= n;i++)
{
scanf("%d",&node[i].val);
node[i].id = i;
}
sort(node+1,node+n+1,cmp);
Init();
for(int i = 1; i <= n;i++)
{
Splay(node[i].id,0);
printf("%d",size[ch[root][0]]);
if(i < n)printf(" ");
else printf("\n");
Splay(Get_kth(root,i),0);
Splay(Get_next(node[i].id),root);
Update_Rev(Key_value);
}
}
return 0;
}
bryce1010专题训练——Splay树的更多相关文章
- bryce1010专题训练——LCT&&树链剖分
LCT&&树链剖分专题 参考: https://blog.csdn.net/forever_wjs/article/details/52116682
- bryce1010专题训练——划分树
1.求区间第K大 HDU2665 Kth number /*划分树 查询区间第K大 */ #include<iostream> #include<stdio.h> #inclu ...
- bryce1010专题训练——线段树习题汇总
一.区间查询,无单点更新 hdu2795 Billboard Time Limit: 20000/8000 MS (Java/Others) Memory Limit: 32768/32768 ...
- bryce1010专题训练——树状数组
Bryce1010模板 1.一维树状数组 https://vjudge.net/contest/239647#problem/A[HDU1556] #include<bits/stdc++.h& ...
- bryce1010专题训练——CDQ分治
Bryce1010模板 CDQ分治 1.与普通分治的区别 普通分治中,每一个子问题只解决它本身(可以说是封闭的) 分治中,对于划分出来的两个子问题,前一个子问题用来解决后一个子问题而不是它本身 2.试 ...
- Leedcode算法专题训练(树)
递归 一棵树要么是空树,要么有两个指针,每个指针指向一棵树.树是一种递归结构,很多树的问题可以使用递归来处理. 1. 树的高度 104. Maximum Depth of Binary Tree (E ...
- bryce1010专题训练——LCA
1.Targan算法(离线) http://poj.org/problem?id=1470 /*伪代码 Tarjan(u)//marge和find为并查集合并函数和查找函数 { for each(u, ...
- dp专题训练
****************************************************************************************** 动态规划 专题训练 ...
- DP专题训练之HDU 2955 Robberies
打算专题训练下DP,做一道帖一道吧~~现在的代码风格完全变了~~大概是懒了.所以.将就着看吧~哈哈 Description The aspiring Roy the Robber has seen a ...
随机推荐
- codeforces 463B Caisa and Pylons 解题报告
题目链接:http://codeforces.com/problemset/problem/463/B 题目意思:Caisa 站在 0 pylon 并且只有 0 energy,他需要依次跳过1 pyl ...
- 从Github上下载了项目,导入Android Studio,gradle 报错,应该怎么修改
一.从Github上获取源代码 我这里是直接下载ZIP文件 二.在本机的Android Studio上新建一个空白项目,目的主要是与刚从Github上下载的项目文件结构做对比 三.替换gradle文件 ...
- [ZJOI 2012] 网络
[题目链接] https://www.lydsy.com/JudgeOnline/problem.php?id=2816 [算法] 对每种颜色的边建一棵LCT , 维护联通性即可 时间复杂度 : O( ...
- Dubbo原理与框架设计
Dubbo是常用的开源服务治理型RPC框架,在之前osgi框架下不同bundle之间的方法调用时用到过.其工作原理和框架设计值得开源技术爱好者学习和研究. 一.Dubbo的工作原理 调用关系说明 服务 ...
- MTK DDR调试
1. 获取 flash id: 硬件信息:通过这个节点可以知道当前flash的id,上层根据id找到对应的flash名字. cat /sys/block/mmcblk0/device/cid \ker ...
- Linux 终端显示 Git 当前所在分支
function git_branch { branch="`git branch 2>/dev/null | grep "^\*" | sed -e " ...
- K个联通块
题意: 有一张无重边的无向图, 求有多少个边集,使得删掉边集里的边后,图里恰好有K个联通块. 解法: 考虑dp,$h(i,S)$表示有$i$个联通块,点集为$S$的图的个数,$g(S)$表示点集为S的 ...
- Java中的标识符,类名和包名规则
Java中的包.类.方法.参数和变量的名称 标识符的命名规则 1.变量必须以字母.下划线(_)或美元符号($)开头 2.余下的字符可以是下划线.美元符号或任何的字母或数字 3.不能使用JavaScri ...
- CF-799A
A. Carrot Cakes time limit per test 1 second memory limit per test 256 megabytes input standard inpu ...
- A. Launch of Collider (#363 Div.2)
A. Launch of Collider time limit per test 2 seconds memory limit per test 256 megabytes input standa ...