結果
| 問題 |
No.399 動的な領主
|
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2019-10-02 17:51:54 |
| 言語 | C++14 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
AC
|
| 実行時間 | 750 ms / 2,000 ms |
| コード長 | 7,047 bytes |
| コンパイル時間 | 2,159 ms |
| コンパイル使用メモリ | 192,160 KB |
| 実行使用メモリ | 22,912 KB |
| 最終ジャッジ日時 | 2024-10-03 06:01:41 |
| 合計ジャッジ時間 | 10,571 ms |
|
ジャッジサーバーID (参考情報) |
judge5 / judge2 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| other | AC * 19 |
ソースコード
#include"bits/stdc++.h"
using namespace std;
#define REP(k,m,n) for(int (k)=(m);(k)<(n);(k)++)
#define rep(i,n) REP((i),0,(n))
using ll = long long;
using Graph = vector<vector<int>>;
struct HLDecomposition {
using pii = pair<int, int>;
int n;
Graph G;
vector<int> vid, inv, par, depth, subsize, head, prev, next, type;
HLDecomposition(const Graph& G_) :
n(G_.size()), G(G_),
vid(n, -1), inv(n), par(n), depth(n), subsize(n, 1),
head(n), prev(n, -1), next(n, -1), type(n) {}
void build(vector<int> roots = { 0 }) {
int curtype = 0, pos = 0;
for (int root : roots) {
decide_heavy_edge(root);
reconstruct(root, curtype++, pos);
}
}
void decide_heavy_edge(int root) {
stack<pii> st;
par[root] = -1, depth[root] = 0;
st.emplace(root, 0);
while (!st.empty()) {
int now = st.top().first;
int& way = st.top().second;
if (way < G[now].size()) {
int child = G[now][way++];
if (child == par[now])continue;
par[child] = now;
depth[child] = depth[now] + 1;
st.emplace(child, 0);
}
else {
st.pop();
int maxsize = 0;
for (auto child : G[now]) {
if (child == par[now])continue;
subsize[now] += subsize[child];
if (maxsize < subsize[child]) {
maxsize = subsize[child];
prev[child] = now;
next[now] = child;
}
}
}
}
}
void reconstruct(int root, int curtype, int& pos) {
stack<int> st({ root });
while (!st.empty()) {
int start = st.top(); st.pop();
for (int v = start; v != -1; v = next[v]) {
type[v] = curtype;
vid[v] = pos++;
inv[vid[v]] = v;
head[v] = start;
for (auto child : G[v]) {
if (child != par[v] && child != next[v]) {
st.push(child);
}
}
}
}
}
// node query [u, v], f([left, right])
void foreach_nodes(int u, int v, const function<void(int, int)>& f) {
while (true) {
if (vid[u] > vid[v])swap(u, v);
f(max(vid[head[v]], vid[u]), vid[v]);
if (head[u] != head[v])v = par[head[v]];
else break;
}
}
// edge query[u,v] f([left, right])
// seg_node[vid[i]] := edge(par[i] -> i)
void foreach_edges(int u, int v, const function<void(int, int)>& f) {
while (true) {
if (vid[u] > vid[v])swap(u, v);
if (head[u] != head[v]) {
f(vid[head[v]], vid[v]);
v = par[head[v]];
}
else {
if (u != v)f(vid[u] + 1, vid[v]);
break;
}
}
}
int lca(int u, int v) {
while (true) {
if (vid[u] > vid[v])swap(u, v);
if (head[u] == head[v])return u;
v = par[head[v]];
}
}
};
template<typename Monoid, typename OperatorMonoid = Monoid>
class LazySegmentTree {
private:
using F = function<Monoid(Monoid, Monoid)>;
using G = function<Monoid(Monoid, OperatorMonoid, int)>;
using H = function<OperatorMonoid(OperatorMonoid, OperatorMonoid)>;
int sz; // 対応する配列の幅
vector<Monoid> data;
vector<OperatorMonoid> lazy;
const F f; // 2区間マージ演算(data-data-ボトムアップマージ)
const G g; // 要素,作用素マージ演算(lazy->data同位置変換時の、(data,lazy,len)の計算)
const H h; // 作用素マージ演算 (query->lazyトップダウン伝搬時の、(lazy,query_value)の計算)
const Monoid M1; // モノイド単位元 (data単位元)
const OperatorMonoid OM0; // 作用素単位元 (lazy単位元)
void propagate(int idx, int len) {
// 幅lenのlazy[idx]が存在するとき、値を下に流す
if (lazy[idx] != OM0) {
if (idx < sz) {
lazy[(idx << 1) | 0] = h(lazy[(idx << 1) | 0], lazy[idx]);
lazy[(idx << 1) | 1] = h(lazy[(idx << 1) | 1], lazy[idx]);
}
data[idx] = g(data[idx], lazy[idx], len);
lazy[idx] = OM0;
}
}
Monoid update_impl(int a, int b, const OperatorMonoid& val, int idx, int l, int r) {
propagate(idx, r - l);
if (r <= a || b <= l)return data[idx];
else if (a <= l && r <= b) {
lazy[idx] = h(lazy[idx], val);
propagate(idx, r - l);
return data[idx];
}
else return data[idx] = f(
update_impl(a, b, val, (idx << 1) | 0, l, (l + r) >> 1),
update_impl(a, b, val, (idx << 1) | 1, (l + r) >> 1, r)
);
}
Monoid query_impl(int a, int b, int idx, int l, int r) {
propagate(idx, r - l);
if (r <= a || b <= l)return M1;
else if (a <= l && r <= b)return data[idx];
else return f(
query_impl(a, b, (idx << 1) | 0, l, (l + r) >> 1),
query_impl(a, b, (idx << 1) | 1, (l + r) >> 1, r)
);
}
public:
// init忘れに注意
LazySegmentTree(int n, const F f, const G g, const H h,
const Monoid& M1, const OperatorMonoid OM0)
:f(f), g(g), h(h), M1(M1), OM0(OM0) {
sz = 1;
while (sz < n)sz <<= 1;
data.assign(2 * sz, M1);
lazy.assign(2 * sz, OM0);
}
void build(const vector<Monoid>& vals) {
rep(idx, vals.size())data[idx + sz] = vals[idx];
for (int idx = sz - 1; idx > 0; idx--) {
data[idx] = f(data[(idx << 1) | 0], data[(idx << 1) | 1]);
}
}
Monoid update(int a, int b, const OperatorMonoid& val) {
return update_impl(a, b, val, 1, 0, sz);
}
Monoid query(int a, int b) {
return query_impl(a, b, 1, 0, sz);
}
Monoid operator[](const int& idx) {
return query(idx, idx + 1);
}
};
int main()
{
int N, Q;
cin >> N;
Graph graph(N);
rep(i, N - 1) {
int u, v;
cin >> u >> v;
u--; v--;
graph[u].push_back(v);
graph[v].push_back(u);
}
auto f = [](ll vl, ll vr) {return vl + vr; };
auto g = [](ll data, ll lazy, int len) {return data + lazy * len; };
auto h = [](ll lazy, ll query) {return lazy + query; };
LazySegmentTree<ll> lst(N, f, g, h, 0, 0);
HLDecomposition hld(graph);
hld.build();
ll res = 0;
cin >> Q;
rep(i, Q) {
int A, B;
cin >> A >> B;
A--; B--;
hld.foreach_nodes(A, B, [&](int l, int r) {
lst.update(l, r + 1, 1);
ll tres = lst.query(l, r + 1);
res += tres;
});
}
cout << res << endl;
return 0;
}