結果

問題 No.399 動的な領主
ユーザー pekempeypekempey
提出日時 2016-07-19 13:56:13
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 265 ms / 2,000 ms
コード長 2,256 bytes
コンパイル時間 1,959 ms
コンパイル使用メモリ 171,500 KB
実行使用メモリ 20,224 KB
最終ジャッジ日時 2024-04-25 09:08:18
合計ジャッジ時間 5,375 ms
ジャッジサーバーID
(参考情報)
judge5 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 4 ms
6,016 KB
testcase_01 AC 4 ms
5,760 KB
testcase_02 AC 4 ms
5,888 KB
testcase_03 AC 3 ms
5,888 KB
testcase_04 AC 4 ms
5,888 KB
testcase_05 AC 17 ms
6,528 KB
testcase_06 AC 263 ms
12,544 KB
testcase_07 AC 254 ms
12,416 KB
testcase_08 AC 260 ms
12,544 KB
testcase_09 AC 260 ms
12,416 KB
testcase_10 AC 5 ms
5,760 KB
testcase_11 AC 15 ms
6,656 KB
testcase_12 AC 197 ms
13,056 KB
testcase_13 AC 184 ms
12,928 KB
testcase_14 AC 72 ms
20,224 KB
testcase_15 AC 75 ms
20,224 KB
testcase_16 AC 104 ms
16,384 KB
testcase_17 AC 265 ms
12,416 KB
testcase_18 AC 259 ms
12,416 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.cpp: In function ‘int main()’:
main.cpp:106:22: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
  106 |                 scanf("%d %d", &u, &v);
      |                 ~~~~~^~~~~~~~~~~~~~~~~
main.cpp:122:22: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
  122 |                 scanf("%d %d", &u, &v);
      |                 ~~~~~^~~~~~~~~~~~~~~~~

ソースコード

diff #

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

vector<int> g[101010];

namespace HL {
	int vid[101010];
	int head[101010];
	int parent[101010];
	int heavy[101010];
	int sub[101010];

	void dfs(int curr, int prev) {
		parent[curr] = prev;
		sub[curr] = 1;
		vid[curr] = -1;
		pair<int, int> h(-1, -1);
		for (int next : g[curr]) {
			if (next == prev) continue;
			dfs(next, curr);
			sub[curr] += sub[next];
			h = max(h, { sub[next], next });
		}
		heavy[curr] = h.second;
	}

	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++;
				head[i] = h;
				for (int j : g[i]) {
					if (j == parent[i]) continue;
					if (j == heavy[i]) continue;
					q.push(j);
				}
			}
		}
	}

	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);
	}
}

struct BIT {
	vector<long long> bit;

	BIT(int n) : bit(n) {}

	void update(int k, long long v) {
		for (k++; k < bit.size(); k += k & -k) {
			bit[k] += v;
		}
	}

	long long query(int k) {
		long long res = 0;
		for (k++; k > 0; k -= k & -k) {
			res += bit[k];
		}
		return res;
	}
};

struct RangeSumQuery {
	BIT bit0, bit1;

	RangeSumQuery(int n) : bit0(n), bit1(n) {}

	// [a, b]
	void update(int a, int b, long long x) {
		bit0.update(a, -x * (a - 1));
		bit1.update(a, x);
		bit0.update(b + 1, x * b);
		bit1.update(b + 1, -x);
	}

	// [0, a]
	long long query(int a) {
		return bit0.query(a) + bit1.query(a) * a;
	}

	// [a, b]
	long long query(int a, int b) {
		return query(b) - query(a - 1);
	}
};

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

	for (int i = 0; i < n - 1; i++) {
		int u, v;
		scanf("%d %d", &u, &v);
		u--;
		v--;
		g[u].push_back(v);
		g[v].push_back(u);
	}
	HL::build();

	RangeSumQuery rsq(n + 10);
	rsq.update(0, n - 1, 1);

	int Q;
	cin >> Q;
	long long res = 0;
	while (Q--) {
		int u, v;
		scanf("%d %d", &u, &v);
		u--;
		v--;

		HL::for_each(u, v, [&](int l, int r) {
			res += rsq.query(l, r);
		});

		HL::for_each(u, v, [&](int l, int r) {
			rsq.update(l, r, 1);
		});
	}
	cout << res << endl;
}
0