http://acm.hdu.edu.cn/showproblem.php?pid=4366

  1 #include <cstdio>
  2 #include <cstring>
  3 #include <algorithm>
  4 using namespace std;
  5 #define lson l,m,rt<<1
  6 #define rson m+1,r,rt<<1|1
  7 #define maxn 50005
  8 struct node{
  9     int num,id;
 10 }setree[maxn<<2];
 11 struct {
 12     int l,r;
 13 }mes[maxn];
 14 struct{
 15     int y,next;
 16 }ee[maxn<<1];
 17 struct op{
 18     int id,abl,loy,preid;
 19 }mess[maxn];
 20 int t,cnt,link[maxn],ans[maxn],id,maxnum;
 21 struct opp{
 22     int num,id,a;
 23 }tmp[maxn];
 24 void insert(int a,int b)
 25 {
 26     ee[++t].y=b;
 27     ee[t].next=link[a];
 28     link[a]=t;
 29 }
 30 void dfs(int root,int father)
 31 {
 32     if(link[root]==0){
 33         mes[root].l=mes[root].r=cnt;
 34         mess[root].id=cnt++;
 35         return;
 36     }
 37     mes[root].l=cnt;
 38     for(int i=link[root];i;i=ee[i].next)
 39     if(ee[i].y!=father)
 40     dfs(ee[i].y,root);
 41     mes[root].r=cnt;
 42     mess[root].id=cnt++;
 43 }
 44 bool cmp(struct opp a,struct opp b)
 45 {
 46     return a.num>b.num;
 47 }
 48 bool cmp1(struct op a,struct op b)
 49 {
 50     return a.abl<b.abl;
 51 }
 52 void build(int l,int r,int rt)
 53 {
 54     setree[rt].num=-1;
 55     setree[rt].id=-1;
 56     if(l==r)
 57     return;
 58     int m=(l+r)>>1;
 59     build(lson);
 60     build(rson);
 61 }
 62 void pushup(int rt)
 63 {
 64     if(setree[rt<<1].num>setree[rt<<1|1].num){
 65         setree[rt].num=setree[rt<<1].num;
 66         setree[rt].id=setree[rt<<1].id;
 67     }
 68     else{
 69         setree[rt].num=setree[rt<<1|1].num;
 70         setree[rt].id=setree[rt<<1|1].id;
 71     }
 72 }
 73 void update(int l,int r,int rt,int num,int c,int id)
 74 {
 75     if(l==r){
 76             setree[rt].num=c;
 77             setree[rt].id=id;
 78         return;
 79     }
 80     int m=(l+r)>>1;
 81     if(num<=m)
 82     update(lson,num,c,id);
 83     else
 84     update(rson,num,c,id);
 85     pushup(rt);
 86 }
 87 int query(int l,int r,int rt,int L,int R)
 88 {
 89     if(L<=l&&r<=R){
 90         if(maxnum<setree[rt].num){
 91             id=setree[rt].id;
 92             maxnum=setree[rt].num;
 93         }
 94         return setree[rt].num;
 95     }
 96     int m=(l+r)>>1;
 97     int ans=-1;
 98     if(L<=m)
 99     ans=max(ans,query(lson,L,R));
100     if(R>m)
101     ans=max(ans,query(rson,L,R));
102     return ans;
103 }
104 int main()
105 {
106     int tt;
107     scanf("%d",&tt);
108     while(tt--){
109         int n,m;
110         cnt=t=0;
111         memset(link,0,sizeof(link));
112         scanf("%d%d",&n,&m);
113         for(int i=1;i<n;i++){
114             int a;
115             scanf("%d%d%d",&a,&mess[i].loy,&mess[i].abl);
116             mess[i].preid=i;
117             insert(i,a);
118             insert(a,i);
119         }
120         dfs(0,0);
121         
122         for(int i=0;i<m;i++){
123             scanf("%d",&tmp[i].a);
124             tmp[i].id=i;
125             tmp[i].num=mess[tmp[i].a].abl;
126         }
127         sort(mess+1,mess+n,cmp1);
128         sort(tmp,tmp+m,cmp);
129         build(0,n-1,1);
130         int kk=n-1;
131         for(int i=0;i<m;i++){
132             maxnum=-1;
133             while(mess[kk].abl>tmp[i].num){
134                 update(0,n-1,1,mess[kk].id,mess[kk].loy,mess[kk].preid);
135                 kk--;
136             }
137             int res=query(0,n-1,1,mes[tmp[i].a].l,mes[tmp[i].a].r);
138             if(res==-1)
139             ans[tmp[i].id]=-1;
140             else
141             ans[tmp[i].id]=id;
142         }
143         for(int i=0;i<m;i++)
144         printf("%d\n",ans[i]);
145     }
146     return 0;
147 }
AC Code