2588:spoj 10628. Count on a tree
Time Limit:1 Sec
Memory limit:256 MB
Topic Connection http://www.lydsy.com/JudgeOnline/problem.php?id=2588
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 between the K small point weights. Where Lastans is the answer to the previous query, starting with 0, that is, the first query of U is clear text.
InputThe first row of two integers 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.
Outputm-line, which represents the answer to each question. Sample Input
8 5
105 2 9 3 8 5 7 7
1 2
1 3
1 4
3 5
3 6
3 7
4 8
2 5 1
0 5 2
10 5 3
11 5 4
110 8 2
Sample Output
2
8
9
105
HINTHINT:
n,m<=100000
violent self-esteem ...
Test instructions
Exercises
Tree running the Chairman tree
This kind of problem is like this ...
Code from Hzwer:
Code:
//Qscqesze#include <cstdio>#include<cmath>#include<cstring>#include<ctime>#include<iostream>#include<algorithm>#include<Set>#include<bitset>#include<vector>#include<sstream>#include<queue>#include<typeinfo>#include<fstream>#include<map>#include<stack>typedefLong Longll;using namespacestd;//freopen ("d.in", "R", stdin);//freopen ("D.out", "w", stdout);#defineSspeed ios_base::sync_with_stdio (0); Cin.tie (0)#defineMAXN 2000005#defineEPS 1e-9intNum;//const int INF=0X7FFFFFFF; //§ß§é§à§é¨f§3Const intinf=0x3f3f3f3f; inline ll read () {ll x=0, f=1;CharCh=GetChar (); while(ch<'0'|| Ch>'9'){if(ch=='-') f=-1; ch=GetChar ();} while(ch>='0'&&ch<='9') {x=x*Ten+ch-'0'; ch=GetChar ();} returnx*F;}//**************************************************************************************#defineN 100005#defineM 2000005intN,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//two points{ 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//LCA pretreatment{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//LCA{ 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//Update{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>=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 () {//freopen ("Test.txt", "R", stdin);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 running chairman trees