답안 #743885

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
743885 2023-05-18T05:47:17 Z pavement Tourism (JOI23_tourism) C++17
28 / 100
5000 ms 25004 KB
#include <bits/stdc++.h>
using namespace std;

#define pb push_back
#define mp make_pair
#define mt make_tuple

using ii = pair<int, int>;
using iiii = tuple<int, int, int, int>;

const int BLK_SZ = 3;
int N, M, Q, wL, wR = -1, idx, cur, sv_cur, out[100005], C[100005], L[100005], R[100005], pre[100005], dep[100005], ll[100005], rr[100005], anc[25][100005];
iiii T[100005];
vector<int> adj[100005];
multiset<ii> S;

void dfs(int n, int e = -1) {
	pre[n] = ++idx;
	for (int k = 1; k <= 20; k++) {
		if (anc[k - 1][n] == -1) break;
		anc[k][n] = anc[k - 1][anc[k - 1][n]];
	}
	for (auto u : adj[n]) if (u != e) {
		anc[0][u] = n;
		dep[u] = dep[n] + 1;
		dfs(u, n);
	}
}

int lca(int u, int v) {
	if (dep[u] > dep[v]) swap(u, v);
	for (int k = 20; k >= 0; k--) {
		if (dep[v] - (1 << k) >= dep[u]) {
			v = anc[k][v];
		}
	}
	if (u == v) return u;
	for (int k = 20; k >= 0; k--) {
		if (anc[k][u] != anc[k][v]) {
			u = anc[k][u];
			v = anc[k][v];
		}
	}
	return anc[0][u];
}

int dist(int u, int v) {
	int w = lca(u, v);
	return dep[u] + dep[v] - 2 * dep[w];
}

void add_path(int u, int v) {
	cur += dist(u, v);
}

void rem_path(int u, int v) {
	cur -= dist(u, v);	
}

void add(int k) {
	S.emplace(pre[k], k);
	auto it = S.find(mp(pre[k], k));
	ii x = mp(-1, -1), y = mp(-1, -1);
	if (it != S.begin()) {
		--it;
		x = *it;
		++it;
	}
	++it;
	if (it != S.end()) {
		y = *it;
	}
	--it;
	if (x.first != -1 && y.first != -1) {
		rem_path(x.second, y.second);
	} else if (x.first == -1 && y.first != -1) {
		auto it2 = --S.end();
		rem_path(y.second, it2->second);
	} else if (y.first == -1 && x.first != -1) {
		auto it2 = S.begin();
		rem_path(x.second, it2->second);
	}
	if (x.first == -1 && y.first == -1) {}
	else if (x.first == -1) {
		auto it2 = --S.end();
		add_path(it2->second, k);
		add_path(k, y.second);
	} else if (y.first == -1) {
		auto it2 = S.begin();
		add_path(x.second, k);
		add_path(k, it2->second);
	} else {
		add_path(x.second, k);
		add_path(k, y.second);
	}
}

void rem(int k) {
	S.erase(S.find(mp(pre[k], k)));
}

int main() {
	memset(anc, -1, sizeof anc);
	ios::sync_with_stdio(0);
	cin.tie(0);
	cin >> N >> M >> Q;
	for (int i = 1; i <= M; i++) {
		if (ll[i / BLK_SZ] == 0) ll[i / BLK_SZ] = i;
		rr[i / BLK_SZ] = i;
	}
	for (int i = 1, u, v; i < N; i++) {
		cin >> u >> v;
		adj[u].pb(v);
		adj[v].pb(u);
	}
	dfs(1);
	for (int i = 1; i <= M; i++) {
		cin >> C[i];
	}
	for (int i = 1; i <= Q; i++) {
		cin >> L[i] >> R[i];
		T[i] = mt(L[i] / BLK_SZ, R[i], L[i], i);
	}
	sort(T + 1, T + 1 + Q);
	for (int i = 1; i <= Q; i++) {
		auto [_, r, l, idx] = T[i];
		L[i] = l;
		R[i] = r;
		if (L[i] / BLK_SZ == R[i] / BLK_SZ) {
			continue;
		}
		// move window to [L[i], R[i]]
		if (wR == -1 || wL / BLK_SZ - 1 != L[i] / BLK_SZ) {
			for (int j = wL; j <= wR; j++) rem(C[j]);
			cur = 0;
			for (int j = rr[L[i] / BLK_SZ] + 1; j <= R[i]; j++) add(C[j]);
			sv_cur = cur;
			for (int j = rr[L[i] / BLK_SZ]; j >= L[i]; j--) add(C[j]);
			out[idx] = cur / 2 + 1;
			for (int j = rr[L[i] / BLK_SZ]; j >= L[i]; j--) rem(C[j]);
			cur = sv_cur;
			wL = rr[L[i] / BLK_SZ] + 1;
			wR = R[i];
			continue;
		}
		for (int j = wR + 1; j <= R[i]; j++) add(C[j]);
		sv_cur = cur;
		for (int j = rr[L[i] / BLK_SZ]; j >= L[i]; j--) add(C[j]);
		out[idx] = cur / 2 + 1;
		for (int j = rr[L[i] / BLK_SZ]; j >= L[i]; j--) rem(C[j]);
		cur = sv_cur;
		wL = rr[L[i] / BLK_SZ] + 1;
		wR = R[i];
	}
	for (int i = wL; i <= wR; i++) rem(C[i]);
	cur = 0;
	for (int i = 1; i <= Q; i++) {
		if (L[i] / BLK_SZ == R[i] / BLK_SZ) {
			for (int j = L[i]; j <= R[i]; j++) add(C[j]);
			out[get<3>(T[i])] = cur / 2 + 1;
			for (int j = L[i]; j <= R[i]; j++) rem(C[j]);
			cur = 0;
		}
	}
	for (int i = 1; i <= Q; i++) {
		cout << out[i] << '\n';
	}
}
# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 12500 KB Output is correct
2 Correct 6 ms 12500 KB Output is correct
3 Correct 7 ms 12500 KB Output is correct
4 Correct 10 ms 12500 KB Output is correct
5 Correct 8 ms 12500 KB Output is correct
6 Correct 8 ms 12460 KB Output is correct
7 Correct 8 ms 12500 KB Output is correct
8 Correct 9 ms 12500 KB Output is correct
9 Correct 12 ms 12500 KB Output is correct
10 Correct 11 ms 12500 KB Output is correct
11 Correct 12 ms 12540 KB Output is correct
12 Correct 7 ms 12500 KB Output is correct
13 Correct 8 ms 12500 KB Output is correct
14 Correct 8 ms 12476 KB Output is correct
15 Correct 11 ms 12500 KB Output is correct
16 Correct 12 ms 12432 KB Output is correct
17 Correct 11 ms 12500 KB Output is correct
18 Correct 11 ms 12504 KB Output is correct
19 Correct 12 ms 12500 KB Output is correct
20 Correct 14 ms 12476 KB Output is correct
21 Correct 11 ms 12500 KB Output is correct
22 Correct 12 ms 12500 KB Output is correct
23 Correct 11 ms 12500 KB Output is correct
24 Correct 11 ms 12500 KB Output is correct
25 Correct 12 ms 12500 KB Output is correct
26 Correct 11 ms 12480 KB Output is correct
27 Correct 8 ms 12516 KB Output is correct
28 Correct 6 ms 12500 KB Output is correct
29 Correct 6 ms 12520 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 12500 KB Output is correct
2 Correct 6 ms 12500 KB Output is correct
3 Correct 7 ms 12500 KB Output is correct
4 Correct 10 ms 12500 KB Output is correct
5 Correct 8 ms 12500 KB Output is correct
6 Correct 8 ms 12460 KB Output is correct
7 Correct 8 ms 12500 KB Output is correct
8 Correct 9 ms 12500 KB Output is correct
9 Correct 12 ms 12500 KB Output is correct
10 Correct 11 ms 12500 KB Output is correct
11 Correct 12 ms 12540 KB Output is correct
12 Correct 7 ms 12500 KB Output is correct
13 Correct 8 ms 12500 KB Output is correct
14 Correct 8 ms 12476 KB Output is correct
15 Correct 11 ms 12500 KB Output is correct
16 Correct 12 ms 12432 KB Output is correct
17 Correct 11 ms 12500 KB Output is correct
18 Correct 11 ms 12504 KB Output is correct
19 Correct 12 ms 12500 KB Output is correct
20 Correct 14 ms 12476 KB Output is correct
21 Correct 11 ms 12500 KB Output is correct
22 Correct 12 ms 12500 KB Output is correct
23 Correct 11 ms 12500 KB Output is correct
24 Correct 11 ms 12500 KB Output is correct
25 Correct 12 ms 12500 KB Output is correct
26 Correct 11 ms 12480 KB Output is correct
27 Correct 8 ms 12516 KB Output is correct
28 Correct 6 ms 12500 KB Output is correct
29 Correct 6 ms 12520 KB Output is correct
30 Correct 147 ms 12680 KB Output is correct
31 Correct 187 ms 12708 KB Output is correct
32 Correct 288 ms 12764 KB Output is correct
33 Correct 276 ms 12772 KB Output is correct
34 Correct 275 ms 12860 KB Output is correct
35 Correct 31 ms 12780 KB Output is correct
36 Correct 31 ms 12764 KB Output is correct
37 Correct 32 ms 12740 KB Output is correct
38 Correct 238 ms 12844 KB Output is correct
39 Correct 238 ms 12860 KB Output is correct
40 Correct 228 ms 12852 KB Output is correct
41 Correct 27 ms 12756 KB Output is correct
42 Correct 30 ms 12884 KB Output is correct
43 Correct 29 ms 12856 KB Output is correct
44 Correct 246 ms 12816 KB Output is correct
45 Correct 253 ms 12764 KB Output is correct
46 Correct 245 ms 12808 KB Output is correct
47 Correct 28 ms 12756 KB Output is correct
48 Correct 29 ms 12780 KB Output is correct
49 Correct 28 ms 12776 KB Output is correct
50 Correct 261 ms 12780 KB Output is correct
51 Correct 274 ms 12776 KB Output is correct
52 Correct 258 ms 12776 KB Output is correct
53 Correct 258 ms 12792 KB Output is correct
54 Correct 276 ms 12860 KB Output is correct
55 Correct 278 ms 12776 KB Output is correct
56 Correct 126 ms 12668 KB Output is correct
57 Correct 7 ms 12628 KB Output is correct
58 Correct 9 ms 12612 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 12500 KB Output is correct
2 Correct 8 ms 12500 KB Output is correct
3 Correct 147 ms 12640 KB Output is correct
4 Execution timed out 5046 ms 25004 KB Time limit exceeded
5 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 12500 KB Output is correct
2 Correct 66 ms 15072 KB Output is correct
3 Correct 98 ms 15664 KB Output is correct
4 Correct 90 ms 15616 KB Output is correct
5 Correct 221 ms 21816 KB Output is correct
6 Correct 203 ms 18708 KB Output is correct
7 Correct 199 ms 17636 KB Output is correct
8 Correct 182 ms 17316 KB Output is correct
9 Correct 195 ms 17100 KB Output is correct
10 Correct 177 ms 17268 KB Output is correct
11 Correct 173 ms 17312 KB Output is correct
12 Correct 148 ms 17252 KB Output is correct
13 Correct 143 ms 17396 KB Output is correct
14 Correct 121 ms 18036 KB Output is correct
15 Correct 83 ms 19952 KB Output is correct
16 Correct 191 ms 19540 KB Output is correct
17 Correct 184 ms 19496 KB Output is correct
18 Correct 185 ms 19472 KB Output is correct
19 Correct 157 ms 21780 KB Output is correct
20 Correct 159 ms 19444 KB Output is correct
21 Correct 158 ms 18088 KB Output is correct
22 Correct 175 ms 17632 KB Output is correct
23 Correct 185 ms 17400 KB Output is correct
24 Correct 208 ms 17252 KB Output is correct
25 Correct 225 ms 17204 KB Output is correct
26 Correct 212 ms 17148 KB Output is correct
27 Correct 222 ms 17192 KB Output is correct
28 Correct 250 ms 17156 KB Output is correct
29 Correct 221 ms 17120 KB Output is correct
30 Correct 198 ms 17292 KB Output is correct
31 Correct 192 ms 17484 KB Output is correct
32 Correct 178 ms 17816 KB Output is correct
33 Correct 136 ms 18676 KB Output is correct
34 Correct 85 ms 20092 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 12500 KB Output is correct
2 Correct 8 ms 12500 KB Output is correct
3 Correct 118 ms 12656 KB Output is correct
4 Execution timed out 5079 ms 22176 KB Time limit exceeded
5 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 12500 KB Output is correct
2 Correct 6 ms 12500 KB Output is correct
3 Correct 7 ms 12500 KB Output is correct
4 Correct 10 ms 12500 KB Output is correct
5 Correct 8 ms 12500 KB Output is correct
6 Correct 8 ms 12460 KB Output is correct
7 Correct 8 ms 12500 KB Output is correct
8 Correct 9 ms 12500 KB Output is correct
9 Correct 12 ms 12500 KB Output is correct
10 Correct 11 ms 12500 KB Output is correct
11 Correct 12 ms 12540 KB Output is correct
12 Correct 7 ms 12500 KB Output is correct
13 Correct 8 ms 12500 KB Output is correct
14 Correct 8 ms 12476 KB Output is correct
15 Correct 11 ms 12500 KB Output is correct
16 Correct 12 ms 12432 KB Output is correct
17 Correct 11 ms 12500 KB Output is correct
18 Correct 11 ms 12504 KB Output is correct
19 Correct 12 ms 12500 KB Output is correct
20 Correct 14 ms 12476 KB Output is correct
21 Correct 11 ms 12500 KB Output is correct
22 Correct 12 ms 12500 KB Output is correct
23 Correct 11 ms 12500 KB Output is correct
24 Correct 11 ms 12500 KB Output is correct
25 Correct 12 ms 12500 KB Output is correct
26 Correct 11 ms 12480 KB Output is correct
27 Correct 8 ms 12516 KB Output is correct
28 Correct 6 ms 12500 KB Output is correct
29 Correct 6 ms 12520 KB Output is correct
30 Correct 147 ms 12680 KB Output is correct
31 Correct 187 ms 12708 KB Output is correct
32 Correct 288 ms 12764 KB Output is correct
33 Correct 276 ms 12772 KB Output is correct
34 Correct 275 ms 12860 KB Output is correct
35 Correct 31 ms 12780 KB Output is correct
36 Correct 31 ms 12764 KB Output is correct
37 Correct 32 ms 12740 KB Output is correct
38 Correct 238 ms 12844 KB Output is correct
39 Correct 238 ms 12860 KB Output is correct
40 Correct 228 ms 12852 KB Output is correct
41 Correct 27 ms 12756 KB Output is correct
42 Correct 30 ms 12884 KB Output is correct
43 Correct 29 ms 12856 KB Output is correct
44 Correct 246 ms 12816 KB Output is correct
45 Correct 253 ms 12764 KB Output is correct
46 Correct 245 ms 12808 KB Output is correct
47 Correct 28 ms 12756 KB Output is correct
48 Correct 29 ms 12780 KB Output is correct
49 Correct 28 ms 12776 KB Output is correct
50 Correct 261 ms 12780 KB Output is correct
51 Correct 274 ms 12776 KB Output is correct
52 Correct 258 ms 12776 KB Output is correct
53 Correct 258 ms 12792 KB Output is correct
54 Correct 276 ms 12860 KB Output is correct
55 Correct 278 ms 12776 KB Output is correct
56 Correct 126 ms 12668 KB Output is correct
57 Correct 7 ms 12628 KB Output is correct
58 Correct 9 ms 12612 KB Output is correct
59 Correct 6 ms 12500 KB Output is correct
60 Correct 8 ms 12500 KB Output is correct
61 Correct 147 ms 12640 KB Output is correct
62 Execution timed out 5046 ms 25004 KB Time limit exceeded
63 Halted 0 ms 0 KB -