【题目链接】: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做法)的更多相关文章

  1. Hihocoder 1329 平衡树·Splay(平衡树)

    Hihocoder 1329 平衡树·Splay(平衡树) Description 小Ho:小Hi,上一次你跟我讲了Treap,我也实现了.但是我遇到了一个关键的问题. 小Hi:怎么了? 小Ho:小H ...

  2. hiho #1329 : 平衡树·Splay

    #1329 : 平衡树·Splay 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 小Ho:小Hi,上一次你跟我讲了Treap,我也实现了.但是我遇到了一个关键的问题. ...

  3. [知识点]平衡树之Splay

    // 此博文为迁移而来,写于2015年7月18日,不代表本人现在的观点与看法.原始地址:http://blog.sina.com.cn/s/blog_6022c4720102w6rg.html 1.前 ...

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

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

  5. 平衡树(Splay、fhq Treap)

    Splay Splay(伸展树)是一种二叉搜索树. 其复杂度为均摊\(O(n\log n)\),所以并不可以可持久化. Splay的核心操作有两个:rotate和splay. pushup: 上传信息 ...

  6. fhq_treap || BZOJ 3223: Tyvj 1729 文艺平衡树 || Luogu P3391 【模板】文艺平衡树(Splay)

    题面: [模板]文艺平衡树(Splay) 题解:无 代码: #include<cstdio> #include<cstring> #include<iostream> ...

  7. P3391 文艺平衡树(Splay)

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

  8. Hihocoder 1325 平衡树·Treap(平衡树,Treap)

    Hihocoder 1325 平衡树·Treap(平衡树,Treap) Description 小Ho:小Hi,我发现我们以前讲过的两个数据结构特别相似. 小Hi:你说的是哪两个啊? 小Ho:就是二叉 ...

  9. HihoCoder 1325 平衡树·Treap

    HihoCoder 1325 平衡树·Treap 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 小Ho:小Hi,我发现我们以前讲过的两个数据结构特别相似. 小Hi:你说 ...

随机推荐

  1. C++组合通信

    #include <iostream> #include<vector> #include<string> using namespace std; class A ...

  2. 最经典的SDK程序结构 HelloWin

    程序运行效果:在创建窗口的时候,播放一个声音.且在窗口的客户区中央画一句文字:Hello, Windows 98!,无论程序怎么移动.最大化,文字始终在程序的中央部位. 程序总共分为六个步骤:定义,注 ...

  3. 背包问题的方案总数 P1474 货币系统

    背包问题的方案总数 对于一个给定了背包容量.物品费用.物品间相互关系(分组.依赖等)的背包问题,除了再给定每个物品的价值后求可得到的最大价值外,还可以得到装满背包或将背包装至某一指定容量的方案总数. ...

  4. EJB是什么鸟东西?

    到底EJB是什么 到底EJB是什么?被口口相传的神神秘秘的,百度一番,总觉得没有讲清楚的,仍觉得一头雾水.百度了很久,也从网络的文章的只言片语中,渐渐有了头绪. 用通俗话说,EJB就是:"把 ...

  5. Codeforces--630I--Parking Lot(规律)

     I - Parking Lot Crawling in process... Crawling failed Time Limit:500MS     Memory Limit:65536KB  ...

  6. UESTC--1252--24点游戏(dfs)

     24点游戏 Time Limit: 1000MS   Memory Limit: 65535KB   64bit IO Format: %lld & %llu Submit Status ...

  7. c++之——————各种变量

    对我们程序员来讲,“变量”和“对象”是可以相互互换使用的.-------------开篇之词. 变量:提供一个具有名字的可供程序操作的存储空间.由类型说明符和其后紧跟的数个列表组成,其中变量名之间使用 ...

  8. 洛谷 P2986 [USACO10MAR]Great Cow Gat…(树形dp+容斥原理)

    P2986 [USACO10MAR]伟大的奶牛聚集Great Cow Gat… 题目描述 Bessie is planning the annual Great Cow Gathering for c ...

  9. [Apple开发者帐户帮助]四、管理密钥(1)创建私钥以访问服务

    私钥允许您访问和验证与某些应用服务(如APN,MusicKit和DeviceCheck)的通信.您将在对该服务的请求中使用JSON Web令牌(JWT)中的私钥. 所需角色:帐户持有人或管理员. 在“ ...

  10. OOM三种类型

    OOM的三种类型: 堆OOM /** * -Xmx1g -XX:+PrintGCDetails -XX:MaxDirectMemorySize=100m * * @param args */ publ ...