답안 #944975

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
944975 2024-03-13T09:18:01 Z IBory Tourism (JOI23_tourism) C++17
28 / 100
5000 ms 20816 KB
#include <bits/stdc++.h>
using namespace std;

const int MAX = 100007, SQ = 8;
int D[MAX], S[MAX], P[17][MAX], in[MAX], pin[MAX], cnt[MAX], ans[MAX], dn, edge;
vector<int> G[MAX];
set<int> A;

struct Query {
	int s, e, id;
	Query(int a = 0, int b = 0, int c = 0) {
		s = a, e = b, id = c;
	}
	const bool operator<(Query& a) {
		if ((s >> SQ) != (a.s >> SQ)) return (s >> SQ) < (a.s >> SQ);
		return e > a.e;
	}
} QS[MAX];

void DFS(int cur, int prev) {
	P[0][cur] = prev;
	D[cur] = D[prev] + 1;
	in[cur] = ++dn;
	pin[dn] = cur;
	for (int next : G[cur]) {
		if (next == prev) continue;
		DFS(next, cur);
	}
}

int LCA(int a, int b) {
	if (D[a] > D[b]) swap(a, b);
	for (int i = 16; i >= 0; --i) if ((D[b] - D[a]) & (1 << i)) b = P[i][b];
	if (a == b) return a;
	for (int i = 16; i >= 0; --i) {
		if (P[i][a] == P[i][b]) continue;
		a = P[i][a], b = P[i][b];
	}
	return P[0][a];
}

void Add(int idx) {
	if (!cnt[S[idx]]++) {
		int n = in[S[idx]];
		auto it = A.lower_bound(n);

		int a = (it != A.begin() ? *prev(it) : 0);
		int b = (it != A.end() ? *it : 0);
		if (a && b) edge += D[LCA(pin[a], pin[b])];
		
		it = A.insert(it, n);
		edge += D[S[idx]];
		if (a) edge -= D[LCA(pin[a], S[idx])];
		if (b) edge -= D[LCA(pin[b], S[idx])];
	}
}

void Sub(int idx) {
	if (!--cnt[S[idx]]) {
		int n = in[S[idx]];
		auto it = A.lower_bound(n);

		int a = (it != A.begin() ? *prev(it) : 0);
		int b = (next(it) != A.end() ? *next(it) : 0);
		if (a && b) edge -= D[LCA(pin[a], pin[b])];
		
		A.erase(n);
		edge -= D[S[idx]];
		if (a) edge += D[LCA(pin[a], S[idx])];
		if (b) edge += D[LCA(pin[b], S[idx])];
	}
}

int main() {
	ios::sync_with_stdio(0); cin.tie(0);
	int N, M, Q;
	cin >> N >> M >> Q;
	for (int i = 1; i < N; ++i) {
		int a, b;
		cin >> a >> b;
		G[a].push_back(b);
		G[b].push_back(a);
	}
	for (int i = 1; i <= M; ++i) cin >> S[i];
	for (int i = 0; i < Q; ++i) {
		int s, e;
		cin >> s >> e;
		QS[i] = Query(s, e, i);
	}
	sort(QS, QS + Q);

	DFS(1, 0);
	for (int n = 1; n < 17; ++n) for (int i = 1; i <= N; ++i) P[n][i] = P[n - 1][P[n - 1][i]];

	int L = 1, R = 0;
	for (int d = 0; d < Q; ++d) {
		auto [s, e, id] = QS[d];
		while (R < e) Add(++R);
		while (s < L) Add(--L);
		while (e < R) Sub(R--);
		while (L < s) Sub(L++);
		ans[id] = edge - D[LCA(pin[*A.begin()], pin[*A.rbegin()])] + 1;
	}

	for (int i = 0; i < Q; ++i) cout << ans[i] << '\n';
	return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 12888 KB Output is correct
2 Correct 2 ms 12888 KB Output is correct
3 Correct 2 ms 12924 KB Output is correct
4 Correct 4 ms 12924 KB Output is correct
5 Correct 4 ms 12892 KB Output is correct
6 Correct 4 ms 12892 KB Output is correct
7 Correct 4 ms 12892 KB Output is correct
8 Correct 4 ms 12888 KB Output is correct
9 Correct 5 ms 12888 KB Output is correct
10 Correct 7 ms 12892 KB Output is correct
11 Correct 5 ms 13144 KB Output is correct
12 Correct 2 ms 12892 KB Output is correct
13 Correct 3 ms 12892 KB Output is correct
14 Correct 2 ms 12892 KB Output is correct
15 Correct 6 ms 12896 KB Output is correct
16 Correct 5 ms 12936 KB Output is correct
17 Correct 5 ms 12892 KB Output is correct
18 Correct 6 ms 12892 KB Output is correct
19 Correct 5 ms 12976 KB Output is correct
20 Correct 5 ms 12888 KB Output is correct
21 Correct 5 ms 12892 KB Output is correct
22 Correct 5 ms 12980 KB Output is correct
23 Correct 5 ms 12892 KB Output is correct
24 Correct 5 ms 12956 KB Output is correct
25 Correct 5 ms 12976 KB Output is correct
26 Correct 5 ms 12888 KB Output is correct
27 Correct 2 ms 12892 KB Output is correct
28 Correct 2 ms 12892 KB Output is correct
29 Correct 3 ms 12888 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 12888 KB Output is correct
2 Correct 2 ms 12888 KB Output is correct
3 Correct 2 ms 12924 KB Output is correct
4 Correct 4 ms 12924 KB Output is correct
5 Correct 4 ms 12892 KB Output is correct
6 Correct 4 ms 12892 KB Output is correct
7 Correct 4 ms 12892 KB Output is correct
8 Correct 4 ms 12888 KB Output is correct
9 Correct 5 ms 12888 KB Output is correct
10 Correct 7 ms 12892 KB Output is correct
11 Correct 5 ms 13144 KB Output is correct
12 Correct 2 ms 12892 KB Output is correct
13 Correct 3 ms 12892 KB Output is correct
14 Correct 2 ms 12892 KB Output is correct
15 Correct 6 ms 12896 KB Output is correct
16 Correct 5 ms 12936 KB Output is correct
17 Correct 5 ms 12892 KB Output is correct
18 Correct 6 ms 12892 KB Output is correct
19 Correct 5 ms 12976 KB Output is correct
20 Correct 5 ms 12888 KB Output is correct
21 Correct 5 ms 12892 KB Output is correct
22 Correct 5 ms 12980 KB Output is correct
23 Correct 5 ms 12892 KB Output is correct
24 Correct 5 ms 12956 KB Output is correct
25 Correct 5 ms 12976 KB Output is correct
26 Correct 5 ms 12888 KB Output is correct
27 Correct 2 ms 12892 KB Output is correct
28 Correct 2 ms 12892 KB Output is correct
29 Correct 3 ms 12888 KB Output is correct
30 Correct 25 ms 12888 KB Output is correct
31 Correct 31 ms 13020 KB Output is correct
32 Correct 38 ms 13344 KB Output is correct
33 Correct 38 ms 13104 KB Output is correct
34 Correct 37 ms 13108 KB Output is correct
35 Correct 5 ms 12892 KB Output is correct
36 Correct 5 ms 12892 KB Output is correct
37 Correct 5 ms 12892 KB Output is correct
38 Correct 31 ms 13148 KB Output is correct
39 Correct 31 ms 13144 KB Output is correct
40 Correct 32 ms 13168 KB Output is correct
41 Correct 5 ms 13148 KB Output is correct
42 Correct 5 ms 13148 KB Output is correct
43 Correct 5 ms 13148 KB Output is correct
44 Correct 43 ms 12888 KB Output is correct
45 Correct 37 ms 12892 KB Output is correct
46 Correct 33 ms 12888 KB Output is correct
47 Correct 4 ms 13148 KB Output is correct
48 Correct 5 ms 12892 KB Output is correct
49 Correct 7 ms 13148 KB Output is correct
50 Correct 30 ms 12892 KB Output is correct
51 Correct 38 ms 12892 KB Output is correct
52 Correct 29 ms 12888 KB Output is correct
53 Correct 30 ms 13116 KB Output is correct
54 Correct 30 ms 13116 KB Output is correct
55 Correct 31 ms 13096 KB Output is correct
56 Correct 3 ms 12888 KB Output is correct
57 Correct 3 ms 12892 KB Output is correct
58 Correct 6 ms 12940 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 12888 KB Output is correct
2 Correct 2 ms 12892 KB Output is correct
3 Correct 3 ms 12960 KB Output is correct
4 Execution timed out 5054 ms 20540 KB Time limit exceeded
5 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 12888 KB Output is correct
2 Correct 145 ms 15484 KB Output is correct
3 Correct 220 ms 15636 KB Output is correct
4 Correct 173 ms 16212 KB Output is correct
5 Correct 81 ms 20816 KB Output is correct
6 Correct 102 ms 19284 KB Output is correct
7 Correct 139 ms 18260 KB Output is correct
8 Correct 129 ms 17536 KB Output is correct
9 Correct 135 ms 17104 KB Output is correct
10 Correct 128 ms 17192 KB Output is correct
11 Correct 263 ms 17268 KB Output is correct
12 Correct 266 ms 17236 KB Output is correct
13 Correct 266 ms 17508 KB Output is correct
14 Correct 275 ms 17508 KB Output is correct
15 Correct 299 ms 16224 KB Output is correct
16 Correct 235 ms 17868 KB Output is correct
17 Correct 204 ms 17744 KB Output is correct
18 Correct 239 ms 17904 KB Output is correct
19 Correct 62 ms 19020 KB Output is correct
20 Correct 77 ms 19028 KB Output is correct
21 Correct 107 ms 17492 KB Output is correct
22 Correct 114 ms 16976 KB Output is correct
23 Correct 152 ms 16632 KB Output is correct
24 Correct 146 ms 16352 KB Output is correct
25 Correct 182 ms 16204 KB Output is correct
26 Correct 166 ms 16220 KB Output is correct
27 Correct 266 ms 16220 KB Output is correct
28 Correct 370 ms 15952 KB Output is correct
29 Correct 465 ms 16128 KB Output is correct
30 Correct 451 ms 16464 KB Output is correct
31 Correct 446 ms 16212 KB Output is correct
32 Correct 445 ms 16212 KB Output is correct
33 Correct 430 ms 16172 KB Output is correct
34 Correct 412 ms 16240 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 12892 KB Output is correct
2 Correct 2 ms 12892 KB Output is correct
3 Correct 4 ms 12892 KB Output is correct
4 Execution timed out 5087 ms 16728 KB Time limit exceeded
5 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 12888 KB Output is correct
2 Correct 2 ms 12888 KB Output is correct
3 Correct 2 ms 12924 KB Output is correct
4 Correct 4 ms 12924 KB Output is correct
5 Correct 4 ms 12892 KB Output is correct
6 Correct 4 ms 12892 KB Output is correct
7 Correct 4 ms 12892 KB Output is correct
8 Correct 4 ms 12888 KB Output is correct
9 Correct 5 ms 12888 KB Output is correct
10 Correct 7 ms 12892 KB Output is correct
11 Correct 5 ms 13144 KB Output is correct
12 Correct 2 ms 12892 KB Output is correct
13 Correct 3 ms 12892 KB Output is correct
14 Correct 2 ms 12892 KB Output is correct
15 Correct 6 ms 12896 KB Output is correct
16 Correct 5 ms 12936 KB Output is correct
17 Correct 5 ms 12892 KB Output is correct
18 Correct 6 ms 12892 KB Output is correct
19 Correct 5 ms 12976 KB Output is correct
20 Correct 5 ms 12888 KB Output is correct
21 Correct 5 ms 12892 KB Output is correct
22 Correct 5 ms 12980 KB Output is correct
23 Correct 5 ms 12892 KB Output is correct
24 Correct 5 ms 12956 KB Output is correct
25 Correct 5 ms 12976 KB Output is correct
26 Correct 5 ms 12888 KB Output is correct
27 Correct 2 ms 12892 KB Output is correct
28 Correct 2 ms 12892 KB Output is correct
29 Correct 3 ms 12888 KB Output is correct
30 Correct 25 ms 12888 KB Output is correct
31 Correct 31 ms 13020 KB Output is correct
32 Correct 38 ms 13344 KB Output is correct
33 Correct 38 ms 13104 KB Output is correct
34 Correct 37 ms 13108 KB Output is correct
35 Correct 5 ms 12892 KB Output is correct
36 Correct 5 ms 12892 KB Output is correct
37 Correct 5 ms 12892 KB Output is correct
38 Correct 31 ms 13148 KB Output is correct
39 Correct 31 ms 13144 KB Output is correct
40 Correct 32 ms 13168 KB Output is correct
41 Correct 5 ms 13148 KB Output is correct
42 Correct 5 ms 13148 KB Output is correct
43 Correct 5 ms 13148 KB Output is correct
44 Correct 43 ms 12888 KB Output is correct
45 Correct 37 ms 12892 KB Output is correct
46 Correct 33 ms 12888 KB Output is correct
47 Correct 4 ms 13148 KB Output is correct
48 Correct 5 ms 12892 KB Output is correct
49 Correct 7 ms 13148 KB Output is correct
50 Correct 30 ms 12892 KB Output is correct
51 Correct 38 ms 12892 KB Output is correct
52 Correct 29 ms 12888 KB Output is correct
53 Correct 30 ms 13116 KB Output is correct
54 Correct 30 ms 13116 KB Output is correct
55 Correct 31 ms 13096 KB Output is correct
56 Correct 3 ms 12888 KB Output is correct
57 Correct 3 ms 12892 KB Output is correct
58 Correct 6 ms 12940 KB Output is correct
59 Correct 2 ms 12888 KB Output is correct
60 Correct 2 ms 12892 KB Output is correct
61 Correct 3 ms 12960 KB Output is correct
62 Execution timed out 5054 ms 20540 KB Time limit exceeded
63 Halted 0 ms 0 KB -