結果

問題 No.399 動的な領主
ユーザー pekempeypekempey
提出日時 2016-07-19 14:23:36
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 171 ms / 2,000 ms
コード長 2,468 bytes
コンパイル時間 1,406 ms
コンパイル使用メモリ 172,260 KB
実行使用メモリ 25,216 KB
最終ジャッジ日時 2024-04-25 09:08:36
合計ジャッジ時間 3,881 ms
ジャッジサーバーID
(参考情報)
judge3 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 6 ms
10,496 KB
testcase_01 AC 6 ms
10,624 KB
testcase_02 AC 5 ms
10,624 KB
testcase_03 AC 6 ms
10,496 KB
testcase_04 AC 7 ms
10,624 KB
testcase_05 AC 16 ms
11,392 KB
testcase_06 AC 156 ms
19,072 KB
testcase_07 AC 155 ms
19,200 KB
testcase_08 AC 157 ms
19,840 KB
testcase_09 AC 160 ms
19,840 KB
testcase_10 AC 7 ms
10,496 KB
testcase_11 AC 15 ms
11,776 KB
testcase_12 AC 134 ms
22,784 KB
testcase_13 AC 134 ms
22,784 KB
testcase_14 AC 65 ms
25,088 KB
testcase_15 AC 68 ms
25,216 KB
testcase_16 AC 78 ms
23,680 KB
testcase_17 AC 171 ms
19,840 KB
testcase_18 AC 163 ms
19,712 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.cpp: In function ‘int main()’:
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);
      |                 ~~~~~^~~~~~~~~~~~~~~~~
main.cpp:135:22: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
  135 |                 scanf("%d %d", &u, &v);
      |                 ~~~~~^~~~~~~~~~~~~~~~~

ソースコード

diff #

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

vector<int> g[101010];

struct BIT {
	vector<long long> bit;

	void init(int n) {
		bit.resize(n + 1);
	}

	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;

	void init(int n) {
		bit0.init(n);
		bit1.init(n);
		update(0, n - 1, 1);
	}

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

namespace HL {
	const int N = 1e5 + 10;
	int vid[N];
	int head[N];
	int parent[N];
	int heavy[N];
	int sub[N];
	int depth[N];
	RangeSumQuery rsq[N];

	void dfs(int curr, int prev) {
		parent[curr] = prev;
		sub[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, make_pair(sub[next], next));
		}
		heavy[curr] = h.second;
	}

	void bfs() {
		queue<int> q({ 0 });
		while (!q.empty()) {
			int h = q.front(); q.pop();
			int k = 0;
			for (int i = h; i != -1; i = heavy[i]) {
				vid[i] = k++;
				head[i] = h;
				depth[i] = depth[h];
				for (int j : g[i]) {
					if (j == parent[i]) continue;
					if (j == heavy[i]) continue;
					depth[j] = depth[i] + 1;
					q.push(j);
				}
			}
			rsq[h].init(k);
		}
	}

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

	void for_each(int u, int v, function<void(int, int, int)> f) {
		if (head[u] != head[v]) {
			if (depth[u] > depth[v]) swap(u, v);
			f(head[v], 0, vid[v]);
			for_each(u, parent[head[v]], f);
		} else {
			if (vid[u] > vid[v]) swap(u, v);
			f(head[v], vid[u], vid[v]);
		}
	}
}

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

	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 h, int l, int r) {
			res += HL::rsq[h].query(l, r);
		});

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