BZOJ2819 Nim (Dfs sequence)

Source: Internet
Author: User

Title: Single-point modification, tree chain query.

You can do it directly with a tree chain.

The modification is O (qlogn), the query is O (QLOGNLOGN), q=n=500000;

I hear it will time out.

This problem can also be done with DFS sequence.

Without looking at the modification, the query only: You can find the XOR value of each point to the root, then the query for any two points is equal to the XOR (U) ^xor (v) ^val (LCA (U,V));

If there is a modification: the modification is only a single point, and the maintenance is only the path to the root, so the modification will only affect the information of all nodes to the root of the subtree at the root of the point.

So using DFS sequence to the tree to continuous interval with the maintenance of the line segment tree, the modification is essentially a line segment tree of the interval modification, query is a single point query.

Each modification and query is O (logn).

1#include <cstdio>2#include <cstring>3#include <algorithm>4 using namespacestd;5 #defineMAXN 5500006 structedge{7     intv,nxt;8}edge[maxn<<1];9 intNE,HEAD[MAXN];Ten voidAddedge (intUintv) { OneEdge[ne].v=v; Edge[ne].nxt=head[u]; head[u]=ne++; A } - intN,STONE[MAXN]; - intodr,stack[maxn],l[maxn],r[maxn],dep[maxn],fa[ -][MAXN],VAL[MAXN]; the voidDfs () { -     inttop=0; -stack[++top]=1; -val[1]=stone[1]; +      while(top) { -         intu=Stack[top]; +         if(L[u]) { AR[U]=ODR; --top; at             Continue; -         } -l[u]=++ODR; -          for(intI=head[u]; i!=-1; I=edge[i].nxt) { -             intv=edge[i].v; -             if(fa[0][U]==V)Continue; infa[0][v]=u; dep[v]=dep[u]+1; VAL[V]=VAL[U]^STONE[V]; stack[++top]=v; -         } to     } + } -  the inttree[maxn<<2],n,x,y,z; * voidUpdateintIintJintk) { $     if(X<=i && j<=y) {Panax Notoginsengtree[k]^=Z; -         return; the     } +     if(Tree[k]) { Atree[k<<1]^=TREE[K]; tree[k<<1|1]^=Tree[k]; thetree[k]=0; +     } -     intMid=i+j>>1; $     if(x<=mid) Update (i,mid,k<<1); $     if(y>mid) Update (mid+1,j,k<<1|1); - } - intQueryintIintJintk) { the     if(I==J)returnTree[k]; -     if(Tree[k]) {Wuyitree[k<<1]^=TREE[K]; tree[k<<1|1]^=Tree[k]; thetree[k]=0; -     } Wu     intMid=i+j>>1; -     if(X<=mid)returnQuery (i,mid,k<<1); About     returnQuery (mid+1,j,k<<1|1); $ } -  - intLcaintUintv) { -     if(dep[u]>Dep[v]) swap (U,V); A      for(intk=0; k< -; ++k) { +         if((Dep[v]-dep[u]) >>k&1){ thev=Fa[k][v]; -         } $     } the     if(V==u)returnu; the      for(intk= +; k>=0; --k) { the         if(fa[k][u]!=Fa[k][v]) { theu=Fa[k][u]; -v=Fa[k][v]; in         } the     } the     returnfa[0][u]; About } the voidinit () { the dfs (); the      for(intI=1; i< -; ++i) { +          for(intj=1; j<=n; ++j) { -             intt=fa[i-1][j]; the             if(t) fa[i][j]=fa[i-1][t];Bayi         } the     } the      for(n=1; n<odr; n<<=1); -      for(intI=1; i<=n; ++i) { -X=l[i]; Y=l[i]; z=Val[i]; theUpdate1N1); the     } the } the intMain () { -     intq,a,b; the     Charop[ One]; thememset (head,-1,sizeof(head)); thescanf"%d",&n);94      for(intI=1; i<=n; ++i) { thescanf"%d", stone+i); the     } the      for(intI=1; i<n; ++i) {98scanf"%d%d",&a,&b); About Addedge (A, b); - Addedge (b,a);101     }102 init ();103scanf"%d",&q);104      while(q--){ thescanf"%s%d%d",op,&a,&b);106         if(op[0]=='Q'){107             intRes;108X=l[a]; Res=query (1N1);109X=L[B]; Res^=query (1N1); theres^=Stone[lca (A, b)];111             if(res) puts ("Yes"); the             ElsePuts"No");113}Else{ theX=l[a]; Y=r[a]; z=b^Stone[a]; theUpdate1N1); thestone[a]=b;117         }118     }119     return 0; -}

BZOJ2819 Nim (Dfs sequence)

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.