题意:给出一棵树,给出每条边的权值,现在给出m个询问,要你每次输出u~v的最短路径中,边权 <= k 的边有几条

思路:当时网络赛的时候没学过主席树,现在补上。先树上建主席树,然后把边权交给子节点,然后数量就变成了 u + v - lca * 2。专题里那道算点权的应该算原题吧。1A = =,强行做模板题提高自信。

代码:

#include<cmath>
#include<set>
#include<map>
#include<queue>
#include<cstdio>
#include<vector>
#include<cstring>
#include <iostream>
#include<algorithm>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const int maxn = 1e5 + 10;
const int M = maxn * 30;
const ull seed = 131;
const int INF = 0x3f3f3f3f;
const int MOD = 1e9 + 7;
int n, m;
int root[maxn], tot;
struct Edge{
int v, next;
ll w;
}edge[maxn << 1];
int head[maxn], tol;
void addEdge(int u, int v, ll w){
edge[tol].v = v;
edge[tol].w = w;
edge[tol].next = head[u];
head[u] = tol++;
}
struct node{
int lson, rson;
int sum;
}T[maxn * 40];
void init(){
memset(T, 0, sizeof(T));
memset(root, 0, sizeof(root));
memset(head, -1, sizeof(head));
tot = tol = 0;
}
vector<int> vv;
int getid(int x){
return lower_bound(vv.begin(), vv.end(), x) - vv.begin() + 1;
}
void update(int l, int r, int &now, int pre, int v, int pos){
T[++tot] = T[pre], T[tot].sum += v, now = tot;
if(l == r) return;
int m = (l + r) >> 1;
if(pos <= m)
update(l, m, T[now].lson, T[pre].lson, v, pos);
else
update(m + 1, r, T[now].rson, T[pre].rson, v, pos);
}
void build(int now, int pre, ll w){
update(1, vv.size(), root[now], root[pre], 1, getid(w));
for(int i = head[now]; i != -1; i = edge[i].next){
int v = edge[i].v;
if(v == pre) continue;
build(v, now, edge[i].w);
}
}
int query(int l, int r, int now, int pre, int lca, int k){
if(l == r){
if(k >= l) return T[now].sum + T[pre].sum - T[lca].sum * 2;
return 0;
}
if(r <= k) return T[now].sum + T[pre].sum - T[lca].sum * 2;
int m = (l + r) >> 1;
int sum = 0;
if(k <= m)
return query(l, m, T[now].lson, T[pre].lson, T[lca].lson, k);
else{
sum = query(m + 1, r, T[now].rson, T[pre].rson, T[lca].rson, k);
return sum + T[T[now].lson].sum + T[T[pre].lson].sum - T[T[lca].lson].sum * 2;
}
} //lca
int fa[maxn][20];
int dep[maxn];
void lca_dfs(int u, int pre, int d){
dep[u] = d;
fa[u][0] = pre;
for(int i = head[u]; i != -1; i = edge[i].next){
int v = edge[i].v;
if(v != pre)
lca_dfs(v, u, d + 1);
}
}
void lca_update(){
for(int i = 1; (1 << i) <= n; i++){
for(int u = 1; u <= n; u++){
fa[u][i] = fa[fa[u][i - 1]][i - 1];
}
}
}
int lca_query(int u, int v){
if(dep[u] < dep[v]) swap(u, v);
int d = dep[u] - dep[v];
for(int i = 0; (1 << i) <= d; i++){
if(d & (1 << i)){
u = fa[u][i];
}
}
if(u != v){
for(int i = (int)log2(n); i >= 0; i--){
if(fa[u][i] != fa[v][i]){
u = fa[u][i];
v = fa[v][i];
}
}
u = fa[u][0];
}
return u;
}
int u1[maxn], v1[maxn];
ll k1[maxn];
int main(){
init();
vv.clear();
scanf("%d%d", &n, &m);
vv.push_back(0);
for(int i = 1; i <= n - 1; i++){
int u, v;
ll w;
scanf("%d%d%lld", &u, &v, &w);
addEdge(u, v, w);
addEdge(v, u, w);
vv.push_back(w);
}
for(int i = 1; i <= m; i++){
scanf("%d%d%lld", &u1[i], &v1[i], &k1[i]);
vv.push_back(k1[i]);
}
sort(vv.begin(), vv.end());
vv.erase(unique(vv.begin(), vv.end()), vv.end());
lca_dfs(1, 0, 1);
lca_update();
build(1, 0, 0);
for(int i = 1; i <= m; i++){
int lca = lca_query(u1[i], v1[i]);
printf("%d\n", query(1, vv.size(), root[u1[i]], root[v1[i]], root[lca], getid(k1[i])));
}
return 0;
}

计蒜客 2019南昌邀请网络赛J Distance on the tree(主席树)题解的更多相关文章

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

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

  2. 2019南昌网络赛 J Distance on the tree 主席树+lca

    题意 给一颗树,每条边有边权,每次询问\(u\)到\(v\)的路径中有多少边的边权小于等于\(k​\) 分析 在树的每个点上建\(1​\)到\(i​\)的权值线段树,查询的时候同时跑\(u,v,lca ...

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

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

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

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

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

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

  6. 计蒜客 2019 蓝桥杯省赛 B 组模拟赛(一)

    D题:马的管辖 二进制枚举方案.判断该方案是否全部能被覆盖,将最优方案存下来并进行剪枝. #include<iostream> #include<cstring> #inclu ...

  7. 计蒜客 2019 蓝桥杯省赛 B 组模拟赛(三)一笔画

    #include<iostream> #include<cstring> #include<cstdio> #include<algorithm> us ...

  8. 计蒜客 2019 蓝桥杯省赛 B 组模拟赛(三)数字拆分

    #include<iostream> #include<cstring> #include<cstdio> #include<algorithm> us ...

  9. 蒜厂年会|计蒜客2019蓝桥杯省赛 B 组模拟赛(一)

    样例输入: 3 1 -2 1 样例输出: 2 方法一: 将环形数组拆分成为普通数组,(通过搬运复制数据到尾部),再求前缀和,找出最大前缀和.因为枚举了每一个起点,所以最大连续和也一定出现在前缀和中.. ...

随机推荐

  1. 简单明朗的 RNN 写诗教程

    目录 简单明朗的 RNN 写诗教程 数据集介绍 代码思路 输入 and 输出 训练集构建 生成一首完整的诗 代码实现 读取文件 统计字数 构建word 与 id的映射 转成one-hot代码 随机打乱 ...

  2. Android N selectQualifiedNetwork分析

    前言: 参考:Android N wifi auto connect流程分析 后续 Android 8.0/9.0 wifi 自动连接评分机制 分析 前面说了,handleScanResults会去调 ...

  3. 使用Jmeter对SHA1加密接口进行性能测试

    性能测试过程中,有时候会遇到需要对信息头进行加密鉴权,下面我就来介绍如何针对SHA1加密鉴权开发性能测试脚本1.首先了解原理,就是需要对如下三个参数进行SHA1加密,(AppSecret + Nonc ...

  4. 关于MongoDB的简单理解(一)--基础篇

    一.什么是MongoDB? MongoDB是一个基于分布式文件存储的文档数据库,旨在简化开发和扩展,为WEB应用提供可扩展的高性能数据存储解决方案. MongoDB是一个介于关系数据库和非关系数据库之 ...

  5. BBR implements bbr-like limiter 限流

    pkg/ratelimit/bbr/bbr.go:68 github.com/go-kratos // BBR implements bbr-like limiter.// It is inspire ...

  6. https://www.cnblogs.com/AloneSword/p/3209653.html

    proc/sys/net/ipv4/下各项的意义 - 孤剑 - 博客园 https://www.cnblogs.com/AloneSword/p/3209653.html

  7. 文件系统层次结构标准 Linux 系统目录结构

    https://zh.wikipedia.org/wiki/文件系统层次结构标准 多数Linux发行版遵从FHS标准并且声明其自身政策以维护FHS的要求. [3] [4] [5] [6] 但截至200 ...

  8. LDAP学习

    .top pre { display: block; background: rgba(68, 67, 65, 1); color: rgba(255, 255, 255, 1); margin: 1 ...

  9. 在ubuntu编写helloworld

    安装vim 打开终端 输入sudo apt-get install vim-gtk 输入登陆密码 等待安装完成 编译C 创建.c文件:vim helloworld.c 编写代码,保存并退出 编译:gc ...

  10. Jaspersoft Studio报表设计

    1      开发工具 1.1  软件名称 名称:TIBCO Jaspersoft Studio 版本:6.0或以上,建议6.2.1 1.2  软件安装 免安装软件包,拷贝即可使用,建议放在D:盘或其 ...