南昌网络赛J. Distance on the tree 树链剖分
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 树链剖分的更多相关文章
- 2019年ICPC南昌网络赛 J. Distance on the tree 树链剖分+主席树
边权转点权,每次遍历到下一个点,把走个这条边的权值加入主席树中即可. #include<iostream> #include<algorithm> #include<st ...
- 南昌网络赛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 的边有几条 思路:当时网络赛的时候没学过主席树,现在补上.先树上建主席树,然后把边权交给子节 ...
- Hdu 5274 Dylans loves tree (树链剖分模板)
Hdu 5274 Dylans loves tree (树链剖分模板) 题目传送门 #include <queue> #include <cmath> #include < ...
- POJ3237 Tree 树链剖分 边权
POJ3237 Tree 树链剖分 边权 传送门:http://poj.org/problem?id=3237 题意: n个点的,n-1条边 修改单边边权 将a->b的边权取反 查询a-> ...
- 2019南昌邀请赛网络预选赛 J.Distance on the tree(树链剖分)
传送门 题意: 给出一棵树,每条边都有权值: 给出 m 次询问,每次询问有三个参数 u,v,w ,求节点 u 与节点 v 之间权值 ≤ w 的路径个数: 题解: 昨天再打比赛的时候,中途,凯少和我说, ...
- 计蒜客 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 ...
随机推荐
- SSH 远程上传本地文件至服务器
使用SSH命令行传输文件到远程服务器 以前一直在windows下用SSH Secure Shell连接远程服务器,它自带了一个可视化的文件传输工具,跟ftp差不多 但是它也存在一个缺陷,不支持编码 ...
- I Hate It (HDU 1754)
Problem 很多学校流行一种比较的习惯.老师们很喜欢询问,从某某到某某当中,分数最高的是多少. 这让很多学生很反感. 不管你喜不喜欢,现在需要你做的是,就是按照老师的要求,写一个程序,模拟老师 ...
- mac安装需要的骚操作
显示隐藏文件 defaults write com.apple.finder AppleShowAllFiles -bool true; KillAll Finder 允许任何来源 sudo spct ...
- 解决“cv2.error: OpenCV(3.4.2) C:\projects\opencv-python\opencv\modules\highgui\src\window.cpp:356:...”
主要是图片路径中“文件夹分隔符”使用的错误 将“\”改成“/”就好了 修改后的测试代码如下:x.py #导入cv模块 import cv2 as cv #读取图像,支持 bmp.jpg.png.tif ...
- 使用setUncaughtExceptionHandler在线程外面捕获异常
package com.dwz.concurrency.chapter11; /** * Thread的run方法是不能throw出异常的,只能在日志或者console中打印出来 */ public ...
- CentOS7.4搭建ftp服务
1.使用yum安装vsftpd yum install vsftpd -y 2.安装完成后,启动 FTP 服务: service vsftpd start 3.配置ftp权限 目前 FTP 服务登陆允 ...
- vmware压缩vmdk文件大小
在搭建靶机环境的过程中总是遇见vmdk越来越大,导致上传时间变长.记一下压缩vmdk的方法 ;sync;rm -f zero.fill /usr/bin/vmware-toolbox-cmd disk ...
- 第11组 Alpha冲刺(5/6)
第11组 Alpha冲刺(5/6) 队名 不知道叫什么团队 组长博客 https://www.cnblogs.com/xxylac/p/11898559.html 作业博客 https://edu ...
- SpringBoot RestTemplate接收文件,并将文件发送到另外一个程序进行存储
最近有个需求,接收用户上报的证书,并且保存起来,证书大小不到1M,但该证书的保存必须在另外一个程序进行,所以想到使用springboot接收上传文件后,再通过RestTemplate将文件发送给另外一 ...
- Window 32位 编程总结
Lesson01 win32 Window 32位 编程 1.Windows编程基础 2.Windows的字符 3.窗口处理 4.消息处理 5.绘图 6.对话框 7.控件 Win32编程: Windo ...