hdu_3804_Query on a tree(树链剖分)
题目连接: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(树链剖分)的更多相关文章
- Hdu 5274 Dylans loves tree (树链剖分模板)
Hdu 5274 Dylans loves tree (树链剖分模板) 题目传送门 #include <queue> #include <cmath> #include < ...
- POJ3237 Tree 树链剖分 边权
POJ3237 Tree 树链剖分 边权 传送门:http://poj.org/problem?id=3237 题意: n个点的,n-1条边 修改单边边权 将a->b的边权取反 查询a-> ...
- Query on a tree——树链剖分整理
树链剖分整理 树链剖分就是把树拆成一系列链,然后用数据结构对链进行维护. 通常的剖分方法是轻重链剖分,所谓轻重链就是对于节点u的所有子结点v,size[v]最大的v与u的边是重边,其它边是轻边,其中s ...
- 【BZOJ-4353】Play with tree 树链剖分
4353: Play with tree Time Limit: 20 Sec Memory Limit: 256 MBSubmit: 31 Solved: 19[Submit][Status][ ...
- SPOJ Query on a tree 树链剖分 水题
You are given a tree (an acyclic undirected connected graph) with N nodes, and edges numbered 1, 2, ...
- poj 3237 Tree 树链剖分
题目链接:http://poj.org/problem?id=3237 You are given a tree with N nodes. The tree’s nodes are numbered ...
- Codeforces Round #200 (Div. 1) D Water Tree 树链剖分 or dfs序
Water Tree 给出一棵树,有三种操作: 1 x:把以x为子树的节点全部置为1 2 x:把x以及他的所有祖先全部置为0 3 x:询问节点x的值 分析: 昨晚看完题,马上想到直接树链剖分,在记录时 ...
- poj 3237 Tree 树链剖分+线段树
Description You are given a tree with N nodes. The tree’s nodes are numbered 1 through N and its edg ...
- 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 ...
随机推荐
- linux 文件和目录操作
1 相对路径和绝对路径 (与C++中概念相似) cd /root/cat 绝对路径 cd ../文件名/ 相对路径,指相对某个文件而言的 2 切换目录 cd 当前目录 进入某个目录 pwd 显示当前 ...
- [Rails] 从 Request 到 Response(2)
本文翻译自:Rails from Request to Response 系列:个人选择了自己感兴趣的部分进行翻译,需要阅读原文的同学请戳前面的链接. 第二部分 路由(Routing) Blog::A ...
- 完全删除Postgresql
First: If your install isn't already damaged, you can drop unwanted PostgreSQL servers ("cluste ...
- gerrit的merge conflict
找了很多资料,最后参考http://blog.csdn.net/w_jewelry/article/details/8123639 解决的. 先把gerrit的那几个commit abandon掉. ...
- openstack私有云布署实践【4.1 上层代理haproxy配置 (科兴环境)】
官方文档上的高可用配置,它推荐的是使用haproxy的上层代理来实现服务组件的主备访问.或者负载均衡访问 一开始我也是使用haproxy来做的,但后来方式改了 测试环境:haproxy + n ...
- ACdream 1083 人民城管爱人民
拓扑排序,然后从终点开始递推. #pragma comment(linker, "/STACK:1024000000,1024000000") #include<cstdio ...
- linux下如何使用vnstat查看服务器带宽流量统计
因为很多vps或者服务器都是限流量的,但是又很多服务商并没有提供详细的流量表,比如每天的流量表,所以肯定有人很想知道自己服务器到底跑了多少流量. vnstat就是一个很好用的服务器流量统计命令.我截几 ...
- web page diagnostics
1.概念说明: DNS解析时间:显示使用最近的DNS服务器将DNS名称解析为IP地址所需的时间:DNS查找度量是指示DNS解析问题或DNS服务器问题的一个很好的指示器: Connect时间:显示与 ...
- git的使用,将本地项目push到github上
Git教程(推荐): http://www.liaoxuefeng.com/wiki/0013739516305929606dd18361248578c67b8067c8c017b000git是先用a ...
- iOS中的base64加密
#import <UIKit/UIKit.h> @interface Base64String : NSObject + (NSString *)base64String:(NSStrin ...