hdu 3804 树链剖分

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

#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*3];
struct Tree{
    int l,r,c;
    int *p;
    int mid(){
        return (l+r)>>1;
    }
}tree[Maxn*3];
void init()
{
    clr(head,-1);clr(vi,0);
    e=0;id=0;
    for(int i=0;i<Maxn*3;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=0;
    tree[po].p=new int[r-l+2];
    if(l==r)
        return ;
    int mid=tree[po].mid();
    BuildTree(l,mid,lson(po));
    BuildTree(mid+1,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+1,tree[po].p+tree[po].c+1,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>1){
            return tree[po].p[pos-1];
        }
        return -1;
    }
    int mid=tree[po].mid();
    if(r<=mid)
        return getans(l,r,val,lson(po));
    else if(l>=mid+1)
        return getans(l,r,val,rson(po));
    else{
        return max(getans(l,mid,val,lson(po)),getans(mid+1,r,val,rson(po)));
    }
}
void dfs(int u,int val)
{
    vi[u]=1;
    int i,v;
    son[u]=0,sz[u]=1;
    num[u]=val;
    for(i=head[u];i!=-1;i=edge[i].next){
        v=edge[i].v;
        if(vi[v]) continue;
        dep[v]=dep[u]+1;
        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]=1;
    lis[id].i=id,lis[id].val=num[u];
    if(son[u]) build(son[u],ti);
    for(i=head[u];i!=-1;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=-10000;
    while(f1!=1){
        ans=max(ans,getans(w[f1],w[u],c,1));
        u=fa[f1];f1=top[u];
    }
    //cout<<w[f1]<<" "<<w[u]<<endl;
    ans=max(ans,getans(w[f1],w[u],c,1));
    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=1;i<n;i++){
            scanf("%d%d%d",&u,&v,&val);
            add(u,v,val);
            add(v,u,val);
        }
        dfs(1,1200000000);
        memset(vi,0,sizeof(vi));
        build(1,1);
        sort(lis+1,lis+id+1);
        BuildTree(1,n,1);
        for(i=1;i<=n;i++){
            update(lis[i].i,lis[i].val,1);
        }
        scanf("%d",&q);
        for(i=1;i<=q;i++){
            scanf("%d%d",&u,&v);
            calc(u,v);
        }
    }
    return 0;
}

 

posted @ 2013-09-14 11:28  fangguo  阅读(211)  评论(0编辑  收藏  举报