Distance on the tree

题目链接

https://nanti.jisuanke.com/t/38229

Describe

DSM(Data Structure Master) once learned about tree when he was preparing for NOIP(National Olympiad in Informatics in Provinces) in Senior High School. So when in Data Structure Class in College, he is always absent-minded about what the teacher says.

The experienced and knowledgeable teacher had known about him even before the first class. However, she didn't wish an informatics genius would destroy himself with idleness. After she knew that he was so interested in ACM(ACM International Collegiate Programming Contest), she finally made a plan to teach him to work hard in class, for knowledge is infinite.

This day, the teacher teaches about trees." A tree with nnn nodes, can be defined as a graph with only one connected component and no cycle. So it has exactly n−1 edges..." DSM is nearly asleep until he is questioned by teacher. " I have known you are called Data Structure Master in Graph Theory, so here is a problem. "" A tree with nnn nodes, which is numbered from 1 to n. Edge between each two adjacent vertexes uuu and v has a value w, you're asked to answer the number of edge whose value is no more than k during the path between u and v."" If you can't solve the problem during the break, we will call you DaShaMao(Foolish Idiot) later on."

The problem seems quite easy for DSM. However, it can hardly be solved in a break. It's such a disgrace if DSM can't solve the problem. So during the break, he telephones you just for help. Can you save him for his dignity?

Input

In the first line there are two integers n,m represent the number of vertexes on the tree and queries(\(2≤n≤10^5,1≤m≤10^5\))

The next n-1 lines, each line contains three integers u,v,w indicates there is an undirected edge between nodes uuu and v with value w. (\(1≤u,v≤n,1≤w≤10^9\))

The next mmm lines, each line contains three integers u,v,k be consistent with the problem given by the teacher above. (\(1≤u,v≤n,0≤k≤10^9\))

Output

For each query, just print a single line contains the number of edges which meet the condition.

样例输入1

3 3

1 3 2

2 3 7

1 3 0

1 2 4

1 2 7

样例输出1

0

1

2

样例输入2

5 2

1 2 1000000000

1 3 1000000000

2 4 1000000000

3 5 1000000000

2 3 1000000000

4 5 1000000000

样例输出2

2

4

题意

给你一棵树,问两个点之间边权小与等于k的数量。

题解

方法一:

比赛时,智商不够,靠数据结构来凑,弱弱的我直接树剖,然后用主席树,感觉就是把两个板子结合一下。第一次一遍AC,看了好几遍才确定自己没看错。

过了n天之后,当我刷完了树链剖分专题,并且遇到了一道和此题相似度极高的题目后,

我终于发现了原来我就是个DaShaMao(Foolish Idiot),完全可以不用主席树。

方法二:

可以看 https://www.cnblogs.com/mmmqqdd/p/10799395.html

这里我也讲一下大概思路:我们先将询问按照val(题目中的k)从小到大排序,那么我们扫一遍询问数组,因为val单增,所以对于每个询问i,我们就把权值小于等于val[i]的位置加一。答案就是直接求u到v的路径和。

方法二很好写,我写了25分钟,一遍AC了。

代码1——树链剖分+主席数(在线,不建议使用)

#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define N 100050
#define INF 123456789
int n,m;
int tot,last[N];
ll ans[N];
int cnt,fa[N],dp[N],size[N],son[N],rk[N],kth[N],top[N];
struct Query
{
int l,r,id; ll val;
bool operator <(const Query&b)const
{return val<b.val;}
}a[N],que[N<<1];
struct Edge{int from,to,s;}edges[N<<1];
struct Tree{int l,r;ll sum;}tr[N<<2];
template<typename T>void read(T&x)
{
ll k=0; char c=getchar();
x=0;
while(!isdigit(c)&&c!=EOF)k^=c=='-',c=getchar();
if (c==EOF)exit(0);
while(isdigit(c))x=x*10+c-'0',c=getchar();
x=k?-x:x;
}
void read_char(char &c)
{while(!isalpha(c=getchar())&&c!=EOF);}
void AddEdge(int x,int y)
{
edges[++tot]=Edge{x,y,last[x]};
last[x]=tot;
}
void dfs1(int x,int pre)
{
fa[x]=pre;
dp[x]=dp[pre]+1;
size[x]=1;
son[x]=0;
for(int i=last[x];i;i=edges[i].s)
{
Edge &e=edges[i];
if (e.to==pre)continue;
dfs1(e.to,x);
size[x]+=size[e.to];
if (size[e.to]>size[son[x]])son[x]=e.to;
}
}
void dfs2(int x,int y)
{
rk[x]=++cnt;
kth[cnt]=x;
top[x]=y;
if (son[x]==0)return;
dfs2(son[x],y);
for(int i=last[x];i;i=edges[i].s)
{
Edge &e=edges[i];
if (e.to==fa[x]||e.to==son[x])continue;
dfs2(e.to,e.to);
}
}
void bt(int x,int l,int r)
{
tr[x].l=l; tr[x].r=r; tr[x].sum=0;
if (l==r)return;
int mid=(l+r)>>1;
bt(x<<1,l,mid);
bt(x<<1|1,mid+1,r);
}
void update(int x,int p,ll tt)
{
if (p<=tr[x].l&&tr[x].r<=p)
{
tr[x].sum+=tt;
return;
}
int mid=(tr[x].l+tr[x].r)>>1;
if (p<=mid)update(x<<1,p,tt);
if (mid<p)update(x<<1|1,p,tt);
tr[x].sum=tr[x<<1].sum+tr[x<<1|1].sum;
}
ll query(int x,int l,int r)
{
if (l<=tr[x].l&&tr[x].r<=r)
return tr[x].sum;
int mid=(tr[x].l+tr[x].r)>>1; ll ans=0;
if (l<=mid)ans+=query(x<<1,l,r);
if (mid<r)ans+=query(x<<1|1,l,r);
return ans;
}
ll get_sum(int x,int y)
{
int fx=top[x],fy=top[y];ll ans=0;
while(fx!=fy)
{
if (dp[fx]<dp[fy])swap(x,y),swap(fx,fy);
ans+=query(1,rk[fx],rk[x]);
x=fa[fx]; fx=top[x];
}
if (dp[x]<dp[y])swap(x,y);
ans+=query(1,rk[y],rk[x]);
return ans;
}
void work()
{
read(n); read(m);
for(int i=1;i<=n;i++)read(a[i].val),a[i].id=i;
for(int i=1;i<=n-1;i++)
{
int x,y;
read(x); read(y);
AddEdge(x,y);
AddEdge(y,x);
}
int num=0;
for(int i=1;i<=m;i++)
{
int l,r,x,y;
read(l); read(r); read(x);read(y);
que[++num]=Query{l,r,-i,x-1};
que[++num]=Query{l,r,i,y};
}
sort(a+1,a+n+1);
sort(que+1,que+num+1);
dfs1(1,0);
dfs2(1,1);
bt(1,1,n);
int ds=1;
for(int i=1;i<=num;i++)
{
while(ds<=n&&a[ds].val<=que[i].val)
{
update(1,rk[a[ds].id],a[ds].val);
ds++;
}
ll sum=get_sum(que[i].l,que[i].r);
if (que[i].id<0) ans[-que[i].id]-=sum;
else ans[que[i].id]+=sum;
}
printf("%lld",ans[1]);
for(int i=2;i<=m;i++)printf(" %lld",ans[i]);
printf("\n");
}
void clear()
{
tot=0; cnt=0;
memset(last,0,sizeof(last));
memset(ans,0,sizeof(ans));
}
int main()
{
#ifndef ONLINE_JUDGE
freopen("aa.in","r",stdin);
#endif
while(1)
{
clear();
work();
}
}

代码2--树链剖分+线段树(离线)

#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define N 100050
int n,m,ans[N];
int tot,last[N];
int cnt,fa[N],dp[N],size[N],son[N],rk[N],kth[N],top[N];
struct Tree{int l,r,sum;}tr[N<<2];
struct Edge{int from,to,val,s;}edges[N<<1];
struct Query
{
int id,l,r,val;
bool operator <(const Query&b)const
{return val<b.val;}
}que[N],a[N];
template<typename T>void read(T&x)
{
ll k=0; char c=getchar();
x=0;
while(!isdigit(c)&&c!=EOF)k^=c=='-',c=getchar();
if (c==EOF)exit(0);
while(isdigit(c))x=x*10+c-'0',c=getchar();
x=k?-x:x;
}
void read_char(char &c)
{while(!isalpha(c=getchar())&&c!=EOF);}
void AddEdge(int x,int y,int z)
{
edges[++tot]=Edge{x,y,z,last[x]};
last[x]=tot;
}
void dfs1(int x,int pre)
{
fa[x]=pre;
dp[x]=dp[pre]+1;
size[x]=1;
son[x]=0;
for(int i=last[x];i;i=edges[i].s)
{
Edge &e=edges[i];
if (e.to==pre)continue;
a[e.to].id=e.to;
a[e.to].val=e.val;
dfs1(e.to,x);
size[x]+=size[e.to];
if (size[e.to]>size[son[x]])son[x]=e.to;
}
}
void dfs2(int x,int y)
{
rk[x]=++cnt;
kth[cnt]=x;
top[x]=y;
if (son[x]==0)return;
dfs2(son[x],y);
for(int i=last[x];i;i=edges[i].s)
{
Edge &e=edges[i];
if (e.to==fa[x]||e.to==son[x])continue;
dfs2(e.to,e.to);
}
}
void bt(int x,int l,int r)
{
tr[x].l=l; tr[x].r=r; tr[x].sum=0;
if (l==r)return;
int mid=(l+r)>>1;
bt(x<<1,l,mid);
bt(x<<1|1,mid+1,r);
}
void update(int x,int p)
{
tr[x].sum++;
if (tr[x].l==tr[x].r)return;
int mid=(tr[x].l+tr[x].r)>>1;
if (p<=mid)update(x<<1,p);
else update(x<<1|1,p);
}
int query(int x,int l,int r)
{
if (l<=tr[x].l&&tr[x].r<=r)return tr[x].sum;
int mid=(tr[x].l+tr[x].r)>>1,a1=0,a2=0;
if(l<=mid)a1=query(x<<1,l,r);
if(mid<r)a2=query(x<<1|1,l,r);
return a1+a2;
}
int get_sum(int x,int y)
{
int fx=top[x],fy=top[y],ans=0;
while(fx!=fy)
{
if (dp[fx]<dp[fy])swap(x,y),swap(fx,fy);
ans+=query(1,rk[fx],rk[x]);
x=fa[fx]; fx=top[x];
}
if (dp[x]<dp[y])swap(x,y);
ans+=query(1,rk[y]+1,rk[x]);
return ans;
}
int main()
{
#ifndef ONLINE_JUDGE
freopen("aa.in","r",stdin);
#endif
read(n); read(m);
for(int i=1;i<=n-1;i++)
{
int x,y,z;
read(x); read(y); read(z);
AddEdge(x,y,z);
AddEdge(y,x,z);
}
dfs1(1,0);
dfs2(1,1);
bt(1,1,n);
sort(a+1,a+n+1);
for(int i=1;i<=m;i++)
{
que[i].id=i;
read(que[i].l); read(que[i].r); read(que[i].val);
}
sort(que+1,que+m+1);
int top=1;
for(int i=1;i<=m;i++)
{
while(top<=n&&a[top].val<=que[i].val)
{
update(1,rk[a[top].id]);
top++;
}
ans[que[i].id]=get_sum(que[i].l,que[i].r);
}
for(int i=1;i<=m;i++)printf("%d\n",ans[i]);
}

南昌网络赛J. Distance on the tree 树链剖分的更多相关文章

  1. 2019年ICPC南昌网络赛 J. Distance on the tree 树链剖分+主席树

    边权转点权,每次遍历到下一个点,把走个这条边的权值加入主席树中即可. #include<iostream> #include<algorithm> #include<st ...

  2. 南昌网络赛J. Distance on the tree 树链剖分+主席树

    Distance on the tree 题目链接 https://nanti.jisuanke.com/t/38229 Describe DSM(Data Structure Master) onc ...

  3. 2019南昌网络赛 J Distance on the tree 主席树+lca

    题意 给一颗树,每条边有边权,每次询问\(u\)到\(v\)的路径中有多少边的边权小于等于\(k​\) 分析 在树的每个点上建\(1​\)到\(i​\)的权值线段树,查询的时候同时跑\(u,v,lca ...

  4. 2019南昌邀请赛网络赛:J distance on the tree

    1000ms 262144K   DSM(Data Structure Master) once learned about tree when he was preparing for NOIP(N ...

  5. 计蒜客 2019南昌邀请网络赛J Distance on the tree(主席树)题解

    题意:给出一棵树,给出每条边的权值,现在给出m个询问,要你每次输出u~v的最短路径中,边权 <= k 的边有几条 思路:当时网络赛的时候没学过主席树,现在补上.先树上建主席树,然后把边权交给子节 ...

  6. Hdu 5274 Dylans loves tree (树链剖分模板)

    Hdu 5274 Dylans loves tree (树链剖分模板) 题目传送门 #include <queue> #include <cmath> #include < ...

  7. POJ3237 Tree 树链剖分 边权

    POJ3237 Tree 树链剖分 边权 传送门:http://poj.org/problem?id=3237 题意: n个点的,n-1条边 修改单边边权 将a->b的边权取反 查询a-> ...

  8. 2019南昌邀请赛网络预选赛 J.Distance on the tree(树链剖分)

    传送门 题意: 给出一棵树,每条边都有权值: 给出 m 次询问,每次询问有三个参数 u,v,w ,求节点 u 与节点 v 之间权值 ≤ w 的路径个数: 题解: 昨天再打比赛的时候,中途,凯少和我说, ...

  9. 计蒜客 38229.Distance on the tree-1.树链剖分(边权)+可持久化线段树(区间小于等于k的数的个数)+离散化+离线处理 or 2.树上第k大(主席树)+二分+离散化+在线查询 (The Preliminary Contest for ICPC China Nanchang National Invitational 南昌邀请赛网络赛)

    Distance on the tree DSM(Data Structure Master) once learned about tree when he was preparing for NO ...

随机推荐

  1. Ubuntu系统图形化界面无法登录到root用户的解决方法

    Ubuntu默认是禁用了root用户的登录. 系统安装后, 图形化界面无法登录到root用户解决方法:Ubuntu 12.04:1.设置root用户密码:  普通用户登录,sudo passwd ro ...

  2. jmeter连接oracle数据库

    == 下载及添加这个文件到 这个路径下 连接设置: 测试连接 链接: https://pan.baidu.com/s/1W0YcVf4VLdsjnxv5umKngQ 提取码: np7j

  3. 文件转移 互联网组成 路由器 分组交换 交换机 冲突域 网卡 数据帧的发送与接收会带来CPU开销 CPU中断 双网卡切换

    https://zh.wikipedia.org/zh-cn/网段 在以太网环境中,一个网段其实也就是一个冲突域(碰撞域).同一网段中的设备共享(包括通过集线器等设备中转连接)同一物理总线,在这一总线 ...

  4. Vue插件编写、用法详解(附demo)

    Vue插件编写.用法详解(附demo) 1.概述 简单来说,插件就是指对Vue的功能的增强或补充. 比如说,让你在每个单页面的组件里,都可以调用某个方法,或者共享使用某个变量,或者在某个方法之前执行一 ...

  5. [Java复习] Spring 常见面试问题

    1. 什么是 Spring 框架?Spring 框架有哪些主要模块? 轻量级实现IoC和AOP的JavaEE框架. Core模块: bean(bean定义创建解析), context(环境, IoC容 ...

  6. python一个文件里面多个函数同时执行(多进程的方法,并发)

    #coding=utf-8 import timefrom selenium import webdriverimport threading def fun1(a): print a def fun ...

  7. golang的下载与安装

    golang的官网可能由于政策原因登陆不上. 所以可以到Go语言中文网下载:https://studygolang.com/dl 我下载的是go1.10.3.windows-amd64.msi安装包, ...

  8. 一个小故事,玩转Python-while循环

    无论是传统编程场景还是当下火爆的人工智能应用场景,循环的应用都是必不可少的,上一篇文章中阐述了如何使用for循环来进行编程,这篇文章将会由一个小朋友经常听的故事来讲Python编程中的while循环. ...

  9. Install Virtualbox on CentOS7---(後話,最終還是沒有用virtualbox做VM server ,感覺只適用于桌面)

    參考: https://wiki.centos.org/zh-tw/HowTos/Virtualization/VirtualBox cd /etc/yum.repos.d wget http://d ...

  10. 慎用rm -rf

    首先,搞个回收站在~下 .bashrc或者.bash_profile加入 mkdir -p ~/.trash alias rm=trash alias r=trash alias rl='ls ~/. ...