Bzoj 2588:spoj 10628. Count on a tree chairman trees +LCA

Source: Internet
Author: User

2588:spoj 10628. Count on a tree
Description given a tree of n nodes, each point has a weight, for M queries (U,V,K), you need to answer U xor Lastans and v these two nodes K small point right. Where Lastans is the answer to the previous query, starting with 0, that is, the first query of U is clear text.

Input the first row of two integer n,m. The second line has n integers, where the I integer represents the weight of the point I. The following N-1 line has two integers (x, y) per line, indicating that the point x points y has an edge. The last m row of two integers per line (u,v,k) represents a set of queries. The Output m line, which represents the answer to each query. Sample Input8 5
105 2 9 3 8 5 7 7
1 2
1 3
1 4
3 5
6 S
3 7
4 8
2 5 1
0 5 2
10 5 3
11 5 4
110 8 2
Sample Output2
8
9
105
7
HINT




HINT:
n,m<=100000
Violent self-esteem ...

Exercises

After iterating through the DFS sequence

Just run the chairman's tree.

Pay attention to the requirement of LCA

The following number of hzwer codes

#include<iostream>#include<cstdio>#include<cstdlib>#include<algorithm>#include<cstring>#defineINF 0x7fffffff#definell Long Long#defineN 100005#defineM 2000005using namespacestd;inline ll Read () {ll x=0, f=1;CharCh=GetChar ();  while(ch>'9'|| ch<'0'){if(ch=='-') f=-1; ch=GetChar ();}  while(ch>='0'&&ch<='9') {x=x*Ten+ch-'0'; ch=GetChar ();} returnx*F;}intN,m,tot,sz,cnt,ind,last;intNum[n],pos[n];intV[n],tmp[n],hash[n],root[n];intLs[m],rs[m],sum[m];intdeep[n],fa[n][ -];structdata{intTo,next;} e[200005];intHead[n];voidInsintUintv) {e[++cnt].to=v;e[cnt].next=head[u];head[u]=CNT;}voidInsertintUintv) {ins (u,v); ins (v,u);}intFindintx) {    intL=1, r=tot;  while(l<=r) {intMid= (l+r) >>1; if(hash[mid]<x) l=mid+1; Else if(hash[mid]==x)returnmid; Elser=mid-1; }}voidDfsintx) {IND++;num[ind]=x;pos[x]=IND;  for(intI=1; i<= -; i++)        if((1<<i) <=deep[x]) fa[x][i]=fa[fa[x][i-1]][i-1]; Else  Break;  for(intI=head[x];i;i=e[i].next)if(fa[x][0]!=e[i].to) {Deep[e[i].to]=deep[x]+1; fa[e[i].to][0]=x;        DFS (E[I].TO); }}intLcaintXinty) {    if(deep[x]<Deep[y]) swap (x, y); intt=deep[x]-Deep[y];  for(intI=0; i<= -; i++)        if((1<<i) &t) x=Fa[x][i];  for(intI= -; i>=0; i--)        if(fa[x][i]!=Fa[y][i]) x=fa[x][i],y=Fa[y][i]; if(x==y)returnx; returnfa[x][0];}voidUpdateintLintRintXint&y,intnum) {y=++sz; Sum[y]=sum[x]+1; if(L==R)return; Ls[y]=ls[x];rs[y]=Rs[x]; intMid= (l+r) >>1; if(num<=mid) Update (L,MID,LS[X],LS[Y],NUM); ElseUpdate (mid+1, R,rs[x],rs[y],num);}intQueintXintYintRK) {    intA=x,b=y,c=lca (x, y), d=fa[c][0]; A=root[pos[a]],b=root[pos[b]],c=root[pos[c]],d=Root[pos[d]]; intL=1, r=tot;  while(l<r) {intMid= (l+r) >>1; inttmp=sum[ls[a]]+sum[ls[b]]-sum[ls[c]]-Sum[ls[d]]; if(TMP&GT;=RK) r=mid,a=ls[a],b=ls[b],c=ls[c],d=Ls[d]; ElseRk-=tmp,l=mid+1, a=rs[a],b=rs[b],c=rs[c],d=Rs[d]; }    returnhash[l];}intMain () {n=read (), m=read ();  for(intI=1; i<=n;i++) V[i]=read (), tmp[i]=V[i]; Sort (tmp+1, tmp+n+1); hash[++tot]=tmp[1];  for(intI=2; i<=n;i++)        if(tmp[i]!=tmp[i-1]) hash[++tot]=Tmp[i];  for(intI=1; i<=n;i++) v[i]=find (V[i]);  for(intI=1; i<n;i++)    {        intU=read (), v=read ();    Insert (U,V); } DFS (1);  for(intI=1; i<=n;i++)    {        intt=Num[i]; Update (1, tot,root[pos[fa[t][0]]],root[i],v[t]); }     for(intI=1; i<=m;i++)    {        intX=read (), Y=read (), rk=read (); X^=Last ; Last=que (X,Y,RK); printf ("%d", last); if(i!=m) printf ("\ n"); }    return 0;}

Bzoj 2588:spoj 10628. Count on a tree chairman trees +LCA

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.