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 ...
随机推荐
- Nohttp框架在Android Studio中的使用
1.添加noHttp的使用权限 <!-- 读写 sd 卡 --> <uses-permission android:name="android.permission.REA ...
- scala集合和高级函数操作
scala常用函数操作 reduceLeft 是将集合的元素从左向右进行所需要的相应操作,图以减法为例展示,表达的算法是 : 1-2-3-4-5 例: (1 to 5).reduceLeft(_ ...
- hdu 3986 Harry Potter and the Final Battle
一个水题WA了60发,数组没开大,这OJ也不提示RE,光提示WA...... 思路:先求出最短路,如果删除的边不是最短路上的,那么对结果没有影响,要有影响,只能删除最短路上的边.所以枚举一下最短路上的 ...
- 常用JavaScript字符串方法简述
网址来源:http://www.html-js.com/article/JS-rookie-in-the-rookie-to-start-learning-to-fly-the-commonly-us ...
- IIS的集成和经典模式的区别
集成和经典的区别: 1.集成模式集成通用映射,而经典模式必须添加DLL才可以使用通用映射,存在通用映射才可以拦截 2.集成模式是IIS7.0以上,经典模式是IIS6.0以下
- HDU 1562 Guess the number
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1562 Problem Description Happy new year to everybody ...
- 关于IOS的Cocoapods相关问题
Cocoa Pods确实是一个方便的工具,特别是在敏捷开发多个项目的时候,一个工具重复使用,大量节约时间: 可以及时更新github上面的开源库代码,只要改动Podfile文件中对应的开源库的版本号即 ...
- apache 安装[转]
Apache简介 Apache HTTP Server(简称Apache)是Apache软件基金会的一个开放源码的网页服务器,可以在大多数计算机操作系统中运行,由于其多平台和安全性被广 ...
- Leetcode 39 40 216 Combination Sum I II III
Combination Sum Given a set of candidate numbers (C) and a target number (T), find all unique combin ...
- java反射机制(1)
反射,当时经常听他们说,自己也看过一些资料,也可能在设计模式中使用过,但是感觉对它没有一个较深入的了解,这次重新学习了一下,感觉还行吧! 一,先看一下反射的概念: 主要是指程序可以访问,检测和修改它本 ...