AC!

P3391 【模板】文艺平衡树

_gifbmp @ 2019-10-05 13:45:31

#include<cstdio>
#define INF 0x3f3f3f3f
using namespace std;
int n,m;
struct Node *null;
struct Node{
    Node *ch[2],*fa;
    int val,cnt,size,mark;
    Node(int v=0){ch[0]=ch[1]=fa=null;val=v;cnt=size=1;mark=0;}
    bool get(){return fa->ch[1]==this;}
    void push_up(){size=cnt+ch[0]->size+ch[1]->size;}
};
struct Splay{
    Node *root;
    Splay(){
        null=new Node(),null->cnt=null->size=0;
        null->ch[0]=null->ch[1]=null->fa=null;
        root=null;
        insert(-INF);
        insert(INF);
    }
    void push_down(Node *x){
        if(!(x->mark))
            return ;
        x->ch[0]->mark^=1;
        x->ch[1]->mark^=1;
        x->mark=0;
        Node *tmp=x->ch[0];
        x->ch[0]=x->ch[1];
        x->ch[1]=tmp;
    } 
    void clear(Node *x){
        if(x==null)
            return ;
        clear(x->ch[0]);
        clear(x->ch[1]);
        delete x;
    }
    void rotate(Node *x){
        Node *y=x->fa,*z=y->fa;
        int type=x->get();
        z->ch[y->get()]=x;x->fa=z;
        y->ch[type]=x->ch[!type];
        x->ch[!type]->fa=y;
        x->ch[!type]=y;
        y->fa=x;
        y->push_up();
    }
    void splay(Node *x,Node *goal){
        while(x->fa!=goal){
            Node *y=x->fa;
            if(y->fa!=goal)
                rotate(x->get()==y->get()?y:x);
            rotate(x);
        }
        x->push_up();
        if(goal==null)
            root=x;
    }
    Node *kth(int x){
        Node *u=root;
        x++;
        while(1){
            if(x<=u->ch[0]->size)
                u=u->ch[0];
            else if(x>u->ch[0]->size+u->cnt)
                x-=u->ch[0]->size+u->cnt,u=u->ch[1];
            else return u;
        }
    }
    void insert(int x){
        Node *u=root,*f=null;
        while(u!=null&&x!=u->val)
            f=u,u=u->ch[x>u->val];
        if(u!=null)
            u->cnt++;
        else{
            u=new Node(x);
            if(f!=null)
                f->ch[x>f->val]=u,u->fa=f;
        }
        splay(u,null);
    }
    void reverse(int l,int r){
        Node *ll=kth(l-1),*rr=kth(r+1);
        splay(ll,null);
        splay(rr,ll);
        root->ch[1]->ch[0]->mark^=1;
    }
    void print(Node *x){
        push_down(x);
        if(x->ch[0]!=null)
            print(x->ch[0]);
        if(x->val>0&&x->val<=n)
            printf("%d ",x->val);
        if(x->ch[1]!=null)
            print(x->ch[1]);
    }
}tree;
int main(){
    scanf("%d%d",&n,&m);
    for(int i=0;i<=n+1;i++)
        tree.insert(i);
    while(m--){
        int l,r;
        scanf("%d%d",&l,&r);
        tree.reverse(l,r);
    }
    tree.print(tree.root);
    return 0;
}

求助,连样例都没过


by pzc2004 @ 2019-10-05 14:27:50

QNDMX


by Hexarhy @ 2019-10-05 14:33:04

指针……


by MC方块人 @ 2019-10-05 15:45:57

@_gifbmp 我看标题的时候,真想说:“你tm不会发题解吗?”


by MC方块人 @ 2019-10-05 15:51:21

@_gifbmp 样例没过你敢提交?老师是怎么教你的?


上一页 |