2019南昌网络赛 J Distance on the tree 主席树+lca
题意
给一颗树,每条边有边权,每次询问\(u\)到\(v\)的路径中有多少边的边权小于等于\(k\)
分析
在树的每个点上建\(1\)到\(i\)的权值线段树,查询的时候同时跑\(u,v,lca(u,v)\)三个版本的线段树,查询\(1\)到\(k\)的树上差分和\(val[u]+val[v]-2*val[lca]\)
Code
#include<bits/stdc++.h>
#define fi first
#define se second
#define pb push_back
using namespace std;
typedef long long ll;
const int inf=1e9;
const int maxn=2e5+10;
int n,m,q;
int a[maxn],b[maxn];
typedef pair<int,int> pii;
vector<pii>g[maxn];
int top[maxn],sz[maxn],f[maxn],son[maxn],d[maxn];
int val[maxn*30],ls[maxn*30],rs[maxn*30],rt[maxn],tot;
int qu[maxn],qv[maxn],qk[maxn];
void bd(int l,int r,int &p){
val[++tot]=val[p],ls[tot]=ls[p],rs[tot]=rs[p],p=tot;
if(l==r) return;int mid=l+r>>1;
bd(l,mid,ls[p]);bd(mid+1,r,rs[p]);
}
void up(int k,int l,int r,int &p){
val[++tot]=val[p]+1,ls[tot]=ls[p],rs[tot]=rs[p],p=tot;
if(l==r) return;int mid=l+r>>1;
if(k<=mid) up(k,l,mid,ls[p]);
else up(k,mid+1,r,rs[p]);
}
int qy(int k,int l,int r,int a,int b,int c){
int ret=0;
if(l>=1&&r<=k) return val[a]+val[b]-2*val[c];
int mid=l+r>>1;
if(1<=mid) ret+=qy(k,l,mid,ls[a],ls[b],ls[c]);
if(k>mid) ret+=qy(k,mid+1,r,rs[a],rs[b],rs[c]);
return ret;
}
void add(int x){
int k=lower_bound(b+1,b+m+1,a[x])-b;
rt[x]=rt[f[x]];up(k,1,m,rt[x]);
}
void dfs1(int u){
sz[u]=1;d[u]=d[f[u]]+1;add(u);
for(pii x:g[u]){
if(x.fi==f[u]) continue;
a[x.fi]=x.se;f[x.fi]=u;
dfs1(x.fi);sz[u]+=sz[x.fi];
if(sz[x.fi]>sz[son[u]]) son[u]=x.fi;
}
}
void dfs2(int u,int t){
top[u]=t;
if(!son[u]) return;
dfs2(son[u],t);
for(pii x:g[u]){
if(x.fi==son[u]||x.fi==f[u]) continue;
dfs2(x.fi,x.fi);
}
}
int lca(int x,int y){
while(top[x]!=top[y]){
if(d[top[x]]>=d[top[y]]) x=f[top[x]];
else y=f[top[y]];
}
if(d[x]>=d[y]) return y;
else return x;
}
int main(){
scanf("%d%d",&n,&q);
for(int i=1,a,x,c;i<n;i++){
scanf("%d%d%d",&a,&x,&c);
b[++m]=c;
g[a].pb(pii(x,c));
g[x].pb(pii(a,c));
}
for(int i=1;i<=q;i++){
scanf("%d%d%d",&qu[i],&qv[i],&qk[i]);
b[++m]=qk[i];
}
sort(b+1,b+m+1);
m=unique(b+1,b+m+1)-b-1;
bd(1,m,rt[0]);
dfs1(1);dfs2(1,1);
for(int i=1;i<=q;i++){
qk[i]=lower_bound(b+1,b+m+1,qk[i])-b;
printf("%d\n",qy(qk[i],1,m,rt[qu[i]],rt[qv[i]],rt[lca(qu[i],qv[i])]));
}
return 0;
}
2019南昌网络赛 J Distance on the tree 主席树+lca的更多相关文章
- 南昌网络赛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年ICPC南昌网络赛 J. Distance on the tree 树链剖分+主席树
边权转点权,每次遍历到下一个点,把走个这条边的权值加入主席树中即可. #include<iostream> #include<algorithm> #include<st ...
- 计蒜客 2019南昌邀请网络赛J Distance on the tree(主席树)题解
题意:给出一棵树,给出每条边的权值,现在给出m个询问,要你每次输出u~v的最短路径中,边权 <= k 的边有几条 思路:当时网络赛的时候没学过主席树,现在补上.先树上建主席树,然后把边权交给子节 ...
- 2019南昌网络赛I:Yukino With Subinterval(CDQ) (树状数组套主席树)
题意:询问区间有多少个连续的段,而且这段的颜色在[L,R]才算贡献,每段贡献是1. 有单点修改和区间查询. 思路:46min交了第一发树套树,T了. 稍加优化多交几次就过了. 不难想到,除了L这个点, ...
- 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,w ,求节点 u 与节点 v 之间权值 ≤ w 的路径个数: 题解: 昨天再打比赛的时候,中途,凯少和我说, ...
- 2019南昌网络赛 I. Yukino With Subinterval 树状数组套线段树
I. Yukino With Subinterval 题目链接: Problem Descripe Yukino has an array \(a_1, a_2 \cdots a_n\). As a ...
- ACM-ICPC 2019南昌网络赛I题 Yukino With Subinterval
ACM-ICPC 2019南昌网络赛I题 Yukino With Subinterval 题目大意:给一个长度为n,值域为[1, n]的序列{a},要求支持m次操作: 单点修改 1 pos val 询 ...
随机推荐
- MyBatis学习存档(1)——入门
一.简介 MyBatis的前身是iBatis,本是Apache的一个开源的项目 MyBatis是一个数据持久层(ORM)框架,把实体类和SQL语句之间建立了映射关系,是一种半自动化的ORM实现 MyB ...
- SSD性能测试
Tested by CrystalDiskMark 7 * MB/s = 1,000,000 bytes/s [SATA/600 = 600,000,000 bytes/s]* KB = 1000 b ...
- TensorFlow良心入门教程
All the matrials come from Machine Learning class in Polyu,HK and I reorganize them and add referenc ...
- O030、Launch 和 shut off 操作详解
参考https://www.cnblogs.com/CloudMan6/p/5460464.html 本节详细分析 instance launch 和 shut off 操作 ,以及如何在日志中快 ...
- electron-vue在npm run build时报错 ⨯ cannot execute cause=fork/exec C:\Users\801\AppData\Local\electron-builder\Cache\winCodeSign\winCodeSign-2.5.0\rcedit-ia32.exe: Access is denied.
问题描述 在electron-vue执行npm run build时报错,错误如下: ⨯ cannot execute cause=fork/exec C:\Users\801\AppData\Loc ...
- 数组通常在JS中使用
数组通常在JS中使用,例如具有相同名称的多个输入.如果它们是动态生成的,则需要在提交时确定它们是否是数组.如果(文件).MyList.长度!=“未定义”)此用法不正确.正确的是如果(文件.MyList ...
- 【Day1】1.了解Python
视频地址(全部) https://edu.csdn.net/course/detail/26057 课件地址(全部) https://download.csdn.net/download/gentl ...
- SpringBoot static修饰的字段/方法如何获取application.yml配置
SpringBoot的application.yml一种特殊的应用场景,一般我们获取application.yml的配置文件只要@Value就可以获取到值了,但是如果是static修饰的字段肯定就不能 ...
- 采用kubeadm部署工具,部署kubernetes1.16.3
安装kubenetes有5种部署工具,分别是kubeadm.kops.KRIB.Kubespray.本实验采用的是kubeadm部署工具.如有想了解其他部署工具,请点击这里 环境说明 角色/主机名 系 ...
- docker使用上的错误
docker启动问题 Cannot connect to the Docker daemon at unix:///var/run/docker.sock. Is the docker daemon ...