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 的路径个数: 题解: 昨天再打比赛的时候,中途,凯少和我说, ...
随机推荐
- php分页查询的简单实现代码
<body><h1>分页查询</h1><?phpinclude("DADB.class.php");$db=new DADB();$tj= ...
- oracle-DML-2
1.update 语句 update table set [column,column......] where column ='' 示例: update customers set ...
- Django独有报错的原因和解决
RuntimeError at /login You called this URL via POST, but the URL doesn't end in a slash and you have ...
- LUOGU P2587 [ZJOI2008]泡泡堂
传送门 解题思路 刚开始先写了个田忌赛马的贪心,就是要是打不过就拿最弱的送死,30分...后来瞄了眼题解,发现这样是错的,比如说这样的数据 : 3 3 2 3 1 如果用田忌赛马的话,让2-3 3-1 ...
- hihocoder1317 :搜索四·跳舞链
精确覆盖问题是指对于给定的一个由0-1组成的矩阵,是否能找到一个行的集合,使得集合中每一列都恰好包含一个1. //Achen #include<algorithm> #include< ...
- Vue--公有组件以及组件的使用和特点
组件的作用:为了能够让功能与功能之间互不影响,使代码更加清晰整洁 1 <!DOCTYPE html> <html lang="en"> <head&g ...
- webpack学习之——Entry Points(入口起点)
1.Entry property(entry属性) 1.1 Single Entry (Shorthand) Syntax(单个入口语法) 用法:entry: string | Array<st ...
- 订阅 如何在IntelliJ IDEA中使用.ignore插件忽略不必要提交的文件
如何在IntelliJ IDEA中使用.ignore插件忽略不必要提交的文件 标签: idea git 插件 分类: Git 最近初学Git,而且在使用的IDE是IntelliJ IDEA,发现IDE ...
- 分析ajax请求过程以及请求方法
ajax 的全称是Asynchronous JavaScript and XML,其中,Asynchronous 是异步的意思,它有别于传统web开发中采用的同步的方式.据小编翻墙了解到,ajax很早 ...
- babel 7.x 结合 webpack 4.x 配置
今天在学习webpack的使用的时候,由于学习的教程是2018年初的,使用的是 webpack 3.x 和 babel 6.x ,然后学习的过程中出现的了很多问题. 解决问题之后,总结一下新的 bab ...