题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=3804

题意:给你一棵树,然后给出树上边的价值,然后给出x,y,问从1到x的边上不超过y的最大值为多少

题解:这题如果直接写裸线段树来在线更新会T飞,正解是把边的值存下来,把询问也存下来,然后都按照价值从小到大排序,然后从最小的开始找,找之前把边的值插进线段树,这样就成了查询区间最大值了,很巧妙的方法。还有就是这题的内存卡的很紧,用前向星爆内存了,我没想通,用vector就过了。很尴尬

 #include<cstdio>
#include<vector>
#include<algorithm>
#define F(i,a,b) for(int i=a;i<=b;++i)
#define root 1,n,1
#define ls l,m,rt<<1
#define rs m+1,r,rt<<1|1
using namespace std;
const int N=1e5+;
vector<int>g[N];
struct vs{
int val,u,v;
bool operator<(const vs &b)const{return val<b.val;}
}va[N];
struct ser{
int x,k,id;
bool operator<(const ser &b)const{return k<b.k;}
}ask[N];
int t,n,m,x,y,c,tot,ans[N],sum[N*],sz[N],fa[N],dep[N],hs[N],top[N],tid[N],idx;
//线段树部分
void update(int x,int k,int l,int r,int rt){
if(l==r){sum[rt]=k;return;}
int m=(l+r)>>;
if(x<=m)update(x,k,ls);
else update(x,k,rs);
sum[rt]=max(sum[rt<<],sum[rt<<|]);
} int query(int L,int R,int l,int r,int rt){
if(L<=l&&r<=R)return sum[rt];
int m=(l+r)>>,maxs=;
if(L<=m)maxs=max(query(L,R,ls),maxs);
if(m<R)maxs=max(query(L,R,rs),maxs);
return maxs;
}
//树链部分
void dfs1(int u,int pre){
sz[u]=,fa[u]=pre,dep[u]=dep[pre]+,hs[u]=;
for(int i=;i<g[u].size();i++){
int vv=g[u][i];
if(vv!=pre){
dfs1(vv,u);
if(sz[vv]>sz[hs[u]])hs[u]=vv;
sz[u]+=sz[vv];
}
}
} void dfs2(int u,int tp){
tid[u]=++idx,top[u]=tp;
if(hs[u])dfs2(hs[u],tp);
for(int i=;i<g[u].size();i++){
int vv=g[u][i];
if(vv!=fa[u]&&vv!=hs[u])dfs2(vv,vv);
}
} int findmax(int x){
int fx=top[x],an=;
while(fx!=)an=max(query(tid[fx],tid[x],root),an),x=fa[fx],fx=top[x];
if(x==)return an;
return max(query(,tid[x],root),an);
} int main(){
scanf("%d",&t);
while(t--){
scanf("%d",&n);
F(i,,n)g[i].clear();
dep[]=,sz[]=;
F(i,,*N-)sum[i]=;
F(i,,n-)scanf("%d%d%d",&va[i].u,&va[i].v,&va[i].val),g[va[i].u].push_back(va[i].v),g[va[i].v].push_back(va[i].u);
dfs1(,),idx=,dfs2(,);
scanf("%d",&m),tot=;
F(i,,m)scanf("%d%d",&ask[i].x,&ask[i].k),ask[i].id=i;
sort(va+,va+n),sort(ask+,ask++m);
F(i,,m){
while(tot<n&&va[tot].val<=ask[i].k){
if(dep[va[tot].u]>dep[va[tot].v])c=va[tot].u;else c=va[tot].v;
update(tid[c],va[tot].val,root),tot++;
}
ans[ask[i].id]=findmax(ask[i].x);
}
F(i,,m)printf("%d\n",ans[i]==?-:ans[i]);
}
return ;
}

hdu_3804_Query on a tree(树链剖分)的更多相关文章

  1. Hdu 5274 Dylans loves tree (树链剖分模板)

    Hdu 5274 Dylans loves tree (树链剖分模板) 题目传送门 #include <queue> #include <cmath> #include < ...

  2. POJ3237 Tree 树链剖分 边权

    POJ3237 Tree 树链剖分 边权 传送门:http://poj.org/problem?id=3237 题意: n个点的,n-1条边 修改单边边权 将a->b的边权取反 查询a-> ...

  3. Query on a tree——树链剖分整理

    树链剖分整理 树链剖分就是把树拆成一系列链,然后用数据结构对链进行维护. 通常的剖分方法是轻重链剖分,所谓轻重链就是对于节点u的所有子结点v,size[v]最大的v与u的边是重边,其它边是轻边,其中s ...

  4. 【BZOJ-4353】Play with tree 树链剖分

    4353: Play with tree Time Limit: 20 Sec  Memory Limit: 256 MBSubmit: 31  Solved: 19[Submit][Status][ ...

  5. SPOJ Query on a tree 树链剖分 水题

    You are given a tree (an acyclic undirected connected graph) with N nodes, and edges numbered 1, 2, ...

  6. poj 3237 Tree 树链剖分

    题目链接:http://poj.org/problem?id=3237 You are given a tree with N nodes. The tree’s nodes are numbered ...

  7. Codeforces Round #200 (Div. 1) D Water Tree 树链剖分 or dfs序

    Water Tree 给出一棵树,有三种操作: 1 x:把以x为子树的节点全部置为1 2 x:把x以及他的所有祖先全部置为0 3 x:询问节点x的值 分析: 昨晚看完题,马上想到直接树链剖分,在记录时 ...

  8. poj 3237 Tree 树链剖分+线段树

    Description You are given a tree with N nodes. The tree’s nodes are numbered 1 through N and its edg ...

  9. Aizu 2450 Do use segment tree 树链剖分+线段树

    Do use segment tree Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://www.bnuoj.com/v3/problem_show ...

随机推荐

  1. 【c++】size_t 和 size_type的区别

    为了使自己的程序有很好的移植性,c++程序员应该尽量使用size_t和size_type而不是int, unsigned 1. size_t是全局定义的类型:size_type是STL类中定义的类型属 ...

  2. Linux系统VPS/服务器安装WINDOWS桌面环境可以采用的几个方法

    我们公司的几个项目需要在WINDOWS桌面类型的界面操作,哪怕仅有一个浏览器远程操作也是可以的,我们运维部门得到的任务就是需要能在已有的Linux系统的VPS.服务器环境中能够远程操作,至少需要能可以 ...

  3. String.getBytes()--->字符串转字节数组

    是String的一个方法String的getBytes()方法是得到一个系统默认的编码格式的字节数组getBytes("utf-8") 得到一个UTF-8格式的字节数组把Strin ...

  4. js--闭包的理解

    从技术上来讲,在JS中,每个function都是闭包,因为它总是能访问在它外部定义的数据. 当该内部函数在外部函数外被调用,就生成了闭包. 函数内部可以直接读取全局变量. 闭包就是能够读取其他函数内部 ...

  5. firebug的调试,console

    console.log() console.warn()  警告 console.error()  错误 console.group()  分组 console.grounpEnd() 分组结束 co ...

  6. html5权威指南:客户端分区响应图

    客户端分区响应图:http://www.cnblogs.com/enjoymylift/p/5984338.html

  7. ZUFEOJ 2395 天棋哥哥大战AlphGo

    Description3月15日,人机围棋大战巅峰对决在韩国首尔落下帷幕.五番棋的最后一局中,韩国著名棋手李世乭尽管与人工智能“AlphaGo”缠斗至官子阶段,但在双双进入读秒后最终还是投子认输,以总 ...

  8. spring注解支持

    Spring基于注解实现Bean定义支持如下三种注解: Spring自带的@Component注解及扩展@Repository.@Service.@Controller JSR-250 1.1版本中中 ...

  9. 【LeetCode】2.Add Two Numbers

    首先想到的是走到其中一个链表的尽头,然后把剩余的链表中的值放入写的链表,返回,但是自己写的代码好长. struct ListNode* addTwoNumbers(struct ListNode* l ...

  10. OSCache 使用

    引入OSCache的jar包 package com.sun.utils; import java.util.Date; import com.opensymphony.oscache.base.Ne ...