/**
____ ____ ____ ____ ____ ____
||l |||e |||i |||n |||a |||d ||
||__|||__|||__|||__|||__|||__||
|/__\|/__\|/__\|/__\|/__\|/__\|
**/
#include<bits/stdc++.h>
#define endl '\n'
using namespace std;
typedef long long ll;
void speed()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
}
const int maxn = 1e5 + 10;
struct query
{
int l, r;
}task[maxn];
int n, m, c[maxn], q;
vector < int > adj[maxn];
void input()
{
cin >> n >> m >> q;
for (int i = 1; i < n; i ++)
{
int v, u;
cin >> v >> u;
adj[v].push_back(u);
adj[u].push_back(v);
}
for (int i = 1; i <= m; i ++)
{
cin >> c[i];
}
for (int i = 1; i <= q; i ++)
{
cin >> task[i].l >> task[i].r;
}
}
int depth[maxn], tin[maxn], tout[maxn];
int occ[2 * maxn], timer;
void euler(int v = 1, int p = - 1)
{
occ[++ timer] = v;
tin[v] = timer;
for (int u : adj[v])
{
if (u == p)
continue;
depth[u] = depth[v] + 1;
euler(u, v);
occ[++ timer] = v;
}
tout[v] = timer;
}
const int maxlog = 20;
int lg[2 * maxn], dp[maxlog][2 * maxn];
void sparse_table()
{
for (int i = 1; i <= timer; i ++)
{
lg[i] = lg[i / 2] + 1;
dp[0][i] = occ[i];
}
for (int j = 1; j < lg[timer]; j ++)
for (int i = 1; i <= timer - (1 << j); i ++)
{
dp[j][i] = dp[j - 1][i + (1 << (j - 1))];
if (depth[dp[j - 1][i]] < depth[dp[j][i]])
dp[j][i] = dp[j - 1][i];
}
}
int get_lca(int v, int u)
{
int l = tin[v], r = tin[u];
if (l > r)
swap(l, r);
int len = lg[r - l + 1] - 1;
int lca = dp[len][r - (1 << len) + 1];
if (depth[dp[len][l]] < depth[lca])
lca = dp[len][l];
return lca;
}
int get_distance(int v, int u)
{
return depth[v] + depth[u] - 2 * depth[get_lca(v, u)];
}
bool cmp_tin(int v, int u)
{
return tin[v] < tin[u];
}
void solve_query(int left, int right)
{
vector < int > ord;
int global_lca = c[left];
for (int i = left; i <= right; i ++)
{
ord.push_back(c[i]);
global_lca = get_lca(global_lca, c[i]);
}
sort(ord.begin(), ord.end(), cmp_tin);
int ans = 0;
for (int i = 0; i < ord.size(); i ++)
{
ans = ans + depth[ord[i]] - depth[global_lca];
if (i > 0)
{
ans = ans - (depth[get_lca(ord[i - 1], ord[i])] - depth[global_lca]);
}
}
cout << ans + 1 << endl;
}
void queries()
{
for (int i = 1; i <= q; i ++)
solve_query(task[i].l, task[i].r);
}
void solve()
{
input();
euler();
sparse_table();
queries();
}
int main()
{
speed();
solve();
return 0;
}
Compilation message
tourism.cpp: In function 'void solve_query(int, int)':
tourism.cpp:128:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
128 | for (int i = 0; i < ord.size(); i ++)
| ~~^~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
8536 KB |
Output is correct |
2 |
Correct |
2 ms |
8536 KB |
Output is correct |
3 |
Correct |
1 ms |
8536 KB |
Output is correct |
4 |
Correct |
3 ms |
10840 KB |
Output is correct |
5 |
Correct |
2 ms |
10584 KB |
Output is correct |
6 |
Correct |
3 ms |
10588 KB |
Output is correct |
7 |
Correct |
3 ms |
10588 KB |
Output is correct |
8 |
Correct |
2 ms |
12636 KB |
Output is correct |
9 |
Correct |
4 ms |
12632 KB |
Output is correct |
10 |
Correct |
4 ms |
12632 KB |
Output is correct |
11 |
Correct |
4 ms |
12888 KB |
Output is correct |
12 |
Correct |
8 ms |
12636 KB |
Output is correct |
13 |
Correct |
8 ms |
12632 KB |
Output is correct |
14 |
Correct |
8 ms |
12632 KB |
Output is correct |
15 |
Correct |
4 ms |
12636 KB |
Output is correct |
16 |
Correct |
4 ms |
12632 KB |
Output is correct |
17 |
Correct |
4 ms |
12632 KB |
Output is correct |
18 |
Correct |
4 ms |
12636 KB |
Output is correct |
19 |
Correct |
4 ms |
12632 KB |
Output is correct |
20 |
Correct |
4 ms |
12632 KB |
Output is correct |
21 |
Correct |
4 ms |
12632 KB |
Output is correct |
22 |
Correct |
4 ms |
12632 KB |
Output is correct |
23 |
Correct |
4 ms |
12636 KB |
Output is correct |
24 |
Correct |
4 ms |
12632 KB |
Output is correct |
25 |
Correct |
4 ms |
12632 KB |
Output is correct |
26 |
Correct |
4 ms |
12632 KB |
Output is correct |
27 |
Correct |
2 ms |
4440 KB |
Output is correct |
28 |
Correct |
2 ms |
12636 KB |
Output is correct |
29 |
Correct |
2 ms |
12632 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
8536 KB |
Output is correct |
2 |
Correct |
2 ms |
8536 KB |
Output is correct |
3 |
Correct |
1 ms |
8536 KB |
Output is correct |
4 |
Correct |
3 ms |
10840 KB |
Output is correct |
5 |
Correct |
2 ms |
10584 KB |
Output is correct |
6 |
Correct |
3 ms |
10588 KB |
Output is correct |
7 |
Correct |
3 ms |
10588 KB |
Output is correct |
8 |
Correct |
2 ms |
12636 KB |
Output is correct |
9 |
Correct |
4 ms |
12632 KB |
Output is correct |
10 |
Correct |
4 ms |
12632 KB |
Output is correct |
11 |
Correct |
4 ms |
12888 KB |
Output is correct |
12 |
Correct |
8 ms |
12636 KB |
Output is correct |
13 |
Correct |
8 ms |
12632 KB |
Output is correct |
14 |
Correct |
8 ms |
12632 KB |
Output is correct |
15 |
Correct |
4 ms |
12636 KB |
Output is correct |
16 |
Correct |
4 ms |
12632 KB |
Output is correct |
17 |
Correct |
4 ms |
12632 KB |
Output is correct |
18 |
Correct |
4 ms |
12636 KB |
Output is correct |
19 |
Correct |
4 ms |
12632 KB |
Output is correct |
20 |
Correct |
4 ms |
12632 KB |
Output is correct |
21 |
Correct |
4 ms |
12632 KB |
Output is correct |
22 |
Correct |
4 ms |
12632 KB |
Output is correct |
23 |
Correct |
4 ms |
12636 KB |
Output is correct |
24 |
Correct |
4 ms |
12632 KB |
Output is correct |
25 |
Correct |
4 ms |
12632 KB |
Output is correct |
26 |
Correct |
4 ms |
12632 KB |
Output is correct |
27 |
Correct |
2 ms |
4440 KB |
Output is correct |
28 |
Correct |
2 ms |
12636 KB |
Output is correct |
29 |
Correct |
2 ms |
12632 KB |
Output is correct |
30 |
Correct |
51 ms |
14988 KB |
Output is correct |
31 |
Correct |
75 ms |
15008 KB |
Output is correct |
32 |
Correct |
123 ms |
15024 KB |
Output is correct |
33 |
Correct |
110 ms |
15192 KB |
Output is correct |
34 |
Correct |
107 ms |
15024 KB |
Output is correct |
35 |
Correct |
340 ms |
15020 KB |
Output is correct |
36 |
Correct |
342 ms |
15020 KB |
Output is correct |
37 |
Correct |
337 ms |
15192 KB |
Output is correct |
38 |
Correct |
108 ms |
15140 KB |
Output is correct |
39 |
Correct |
109 ms |
14940 KB |
Output is correct |
40 |
Correct |
112 ms |
14936 KB |
Output is correct |
41 |
Correct |
342 ms |
14940 KB |
Output is correct |
42 |
Correct |
333 ms |
15168 KB |
Output is correct |
43 |
Correct |
337 ms |
15124 KB |
Output is correct |
44 |
Correct |
104 ms |
14936 KB |
Output is correct |
45 |
Correct |
108 ms |
15060 KB |
Output is correct |
46 |
Correct |
109 ms |
15080 KB |
Output is correct |
47 |
Correct |
333 ms |
15064 KB |
Output is correct |
48 |
Correct |
336 ms |
14936 KB |
Output is correct |
49 |
Correct |
339 ms |
15072 KB |
Output is correct |
50 |
Correct |
108 ms |
14936 KB |
Output is correct |
51 |
Correct |
111 ms |
14940 KB |
Output is correct |
52 |
Correct |
110 ms |
15028 KB |
Output is correct |
53 |
Correct |
105 ms |
14940 KB |
Output is correct |
54 |
Correct |
109 ms |
15036 KB |
Output is correct |
55 |
Correct |
109 ms |
15032 KB |
Output is correct |
56 |
Correct |
45 ms |
4440 KB |
Output is correct |
57 |
Correct |
3 ms |
14936 KB |
Output is correct |
58 |
Correct |
3 ms |
14940 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
8536 KB |
Output is correct |
2 |
Correct |
2 ms |
4440 KB |
Output is correct |
3 |
Correct |
43 ms |
4444 KB |
Output is correct |
4 |
Execution timed out |
5033 ms |
27744 KB |
Time limit exceeded |
5 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
8536 KB |
Output is correct |
2 |
Correct |
27 ms |
21592 KB |
Output is correct |
3 |
Correct |
35 ms |
22060 KB |
Output is correct |
4 |
Correct |
33 ms |
22096 KB |
Output is correct |
5 |
Correct |
56 ms |
24268 KB |
Output is correct |
6 |
Correct |
57 ms |
24116 KB |
Output is correct |
7 |
Correct |
56 ms |
23632 KB |
Output is correct |
8 |
Correct |
54 ms |
23636 KB |
Output is correct |
9 |
Correct |
56 ms |
23632 KB |
Output is correct |
10 |
Correct |
67 ms |
23660 KB |
Output is correct |
11 |
Correct |
55 ms |
23636 KB |
Output is correct |
12 |
Correct |
54 ms |
23716 KB |
Output is correct |
13 |
Correct |
49 ms |
23632 KB |
Output is correct |
14 |
Correct |
60 ms |
23988 KB |
Output is correct |
15 |
Correct |
64 ms |
24472 KB |
Output is correct |
16 |
Correct |
54 ms |
24144 KB |
Output is correct |
17 |
Correct |
69 ms |
24028 KB |
Output is correct |
18 |
Correct |
53 ms |
24092 KB |
Output is correct |
19 |
Correct |
53 ms |
24268 KB |
Output is correct |
20 |
Correct |
51 ms |
24076 KB |
Output is correct |
21 |
Correct |
51 ms |
23948 KB |
Output is correct |
22 |
Correct |
50 ms |
23632 KB |
Output is correct |
23 |
Correct |
50 ms |
23632 KB |
Output is correct |
24 |
Correct |
48 ms |
23628 KB |
Output is correct |
25 |
Correct |
48 ms |
23632 KB |
Output is correct |
26 |
Correct |
49 ms |
23784 KB |
Output is correct |
27 |
Correct |
47 ms |
23768 KB |
Output is correct |
28 |
Correct |
46 ms |
23644 KB |
Output is correct |
29 |
Correct |
46 ms |
23632 KB |
Output is correct |
30 |
Correct |
48 ms |
23828 KB |
Output is correct |
31 |
Correct |
53 ms |
23892 KB |
Output is correct |
32 |
Correct |
50 ms |
23880 KB |
Output is correct |
33 |
Correct |
53 ms |
24144 KB |
Output is correct |
34 |
Correct |
57 ms |
24656 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
8536 KB |
Output is correct |
2 |
Correct |
2 ms |
4440 KB |
Output is correct |
3 |
Correct |
43 ms |
4656 KB |
Output is correct |
4 |
Execution timed out |
5025 ms |
23828 KB |
Time limit exceeded |
5 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
8536 KB |
Output is correct |
2 |
Correct |
2 ms |
8536 KB |
Output is correct |
3 |
Correct |
1 ms |
8536 KB |
Output is correct |
4 |
Correct |
3 ms |
10840 KB |
Output is correct |
5 |
Correct |
2 ms |
10584 KB |
Output is correct |
6 |
Correct |
3 ms |
10588 KB |
Output is correct |
7 |
Correct |
3 ms |
10588 KB |
Output is correct |
8 |
Correct |
2 ms |
12636 KB |
Output is correct |
9 |
Correct |
4 ms |
12632 KB |
Output is correct |
10 |
Correct |
4 ms |
12632 KB |
Output is correct |
11 |
Correct |
4 ms |
12888 KB |
Output is correct |
12 |
Correct |
8 ms |
12636 KB |
Output is correct |
13 |
Correct |
8 ms |
12632 KB |
Output is correct |
14 |
Correct |
8 ms |
12632 KB |
Output is correct |
15 |
Correct |
4 ms |
12636 KB |
Output is correct |
16 |
Correct |
4 ms |
12632 KB |
Output is correct |
17 |
Correct |
4 ms |
12632 KB |
Output is correct |
18 |
Correct |
4 ms |
12636 KB |
Output is correct |
19 |
Correct |
4 ms |
12632 KB |
Output is correct |
20 |
Correct |
4 ms |
12632 KB |
Output is correct |
21 |
Correct |
4 ms |
12632 KB |
Output is correct |
22 |
Correct |
4 ms |
12632 KB |
Output is correct |
23 |
Correct |
4 ms |
12636 KB |
Output is correct |
24 |
Correct |
4 ms |
12632 KB |
Output is correct |
25 |
Correct |
4 ms |
12632 KB |
Output is correct |
26 |
Correct |
4 ms |
12632 KB |
Output is correct |
27 |
Correct |
2 ms |
4440 KB |
Output is correct |
28 |
Correct |
2 ms |
12636 KB |
Output is correct |
29 |
Correct |
2 ms |
12632 KB |
Output is correct |
30 |
Correct |
51 ms |
14988 KB |
Output is correct |
31 |
Correct |
75 ms |
15008 KB |
Output is correct |
32 |
Correct |
123 ms |
15024 KB |
Output is correct |
33 |
Correct |
110 ms |
15192 KB |
Output is correct |
34 |
Correct |
107 ms |
15024 KB |
Output is correct |
35 |
Correct |
340 ms |
15020 KB |
Output is correct |
36 |
Correct |
342 ms |
15020 KB |
Output is correct |
37 |
Correct |
337 ms |
15192 KB |
Output is correct |
38 |
Correct |
108 ms |
15140 KB |
Output is correct |
39 |
Correct |
109 ms |
14940 KB |
Output is correct |
40 |
Correct |
112 ms |
14936 KB |
Output is correct |
41 |
Correct |
342 ms |
14940 KB |
Output is correct |
42 |
Correct |
333 ms |
15168 KB |
Output is correct |
43 |
Correct |
337 ms |
15124 KB |
Output is correct |
44 |
Correct |
104 ms |
14936 KB |
Output is correct |
45 |
Correct |
108 ms |
15060 KB |
Output is correct |
46 |
Correct |
109 ms |
15080 KB |
Output is correct |
47 |
Correct |
333 ms |
15064 KB |
Output is correct |
48 |
Correct |
336 ms |
14936 KB |
Output is correct |
49 |
Correct |
339 ms |
15072 KB |
Output is correct |
50 |
Correct |
108 ms |
14936 KB |
Output is correct |
51 |
Correct |
111 ms |
14940 KB |
Output is correct |
52 |
Correct |
110 ms |
15028 KB |
Output is correct |
53 |
Correct |
105 ms |
14940 KB |
Output is correct |
54 |
Correct |
109 ms |
15036 KB |
Output is correct |
55 |
Correct |
109 ms |
15032 KB |
Output is correct |
56 |
Correct |
45 ms |
4440 KB |
Output is correct |
57 |
Correct |
3 ms |
14936 KB |
Output is correct |
58 |
Correct |
3 ms |
14940 KB |
Output is correct |
59 |
Correct |
2 ms |
8536 KB |
Output is correct |
60 |
Correct |
2 ms |
4440 KB |
Output is correct |
61 |
Correct |
43 ms |
4444 KB |
Output is correct |
62 |
Execution timed out |
5033 ms |
27744 KB |
Time limit exceeded |
63 |
Halted |
0 ms |
0 KB |
- |