Bzoj 4129 Haruna ' s Breakfast with modified tree on MO team + chunking

Source: Internet
Author: User

Topic: Given a tree, each point has a non-negative point, support the following operations
1. Modify the point right of a point
2. Query the MEX on a chain
Consider the link without the modified version, we can use MO Team + sub-block to engage (link stamp here)
Now to the tree with modified, decisive Candy Park
Would have been holding a tease than the mentality of writing a result 1.4s over
It's not exactly proportional to the 80s in the Candy Park. 0.0

#include <cmath>#include <cstdio>#include <cstring>#include <iostream>#include <algorithm>#define M 50500#define Busing namespace STD;structabcd{intTo,next;} table[m<<1];intHead[m],tot;intN,m,b1,b2,q,c,l=1, r=1, t;intfa[m],ancestor[m][ -],DPT[M];intA[M],CNT[M],BLOCK[M];BOOLV[M];intbelong[m],_cnt;structmodifiction{intX,from,to,t;} MODIFICTIONS[M];structquery{intX,y,t,ans;BOOL operator< (ConstQuery &q)Const{if(belong[x]! = belong[q.x])returnBELONG[X] < belong[q.x];if(Belong[y]! = Belong[q.y])returnBelong[y] < BELONG[Q.Y];returnT < q.t; }}QUERIES[M];BOOLCompare (ConstQuery &q1,ConstQuery &q2) {returnq1.t < q2.t;}voidADD (intXintY) {table[++tot].to=y;    TABLE[TOT].NEXT=HEAD[X]; Head[x]=tot;}voidDFS (intx) {Static int Stack[M],top;intI,bottom=top; dpt[x]=dpt[fa[x]]+1; for(i=1; i<= the; i++) ancestor[x][i]=ancestor[ancestor[x][i-1]][i-1]; for(I=head[x];i;i=table[i].next)if(Table[i].to!=fa[x]) {fa[table[i].to]=ancestor[table[i].to][0]=x; DFS (table[i].to);if(TOP-BOTTOM&GT;=B1) {++_cnt; while(Top>bottom) belong[Stack[top--]]            =_cnt; }        }Stack[++top]=x;if(x==1) {++_cnt; while(top) belong[Stack[top--]]    =_cnt; }}voidUpdate (intx) {v[x]=true;if(a[x]>n)return; cnt[a[x]]++;if(cnt[a[x]]==1) block[a[x]/b2]++;}voidDowndate (intx) {v[x]=false;if(a[x]>n)return; cnt[a[x]]--;if(!cnt[a[x]]) block[a[x]/b2]--;}intLCA (intXintY) {intJif(Dpt[x]<dpt[y]) swap (x, y); for(j= the; ~j;j--)if(Dpt[ancestor[x][j]]>=dpt[y]) x=ancestor[x][j];if(x==y)returnX for(j= the; ~j;j--)if(Ancestor[x][j]!=ancestor[y][j]) x=ancestor[x][j],y=ancestor[y][j];returnancestor[x][0];}voidTransfer (intXintY) {intLca=lca (x, y); for(; X!=lca;x=fa[x]) {if(V[x]) Downdate (x);ElseUpdate (x); } for(; Y!=lca;y=fa[y]) {if(V[y]) Downdate (y);ElseUpdate (y); }}intGet_mex () {intI,j; for(i=0;; i++)if(BLOCK[I]!=B2) for(J=I*B2;; J + +)if(!cnt[j])returnJ;}intMain () {intI,p,x,y;Cin>>n>>m; b1=POW(N,2.0/3.0)+1e-7; B2=sqrt(n) +1e-7; for(i=1; i<=n;i++)scanf("%d", &a[i]); for(i=1; i<n;i++) {scanf("%d%d", &x,&y); ADD (x, y);    ADD (Y,X); } DFS (1); for(i=1; i<=m;i++) {scanf("%d%d%d", &p,&x,&y);if(p==0) {modifictions[++c].x=x;            modifictions[C].from=a[x];            modifictions[c].to=y;            modifictions[c].t=i;        A[x]=y; }Else{if(Belong[x]>belong[y]) swap (x, y);            Queries[++q].x=x;            queries[q].y=y;        queries[q].t=i;    }} t=c; Sort (queries+1, queries+q+1); for(i=1; i<=q;i++) {intLca=lca (QUERIES[I].X,QUERIES[I].Y);        Transfer (l,queries[i].x);        Transfer (R,QUERIES[I].Y);        l=queries[i].x; R=QUERIES[I].Y; while(modifictions[t].t > queries[i].t) {if(v[modifictions[t].x])                {downdate (modifictions[t].x);                A[modifictions[t].x]=modifictions[t].from;            Update (modifictions[t].x); }ElseA[modifictions[t].x]=modifictions[t].from;        --t; } while(T<c && modifictions[t+1].t < queries[i].t) {if(v[modifictions[t+1].x]) {downdate (modifictions[t+1].x); a[modifictions[t+1].x]=modifictions[t+1].to; Update (modifictions[t+1].x); }Elsea[modifictions[t+1].x]=modifictions[t+1].to;        ++t;        } Update (LCA);        Queries[i].ans=get_mex ();    Downdate (LCA); } sort (queries+1, queries+q+1, Compare); for(i=1; i<=q;i++)printf("%d\n", Queries[i].ans);return 0;}

Bzoj 4129 Haruna ' s Breakfast with modified tree on MO team + chunking

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: 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.