1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159
| int rt, L, R; struct SMT { int pcnt; struct info { ll a, b, c, ab, bc, abc; ll mrk; int l, r; } t[N << 3];
void init(int l, int r) { pcnt = 0, rt = 0; L = l, R = r; }
void init(int p) { t[p] = { 0, 0, 0, 0, 0, 0, 0, 0 }; }
#define ls t[p].l #define rs t[p].r #define mid ((cl+cr)>>1) #define len (cr-cl+1) #define lson ls,cl,mid #define rson rs,mid+1,cr
void push_up(info& p, info l, info r) { p.a = max(l.a, r.a); p.b = max(l.b, r.b); p.c = max(l.c, r.c); p.ab = max({ l.ab, r.ab, l.a + r.b }); p.bc = max({ l.bc, r.bc, l.b + r.c }); p.abc = max({ l.abc, r.abc, l.ab + r.c, l.a + r.bc }); }
void push_down(info& p, ll d, int l) { p.a += d; p.b -= 2 * d; p.c += d; p.ab -= d; p.bc -= d; p.mrk += d; }
void push_up(int p) { push_up(t[p], t[ls], t[rs]); } void push_down(int p, int l) { push_down(t[ls], t[p].mrk, l - (l >> 1)); push_down(t[rs], t[p].mrk, l >> 1); t[p].mrk = 0; }
void build(auto& arr, int& p = rt, int cl = L, int cr = R) { p = ++pcnt, init(p); if (cl == cr) return push_down(t[p], arr[cl], 1); build(arr, lson), build(arr, rson); return push_up(p); }
void modify(int l, int r, ll d, int& p = rt, int cl = L, int cr = R) { if (l <= cl && cr <= r) return push_down(t[p], d, len); push_down(p, len); if (l <= mid) modify(l, r, d, lson); if (mid < r) modify(l, r, d, rson); return push_up(p); }
info query(int l, int r, int& p = rt, int cl = L, int cr = R) { if (l <= cl && cr <= r) return t[p]; push_down(p, len); if (r <= mid) return query(l, r, lson); if (mid < l) return query(l, r, rson); info res; push_up(res, query(l, r, lson), query(l, r, rson)); return res; } } smt;
vector<pair<int, int>> E[N]; int son[N]; ll w[N]; ll dep[N]; int siz[N]; int tfn[N], tfnR[N], tseq[N << 1], tunix; void dfs(int u, int p) { tseq[++tunix] = u; tfn[u] = tunix; siz[u] = 1; for (auto& [v, id] : E[u]) { if (v == p) continue; son[id] = v; dep[v] = dep[u] + w[id]; dfs(v, u); siz[u] += siz[v]; tseq[++tunix] = u; } tfnR[u] = tunix; }
void init() { dfs(1, 0); smt.init(1, tunix); vector<ll> arr(tunix + 1); for (int i = 1; i <= tunix; i++) { arr[i] = dep[tseq[i]]; } smt.build(arr); }
void change(int id, ll e) { int v = son[id]; smt.modify(tfn[v], tfnR[v], -w[id]); w[id] = e; smt.modify(tfn[v], tfnR[v], w[id]); }
ll get() { return smt.query(1, tunix).abc; }
void eachT() { int n = read(), q = read();
for (int id = 1; id < n; id++) { int u = read(), v = read(); w[id] = read(); E[u].emplace_back(v, id); E[v].emplace_back(u, id); }
init();
vector<vector<int>> vec(n); for (int id = 1; id < n; id++) { int v = son[id]; vec[min(siz[v], n - siz[v])].push_back(id); }
vector<vector<tuple<int, int, int>>> que(n); for (int qid = 0; qid < q; qid++) { int id = read(), e = read(), k = read(); que[k].emplace_back(id, e, qid); }
vector<ll> ans(q); for (int k = 1; k < n; k++) { for (auto& [id, e, qid] : que[k]) { int old = w[id]; if (old) change(id, e); ans[qid] = get(); change(id, old); } for (auto& id : vec[k]) { change(id, 0); } }
for (int i = 0; i < q; i++) { printf("%lld\n", ans[i]); } }
|