結果

問題 No.399 動的な領主
ユーザー pekempeypekempey
提出日時 2016-07-19 05:21:17
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 606 ms / 2,000 ms
コード長 3,109 bytes
コンパイル時間 1,344 ms
コンパイル使用メモリ 169,988 KB
実行使用メモリ 14,720 KB
最終ジャッジ日時 2024-04-25 09:04:19
合計ジャッジ時間 6,576 ms
ジャッジサーバーID
(参考情報)
judge3 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
5,248 KB
testcase_01 AC 1 ms
5,376 KB
testcase_02 AC 2 ms
5,376 KB
testcase_03 AC 2 ms
5,376 KB
testcase_04 AC 4 ms
5,376 KB
testcase_05 AC 35 ms
5,376 KB
testcase_06 AC 525 ms
13,952 KB
testcase_07 AC 488 ms
13,952 KB
testcase_08 AC 502 ms
14,080 KB
testcase_09 AC 504 ms
13,952 KB
testcase_10 AC 6 ms
5,376 KB
testcase_11 AC 26 ms
5,376 KB
testcase_12 AC 350 ms
14,720 KB
testcase_13 AC 348 ms
14,592 KB
testcase_14 AC 79 ms
14,336 KB
testcase_15 AC 139 ms
14,336 KB
testcase_16 AC 222 ms
12,928 KB
testcase_17 AC 536 ms
13,952 KB
testcase_18 AC 606 ms
13,952 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.cpp: In function ‘int main()’:
main.cpp:139:22: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
  139 |                 scanf("%d %d", &u, &v);
      |                 ~~~~~^~~~~~~~~~~~~~~~~
main.cpp:152:22: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
  152 |                 scanf("%d %d", &u, &v);
      |                 ~~~~~^~~~~~~~~~~~~~~~~

ソースコード

diff #

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

class HLDecomposition { 
public:
	HLDecomposition(int n) :
		g(n),
		vid(n),
		sub(n, 1),
		head(n),
		heavy(n, -1),
		parent(n) { }

	void add(int u, int v) {
		g[u].push_back(v);
		g[v].push_back(u);
	}

	void build() {
		dfs(0, -1);
		bfs();
	}

	struct Iterator {
		int u, v;
		HLDecomposition *hl;
		
		Iterator(int u, int v, HLDecomposition *hl) : u(u), v(v), hl(hl) {}

		pair<int, int> operator*() {
			if (hl->vid[u] > hl->vid[v]) swap(u, v);
			int l = max(hl->vid[hl->head[v]], hl->vid[u]);
			int r = hl->vid[v];
			v = hl->head[u] != hl->head[v] ? hl->parent[hl->head[v]] : -1;
			return make_pair(l, r + 1);
		}

		void operator++() {}

		bool operator !=(Iterator &) {
			return v != -1;
		}
	};

	struct Enumerator {
		int u, v;
		HLDecomposition *hl;
		Enumerator(HLDecomposition *hl, int u, int v) : u(u), v(v), hl(hl) {}

		Iterator begin() const {
			return Iterator(u, v, hl);
		}

		Iterator end() const {
			return Iterator(u, v, hl);
		}
	};

	Enumerator enumerate(int u, int v) {
		return Enumerator(this, u, v);
	}

private:
	vector<vector<int>> g;
	vector<int> vid;
	vector<int> sub;
	vector<int> head;
	vector<int> heavy;
	vector<int> parent;

	void dfs(int curr, int prev) {
		parent[curr] = prev;
		for (int next : g[curr]) if (next != prev) {
			dfs(next, curr);
			sub[curr] += sub[next];
			if (heavy[curr] == -1 || sub[heavy[curr]] < sub[next]) heavy[curr] = next;
		}
	}

	void bfs() {
		int k = 0;
		queue<int> q;
		q.push(0);
		while (!q.empty()) {
			int first = q.front(); q.pop();
			for (int curr = first; curr != -1; curr = heavy[curr]) {
				vid[curr] = k++;
				head[curr] = first;
				for (int next : g[curr]) if (next != parent[curr] && next != heavy[curr]) {
					q.push(next);
				}
			}
		}
	}
};

const int N = 1 << 17;
long long seg[N * 2];
long long lazy[N * 2];

void push(int k, int l, int r) {
	if (lazy[k] == 0) return;
	seg[k] += lazy[k] * (r - l);
	if (r - l > 1) {
		lazy[k * 2 + 1] += lazy[k];
		lazy[k * 2 + 2] += lazy[k];
	}
	lazy[k] = 0;
}

void update(int a, int b, long long v, int k = 0, int l = 0, int r = N) {
	push(k, l, r);
	if (r <= a || b <= l) return;
	if (a <= l && r <= b) {
		lazy[k] = v;
		push(k, l, r);
		return;
	}
	update(a, b, v, k * 2 + 1, l, (l + r) / 2);
	update(a, b, v, k * 2 + 2, (l + r) / 2, r);
	seg[k] = seg[k * 2 + 1] + seg[k * 2 + 2];
}

long long query(int a, int b, int k = 0, int l = 0, int r = N) {
	push(k, l, r);
	if (r <= a || b <= l) return 0;
	if (a <= l && r <= b) return seg[k];
	return query(a, b, k * 2 + 1, l, (l + r) / 2) + query(a, b, k * 2 + 2, (l + r) / 2, r);
}

int main() {
	int n;
	cin >> n;

	HLDecomposition hl(n);

	for (int i = 0; i < n - 1; i++) {
		int u, v;
		scanf("%d %d", &u, &v);
		hl.add(u - 1, v - 1);
	}

	hl.build();

	update(0, n, 1);

	int Q;
	cin >> Q;
	long long res = 0;
	while (Q--) {
		int u, v;
		scanf("%d %d", &u, &v);
		u--;
		v--;
		
		for (auto lr : hl.enumerate(u, v)) {
			res += query(lr.first, lr.second);
		}

		for (auto lr : hl.enumerate(u, v)) {
			update(lr.first, lr.second, 1);
		}
	}
	cout << res << endl;
}
0