#include <algorithm>
#include <iostream>
#include <numeric>
#include <cassert>
#include <vector>
typedef long long llong;
const int MAXN = 100000 + 10;
const int MAXLOG = 20;
const int INF = 1e9;
int n, m, q;
int in[MAXN];
int out[MAXN];
struct Sparse
{
int sparse[MAXLOG][2 * MAXN];
std::vector <int> tour;
int getLog[2 * MAXN];
int depth[MAXN];
int cmp(int x, int y)
{
if (depth[x] < depth[y]) return x;
else return y;
}
void build(std::vector <int> _tour, int dep[])
{
tour = _tour;
for (int i = 1 ; i <= n ; ++i)
{
depth[i] = dep[i];
}
for (int i = 0 ; i < tour.size() ; ++i)
{
sparse[0][i] = tour[i];
}
for (int lg = 1 ; (1 << lg) <= tour.size() ; ++lg)
{
for (int i = 0 ; i + (1 << lg) - 1 < tour.size() ; ++i)
{
sparse[lg][i] = cmp(sparse[lg - 1][i], sparse[lg - 1][i + (1 << lg - 1)]);
}
}
for (int i = 1 ; i <= tour.size() ; ++i)
{
getLog[i] = getLog[i - 1];
if ((1 << getLog[i] + 1) < i) getLog[i]++;
}
}
int findLCA(int l, int r)
{
int lg = getLog[r - l + 1];
return cmp(sparse[lg][l], sparse[lg][r - (1 << lg) + 1]);
}
int findDist(int u, int v)
{
if (in[u] > in[v])
{
std::swap(u, v);
}
return depth[u] + depth[v] - 2 * depth[findLCA(in[u], in[v])];
}
};
int c[MAXN];
int depth[MAXN];
std::vector <int> tour;
std::vector <int> g[MAXN];
Sparse sparse;
void buildDFS(int node, int par)
{
depth[node] = depth[par] + 1;
in[node] = tour.size();
tour.push_back(node);
for (const int &u : g[node])
{
if (u == par)
{
continue;
}
buildDFS(u, node);
tour.push_back(node);
}
out[node] = tour.size();
}
void solve()
{
buildDFS(1, 0);
sparse.build(tour, depth);
for (int i = 1 ; i <= q ; ++i)
{
std::vector <int> v;
int l, r;
std::cin >> l >> r;
for (int idx = l ; idx <= r ; ++idx)
{
v.push_back(c[idx]);
}
std::sort(v.begin(), v.end(), [&](int x, int y)
{
return in[x] < in[y];
});
int dist = 0;
for (int i = 0 ; i < v.size() ; ++i)
{
int next = (i + 1) % v.size();
dist += sparse.findDist(v[i], v[next]);
}
assert(dist % 2 == 0);
std::cout << dist / 2 + 1 << '\n';
}
}
void input()
{
std::cin >> n >> m >> q;
for (int i = 1 ; i < n ; ++i)
{
int u, v;
std::cin >> u >> v;
g[u].push_back(v);
g[v].push_back(u);
}
for (int i = 1 ; i <= m ; ++i)
{
std::cin >> c[i];
}
}
void fastIOI()
{
std::ios_base :: sync_with_stdio(0);
std::cout.tie(nullptr);
std::cin.tie(nullptr);
}
int main()
{
fastIOI();
input();
solve();
return 0;
}
Compilation message
tourism.cpp: In member function 'void Sparse::build(std::vector<int>, int*)':
tourism.cpp:37:28: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
37 | for (int i = 0 ; i < tour.size() ; ++i)
| ~~^~~~~~~~~~~~~
tourism.cpp:42:37: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
42 | for (int lg = 1 ; (1 << lg) <= tour.size() ; ++lg)
| ~~~~~~~~~~^~~~~~~~~~~~~~
tourism.cpp:44:48: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
44 | for (int i = 0 ; i + (1 << lg) - 1 < tour.size() ; ++i)
| ~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~
tourism.cpp:46:84: warning: suggest parentheses around '-' inside '<<' [-Wparentheses]
46 | sparse[lg][i] = cmp(sparse[lg - 1][i], sparse[lg - 1][i + (1 << lg - 1)]);
| ~~~^~~
tourism.cpp:50:28: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
50 | for (int i = 1 ; i <= tour.size() ; ++i)
| ~~^~~~~~~~~~~~~~
tourism.cpp:53:33: warning: suggest parentheses around '+' inside '<<' [-Wparentheses]
53 | if ((1 << getLog[i] + 1) < i) getLog[i]++;
| ~~~~~~~~~~^~~
tourism.cpp: In function 'void solve()':
tourism.cpp:121:28: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
121 | for (int i = 0 ; i < v.size() ; ++i)
| ~~^~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
10584 KB |
Output is correct |
2 |
Correct |
2 ms |
10840 KB |
Output is correct |
3 |
Correct |
3 ms |
10588 KB |
Output is correct |
4 |
Correct |
3 ms |
12636 KB |
Output is correct |
5 |
Correct |
3 ms |
12636 KB |
Output is correct |
6 |
Correct |
2 ms |
12636 KB |
Output is correct |
7 |
Correct |
3 ms |
12632 KB |
Output is correct |
8 |
Correct |
3 ms |
14680 KB |
Output is correct |
9 |
Correct |
4 ms |
14684 KB |
Output is correct |
10 |
Correct |
4 ms |
14868 KB |
Output is correct |
11 |
Correct |
4 ms |
14868 KB |
Output is correct |
12 |
Correct |
6 ms |
14864 KB |
Output is correct |
13 |
Correct |
6 ms |
14856 KB |
Output is correct |
14 |
Correct |
6 ms |
14680 KB |
Output is correct |
15 |
Correct |
4 ms |
14684 KB |
Output is correct |
16 |
Correct |
4 ms |
14684 KB |
Output is correct |
17 |
Correct |
4 ms |
14684 KB |
Output is correct |
18 |
Correct |
5 ms |
14684 KB |
Output is correct |
19 |
Correct |
4 ms |
14684 KB |
Output is correct |
20 |
Correct |
4 ms |
14864 KB |
Output is correct |
21 |
Correct |
4 ms |
14684 KB |
Output is correct |
22 |
Correct |
4 ms |
14684 KB |
Output is correct |
23 |
Correct |
4 ms |
14684 KB |
Output is correct |
24 |
Correct |
4 ms |
14684 KB |
Output is correct |
25 |
Correct |
4 ms |
14820 KB |
Output is correct |
26 |
Correct |
4 ms |
14684 KB |
Output is correct |
27 |
Correct |
2 ms |
6492 KB |
Output is correct |
28 |
Correct |
2 ms |
14684 KB |
Output is correct |
29 |
Correct |
2 ms |
14684 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
10584 KB |
Output is correct |
2 |
Correct |
2 ms |
10840 KB |
Output is correct |
3 |
Correct |
3 ms |
10588 KB |
Output is correct |
4 |
Correct |
3 ms |
12636 KB |
Output is correct |
5 |
Correct |
3 ms |
12636 KB |
Output is correct |
6 |
Correct |
2 ms |
12636 KB |
Output is correct |
7 |
Correct |
3 ms |
12632 KB |
Output is correct |
8 |
Correct |
3 ms |
14680 KB |
Output is correct |
9 |
Correct |
4 ms |
14684 KB |
Output is correct |
10 |
Correct |
4 ms |
14868 KB |
Output is correct |
11 |
Correct |
4 ms |
14868 KB |
Output is correct |
12 |
Correct |
6 ms |
14864 KB |
Output is correct |
13 |
Correct |
6 ms |
14856 KB |
Output is correct |
14 |
Correct |
6 ms |
14680 KB |
Output is correct |
15 |
Correct |
4 ms |
14684 KB |
Output is correct |
16 |
Correct |
4 ms |
14684 KB |
Output is correct |
17 |
Correct |
4 ms |
14684 KB |
Output is correct |
18 |
Correct |
5 ms |
14684 KB |
Output is correct |
19 |
Correct |
4 ms |
14684 KB |
Output is correct |
20 |
Correct |
4 ms |
14864 KB |
Output is correct |
21 |
Correct |
4 ms |
14684 KB |
Output is correct |
22 |
Correct |
4 ms |
14684 KB |
Output is correct |
23 |
Correct |
4 ms |
14684 KB |
Output is correct |
24 |
Correct |
4 ms |
14684 KB |
Output is correct |
25 |
Correct |
4 ms |
14820 KB |
Output is correct |
26 |
Correct |
4 ms |
14684 KB |
Output is correct |
27 |
Correct |
2 ms |
6492 KB |
Output is correct |
28 |
Correct |
2 ms |
14684 KB |
Output is correct |
29 |
Correct |
2 ms |
14684 KB |
Output is correct |
30 |
Correct |
34 ms |
16872 KB |
Output is correct |
31 |
Correct |
49 ms |
16992 KB |
Output is correct |
32 |
Correct |
72 ms |
16988 KB |
Output is correct |
33 |
Correct |
71 ms |
17052 KB |
Output is correct |
34 |
Correct |
69 ms |
16988 KB |
Output is correct |
35 |
Correct |
218 ms |
17052 KB |
Output is correct |
36 |
Correct |
220 ms |
17052 KB |
Output is correct |
37 |
Correct |
216 ms |
17064 KB |
Output is correct |
38 |
Correct |
70 ms |
16984 KB |
Output is correct |
39 |
Correct |
69 ms |
16988 KB |
Output is correct |
40 |
Correct |
70 ms |
16984 KB |
Output is correct |
41 |
Correct |
216 ms |
16984 KB |
Output is correct |
42 |
Correct |
216 ms |
17160 KB |
Output is correct |
43 |
Correct |
220 ms |
17140 KB |
Output is correct |
44 |
Correct |
77 ms |
16988 KB |
Output is correct |
45 |
Correct |
71 ms |
16984 KB |
Output is correct |
46 |
Correct |
70 ms |
17112 KB |
Output is correct |
47 |
Correct |
219 ms |
17236 KB |
Output is correct |
48 |
Correct |
218 ms |
17084 KB |
Output is correct |
49 |
Correct |
216 ms |
17088 KB |
Output is correct |
50 |
Correct |
69 ms |
16984 KB |
Output is correct |
51 |
Correct |
72 ms |
16988 KB |
Output is correct |
52 |
Correct |
73 ms |
16988 KB |
Output is correct |
53 |
Correct |
70 ms |
16988 KB |
Output is correct |
54 |
Correct |
74 ms |
17236 KB |
Output is correct |
55 |
Correct |
70 ms |
17080 KB |
Output is correct |
56 |
Correct |
17 ms |
6492 KB |
Output is correct |
57 |
Correct |
4 ms |
16984 KB |
Output is correct |
58 |
Correct |
4 ms |
16988 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
10588 KB |
Output is correct |
2 |
Correct |
2 ms |
6628 KB |
Output is correct |
3 |
Correct |
17 ms |
6664 KB |
Output is correct |
4 |
Execution timed out |
5092 ms |
29468 KB |
Time limit exceeded |
5 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
10588 KB |
Output is correct |
2 |
Correct |
27 ms |
25172 KB |
Output is correct |
3 |
Correct |
31 ms |
25544 KB |
Output is correct |
4 |
Correct |
32 ms |
26064 KB |
Output is correct |
5 |
Correct |
49 ms |
28528 KB |
Output is correct |
6 |
Correct |
51 ms |
28588 KB |
Output is correct |
7 |
Correct |
52 ms |
28616 KB |
Output is correct |
8 |
Correct |
60 ms |
28656 KB |
Output is correct |
9 |
Correct |
47 ms |
28468 KB |
Output is correct |
10 |
Correct |
46 ms |
28612 KB |
Output is correct |
11 |
Correct |
47 ms |
28616 KB |
Output is correct |
12 |
Correct |
45 ms |
28620 KB |
Output is correct |
13 |
Correct |
52 ms |
28868 KB |
Output is correct |
14 |
Correct |
56 ms |
28612 KB |
Output is correct |
15 |
Correct |
64 ms |
29104 KB |
Output is correct |
16 |
Correct |
52 ms |
28612 KB |
Output is correct |
17 |
Correct |
48 ms |
28516 KB |
Output is correct |
18 |
Correct |
56 ms |
28652 KB |
Output is correct |
19 |
Correct |
49 ms |
28624 KB |
Output is correct |
20 |
Correct |
49 ms |
28648 KB |
Output is correct |
21 |
Correct |
48 ms |
28536 KB |
Output is correct |
22 |
Correct |
47 ms |
28416 KB |
Output is correct |
23 |
Correct |
48 ms |
28868 KB |
Output is correct |
24 |
Correct |
53 ms |
28552 KB |
Output is correct |
25 |
Correct |
49 ms |
28540 KB |
Output is correct |
26 |
Correct |
49 ms |
28660 KB |
Output is correct |
27 |
Correct |
45 ms |
28592 KB |
Output is correct |
28 |
Correct |
44 ms |
28616 KB |
Output is correct |
29 |
Correct |
44 ms |
28668 KB |
Output is correct |
30 |
Correct |
45 ms |
28616 KB |
Output is correct |
31 |
Correct |
46 ms |
28616 KB |
Output is correct |
32 |
Correct |
48 ms |
28720 KB |
Output is correct |
33 |
Correct |
54 ms |
28616 KB |
Output is correct |
34 |
Correct |
62 ms |
29112 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
10588 KB |
Output is correct |
2 |
Correct |
2 ms |
6488 KB |
Output is correct |
3 |
Correct |
18 ms |
6492 KB |
Output is correct |
4 |
Execution timed out |
5063 ms |
26308 KB |
Time limit exceeded |
5 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
10584 KB |
Output is correct |
2 |
Correct |
2 ms |
10840 KB |
Output is correct |
3 |
Correct |
3 ms |
10588 KB |
Output is correct |
4 |
Correct |
3 ms |
12636 KB |
Output is correct |
5 |
Correct |
3 ms |
12636 KB |
Output is correct |
6 |
Correct |
2 ms |
12636 KB |
Output is correct |
7 |
Correct |
3 ms |
12632 KB |
Output is correct |
8 |
Correct |
3 ms |
14680 KB |
Output is correct |
9 |
Correct |
4 ms |
14684 KB |
Output is correct |
10 |
Correct |
4 ms |
14868 KB |
Output is correct |
11 |
Correct |
4 ms |
14868 KB |
Output is correct |
12 |
Correct |
6 ms |
14864 KB |
Output is correct |
13 |
Correct |
6 ms |
14856 KB |
Output is correct |
14 |
Correct |
6 ms |
14680 KB |
Output is correct |
15 |
Correct |
4 ms |
14684 KB |
Output is correct |
16 |
Correct |
4 ms |
14684 KB |
Output is correct |
17 |
Correct |
4 ms |
14684 KB |
Output is correct |
18 |
Correct |
5 ms |
14684 KB |
Output is correct |
19 |
Correct |
4 ms |
14684 KB |
Output is correct |
20 |
Correct |
4 ms |
14864 KB |
Output is correct |
21 |
Correct |
4 ms |
14684 KB |
Output is correct |
22 |
Correct |
4 ms |
14684 KB |
Output is correct |
23 |
Correct |
4 ms |
14684 KB |
Output is correct |
24 |
Correct |
4 ms |
14684 KB |
Output is correct |
25 |
Correct |
4 ms |
14820 KB |
Output is correct |
26 |
Correct |
4 ms |
14684 KB |
Output is correct |
27 |
Correct |
2 ms |
6492 KB |
Output is correct |
28 |
Correct |
2 ms |
14684 KB |
Output is correct |
29 |
Correct |
2 ms |
14684 KB |
Output is correct |
30 |
Correct |
34 ms |
16872 KB |
Output is correct |
31 |
Correct |
49 ms |
16992 KB |
Output is correct |
32 |
Correct |
72 ms |
16988 KB |
Output is correct |
33 |
Correct |
71 ms |
17052 KB |
Output is correct |
34 |
Correct |
69 ms |
16988 KB |
Output is correct |
35 |
Correct |
218 ms |
17052 KB |
Output is correct |
36 |
Correct |
220 ms |
17052 KB |
Output is correct |
37 |
Correct |
216 ms |
17064 KB |
Output is correct |
38 |
Correct |
70 ms |
16984 KB |
Output is correct |
39 |
Correct |
69 ms |
16988 KB |
Output is correct |
40 |
Correct |
70 ms |
16984 KB |
Output is correct |
41 |
Correct |
216 ms |
16984 KB |
Output is correct |
42 |
Correct |
216 ms |
17160 KB |
Output is correct |
43 |
Correct |
220 ms |
17140 KB |
Output is correct |
44 |
Correct |
77 ms |
16988 KB |
Output is correct |
45 |
Correct |
71 ms |
16984 KB |
Output is correct |
46 |
Correct |
70 ms |
17112 KB |
Output is correct |
47 |
Correct |
219 ms |
17236 KB |
Output is correct |
48 |
Correct |
218 ms |
17084 KB |
Output is correct |
49 |
Correct |
216 ms |
17088 KB |
Output is correct |
50 |
Correct |
69 ms |
16984 KB |
Output is correct |
51 |
Correct |
72 ms |
16988 KB |
Output is correct |
52 |
Correct |
73 ms |
16988 KB |
Output is correct |
53 |
Correct |
70 ms |
16988 KB |
Output is correct |
54 |
Correct |
74 ms |
17236 KB |
Output is correct |
55 |
Correct |
70 ms |
17080 KB |
Output is correct |
56 |
Correct |
17 ms |
6492 KB |
Output is correct |
57 |
Correct |
4 ms |
16984 KB |
Output is correct |
58 |
Correct |
4 ms |
16988 KB |
Output is correct |
59 |
Correct |
2 ms |
10588 KB |
Output is correct |
60 |
Correct |
2 ms |
6628 KB |
Output is correct |
61 |
Correct |
17 ms |
6664 KB |
Output is correct |
62 |
Execution timed out |
5092 ms |
29468 KB |
Time limit exceeded |
63 |
Halted |
0 ms |
0 KB |
- |