跳到正文

剪贴板

剪贴板 n9g9ksah

原文
剪贴板信息

剪贴板信息

收录
更新
C++
#include <bits/stdc++.h>
#define dbg(x) cerr<<#x<<": "<<x<<endl;
#define int long long
using namespace std;
#define MAX_SIZE (int)3.1e5

const double alpha = 0.2928;

inline int read()
{
	int x=0,f=1;char ch=getchar();
	while (ch<'0'||ch>'9'){if (ch=='-') f=-1;ch=getchar();}
	while (ch>='0'&&ch<='9'){x=x*10+ch-48;ch=getchar();}
	return x*f;
}

typedef struct NODE{
    int value;
    int size;
    int label;
    NODE *lchild,*rchild;
    NODE(){
        this->lchild = this->rchild = nullptr;
        this->size = 1;
        this->value = INT_MAX;
        this->label = INT_MAX;
    }
    NODE(int x){
        this->lchild = this->rchild = nullptr;
        this->size = 1;
        this->value = x;
        this->label = INT_MAX;
    }
}NODE,*PNODE;

int n,m;
int fa[MAX_SIZE];
PNODE root[MAX_SIZE];
bool exist[MAX_SIZE];

inline PNODE newNode(int x){
    return new NODE(x);
}

inline void deleteNode(PNODE &T){
    delete T;
    T = nullptr;
}

inline bool isLeaf(PNODE T){
    if(T==nullptr)
        return true;
    return T->lchild == nullptr;
}

inline void pushup(PNODE T){
    if(!isLeaf(T)){
        T->size = T->lchild->size + T->rchild->size;
        if(T->lchild->value == T->rchild->value){
            if(T->lchild->label > T->rchild->label)
                swap(T->lchild,T->rchild);
        } else if(T->lchild->value > T->rchild->value)
            swap(T->lchild,T->rchild);
        T->value = T->lchild->value;
        T->label = T->lchild->label;
    }
}

inline PNODE Mix(PNODE a, PNODE b){
    PNODE temp = new NODE;
    if(a->value==b->value){
        if(a->label>b->label)
            swap(a,b);
    }
    else if(a->value>b->value)
        swap(a,b);
    temp->lchild = a;
    temp->rchild = b;
    pushup(temp);
    return temp;
}

inline void rotate(PNODE T,bool d){
    PNODE temp;
    if(!d){
        temp = T->rchild;
        T->rchild = T->lchild;
        T->lchild = T->rchild->lchild;
        T->rchild->lchild = T->rchild->rchild;
        T->rchild->rchild = temp;
    } else {
        temp = T->lchild;
        T->lchild = T->rchild;
        T->rchild = T->lchild->rchild;
        T->lchild->rchild = T->lchild->lchild;
        T->lchild->lchild = temp;
    }
    pushup(T->lchild);
    pushup(T->rchild);
    pushup(T);
}

inline void maintain(PNODE T){
    bool d;
    if(!isLeaf(T)){
        if(T->lchild->size < T->size * alpha)
            d = 1;
        else if(T->rchild->size < T->size * alpha)
            d = 0;
        else
            return;
        if(d){
            if(T->rchild->lchild->size >= T->rchild->size * (1-2*alpha)/(1-alpha))
                rotate(T->rchild,!d);
        }else{
            if(T->lchild->rchild->size >= T->lchild->size * (1-2*alpha)/(1-alpha))
                rotate(T->lchild,!d);
        }
        rotate(T,d);
    }
}

void Insert(PNODE T,int x,int pos){
    if(isLeaf(T)){
        T->lchild = newNode(T->value);
        T->rchild = newNode(x);
        T->lchild->label = T->label;
        T->rchild->label = pos;
        if(x==T->value){
            if(pos<T->label)
                swap(T->lchild,T->rchild);
        } else if(x<T->value){
            swap(T->lchild,T->rchild);
        }
        pushup(T);
        return;
    }
    if(x<=T->lchild->value)
        Insert(T->lchild,x,pos);
    else
        Insert(T->rchild,x,pos);
    return ;
}

void Delete(PNODE T,int x){
    if(isLeaf(T))
        return;
    if(x<=T->lchild->value){
        if(isLeaf(T->lchild)){
            if(x!=T->lchild->value)
                return ;
            exist[T->lchild->label] = 1;
            deleteNode(T->lchild);
            PNODE buf = T->rchild;
            *T = *buf;
            deleteNode(buf);
            return ;
        } else {
            Delete(T->lchild,x);
        }
    } else {
        if(isLeaf(T->rchild)){
            if(x!=T->rchild->value)
                return ;
            exist[T->rchild->label] = 1;
            deleteNode(T->rchild);
            PNODE buf = T->lchild;
            *T = *buf;
            deleteNode(buf);
            return ;
        } else {
            Delete(T->rchild,x);
        }
    }
    pushup(T);
    maintain(T);
}

PNODE Merge(PNODE a, PNODE b){
    if(a==nullptr)
        return b;
    if(b==nullptr)
        return a;

    if(b->size >= a->size * alpha / (1-alpha))
        return Mix(a,b);

    PNODE ret = nullptr;
    if(a->size > b->size){
        if(a->lchild->size >= (a->size + b->size) * alpha){
            ret = Merge(a->lchild, Merge(a->rchild,b));
            deleteNode(a);
            pushup(ret);
            return ret;
        }

        ret = Merge(Merge(a->lchild, a->rchild->lchild), Merge(a->rchild->rchild, b));
        deleteNode(a);
        pushup(ret);
        return ret;
    }
    else{
        if(b->rchild->size >= (a->size + b->size) * alpha){
            ret = Merge(Merge(a, b->lchild), b->rchild);
            deleteNode(b);
            pushup(ret);
            return ret;
        }

        ret = Merge(Merge(a, b->lchild->lchild), Merge(b->lchild->rchild, b->rchild));
        deleteNode(b);
        pushup(ret);
        return ret;
    }
}

void init(){
    for(int i=1;i<=n;i++){
        root[i] = new NODE();
        fa[i] = i;
    }
}

int getfa(int x){
    if(fa[x] == x)
        return fa[x];
    return fa[x] = getfa(fa[x]);
}

void merge(int a,int b){
    a = getfa(a);
    b = getfa(b);
    fa[b] = a;
}

void MERGE(int a,int b){
    if(exist[a]||exist[b])
        return ;
    int da = getfa(a);
    int db = getfa(b);
    if(da==db)
        return ;
    root[da] = Merge(root[da],root[db]);
    merge(da,db);
}

int QUERY(int x){
    int fax = getfa(x);
    if(exist[x])
        return -1;
    int ans = root[fax]->value;
    Delete(root[fax],ans);
    return ans;
}

signed main(){
    ios::sync_with_stdio(false);
#ifdef LOCAL
    freopen("in.in","r",stdin);
    freopen("out.out","w",stdout);
    double c1 = clock();
#endif
//============================================
    int opt,x,y;
    n = read();
    m = read();
    init();
    for(int i=1;i<=n;i++)
        Insert(root[i],read(),i);
    for(int i=1;i<=m;i++){
        opt = read();
        switch(opt){
            case 1:
                x = read();
                y = read();
                MERGE(x,y);
                break;
            case 2:
                x = read();
                cout<<QUERY(x)<<endl;
                break;
        }
    }
//============================================
#ifdef LOCAL
    double c2 = clock();
    cerr<<"Used Time: "<<c2-c1<<"ms"<<endl;
    if(c2-c1>1000)
        cerr<<"Warning!! Time Limit Exceeded!!"<<endl;
    fclose(stdin);
    fclose(stdout);
#endif
    return 0;
}