计蒜客 2019南昌邀请网络赛J Distance on the tree(主席树)题解
题意:给出一棵树,给出每条边的权值,现在给出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(主席树)题解的更多相关文章
- 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 主席树+lca
题意 给一颗树,每条边有边权,每次询问\(u\)到\(v\)的路径中有多少边的边权小于等于\(k\) 分析 在树的每个点上建\(1\)到\(i\)的权值线段树,查询的时候同时跑\(u,v,lca ...
- 2019南昌邀请赛网络预选赛 J.Distance on the tree(树链剖分)
传送门 题意: 给出一棵树,每条边都有权值: 给出 m 次询问,每次询问有三个参数 u,v,w ,求节点 u 与节点 v 之间权值 ≤ w 的路径个数: 题解: 昨天再打比赛的时候,中途,凯少和我说, ...
- 南昌网络赛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 蓝桥杯省赛 B 组模拟赛(一)
D题:马的管辖 二进制枚举方案.判断该方案是否全部能被覆盖,将最优方案存下来并进行剪枝. #include<iostream> #include<cstring> #inclu ...
- 计蒜客 2019 蓝桥杯省赛 B 组模拟赛(三)一笔画
#include<iostream> #include<cstring> #include<cstdio> #include<algorithm> us ...
- 计蒜客 2019 蓝桥杯省赛 B 组模拟赛(三)数字拆分
#include<iostream> #include<cstring> #include<cstdio> #include<algorithm> us ...
- 蒜厂年会|计蒜客2019蓝桥杯省赛 B 组模拟赛(一)
样例输入: 3 1 -2 1 样例输出: 2 方法一: 将环形数组拆分成为普通数组,(通过搬运复制数据到尾部),再求前缀和,找出最大前缀和.因为枚举了每一个起点,所以最大连续和也一定出现在前缀和中.. ...
随机推荐
- Ubuntu源、Python虚拟环境及pip源配置
Ubuntu 命令行更改源 在修改source.list前,最好先备份一份 软件源的地址配置文件在 /etc/apt/sources.list 执行备份命令 sudo cp /etc/apt/sour ...
- jQuery 多选与清除
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...
- 借助 AppleScript 一键打开工作空间
我有个小毛病:同时只能在一个工程里工作. 假如让我开四五个 Webstorm,在工程里 A 改个Bug,然后又到工程 B 里加个需求,再去工程 C 发个版,切来切去一会儿就懵了. 于是有了这个项目:m ...
- HTTP/2与HTTP/3的新特性
解密HTTP/2与HTTP/3的新特性 - InfoQ https://www.infoq.cn/article/kU4OkqR8vH123a8dLCCJ
- 风险识别系统-大数据智能风控管理平台-企业风控解决方案– 阿里云 https://www.aliyun.com/product/saf
风险识别系统-大数据智能风控管理平台-企业风控解决方案– 阿里云 https://www.aliyun.com/product/saf
- 内存模型 Memory model 内存分布及程序运行中(BSS段、数据段、代码段、堆栈
C语言中内存分布及程序运行中(BSS段.数据段.代码段.堆栈) - 秦宝艳的个人页面 - 开源中国 https://my.oschina.net/pollybl1255/blog/140323 Mem ...
- 配置完xadmin源码包后启动报错“ Apps aren't loaded yet.”
raise AppRegistryNotReady("Apps aren't loaded yet.") django.core.exceptions. 碰到这种情况就要查看下是否 ...
- 「一本通 1.3 例 5」weight]
「一本通 1.3 例 5」weight 题面 给定原数列 \(a_1,a_2,a_n\) ,给定每个数的前缀和以及后缀和,并且打乱顺序. 给出一个集合 \(S\) 要求从集合 \(S\) 中找到合适的 ...
- Spring框架——事务管理方式搭建一个小的项目
学习Spring框架,通过事务管理的方式搭建一个小的项目,该项目可以查询对数据库中的图书库存数量进行修改. 首先,使用MVC分层的设计模式思想搭建项目目录结构. 此部分代码源码之中都有相关注释,所以尽 ...
- Java 8教程(知识内容详细,快速学习Java 8)
允许在接口中有默认方法实现 Lambda表达式 函数式接口 方法和构造函数引用 Lambda的范围 内置函数式接口 Predicates Functions Suppliers Consumers C ...