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

#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 ;
}

hdu 3804 树链剖分的更多相关文章

  1. hdu 3804树链剖分+离线操作

    /* 树链刨分+离线操作 题意:给你一棵树,和询问x,y 从节点x--节点1的小于等于y的最大值. 解:先建一个空树,将树的边权值从小到大排序,将询问y按从小到大排序 对于每次询问y将小于等于y的边权 ...

  2. hdu 5893 (树链剖分+合并)

    List wants to travel Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/O ...

  3. hdu 5052 树链剖分

    Yaoge’s maximum profit Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/ ...

  4. hdu 4897 树链剖分(重轻链)

    Little Devil I Time Limit: 16000/8000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others ...

  5. hdu 5274 树链剖分

    Dylans loves tree Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Othe ...

  6. HDU 3966 (树链剖分+线段树)

    Problem Aragorn's Story (HDU 3966) 题目大意 给定一颗树,有点权. 要求支持两种操作,将一条路径上的所有点权值增加或减少ai,询问某点的权值. 解题分析 树链剖分模板 ...

  7. hdu 3966(树链剖分+线段树区间更新)

    传送门:Problem 3966 https://www.cnblogs.com/violet-acmer/p/9711441.html 学习资料: [1]线段树区间更新:https://blog.c ...

  8. HDU 3966 /// 树链剖分+树状数组

    题意: http://acm.hdu.edu.cn/showproblem.php?pid=3966 给一棵树,并给定各个点权的值,然后有3种操作: I x y z : 把x到y的路径上的所有点权值加 ...

  9. hdu 4729 树链剖分

    思路:这个树链剖分其实还是比较明显的.将边按权值排序后插入线段树,然后用线段树查找区间中比某个数小的数和,以及这样的数的个数.当A<=B时,就全部建新的管子. 对于A>B的情况比较 建一条 ...

随机推荐

  1. GetWindowThreadProcessId用法(转)

    函数功能:该函数返回创建指定窗口线程的标识和创建窗口的进程的标识符,后一项是可选的. 函数原型:DWORD GetWindowThreadProcessld(HWND hwnd,LPDWORD lpd ...

  2. jdk各个班本官网下载地址

    http://www.oracle.com/technetwork/java/archive-139210.html

  3. Java常见排序算法之归并排序

    在学习算法的过程中,我们难免会接触很多和排序相关的算法.总而言之,对于任何编程人员来说,基本的排序算法是必须要掌握的. 从今天开始,我们将要进行基本的排序算法的讲解.Are you ready?Let ...

  4. CSDN问答频道“华章杯”7月排行榜活动开始,丰厚奖品等你拿

    CSDN问答频道月度排行榜,是CSDN问答频道从3月开始举办的活动,旨在鼓励更多用户参与提问和解答,创造一个良好的互帮互助氛围,使参与者在问和答的过程中得到技术水平的提升,也希望大家能在技术交流中结交 ...

  5. c++学习笔记(1)

    流操纵符std::endl输出一个换行符,然后刷新输出缓冲,在一些系统中,输出在机器中缓存,直至积累到“值得”输出到屏幕上为止,而std::endl强制显示所有积存的输出。 c++的一个强大的功能是用 ...

  6. 11个新鲜出炉的jQuery图像滑块插件

    如今图像滑块已成为一种流行的Web设计元素,通过滑块,你可以在有限的页面空间中展示更多的内容,带给用户更佳的浏览体验.本文将为你带来一些非常实用的jQuery图像滑块插件. 1.  Basic Sli ...

  7. Codeforces Educational Codeforces Round 5 A. Comparing Two Long Integers 高精度比大小,模拟

    A. Comparing Two Long Integers 题目连接: http://www.codeforces.com/contest/616/problem/A Description You ...

  8. Yeoman+Express+Angular在Linux上开发配置方法

    $mkdir ExpressWithAngularTest $cd ExpressWithAngularTest choose needed components you'd like to add ...

  9. Java中怎样由枚举常量的ordinal值获得枚举常量对象

    Java1.5提供了关键字enum,能够通过该关键字方便得定义自己须要的枚举类型,比方 enum Season { SPRING, SUMMER, AUTUMN, WINTER } 就定义了一个季节枚 ...

  10. Laravel 5.0 之命令及处理程序

    本文译自 Matt Stauffer 的 系列文章 . 本文中涉及的新功能都是关于 Commands 的,这些特性在 Laravel 旧版本中已经有了,但是在 Laravel 5.0 中变得更加好用了 ...