ZOJ 3765 Lights (SPLAY)

来源:互联网 发布:淘宝换货 编辑:程序博客网 时间:2024/06/13 11:20

splay。只为了贴代码保存,没用其他作用。


#include<cstdio>#include<cstring>#include<algorithm>#include<iostream>using namespace std;#define maxn 500007#define inf  1000000000struct Node{    Node *fa,*ch[2];    int val,gcd[2],size,statu;};Node pool[maxn];Node *nil;int cnt = 0;int gcd(int a,int b){    if(b <= 0) return a;    return gcd(b,a%b);}void init(){    cnt = 1;    nil = pool;    nil->size = 0;    nil->fa = nil;    nil->gcd[0] = nil->gcd[1] = -1;}Node *newnode(int val,int statu,Node *f){    pool[cnt].fa = f;    pool[cnt].ch[0]=pool[cnt].ch[1]=nil;    pool[cnt].val = val;    pool[cnt].size = 1;    pool[cnt].gcd[statu] = val;    pool[cnt].gcd[!statu] = -1;    pool[cnt].statu = statu;    return &pool[cnt++];}//splay向上更新信息******void update(Node *x){    x->size = x->ch[0]->size + x->ch[1]->size + 1;    int statu = x->statu;    x->gcd[statu] = x->val;    x->gcd[!statu] = -1;    if(x->ch[0] != nil){        x->gcd[0] = gcd(x->ch[0]->gcd[0],x->gcd[0]);        x->gcd[1] = gcd(x->ch[0]->gcd[1],x->gcd[1]);    }    if(x->ch[1] != nil){        x->gcd[0] = gcd(x->ch[1]->gcd[0],x->gcd[0]);        x->gcd[1] = gcd(x->ch[1]->gcd[1],x->gcd[1]);    }}//将结点x旋转至splay中父亲的位置******void rotate(Node *x){    Node *f = x->fa, *ff = f->fa;    int t = (f->ch[1] == x);    if(ff != nil)        ff->ch[ff->ch[1] == f] = x;    x->fa = ff;    f->ch[t] = x->ch[t^1];    x->ch[t^1]->fa = f;    x->ch[t^1] = f;    f->fa = x;    update(f);}//将结点x旋转至x所在splay的根位置******void splay(Node *x,Node *end){    Node *f, *ff;    while(x->fa!=end){        f = x->fa,ff = f->fa;        if(f->fa != end)            if((ff->ch[1]==f)&&(f->ch[1] == x))                rotate(f);            else rotate(x);        rotate(x);    }    update(x);}Node*find(int k,Node *root){    if(root->ch[0]->size == k-1) return root;    if(root->ch[0]->size >= k)        return find(k,root->ch[0]);    return find(k-root->ch[0]->size-1,root->ch[1]);}int main(){    int n,m;    while(scanf("%d%d",&n,&m)!=EOF){        init();        Node* root = newnode(-1,0,nil);        Node* left,*right;        int l,r,u,statu,val;        char x[2];        for(int i = 0;i < n; i++){            scanf("%d%d",&val,&statu);            left = newnode(val,statu,root);            root->ch[1] = left;            splay(left,nil);            root = left;        }        left = newnode(-1,0,root);        root->ch[1] = left;        splay(left,nil);        root = left;        while(m--){            scanf("%s",x);            if(x[0] == 'Q'){                scanf("%d%d%d",&l,&r,&statu);                root = find(l,root);                splay(root,nil);                right = find(r+2,root);                splay(right,root);                printf("%d\n",right->ch[0]->gcd[statu]);            }            else if(x[0] == 'I'){                scanf("%d%d%d",&l,&val,&statu);                root = find(l+1,root);                splay(root,nil);                right = newnode(val,statu,root);                right->ch[1] = root->ch[1];                root->ch[1]->fa = right;                root->ch[1] = right;                update(right);                splay(right,nil);                root = right;            }            else if(x[0] == 'D'){                scanf("%d",&l);                root = find(l,root);                splay(root,nil);                right = find(l+2,root);                splay(right,root);                right->ch[0] = nil;                splay(root=right,nil);            }            else if(x[0] == 'R'){                scanf("%d",&l);                root = find(l+1,root);                splay(root,nil);                root->statu = !root->statu;                update(root);            }            else if(x[0] == 'M'){                scanf("%d%d",&l,&val);                root = find(l+1,root);                splay(root,nil);                root->val = val;                update(root);            }        }    }    return 0;}


0 0
原创粉丝点击