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
| #include <bits/stdc++.h> using namespace std; using LL = long long; #define FOR(i, x, y) for (decay<decltype(y)>::type i = (x), _##i = (y); i < _##i; ++i) #define FORD(i, x, y) for (decay<decltype(x)>::type i = (x), _##i = (y); i > _##i; --i) #ifdef zerol #define dbg(args...) do { cout << "\033[32;1m" << #args << " -> "; err(args); } while (0) #else #define dbg(...) #endif void err() { cout << "\033[39;0m" << endl; } template<template<typename...> class T, typename t, typename... Args> void err(T<t> a, Args... args) { for (auto x: a) cout << x << ' '; err(args...); } template<typename T, typename... Args> void err(T a, Args... args) { cout << a << ' '; err(args...); } // ----------------------------------------------------------------------------- const int maxn = 15E4 + 100; int v[maxn]; struct E { int to, d; }; vector<E> G[maxn]; int n, Q; LL ans, A; int sz[maxn], son[maxn], fa[maxn], idx[maxn], top[maxn], dis[maxn], clk, len[maxn];
void predfs(int u, int d = 0) { dis[u] = d; sz[u] = 1; int& maxs = son[u] = -1; for (E& e: G[u]) { int v = e.to; if (v == fa[u]) continue; fa[v] = u; predfs(v, d + e.d); sz[u] += sz[v]; if (maxs == -1 || sz[v] > sz[maxs]) maxs = v; } }
void dfs(int u, int tp) { top[u] = tp; idx[u] = ++clk; if (~son[u]) dfs(son[u], tp); for (E& e: G[u]) { int v = e.to; if (v != son[u] && v != fa[u]) dfs(v, v); if (v != fa[u]) len[idx[v]] = e.d; } }
template<typename T> void mod(int u, T&& fn) { while (top[u] != 1) { fn(idx[top[u]], idx[u]); u = fa[top[u]]; } if (u != 1) fn(2, idx[u]); } LL dsum[maxn], cnt[maxn]; struct P { int u, v; }; vector<P> a;
namespace tree { #define mid ((l + r) >> 1) #define lson ql, qr, l, mid #define rson ql, qr, mid + 1, r struct P { LL add, sum; int ls, rs; } tr[maxn * 45 * 2]; int sz = 1; int N(LL add, int l, int r, int ls, int rs) { tr[sz] = {add, tr[ls].sum + tr[rs].sum + add * (len[r] - len[l - 1]), ls, rs}; return sz++; } int update(int o, int ql, int qr, int l, int r, LL add) { if (ql > r || l > qr) return o; const P& t = tr[o]; if (ql <= l && r <= qr) return N(add + t.add, l, r, t.ls, t.rs); return N(t.add, l, r, update(t.ls, lson, add), update(t.rs, rson, add)); } LL query(int o, int ql, int qr, int l, int r, LL add = 0) { if (ql > r || l > qr) return 0; const P& t = tr[o]; if (ql <= l && r <= qr) return add * (len[r] - len[l - 1]) + t.sum; return query(t.ls, lson, add + t.add) + query(t.rs, rson, add + t.add); } }
int rt[maxn]; int main() { cin >> n >> Q >> A; vector<int> vv; FOR (i, 0, n) { scanf("%d", &v[i]); vv.push_back(v[i]); } sort(vv.begin(), vv.end()); vv.erase(unique(vv.begin(), vv.end()), vv.end()); FOR (i, 0, n) a.push_back({i + 1, int(lower_bound(vv.begin(), vv.end(), v[i]) - vv.begin() + 1)}); FOR (_, 1, n) { int u, v, d; scanf("%d%d%d", &u, &v, &d); G[u].push_back({v, d}); G[v].push_back({u, d}); } predfs(1); dfs(1, 1); FOR (i, 1, n + 1) len[i] += len[i - 1]; for (P& x: a) dsum[x.v] += dis[x.u], cnt[x.v]++; FOR (i, 1, vv.size() + 1) dsum[i] += dsum[i - 1], cnt[i] += cnt[i - 1]; sort(a.begin(), a.end(), [](const P& a, const P& b)->bool { return a.v < b.v; }); int pt = 0, o = 0; FOR (val, 1, (int)vv.size() + 1) { while (pt < n && a[pt].v == val) mod(a[pt++].u, [&](int ql, int qr) { o = tree::update(o, ql, qr, 1, n, 1); }); rt[val] = o; } while (Q--) { int u; LL a, b; scanf("%d%lld%lld", &u, &a, &b); a = (a + ans) % A; b = (b + ans) % A; LL l = min(a, b), r = max(a, b); l = lower_bound(vv.begin(), vv.end(), l) - vv.begin() + 1; r = upper_bound(vv.begin(), vv.end(), r) - vv.begin(); if (l > r) { cout << (ans = 0) << endl; continue; } ans = dsum[r] - dsum[l - 1] + 1LL * (cnt[r] - cnt[l - 1]) * dis[u]; mod(u, [&](int ql, int qr) { ans -= (tree::query(rt[r], ql, qr, 1, n) - tree::query(rt[l - 1], ql, qr, 1, n)) * 2; }); cout << ans << endl; } }
|