【hihocoder 1329】平衡树·Splay(Splay做法)
【题目链接】:http://hihocoder.com/problemset/problem/1329
【题意】
【题解】
插入操作:…,记住每次插入之后都要把它放到根节点去就好;
询问操作:对于询问x,然后找到权值为x+1的这个节点的左子树中的最大值;(如果没有这个x+1节点,则自己插入一个,之后删掉就好);
删除操作:插入两个节点l和r;然后找到小于l且最大的数字所在的节点lu,把它提到根节点所在的位置,然后再找到大于r且最小的数字所在的节点rv;把它提到根节点的右儿子所在的位置;
则根节点的右儿子的左子树就是需要删掉的->也即代表了[l..r]这个区间
【Number Of WA】
0
【完整代码】
#include <bits/stdc++.h>
using namespace std;
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define LL long long
#define rep1(i,a,b) for (int i = a;i <= b;i++)
#define rep2(i,a,b) for (int i = a;i >= b;i--)
#define mp make_pair
#define pb push_back
#define fi first
#define se second
#define ms(x,y) memset(x,y,sizeof x)
typedef pair<int,int> pii;
typedef pair<LL,LL> pll;
const int dx[9] = {0,1,-1,0,0,-1,-1,1,1};
const int dy[9] = {0,0,0,-1,1,-1,1,-1,1};
const double pi = acos(-1.0);
const int N = 110;
struct node
{
int val;
node *par,*child[2];
node(int val): val(val),par(NULL){}
};
int n;
char s[3];
node *root;
void rotate(node* const x, int c) {
node* const y = x->par;
y->child[c ^ 1] = x->child[c];
if (x->child[c] != NULL) x->child[c]->par = y;
x->par = y->par;
if (y->par != NULL && y->par->child[0] == y) y->par->child[0] = x;
else if (y->par != NULL && y->par->child[1] == y) y->par->child[1] = x;
y->par = x;
x->child[c] = y;
}
inline bool _splay_parent(node *x, node* (&y), node* stop) {
return (y = x->par) != stop && (x == y->child[0] || x == y->child[1]);
}
void splay(node* const x, node* const stop) {
for (node *y, *z; _splay_parent(x, y, stop); ) {
if (_splay_parent(y, z, stop)) {
const int c = y == z->child[0];
if (x == y->child[c]) rotate(x, c ^ 1), rotate(x, c);
else rotate(y, c), rotate(x, c);
} else {
rotate(x, x == y->child[0]);
break;
}
}
if (stop == NULL) root = x;
}
node *cr(node *u,int val)
{
if (u->val==val) return u;
if (u->child[val>u->val]==NULL)
{
node *v = new node(val);
v->child[0] = v->child[1] = NULL;
v->par = u;
u->child[val>u->val] = v;
return v;
}
return cr(u->child[val>u->val],val);
}
node *cz(node *v,int val)
{
if (v->val==val) return v;
if (v->child[val>v->val]==NULL) return NULL;
return cz(v->child[val>v->val],val);
}
void cr(int x)
{
node *u = cr(root,x);
splay(u,NULL);
}
node *get_max(node * v)
{
if (v->child[1]==NULL)
return v;
return get_max(v->child[1]);
}
node *get_min(node *v)
{
if (v->child[0]==NULL)
return v;
return get_min(v->child[0]);
}
void sc(int l,int r)
{
cr(l),cr(r);
node *u = cz(root,l);
splay(u,NULL);
node *lu = get_max(u->child[0]);
node *v = cz(root,r);
splay(v,NULL);
node *rv = get_min(v->child[1]);
splay(lu,NULL);
splay(rv,lu);
rv->child[0] = NULL;
}
int query(int x)
{
node *v = cz(root,x+1);
bool ju = (v==NULL);
if (ju) v = cr(root,x+1);
splay(v,NULL);
int k = get_max(v->child[0])->val;
if (ju) sc(x+1,x+1);
return k;
}
int main()
{
//freopen("F:\\rush.txt","r",stdin);
ios::sync_with_stdio(false),cin.tie(0);//scanf,puts,printf not use
root = new node(-1);
root->child[0] = root->child[1] = NULL;
cr(21e8);
cin >> n;
rep1(i,1,n)
{
cin >> s;
if (s[0]=='I')
{
int x;
cin >> x;
cr(x);
}
else
if (s[0]=='D'){
int l,r;
cin >> l >> r;
sc(l,r);
}
else
if (s[0]=='Q'){
int x;
cin >> x;
cout << query(x) << endl;
}
}
return 0;
}
【hihocoder 1329】平衡树·Splay(Splay做法)的更多相关文章
- Hihocoder 1329 平衡树·Splay(平衡树)
Hihocoder 1329 平衡树·Splay(平衡树) Description 小Ho:小Hi,上一次你跟我讲了Treap,我也实现了.但是我遇到了一个关键的问题. 小Hi:怎么了? 小Ho:小H ...
- hiho #1329 : 平衡树·Splay
#1329 : 平衡树·Splay 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 小Ho:小Hi,上一次你跟我讲了Treap,我也实现了.但是我遇到了一个关键的问题. ...
- [知识点]平衡树之Splay
// 此博文为迁移而来,写于2015年7月18日,不代表本人现在的观点与看法.原始地址:http://blog.sina.com.cn/s/blog_6022c4720102w6rg.html 1.前 ...
- P3391 【模板】文艺平衡树(Splay)新板子
P3391 [模板]文艺平衡树(Splay) 题目背景 这是一道经典的Splay模板题——文艺平衡树. 题目描述 您需要写一种数据结构(可参考题目标题),来维护一个有序数列,其中需要提供以下操作:翻转 ...
- 平衡树(Splay、fhq Treap)
Splay Splay(伸展树)是一种二叉搜索树. 其复杂度为均摊\(O(n\log n)\),所以并不可以可持久化. Splay的核心操作有两个:rotate和splay. pushup: 上传信息 ...
- fhq_treap || BZOJ 3223: Tyvj 1729 文艺平衡树 || Luogu P3391 【模板】文艺平衡树(Splay)
题面: [模板]文艺平衡树(Splay) 题解:无 代码: #include<cstdio> #include<cstring> #include<iostream> ...
- P3391 文艺平衡树(Splay)
题目背景 这是一道经典的Splay模板题--文艺平衡树. 题目描述 您需要写一种数据结构(可参考题目标题),来维护一个有序数列,其中需要提供以下操作:翻转一个区间,例如原有序序列是5 4 3 2 1, ...
- Hihocoder 1325 平衡树·Treap(平衡树,Treap)
Hihocoder 1325 平衡树·Treap(平衡树,Treap) Description 小Ho:小Hi,我发现我们以前讲过的两个数据结构特别相似. 小Hi:你说的是哪两个啊? 小Ho:就是二叉 ...
- HihoCoder 1325 平衡树·Treap
HihoCoder 1325 平衡树·Treap 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 小Ho:小Hi,我发现我们以前讲过的两个数据结构特别相似. 小Hi:你说 ...
随机推荐
- Android序列化的存储和读取
Android中序列化的实现有两种方式:Serializable接口和Parcelable接口,本文对这两种方式进行简单的总结和使用. 一.相关概念 (一)序列化的原因(序列化能实现的效果) 1.永久 ...
- POJ 2728(最优比率生成树+01规划)
Dese ...
- bzoj4373 算术天才⑨与等差数列——线段树+set
题目:https://www.lydsy.com/JudgeOnline/problem.php?id=4373 一个区间有以 k 为公差的数列,有3个条件: 1.区间 mx - mn = (r-l) ...
- bzoj2503 相框——思路
题目:https://www.lydsy.com/JudgeOnline/problem.php?id=2503 思路题: 首先,这种问题应该注意到答案只跟度数有关,跟其他什么连接方法之类的完全无关: ...
- Thinkpad E450c进入BIOS
重启系统,一直按F12,进入系统设置后,按tab进入App Menu选项卡,选择Setup按回车进入BIOS设置
- (Go)07.strings与strconv的示例
package main import ( "strconv" "fmt" "strings" ) func main() { str := ...
- javascript必须知道的知识要点(二)
该文章不详细叙述各知识要点的具体内容,仅把要点列出来,供大家学习的时候参照,或者检测自己是否熟练掌握了javascript,清楚各个部分的内容. 内建对象可划分为数据封装类对象.工具类对象.错误类对象 ...
- iTex导出PDF
iText导出PDF,所需jar包如下: itext-asian-5.2.0.jar 支持导出中文的jar包 itextpdf-5.5.9.jar PDF核心jar包 bcprov-jdk15on-1 ...
- BZOJ 4810 莫队+bitset
思路: 看完这道题根本没有思路啊.... 然后我就膜拜了一波题解... 这神tm乱搞思路 维护两个bitset 第一个bitset代表当前区间哪些数出现过 第二个bitset是 maxp-p出现过 差 ...
- D - Replacement
Problem description Little Petya very much likes arrays consisting of n integers, where each of them ...