hdu 3804 树链剖分

时间:2022-12-20 03:55:12

思路:将边权排序,然后插入线段树,这样就可以直接用二分查找确定答案。

#pragma comment(linker, "/STACK:1024000000,1024000000")
#include<set>
#include<map>
#include<cmath>
#include<queue>
#include<cstdio>
#include<vector>
#include<string>
#include<cstdlib>
#include<cstring>
#include<iostream>
#include<algorithm>
#define pb push_back
#define mp make_pair
#define Maxn 100010
#define Maxm 200010
#define LL __int64
#define Abs(x) ((x)>0?(x):(-x))
#define lson(x) (x<<1)
#define rson(x) (x<<1|1)
#define inf 100000
#define lowbit(x) (x&(-x))
#define clr(x,y) memset(x,y,sizeof(x))
#define Mod 1000000007
using namespace std;
int head[Maxn],vi[Maxn],dep[Maxn],w[Maxn],top[Maxn],son[Maxn],sz[Maxn],fa[Maxn],e,id;
int val[Maxn];
int num[Maxn];
struct Point{
int val,i;
int operator <(const Point &temp) const{
return val<temp.val;
}
}lis[Maxn];
struct Edge{
int u,v,next,val;
}edge[Maxn*];
struct Tree{
int l,r,c;
int *p;
int mid(){
return (l+r)>>;
}
}tree[Maxn*];
void init()
{
clr(head,-);clr(vi,);
e=;id=;
for(int i=;i<Maxn*;i++){
delete [] tree[i].p;
}
}
void add(int u,int v,int val)
{
edge[e].u=u,edge[e].v=v,edge[e].val=val,edge[e].next=head[u],head[u]=e++;
}
void BuildTree(int l,int r,int po)
{
tree[po].l=l,tree[po].r=r,tree[po].c=;
tree[po].p=new int[r-l+];
if(l==r)
return ;
int mid=tree[po].mid();
BuildTree(l,mid,lson(po));
BuildTree(mid+,r,rson(po));
}
void update(int i,int c,int po)
{
if(tree[po].l==tree[po].r){
tree[po].p[++tree[po].c]=c;
return ;
}
tree[po].p[++tree[po].c]=c;
int mid=tree[po].mid();
if(i<=mid)
update(i,c,lson(po));
else
update(i,c,rson(po));
}
int getans(int l,int r,int val,int po)
{
if(l<=tree[po].l&&tree[po].r<=r){
int pos=lower_bound(tree[po].p+,tree[po].p+tree[po].c+,val)-tree[po].p;
// cout<<tree[po].l<<" "<<tree[po].r<<" "<<l<<" "<<r
if(pos>tree[po].c)
return tree[po].p[tree[po].c];
if(tree[po].p[pos]==val)
return val;
if(pos>){
return tree[po].p[pos-];
}
return -;
}
int mid=tree[po].mid();
if(r<=mid)
return getans(l,r,val,lson(po));
else if(l>=mid+)
return getans(l,r,val,rson(po));
else{
return max(getans(l,mid,val,lson(po)),getans(mid+,r,val,rson(po)));
}
}
void dfs(int u,int val)
{
vi[u]=;
int i,v;
son[u]=,sz[u]=;
num[u]=val;
for(i=head[u];i!=-;i=edge[i].next){
v=edge[i].v;
if(vi[v]) continue;
dep[v]=dep[u]+;
fa[v]=u;
dfs(v,edge[i].val);
if(sz[v]>sz[son[u]])son[u]=v;
sz[u]+=sz[v];
}
}
void build(int u,int ti)
{
int i,v;
w[u]=++id;top[u]=ti;vi[u]=;
lis[id].i=id,lis[id].val=num[u];
if(son[u]) build(son[u],ti);
for(i=head[u];i!=-;i=edge[i].next){
v=edge[i].v;
if(vi[v]||v==son[u]) continue;
build(v,v);
}
}
void calc(int u,int c)
{
int f1=top[u];
int ans=-;
while(f1!=){
ans=max(ans,getans(w[f1],w[u],c,));
u=fa[f1];f1=top[u];
}
//cout<<w[f1]<<" "<<w[u]<<endl;
ans=max(ans,getans(w[f1],w[u],c,));
printf("%d\n",ans);
return ;
}
int main()
{
int t,n,i,j,u,v,val,q;
scanf("%d",&t);
while(t--){
init();
scanf("%d",&n);
for(i=;i<n;i++){
scanf("%d%d%d",&u,&v,&val);
add(u,v,val);
add(v,u,val);
}
dfs(,);
memset(vi,,sizeof(vi));
build(,);
sort(lis+,lis+id+);
BuildTree(,n,);
for(i=;i<=n;i++){
update(lis[i].i,lis[i].val,);
}
scanf("%d",&q);
for(i=;i<=q;i++){
scanf("%d%d",&u,&v);
calc(u,v);
}
}
return ;
}