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 ...
随机推荐
- jdbc_servlet基础增删改分页2(userinfo表的)
1.创建如下文件目录 2.导入MyBatis的jar文件 3.创建实体类 package com.entity; public class Userinfo { private Integer uid ...
- sqlserver2012评估期已过问题处理
于之前安装sqlserver2012忘记输入序列号,现在出现评估期已过的问题,网上忙活半天,才解决,发现网上叙述都很凌乱,而且只有大意,新手很难操作,所以把我操作的过程分享给大家 步骤阅读 百度经 ...
- Hibernate3 第三天
Hibernate3 第三天 第一天:三个准备.七个步骤 第二天:一级缓存.快照.多对多和一对多的配置 学习内容: Hibernate的查询详解(各种检索(fetch)对象的方式) 1)条件查询分类( ...
- ACdream 1015 Double Kings
假设第一个人选的点为P,并且当作根,那么第二个人选的最优情况必然是根p连着的那些点中的一个.然后枚举一下P即可. #pragma comment(linker, "/STACK:102400 ...
- 第五十四节,socketserver通讯模块实现并发操作,真多线程并发
socketserver通讯模块实现并发操作,基于select.epoll.socket.多线程,实现的正真多线程多并发 socketserver通讯模块底层调用的socket模块,只是它作了处理基于 ...
- mysql去重的最方便的两种方法
参考资料:http://blog.csdn.net/guocuifang655/article/details/3993612 方法一: 在使用mysql时,有时需要查询出某个字段不重复的记录,虽然m ...
- 【LeetCode】26. Remove Duplicates from Sorted Array
Given a sorted array, remove the duplicates in place such that each element appear only once and ret ...
- hdu_4417_Super Mario(主席树)
题目链接:hdu_4417_Super Mario 题意: 给你n个树,有m个询问,每个询问有一个区间和一个k,问你这个区间内不大于k的数有多少个. 题解: 考虑用主席树的话就比较裸,当然也可以用其他 ...
- hdu_5832_A water problem(模拟)
题目链接:hdu_5832_A water problem 这是一个惨痛的教训,想这种这么大的大数肯定就是找个规律模拟一下. 然而我们队还写JAVA,用大数艹了13发罚时,真是TM智障了. #incl ...
- Python之Socket&异常处理
Socket Socket用于描述IP地址和端口号,每个应用程序都是通过它来进行网络请求或者网络应答. socket模块和file模块有相似之处,file主要对某个文件进行打开.读写.关闭操作.soc ...