# BZOJ2588 [SPOJ10628]Count on a tree

## Output

M行，表示每个询问的答案。最后一个询问不输出换行符

## 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

2
8
9
105
7

## HINT

N,M<=100000

  1 #include <iostream>
2 #include <iomanip>
3 #include <cstdlib>
4 #include <cstdio>
5 #include <cstring>
6 #include <cmath>
7 #include <algorithm>
8 #include <string>
9 #define RG register
10 const int N = 3000000;
11 const int M = 300000;
12
13 using namespace std;
14
16 int cnt,dis[M],top[M],n,m,a[N],b[N],rt[M],sa[M],w[M],ans;
17
18 struct date{
19     int s,i;
20     bool operator < (const date a) const{
21         return s<a.s;
22     }
23 }f[M];
24
25 int gi(){
26     char ch=getchar();int x=0;
27     while(ch<'0' || ch>'9') ch=getchar();
28     while(ch>='0' && ch<='9') x=x*10+ch-'0',ch=getchar();
29     return x;
30 }
31
32 void dfs1(int xh,int fu){
33     fa[xh]=fu,dis[xh]=dis[fu]+1;
34     for (RG int i=head[xh]; i; i=nn[i][0]){
35         if (nn[i][1]==fu) continue;
36         dfs1(nn[i][1],xh);
37         siz[xh]+=siz[nn[i][1]];
38         if (siz[nn[i][1]]>siz[son[xh]])
39             son[xh]=nn[i][1];
40     }
41     ++siz[xh];
42     return;
43 }
44
45 struct dota{
46     int s,l,r;
47 }tr[N];
48
49 void build(int x,int &t,int l,int r){
50     tr[++ans]=tr[t];t=ans;
51     ++tr[ans].s;
52     if (l==r) return;
53     int mid=(l+r)>>1;
54     if (x<=mid) build(x,tr[ans].l,l,mid);
55     else build(x,tr[ans].r,mid+1,r);
56     return;
57 }
58
59 void dfs2(int xh,int tp){
60     id[xh]=++cnt,top[xh]=tp;
61     rt[cnt]=rt[cnt-1],build(ra[xh],rt[cnt],1,n);
62     if (son[xh]) dfs2(son[xh],tp);
63     for (RG int i=head[xh]; i; i=nn[i][0]){
64         if (nn[i][1]==son[xh] || nn[i][1]==fa[xh]) continue;
65         dfs2(nn[i][1],nn[i][1]);
66     }
67     return;
68 }
69
70 int query(int l,int r,int k){
71     RG int tot=0;
72     while(top[l]!=top[r])
73         if (dis[top[l]]>dis[top[r]]) a[++tot]=rt[id[top[l]]-1],b[tot]=rt[id[l]],l=fa[top[l]];
74         else a[++tot]=rt[id[top[r]]-1],b[tot]=rt[id[r]],r=fa[top[r]];
75     if (dis[l]>dis[r]) a[++tot]=rt[id[r]-1],b[tot]=rt[id[l]];
76     else a[++tot]=rt[id[l]-1],b[tot]=rt[id[r]];
77     RG int z=1,y=n,mid;
78     while(z!=y){
79         RG int a1=0,b1=0;
80         mid=(z+y)>>1;
81         for (RG int i=1; i<=tot; ++i) a1+=tr[tr[a[i]].l].s;
82         for (RG int i=1; i<=tot; ++i) b1+=tr[tr[b[i]].l].s;
83         if (b1-a1>=k){
84             for (RG int i=1; i<=tot; ++i) a[i]=tr[a[i]].l;
85             for (RG int i=1; i<=tot; ++i) b[i]=tr[b[i]].l;
86             y=mid;
87         }
88         else{
89             for (RG int i=1; i<=tot; ++i) a[i]=tr[a[i]].r;
90             for (RG int i=1; i<=tot; ++i) b[i]=tr[b[i]].r;
91             z=mid+1,k-=(b1-a1);
92         }
93     }
94     return z;
95 }
96
97 int main(){
98     n=gi(),m=gi();
99     for (RG int i=1; i<=n; ++i) f[i]=(date){gi(),i},w[i]=f[i].s;
100     sort(f+1,f+n+1);
101     for (RG int i=1; i<=n; ++i) ra[f[i].i]=i,sa[i]=f[i].i;
102     for (RG int i=1; i<n;  ++i){
103         RG int l=gi(),r=gi();
106     }
107     cnt=0;
108     dfs1(1,0),dfs2(1,1);
109     int jl=0;
110     for (RG int i=1; i<m; ++i){
111         RG int l=gi()^jl,r=gi(),k=gi();
112         jl=query(l,r,k);
113         printf("%d\n",w[sa[jl]]);
114         jl=w[sa[jl]];
115     }
116     RG int l=gi()^jl,r=gi(),k=gi();
117     jl=query(l,r,k);
118     printf("%d",w[sa[jl]]);
119     jl=w[sa[jl]];
120     return 0;
121 }

posted @ 2017-02-28 11:30  Cjk_2001  阅读(117)  评论(0编辑  收藏  举报