标签:
1 3 2 0 100 99 1 101 100 1 2
2 -1
1 #include <iostream> 2 #include <vector> 3 #include <cstdio> 4 #include <algorithm> 5 #include <cstring> 6 #pragma comment(linker, "/STACK:102400000,102400000") 7 using namespace std; 8 const int maxn = 200010; 9 struct node { 10 int maxv,id; 11 } tree[maxn<<2]; 12 void update(int L,int R,int lt,int rt,int val,int id,int v) { 13 if(lt <= L && rt >= R) { 14 tree[v].maxv = val; 15 tree[v].id = id; 16 return ; 17 } 18 int mid = (L + R)>>1; 19 if(lt <= mid) update(L,mid,lt,rt,val,id,v<<1); 20 if(rt > mid) update(mid+1,R,lt,rt,val,id,v<<1|1); 21 if(tree[v<<1].maxv > tree[v<<1|1].maxv) 22 tree[v] = tree[v<<1]; 23 else tree[v] = tree[v<<1|1]; 24 } 25 node query(int L,int R,int lt,int rt,int v) { 26 if(lt <= L && rt >= R) return tree[v]; 27 int mid = (L + R)>>1; 28 node a,b; 29 a.maxv = -1,b.maxv = -1; 30 a.id = -1,b.id = -1; 31 if(lt <= mid) a = query(L,mid,lt,rt,v<<1); 32 if(rt > mid) b = query(mid+1,R,lt,rt,v<<1|1); 33 if(a.maxv > b.maxv) return a; 34 return b; 35 } 36 vector<int>g[maxn]; 37 int L[maxn],R[maxn],ret[maxn],tt; 38 void dfs(int u) { 39 L[u] = tt++; 40 for(int i = g[u].size()-1; i >= 0; --i) 41 dfs(g[u][i]); 42 R[u] = tt-1; 43 } 44 struct STAFF { 45 int id,ability,loyalty; 46 bool operator<(const STAFF &t)const { 47 if(ability == t.ability) return id < t.id; 48 return ability > t.ability; 49 } 50 } staff[maxn]; 51 int main() { 52 int kase,n,m,fa; 53 scanf("%d",&kase); 54 while(kase--) { 55 scanf("%d%d",&n,&m); 56 for(int i = tt = 0; i <= n; ++i) g[i].clear(); 57 for(int i = 1; i < n; ++i) { 58 scanf("%d%d%d",&fa,&staff[i-1].loyalty,&staff[i-1].ability); 59 g[fa].push_back(i); 60 staff[i-1].id = i; 61 } 62 dfs(0); 63 sort(staff,staff+n-1); 64 memset(tree,-1,sizeof tree); 65 for(int i = 0; i < n-1; ++i) { 66 node now = query(0,R[0],L[staff[i].id],R[staff[i].id],1); 67 ret[staff[i].id] = now.id; 68 update(0,R[0],L[staff[i].id],L[staff[i].id],staff[i].loyalty,staff[i].id,1); 69 } 70 while(m--) { 71 scanf("%d",&fa); 72 printf("%d\n",ret[fa]); 73 } 74 } 75 return 0; 76 }
标签:
原文地址:http://www.cnblogs.com/crackpotisback/p/4694603.html