답안 #972967

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
972967 2024-05-01T11:03:24 Z THXuan Tourism (JOI23_tourism) C++14
28 / 100
5000 ms 24148 KB
#include <iostream>
#include <vector>
#include <algorithm>
#include <queue>
#include <stack>
#include <set>
#include <map>
#define INF 1e9
using namespace std;
typedef long long ll;

int timer = 0, ans = 0;
vector<int>adj[100005], g[100005];
int tin[100005], tout[100005], d[100005], p[100005][20], c[100005];
map<pair<int,int>, int>seen;

void dfs(int s, int e) {
	for (int i = 1; i <= 18; i++) {
		p[s][i] = p[p[s][i - 1]][i - 1];
	}
	++timer;
	tin[s] = timer;
	for (auto u : adj[s]) {
		if (u == e) continue;
		d[u] = d[s] + 1;
		p[u][0] = s;
		dfs(u, s);
	}
	tout[s] = timer;
}

int lca(int a, int b) {
	if (d[a] > d[b]) swap(a, b);
	int dis = d[b] - d[a];
	for (int i = 0; i <= 18; i++) {
		if (dis & (1 << i)) b = p[b][i];
	}
	if (a == b) return a;
	for (int i = 18; i >= 0; i--) {
		if (p[a][i] != p[b][i]) {
			a = p[a][i];
			b = p[b][i];
		}
	}
	return p[a][0];
}

bool ancestor(int x, int y) {
	return tin[x] <= tin[y] && tout[y] <= tout[x];
}

bool comp(int x, int y) {
	return tin[x] < tin[y];
}

void dfs1(int s) {
	for (auto u : g[s]) {
		ans += d[u] - d[s];
		dfs1(u);
	}
}

void build(vector<int>& a) {
	sort(a.begin(), a.end(), comp);
	int sz = a.size();
	for (int i = 0; i < sz - 1; i++) {
		a.push_back(lca(a[i], a[i + 1]));
	}
	sort(a.begin(), a.end(), comp);
	a.resize(unique(a.begin(), a.end()) - a.begin());
	stack<int>stk;
	for (auto u : a) {
		while (stk.size() && !ancestor(stk.top(), u))stk.pop();
		if (stk.size()) g[stk.top()].push_back(u);
		stk.push(u);
	}
	//for (int i = 0; i < a.size(); i++) cout << a[i] << " ";
	dfs1(a[0]);
	for (auto u : a) g[u].clear();
}

void solve()
{
	int n, m, q; cin >> n >> m >> q;
	for (int i = 1; i <= n - 1; i++) {
		int u, v; cin >> u >> v;
		adj[u].push_back(v);
		adj[v].push_back(u);
	}
	dfs(1, 0);
	for (int i = 1; i <= m; i++) cin >> c[i];
	while (q--) {
		int l, r; cin >> l >> r;
		if (seen[{l, r}] != 0)cout << seen[{l, r}] << "\n";
		else if (l == r) { cout << 1 << "\n"; }
		else {
			vector<int>a;
			ans = 0;
			for (int i = l; i <= r; i++) a.push_back(c[i]);
			build(a);
			cout << ans + 1 << "\n";
			seen[{l, r}] = ans + 1;
		}
	}
}

int main()
{
	ios_base::sync_with_stdio(0);
	cin.tie(0);
	int t = 1;// cin>>t;
	while (t--) solve();
	return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 5976 KB Output is correct
2 Correct 1 ms 5980 KB Output is correct
3 Correct 2 ms 5980 KB Output is correct
4 Correct 6 ms 5980 KB Output is correct
5 Correct 4 ms 6224 KB Output is correct
6 Correct 5 ms 5976 KB Output is correct
7 Correct 5 ms 5980 KB Output is correct
8 Correct 4 ms 5980 KB Output is correct
9 Correct 8 ms 6240 KB Output is correct
10 Correct 8 ms 6236 KB Output is correct
11 Correct 8 ms 6236 KB Output is correct
12 Correct 18 ms 6236 KB Output is correct
13 Correct 17 ms 6232 KB Output is correct
14 Correct 21 ms 6236 KB Output is correct
15 Correct 7 ms 6232 KB Output is correct
16 Correct 6 ms 6236 KB Output is correct
17 Correct 7 ms 6260 KB Output is correct
18 Correct 7 ms 6236 KB Output is correct
19 Correct 7 ms 6236 KB Output is correct
20 Correct 7 ms 6236 KB Output is correct
21 Correct 6 ms 6236 KB Output is correct
22 Correct 6 ms 6236 KB Output is correct
23 Correct 6 ms 6236 KB Output is correct
24 Correct 7 ms 6232 KB Output is correct
25 Correct 7 ms 6236 KB Output is correct
26 Correct 6 ms 6236 KB Output is correct
27 Correct 3 ms 5980 KB Output is correct
28 Correct 2 ms 5976 KB Output is correct
29 Correct 2 ms 5976 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 5976 KB Output is correct
2 Correct 1 ms 5980 KB Output is correct
3 Correct 2 ms 5980 KB Output is correct
4 Correct 6 ms 5980 KB Output is correct
5 Correct 4 ms 6224 KB Output is correct
6 Correct 5 ms 5976 KB Output is correct
7 Correct 5 ms 5980 KB Output is correct
8 Correct 4 ms 5980 KB Output is correct
9 Correct 8 ms 6240 KB Output is correct
10 Correct 8 ms 6236 KB Output is correct
11 Correct 8 ms 6236 KB Output is correct
12 Correct 18 ms 6236 KB Output is correct
13 Correct 17 ms 6232 KB Output is correct
14 Correct 21 ms 6236 KB Output is correct
15 Correct 7 ms 6232 KB Output is correct
16 Correct 6 ms 6236 KB Output is correct
17 Correct 7 ms 6260 KB Output is correct
18 Correct 7 ms 6236 KB Output is correct
19 Correct 7 ms 6236 KB Output is correct
20 Correct 7 ms 6236 KB Output is correct
21 Correct 6 ms 6236 KB Output is correct
22 Correct 6 ms 6236 KB Output is correct
23 Correct 6 ms 6236 KB Output is correct
24 Correct 7 ms 6232 KB Output is correct
25 Correct 7 ms 6236 KB Output is correct
26 Correct 6 ms 6236 KB Output is correct
27 Correct 3 ms 5980 KB Output is correct
28 Correct 2 ms 5976 KB Output is correct
29 Correct 2 ms 5976 KB Output is correct
30 Correct 158 ms 6464 KB Output is correct
31 Correct 231 ms 6748 KB Output is correct
32 Correct 333 ms 6484 KB Output is correct
33 Correct 331 ms 6816 KB Output is correct
34 Correct 330 ms 6484 KB Output is correct
35 Correct 1002 ms 6824 KB Output is correct
36 Correct 951 ms 6588 KB Output is correct
37 Correct 967 ms 6488 KB Output is correct
38 Correct 310 ms 6608 KB Output is correct
39 Correct 297 ms 6492 KB Output is correct
40 Correct 305 ms 6736 KB Output is correct
41 Correct 1004 ms 6956 KB Output is correct
42 Correct 962 ms 6764 KB Output is correct
43 Correct 973 ms 6900 KB Output is correct
44 Correct 294 ms 6492 KB Output is correct
45 Correct 309 ms 6840 KB Output is correct
46 Correct 307 ms 6492 KB Output is correct
47 Correct 982 ms 6540 KB Output is correct
48 Correct 941 ms 6504 KB Output is correct
49 Correct 946 ms 6488 KB Output is correct
50 Correct 241 ms 6572 KB Output is correct
51 Correct 237 ms 6536 KB Output is correct
52 Correct 273 ms 6740 KB Output is correct
53 Correct 232 ms 6476 KB Output is correct
54 Correct 239 ms 6484 KB Output is correct
55 Correct 232 ms 6808 KB Output is correct
56 Correct 80 ms 6232 KB Output is correct
57 Correct 3 ms 6236 KB Output is correct
58 Correct 3 ms 6492 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 5980 KB Output is correct
2 Correct 3 ms 5980 KB Output is correct
3 Correct 84 ms 6248 KB Output is correct
4 Execution timed out 5015 ms 22040 KB Time limit exceeded
5 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 5976 KB Output is correct
2 Correct 37 ms 14172 KB Output is correct
3 Correct 54 ms 14676 KB Output is correct
4 Correct 48 ms 14684 KB Output is correct
5 Correct 102 ms 19568 KB Output is correct
6 Correct 111 ms 19140 KB Output is correct
7 Correct 111 ms 18424 KB Output is correct
8 Correct 87 ms 18000 KB Output is correct
9 Correct 112 ms 18004 KB Output is correct
10 Correct 83 ms 17744 KB Output is correct
11 Correct 88 ms 17744 KB Output is correct
12 Correct 87 ms 18000 KB Output is correct
13 Correct 136 ms 18268 KB Output is correct
14 Correct 83 ms 19540 KB Output is correct
15 Correct 82 ms 24148 KB Output is correct
16 Correct 125 ms 18932 KB Output is correct
17 Correct 88 ms 18948 KB Output is correct
18 Correct 91 ms 19108 KB Output is correct
19 Correct 94 ms 20420 KB Output is correct
20 Correct 95 ms 19924 KB Output is correct
21 Correct 97 ms 19768 KB Output is correct
22 Correct 98 ms 19644 KB Output is correct
23 Correct 119 ms 19676 KB Output is correct
24 Correct 98 ms 19204 KB Output is correct
25 Correct 90 ms 19024 KB Output is correct
26 Correct 101 ms 18852 KB Output is correct
27 Correct 122 ms 18576 KB Output is correct
28 Correct 94 ms 18192 KB Output is correct
29 Correct 90 ms 18260 KB Output is correct
30 Correct 89 ms 18228 KB Output is correct
31 Correct 87 ms 18584 KB Output is correct
32 Correct 90 ms 19032 KB Output is correct
33 Correct 92 ms 20868 KB Output is correct
34 Correct 113 ms 24124 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 5980 KB Output is correct
2 Correct 4 ms 5980 KB Output is correct
3 Correct 84 ms 6236 KB Output is correct
4 Execution timed out 5006 ms 17136 KB Time limit exceeded
5 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 5976 KB Output is correct
2 Correct 1 ms 5980 KB Output is correct
3 Correct 2 ms 5980 KB Output is correct
4 Correct 6 ms 5980 KB Output is correct
5 Correct 4 ms 6224 KB Output is correct
6 Correct 5 ms 5976 KB Output is correct
7 Correct 5 ms 5980 KB Output is correct
8 Correct 4 ms 5980 KB Output is correct
9 Correct 8 ms 6240 KB Output is correct
10 Correct 8 ms 6236 KB Output is correct
11 Correct 8 ms 6236 KB Output is correct
12 Correct 18 ms 6236 KB Output is correct
13 Correct 17 ms 6232 KB Output is correct
14 Correct 21 ms 6236 KB Output is correct
15 Correct 7 ms 6232 KB Output is correct
16 Correct 6 ms 6236 KB Output is correct
17 Correct 7 ms 6260 KB Output is correct
18 Correct 7 ms 6236 KB Output is correct
19 Correct 7 ms 6236 KB Output is correct
20 Correct 7 ms 6236 KB Output is correct
21 Correct 6 ms 6236 KB Output is correct
22 Correct 6 ms 6236 KB Output is correct
23 Correct 6 ms 6236 KB Output is correct
24 Correct 7 ms 6232 KB Output is correct
25 Correct 7 ms 6236 KB Output is correct
26 Correct 6 ms 6236 KB Output is correct
27 Correct 3 ms 5980 KB Output is correct
28 Correct 2 ms 5976 KB Output is correct
29 Correct 2 ms 5976 KB Output is correct
30 Correct 158 ms 6464 KB Output is correct
31 Correct 231 ms 6748 KB Output is correct
32 Correct 333 ms 6484 KB Output is correct
33 Correct 331 ms 6816 KB Output is correct
34 Correct 330 ms 6484 KB Output is correct
35 Correct 1002 ms 6824 KB Output is correct
36 Correct 951 ms 6588 KB Output is correct
37 Correct 967 ms 6488 KB Output is correct
38 Correct 310 ms 6608 KB Output is correct
39 Correct 297 ms 6492 KB Output is correct
40 Correct 305 ms 6736 KB Output is correct
41 Correct 1004 ms 6956 KB Output is correct
42 Correct 962 ms 6764 KB Output is correct
43 Correct 973 ms 6900 KB Output is correct
44 Correct 294 ms 6492 KB Output is correct
45 Correct 309 ms 6840 KB Output is correct
46 Correct 307 ms 6492 KB Output is correct
47 Correct 982 ms 6540 KB Output is correct
48 Correct 941 ms 6504 KB Output is correct
49 Correct 946 ms 6488 KB Output is correct
50 Correct 241 ms 6572 KB Output is correct
51 Correct 237 ms 6536 KB Output is correct
52 Correct 273 ms 6740 KB Output is correct
53 Correct 232 ms 6476 KB Output is correct
54 Correct 239 ms 6484 KB Output is correct
55 Correct 232 ms 6808 KB Output is correct
56 Correct 80 ms 6232 KB Output is correct
57 Correct 3 ms 6236 KB Output is correct
58 Correct 3 ms 6492 KB Output is correct
59 Correct 2 ms 5980 KB Output is correct
60 Correct 3 ms 5980 KB Output is correct
61 Correct 84 ms 6248 KB Output is correct
62 Execution timed out 5015 ms 22040 KB Time limit exceeded
63 Halted 0 ms 0 KB -