【题目链接】: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;
}

最新文章

  1. Linux ls
  2. sqlserver2008 创建定时任务
  3. Reveal使用步骤
  4. POJ 1496 Word Index
  5. jsdoc文档
  6. Canu Parameter Reference(canu参数介绍)
  7. 关于时间序列数据库的思考——(1)运用hash文件(例如:RRD,Whisper) (2)运用LSM树来备份(例如:LevelDB,RocksDB,Cassandra) (3)运用B-树排序和k/v存储(例如:BoltDB,LMDB)
  8. NFS 配置服务
  9. 三菱Ethernet工业以太网
  10. php封装curl,模拟POST和GET请求HTTPS请求
  11. NiftyNet开源平台使用
  12. nginx介绍 - 部署到linux中
  13. 关于Stm32定时器+ADC+DMA进行AD采样的实现
  14. TargetScan 数据库简介
  15. ES6高频面试题目整理
  16. Scala编程之访问修饰符
  17. PHPstorm配置PHPunit对composer引入的php代码进行单元测试
  18. Java中float/double取值范围与精度
  19. 为Spring Cloud Config Server配置远程git仓库
  20. JSTL中EL表达式无法直接取size的处理

热门文章

  1. 《Java编程思想》笔记
  2. (转)dp动态规划分类详解
  3. B1607 [Usaco2008 Dec]Patting Heads 轻拍牛头 数学
  4. 使用Web Workers处理线程
  5. PCB Genesis原点坐标转换关系
  6. 使用MALTAB标定实践记录
  7. Oracle 生成数据字典
  8. CDN 内容分发网络
  9. Oracle数据库的导入和导出
  10. 使用std::cout不能输出显示