1803:spoj1487 Query on a tree III time limit:1 Sec Memory limit:64 MB
submit:286 solved:125
[Submit] [Status] Descriptionyou is given a node-labeled rooted tree with n nodes. Define the query (x, K): Find the node whose label is k-th largest in the subtree of the node X. Assume No. Nodes has The same labels. Inputthe first line contains one integer n (1 <= n <= 10^5). The next line contains n integers li (0 <= Li <= 109) which denotes the label of the i-th node. Each line of the following n-1 lines contains the integers u, v. They denote there is an edge between node U and Node v. Node 1 is the root of the tree. The next line contains one integer m (1 <= m <= 10^4) which denotes the number of the queries. Each line of the next m contains and integers x, K. (k <= The total node number in the subtree of x) outputfor each que Ry (x, k), output the index of the node whose label is the k-th largest in the subtree of the node X. Sample Input5
1 3 5) 2 7
1 2
2 3
1 4
3 5
4
2 3
4 1
3 2
3 2
Sample Output
5
4
5
5
Exercises
Well, the first k big, the trust between people ...
Subtree Query DFS sequence + chairman tree get rid of ...
Code:
1#include <cstdio>2 3#include <cstdlib>4 5#include <cmath>6 7#include <cstring>8 9#include <algorithm>Ten One#include <iostream> A -#include <vector> - the#include <map> - -#include <Set> - +#include <queue> - +#include <string> A at #defineINF 1000000000 - - #defineMAXN 200000+5 - - #defineMAXM 3000000+5 - in #defineEPS 1e-10 - to #definell Long Long + - #definePA pair<int,int> the * #defineFor0 (i,n) for (int i=0;i<= (n); i++) $ Panax Notoginseng #defineFor1 (i,n) for (int i=1;i<= (n); i++) - the #defineFor2 (i,x,y) for (int i= (x); i<= (y); i++) + A #defineFor3 (i,x,y) for (int i= (x); i>= (y); i--) the + #defineMoD 1000000007 - $ using namespacestd; $ -InlineintRead () - the { - Wuyi intx=0, f=1;CharCh=GetChar (); the - while(ch<'0'|| Ch>'9'){if(ch=='-') f=-1; ch=GetChar ();} Wu - while(ch>='0'&&ch<='9') {x=Ten*x+ch-'0'; ch=GetChar ();} About $ returnx*F; - - } - structedge{intGo,next;} e[2*MAXN]; A intN,M,CNT,TOT,A[MAXN],B[MAXN],C[MAXN],D[MAXN],RT[MAXN],LS[MAXM],RS[MAXM],S[MAXM]; + inthead[maxn],l[maxn],r[maxn],t[maxn][2]; theInlinevoidInsertintXinty) - { $E[++tot]= (Edge) {y,head[x]};head[x]=tot; theE[++tot]= (Edge) {x,head[y]};head[y]=tot; the } theInlineBOOLcmpintXintY) {returna[x]<a[y];} theInlinevoidUpdateintLintRintXint&y,intz) - { iny=++CNT; thes[y]=s[x]+1; the if(L==R)return; Aboutls[y]=ls[x];rs[y]=Rs[x]; the intMid= (l+r) >>1; the if(z<=mid) update (L,MID,LS[X],LS[Y],Z);ElseUpdate (mid+1, r,rs[x],rs[y],z); the } +InlinevoidDfsintXintf) - { thet[x][0]=++m;BayiUpdate1, n,rt[m-1],rt[m],c[x]); the for(intI=head[x];i;i=e[i].next)if(e[i].go!=f) DFS (e[i].go,x); thet[x][1]=m; - } - the intMain () the the { the -Freopen ("Input.txt","R", stdin); the theFreopen ("output.txt","W", stdout); the 94n=read (); theFor1 (I,n) A[i]=read (), b[i]=i; theSort (b +1, b+n+1, CMP); theFor1 (i,n) c[b[i]]=i;98For1 (i,n-1) Insert (read (), read ()); AboutDfs1,0); -m=read ();101 while(m--)102 {103 intX=read (), K=read (), l=1, r=n,xx=rt[t[x][0]-1],yy=rt[t[x][1]];104 //k=s[yy]-s[xx]+1-k; the while(l!=R)106 {107 intMid= (l+r) >>1, t=s[ls[yy]]-S[ls[xx]];108 if(t>=k) {xx=ls[xx];yy=ls[yy];r=mid;}109 Else{xx=rs[xx];yy=rs[yy];l=mid+1; k-=t;} the }111printf"%d\n", B[l]); the }113 the return 0; the the}
View Code
bzoj1803:spoj1487 Query on a tree III