2019年ICPC南昌网络赛 J. Distance on the tree 树链剖分+主席树
边权转点权,每次遍历到下一个点,把走个这条边的权值加入主席树中即可。
#include<iostream>
#include<algorithm>
#include<stdio.h>
#include<string.h>
using namespace std;
const int maxx = 2e5+;
struct node{
int l,r,cnt;
}tree[maxx*];
int head[maxx],rk[maxx],siz[maxx],top[maxx],son[maxx],d[maxx],fa[maxx],id[maxx],rt[maxx];
int dis[maxx];
struct Edge{
int Next,to,w;
}edge[maxx<<];
int root[maxx];
int tot=,cnt=,order;
void inserts(int l,int r,int pre,int &now,int pos){
now=++cnt;
tree[now]=tree[pre];
tree[now].cnt++;
if (l==r){
return ;
}
int mid=(l+r)>>;
if (pos<=mid){
inserts(l,mid,tree[pre].l,tree[now].l,pos);
}else {
inserts(mid+,r,tree[pre].r,tree[now].r,pos);
}
}
int query(int L,int R,int l,int r,int w){
if (l==r){
return tree[R].cnt-tree[L].cnt;
}
int mid=(l+r)>>;
if (w<=mid){
return query(tree[L].l,tree[R].l,l,mid,w);
}else {
return tree[tree[R].l].cnt-tree[tree[L].l].cnt+query(tree[L].r,tree[R].r,mid+,r,w);
}
}
void add(int x,int y,int z){
edge[++tot].to=y;
edge[tot].w=z;
edge[tot].Next=head[x];
head[x]=tot;
}
void dfs1(int u,int f,int depth){
d[u]=depth;
fa[u]=f;
siz[u]=;
for (int i=head[u];i;i=edge[i].Next){
int v=edge[i].to;
if (v==f)continue;
dfs1(v,u,depth+);
dis[v]=edge[i].w;
siz[u]+=siz[v];
if(siz[v]>siz[son[u]])
son[u]=v;
}
}
void dfs2(int u,int t){
top[u]=t;
id[u]=++order;
rk[order]=u;
// cout<<dis[u]<<endl;
inserts(,1e9,root[order-],root[order],dis[u]);
if(!son[u])return;
dfs2(son[u],t);
for (int i=head[u];i;i=edge[i].Next)
{
int v=edge[i].to;
if(v!=son[u] && v!=fa[u])
dfs2(v,v);
}
}
int query_line(int a,int b,int c){
int ans=;
while(top[a]!=top[b]){
if (d[top[a]]>d[top[b]])swap(a,b);
ans+=query(root[id[top[b]]-],root[id[b]],,1e9,c);
b=fa[top[b]];
}
if (d[a]>d[b])swap(a,b);
ans+=query(root[id[a]],root[id[b]],,1e9,c);
return ans;
}
int main(){
int w,op,n,uu,vv;
scanf("%d%d",&n,&op);
tot=;
cnt=;
order=;
for (int i=;i<n;i++){
scanf("%d%d%d",&uu,&vv,&w);
add(uu,vv,w);
add(vv,uu,w);
}
dfs1(,,);
dfs2(,);
// cout<<endl;
// for (int i=1;i<=n;i++){
// cout<<dis[i]<<"--";
// }
// cout<<endl;
// cout<<endl;
// for (int i=1;i<=n;i++){
// cout<<"--"<<id[i]<<endl;
// }
while(op--){
scanf("%d%d%d",&uu,&vv,&w);
printf("%d\n",w?query_line(uu,vv,w):);
}
return ;
}
2019年ICPC南昌网络赛 J. Distance on the tree 树链剖分+主席树的更多相关文章
- 南昌网络赛J. Distance on the tree 树链剖分
Distance on the tree 题目链接 https://nanti.jisuanke.com/t/38229 Describe DSM(Data Structure Master) onc ...
- 南昌网络赛J. Distance on the tree 树链剖分+主席树
Distance on the tree 题目链接 https://nanti.jisuanke.com/t/38229 Describe DSM(Data Structure Master) onc ...
- 2019南昌网络赛 J Distance on the tree 主席树+lca
题意 给一颗树,每条边有边权,每次询问\(u\)到\(v\)的路径中有多少边的边权小于等于\(k\) 分析 在树的每个点上建\(1\)到\(i\)的权值线段树,查询的时候同时跑\(u,v,lca ...
- 2019南昌邀请赛网络赛:J distance on the tree
1000ms 262144K DSM(Data Structure Master) once learned about tree when he was preparing for NOIP(N ...
- 计蒜客 2019南昌邀请网络赛J Distance on the tree(主席树)题解
题意:给出一棵树,给出每条边的权值,现在给出m个询问,要你每次输出u~v的最短路径中,边权 <= k 的边有几条 思路:当时网络赛的时候没学过主席树,现在补上.先树上建主席树,然后把边权交给子节 ...
- 2019 ICPC 南昌网络赛
2019 ICPC 南昌网络赛 比赛时间:2019.9.8 比赛链接:The 2019 Asia Nanchang First Round Online Programming Contest 总结 ...
- J. Distance on the tree(树链剖分+线段树)
贴贴大佬的计蒜客题解: The Preliminary Contest for ICPC China Nanchang National Invitational and International ...
- 2019.01.21 bzoj1758: [Wc2010]重建计划(01分数规划+长链剖分+线段树)
传送门 长链剖分好题. 题意简述:给一棵树,问边数在[L,R][L,R][L,R]之间的路径权值和与边数之比的最大值. 思路: 用脚指头想都知道要01分数规划. 考虑怎么checkcheckcheck ...
- 2019南昌邀请赛网络预选赛 J.Distance on the tree(树链剖分)
传送门 题意: 给出一棵树,每条边都有权值: 给出 m 次询问,每次询问有三个参数 u,v,w ,求节点 u 与节点 v 之间权值 ≤ w 的路径个数: 题解: 昨天再打比赛的时候,中途,凯少和我说, ...
随机推荐
- SpringBooot-基础<1>-构建项目
SpringBooot-基础<1>-构建项目 使用Springboot创建web工程,我使用的工具:STS,JDK1.8 1. File -> New -> Maven Pro ...
- vue里图片压缩上传组件
//单图上传 <template> <div> <div class="uploader" v-if='!dwimg'> <van-upl ...
- 2019-3-1-win10-uwp-发布旁加载自动更新
title author date CreateTime categories win10 uwp 发布旁加载自动更新 lindexi 2019-03-01 09:40:27 +0800 2019-0 ...
- 百分比宽度并排元素浮动之后,设置margin,padding换行的问题
今天遇到一个问题, 如下图,右边的div加了内边距换行: 解决方法: box-sizing: border-box;
- node安装镜像和webpack
先安装node 安装指南:https://npm.taobao.org/
- Html5知识点以及兼容性
什么的HTNL5? HTML5 是最新的 HTML 标准. HTML5 是专门为承载丰富的 web 内容而设计的,并且无需额外插件. HTML5 拥有新的语义.图形以及多媒体元素. HTML5 提供的 ...
- Watering Grass (贪心,最小覆盖)
参考: https://blog.csdn.net/shuangde800/article/details/7828675 https://www.cnblogs.com/haoabcd2010/p/ ...
- vue 根据数组中某一项的值进行排序
一.前言 我在vue项目中遇到了一个表格排序的需求,根据某一项的值的大小从大到小调整数组顺序. 二.代码 表格大概是这个样子,样式和图片在代码中简化了. <table class="r ...
- Linux常用命令5 用户管理命令
1.用户管理命令:useradd 所在路径:/usr/bin/useradd 执行权限:root 语法:useradd 用户名 功能描述:添加新用户 例如:useradd hzw userd ...
- Find Minimumd in Rotated Sorted Array
二分搜索查最小数,from mid to分别为区间的第一个,中位数,和最后一个数 if(from<=mid&&mid<=to)//顺序,第一个即为最小值 return fr ...