結果
問題 | No.2676 A Tourist |
ユーザー | 👑 hos.lyric |
提出日時 | 2024-03-15 22:29:58 |
言語 | C++14 (gcc 12.3.0 + boost 1.83.0) |
結果 |
AC
|
実行時間 | 418 ms / 5,000 ms |
コード長 | 8,073 bytes |
コンパイル時間 | 1,812 ms |
コンパイル使用メモリ | 129,508 KB |
実行使用メモリ | 47,920 KB |
最終ジャッジ日時 | 2024-09-30 01:55:17 |
合計ジャッジ時間 | 11,457 ms |
ジャッジサーバーID (参考情報) |
judge2 / judge5 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 2 ms
5,248 KB |
testcase_01 | AC | 118 ms
10,368 KB |
testcase_02 | AC | 418 ms
24,448 KB |
testcase_03 | AC | 294 ms
24,448 KB |
testcase_04 | AC | 331 ms
24,432 KB |
testcase_05 | AC | 342 ms
24,500 KB |
testcase_06 | AC | 80 ms
10,752 KB |
testcase_07 | AC | 241 ms
25,308 KB |
testcase_08 | AC | 159 ms
25,408 KB |
testcase_09 | AC | 211 ms
25,332 KB |
testcase_10 | AC | 164 ms
25,372 KB |
testcase_11 | AC | 277 ms
23,136 KB |
testcase_12 | AC | 89 ms
18,048 KB |
testcase_13 | AC | 255 ms
47,904 KB |
testcase_14 | AC | 228 ms
47,920 KB |
testcase_15 | AC | 241 ms
47,920 KB |
testcase_16 | AC | 113 ms
10,496 KB |
testcase_17 | AC | 387 ms
24,688 KB |
testcase_18 | AC | 295 ms
24,616 KB |
testcase_19 | AC | 360 ms
24,632 KB |
testcase_20 | AC | 341 ms
24,600 KB |
testcase_21 | AC | 38 ms
6,816 KB |
testcase_22 | AC | 37 ms
6,816 KB |
testcase_23 | AC | 19 ms
6,816 KB |
testcase_24 | AC | 36 ms
6,820 KB |
testcase_25 | AC | 10 ms
6,820 KB |
testcase_26 | AC | 2 ms
6,816 KB |
testcase_27 | AC | 76 ms
10,604 KB |
testcase_28 | AC | 224 ms
25,308 KB |
testcase_29 | AC | 153 ms
25,312 KB |
testcase_30 | AC | 213 ms
25,308 KB |
testcase_31 | AC | 204 ms
25,436 KB |
testcase_32 | AC | 176 ms
47,832 KB |
ソースコード
#include <cassert> #include <cmath> #include <cstdint> #include <cstdio> #include <cstdlib> #include <cstring> #include <algorithm> #include <bitset> #include <complex> #include <deque> #include <functional> #include <iostream> #include <limits> #include <map> #include <numeric> #include <queue> #include <random> #include <set> #include <sstream> #include <string> #include <unordered_map> #include <unordered_set> #include <utility> #include <vector> using namespace std; using Int = long long; template <class T1, class T2> ostream &operator<<(ostream &os, const pair<T1, T2> &a) { return os << "(" << a.first << ", " << a.second << ")"; }; template <class T> ostream &operator<<(ostream &os, const vector<T> &as) { const int sz = as.size(); os << "["; for (int i = 0; i < sz; ++i) { if (i >= 256) { os << ", ..."; break; } if (i > 0) { os << ", "; } os << as[i]; } return os << "]"; } template <class T> void pv(T a, T b) { for (T i = a; i != b; ++i) cerr << *i << " "; cerr << endl; } template <class T> bool chmin(T &t, const T &f) { if (t > f) { t = f; return true; } return false; } template <class T> bool chmax(T &t, const T &f) { if (t < f) { t = f; return true; } return false; } #define COLOR(s) ("\x1b[" s "m") struct Hld { int n, rt; // needs to be tree // vertex lists // modified in build(rt) (parent removed, heavy child first) vector<vector<int>> graph; vector<int> sz, par, dep; int zeit; vector<int> dis, fin, sid; // head vertex (minimum depth) in heavy path vector<int> head; Hld() : n(0), rt(-1), zeit(0) {} explicit Hld(int n_) : n(n_), rt(-1), graph(n), zeit(0) {} void ae(int u, int v) { assert(0 <= u); assert(u < n); assert(0 <= v); assert(v < n); graph[u].push_back(v); graph[v].push_back(u); } void dfsSz(int u) { sz[u] = 1; for (const int v : graph[u]) { auto it = std::find(graph[v].begin(), graph[v].end(), u); if (it != graph[v].end()) graph[v].erase(it); par[v] = u; dep[v] = dep[u] + 1; dfsSz(v); sz[u] += sz[v]; } } void dfsHld(int u) { dis[u] = zeit++; const int deg = graph[u].size(); if (deg > 0) { int vm = graph[u][0]; int jm = 0; for (int j = 1; j < deg; ++j) { const int v = graph[u][j]; if (sz[vm] < sz[v]) { vm = v; jm = j; } } swap(graph[u][0], graph[u][jm]); head[vm] = head[u]; dfsHld(vm); for (int j = 1; j < deg; ++j) { const int v = graph[u][j]; head[v] = v; dfsHld(v); } } fin[u] = zeit; } void build(int rt_) { assert(0 <= rt_); assert(rt_ < n); rt = rt_; sz.assign(n, 0); par.assign(n, -1); dep.assign(n, -1); dep[rt] = 0; dfsSz(rt); zeit = 0; dis.assign(n, -1); fin.assign(n, -1); head.assign(n, -1); head[rt] = rt; dfsHld(rt); assert(zeit == n); sid.assign(n, -1); for (int u = 0; u < n; ++u) sid[dis[u]] = u; } friend ostream &operator<<(ostream &os, const Hld &hld) { const int maxDep = *max_element(hld.dep.begin(), hld.dep.end()); vector<string> ss(2 * maxDep + 1); int pos = 0, maxPos = 0; for (int j = 0; j < hld.n; ++j) { const int u = hld.sid[j]; const int d = hld.dep[u]; if (hld.head[u] == u) { if (j != 0) { pos = maxPos + 1; ss[2 * d - 1].resize(pos, '-'); ss[2 * d - 1] += '+'; } } else { ss[2 * d - 1].resize(pos, ' '); ss[2 * d - 1] += '|'; } ss[2 * d].resize(pos, ' '); ss[2 * d] += std::to_string(u); if (maxPos < static_cast<int>(ss[2 * d].size())) { maxPos = ss[2 * d].size(); } } for (int d = 0; d <= 2 * maxDep; ++d) os << ss[d] << '\n'; return os; } bool contains(int u, int v) const { return (dis[u] <= dis[v] && dis[v] < fin[u]); } int lca(int u, int v) const { assert(0 <= u); assert(u < n); assert(0 <= v); assert(v < n); for (; head[u] != head[v]; ) (dis[u] > dis[v]) ? (u = par[head[u]]) : (v = par[head[v]]); return (dis[u] > dis[v]) ? v : u; } int jumpUp(int u, int d) const { assert(0 <= u); assert(u < n); assert(d >= 0); if (dep[u] < d) return -1; const int tar = dep[u] - d; for (u = head[u]; ; u = head[par[u]]) { if (dep[u] <= tar) return sid[dis[u] + (tar - dep[u])]; } } int jump(int u, int v, int d) const { assert(0 <= u); assert(u < n); assert(0 <= v); assert(v < n); assert(d >= 0); const int l = lca(u, v); const int du = dep[u] - dep[l], dv = dep[v] - dep[l]; if (d <= du) { return jumpUp(u, d); } else if (d <= du + dv) { return jumpUp(v, du + dv - d); } else { return -1; } } // [u, v) or [u, v] template <class F> void doPathUp(int u, int v, bool inclusive, F f) const { assert(contains(v, u)); for (; head[u] != head[v]; u = par[head[u]]) f(dis[head[u]], dis[u] + 1); if (inclusive) { f(dis[v], dis[u] + 1); } else { if (v != u) f(dis[v] + 1, dis[u] + 1); } } // not path order, include lca(u, v) or not template <class F> void doPath(int u, int v, bool inclusive, F f) const { const int l = lca(u, v); doPathUp(u, l, false, f); doPathUp(v, l, inclusive, f); } // (vs, ps): compressed tree // vs: DFS order (sorted by dis) // vs[ps[x]]: the parent of vs[x] // ids[vs[x]] = x, not set for non-tree vertex vector<int> ids; pair<vector<int>, vector<int>> compress(vector<int> us) { // O(n) first time ids.resize(n, -1); std::sort(us.begin(), us.end(), [&](int u, int v) -> bool { return (dis[u] < dis[v]); }); us.erase(std::unique(us.begin(), us.end()), us.end()); int usLen = us.size(); assert(usLen >= 1); for (int x = 1; x < usLen; ++x) us.push_back(lca(us[x - 1], us[x])); std::sort(us.begin(), us.end(), [&](int u, int v) -> bool { return (dis[u] < dis[v]); }); us.erase(std::unique(us.begin(), us.end()), us.end()); usLen = us.size(); for (int x = 0; x < usLen; ++x) ids[us[x]] = x; vector<int> ps(usLen, -1); for (int x = 1; x < usLen; ++x) ps[x] = ids[lca(us[x - 1], us[x])]; return make_pair(us, ps); } }; //////////////////////////////////////////////////////////////////////////////// template <class T> void bAdd(vector<T> &bit, int pos, const T &val) { const int bitN = bit.size(); for (int x = pos; x < bitN; x |= x + 1) bit[x] += val; } template <class T> T bSum(const vector<T> &bit, int pos) { T ret = 0; for (int x = pos; x > 0; x &= x - 1) ret += bit[x - 1]; return ret; } template <class T> T bSum(const vector<T> &bit, int pos0, int pos1) { return bSum(bit, pos1) - bSum(bit, pos0); } int N, Q; vector<Int> C; vector<int> A, B; int main() { for (; ~scanf("%d%d", &N, &Q); ) { C.resize(N); for (int u = 0; u < N; ++u) { scanf("%lld", &C[u]); } A.resize(N - 1); B.resize(N - 1); for (int i = 0; i < N - 1; ++i) { scanf("%d%d", &A[i], &B[i]); --A[i]; --B[i]; } Hld hld(N); for (int i = 0; i < N - 1; ++i) { hld.ae(A[i], B[i]); } hld.build(0); vector<Int> bit(N, 0); auto add = [&](int u, Int c) -> void { const int p = hld.par[u]; if (~p) bAdd(bit, hld.dis[p], c); }; for (int u = 0; u < N; ++u) { add(u, C[u]); } for (; Q--; ) { int O; scanf("%d", &O); if (O == 0) { int U; Int X; scanf("%d%lld", &U, &X); --U; add(U, X); C[U] += X; } else if (O == 1) { int S, T; scanf("%d%d", &S, &T); --S; --T; Int ans = 0; hld.doPath(S, T, true, [&](int j0, int j1) -> void { ans += bSum(bit, j0, j1); }); const int l = hld.lca(S, T); const int p = hld.par[l]; ans += C[l]; if (~p) ans += C[p]; printf("%lld\n", ans); } else { assert(false); } } } return 0; }