結果

問題 No.399 動的な領主
ユーザー akusyounin2412akusyounin2412
提出日時 2018-03-16 22:19:30
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 6,287 bytes
コンパイル時間 2,082 ms
コンパイル使用メモリ 155,220 KB
実行使用メモリ 20,852 KB
最終ジャッジ日時 2024-06-01 09:38:44
合計ジャッジ時間 9,646 ms
ジャッジサーバーID
(参考情報)
judge2 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 7 ms
11,396 KB
testcase_01 AC 7 ms
11,384 KB
testcase_02 AC 7 ms
11,492 KB
testcase_03 AC 7 ms
11,444 KB
testcase_04 AC 10 ms
11,512 KB
testcase_05 AC 52 ms
12,308 KB
testcase_06 WA -
testcase_07 WA -
testcase_08 WA -
testcase_09 WA -
testcase_10 AC 12 ms
11,636 KB
testcase_11 AC 40 ms
12,312 KB
testcase_12 WA -
testcase_13 WA -
testcase_14 WA -
testcase_15 WA -
testcase_16 WA -
testcase_17 WA -
testcase_18 WA -
権限があれば一括ダウンロードができます

ソースコード

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--)
int 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 {
	int n, pos;
	vector<vector<int> > G;
	vector<int> vid, head, sub, hvy, par, dep, inv, type;

	HLD() {}
	HLD(int 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(int u, int v) {
		G[u].push_back(v);
		G[v].push_back(u);
	}

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

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

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

	// for_each(vertex)
	// [l,r] <- attention!!, const function<void(int, int)>& f
	void for_each(int u, int v, int 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(int u, int v, const function<void(int, int)>& 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;
			}
		}
	}

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

	int distance(int u, int v) {
		return dep[u] + dep[v] - 2 * dep[lca(u, v)];
	}
};
int main() {
	cin >> n;
	HLD hl(n);
	rep(i, n-1) {
		int 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