結果

問題 No.399 動的な領主
ユーザー kazumakazuma
提出日時 2018-07-03 02:50:48
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 193 ms / 2,000 ms
コード長 2,955 bytes
コンパイル時間 2,611 ms
コンパイル使用メモリ 219,416 KB
実行使用メモリ 12,224 KB
最終ジャッジ日時 2023-09-13 17:11:34
合計ジャッジ時間 5,915 ms
ジャッジサーバーID
(参考情報)
judge15 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,380 KB
testcase_01 AC 2 ms
4,380 KB
testcase_02 AC 1 ms
4,376 KB
testcase_03 AC 1 ms
4,376 KB
testcase_04 AC 2 ms
4,376 KB
testcase_05 AC 14 ms
4,376 KB
testcase_06 AC 186 ms
11,580 KB
testcase_07 AC 186 ms
11,456 KB
testcase_08 AC 188 ms
11,664 KB
testcase_09 AC 188 ms
11,584 KB
testcase_10 AC 3 ms
4,376 KB
testcase_11 AC 12 ms
4,380 KB
testcase_12 AC 142 ms
12,000 KB
testcase_13 AC 136 ms
12,084 KB
testcase_14 AC 57 ms
12,224 KB
testcase_15 AC 63 ms
12,100 KB
testcase_16 AC 88 ms
11,860 KB
testcase_17 AC 193 ms
11,628 KB
testcase_18 AC 188 ms
11,516 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

class heavy_light_decomposition {
	const int n;
	vector<vector<int>> g;
	vector<int> in, out, head, par;
public:
	heavy_light_decomposition(int n_)
		: n(n_), g(n), in(n, -1), out(n, -1), head(n), par(n, -1) {}
	heavy_light_decomposition(const vector<vector<int>>& G)
		: n(G.size()), g(G), in(n, -1), out(n, -1), head(n), par(n, -1) {}
	void add_edge(int u, int v) {
		g[u].push_back(v);
		g[v].push_back(u);
	}
	void build(int rt = 0) {
		vector<int> size(n, 1);
		vector<size_t> iter(n);
		stack<pair<int, int>> stp;
		stp.emplace(rt, -1);
		while (!stp.empty()) {
			int v = stp.top().first, prev = stp.top().second;
			if (iter[v] < g[v].size()) {
				if (g[v][iter[v]] != prev) stp.emplace(g[v][iter[v]], v);
				++iter[v];
				continue;
			}
			par[v] = prev;
			for (auto& u : g[v]) if (u == prev) {
				if (u != g[v].back()) swap(u, g[v].back());
				g[v].pop_back();
				break;
			}
			for (auto& u : g[v]) {
				size[v] += size[u];
				if (size[u] > size[g[v].front()]) swap(u, g[v].front());
			}
			stp.pop();
		}
		fill(iter.begin(), iter.end(), 0);
		int it = 0;
		stack<int> st;
		head[rt] = rt;
		st.push(rt);
		while (!st.empty()) {
			int v = st.top();
			if (!iter[v]) in[v] = it++;
			if (iter[v] < g[v].size()) {
				int u = g[v][iter[v]];
				head[u] = iter[v] ? u : head[v];
				st.push(u);
				++iter[v];
				continue;
			}
			out[v] = it;
			st.pop();
		}
	}
	void path_query(int u, int v, function<void(int, int)> f) {
		while (true) {
			if (in[u] > in[v]) swap(u, v);
			f(max(in[head[v]], in[u]), in[v] + 1);
			if (head[u] == head[v]) return;
			v = par[head[v]];
		}
	}
};

template <typename T>
class fenwick_tree {
	const int n;
	vector<T> data;
public:
	fenwick_tree(int n_) : n(n_), data(n) {}
	T find(int p) const {
		T res = 0;
		while (p >= 0) {
			res += data[p];
			p = (p & (p + 1)) - 1;
		}
		return res;
	}
	void add(int p, T val) {
		while (p < n) {
			data[p] += val;
			p |= p + 1;
		}
	}
};

template <typename T>
class range_add_range_sum {
	const int n;
	fenwick_tree<T> bit0, bit1;
public:
	range_add_range_sum(int n_) : n(n_), bit0(n), bit1(n) {}
	T find(int p) const {
		return bit1.find(p) * (p + 1) + bit0.find(p);
	}
	T find(int l, int r) const {
		return find(r - 1) - find(l - 1);
	}
	void add(int l, int r, T val) {
		bit0.add(l, -val * l);
		bit1.add(l, val);
		bit0.add(r, val * r);
		bit1.add(r, -val);
	}
};

int main()
{
	ios::sync_with_stdio(false), cin.tie(0);
	ll N, Q, A, B;
	cin >> N;
	heavy_light_decomposition hl(N);
	for (int i = 0; i < N - 1; i++) {
		int u, v;
		cin >> u >> v; u--, v--;
		hl.add_edge(u, v);
	}
	hl.build();
	range_add_range_sum<ll> ra(N);
	cin >> Q;
	ll res = 0;
	while (Q--) {
		cin >> A >> B; A--; B--;
		hl.path_query(A, B, [&](int l, int r) {
			res += ra.find(l, r) + (r - l);
		});
		hl.path_query(A, B, [&](int l, int r) {
			ra.add(l, r, 1);
		});
	}
	cout << res << endl;
	return 0;
}
0