結果

問題 No.399 動的な領主
ユーザー kazumakazuma
提出日時 2017-06-25 03:59:12
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 583 ms / 2,000 ms
コード長 2,894 bytes
コンパイル時間 2,205 ms
コンパイル使用メモリ 182,096 KB
実行使用メモリ 20,352 KB
最終ジャッジ日時 2024-04-14 23:30:21
合計ジャッジ時間 8,760 ms
ジャッジサーバーID
(参考情報)
judge2 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,812 KB
testcase_01 AC 2 ms
6,940 KB
testcase_02 AC 2 ms
6,940 KB
testcase_03 AC 2 ms
6,944 KB
testcase_04 AC 4 ms
6,940 KB
testcase_05 AC 36 ms
6,940 KB
testcase_06 AC 580 ms
15,232 KB
testcase_07 AC 574 ms
15,232 KB
testcase_08 AC 575 ms
15,488 KB
testcase_09 AC 580 ms
15,488 KB
testcase_10 AC 5 ms
6,940 KB
testcase_11 AC 27 ms
6,940 KB
testcase_12 AC 415 ms
15,872 KB
testcase_13 AC 401 ms
15,840 KB
testcase_14 AC 146 ms
20,224 KB
testcase_15 AC 195 ms
20,352 KB
testcase_16 AC 268 ms
17,920 KB
testcase_17 AC 583 ms
15,360 KB
testcase_18 AC 569 ms
15,616 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
using ll = long long;

class HLDecomposition {
	vector<vector<int>> g;
	vector<int> vid, head, heavy, parent, depth, inv;
	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);
			}
		}
	}
public:
	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].push_back(v);
		g[v].push_back(u);
	}
	void build() {
		dfs(0, -1);
		bfs();
	}
	void for_each(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]) for_each(u, parent[head[v]], f);
	}
	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)];
	}
};

template <typename T>
class LazySegmentTree {
	const int n;
	const T id;
	vector<T> data, data2;
	int size(int n) {
		int res = 1;
		while (res < n) res <<= 1;
		return res;
	}
	T sub(int l, int r, int node, int lb, int ub) {
		if (ub <= l || r <= lb) return id;
		if (l <= lb && ub <= r) {
			return data[node] + data2[node] * (ub - lb);
		}
		return data2[node] * (min(r, ub) - max(l, lb))
			+ sub(l, r, node * 2, lb, (lb + ub) / 2)
			+ sub(l, r, node * 2 + 1, (lb + ub) / 2, ub);
	}
	void suc(int l, int r, int node, int lb, int ub, T val) {
		if (ub <= l || r <= lb) return;
		if (l <= lb && ub <= r) {
			data2[node] += val;
			return;
		}
		data[node] += val * (min(r, ub) - max(l, lb));
		suc(l, r, node * 2, lb, (lb + ub) / 2, val);
		suc(l, r, node * 2 + 1, (lb + ub) / 2, ub, val);
	}
public:
	LazySegmentTree(int n_) :
		n(size(n_)), id(0), data(n * 2, id), data2(n * 2, id) {}
	void add(int l, int r, T val) {
		suc(l, r + 1, 1, 0, n, val);
	}
	T getSum(int l, int r) {
		return sub(l, r + 1, 1, 0, n);
	}
};

int main()
{
	ll N, Q, A, B;
	cin >> N;
	HLDecomposition hl(N);
	for (int i = 0, u, v; i < N - 1; i++) {
		cin >> u >> v;
		hl.add(u - 1, v - 1);
	}
	hl.build();
	LazySegmentTree<ll> lst(N);
	cin >> Q;
	ll res = 0;
	while (Q--) {
		cin >> A >> B; A--; B--;
		hl.for_each(A, B, [&](int l, int r) {
			res += lst.getSum(l, r);
		});
		res += hl.distance(A, B) + 1;
		hl.for_each(A, B, [&](int l, int r) {
			lst.add(l, r, 1);
		});
	}
	cout << res << endl;
	return 0;
}
0