http://acm.hdu.edu.cn/showproblem.php?pid=4366
给定后继关系,寻找后继者满足能力值大于被继承者中忠诚值最大的。
先将树形结构变为线性结构,用邻接表+dfs处理。然后对能力值大到小排序,保证查询时已经插入的一定是能力值大于该查询的值。
线段树功能转化为求解区间最值。
本来1Y的代码,结果loy和blt的输入搞反了!试了好多数据还没试出来!郁闷,这两天写的代码好多这种脑残错。。。
code:
#include <cstdlib>
#include <cctype>
#include <cstring>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <vector>
#include <string>
#include <iostream>
#include <sstream>
#include <set>
#include <queue>
#include <stack>
#include <fstream>
#include <iomanip>
#include <bitset>
#include <list>
#include <ctime>
using namespace std ;
#define SET(arr, what) memset(arr, what, sizeof(arr))
#define FF(i, a) for(i=0; i<a; i++)
#define SD(a) scanf("%d", &a)
#define SSD(a, b) scanf("%d%d", &a, &b)
#define SF(a) scanf("%lf", &a)
#define SS(a) scanf("%s", a)
#define SLD(a) scanf("%lld", &a)
#define PF(a) printf("%d\n", a)
#define PPF(a, b) printf("%d %d\n", a, b)
#define SZ(arr) (int)a.size()
#define SWAP(a,b) a=a xor b;b= a xor b;a=a xor b;
#define read freopen("in.txt", "r", stdin)
#define write freopen("out.txt", "w", stdout)
#define MAX 1<<30
#define ESP 1e-5
#define lson l, m, rt<<1
#define rson m+1, r, rt<<1|1
template<class T> inline T sqr(T a){return a*a;}
template<class T> inline void AMin(T &a,T b){if(a==-1||a>b)a=b;}
template<class T> inline void AMax(T &a,T b){if(a<b)a=b;}
template<class T> inline T Min(T a,T b){return a>b?b:a;}
template<class T> inline T Max(T a,T b){return a>b?a:b;}
const int maxn = 55555 ;
int loy[maxn<<2], hash[1000001], head[maxn], k, ans[maxn] ;
int l[maxn], r[maxn] ;
struct node{
int loy, blt, sup ;
}q[maxn] ;
struct edge{
int v, nex ;
}e[maxn] ;
int cmp(const void *a, const void *b){
return (*(node *)a).blt > (*(node *)b).blt ? -1 : 1 ;
}
void addedge(int a,int b){
e[k].v = b ;
e[k].nex = head[a] ;
head[a] = k ;
k ++ ;
}
void dfs(int x){
l[x] = k ++ ;
for(int i=head[x]; i; i=e[i].nex)
dfs(e[i].v) ;
r[x] = k ;
}
int query(int L, int R, int l, int r, int rt){
if(r<l) return -1 ;
if(L<=l&&r<=R){
return loy[rt] ;
}
int m = (l + r) >> 1 ;
int ret = -1 ;
if(L<=m) ret = Max(ret, query(L, R, lson)) ;
if(m<R) ret = Max(ret, query(L, R, rson)) ;
return ret ;
}
void update(int idx, int val, int l, int r, int rt){
if(l==r){
loy[rt] = val ;
return ;
}
int m = (l + r) >> 1 ;
if(idx<=m) update(idx, val, lson) ;
else update(idx, val, rson) ;
loy[rt] = Max(loy[rt<<1], loy[rt<<1|1]) ;
}
int main(){
int t, n, m, i, j ;
SD(t) ;
while(t--){
SET(head, 0) ;
SSD(n, m) ;
k = 1 ;
for(i=1; i<n; i++){
SD(q[i].sup) ;SSD(q[i].loy, q[i].blt) ;
hash[q[i].loy] = i ;
addedge(q[i].sup, i) ;
}
k = 1 ;
dfs(0) ;
SET(loy, -1) ;
qsort(q+1, n-1, sizeof(node), cmp) ;
for(i=1; i<n; i=j){
j = i ;
while(j<n&&q[i].blt==q[j].blt){
int id = hash[q[j].loy] ;
int temp = query(l[id]+1, r[id]-1, 1, k-1, 1) ;
if(temp==-1) ans[id] = -1 ;
else ans[id] = hash[temp] ;
j ++ ;
}
j = i ;
while(j<n&&q[i].blt==q[j].blt){
int id = hash[q[j].loy] ;
update(l[id], q[j].loy, 1, k-1, 1) ;
j ++ ;
}
}
FF(i, m){
SD(j) ;
PF(ans[j]) ;
}
}
return 0 ;
}
#include <cctype>
#include <cstring>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <vector>
#include <string>
#include <iostream>
#include <sstream>
#include <set>
#include <queue>
#include <stack>
#include <fstream>
#include <iomanip>
#include <bitset>
#include <list>
#include <ctime>
using namespace std ;
#define SET(arr, what) memset(arr, what, sizeof(arr))
#define FF(i, a) for(i=0; i<a; i++)
#define SD(a) scanf("%d", &a)
#define SSD(a, b) scanf("%d%d", &a, &b)
#define SF(a) scanf("%lf", &a)
#define SS(a) scanf("%s", a)
#define SLD(a) scanf("%lld", &a)
#define PF(a) printf("%d\n", a)
#define PPF(a, b) printf("%d %d\n", a, b)
#define SZ(arr) (int)a.size()
#define SWAP(a,b) a=a xor b;b= a xor b;a=a xor b;
#define read freopen("in.txt", "r", stdin)
#define write freopen("out.txt", "w", stdout)
#define MAX 1<<30
#define ESP 1e-5
#define lson l, m, rt<<1
#define rson m+1, r, rt<<1|1
template<class T> inline T sqr(T a){return a*a;}
template<class T> inline void AMin(T &a,T b){if(a==-1||a>b)a=b;}
template<class T> inline void AMax(T &a,T b){if(a<b)a=b;}
template<class T> inline T Min(T a,T b){return a>b?b:a;}
template<class T> inline T Max(T a,T b){return a>b?a:b;}
const int maxn = 55555 ;
int loy[maxn<<2], hash[1000001], head[maxn], k, ans[maxn] ;
int l[maxn], r[maxn] ;
struct node{
int loy, blt, sup ;
}q[maxn] ;
struct edge{
int v, nex ;
}e[maxn] ;
int cmp(const void *a, const void *b){
return (*(node *)a).blt > (*(node *)b).blt ? -1 : 1 ;
}
void addedge(int a,int b){
e[k].v = b ;
e[k].nex = head[a] ;
head[a] = k ;
k ++ ;
}
void dfs(int x){
l[x] = k ++ ;
for(int i=head[x]; i; i=e[i].nex)
dfs(e[i].v) ;
r[x] = k ;
}
int query(int L, int R, int l, int r, int rt){
if(r<l) return -1 ;
if(L<=l&&r<=R){
return loy[rt] ;
}
int m = (l + r) >> 1 ;
int ret = -1 ;
if(L<=m) ret = Max(ret, query(L, R, lson)) ;
if(m<R) ret = Max(ret, query(L, R, rson)) ;
return ret ;
}
void update(int idx, int val, int l, int r, int rt){
if(l==r){
loy[rt] = val ;
return ;
}
int m = (l + r) >> 1 ;
if(idx<=m) update(idx, val, lson) ;
else update(idx, val, rson) ;
loy[rt] = Max(loy[rt<<1], loy[rt<<1|1]) ;
}
int main(){
int t, n, m, i, j ;
SD(t) ;
while(t--){
SET(head, 0) ;
SSD(n, m) ;
k = 1 ;
for(i=1; i<n; i++){
SD(q[i].sup) ;SSD(q[i].loy, q[i].blt) ;
hash[q[i].loy] = i ;
addedge(q[i].sup, i) ;
}
k = 1 ;
dfs(0) ;
SET(loy, -1) ;
qsort(q+1, n-1, sizeof(node), cmp) ;
for(i=1; i<n; i=j){
j = i ;
while(j<n&&q[i].blt==q[j].blt){
int id = hash[q[j].loy] ;
int temp = query(l[id]+1, r[id]-1, 1, k-1, 1) ;
if(temp==-1) ans[id] = -1 ;
else ans[id] = hash[temp] ;
j ++ ;
}
j = i ;
while(j<n&&q[i].blt==q[j].blt){
int id = hash[q[j].loy] ;
update(l[id], q[j].loy, 1, k-1, 1) ;
j ++ ;
}
}
FF(i, m){
SD(j) ;
PF(ans[j]) ;
}
}
return 0 ;
}