BZOJ 3673 可持久化並查集 by zky && 3674 可持久化並查集加強版,bzojzky

來源:互聯網
上載者:User

BZOJ 3673 可持久化並查集 by zky && 3674 可持久化並查集加強版,bzojzky

題目大意:維護一種資料結構實現可持久化並查集。


思路:利用可持久化線段樹實現可持久化數組維護可持久化並查集。(不知道3674哪裡加強了。。。


CODE:

#include <cstdio>#include <cstring>#include <iostream>#include <algorithm>#define RANGE 8000010#define MAX 200200using namespace std; struct SegTree{    SegTree *son[2];    int val;         void *operator new(size_t,SegTree *_,SegTree *__,int ___);}mempool[RANGE],*C = mempool,*father[MAX],*h[MAX];void *SegTree:: operator new(size_t,SegTree *_,SegTree *__,int ___) {    C->son[0] = _;    C->son[1] = __;    C->val = ___;    return C++;} int cnt,asks;int verson[MAX],now_ver,latest;int last_ans; SegTree *Modify(SegTree *a,int l,int r,int x,int val){    if(l == r)  return new(NULL,NULL,val)SegTree;    int mid = (l + r) >> 1;    if(x <= mid) return new(Modify(a->son[0],l,mid,x,val),a->son[1],0)SegTree;    return new(a->son[0],Modify(a->son[1],mid + 1,r,x,val),0)SegTree;} int Ask(SegTree *a,int l,int r,int x){    if(l == r)  return a->val;    int mid = (l + r) >> 1;    if(x <= mid) return Ask(a->son[0],l,mid,x);    return Ask(a->son[1],mid + 1,r,x);} inline int Find(int x){    int y;    while(x)        x = Ask(father[now_ver],1,cnt,y = x);    return y;} inline void Unite(int x,int y){    int fx = Find(x),fy = Find(y);    if(fx == fy)    return ;    ++latest;    int hx = Ask(h[now_ver],1,cnt,fx);    int hy = Ask(h[now_ver],1,cnt,fy);    if(hx < hy)        swap(x,y),swap(fx,fy),swap(hx,hy);    father[latest] = Modify(father[now_ver],1,cnt,fy,fx);    h[latest] = Modify(h[now_ver],1,cnt,fx,hx + hy);    now_ver = latest;} inline bool Query(int x,int y)  {      return Find(x) == Find(y);  }   int main(){    cin >> cnt >> asks;    father[0] = new(C,C,0)SegTree;    h[0] = new(C,C,1)SegTree;    for(int flag,x,y,i = 1; i <= asks; ++i) {        scanf("%d",&flag);        if(flag == 1) {            scanf("%d%d",&x,&y);            x ^= last_ans,y ^= last_ans;            Unite(x,y);        }        else if(flag == 2) {            scanf("%d",&x);            x ^= last_ans;            now_ver = verson[x];        }        else {            scanf("%d%d",&x,&y);            x ^= last_ans,y ^= last_ans;            printf("%d\n",last_ans = Query(x,y));        }        verson[i] = now_ver;    }    return 0;}


聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.