結果
問題 | No.399 動的な領主 |
ユーザー | kwm_t |
提出日時 | 2022-01-07 20:52:04 |
言語 | C++14 (gcc 12.3.0 + boost 1.83.0) |
結果 |
AC
|
実行時間 | 470 ms / 2,000 ms |
コード長 | 3,740 bytes |
コンパイル時間 | 4,779 ms |
コンパイル使用メモリ | 241,564 KB |
実行使用メモリ | 34,904 KB |
最終ジャッジ日時 | 2024-11-14 08:12:08 |
合計ジャッジ時間 | 10,390 ms |
ジャッジサーバーID (参考情報) |
judge1 / judge3 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 2 ms
6,816 KB |
testcase_01 | AC | 2 ms
6,816 KB |
testcase_02 | AC | 2 ms
6,816 KB |
testcase_03 | AC | 2 ms
6,816 KB |
testcase_04 | AC | 3 ms
6,816 KB |
testcase_05 | AC | 27 ms
6,816 KB |
testcase_06 | AC | 452 ms
29,664 KB |
testcase_07 | AC | 454 ms
29,788 KB |
testcase_08 | AC | 447 ms
32,256 KB |
testcase_09 | AC | 459 ms
32,340 KB |
testcase_10 | AC | 4 ms
6,820 KB |
testcase_11 | AC | 22 ms
6,816 KB |
testcase_12 | AC | 325 ms
31,456 KB |
testcase_13 | AC | 317 ms
30,972 KB |
testcase_14 | AC | 104 ms
34,840 KB |
testcase_15 | AC | 127 ms
34,904 KB |
testcase_16 | AC | 183 ms
32,072 KB |
testcase_17 | AC | 470 ms
32,408 KB |
testcase_18 | AC | 453 ms
31,320 KB |
ソースコード
#include <bits/stdc++.h> #include <atcoder/all> using namespace std; using namespace atcoder; //using mint = modint1000000007; //const int mod = 1000000007; //using mint = modint998244353; //const int mod = 998244353; const int INF = 1e9; //const long long LINF = 1e18; //const bool debug = false; #define rep(i, n) for (int i = 0; i < (n); ++i) #define rep2(i,l,r)for(int i=(l);i<(r);++i) #define rrep(i, n) for (int i = (n-1); i >= 0; --i) #define rrep2(i,l,r)for(int i=(r-1);i>=(l);--i) #define all(x) (x).begin(),(x).end() #define allR(x) (x).rbegin(),(x).rend() #define endl "\n" #define P pair<int,int> template<typename A, typename B> inline bool chmax(A & a, const B & b) { if (a < b) { a = b; return true; } return false; } template<typename A, typename B> inline bool chmin(A & a, const B & b) { if (a > b) { a = b; return true; } return false; } struct S { long long value; int size; }; using F = long long; S op(S a, S b) { return { a.value + b.value, a.size + b.size }; } S e() { return { 0, 0 }; } S mapping(F f, S x) { return { x.value + f * x.size, x.size }; } F composition(F f, F g) { return f + g; } F id() { return 0; } struct HeavyLightDecomposition { HeavyLightDecomposition(vector<vector<int>> _g, vector<int>&_val) :g(_g), val(_val) { n = g.size(); sz.resize(n); par.resize(n); in.resize(n); out.resize(n); rev.resize(n); head.resize(n); } int n; vector<vector<int>>g; vector<int>sz, par, in, out, rev, head; vector<int>val; lazy_segtree<S, op, e, F, mapping, composition, id> seg; void build() { dfs_sz(0); int time = 0; dfs_hld(0, time); initSegmentTree(); } void dfs_sz(int v, int p = -1) { par[v] = p; sz[v] = 1; if (g[v].size() && (p == g[v][0]))swap(g[v][0], g[v].back()); for (auto &e : g[v]) { if (p == e)continue; dfs_sz(e, v); sz[v] += sz[e]; if (sz[g[v][0]] < sz[e])swap(g[v][0], e); } } void dfs_hld(int v, int &time, int p = -1) { in[v] = time; time++; rev[in[v]] = v; for (auto &e : g[v]) { if (p == e)continue; if (e == g[v][0])head[e] = head[v]; else head[e] = e; dfs_hld(e, time, v); } out[v] = time; } void initSegmentTree() { vector<S>a(n); rep(i, n)a[i].value = val[in[i]], a[i].size = 1; lazy_segtree<S, op, e, F, mapping, composition, id> _seg(a); seg = _seg; } int query(int u, int v) { int ret = 0; for (;; v = par[head[v]]) { if (in[u] > in[v])swap(u, v); if (head[u] == head[v])break; ret += seg.prod(in[head[v]], in[v] + 1).value; } ret += seg.prod(in[u], in[v] + 1).value; return ret; } int la(int v, int k) { while (1) { int u = head[v]; if (in[v] - k > in[u])return rev[in[v] - k]; k -= in[v] - in[u] + 1; v = par[u]; } } int lca(int u, int v) { for (;; v = par[head[v]]) { if (in[u] > in[v])swap(u, v); if (head[u] == head[v])return u; } } void debug() { cout << "in" << endl; rep(i, n) cout << in[i] << " "; cout << endl; cout << "head" << endl; rep(i, n) cout << head[i] << " "; cout << endl; } void update(int u, int v, int x) { for (;; v = par[head[v]]) { if (in[u] > in[v])swap(u, v); if (head[u] == head[v])break; seg.apply(in[head[v]], in[v] + 1, x); } seg.apply(in[u], in[v] + 1, x); } }; int main() { ios::sync_with_stdio(false); cin.tie(nullptr); int n; cin >> n; vector<vector<int>>g(n); rep(i, n - 1) { int u, v; cin >> u >> v; u--; v--; g[u].push_back(v); g[v].push_back(u); } vector<int>x(n, 0); HeavyLightDecomposition hld(g, x); hld.build(); int q; cin >> q; while (q--) { int a, b; cin >> a >> b; a--; b--; hld.update(a, b, 1); } long long ans = 0; rep(i, n) { long long get = hld.seg.get(i).value; ans += get * (get + 1) / 2; } cout << ans << endl; return 0; }