本文共 6782 字,大约阅读时间需要 22 分钟。
在前面代码的基础上,我增加一些操作!
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- #include <iostream>
- #include <cstdio>
- #include <cstring>
- #include <cstdlib>
- #include <cmath>
- #define INF ~0u>>1
- #define NIL SPLAY
- #define MN 200005
- using namespace std;
- int n,m,l,r,x,pos;
- char s[10];
- struct SPLAYTREE{
- struct Node{
- int key,minv,size,add;
- bool rev;
- Node *left,*right,*father;
- Node (){}
- Node(int _key):key(_key){minv=_key,size=1,add=0,rev=false;}
- }SPLAY[MN],*SP,*root,*head,*tail;
-
-
-
-
- void init(){
- SP=NIL;
- NIL->key=NIL->minv=INF,NIL->size=0;NIL->rev=false;NIL->add=0;
- NIL->left=NIL->right=NIL->father=NIL;
- head=new(++SP)Node(INF);
- head->left=head->right=head->father=NIL;
- tail=new(++SP)Node(INF);
- tail->left=tail->right=tail->father=NIL;
- head->right=tail,tail->father=head,head->size++;
- root=head;
- }
-
-
-
- void pushdown(Node *&t){
- if(t->rev){
- swap(t->left,t->right);
- t->left->rev=!t->left->rev;
- t->right->rev=!t->right->rev;
- t->rev=false;
- }
- if(t->add){
- if(t->left!=NIL){
- t->left->key+=t->add;
- t->left->minv+=t->add;
- t->left->add+=t->add;
- }
- if(t->right!=NIL){
- t->right->key+=t->add;
- t->right->minv+=t->add;
- t->right->add+=t->add;
- }
- t->add=0;
- }
- }
-
-
-
- void update(Node *&t){
- t->size=t->left->size+t->right->size+1;
- t->minv=min(t->key,min(t->left->minv,t->right->minv));
- }
-
-
-
- void zig(Node *&t){
- Node *f=t->father,*r=t->right;
- pushdown(f->right);
- pushdown(t->left);
- pushdown(t->right);
- t->father=f->father;
- if(f==root) root=t;
- else{
- if(f->father->left==f) f->father->left=t;
- else f->father->right=t;
- }
- t->right=f,r->father=f,f->father=t,f->left=r;
- update(f);update(t);
- }
-
-
-
- void zag(Node *&t){
- Node *f=t->father,*l=t->left;
- pushdown(f->left);
- pushdown(t->left);
- pushdown(t->right);
- t->father=f->father;
- if(f==root) root=t;
- else{
- if(f->father->left==f) f->father->left=t;
- else f->father->right=t;
- }
- t->left=f,l->father=f,f->father=t,f->right=l;
- update(f);update(t);
- }
-
-
-
- void splay(Node *&root,Node *&t){
- pushdown(t);
- while(root!=t){
- if(t->father==root){
- if(t->father->left==t) zig(t);
- else zag(t);
- }
- else{
- if(t->father->father->left==t->father){
- if(t->father->left==t) zig(t->father),zig(t);
- else zag(t),zig(t);
- }else{
- if(t->father->left==t) zig(t),zag(t);
- else zag(t->father),zag(t);
- }
- }
- }
- }
-
-
-
- Node* build(Node *father,int *a,int ll,int rr){
- if(ll>rr)return NIL;
- int mid=(ll+rr)>>1;
- SP=SP+mid;
- Node *t;
- t=new(SP)Node(a[mid]);
- t->father=father;
- t->left=build(t,a,ll,mid-1);
- t->right=build(t,a,mid+1,rr);
- update(t);
- return t;
- }
-
-
-
-
-
-
-
- void build_tree(Node *&t,int *a,int n){
- int ll=1,rr=n;
- int mid=(ll+rr)>>1;
- SP=SP+mid;
- t=new(SP)Node(a[mid]);
- t->father=NIL;
- t->left=build(t,a,ll,mid-1);
- t->right=build(t,a,mid+1,rr);
- update(t);
- }
-
-
-
- void insert(int key,int pos){
- Node *t=new(++SP)Node(key);
- t->left=t->right=t->father=NIL;
- Node *r=root,*p;
- bool flag=false;
- while(pushdown(r),r!=NIL){
- p=r,r->size++;
- if(r->left->size+1>pos)r=r->left,flag=false;
- else pos-=r->left->size+1,r=r->right,flag=true;
- }
- if(flag) p->right=t;
- else p->left=t;
- t->father=p;
- splay(root,t);
- }
-
-
-
- void select(Node *&root,int pos){
- Node *r=root;
- while(pushdown(r),r->left->size+1!=pos){
- if(r->left->size+1>pos) r=r->left;
- else pos-=r->left->size+1,r=r->right;
- }
- splay(root,r);
- }
-
-
-
- void insert_k(int pos,int *a,int n){
- Node *t;
- build_tree(t,a,n);
- select(root,pos);
- select(root->right,1);
- root->right->left=t;
- t->father=root->right;
- update(root->right);
- update(root);
- splay(root,t);
- }
-
-
-
- void remove(int pos){
- select(root,pos);
- if(root->left==NIL) root=root->right;
- else if(root->right==NIL) root=root->left;
- else{
- select(root->left,root->left->size);
- root->left->right=root->right;
- root->right->father=root->left;
- root=root->left;
- }
- root->father=NIL;
- update(root);
- }
-
-
-
- void remove_k(int ll,int rr){
-
- select(root,ll);
- select(root->right,rr-ll);
-
- root->right->left=NIL;
- update(root->right);
- update(root);
- }
-
-
-
- void plus(int l,int r,int a){
-
- select(root,l);
- select(root->right,r-l);
-
- Node *t=root->right->left;
- t->add+=a,t->key+=a,t->minv+=a;
- splay(root,t);
- }
-
-
-
- void reverse(int l,int r){
- select(root,l);
- select(root->right,r-l);
- Node *t=root->right->left;
- t->rev=!t->rev;
- splay(root,t);
- }
-
-
-
- void revolve(int l,int r,int a){
- select(root,l);
- select(root->right,r-l);
- select(root->right->left,root->right->left->size-a);
- select(root->right->left->right,root->right->left->right->size);
- Node *p=root->right->left,*t=root->right->left->right;
- p->right=NIL;
- p->father->left=t,t->father=p->father;
- t->right=p,p->father=t;
- update(t);update(p);
- splay(root,p);
- }
-
-
-
- int query(int l,int r){
- select(root,l);
- select(root->right,r-l);
- return root->right->left->minv;
- }
-
-
-
- void inorder(Node *t){
- pushdown(t);
- if(t->left!=NIL)
- inorder(t->left);
- if(t->key!=INF)printf("%d ",t->key);
- if(t->right!=NIL)
- inorder(t->right);
-
- }
- }tree;
-
- int main(){
- int a[10]={0,1,2,3,4,5,6,7,8,9};
- tree.init();
- tree.insert_k(1,a,9);
- cout<<"after insert array a after position 0:";tree.inorder(tree.root);cout<<endl;
- tree.insert_k(1,a,9);
- cout<<"after insert array a after position 0:";tree.inorder(tree.root);cout<<endl;
- tree.remove_k(1,11);
- cout<<"after remove interval [1,9]:"; tree.inorder(tree.root);cout<<endl;
- tree.insert(19,1);
- cout<<"after insert 19 at position 0:"; tree.inorder(tree.root);cout<<endl;
- tree.remove(2);
- cout<<"after remove position 1:";tree.inorder(tree.root);cout<<endl;
- tree.plus(1,11,10);
- cout<<"after plus 10 between interval [1,9]:"; tree.inorder(tree.root);cout<<endl;
- tree.reverse(1,11);
- cout<<"after reverse interval [1,9]:"; tree.inorder(tree.root);cout<<endl;
- tree.revolve(1,11,3);
- cout<<"after revolve interval [1,9] 3 times:"; tree.inorder(tree.root);cout<<endl;
- int ans=tree.query(1,11);
- cout<<"the min value between interval [1,9] is:"<<ans<<endl;
- return 0;
- }
转载地址:http://uftto.baihongyu.com/