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 的路径个数: 题解: 昨天再打比赛的时候,中途,凯少和我说, ...
随机推荐
- oracle-PL/SQL2
一 存储过程 存储过程是SQL 语句和可选控制流语句的预编译集合,以一个名称存储并作为一个单元处理.在 ORACLE SERVER上建立存储过程,可以被多个应用程序调用,可以向存储过程传递参数,也可以 ...
- Vue--moment时间格式插件安装和使用
使用链接:http://momentjs.cn/ 1.安装monent 2.导入 3.过滤器 4.template模板使用:
- Ajax--art-template + 调用天气接口
一.实现原理: <!DOCTYPE html> <html lang="en"> <head> <meta charset="U ...
- Poj 2104区间第k大(归并树)
题目链接 K-th Number Time Limit: 20000MS Memory Limit: 65536K Total Submissions: 36890 Accepted: 11860 C ...
- 学习JDK1.8集合源码之--HashSet
1. HashSet简介 HashSet是一个不可重复的无序集合,底层由HashMap实现存储,故HashSet是非线程安全的,由于HashSet使用HashMap的Key来存储元素,而HashMap ...
- SDUT-3377_数据结构实验之查找五:平方之哈希表
数据结构实验之查找五:平方之哈希表 Time Limit: 400 ms Memory Limit: 65536 KiB Problem Description 给定的一组无重复数据的正整数,根据给定 ...
- Gatling初次体验
主要步骤: 1. 利用springboot编写了一个简单的服务jdktest 2.将jdktest利用docker在虚拟机中启动 3.创建一个scala工程,利用gatling提供的DSL编写性能脚本 ...
- R语言因子
R语言因子 因子是它们用于将数据进行分类并将其存储为级别的数据对象.它们可以同时存储字符串和整数.它们在具有唯一值的有限数目的列是有用的. 例如,"male, "Female&qu ...
- jQuery纵向分类下拉菜单导航
在线演示 本地下载
- 2018-8-14-解决-VS-跳转定义和-Resharper-重复
title author date CreateTime categories 解决 VS 跳转定义和 Resharper 重复 lindexi 2018-8-14 17:35:6 +0800 201 ...