hiho一下-第104周-平衡树·Splay
2017-03-16 18:56
302 查看
记录一个菜逼的成长。。
题目链接
这题可以直接用set做。
这里记下模板。
题目链接
这题可以直接用set做。
这里记下模板。
#include <bits/stdc++.h> using namespace std; #define MINK 0 #define MAXK 1000000001 struct Node{ Node *father,*left,*right; int v; Node(int k, Node* f) {left=NULL;right=NULL;father=f;v=k;} }*root = NULL; void right_Rotate(Node *x) { Node* p = x->father; x -> father = p -> father; if(p -> father != NULL){ if(p->father->left == p) p -> father -> left = x; else p -> father -> right = x; } else root = x; p -> left = x -> right; if(x -> right != NULL)x -> right -> father = p; x -> right = p; p -> father = x; } void left_Rotate(Node *x) { Node* p = x->father; x -> father = p -> father; if(p -> father != NULL){ if(p->father->left == p) p -> father -> left = x; else p -> father -> right = x; } else root = x; p -> right = x -> left; if(x -> left != NULL)x -> left -> father = p; x -> left = p; p -> father = x; } void splay(Node *x, Node *y) { if(x == NULL)return ; while(x -> father != y){ Node *p = x -> father; if(p -> father == y){ if(p -> left == x) right_Rotate(x); else left_Rotate(x); } else { Node *g = p -> father; if(g -> left == p){ if(p -> left == x){ right_Rotate(p); right_Rotate(x); } else { left_Rotate(x); right_Rotate(x); } } else { if(p -> right == x){ left_Rotate(p); left_Rotate(x); } else { right_Rotate(x); left_Rotate(x); } } } } }///在执行这个操作的时候,需要保证y节点一定是x节点祖先。 Node* bst_Insert(Node *T,int key) { if(key < T -> v){ if(T -> left == NULL){ T -> left = new Node(key,T); return T -> left; } else { return bst_Insert(T->left,key); } } else { if(T -> right == NULL){ T -> right = new Node(key,T); return T -> right; } else { return bst_Insert(T->right,key); } } } Node* bst_Find(Node *T,int key) { if(T -> v == key){ return T; } if(key < T -> v){ if(T -> left == NULL){ return NULL; } else { return bst_Find(T->left,key); } } else { if(T -> right == NULL){ return NULL; } else { return bst_Find(T->right,key); } } } void Insert(int key) { if(root == NULL){ root = new Node(key,NULL); } else { Node *node = bst_Insert(root,key); splay(node,NULL); } } void* Find(int key) { Node *node = bst_Find(root,key); splay(node,NULL); } Node* findPrev(int key) { Find(key); Node *node = root -> left; while(node -> right != NULL){ node = node -> right; } return node; } Node* findNext(int key) { Find(key); Node *node = root -> right; while(node -> left != NULL) node = node -> left; return node; } void Delete(int key) { Node* prev = findPrev(key); Node* Next = findNext(key); splay(prev,NULL); splay(Next,prev); if(Next != NULL)Next -> left = NULL; } void deleteInterval(int a,int b) { if(a <= MINK) a = MINK + 1; if(b >= MAXK) b = MAXK - 1; Node *aa = bst_Find(root,a); if(aa == NULL)Insert(a); Node *prev = findPrev(a); Node *bb = bst_Find(root,b); if(bb == NULL)Insert(b); Node *Next = findNext(b); splay(prev,NULL); splay(Next,prev); if(Next != NULL)Next -> left = NULL; } int ans; int query(Node* T, int k) { if (T->v == k) { return k; } if (T->v > k) { if (T->left==NULL) return ans; else return query(T->left, k); } if (T->right==NULL) return T->v; else { ans=T->v; return query(T->right,k); } } int main() { int n,a,b; char ope[5]; scanf("%d",&n); Insert(MINK); Insert(MAXK); for( int i = 0; i < n; i++ ){ scanf("%s",ope); if(ope[0] == 'I'){ scanf("%d",&a); Insert(a); } if(ope[0] == 'Q'){ scanf("%d",&a); ans = query(root,a); printf("%d\n",ans==0?a:ans); } if(ope[0] == 'D'){ scanf("%d%d",&a,&b); deleteInterval(a,b); } } return 0; }
相关文章推荐
- hiho 1333 : 平衡树 splay 区间删除,更新,查询,单点插入
- hiho一下 第四十八周 拓扑排序·二【拓扑排序的应用 + 静态数组 + 拓扑排序算法的时间优化】
- hihoCoder hiho一下 第五十一周 欧拉路·三
- hiho一下 连通性二·边的双连通分量
- Hihocoder #1333 : 平衡树·Splay2
- hiho一下第134周 1468 : 2-SAT·hihoCoder新春晚会
- hiho一下 第五十四周 题目1 : 连通性·三
- hiho一下 连通性·四 点的双连通分量
- [知识点]平衡树之Splay
- hihocoder第57周hiho一下#1196 : 高斯消元·二
- (hiho一下第二周)#1014 Trie树 【模版】
- [CODEVS1343][HNOI]蚱蜢(平衡树splay)
- bzoj 1588 平衡树 splay
- 【学校OJ】 splay平衡树 文艺平衡树
- hiho一下 第六十四周
- hiho一下[156周]:岛屿
- 【BZOJ3224】 【CODEVS4543】 普通平衡树 splay
- hiho一下 第156周 岛屿
- bzoj 3224==tyvj 1728普通平衡树 splay
- 【BZOJ3224】【codevs4543】【tyvj1728】普通平衡树,第一次的splay