题目连接: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. NS_ASSUME_NONNULL_BEGIN 延伸

    NS_ASSUME_NONNULL_BEGIN和NS_ASSUME_NONNULL_END 在.h文件中,可以看到这两个宏,翻看定义,这两个宏的代码是 #define NS_ASSUME_NONNUL ...

  2. php系统无法上传图片问题

    PHP Warning:  File upload error - unable to create a temporary file in Unknown on line 0   我的测试环境是 w ...

  3. python 元组 字符串 字典 列表嵌套练习题1

    最近学习做的习题,直接复制过来 缩进就乱掉了,所以直接以图片方式上传,题目和答案一起

  4. 整体认识flume:Flume介绍、分布式安装、常见问题及解决方案

    问题导读 1.什么是flume? 2.flume包含哪些组件? 3.Flume在读取utf-8格式的文件时会出现解析不了时间戳,该如何解决? Flume是一个分布式.可靠.和高可用的海量日志采集.聚合 ...

  5. Java JVM 类的连接与初始化 [ 转载 ]

    Java类的连接与初始化 (及2013阿里初始化笔试题解析)  转自http://www.cnblogs.com/iceAeterNa/p/4876747.html         Java虚拟机通过 ...

  6. IOS 中会发生crash的操作

    对字典和数组进行下列操作时会产生crash: 对于字典来说: 查询时,key=nil 或者 key=null 时都能正常运行 插入时,,key=nil 或者 key=null 都会crash 对于数组 ...

  7. Struts文件上传

    首先要加入上传文件需要的jar文件 commons-fileupload-1.2.1.jar commomons-io-1.3.2.jar不同版本的strutsjar文件的版本也可能不同,一般在配置s ...

  8. strrchr

    strrchr() 函数查找字符在指定字符串中从正面开始的最后一次出现的位置,如果成功,则返回从该位置到字符串结尾的所有字符,如果失败,则返回 false.与之相对应的是strchr()函数,它查找字 ...

  9. 【转】Spring事务超时时间可能存在的错误认识

    1.先看代码 1.1.spring-config.xml <bean id="dataSource" class="org.springframework.jdbc ...

  10. 把对象转换成map

    public static Map toMap(Object object){ Map _result = new CaseInsensitiveMap(); if (object != null) ...