题意:给出一棵树,给出每条边的权值,现在给出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. 干电池升压3.3V的电源芯片

    PW5100适用于一节干电池升压到3.3V,两节干电池升压3.3V的升压电路,PW5100干电池升压IC. 干电池1.5V和两节干电池3V升压到3.3V的测试数据 两节干电池输出500MA测试: PW ...

  2. kettle 连接oracle12c问题解决办法:

    在oracle的安装文件目录......\NETWORK\ADMIN\sqlnet.ora 文件中添加该语句:SQLNET.ALLOWED_LOGON_VERSION_SERVER = 8window ...

  3. Linux监控内核SNMP计数器

    nstat命令和rtacct命令是一个简单的监视内核的SNMP计数器和网络接口状态的实用工具. 语法 nstat/rtacct (选项) 选项 -h:显示帮助信息: -V:显示指令版本信息: -z:显 ...

  4. .Net 5 C# 泛型(Generics)

    这里有个目录 什么是泛型? 后记 什么是泛型? 我们试试实现这个需求,给一个对象,然后返回 另一个同样的对象,先不管这个实用性,我们实现看看 首先是int型 private int Get(int a ...

  5. LOJ10099矿场搭建

    HNOI 2012 煤矿工地可以看成是由隧道连接挖煤点组成的无向图.为安全起见,希望在工地发生事故时所有挖煤点的工人都能有一条出路逃到救援出口处.于是矿主决定在某些挖煤点设立救援出口,使得无论哪一个挖 ...

  6. 洛谷 p3391

    题目背景 这是一道经典的Splay模板题--文艺平衡树. 题目描述 您需要写一种数据结构(可参考题目标题),来维护一个有序数列,其中需要提供以下操作:翻转一个区间,例如原有序序列是5 4 3 2 1, ...

  7. 记一次,Docker镜像1G多精简至300+M的过程

    记一次,Docker镜像1G多精简至300+M的过程 一.业务场景描述 二.Docker时区不一致,相差8小时 三.通过Docker发布的服务上传文件上传不上去 四.上传的图片带水印,水印中包含的字体 ...

  8. Spark SQL 自定义函数类型

    Spark SQL 自定义函数类型 一.spark读取数据 二.自定义函数结构 三.附上长长的各种pom 一.spark读取数据 前段时间一直在研究GeoMesa下的Spark JTS,Spark J ...

  9. 根据pom标签修改

    sed -i "s/<count>1<\/count>/<count>2<\/count>/g"  pom.xml

  10. Vuejs 基础学习教程

    (四)构建基础进阶-env文件与环境设置 我们在实际开发中,我们一般会经历项目的开发阶段,测试阶段,和最终上线阶段,每个阶段对于项目代码的需要可能都有所不同,那我们怎么让它在不同阶段呈现不同的效果呢? ...