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的更多相关文章

  1. 「CF838B」 Diverging Directions

    B. Diverging Directions 题意 给出一个n个点2n-2条边的有向图.n-1条指向远离根方向的边形成一棵树,还有n-1条从非根节点指向根节点的边. q次操作,1修改第x条边权值为y ...

  2. Codeforces 838B - Diverging Directions - [DFS序+线段树]

    题目链接:http://codeforces.com/problemset/problem/838/B You are given a directed weighted graph with n n ...

  3. CodeForces 838B Diverging Directions 兼【20180808模拟测试】t3

    描述 给你一个图,一共有 N 个点,2*N-2 条有向边. 边目录按两部分给出 1. 开始的 n-1 条边描述了一颗以 1 号点为根的生成树,即每个点都可以由 1 号点到达. 2. 接下来的 N-1 ...

  4. 线段树(Segment Tree)总结

    0 写在前面 怎么说呢,其实从入坑线段树一来,经历过两个阶段,第一个阶段是初学阶段,那个时候看网上的一些教学博文和模板入门了线段树, 然后挑选了一个线段树模板作为自己的模板,经过了一点自己的修改,然后 ...

  5. 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 ...

  6. 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 ...

  7. 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. ...

  8. 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 ...

  9. 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 ...

随机推荐

  1. GitHub使用笔记2:github常用操作

    1: 绑定ssh keys 2:github新建仓库 echo "# SpringStack" >> README.md git init git add README ...

  2. 如何用git将项目代码上传到github

    注册账户以及创建仓库 要想使用github第一步当然是注册github账号了.之后就可以创建仓库了(免费用户只能建公共仓库),Create a New Repository,填好名称后Create,之 ...

  3. linux 函数库使用

    程序函数库可分为3种类型:静态函 数库(static libraries).共享函数库(shared libraries)和动态加载函数库(dynamically loaded libraries) ...

  4. php5.3.x连接MS SQL server2008

    开篇 因为毕设老师需求的原因,虚拟旅游网站要求的数据库必须使用MS SQL server. 我最擅长的web编程语言是PHP,但是在PHP中链接MS SQL server是一件非常麻烦的事,我个人分析 ...

  5. Kali系列之aircrack-ng wifi密码穷举

    kali linux安全渗透 网卡:rtl8187 工具aircrack-ng 操作+ 查看无线网卡信息 ifconfig 或者 iwconfig 有个是wlan0的就是您的外置无线网卡 启动网卡监听 ...

  6. tomcat8.5之后版本,远程无法登录管理页面

    转载自http://jingyan.baidu.com/article/1612d500b56fa1e20e1eeed2.html 服务器采用的是linux系统. 安装tomcat在服务器上后,客户端 ...

  7. topcoder srm 435 div1

    problem1 link 遍历未被删除的叶子结点即可. problem2 link 首先,将所有的蛋白质原子编号,设为$[0,m-1]$,每个原子可能对应多个长度为3的$ACGT$.设$n$为DNA ...

  8. Web操作web.config

    1.引用System.Configuration.DLL 2.引用命名空间System.Configuration和System.Web.Configuration 3.上代码 // 使用指定的虚拟路 ...

  9. UVA 10382 Watering Grass(区间覆盖,贪心)题解

    题意:有一块草坪,这块草坪长l 米,宽 w 米,草坪有一些喷头,每个喷头在横坐标为 p 处,每个喷头的纵坐标都是(w/2) ,并且喷头的洒水范围是一个以喷头为圆心,半径为 r 米的圆.每次最少需要打开 ...

  10. Linux/shell: remove adjacent similar patterns

    cat > temp004AA1abcAA2AA3abcAA4abcAA5AA6 awk 'BEGIN {pre=0; str="";} { if(NR==1){     i ...