#define ll long long
#define INF numeric_limits<ll>::max()
#include <bits/stdc++.h>
using namespace std;
void init(ll i, vector<ll>& depth, vector<vector<ll>>& adjlist, vector<bool>& visited, vector<ll>& subtree, vector<vector<ll>>& jump)
{
visited[i] = true;
for(ll j : adjlist[i])
{
if(visited[j])
{
jump[0][i] = j;
depth[i] = depth[j]+1;
}
}
for(ll j : adjlist[i])
{
if(!visited[j])
{
init(j, depth, adjlist, visited, subtree, jump);
subtree[i] += subtree[j];
}
}
}
ll lift(ll a, vector<vector<ll>>& jump, ll dist)
{
for(ll i = jump.size()-1; i >= 0 && a != -1; i--)
{
if(((1<<i)&dist) != 0)
{
a = jump[i][a];
}
}
return a;
}
ll lca(ll a, ll b, vector<vector<ll>>& jump, vector<ll>& depth)
{
if(depth[a] < depth[b])
swap(a, b);
a = lift(a, jump, depth[a]-depth[b]);
if(a == b)
{
return a;
}
for(ll j = jump.size()-1; j >= 0; j--)
{
if(jump[j][a] != jump[j][b])
{
a = jump[j][a];
b = jump[j][b];
}
}
return jump[0][a];
}
int main()
{
ios::sync_with_stdio(false);
cin.tie(0);
ll n, m, q;
cin>>n>>m>>q;
vector<vector<ll>> adjlist(n, vector<ll> (0));
bool sub3 = true;
for(ll i = 0; i < n-1; i++)
{
ll a, b;
cin>>a>>b;
a--; b--;
if(a != i || b != i+1)
{
sub3 = false;
}
adjlist[a].push_back(b);
adjlist[b].push_back(a);
}
ll segnodes = 1;
ll logdepth = 0;
while(2*n > segnodes)
{
segnodes *= 2;
logdepth++;
}
vector<vector<ll>> jump(logdepth, vector<ll> (n, -1));
vector<ll> subtree(n, 1);
vector<bool> visited(n, false);
vector<ll> depth(n, 0);
init(0, depth, adjlist, visited, subtree, jump);
for(ll i = 1; i < logdepth; i++)
{
for(ll j = 0; j < n; j++)
{
if(jump[i-1][j] != -1)
{
jump[i][j] = jump[i-1][jump[i-1][j]];
}
}
}
vector<ll> c(m);
for(ll i = 0; i < m; i++)
{
cin>>c[i];
c[i]--;
}
for(ll x = 0; x < q; x++)
{
ll left, right;
cin>>left>>right;
left--; right--;
if(left == right)
{
cout<<1<<"\n";
continue;
}
if(sub3)
{
ll top = INF;
ll bottom = -1;
for(ll i = left; i <= right; i++)
{
top = min(top, depth[c[i]]);
bottom = max(bottom, depth[c[i]]);
}
cout<<bottom+1-top<<"\n";
continue;
}
ll l = lca(c[left], c[left+1], jump, depth);
for(ll i = left+2; i <= right; i++)
{
l = lca(l, c[i], jump, depth);
}
visited.assign(n, false);
visited[l] = true;
ll counter = 1;
for(ll i = left; i <= right; i++)
{
ll node = c[i];
while(!visited[node])
{
visited[node] = true;
counter++;
node = jump[0][node];
}
}
cout<<counter<<"\n";
}
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
368 KB |
Output is correct |
12 |
Correct |
3 ms |
340 KB |
Output is correct |
13 |
Correct |
2 ms |
340 KB |
Output is correct |
14 |
Correct |
3 ms |
368 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
1 ms |
340 KB |
Output is correct |
17 |
Correct |
2 ms |
468 KB |
Output is correct |
18 |
Correct |
2 ms |
340 KB |
Output is correct |
19 |
Correct |
2 ms |
384 KB |
Output is correct |
20 |
Correct |
1 ms |
340 KB |
Output is correct |
21 |
Correct |
1 ms |
340 KB |
Output is correct |
22 |
Correct |
1 ms |
340 KB |
Output is correct |
23 |
Correct |
1 ms |
340 KB |
Output is correct |
24 |
Correct |
1 ms |
340 KB |
Output is correct |
25 |
Correct |
1 ms |
340 KB |
Output is correct |
26 |
Correct |
1 ms |
340 KB |
Output is correct |
27 |
Correct |
0 ms |
212 KB |
Output is correct |
28 |
Correct |
1 ms |
340 KB |
Output is correct |
29 |
Correct |
0 ms |
340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
368 KB |
Output is correct |
12 |
Correct |
3 ms |
340 KB |
Output is correct |
13 |
Correct |
2 ms |
340 KB |
Output is correct |
14 |
Correct |
3 ms |
368 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
1 ms |
340 KB |
Output is correct |
17 |
Correct |
2 ms |
468 KB |
Output is correct |
18 |
Correct |
2 ms |
340 KB |
Output is correct |
19 |
Correct |
2 ms |
384 KB |
Output is correct |
20 |
Correct |
1 ms |
340 KB |
Output is correct |
21 |
Correct |
1 ms |
340 KB |
Output is correct |
22 |
Correct |
1 ms |
340 KB |
Output is correct |
23 |
Correct |
1 ms |
340 KB |
Output is correct |
24 |
Correct |
1 ms |
340 KB |
Output is correct |
25 |
Correct |
1 ms |
340 KB |
Output is correct |
26 |
Correct |
1 ms |
340 KB |
Output is correct |
27 |
Correct |
0 ms |
212 KB |
Output is correct |
28 |
Correct |
1 ms |
340 KB |
Output is correct |
29 |
Correct |
0 ms |
340 KB |
Output is correct |
30 |
Correct |
23 ms |
588 KB |
Output is correct |
31 |
Correct |
32 ms |
580 KB |
Output is correct |
32 |
Correct |
48 ms |
668 KB |
Output is correct |
33 |
Correct |
50 ms |
596 KB |
Output is correct |
34 |
Correct |
47 ms |
596 KB |
Output is correct |
35 |
Correct |
112 ms |
672 KB |
Output is correct |
36 |
Correct |
121 ms |
660 KB |
Output is correct |
37 |
Correct |
113 ms |
672 KB |
Output is correct |
38 |
Correct |
87 ms |
772 KB |
Output is correct |
39 |
Correct |
77 ms |
724 KB |
Output is correct |
40 |
Correct |
76 ms |
724 KB |
Output is correct |
41 |
Correct |
233 ms |
804 KB |
Output is correct |
42 |
Correct |
233 ms |
812 KB |
Output is correct |
43 |
Correct |
235 ms |
780 KB |
Output is correct |
44 |
Correct |
75 ms |
724 KB |
Output is correct |
45 |
Correct |
67 ms |
708 KB |
Output is correct |
46 |
Correct |
75 ms |
736 KB |
Output is correct |
47 |
Correct |
211 ms |
700 KB |
Output is correct |
48 |
Correct |
200 ms |
680 KB |
Output is correct |
49 |
Correct |
220 ms |
716 KB |
Output is correct |
50 |
Correct |
27 ms |
688 KB |
Output is correct |
51 |
Correct |
28 ms |
596 KB |
Output is correct |
52 |
Correct |
27 ms |
600 KB |
Output is correct |
53 |
Correct |
33 ms |
724 KB |
Output is correct |
54 |
Correct |
29 ms |
688 KB |
Output is correct |
55 |
Correct |
29 ms |
596 KB |
Output is correct |
56 |
Correct |
2 ms |
340 KB |
Output is correct |
57 |
Correct |
1 ms |
596 KB |
Output is correct |
58 |
Correct |
1 ms |
596 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
2 ms |
340 KB |
Output is correct |
4 |
Correct |
3531 ms |
18604 KB |
Output is correct |
5 |
Correct |
2170 ms |
27848 KB |
Output is correct |
6 |
Correct |
2736 ms |
31524 KB |
Output is correct |
7 |
Execution timed out |
5103 ms |
34956 KB |
Time limit exceeded |
8 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
34 ms |
12244 KB |
Output is correct |
3 |
Correct |
46 ms |
13428 KB |
Output is correct |
4 |
Correct |
46 ms |
15620 KB |
Output is correct |
5 |
Correct |
84 ms |
22908 KB |
Output is correct |
6 |
Correct |
68 ms |
22896 KB |
Output is correct |
7 |
Correct |
55 ms |
22872 KB |
Output is correct |
8 |
Correct |
55 ms |
22896 KB |
Output is correct |
9 |
Correct |
66 ms |
22820 KB |
Output is correct |
10 |
Correct |
56 ms |
22844 KB |
Output is correct |
11 |
Correct |
69 ms |
22908 KB |
Output is correct |
12 |
Correct |
74 ms |
22892 KB |
Output is correct |
13 |
Correct |
78 ms |
22904 KB |
Output is correct |
14 |
Correct |
90 ms |
22876 KB |
Output is correct |
15 |
Correct |
69 ms |
23012 KB |
Output is correct |
16 |
Correct |
86 ms |
22904 KB |
Output is correct |
17 |
Correct |
76 ms |
22916 KB |
Output is correct |
18 |
Correct |
75 ms |
22920 KB |
Output is correct |
19 |
Correct |
53 ms |
22460 KB |
Output is correct |
20 |
Correct |
57 ms |
22376 KB |
Output is correct |
21 |
Correct |
58 ms |
22420 KB |
Output is correct |
22 |
Correct |
71 ms |
22496 KB |
Output is correct |
23 |
Correct |
74 ms |
22488 KB |
Output is correct |
24 |
Correct |
91 ms |
22460 KB |
Output is correct |
25 |
Correct |
117 ms |
22388 KB |
Output is correct |
26 |
Correct |
162 ms |
22524 KB |
Output is correct |
27 |
Correct |
203 ms |
22500 KB |
Output is correct |
28 |
Correct |
240 ms |
22512 KB |
Output is correct |
29 |
Correct |
300 ms |
22500 KB |
Output is correct |
30 |
Correct |
398 ms |
22552 KB |
Output is correct |
31 |
Correct |
404 ms |
22532 KB |
Output is correct |
32 |
Correct |
479 ms |
22476 KB |
Output is correct |
33 |
Correct |
398 ms |
22528 KB |
Output is correct |
34 |
Correct |
60 ms |
22848 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
2 ms |
212 KB |
Output is correct |
4 |
Execution timed out |
5052 ms |
12912 KB |
Time limit exceeded |
5 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
368 KB |
Output is correct |
12 |
Correct |
3 ms |
340 KB |
Output is correct |
13 |
Correct |
2 ms |
340 KB |
Output is correct |
14 |
Correct |
3 ms |
368 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
1 ms |
340 KB |
Output is correct |
17 |
Correct |
2 ms |
468 KB |
Output is correct |
18 |
Correct |
2 ms |
340 KB |
Output is correct |
19 |
Correct |
2 ms |
384 KB |
Output is correct |
20 |
Correct |
1 ms |
340 KB |
Output is correct |
21 |
Correct |
1 ms |
340 KB |
Output is correct |
22 |
Correct |
1 ms |
340 KB |
Output is correct |
23 |
Correct |
1 ms |
340 KB |
Output is correct |
24 |
Correct |
1 ms |
340 KB |
Output is correct |
25 |
Correct |
1 ms |
340 KB |
Output is correct |
26 |
Correct |
1 ms |
340 KB |
Output is correct |
27 |
Correct |
0 ms |
212 KB |
Output is correct |
28 |
Correct |
1 ms |
340 KB |
Output is correct |
29 |
Correct |
0 ms |
340 KB |
Output is correct |
30 |
Correct |
23 ms |
588 KB |
Output is correct |
31 |
Correct |
32 ms |
580 KB |
Output is correct |
32 |
Correct |
48 ms |
668 KB |
Output is correct |
33 |
Correct |
50 ms |
596 KB |
Output is correct |
34 |
Correct |
47 ms |
596 KB |
Output is correct |
35 |
Correct |
112 ms |
672 KB |
Output is correct |
36 |
Correct |
121 ms |
660 KB |
Output is correct |
37 |
Correct |
113 ms |
672 KB |
Output is correct |
38 |
Correct |
87 ms |
772 KB |
Output is correct |
39 |
Correct |
77 ms |
724 KB |
Output is correct |
40 |
Correct |
76 ms |
724 KB |
Output is correct |
41 |
Correct |
233 ms |
804 KB |
Output is correct |
42 |
Correct |
233 ms |
812 KB |
Output is correct |
43 |
Correct |
235 ms |
780 KB |
Output is correct |
44 |
Correct |
75 ms |
724 KB |
Output is correct |
45 |
Correct |
67 ms |
708 KB |
Output is correct |
46 |
Correct |
75 ms |
736 KB |
Output is correct |
47 |
Correct |
211 ms |
700 KB |
Output is correct |
48 |
Correct |
200 ms |
680 KB |
Output is correct |
49 |
Correct |
220 ms |
716 KB |
Output is correct |
50 |
Correct |
27 ms |
688 KB |
Output is correct |
51 |
Correct |
28 ms |
596 KB |
Output is correct |
52 |
Correct |
27 ms |
600 KB |
Output is correct |
53 |
Correct |
33 ms |
724 KB |
Output is correct |
54 |
Correct |
29 ms |
688 KB |
Output is correct |
55 |
Correct |
29 ms |
596 KB |
Output is correct |
56 |
Correct |
2 ms |
340 KB |
Output is correct |
57 |
Correct |
1 ms |
596 KB |
Output is correct |
58 |
Correct |
1 ms |
596 KB |
Output is correct |
59 |
Correct |
1 ms |
212 KB |
Output is correct |
60 |
Correct |
1 ms |
212 KB |
Output is correct |
61 |
Correct |
2 ms |
340 KB |
Output is correct |
62 |
Correct |
3531 ms |
18604 KB |
Output is correct |
63 |
Correct |
2170 ms |
27848 KB |
Output is correct |
64 |
Correct |
2736 ms |
31524 KB |
Output is correct |
65 |
Execution timed out |
5103 ms |
34956 KB |
Time limit exceeded |
66 |
Halted |
0 ms |
0 KB |
- |