結果
| 問題 |
No.399 動的な領主
|
| コンテスト | |
| ユーザー |
はまやんはまやん
|
| 提出日時 | 2017-06-10 14:45:17 |
| 言語 | C++14 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
WA
|
| 実行時間 | - |
| コード長 | 5,278 bytes |
| コンパイル時間 | 2,218 ms |
| コンパイル使用メモリ | 189,732 KB |
| 実行使用メモリ | 57,380 KB |
| 最終ジャッジ日時 | 2024-09-24 15:36:56 |
| 合計ジャッジ時間 | 9,724 ms |
|
ジャッジサーバーID (参考情報) |
judge5 / judge4 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| other | AC * 16 WA * 3 |
ソースコード
#include<bits/stdc++.h>
#define rep(i,a,b) for(int i=a;i<b;i++)
using namespace std; void _main(); int main(){cin.tie(0);ios::sync_with_stdio(0);_main();}
//---------------------------------------------------------------------------------------------------
// vid : id -> vid head, heavy, parent : unknown
// depth : id -> depth inv : vid -> id
struct HLDecomposition {
vector<set<int>> g; vector<int> vid, head, heavy, parent, depth, inv;
HLDecomposition(int n) : g(n), vid(n, -1), head(n), heavy(n, -1), parent(n), depth(n), inv(n) {}
void add(int u, int v) { g[u].insert(v); g[v].insert(u); } void build() { dfs(0, -1); bfs(); }
int dfs(int curr, int prev) { parent[curr] = prev; int sub = 1, max_sub = 0;
for (int next : g[curr]) if (next != prev) { depth[next] = depth[curr] + 1;
int sub_next = dfs(next, curr); sub += sub_next;
if (max_sub < sub_next) max_sub = sub_next, heavy[curr] = next; }return sub;}
void bfs() { int k = 0; queue<int> q({ 0 }); while (!q.empty()) { int h = q.front(); q.pop();
for (int i = h; i != -1; i = heavy[i]) {vid[i] = k++; inv[vid[i]] = i; head[i] = h;
for (int j : g[i]) if (j != parent[i] && j != heavy[i]) q.push(j);}}}
void foreach(int u, int v, function<void(int, int)> f) {
if (vid[u] > vid[v]) swap(u, v); f(max(vid[head[v]], vid[u]), vid[v]);
if (head[u] != head[v]) foreach(u, parent[head[v]], f);}
int ancestor(int u, int d) { while (true) { if (depth[head[u]] > depth[u] - d) {
d -= depth[u] - depth[head[u]] + 1; if (head[u] == 0) return 0; u = parent[head[u]];}
else return inv[vid[u] - d];} } // uのd個上の頂点を返す(無いなら0)
int lca(int u, int v) { if (vid[u] > vid[v]) swap(u, v); if (head[u] == head[v]) return u;
return lca(u, parent[head[v]]); }
int distance(int u, int v) { return depth[u] + depth[v] - 2 * depth[lca(u, v)]; }};
typedef long long ll;
template<class V, int NV> struct LazySegTree { // [L,R)
#define def 0
#define ldef 0
V comp(V l, V r) { return l + r; }
void setLazy(int i, V v) { lazy[i] += v; }
void push(int k, int l, int r) {
dat[k] += lazy[k] * (r - l);
if (r - l > 1) { setLazy(k * 2 + 1, lazy[k]); setLazy(k * 2 + 2, lazy[k]); }
lazy[k] = ldef;
}
vector<V> dat, lazy;
LazySegTree() { dat.resize(NV * 2, def); lazy.resize(NV * 2, ldef); }
void update(int a, int b, V v, int k, int l, int r) {
push(k, l, r); if (r <= a || b <= l) return;
if (a <= l && r <= b) { setLazy(k, v); push(k, l, r); }
else {
update(a, b, v, k * 2 + 1, l, (l + r) / 2);
update(a, b, v, k * 2 + 2, (l + r) / 2, r); dat[k] = comp(dat[k * 2 + 1], dat[k * 2 + 2]);
}
}
int get(int a, int b, int k, int l, int r) {
push(k, l, r); if (r <= a || b <= l) return def;
if (a <= l && r <= b) return dat[k]; auto x = get(a, b, k * 2 + 1, l, (l + r) / 2);
auto y = get(a, b, k * 2 + 2, (l + r) / 2, r); return comp(x, y);
}
void update(int a, int b, V v) { update(a, b, v, 0, 0, NV); }
int get(int a, int b) { return get(a, b, 0, 0, NV); }
void debug() {
printf("[OH]\n");
rep(i, 0, 32) printf("%d ", get(i, i + 1));
printf("\n");
/*
set<int> que;
rep(i, 0, 32) que.insert(i + NV - 1);
printf("[dat]\n");
while (1 < que.size()) {
set<int> ne;
for (int i : que) {
printf("%d ", dat[i]);
ne.insert(i / 2);
}
printf("\n");
for (int i : que) {
printf("%d ", i);
ne.insert(i / 2);
}
printf("\n");
swap(que, ne);
}
printf("[lazy]\n");
que.clear();
rep(i, 0, 32) que.insert(i + NV);
while (1 < que.size()) {
set<int> ne;
for (int i : que) {
printf("%d ", lazy[i]);
ne.insert(i / 2);
}
printf("\n");
swap(que, ne);
}*/
}
};
/*---------------------------------------------------------------------------------------------------
∧_∧
∧_∧ (´<_` ) Welcome to My Coding Space!
( ´_ゝ`) / ⌒i
/ \ | |
/ / ̄ ̄ ̄ ̄/ |
__(__ニつ/ _/ .| .|____
\/____/ (u ⊃
---------------------------------------------------------------------------------------------------*/
ll ans = 0;
LazySegTree<ll,1 << 20> st;
//---------------------------------------------------------------------------------------------------
void _main() {
int N; cin >> N;
HLDecomposition hld(N);
rep(i, 0, N - 1) {
int a, b; cin >> a >> b; a--; b--;
hld.add(a, b);
}
hld.build();
st.update(0, N, 1);
int Q; cin >> Q;
rep(q, 0, Q) {
int a, b; cin >> a >> b; a--; b--;
hld.foreach(a, b, [](int x, int y) {
ans += st.get(x, y + 1);
st.update(x, y + 1, 1);
});
}
cout << ans << endl;
}
はまやんはまやん