Submission #743891

# Submission time Handle Problem Language Result Execution time Memory
743891 2023-05-18T05:52:18 Z pavement Tourism (JOI23_tourism) C++17
28 / 100
5000 ms 48376 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 = 350;
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;
unordered_map<int, int> seen[100005];

void dfs(int n, int e = -1) {
	pre[n] = ++idx;
	for (int k = 1; k <= 17; 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 = 17; k >= 0; k--) {
		if (dep[v] - (1 << k) >= dep[u]) {
			v = anc[k][v];
		}
	}
	if (u == v) return u;
	for (int k = 17; 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) {
	if (seen[u].find(v) != seen[u].end()) return seen[u][v];
	int w = lca(u, v);
	return seen[u][v] = seen[v][u] = 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';
	}
}
# Verdict Execution time Memory Grader output
1 Correct 10 ms 17876 KB Output is correct
2 Correct 10 ms 17876 KB Output is correct
3 Correct 10 ms 17944 KB Output is correct
4 Correct 16 ms 18124 KB Output is correct
5 Correct 13 ms 18004 KB Output is correct
6 Correct 13 ms 18004 KB Output is correct
7 Correct 15 ms 18092 KB Output is correct
8 Correct 13 ms 18072 KB Output is correct
9 Correct 23 ms 18260 KB Output is correct
10 Correct 19 ms 18184 KB Output is correct
11 Correct 24 ms 18156 KB Output is correct
12 Correct 39 ms 18052 KB Output is correct
13 Correct 36 ms 18004 KB Output is correct
14 Correct 33 ms 18060 KB Output is correct
15 Correct 22 ms 18096 KB Output is correct
16 Correct 18 ms 18180 KB Output is correct
17 Correct 19 ms 18168 KB Output is correct
18 Correct 19 ms 18084 KB Output is correct
19 Correct 20 ms 18100 KB Output is correct
20 Correct 20 ms 18132 KB Output is correct
21 Correct 18 ms 18148 KB Output is correct
22 Correct 18 ms 18148 KB Output is correct
23 Correct 18 ms 18084 KB Output is correct
24 Correct 20 ms 18132 KB Output is correct
25 Correct 21 ms 18132 KB Output is correct
26 Correct 19 ms 18152 KB Output is correct
27 Correct 14 ms 17876 KB Output is correct
28 Correct 12 ms 17900 KB Output is correct
29 Correct 11 ms 17912 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 10 ms 17876 KB Output is correct
2 Correct 10 ms 17876 KB Output is correct
3 Correct 10 ms 17944 KB Output is correct
4 Correct 16 ms 18124 KB Output is correct
5 Correct 13 ms 18004 KB Output is correct
6 Correct 13 ms 18004 KB Output is correct
7 Correct 15 ms 18092 KB Output is correct
8 Correct 13 ms 18072 KB Output is correct
9 Correct 23 ms 18260 KB Output is correct
10 Correct 19 ms 18184 KB Output is correct
11 Correct 24 ms 18156 KB Output is correct
12 Correct 39 ms 18052 KB Output is correct
13 Correct 36 ms 18004 KB Output is correct
14 Correct 33 ms 18060 KB Output is correct
15 Correct 22 ms 18096 KB Output is correct
16 Correct 18 ms 18180 KB Output is correct
17 Correct 19 ms 18168 KB Output is correct
18 Correct 19 ms 18084 KB Output is correct
19 Correct 20 ms 18100 KB Output is correct
20 Correct 20 ms 18132 KB Output is correct
21 Correct 18 ms 18148 KB Output is correct
22 Correct 18 ms 18148 KB Output is correct
23 Correct 18 ms 18084 KB Output is correct
24 Correct 20 ms 18132 KB Output is correct
25 Correct 21 ms 18132 KB Output is correct
26 Correct 19 ms 18152 KB Output is correct
27 Correct 14 ms 17876 KB Output is correct
28 Correct 12 ms 17900 KB Output is correct
29 Correct 11 ms 17912 KB Output is correct
30 Correct 85 ms 19232 KB Output is correct
31 Correct 109 ms 19212 KB Output is correct
32 Correct 136 ms 19672 KB Output is correct
33 Correct 128 ms 19532 KB Output is correct
34 Correct 126 ms 19676 KB Output is correct
35 Correct 238 ms 18516 KB Output is correct
36 Correct 232 ms 18520 KB Output is correct
37 Correct 285 ms 18516 KB Output is correct
38 Correct 140 ms 19664 KB Output is correct
39 Correct 127 ms 19660 KB Output is correct
40 Correct 125 ms 19548 KB Output is correct
41 Correct 251 ms 18612 KB Output is correct
42 Correct 283 ms 18616 KB Output is correct
43 Correct 273 ms 18580 KB Output is correct
44 Correct 126 ms 19600 KB Output is correct
45 Correct 133 ms 19616 KB Output is correct
46 Correct 143 ms 19544 KB Output is correct
47 Correct 256 ms 18540 KB Output is correct
48 Correct 237 ms 18536 KB Output is correct
49 Correct 242 ms 18516 KB Output is correct
50 Correct 129 ms 19552 KB Output is correct
51 Correct 137 ms 19484 KB Output is correct
52 Correct 133 ms 19468 KB Output is correct
53 Correct 125 ms 19612 KB Output is correct
54 Correct 134 ms 19532 KB Output is correct
55 Correct 143 ms 19552 KB Output is correct
56 Correct 64 ms 18072 KB Output is correct
57 Correct 10 ms 18004 KB Output is correct
58 Correct 12 ms 18012 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 9 ms 17876 KB Output is correct
2 Correct 13 ms 17988 KB Output is correct
3 Correct 63 ms 18088 KB Output is correct
4 Execution timed out 5042 ms 48376 KB Time limit exceeded
5 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 9 ms 17876 KB Output is correct
2 Correct 114 ms 31020 KB Output is correct
3 Correct 164 ms 37388 KB Output is correct
4 Correct 130 ms 34044 KB Output is correct
5 Correct 333 ms 42808 KB Output is correct
6 Correct 329 ms 42736 KB Output is correct
7 Correct 304 ms 43160 KB Output is correct
8 Correct 278 ms 43272 KB Output is correct
9 Correct 286 ms 43192 KB Output is correct
10 Correct 271 ms 42676 KB Output is correct
11 Correct 254 ms 42304 KB Output is correct
12 Correct 258 ms 41948 KB Output is correct
13 Correct 242 ms 40676 KB Output is correct
14 Correct 183 ms 37900 KB Output is correct
15 Correct 111 ms 25256 KB Output is correct
16 Correct 302 ms 40548 KB Output is correct
17 Correct 323 ms 40396 KB Output is correct
18 Correct 287 ms 40400 KB Output is correct
19 Correct 310 ms 42784 KB Output is correct
20 Correct 324 ms 42428 KB Output is correct
21 Correct 327 ms 42780 KB Output is correct
22 Correct 346 ms 43160 KB Output is correct
23 Correct 344 ms 43452 KB Output is correct
24 Correct 315 ms 43296 KB Output is correct
25 Correct 351 ms 43216 KB Output is correct
26 Correct 382 ms 42820 KB Output is correct
27 Correct 332 ms 42444 KB Output is correct
28 Correct 321 ms 42256 KB Output is correct
29 Correct 320 ms 42056 KB Output is correct
30 Correct 284 ms 41572 KB Output is correct
31 Correct 256 ms 40868 KB Output is correct
32 Correct 233 ms 39300 KB Output is correct
33 Correct 183 ms 35048 KB Output is correct
34 Correct 90 ms 25220 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 10 ms 17876 KB Output is correct
2 Correct 16 ms 17888 KB Output is correct
3 Correct 62 ms 18132 KB Output is correct
4 Execution timed out 5066 ms 45016 KB Time limit exceeded
5 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 10 ms 17876 KB Output is correct
2 Correct 10 ms 17876 KB Output is correct
3 Correct 10 ms 17944 KB Output is correct
4 Correct 16 ms 18124 KB Output is correct
5 Correct 13 ms 18004 KB Output is correct
6 Correct 13 ms 18004 KB Output is correct
7 Correct 15 ms 18092 KB Output is correct
8 Correct 13 ms 18072 KB Output is correct
9 Correct 23 ms 18260 KB Output is correct
10 Correct 19 ms 18184 KB Output is correct
11 Correct 24 ms 18156 KB Output is correct
12 Correct 39 ms 18052 KB Output is correct
13 Correct 36 ms 18004 KB Output is correct
14 Correct 33 ms 18060 KB Output is correct
15 Correct 22 ms 18096 KB Output is correct
16 Correct 18 ms 18180 KB Output is correct
17 Correct 19 ms 18168 KB Output is correct
18 Correct 19 ms 18084 KB Output is correct
19 Correct 20 ms 18100 KB Output is correct
20 Correct 20 ms 18132 KB Output is correct
21 Correct 18 ms 18148 KB Output is correct
22 Correct 18 ms 18148 KB Output is correct
23 Correct 18 ms 18084 KB Output is correct
24 Correct 20 ms 18132 KB Output is correct
25 Correct 21 ms 18132 KB Output is correct
26 Correct 19 ms 18152 KB Output is correct
27 Correct 14 ms 17876 KB Output is correct
28 Correct 12 ms 17900 KB Output is correct
29 Correct 11 ms 17912 KB Output is correct
30 Correct 85 ms 19232 KB Output is correct
31 Correct 109 ms 19212 KB Output is correct
32 Correct 136 ms 19672 KB Output is correct
33 Correct 128 ms 19532 KB Output is correct
34 Correct 126 ms 19676 KB Output is correct
35 Correct 238 ms 18516 KB Output is correct
36 Correct 232 ms 18520 KB Output is correct
37 Correct 285 ms 18516 KB Output is correct
38 Correct 140 ms 19664 KB Output is correct
39 Correct 127 ms 19660 KB Output is correct
40 Correct 125 ms 19548 KB Output is correct
41 Correct 251 ms 18612 KB Output is correct
42 Correct 283 ms 18616 KB Output is correct
43 Correct 273 ms 18580 KB Output is correct
44 Correct 126 ms 19600 KB Output is correct
45 Correct 133 ms 19616 KB Output is correct
46 Correct 143 ms 19544 KB Output is correct
47 Correct 256 ms 18540 KB Output is correct
48 Correct 237 ms 18536 KB Output is correct
49 Correct 242 ms 18516 KB Output is correct
50 Correct 129 ms 19552 KB Output is correct
51 Correct 137 ms 19484 KB Output is correct
52 Correct 133 ms 19468 KB Output is correct
53 Correct 125 ms 19612 KB Output is correct
54 Correct 134 ms 19532 KB Output is correct
55 Correct 143 ms 19552 KB Output is correct
56 Correct 64 ms 18072 KB Output is correct
57 Correct 10 ms 18004 KB Output is correct
58 Correct 12 ms 18012 KB Output is correct
59 Correct 9 ms 17876 KB Output is correct
60 Correct 13 ms 17988 KB Output is correct
61 Correct 63 ms 18088 KB Output is correct
62 Execution timed out 5042 ms 48376 KB Time limit exceeded
63 Halted 0 ms 0 KB -