|
| 1 | +package g3501_3600.s3515_shortest_path_in_a_weighted_tree; |
| 2 | + |
| 3 | +// #Hard #2025_04_13_Time_37_ms_(100.00%)_Space_146.42_MB_(100.00%) |
| 4 | + |
| 5 | +import java.util.ArrayList; |
| 6 | +import java.util.List; |
| 7 | + |
| 8 | +@SuppressWarnings("unchecked") |
| 9 | +public class Solution { |
| 10 | + private int[] in; |
| 11 | + private int[] out; |
| 12 | + private int[] baseDist; |
| 13 | + private int[] parent; |
| 14 | + private int[] depth; |
| 15 | + private int timer = 0; |
| 16 | + private int[] edgeWeight; |
| 17 | + private List<int[]>[] adj; |
| 18 | + |
| 19 | + public int[] treeQueries(int n, int[][] edges, int[][] queries) { |
| 20 | + adj = new ArrayList[n + 1]; |
| 21 | + for (int i = 1; i <= n; i++) { |
| 22 | + adj[i] = new ArrayList<>(); |
| 23 | + } |
| 24 | + for (int[] e : edges) { |
| 25 | + int u = e[0], v = e[1], w = e[2]; |
| 26 | + adj[u].add(new int[] {v, w}); |
| 27 | + adj[v].add(new int[] {u, w}); |
| 28 | + } |
| 29 | + in = new int[n + 1]; |
| 30 | + out = new int[n + 1]; |
| 31 | + baseDist = new int[n + 1]; |
| 32 | + parent = new int[n + 1]; |
| 33 | + depth = new int[n + 1]; |
| 34 | + edgeWeight = new int[n + 1]; |
| 35 | + dfs(1, 0, 0); |
| 36 | + Fenw fenw = new Fenw(n); |
| 37 | + List<Integer> ansList = new ArrayList<>(); |
| 38 | + for (int[] query : queries) { |
| 39 | + if (query[0] == 1) { |
| 40 | + int u = query[1], v = query[2], newW = query[3]; |
| 41 | + int child; |
| 42 | + if (parent[v] == u) { |
| 43 | + child = v; |
| 44 | + } else if (parent[u] == v) { |
| 45 | + child = u; |
| 46 | + } else { |
| 47 | + continue; |
| 48 | + } |
| 49 | + int diff = newW - edgeWeight[child]; |
| 50 | + edgeWeight[child] = newW; |
| 51 | + fenw.updateRange(in[child], out[child], diff); |
| 52 | + } else { |
| 53 | + int x = query[1]; |
| 54 | + int delta = fenw.query(in[x]); |
| 55 | + ansList.add(baseDist[x] + delta); |
| 56 | + } |
| 57 | + } |
| 58 | + int[] answer = new int[ansList.size()]; |
| 59 | + for (int i = 0; i < ansList.size(); i++) { |
| 60 | + answer[i] = ansList.get(i); |
| 61 | + } |
| 62 | + return answer; |
| 63 | + } |
| 64 | + |
| 65 | + private void dfs(int node, int par, int dist) { |
| 66 | + parent[node] = par; |
| 67 | + baseDist[node] = dist; |
| 68 | + depth[node] = (par == 0) ? 0 : depth[par] + 1; |
| 69 | + in[node] = ++timer; |
| 70 | + for (int[] neighborInfo : adj[node]) { |
| 71 | + int neighbor = neighborInfo[0]; |
| 72 | + int w = neighborInfo[1]; |
| 73 | + if (neighbor == par) continue; |
| 74 | + edgeWeight[neighbor] = w; |
| 75 | + dfs(neighbor, node, dist + w); |
| 76 | + } |
| 77 | + out[node] = timer; |
| 78 | + } |
| 79 | + |
| 80 | + private static class Fenw { |
| 81 | + int n; |
| 82 | + int[] fenw; |
| 83 | + |
| 84 | + public Fenw(int n) { |
| 85 | + this.n = n; |
| 86 | + fenw = new int[n + 2]; |
| 87 | + } |
| 88 | + |
| 89 | + private void update(int i, int delta) { |
| 90 | + while (i <= n) { |
| 91 | + fenw[i] += delta; |
| 92 | + i += i & -i; |
| 93 | + } |
| 94 | + } |
| 95 | + |
| 96 | + public void updateRange(int l, int r, int delta) { |
| 97 | + update(l, delta); |
| 98 | + update(r + 1, -delta); |
| 99 | + } |
| 100 | + |
| 101 | + public int query(int i) { |
| 102 | + int sum = 0; |
| 103 | + while (i > 0) { |
| 104 | + sum += fenw[i]; |
| 105 | + i -= i & -i; |
| 106 | + } |
| 107 | + return sum; |
| 108 | + } |
| 109 | + } |
| 110 | +} |
0 commit comments