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. 2019CCPC-江西省赛C题 HDU6569 GCD预处理+二分

    Trap Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others)Total Subm ...

  2. [转]vc中调用其他应用程序的方法(函数) winexec,shellexecute ,createprocess

    三个SDK函数: WinExec,ShellExecute ,CreateProcess可以实现调用其他程序的要求,其中以WinExec最为简单,ShellExecute比WinExec灵活一些,Cr ...

  3. 超大地图MMORPG的场景管理

    目前在做一个超大地图MMORPG的场景管理部分,客户端通过动态预读解决了超大图量的动态加载,但是在做人物行走的时候遇到了一些问题: 一张地图上的PLAYER和NPC等是存放在一个list中的,地图超大 ...

  4. deepin安装下载,部署在虚拟机上

    第一次接触linux就用了很久的Ubuntu,后来尝试了Manjaro.Debian,还是用了Ubuntu,但是用VM虚拟机使用的ubuntu经常卡顿.我始终觉得ubuntu的默认桌面环境gnome可 ...

  5. oracle清除归档

    清除Oracle归档日志命令echo -e 'delete noprompt archivelog ALL COMPLETED BEFORE '\'SYSDATE-${DELETE_ARCHIVELO ...

  6. 对@repository,@Service, @Compent,@Controller注解的理解

    注解是没什么本质区别,都是声明作用,取不同的名字只是为了更好区分各自的功能. @Repository 用于标注数据访问组件,即DAO组件 @Service 用于标注业务层组件 @Controller ...

  7. Java并发指南14:Java并发容器ConcurrentSkipListMap与CopyOnWriteArrayList

    原文出处http://cmsblogs.com/ 『chenssy』 到目前为止,我们在Java世界里看到了两种实现key-value的数据结构:Hash.TreeMap,这两种数据结构各自都有着优缺 ...

  8. Spring boot 配置 Tomcat 临时文件缓存目录

    1. 问题现象 spring boot 项目中,Tomcat 接收到 content-type 为 multipart/form-data 的请求时,需要将接收的文件缓存到临时目录(默认下载 /tmp ...

  9. Django module

    1,模型定义 models.py的例子: class Author(models.Model): name=models.CharField(max_length=20) class Book(mod ...

  10. kotlin异常类

    kotlin中所有的错误异常类都是throwable的自雷,没各一次都能带有一个错误消息,调用堆栈,以及可选的错误原因,要抛出异常,可以使用throw表达式 throw myException(&qu ...