結果

問題 No.399 動的な領主
ユーザー akusyounin2412akusyounin2412
提出日時 2018-03-16 22:23:56
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 730 ms / 2,000 ms
コード長 6,244 bytes
コンパイル時間 1,935 ms
コンパイル使用メモリ 154,524 KB
実行使用メモリ 24,892 KB
最終ジャッジ日時 2024-06-01 09:38:57
合計ジャッジ時間 8,944 ms
ジャッジサーバーID
(参考情報)
judge4 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 6 ms
11,488 KB
testcase_01 AC 7 ms
11,392 KB
testcase_02 AC 7 ms
11,376 KB
testcase_03 AC 7 ms
11,396 KB
testcase_04 AC 10 ms
11,636 KB
testcase_05 AC 50 ms
12,696 KB
testcase_06 AC 711 ms
23,736 KB
testcase_07 AC 708 ms
23,832 KB
testcase_08 AC 707 ms
24,028 KB
testcase_09 AC 698 ms
24,024 KB
testcase_10 AC 12 ms
11,628 KB
testcase_11 AC 40 ms
12,868 KB
testcase_12 AC 512 ms
24,740 KB
testcase_13 AC 486 ms
24,536 KB
testcase_14 AC 157 ms
24,752 KB
testcase_15 AC 231 ms
24,892 KB
testcase_16 AC 331 ms
24,708 KB
testcase_17 AC 730 ms
24,024 KB
testcase_18 AC 701 ms
24,144 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

# include <iostream>
# include <algorithm>
#include <array>
# include <cassert>
#include <cctype>
#include <climits>
#include <numeric>
# include <vector>
# include <string>
# include <set>
# include <map>
# include <cmath>
# include <iomanip>
# include <functional>
# include <tuple>
# include <utility>
# include <stack>
# include <queue>
# include <list>
# include <bitset>
# include <complex>
# include <chrono>
# include <random>
# include <limits.h>
# include <unordered_map>
# include <unordered_set>
# include <deque>
# include <cstdio>
# include <cstring>
using namespace std;
using LL = long long;
using ULL = unsigned long long;
constexpr long long MOD = 1000000000 + 7;
constexpr long long INF = std::numeric_limits<long long>::max();
const double PI = acos(-1);
#define fir first
#define sec second
typedef pair<LL, LL> Pll;
typedef pair<LL, pair<LL, LL>> Ppll;
typedef pair<LL, pair<LL, bitset<100001>>> Pbll;
typedef pair<LL, pair<LL, vector<LL>>> Pvll;
typedef pair<LL, LL> Vec2;
struct Tll { LL first, second, third; };
typedef pair<LL, Tll> Ptll;
#define rep(i,rept) for(LL i=0;i<rept;i++)
#define Mfor(i,mf) for(LL i=mf-1;i>=0;i--)
LL h, w, n, m, k, s, t, q, ans, sum, last, cnt;
struct Edge { int to,cost; };
string str;
bool f;
void YN(bool f) {
	if (f)
		cout << "YES" << endl;
	else
		cout << "NO" << endl;
}
void yn(bool f) {
	if (f)
		cout << "Yes" << endl;
	else
		cout << "No" << endl;
}
struct Seg {
	LL n;
	vector<LL>node, lazy;
	Seg(vector<LL>v) {
		n = 1; while (n < v.size())n *= 2;
		node.resize(n * 2 - 1);
		lazy.resize(n * 2 - 1);
		rep(i, n)node[n + i - 1] = v[i];
		Mfor(i, n - 2)node[i] = min(node[i * 2 + 1], node[i * 2 + 2]);
		//rep(i, n * 2 - 1)lazy[i] = -1;
	}
	Seg(LL n_) {
		n = 1; while (n < n_)n *= 2;
		node.resize(n * 2 - 1);
		lazy.resize(n * 2 - 1);
		rep(i, n)node[n + i - 1] = 0;
		Mfor(i, n - 2)node[i] = min(node[i * 2 + 1], node[i * 2 + 2]);
		//区間最小などの時
		//rep(i, n * 2 - 1)lazy[i] = -1;
	}
	//区間の和の遅延
	void eval(LL k, LL l, LL r) {
		if (lazy[k] != 0) {
			node[k] += lazy[k];
			if (r - l >= 1) {
				lazy[k * 2 + 1] += lazy[k] / 2;
				lazy[k * 2 + 2] += lazy[k] / 2;
			}
			lazy[k] = 0;
		}
	}
	//区間の最小の遅延
	void ceval(LL k, LL l, LL r) {
		if (lazy[k] != -1) {
			node[k] = lazy[k];
			if (r - l >= 1) {
				lazy[k * 2 + 1] = lazy[k];
				lazy[k * 2 + 2] = lazy[k];
			}
			lazy[k] = -1;
		}
	}
	void update(LL i, LL x) {
		i = i + n - 1;
		node[i] = x;
		while (i > 0) {
			i = (i - 1) / 2;
			node[i] = min(node[i * 2 + 1], node[i * 2 + 2]);
		}
	}
	void update(LL i, LL x, LL a, LL b, LL l, LL r) {
		//ceval(i, a, b);
		if (b < l || r < a)return;
		if (l <= a&&b <= r) {
			lazy[i] = x;
			ceval(i, a, b);
		}
		else {
			update(i * 2 + 1, x, a, (a + b) / 2, l, r);
			update(i * 2 + 2, x, (a + b + 1) / 2, b, l, r);
			node[i] = min(node[i * 2 + 1], node[i * 2 + 2]);
		}
	}
	LL find(LL i, LL a, LL b, LL l, LL r) {
		ceval(i, a, b);
		if (b< l || r < a)return INF;
		if (l <= a&&b <= r)return node[i];
		return min(find(i * 2 + 1, a, (a + b) / 2, l, r), find(i * 2 + 2, (a + b + 1) / 2, b, l, r));
	}
	void add(LL i, LL x) {
		i = i + n - 1;
		node[i] += x;
		while (i > 0) {
			i = (i - 1) / 2;
			node[i] = (node[i * 2 + 1] + node[i * 2 + 2]);
		}
	}
	void add(LL i, LL x, LL a, LL b, LL l, LL r) {
		eval(i, a, b);
		if (b < l || r < a)return;
		if (l <= a&&b <= r) {
			lazy[i] += (b - a + 1)*x;
			eval(i, a, b);
		}
		else {
			add(i * 2 + 1, x, a, (a + b) / 2, l, r);
			add(i * 2 + 2, x, (a + b + 1) / 2, b, l, r);
			node[i] = node[i * 2 + 1] + node[i * 2 + 2];
		}
	}
	LL getsum(LL i, LL a, LL b, LL l, LL r) {
		eval(i, a, b);
		if (b< l || r < a)return 0;
		if (l <= a&&b <= r)return node[i];
		return (getsum(i * 2 + 1, a, (a + b) / 2, l, r) + getsum(i * 2 + 2, (a + b + 1) / 2, b, l, r));
	}
};
Seg seg(200000);
struct HLD {
	LL n, pos;
	vector<vector<LL> > G;
	vector<LL> vid, head, sub, hvy, par, dep, inv, type;

	HLD() {}
	HLD(LL sz) :
		n(sz), pos(0), G(n),
		vid(n, -1), head(n), sub(n, 1), hvy(n, -1),
		par(n), dep(n), inv(n), type(n) {}

	void add_edge(LL u, LL v) {
		G[u].push_back(v);
		G[v].push_back(u);
	}

	void build(vector<LL> rs = vector<LL>(1, 0)) {
		LL c = 0;
		for (LL r : rs) {
			dfs(r);
			bfs(r, c++);
		}
	}

	void dfs(LL rt) {
		using T = pair<LL, LL>;
		stack<T> st;
		par[rt] = -1;
		dep[rt] = 0;
		st.emplace(rt, 0);
		while (!st.empty()) {
			LL v = st.top().first;
			LL &i = st.top().second;
			if (i<(LL)G[v].size()) {
				LL u = G[v][i++];
				if (u == par[v]) continue;
				par[u] = v;
				dep[u] = dep[v] + 1;
				st.emplace(u, 0);
			}
			else {
				st.pop();
				LL res = 0;
				for (LL u : G[v]) {
					if (u == par[v]) continue;
					sub[v] += sub[u];
					if (res<sub[u]) res = sub[u], hvy[v] = u;
				}
			}
		}
	}

	void bfs(LL r, LL c) {
		LL &k = pos;
		queue<LL> q({ r });
		while (!q.empty()) {
			LL h = q.front(); q.pop();
			for (LL i = h; i != -1; i = hvy[i]) {
				type[i] = c;
				vid[i] = k++;
				inv[vid[i]] = i;
				head[i] = h;
				for (LL j : G[i])
					if (j != par[i] && j != hvy[i]) q.push(j);
			}
		}
	}

	// for_each(vertex)
	// [l,r] <- attention!!, const function<void(LL, LL)>& f
	void for_each(LL u, LL v, LL x) {
		while (1) {
			if (vid[u] > vid[v]) swap(u, v);
			seg.add(0, x, 0, seg.n - 1, max(vid[head[v]], vid[u]), vid[v]);
			ans+=seg.getsum(0, 0, seg.n - 1, max(vid[head[v]], vid[u]), vid[v]);
			if (head[u] != head[v]) v = par[head[v]];
			else break;
		}
	}

	// for_each(edge)
	// [l,r] <- attention!!
	void for_each_edge(LL u, LL v, const function<void(LL, LL)>& f) {
		while (1) {
			if (vid[u]>vid[v]) swap(u, v);
			if (head[u] != head[v]) {
				f(vid[head[v]], vid[v]);
				v = par[head[v]];
			}
			else {
				if (u != v) f(vid[u] + 1, vid[v]);
				break;
			}
		}
	}

	LL lca(LL u, LL v) {
		while (1) {
			if (vid[u]>vid[v]) swap(u, v);
			if (head[u] == head[v]) return u;
			v = par[head[v]];
		}
	}

	LL distance(LL u, LL v) {
		return dep[u] + dep[v] - 2 * dep[lca(u, v)];
	}
};
int main() {
	cin >> n;
	HLD hl(n);
	rep(i, n-1) {
		LL x, y;
		cin >> x >> y;
		hl.add_edge(x-1, y-1);
	}
	hl.build();
	cin >> q;
	rep(i, q) {
		int x, y;
		cin >> x >> y;
		hl.for_each(x-1, y-1, 1);
	}
	cout << ans << endl;
	return 0;
}
0