結果

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

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
5,248 KB
testcase_01 AC 2 ms
5,376 KB
testcase_02 AC 2 ms
5,376 KB
testcase_03 AC 2 ms
5,376 KB
testcase_04 AC 2 ms
5,376 KB
testcase_05 AC 12 ms
5,376 KB
testcase_06 AC 190 ms
12,416 KB
testcase_07 AC 185 ms
12,416 KB
testcase_08 AC 194 ms
12,544 KB
testcase_09 AC 189 ms
12,416 KB
testcase_10 AC 3 ms
5,376 KB
testcase_11 AC 11 ms
5,376 KB
testcase_12 AC 146 ms
13,056 KB
testcase_13 AC 145 ms
13,056 KB
testcase_14 AC 64 ms
15,616 KB
testcase_15 AC 67 ms
15,744 KB
testcase_16 AC 91 ms
14,208 KB
testcase_17 AC 200 ms
12,544 KB
testcase_18 AC 190 ms
12,416 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.cpp: In function ‘int main()’:
main.cpp:165:22: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
  165 |                 scanf("%d %d", &u, &v);
      |                 ~~~~~^~~~~~~~~~~~~~~~~
main.cpp:179:22: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
  179 |                 scanf("%d %d", &u, &v);
      |                 ~~~~~^~~~~~~~~~~~~~~~~

ソースコード

diff #

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

double elapsed() {
	static clock_t curr;
	clock_t prev = curr;
	curr = clock();
	return (double)(curr - prev) / CLOCKS_PER_SEC * 1000;
}

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(HLDecomposition *hl, int u, int v) : hl(hl), u(u), v(v) {}

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

		void operator++() {}

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

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

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

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

	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]) continue;
					if (next == heavy[curr]) continue;
					q.push(next);
				}
			}
		}
	}
};

struct BIT {
	vector<long long> bit;

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

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

	long long query(int k) {
		k++;
		long long res = 0;
		while (k > 0) {
			res += bit[k];
			k -= k & -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;

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

	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--;
		
		for (auto lr : hl.enumerate(u, v)) {
			res += rsq.query(lr.first, lr.second);
		}

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