题意

给一颗树,每条边有边权,每次询问\(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的更多相关文章

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

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

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

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

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

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

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

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

  5. 2019南昌网络赛I:Yukino With Subinterval(CDQ) (树状数组套主席树)

    题意:询问区间有多少个连续的段,而且这段的颜色在[L,R]才算贡献,每段贡献是1. 有单点修改和区间查询. 思路:46min交了第一发树套树,T了. 稍加优化多交几次就过了. 不难想到,除了L这个点, ...

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

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

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

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

  8. 2019南昌网络赛  I. Yukino With Subinterval 树状数组套线段树

    I. Yukino With Subinterval 题目链接: Problem Descripe Yukino has an array \(a_1, a_2 \cdots a_n\). As a ...

  9. ACM-ICPC 2019南昌网络赛I题 Yukino With Subinterval

    ACM-ICPC 2019南昌网络赛I题 Yukino With Subinterval 题目大意:给一个长度为n,值域为[1, n]的序列{a},要求支持m次操作: 单点修改 1 pos val 询 ...

随机推荐

  1. flask 接收参数小坑

    前后端分离: 1.get方式: items = dict(request.args.items()) app_name = items["app_name"].strip() 或 ...

  2. redis 学习(20)-- 常见的持久化开发与运维问题

    常见的持久化开发与运维问题 fork 操作 fork 操作是一个同步操作,若执行较慢会阻塞 redis 主线程 执行时间与内存量相关:内存越大,耗时越长:虚拟机较慢,真机较快 查看 fork 执行时间 ...

  3. JAVA问题String literal is not properly closed by a double-quote

    String literal is not properly closed by a double-quote 这个错误:string字串没有以双引号结束String DBURL = "jd ...

  4. SQL SERVER 语法

    1.获取所有用户名: Select name FROM Sysusers where status='2' and islogin='1' islogin='1' :表示帐户 islogin='0' ...

  5. 【原创】大叔经验分享(56)hue导出行数限制

    /opt/cloudera/parcels/CDH/lib/hue/apps/beeswax/src/beeswax/conf.py # Deprecated DOWNLOAD_CELL_LIMIT ...

  6. Web框架理解

    目录 1.web框架理解     2.http工作原理     3.通过函数实现浏览器和服务端通信案例     4.服务器程序和引用程序理解     5.jinja2渲染模板案例     6.Djan ...

  7. JVM垃圾回收那些事

    Java这种VM类跨平台语言比起C++这种传统编译型语言很大的区别之一在于引入了垃圾自动回收机制.自动垃圾回收大大提高了Java程序员的开发效率并且极大地减少了犯错的概率,但终归而言由于无法像C++程 ...

  8. Active Directory participation features and security extensions

    Participation in the Active Directory Samba 3.0 series, as well as the OS since Windows 2000, is pos ...

  9. Delphi 音频播放

    樊伟胜

  10. 基础数据类型(int,str,bool)

    一 python 中的基础数据类型 1.int  数字类型(整数类型)  主要用来数学计算 2.str  字符串   可以保存少量数据进行操作 3.bool  布尔值  判断真假   True  Fa ...