1803: Spoj1487 Query on a tree III
Description
You are 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 two nodes have the same labels.
Input
The 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 two 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 two integers x, k. (k <= the total node number in the subtree of x)
Output
For each query (x, k), output the index of the node whose label is the k-th largest in the subtree of the node x.
Sample Input
5
1 3 5 2 7
1 2
2 3
1 4
3 5
4
2 3
4 1
3 2
3 2
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
和前面那个寻找树上路径第k大的点的题差不多,这道题是问子树中第k大,那我们用dfs序把子树转化成序列,就变成了区间第k大,然后就好了。。。。
1 #include<iostream> 2 #include<cstdlib> 3 #include<cmath> 4 #include<cstring> 5 #include<cstdio> 6 #include<algorithm> 7 #include<string> 8 #include<map> 9 #include<queue> 10 #include<vector> 11 #include<set> 12 #define inf 1000000000 13 #define maxn 200000+5 14 #define maxm 3000000+5 15 #define eps 1e-10 16 #define ll long long 17 #define for0(i,n) for(int i=0;i<=(n);i++) 18 #define for1(i,n) for(int i=1;i<=(n);i++) 19 #define for2(i,x,y) for(int i=(x);i<=(y);i++) 20 #define for3(i,x,y) for(int i=(x);i>=(y);i--) 21 #define for4(i,x) for(int i=head[x],y=e[i].go;i;i=e[i].next,y=e[i].go) 22 using namespace std; 23 int read(){ 24 int x=0,f=1;char ch=getchar(); 25 while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();} 26 while(ch>='0'&&ch<='9'){x=10*x+ch-'0';ch=getchar();} 27 return x*f; 28 } 29 struct edge{ 30 int go,next; 31 }e[2*maxn]; 32 int n,m,cnt,tot,a[maxn],b[maxn],c[maxn],d[maxn],root[maxn]; 33 int rs[maxm],ls[maxm],s[maxm],head[maxn],l[maxn],r[maxn],t[maxn][2]; 34 void ins(int x,int y){ 35 e[++tot]=(edge){y,head[x]};head[x]=tot; 36 e[++tot]=(edge){x,head[y]};head[y]=tot; 37 } 38 bool cmp(int x,int y){ 39 return a[x]<a[y]; 40 } 41 void insert(int l,int r,int x,int &y,int v){ 42 y=++cnt; 43 s[y]=s[x]+1; 44 if(l==r)return ; 45 ls[y]=ls[x];rs[y]=rs[x]; 46 int mid=(l+r)>>1; 47 if(v<=mid)insert(l,mid,ls[x],ls[y],v); 48 else insert(mid+1,r,rs[x],rs[y],v); 49 } 50 void dfs(int x,int f){ 51 t[x][0]=++m; 52 insert(1,n,root[m-1],root[m],c[x]); 53 for4(i,x) 54 if(y!=f) 55 dfs(y,x); 56 t[x][1]=m; 57 } 58 int main(){ 59 //freopen("input.txt","r",stdin); 60 //freopen("output.txt","w",stdout); 61 n=read(); 62 for1(i,n)a[i]=read(),b[i]=i; 63 sort(b+1,b+n+1,cmp); 64 for1(i,n)c[b[i]]=i; 65 for1(i,n-1)ins(read(),read()); 66 dfs(1,0); 67 m=read(); 68 while(m--){ 69 int x=read(),k=read(),l=1,r=n,xx=root[t[x][0]-1],yy=root[t[x][1]]; 70 while(l!=r){ 71 int mid=(l+r)>>1,t=s[ls[yy]]-s[ls[xx]]; 72 if(t>=k){ 73 xx=ls[xx];yy=ls[yy];r=mid; 74 } 75 else{ 76 xx=rs[xx];yy=rs[yy];l=mid+1;k-=t; 77 } 78 } 79 printf("%d\n",b[l]); 80 } 81 return 0; 82 }