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 询 ...
随机推荐
- 怎样在 Linux 上查看某个端口的相关信息?
比如 TCP端口 / UDP端口 / 端口占用程序的进程号 等, 这些信息都可以使用: netstat -atunlp | grep 端口号 来进行获取. 比如我们想获取 22 端口的相关信息: 这里 ...
- shiro学习(一)
基础依赖: shiro-core,junit(因为在单元测试中) test.class public class AuthenticationTest { SimpleAccountRealm rea ...
- 文件下载不可以使用ajax
参看网站:https://blog.csdn.net/fan510988896/article/details/71520390 总结一下为什么下载请求不能放在ajax里发送: 原因:因为respon ...
- java面试6
1.Redis是单线程还是多线程?为什么能支持访问量和高并发?并举例解释? 1)Redis是单线程的 2)Redis是单线程加多路IO复用 3)例子:上课老师解决同学们的提问 2.Nginx有哪些基本 ...
- 2.OR Mapping 介绍
定义: ORM(Object Relational Mapping) -- 是一种为了解决面向对象与关系型数据库存在的互不匹配的现象的技术. 简单说:ORM是通过使用描述对象和数据库之间的映射的元数据 ...
- 基于Scrapt框架的全站数据爬取
创建scrapy工程项目,除了爬虫文件中的代码需要略微修改,其他模块用法相同(如中间件,管道等): 爬虫文件代码流程 导入链接提取器 from scrapy.linkextractors import ...
- 蓝牙App漏洞系列分析之一CVE-2017-0601
蓝牙App漏洞系列分析之一CVE-2017-0601 0x01 概要 2017年5月的 Android 安全公告修复了我们提交的一个蓝牙提权中危漏洞,这个漏洞尽管简单,但比较有意思,能够使本地恶意 A ...
- 自动匹配输入的内容(AutoCompleteTextView及MultiAutoCompleteTextView)
自动匹配输入的内容 AutoCompleteTextView 1.功能动态匹配输入的内容,如百度搜索引擎当输入文本时,可以根据内容显示匹配的热门信息 2.属性:android:completionTh ...
- Delphi用QJSON解析JSON格式的数据 【转】
本来用superobject来解析JSON已经够用了,可惜这个东东不能在移动端使用,于是找到QJSON来处理. 这是一个国内高手写开源免费的东西,赞一个. 假入数据如下: {"message ...
- windows环境下备份与恢复,与文件拷贝
mySQL数据库在windows环境下备份与恢复 解压版需进入数据库安装目录.安装版可以忽略本步骤: 1.先在运行中输入CMD,启动命令行2.进入Mysql的Bin目录:E:\Program File ...