Codeforces 838 B - Diverging Directions
思路:
用dfs序+线段树维护子树中距离(从1到u,再从u到1)的最小值
代码:
#pragma GCC optimize(2)
#pragma GCC optimize(3)
#pragma GCC optimize(4)
#include<bits/stdc++.h>
using namespace std;
#define fi first
#define se second
#define pi acos(-1.0)
#define LL long long
//#define mp make_pair
#define pb push_back
#define ls rt<<1, l, m
#define rs rt<<1|1, m+1, r
#define ULL unsigned LL
#define pll pair<LL, LL>
#define pii pair<int, int>
#define piii pair<pii, int>
#define mem(a, b) memset(a, b, sizeof(a))
#define fio ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define fopen freopen("in.txt", "r", stdin);freopen("out.txt", "w", stout);
//head const int N = 2e5 + ;
const LL INF = 0x3f3f3f3f3f3f3f3f;
int L[N], R[N], anc[N][], deep[N], f[N], b[N], bb[N], head[N], cnt = , now = ;
LL tree[N<<], lazy[N<<], a[N];
struct edge {
int from, to, w, nxt;
}edge[N];
void add_edge(int u, int v, int w) {
edge[cnt].from = u;
edge[cnt].to = v;
edge[cnt].w = w;
edge[cnt].nxt = head[u];
head[u] = cnt++;
} void push_up(int rt) {
tree[rt] = min(tree[rt<<], tree[rt<<|]);
}
void push_down(int rt) {
lazy[rt<<] += lazy[rt];
lazy[rt<<|] += lazy[rt];
tree[rt<<] += lazy[rt];
tree[rt<<|] += lazy[rt];
lazy[rt] = ;
}
void build(int rt, int l, int r) {
if(l == r) {
tree[rt] = a[f[l]];
return ;
}
int m = l+r >> ;
build(ls);
build(rs);
push_up(rt);
}
void update(int L, int R, int v, int rt, int l, int r) {
if(L <= l && r <= R) {
tree[rt] += v;
lazy[rt] += v;
return ;
}
if(lazy[rt]) push_down(rt);
int m = l+r >> ;
if(L <= m) update(L, R, v, ls);
if(R > m) update(L, R, v, rs);
push_up(rt);
}
LL query(int L, int R, int rt, int l, int r) {
if(L <= l && r <= R) return tree[rt];
if(lazy[rt]) push_down(rt);
int m = l+r >> ;
LL ans = INF;
if(L <= m) ans = min(ans, query(L, R, ls));
if(R > m) ans = min(ans, query(L, R, rs));
push_up(rt);
return ans;
}
void dfs(int u) {
L[u] = now;
f[now] = u;
for (int i = head[u]; ~i; i = edge[i].nxt) {
int v = edge[i].to;
anc[v][] = u;
for (int j = ; j < ; j++) anc[v][j] = anc[anc[v][j-]][j-];
deep[v] = deep[u] + ;
now++;
a[v] = a[u] - b[u] + edge[i].w + b[v];
dfs(v);
}
R[u] = now;
}
int lca(int u, int v) {
if(deep[u] < deep[v]) swap(u, v);
for (int i = ; i >= ; i--) if(deep[anc[u][i]] >= deep[v]) u = anc[u][i];
if(u == v) return u;
for (int i = ; i >= ; i--) if(anc[u][i] != anc[v][i]) u = anc[u][i], v = anc[v][i];
return anc[u][];
}
int main() {
int n, q, ty, u, v, w;
mem(head, -);
scanf("%d %d", &n, &q);
for (int i = ; i < n; i++) {
scanf("%d %d %d", &u, &v, &w);
add_edge(u, v, w);
} for (int i = ; i < n; i++) {
scanf("%d %d %d", &u, &v, &w);
b[u] = w;
bb[i] = u;
} for (int i = ; i < ; i++) anc[][i] = ;
dfs();
build(, L[], R[]);
while(q--) {
scanf("%d %d %d", &ty, &u, &v);
if(ty == ) {
if(u >= n) {
int nod = bb[u-n+];
int add = v - b[nod];
update(L[nod], L[nod], add, , L[], R[]);
b[nod] = v;
}
else {
int nod = edge[u].to;
int add = v - edge[u].w;
update(L[nod], R[nod], add, , L[], R[]);
edge[u].w = v;
}
}
else {
int l = lca(u, v);
LL ans = INF;
if(l == u) {
LL dis1 = query(L[u], L[u], , L[], R[]) - b[u];
LL dis2 = query(L[v], L[v], , L[], R[]) - b[v];
ans = dis2 - dis1;
}
else {
LL dis1 = query(L[u], R[u], , L[], R[]) - (query(L[u], L[u], , L[], R[]) - b[u]);
LL dis2 = query(L[v], L[v], , L[], R[]) - b[v];
ans = dis1 + dis2;
}
printf("%lld\n", ans);
}
}
return ;
}
Codeforces 838 B - Diverging Directions的更多相关文章
- 「CF838B」 Diverging Directions
B. Diverging Directions 题意 给出一个n个点2n-2条边的有向图.n-1条指向远离根方向的边形成一棵树,还有n-1条从非根节点指向根节点的边. q次操作,1修改第x条边权值为y ...
- Codeforces 838B - Diverging Directions - [DFS序+线段树]
题目链接:http://codeforces.com/problemset/problem/838/B You are given a directed weighted graph with n n ...
- CodeForces 838B Diverging Directions 兼【20180808模拟测试】t3
描述 给你一个图,一共有 N 个点,2*N-2 条有向边. 边目录按两部分给出 1. 开始的 n-1 条边描述了一颗以 1 号点为根的生成树,即每个点都可以由 1 号点到达. 2. 接下来的 N-1 ...
- 线段树(Segment Tree)总结
0 写在前面 怎么说呢,其实从入坑线段树一来,经历过两个阶段,第一个阶段是初学阶段,那个时候看网上的一些教学博文和模板入门了线段树, 然后挑选了一个线段树模板作为自己的模板,经过了一点自己的修改,然后 ...
- http://codeforces.com/contest/838/problem/A
A. Binary Blocks time limit per test 2 seconds memory limit per test 256 megabytes input standard in ...
- Codeforces Round #385 (Div. 2) B - Hongcow Solves A Puzzle 暴力
B - Hongcow Solves A Puzzle 题目连接: http://codeforces.com/contest/745/problem/B Description Hongcow li ...
- Codeforces Round #292 (Div. 1) C. Drazil and Park 线段树
C. Drazil and Park 题目连接: http://codeforces.com/contest/516/problem/C Description Drazil is a monkey. ...
- Codeforces Round #370 (Div. 2)B. Memory and Trident
地址:http://codeforces.com/problemset/problem/712/B 题目: B. Memory and Trident time limit per test 2 se ...
- Codeforces Beta Round #69 (Div. 1 Only) C. Beavermuncher-0xFF 树上贪心
题目链接: http://codeforces.com/problemset/problem/77/C C. Beavermuncher-0xFF time limit per test:3 seco ...
随机推荐
- 使用nc命令传输文件和文件夹
相比较常用的scp,nc命令传文件不需要建立ssh连接和输入密码,方便快捷:尤其是在只能用key登录的机器上: 传文件: 先在目标机器执行命令准备好接收(1221为任意可用端口) nc -l 1221 ...
- java配置xml报cvc-complex-type.2.3: 元素 'beans' 必须不含字符 [子级], 因为该类型的内容类型为“仅元素”
今天测试es-job的时候,启动的时候报了标题中的错误, 经查,原因有二: 一:从网上粘贴过来字符没有对应上,所以子元素property报错,将文字复制到Notepad++等编辑工具,转为UTF-8编 ...
- eclipse maven jar工程导出项目依赖的jar包
今天遇到个事,给业务开发/测试搞个了转换工具,是使用swing写的,依赖了很多的三方包,为了方便打算以bat方式提供,但是要导出依赖的三方jar,网上搜了下,如下(已测试): 一.导出到默认目录 ta ...
- Android图片裁剪解决方案 -- 从相册截图
在看Storage Access Framework,里面有一个加载相册图片的程序片断,可能是系统版本的问题,无法返回结果,这里找到一个适用于旧版本的方法. 在Android开发中,可以轻松调用一个I ...
- hog cython
pip安装cython之后,将下面代码写入hogtest2.pyx文件(我通过改文件后缀新建) import numpy as np from PIL import Image cimport num ...
- SSH服务拒绝了密码
原因一:密码错误 解决方法:修改密码 方式一:救援模式修改密码 方式二:单用户模式破解root密码 原因二: /etc/ssh/sshd_config里设置禁止root用户直接远程登录 解决方法: v ...
- Linux服务器搭建Nexus-Maven私服(适合新手比较基础)
背景 在使用maven构建项目的时候,几乎都会涉及到一个“私服”的概念,那么到底什么是私服?使用私服有能够带来哪些益处? 私服:私服是指私有服务器,是架设在局域网的一种特殊的远程仓库,目的是代理远程仓 ...
- hihoCoder week19 RMQ问题再临-线段树 单点更新 区间查询
单点更新 区间查询 #include <bits/stdc++.h> using namespace std; #define m ((l+r)/2) #define ls (rt< ...
- Perceptual Losses for Real-Time Style Transfer and Super-Resolution and Super-Resolution 论文笔记
Perceptual Losses for Real-Time Style Transfer and Super-Resolution and Super-Resolution 论文笔记 ECCV 2 ...
- [bootstrapValidator] - bootstrap的验证工具
翻了下之前和同事做的一个验证 <!--bootstrapValidator--> <script type="text/javascript" th:inline ...