結果
問題 | No.2676 A Tourist |
ユーザー | 2qbingxuan |
提出日時 | 2024-03-15 23:02:20 |
言語 | C++23 (gcc 12.3.0 + boost 1.83.0) |
結果 |
AC
|
実行時間 | 392 ms / 5,000 ms |
コード長 | 6,439 bytes |
コンパイル時間 | 3,078 ms |
コンパイル使用メモリ | 264,520 KB |
実行使用メモリ | 105,600 KB |
最終ジャッジ日時 | 2024-09-30 02:36:49 |
合計ジャッジ時間 | 12,987 ms |
ジャッジサーバーID (参考情報) |
judge1 / judge2 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 2 ms
5,248 KB |
testcase_01 | AC | 106 ms
14,336 KB |
testcase_02 | AC | 368 ms
36,864 KB |
testcase_03 | AC | 338 ms
36,808 KB |
testcase_04 | AC | 333 ms
36,992 KB |
testcase_05 | AC | 337 ms
36,864 KB |
testcase_06 | AC | 70 ms
14,976 KB |
testcase_07 | AC | 204 ms
38,528 KB |
testcase_08 | AC | 148 ms
38,656 KB |
testcase_09 | AC | 184 ms
38,460 KB |
testcase_10 | AC | 151 ms
38,420 KB |
testcase_11 | AC | 286 ms
34,816 KB |
testcase_12 | AC | 100 ms
37,120 KB |
testcase_13 | AC | 286 ms
105,576 KB |
testcase_14 | AC | 249 ms
105,556 KB |
testcase_15 | AC | 271 ms
105,472 KB |
testcase_16 | AC | 112 ms
14,464 KB |
testcase_17 | AC | 392 ms
37,248 KB |
testcase_18 | AC | 249 ms
37,336 KB |
testcase_19 | AC | 352 ms
37,376 KB |
testcase_20 | AC | 303 ms
37,248 KB |
testcase_21 | AC | 29 ms
5,248 KB |
testcase_22 | AC | 27 ms
5,248 KB |
testcase_23 | AC | 13 ms
5,248 KB |
testcase_24 | AC | 26 ms
5,248 KB |
testcase_25 | AC | 7 ms
5,248 KB |
testcase_26 | AC | 2 ms
5,248 KB |
testcase_27 | AC | 65 ms
14,852 KB |
testcase_28 | AC | 188 ms
38,512 KB |
testcase_29 | AC | 139 ms
38,268 KB |
testcase_30 | AC | 179 ms
38,308 KB |
testcase_31 | AC | 171 ms
38,484 KB |
testcase_32 | AC | 213 ms
105,600 KB |
ソースコード
// An AC a day keeps the doctor away. #include <bits/stdc++.h> using namespace std; /*{{{*/ #define all(x) begin(x), end(x) #ifdef CKISEKI #include <experimental/iterator> #define safe cerr<<__PRETTY_FUNCTION__<<" line "<<__LINE__<<" safe\n" #define debug(a...) debug_(#a, a) #define orange(a...) orange_(#a, a) void debug_(auto s, auto ...a) { cerr << "\e[1;32m(" << s << ") = ("; int f = 0; (..., (cerr << (f++ ? ", " : "") << a)); cerr << ")\e[0m\n"; } void orange_(auto s, auto L, auto R) { cerr << "\e[1;33m[ " << s << " ] = [ "; using namespace experimental; copy(L, R, make_ostream_joiner(cerr, ", ")); cerr << " ]\e[0m\n"; } #else #define safe ((void)0) #define debug(...) safe #define orange(...) safe #endif template <typename T, T MOD> class Modular { public: constexpr Modular() : v() {} template <typename U> Modular(const U &u) { v = (0 <= u && u < MOD ? u : (u%MOD+MOD)%MOD); } template <typename U> explicit operator U() const { return U(v); } T operator()() const { return v; } #define REFOP(type, expr...) Modular &operator type (const Modular &rhs) { return expr, *this; } REFOP(+=, v += rhs.v - MOD, v += MOD & (v >> width)) ; REFOP(-=, v -= rhs.v, v += MOD & (v >> width)) // fits for MOD^2 <= 9e18 REFOP(*=, v = static_cast<T>(1LL * v * rhs.v % MOD)) ; REFOP(/=, *this *= inverse(rhs.v)) #define VALOP(op) friend Modular operator op (Modular a, const Modular &b) { return a op##= b; } VALOP(+) ; VALOP(-) ; VALOP(*) ; VALOP(/) Modular operator-() const { return 0 - *this; } friend bool operator == (const Modular &lhs, const Modular &rhs) { return lhs.v == rhs.v; } friend bool operator != (const Modular &lhs, const Modular &rhs) { return lhs.v != rhs.v; } friend std::istream & operator>>(std::istream &I, Modular &m) { T x; I >> x, m = x; return I; } friend std::ostream & operator<<(std::ostream &O, const Modular &m) { return O << m.v; } private: constexpr static int width = sizeof(T) * 8 - 1; T v; static T inverse(T a) { // copy from tourist's template T u = 0, v = 1, m = MOD; while (a != 0) { T t = m / a; m -= t * a; std::swap(a, m); u -= t * v; std::swap(u, v); } assert(m == 1); return u; } }; using lld = int64_t; using llf = long double; template <typename T> using max_heap = std::priority_queue<T,vector<T>,less<T> >; template <typename T> using min_heap = std::priority_queue<T,vector<T>,greater<T> >; template <typename V, typename T> int get_pos(const V &v, T x) { return lower_bound(all(v),x) - begin(v); } template <typename V> void sort_uni(V &v) { sort(all(v)), v.erase(unique(all(v)),end(v)); } template <typename T> bool chmin(T &x, const T &v) { return v < x ? (x=v, true) : false; } template <typename T> bool chmax(T &x, const T &v) { return x < v ? (x=v, true) : false; } constexpr inline lld cdiv(lld x, lld m) { return x/m + (x%m ? (x<0) ^ (m>0) : 0); } // ceiling divide constexpr inline lld modpow(lld e,lld p,lld m) { lld r=1; for(e%=m;p;p>>=1,e=e*e%m) if(p&1) r=r*e%m; return r; }/*}}}*/ constexpr llf eps = 1e-9; constexpr lld maxn = 200025, INF = 1e18, mod = 998244353, K = 14699, inf = 1e9; using Mint = Modular<int, mod>; Mint modpow(Mint e, uint64_t p) { Mint r = 1; while (p) (p&1) && (r *= e), e *= e, p >>= 1; return r; } // 0^0 = 1 const auto dummy = [] { return cin.tie(nullptr)->sync_with_stdio(false); }(); using PII = pair<int,int>; int lowbit(int x) { return x & -x; } struct LBD { int n, timer, chains; vector<vector<int>> G; vector<int> tl, tr, ord, chain, top, dep, pa; // chains : number of chain // tl, tr[u] : subtree interval in the seq. of u // top[i] : top of the chain of vertex i // chian[u] : chain id of the chain u is on void predfs(int u, int f) { dep[u] = dep[pa[u] = f] + 1; for (int v : G[u]) if (v != f) { predfs(v, u); if (lowbit(chain[u]) < lowbit(chain[v])) chain[u] = chain[v]; } if (chain[u] == 0) chain[u] = ++chains; } void dfschain(int u, int f, int t) { ord[timer] = u; tl[u] = timer++; top[u] = t; for (int v : G[u]) if (v != f and chain[v] == chain[u]) dfschain(v, u, t); for (int v : G[u]) if (v != f and chain[v] != chain[u]) dfschain(v, u, v); tr[u] = timer; } public: LBD(auto &&G_) : n((int)size(G_)), timer(0), chains(0), G(G_), tl(n), tr(n), ord(n), chain(n), top(n + 1, -1), dep(n), pa(n) { predfs(0, 0); dfschain(0, 0, 0); } PII get_subtree(int u) { return {tl[u], tr[u]}; } vector<PII> get_path(int u, int v) { vector<PII> res; while (top[u] != top[v]) { if (dep[top[u]] < dep[top[v]]) swap(u, v); int s = top[u]; res.emplace_back(tl[s], tl[u] + 1); u = pa[s]; } if (dep[u] < dep[v]) swap(u, v); res.emplace_back(tl[v], tl[u] + 1); return res; } }; // 記得在資結上對點的修改要改成對其 dfs 序的修改 struct Segtree { int n; vector<lld> sum; Segtree(int n_) : n(n_), sum(n * 2) {} void edit(int p, lld x) { p += n; sum[p] = x; while (p >>= 1) sum[p] = sum[p << 1] + sum[p << 1 | 1]; } lld query(int l, int r) { lld res = 0; for (l += n, r += n; l < r; l >>= 1, r >>= 1) { if (l & 1) res = res + sum[l++]; if (r & 1) res = res + sum[--r]; } return res; } }; signed main() { int N, Q; cin >> N >> Q; vector<lld> a(N), cha(N); for (auto &x : a) cin >> x; vector<vector<int>> g(N); for (int i = 1; i < N; i++) { int u, v; cin >> u >> v; --u, --v; g[u].emplace_back(v); g[v].emplace_back(u); } LBD lbd(g); Segtree sgt(N); for (int i = 1; i < N; i++) { cha[lbd.pa[i]] += a[i]; } for (int i = 0; i < N; i++) sgt.edit(lbd.tl[i], cha[i]); while (Q--) { int t; cin >> t; if (t == 0) { int v, x; cin >> v >> x; --v; a[v] += x; if (v != 0) { cha[lbd.pa[v]] += x; sgt.edit(lbd.tl[lbd.pa[v]], cha[lbd.pa[v]]); } } else { int u, v; cin >> u >> v; --u, --v; auto path = lbd.get_path(u, v); lld ans = 0; for (auto [l, r] : path) { ans += sgt.query(l, r); } int w = lbd.ord[path.back().first]; debug(u, v, w); ans += a[w]; // w if (w != 0) ans += a[lbd.pa[w]]; // pa[w] cout << ans << '\n'; } } }