Bzoj 3673 can be persisted and checked by Zky && 3674 to persist and check the enhanced version

Source: Internet
Author: User

The main idea is to maintain a data structure that can be persisted and checked.


Idea: Durable array maintenance can be persisted and checked using a sustainable segment tree. (I don't know where 3674 has strengthened ...)


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-&GT;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;}


Bzoj 3673 can be persisted and checked by Zky && 3674 to persist and check the enhanced version

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.