#define ll long long
#define pll pair<ll, ll>
#define INF numeric_limits<int>::max()/2
#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];
}
ll left(ll i)
{
return 2*i;
}
ll right(ll i)
{
return 2*i+1;
}
pll conjugate(pll a, pll b)
{
return {max(a.first, b.first), min(a.second, b.second)};
}
pll query3(ll i, vector<pll>& s, ll a, ll b, ll l, ll r)
{
if(l <= a && b <= r)
{
return s[i];
}
if(b < l || r < a)
{
return {-1, INF};
}
ll mid = (a+b)/2;
return conjugate(query3(left(i), s, a, mid, l, r), query3(right(i), s, mid+1, b, l, r));
}
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]--;
}
segnodes = 1;
while(2*m > segnodes)
{
segnodes *= 2;
}
vector<pll> s3(segnodes, {-1, INF}); //Max, min
if(sub3)
{
for(ll i = 0; i < m; i++)
{
s3[i+segnodes/2].first = depth[c[i]];
s3[i+segnodes/2].second = depth[c[i]];
}
for(ll i = segnodes/2-1; i > 0; i--)
{
s3[i].first = max(s3[left(i)].first, s3[right(i)].first);
s3[i].second = min(s3[left(i)].second, s3[right(i)].second);
}
}
for(ll x = 0; x < q; x++)
{
ll left, right;
cin>>left>>right;
left--; right--;
if(left == right)
{
cout<<1<<"\n";
continue;
}
if(sub3)
{
pll range = query3(1, s3, 1, segnodes/2, left+1, right+1);
cout<<range.first+1-range.second<<"\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;
}
# |
Verdict |
Execution time |
Memory |
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 |
340 KB |
Output is correct |
12 |
Correct |
2 ms |
340 KB |
Output is correct |
13 |
Correct |
2 ms |
340 KB |
Output is correct |
14 |
Correct |
2 ms |
340 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 |
340 KB |
Output is correct |
18 |
Correct |
1 ms |
340 KB |
Output is correct |
19 |
Correct |
1 ms |
340 KB |
Output is correct |
20 |
Correct |
1 ms |
340 KB |
Output is correct |
21 |
Correct |
2 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 |
1 ms |
212 KB |
Output is correct |
28 |
Correct |
1 ms |
312 KB |
Output is correct |
29 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
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 |
340 KB |
Output is correct |
12 |
Correct |
2 ms |
340 KB |
Output is correct |
13 |
Correct |
2 ms |
340 KB |
Output is correct |
14 |
Correct |
2 ms |
340 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 |
340 KB |
Output is correct |
18 |
Correct |
1 ms |
340 KB |
Output is correct |
19 |
Correct |
1 ms |
340 KB |
Output is correct |
20 |
Correct |
1 ms |
340 KB |
Output is correct |
21 |
Correct |
2 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 |
1 ms |
212 KB |
Output is correct |
28 |
Correct |
1 ms |
312 KB |
Output is correct |
29 |
Correct |
1 ms |
340 KB |
Output is correct |
30 |
Correct |
24 ms |
684 KB |
Output is correct |
31 |
Correct |
32 ms |
580 KB |
Output is correct |
32 |
Correct |
49 ms |
764 KB |
Output is correct |
33 |
Correct |
51 ms |
784 KB |
Output is correct |
34 |
Correct |
47 ms |
724 KB |
Output is correct |
35 |
Correct |
120 ms |
900 KB |
Output is correct |
36 |
Correct |
119 ms |
768 KB |
Output is correct |
37 |
Correct |
114 ms |
780 KB |
Output is correct |
38 |
Correct |
77 ms |
896 KB |
Output is correct |
39 |
Correct |
77 ms |
908 KB |
Output is correct |
40 |
Correct |
79 ms |
920 KB |
Output is correct |
41 |
Correct |
233 ms |
916 KB |
Output is correct |
42 |
Correct |
237 ms |
928 KB |
Output is correct |
43 |
Correct |
241 ms |
876 KB |
Output is correct |
44 |
Correct |
71 ms |
844 KB |
Output is correct |
45 |
Correct |
67 ms |
812 KB |
Output is correct |
46 |
Correct |
74 ms |
840 KB |
Output is correct |
47 |
Correct |
215 ms |
724 KB |
Output is correct |
48 |
Correct |
205 ms |
800 KB |
Output is correct |
49 |
Correct |
208 ms |
824 KB |
Output is correct |
50 |
Correct |
32 ms |
724 KB |
Output is correct |
51 |
Correct |
27 ms |
796 KB |
Output is correct |
52 |
Correct |
26 ms |
708 KB |
Output is correct |
53 |
Correct |
26 ms |
796 KB |
Output is correct |
54 |
Correct |
29 ms |
724 KB |
Output is correct |
55 |
Correct |
27 ms |
792 KB |
Output is correct |
56 |
Correct |
1 ms |
328 KB |
Output is correct |
57 |
Correct |
1 ms |
596 KB |
Output is correct |
58 |
Correct |
2 ms |
724 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
324 KB |
Output is correct |
4 |
Correct |
66 ms |
24068 KB |
Output is correct |
5 |
Correct |
64 ms |
29860 KB |
Output is correct |
6 |
Correct |
69 ms |
35620 KB |
Output is correct |
7 |
Correct |
101 ms |
39176 KB |
Output is correct |
8 |
Correct |
93 ms |
39188 KB |
Output is correct |
9 |
Correct |
91 ms |
39164 KB |
Output is correct |
10 |
Correct |
100 ms |
39308 KB |
Output is correct |
11 |
Correct |
94 ms |
39224 KB |
Output is correct |
12 |
Correct |
82 ms |
38972 KB |
Output is correct |
13 |
Correct |
92 ms |
39052 KB |
Output is correct |
14 |
Correct |
81 ms |
39052 KB |
Output is correct |
15 |
Correct |
49 ms |
32644 KB |
Output is correct |
16 |
Correct |
66 ms |
38756 KB |
Output is correct |
17 |
Correct |
54 ms |
6680 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
38 ms |
14292 KB |
Output is correct |
3 |
Correct |
39 ms |
17572 KB |
Output is correct |
4 |
Correct |
42 ms |
19640 KB |
Output is correct |
5 |
Correct |
54 ms |
26940 KB |
Output is correct |
6 |
Correct |
55 ms |
26904 KB |
Output is correct |
7 |
Correct |
56 ms |
26896 KB |
Output is correct |
8 |
Correct |
55 ms |
26988 KB |
Output is correct |
9 |
Correct |
57 ms |
27004 KB |
Output is correct |
10 |
Correct |
60 ms |
27000 KB |
Output is correct |
11 |
Correct |
70 ms |
27000 KB |
Output is correct |
12 |
Correct |
82 ms |
27016 KB |
Output is correct |
13 |
Correct |
72 ms |
26944 KB |
Output is correct |
14 |
Correct |
95 ms |
27116 KB |
Output is correct |
15 |
Correct |
64 ms |
27096 KB |
Output is correct |
16 |
Correct |
78 ms |
27032 KB |
Output is correct |
17 |
Correct |
86 ms |
27008 KB |
Output is correct |
18 |
Correct |
67 ms |
27020 KB |
Output is correct |
19 |
Correct |
56 ms |
26588 KB |
Output is correct |
20 |
Correct |
55 ms |
26556 KB |
Output is correct |
21 |
Correct |
61 ms |
26512 KB |
Output is correct |
22 |
Correct |
62 ms |
26484 KB |
Output is correct |
23 |
Correct |
75 ms |
26476 KB |
Output is correct |
24 |
Correct |
88 ms |
26464 KB |
Output is correct |
25 |
Correct |
127 ms |
26564 KB |
Output is correct |
26 |
Correct |
141 ms |
26628 KB |
Output is correct |
27 |
Correct |
197 ms |
26604 KB |
Output is correct |
28 |
Correct |
258 ms |
26560 KB |
Output is correct |
29 |
Correct |
301 ms |
26608 KB |
Output is correct |
30 |
Correct |
362 ms |
26652 KB |
Output is correct |
31 |
Correct |
405 ms |
26640 KB |
Output is correct |
32 |
Correct |
464 ms |
26612 KB |
Output is correct |
33 |
Correct |
393 ms |
26800 KB |
Output is correct |
34 |
Correct |
63 ms |
26788 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Execution timed out |
5045 ms |
18428 KB |
Time limit exceeded |
5 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
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 |
340 KB |
Output is correct |
12 |
Correct |
2 ms |
340 KB |
Output is correct |
13 |
Correct |
2 ms |
340 KB |
Output is correct |
14 |
Correct |
2 ms |
340 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 |
340 KB |
Output is correct |
18 |
Correct |
1 ms |
340 KB |
Output is correct |
19 |
Correct |
1 ms |
340 KB |
Output is correct |
20 |
Correct |
1 ms |
340 KB |
Output is correct |
21 |
Correct |
2 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 |
1 ms |
212 KB |
Output is correct |
28 |
Correct |
1 ms |
312 KB |
Output is correct |
29 |
Correct |
1 ms |
340 KB |
Output is correct |
30 |
Correct |
24 ms |
684 KB |
Output is correct |
31 |
Correct |
32 ms |
580 KB |
Output is correct |
32 |
Correct |
49 ms |
764 KB |
Output is correct |
33 |
Correct |
51 ms |
784 KB |
Output is correct |
34 |
Correct |
47 ms |
724 KB |
Output is correct |
35 |
Correct |
120 ms |
900 KB |
Output is correct |
36 |
Correct |
119 ms |
768 KB |
Output is correct |
37 |
Correct |
114 ms |
780 KB |
Output is correct |
38 |
Correct |
77 ms |
896 KB |
Output is correct |
39 |
Correct |
77 ms |
908 KB |
Output is correct |
40 |
Correct |
79 ms |
920 KB |
Output is correct |
41 |
Correct |
233 ms |
916 KB |
Output is correct |
42 |
Correct |
237 ms |
928 KB |
Output is correct |
43 |
Correct |
241 ms |
876 KB |
Output is correct |
44 |
Correct |
71 ms |
844 KB |
Output is correct |
45 |
Correct |
67 ms |
812 KB |
Output is correct |
46 |
Correct |
74 ms |
840 KB |
Output is correct |
47 |
Correct |
215 ms |
724 KB |
Output is correct |
48 |
Correct |
205 ms |
800 KB |
Output is correct |
49 |
Correct |
208 ms |
824 KB |
Output is correct |
50 |
Correct |
32 ms |
724 KB |
Output is correct |
51 |
Correct |
27 ms |
796 KB |
Output is correct |
52 |
Correct |
26 ms |
708 KB |
Output is correct |
53 |
Correct |
26 ms |
796 KB |
Output is correct |
54 |
Correct |
29 ms |
724 KB |
Output is correct |
55 |
Correct |
27 ms |
792 KB |
Output is correct |
56 |
Correct |
1 ms |
328 KB |
Output is correct |
57 |
Correct |
1 ms |
596 KB |
Output is correct |
58 |
Correct |
2 ms |
724 KB |
Output is correct |
59 |
Correct |
0 ms |
212 KB |
Output is correct |
60 |
Correct |
0 ms |
340 KB |
Output is correct |
61 |
Correct |
1 ms |
324 KB |
Output is correct |
62 |
Correct |
66 ms |
24068 KB |
Output is correct |
63 |
Correct |
64 ms |
29860 KB |
Output is correct |
64 |
Correct |
69 ms |
35620 KB |
Output is correct |
65 |
Correct |
101 ms |
39176 KB |
Output is correct |
66 |
Correct |
93 ms |
39188 KB |
Output is correct |
67 |
Correct |
91 ms |
39164 KB |
Output is correct |
68 |
Correct |
100 ms |
39308 KB |
Output is correct |
69 |
Correct |
94 ms |
39224 KB |
Output is correct |
70 |
Correct |
82 ms |
38972 KB |
Output is correct |
71 |
Correct |
92 ms |
39052 KB |
Output is correct |
72 |
Correct |
81 ms |
39052 KB |
Output is correct |
73 |
Correct |
49 ms |
32644 KB |
Output is correct |
74 |
Correct |
66 ms |
38756 KB |
Output is correct |
75 |
Correct |
54 ms |
6680 KB |
Output is correct |
76 |
Correct |
1 ms |
212 KB |
Output is correct |
77 |
Correct |
38 ms |
14292 KB |
Output is correct |
78 |
Correct |
39 ms |
17572 KB |
Output is correct |
79 |
Correct |
42 ms |
19640 KB |
Output is correct |
80 |
Correct |
54 ms |
26940 KB |
Output is correct |
81 |
Correct |
55 ms |
26904 KB |
Output is correct |
82 |
Correct |
56 ms |
26896 KB |
Output is correct |
83 |
Correct |
55 ms |
26988 KB |
Output is correct |
84 |
Correct |
57 ms |
27004 KB |
Output is correct |
85 |
Correct |
60 ms |
27000 KB |
Output is correct |
86 |
Correct |
70 ms |
27000 KB |
Output is correct |
87 |
Correct |
82 ms |
27016 KB |
Output is correct |
88 |
Correct |
72 ms |
26944 KB |
Output is correct |
89 |
Correct |
95 ms |
27116 KB |
Output is correct |
90 |
Correct |
64 ms |
27096 KB |
Output is correct |
91 |
Correct |
78 ms |
27032 KB |
Output is correct |
92 |
Correct |
86 ms |
27008 KB |
Output is correct |
93 |
Correct |
67 ms |
27020 KB |
Output is correct |
94 |
Correct |
56 ms |
26588 KB |
Output is correct |
95 |
Correct |
55 ms |
26556 KB |
Output is correct |
96 |
Correct |
61 ms |
26512 KB |
Output is correct |
97 |
Correct |
62 ms |
26484 KB |
Output is correct |
98 |
Correct |
75 ms |
26476 KB |
Output is correct |
99 |
Correct |
88 ms |
26464 KB |
Output is correct |
100 |
Correct |
127 ms |
26564 KB |
Output is correct |
101 |
Correct |
141 ms |
26628 KB |
Output is correct |
102 |
Correct |
197 ms |
26604 KB |
Output is correct |
103 |
Correct |
258 ms |
26560 KB |
Output is correct |
104 |
Correct |
301 ms |
26608 KB |
Output is correct |
105 |
Correct |
362 ms |
26652 KB |
Output is correct |
106 |
Correct |
405 ms |
26640 KB |
Output is correct |
107 |
Correct |
464 ms |
26612 KB |
Output is correct |
108 |
Correct |
393 ms |
26800 KB |
Output is correct |
109 |
Correct |
63 ms |
26788 KB |
Output is correct |
110 |
Correct |
0 ms |
212 KB |
Output is correct |
111 |
Correct |
0 ms |
212 KB |
Output is correct |
112 |
Correct |
1 ms |
340 KB |
Output is correct |
113 |
Execution timed out |
5045 ms |
18428 KB |
Time limit exceeded |
114 |
Halted |
0 ms |
0 KB |
- |