首页 > 其他分享 >CF838B Diverging Directions

CF838B Diverging Directions

时间:2022-10-25 11:36:15浏览次数:68  
标签:fr Diverging ca tree CF838B int dfn Directions top


题目链接:​​传送门​

分析一下他给的这个图就知道
一个祖先到它的儿子只有生成树的边可以走
而一个儿子走到祖先有两种方法
一个是先直接到1,再从1走到那个祖先
还有一个很容易想不到,就是继续往它的儿子走,再从它的某个儿子回到1
比如说我一开始就没想到,WA了半天才发现这个问题
所以还需要维护这样的一个最小值
没心情改了,先把没改的代码放这

#include <bits/stdc++.h>
#define

using namespace std;
typedef long long ll;
struct node {int next, to, w;}e[A];
int head[A], num;
void add(int fr, int to, int w) {e[++num].next = head[fr]; e[num].to = to; e[num].w = w; head[fr] = num;}
int siz[A], fa[A], w[A], dep[A], son[A], dfn[A], cnt, top[A], pre[A];
void prepare(int fr) {
siz[fr] = 1;
for (int i = head[fr]; i; i = e[i].next) {
int ca = e[i].to;
if (ca == fa[fr]) continue;
fa[ca] = fr; w[ca] = e[i].w; dep[ca] = dep[fr] + 1;
prepare(ca); siz[fr] += siz[ca];
if (siz[ca] > siz[son[fr]]) son[fr] = ca;
}
}
void dfs(int fr, int tp) {
dfn[fr] = ++cnt; pre[cnt] = fr; top[fr] = tp;
if (son[fr]) dfs(son[fr], tp);
for (int i = head[fr]; i; i = e[i].next) {
int ca = e[i].to;
if (ca == fa[fr] or ca == son[fr]) continue;
dfs(ca, ca);
}
}
struct Tree {int l, r, w;}tree[A];
void build(int k, int l, int r) {
tree[k].l = l; tree[k].r = r;
if (l == r) {tree[k].w = w[pre[l]]; return;}
int m = (l + r) >> 1;
build(k << 1, l, m); build(k << 1 | 1, m + 1, r);
tree[k].w = tree[k << 1].w + tree[k << 1 | 1].w;
}
void change(int k, int pos, int val) {
if (tree[k].l == tree[k].r) {tree[k].w = val; return;}
int m = (tree[k].l + tree[k].r) >> 1;
if (pos <= m) change(k << 1, pos, val);
else change(k << 1 | 1, pos, val);
tree[k].w = tree[k << 1].w + tree[k << 1 | 1].w;
}
int asktree(int k, int l, int r) {
if (tree[k].l >= l and tree[k].r <= r) return tree[k].w;
int m = (tree[k].l + tree[k].r) >> 1, ans = 0;
if (l <= m) ans += asktree(k << 1, l, r);
if (r > m) ans += asktree(k << 1 | 1, l, r);
return ans;
}
int ask(int x, int y, int ans = 0) {
while (top[x] != top[y]) {
if (dep[top[x]] < dep[top[y]]) swap(x, y);
ans += asktree(1, dfn[top[x]], dfn[x]);
x = fa[top[x]];
}
if (dep[x] > dep[y]) swap(x, y);
ans += asktree(1, dfn[x] + 1, dfn[y]);
return ans;
}
map<int, int> v[A];
int n, q, a[A], b[A], c[A], opt, x, y;

int main(int argc, char const *argv[]) {
cin >> n >> q;
for (int i = 1; i < n; i++) {
scanf("%d%d%d", &a[i], &b[i], &c[i]);
add(a[i], b[i], c[i]);
}
prepare(1); dfs(1, 1); build(1, 1, n);
for (int i = n; i < 2 * n - 1; i++) {
scanf("%d%d%d", &a[i], &b[i], &c[i]);
v[a[i]][b[i]] = c[i];
}
while (q--) {
scanf("%d%d%d", &opt, &x, &y);
if (opt == 1) {
if (x <= n - 1) change(1, dfn[b[x]], y);
else v[a[x]][b[x]] = y;
}
else {
if (x == y) {puts("0"); continue;}
if (dfn[x] < dfn[y] and dfn[x] + siz[x] > dfn[y])
{printf("%d\n", ask(x, y)); continue;}
printf("%d\n", v[x][1] + ask(1, y));
}
}
}


标签:fr,Diverging,ca,tree,CF838B,int,dfn,Directions,top
From: https://blog.51cto.com/lyle/5794331

相关文章

  • Directions
    SpatialPartitioningFoundationsofMultidimensionalandMetricDataStructures-HananSametFileOrganizationandProcessing-AlanL.TharpGeometricdatast......
  • IfcGridPlacementDirectionSelect
    IfcGridPlacementDirectionSelect类型定义IfcGridPlacementDirectionSelect允许选择将网格放置定义为显式方向,或通过引用第二个网格交点来提供方向。 IFC4中的新选择......
  • [Google] LeetCode 2096 Step-By-Step Directions From a Binary Tree Node to Anothe
    Youaregiventherootofabinarytreewithnnodes.Eachnodeisuniquelyassignedavaluefrom1ton.YouarealsogivenanintegerstartValuerepresenting......