标签:io os ar for 数据 sp on amp ad
#pragma comment(linker, "/STACK:1024000000,1024000000") #include<iostream> #include<cstring> #include<algorithm> #define lson id << 1 #define rson id << 1|1 #include<cstdio> using namespace std; const int M = 100008; int father[M],dep[M],top[M],siz[M],ti[M],son[M]; int idx,tp = 0; const int inf = 0x3ffff; struct line_tree{ int l,r,Max,Min,mark; int mid(){ return (l+r)/2; } }node[M*4]; struct { int head; }H[M]; struct{ int v,next; }E[M*4]; void add(int u,int v){ E[tp].v = v; E[tp].next = H[u].head; H[u].head = tp++; } void init(){ memset(E,-1,sizeof(E)); memset(H,-1,sizeof(H)); tp = 0;idx = 0; } void dfs_1(int u,int fa){ son[u] = 0;siz[u] = 1;dep[u] = dep[fa] + 1; father[u] = fa; for(int i=H[u].head;i!=-1;i=E[i].next){ int v = E[i].v; if(v == fa)continue; dfs_1(v,u); siz[u] += siz[v]; if(siz[v] > siz[son[u]]) son[u] = v; } } void dfs_2(int u,int fa){ top[u] = fa; ti[u] = idx++; if(son[u])dfs_2(son[u],fa); for(int i=H[u].head;i!=-1;i=E[i].next){ int v = E[i].v; if(v == father[u]||v == son[u])continue; dfs_2(v,v); } } /* 线段树*/ void build_tree(int id,int l,int r){ node[id].l = l;node[id].r = r; node[id].Max = -1; if(l == r)return; int mid = node[id].mid(); build_tree(lson,l,mid); build_tree(rson,mid+1,r); } void push_up(int id){ node[id].Max = max(node[lson].Max,node[rson].Max); } void update(int id,int k,int w){ if(node[id].l == k&&node[id].r == k){ node[id].Max = w; return; } int mid = node[id].mid(); if(k <= mid)update(lson,k,w); else update(rson,k,w); push_up(id); } int query(int id,int l,int r){ if( node[id].l == l&&node[id].r == r)return node[id].Max; int mid= node[id].mid(); if(r <=mid)return query(lson,l,r); else if(l > mid)return query(rson,l,r); else return max( query(lson,l,mid),query(rson,mid+1,r)); push_up(id); } int findMax(int u,int v){ int f1 = top[u]; int f2 = top[v]; int mmax = -1; while(f1 !=f2){ if(dep[f1] < dep[f2]){ swap(f1,f2); swap(u,v); } mmax = max(mmax,query(1,ti[f1],ti[u])); u = father[f1];f1 = top[u]; } if(u == v)return mmax; if(dep[u] > dep[v])swap(u,v); mmax = max(mmax,query(1,ti[son[u]],ti[v])); return mmax; } struct Node{ int x,y,val; }e[M]; struct xx{ int x,y,i,d; }h[M]; bool cmp(xx a,xx b){ return a.y < b.y; } bool cmp1(Node a,Node b){ return a.val < b.val; } bool cmp2(xx a,xx b){ return a.i < b.i; } int main(){ //freopen("input.txt","r",stdin); int n,m; int T; scanf("%d",&T); while(T--){ init(); scanf("%d",&n); for(int i=0;i<n-1;i++){ scanf("%d%d%d",&e[i].x,&e[i].y,&e[i].val); add(e[i].x,e[i].y); add(e[i].y,e[i].x); } dfs_1(1,1); dfs_2(1,1); build_tree(1,1,idx-1); scanf("%d",&m); for(int i=0;i<m;i++){ scanf("%d%d",&h[i].x,&h[i].y); h[i].i = i; } sort(e,e+n-1,cmp1); sort(h,h+m,cmp); int t = 0,x = 0;; while(t < m){ while(h[t].y >= e[x].val &&x < n-1 ){ if(dep[e[x].x] > dep[e[x].y]) swap(e[x].x,e[x].y); update(1,ti[e[x].y],e[x].val); x++; } h[t].d = findMax(h[t].x,1); t++; } sort(h,h+m,cmp2); for(int i=0;i<m;i++){ printf("%d\n",h[i].d); } } }
标签:io os ar for 数据 sp on amp ad
原文地址:http://blog.csdn.net/wyl_zheyang/article/details/39935421