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 询 ...
随机推荐
- EF入门-CRUD操作
一.EF数据查询假设我们已经定义好了context:private AccountContext db = new AccountContext(); 1.[基本查询] 查询所有var users = ...
- 【题解】JSOI2008 最大数
题目描述 现在请求你维护一个数列,要求提供以下两种操作: 查询操作. 语法:Q L 功能:查询当前数列中末尾L个数中的最大的数,并输出这个数的值. 限制:L不超过当前数列的长度.(L>=0) 插 ...
- Docker 部署mysql、tomcat笔记
Docker 笔记整理 #.环境:Ubuntu 18.* #.安装 mysql 5.6.tomcat #.docker search mysql 报错:Error response from daem ...
- 【Git的基本操作七】创建远程库、在本地创建远程库别名
1. 创建远程库
- a标签 href不跳转 禁止跳转
当页面中a标签不需要任何跳转时,从原理上来讲,可分如下两种方法: 标签属性href,使其指向空或不返回任何内容.如: <a href="javascript:void(0);" ...
- MySQL 5.7.18 zip版本的安装使用方法
转自:https://www.cnblogs.com/nepulgh/p/7152618.html MySQL 5.7.18 zip版本的安装使用方法 这个版本的MySQL不像那种点击就可以立即安装, ...
- mysql一些语句
<!-- 报警量排行按创建时间每月来排行 --> <select id="alarmDaySort" resultType="alarm"&g ...
- ASE19 团队项目 模型组 scrum report集合
scrum report 链接 scrum1 report scrum2 report scrum3 report scrum4 report scrum5 report scrum6 report ...
- window, linux, mac 比较文件和文件夹的区别
windows 端 winmerge beyondcompare Mac 和 linux 端 Meld kdiff3 diff command 更多可参考:https://alternativeto ...
- axiso基本使用及python接收处理
安装$ npm install axios 1.发送get请求: axios.get("/api/v1.0/cars?id=132").then(function(res){ co ...