#include "bits/stdc++.h"
using namespace std;
typedef vector<int> vi;
typedef vector<vi> vvi;
#define sz(x) (int)(x).size()
#define all(x) (x).begin(), (x).end()
int N, M, Q;
vi C;
vector<set<int>> CC;
vvi adj;
vi in, out;
vi depth;
vvi vorg;
void dfs(int v, int p, int &idx)
{
in[v] = idx++;
depth[v] = depth[p] + 1;
vorg[v][0] = p;
for (int i = 1; i < sz(vorg[v]); ++i)
vorg[v][i] = vorg[vorg[v][i - 1]][i - 1];
for (int u : adj[v])
if (u != p)
dfs(u, v, idx);
out[v] = idx++;
}
bool is_vorg(int a, int b)
{
return in[a] <= in[b] && out[b] <= out[a];
}
int lca(int a, int b)
{
if (is_vorg(a, b))
return a;
if (is_vorg(b, a))
return b;
int tmp;
for (int j = sz(vorg[a]) - 1; j >= 0; --j)
{
tmp = vorg[a][j];
if (!is_vorg(tmp, b))
a = tmp;
}
return vorg[a][0];
}
int main()
{
ios::sync_with_stdio(false);
cin.tie(0);
cin >> N >> M >> Q;
adj.resize(N);
for (int i = 0, a, b; i < N - 1; ++i)
{
cin >> a >> b;
--a, --b;
adj[a].push_back(b), adj[b].push_back(a);
}
int idx = 0;
in.resize(N), out.resize(N), depth.assign(N, -1);
vorg.assign(N, vi(ceil(log2(N) + 1)));
dfs(0, 0, idx);
C.resize(M), CC.resize(N);
for (int i = 0; i < M; ++i)
{
cin >> C[i];
--C[i];
CC[C[i]].insert(i);
}
for (int i = 0, l, r; i < Q; ++i)
{
cin >> l >> r;
--l;
if (r - l == 1)
{
cout << 1 << "\n";
continue;
}
auto cmp = [&](int a, int b)
{ return out[a] > out[b]; };
priority_queue<int, vi, decltype(cmp)> q(cmp);
for (int j = l; j < r; ++j)
q.push(C[j]);
stack<int> st;
int ans = 1;
int current = q.top();
q.pop();
auto forward = [&]()
{
st.push(current);
current = q.top();
q.pop();
};
auto backward = [&]()
{
int lc = lca(current, st.top());
ans += depth[current] - depth[lc];
ans += depth[st.top()] - depth[lc];
st.pop();
current = lc;
};
while (sz(st) + sz(q) > 0)
{
if (sz(st) == 0)
{
forward();
}
else if (sz(q) == 0)
{
backward();
}
else if (sz(q) > 0 && sz(st) > 0)
{
if (out[lca(current, st.top())] > out[lca(current, q.top())])
{
forward();
}
else
{
backward();
}
}
}
cout << ans << "\n";
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
3 ms |
320 KB |
Output is correct |
5 |
Correct |
2 ms |
340 KB |
Output is correct |
6 |
Correct |
2 ms |
340 KB |
Output is correct |
7 |
Correct |
3 ms |
340 KB |
Output is correct |
8 |
Correct |
2 ms |
340 KB |
Output is correct |
9 |
Correct |
5 ms |
404 KB |
Output is correct |
10 |
Correct |
4 ms |
404 KB |
Output is correct |
11 |
Correct |
4 ms |
340 KB |
Output is correct |
12 |
Correct |
10 ms |
340 KB |
Output is correct |
13 |
Correct |
9 ms |
404 KB |
Output is correct |
14 |
Correct |
10 ms |
340 KB |
Output is correct |
15 |
Correct |
5 ms |
340 KB |
Output is correct |
16 |
Correct |
4 ms |
340 KB |
Output is correct |
17 |
Correct |
3 ms |
340 KB |
Output is correct |
18 |
Correct |
4 ms |
408 KB |
Output is correct |
19 |
Correct |
4 ms |
340 KB |
Output is correct |
20 |
Correct |
4 ms |
340 KB |
Output is correct |
21 |
Correct |
4 ms |
340 KB |
Output is correct |
22 |
Correct |
3 ms |
340 KB |
Output is correct |
23 |
Correct |
3 ms |
340 KB |
Output is correct |
24 |
Correct |
3 ms |
340 KB |
Output is correct |
25 |
Correct |
3 ms |
340 KB |
Output is correct |
26 |
Correct |
4 ms |
404 KB |
Output is correct |
27 |
Correct |
2 ms |
340 KB |
Output is correct |
28 |
Correct |
1 ms |
340 KB |
Output is correct |
29 |
Correct |
1 ms |
340 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 |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
3 ms |
320 KB |
Output is correct |
5 |
Correct |
2 ms |
340 KB |
Output is correct |
6 |
Correct |
2 ms |
340 KB |
Output is correct |
7 |
Correct |
3 ms |
340 KB |
Output is correct |
8 |
Correct |
2 ms |
340 KB |
Output is correct |
9 |
Correct |
5 ms |
404 KB |
Output is correct |
10 |
Correct |
4 ms |
404 KB |
Output is correct |
11 |
Correct |
4 ms |
340 KB |
Output is correct |
12 |
Correct |
10 ms |
340 KB |
Output is correct |
13 |
Correct |
9 ms |
404 KB |
Output is correct |
14 |
Correct |
10 ms |
340 KB |
Output is correct |
15 |
Correct |
5 ms |
340 KB |
Output is correct |
16 |
Correct |
4 ms |
340 KB |
Output is correct |
17 |
Correct |
3 ms |
340 KB |
Output is correct |
18 |
Correct |
4 ms |
408 KB |
Output is correct |
19 |
Correct |
4 ms |
340 KB |
Output is correct |
20 |
Correct |
4 ms |
340 KB |
Output is correct |
21 |
Correct |
4 ms |
340 KB |
Output is correct |
22 |
Correct |
3 ms |
340 KB |
Output is correct |
23 |
Correct |
3 ms |
340 KB |
Output is correct |
24 |
Correct |
3 ms |
340 KB |
Output is correct |
25 |
Correct |
3 ms |
340 KB |
Output is correct |
26 |
Correct |
4 ms |
404 KB |
Output is correct |
27 |
Correct |
2 ms |
340 KB |
Output is correct |
28 |
Correct |
1 ms |
340 KB |
Output is correct |
29 |
Correct |
1 ms |
340 KB |
Output is correct |
30 |
Correct |
92 ms |
724 KB |
Output is correct |
31 |
Correct |
139 ms |
724 KB |
Output is correct |
32 |
Correct |
192 ms |
872 KB |
Output is correct |
33 |
Correct |
227 ms |
880 KB |
Output is correct |
34 |
Correct |
208 ms |
992 KB |
Output is correct |
35 |
Correct |
517 ms |
868 KB |
Output is correct |
36 |
Correct |
591 ms |
1000 KB |
Output is correct |
37 |
Correct |
519 ms |
868 KB |
Output is correct |
38 |
Correct |
141 ms |
948 KB |
Output is correct |
39 |
Correct |
180 ms |
960 KB |
Output is correct |
40 |
Correct |
129 ms |
972 KB |
Output is correct |
41 |
Correct |
376 ms |
980 KB |
Output is correct |
42 |
Correct |
370 ms |
980 KB |
Output is correct |
43 |
Correct |
511 ms |
944 KB |
Output is correct |
44 |
Correct |
220 ms |
904 KB |
Output is correct |
45 |
Correct |
205 ms |
844 KB |
Output is correct |
46 |
Correct |
181 ms |
840 KB |
Output is correct |
47 |
Correct |
506 ms |
1016 KB |
Output is correct |
48 |
Correct |
583 ms |
888 KB |
Output is correct |
49 |
Correct |
588 ms |
852 KB |
Output is correct |
50 |
Correct |
129 ms |
852 KB |
Output is correct |
51 |
Correct |
135 ms |
876 KB |
Output is correct |
52 |
Correct |
134 ms |
872 KB |
Output is correct |
53 |
Correct |
128 ms |
852 KB |
Output is correct |
54 |
Correct |
131 ms |
872 KB |
Output is correct |
55 |
Correct |
133 ms |
872 KB |
Output is correct |
56 |
Correct |
73 ms |
340 KB |
Output is correct |
57 |
Correct |
2 ms |
724 KB |
Output is correct |
58 |
Correct |
2 ms |
852 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
2 ms |
340 KB |
Output is correct |
3 |
Correct |
85 ms |
340 KB |
Output is correct |
4 |
Execution timed out |
5076 ms |
21740 KB |
Time limit exceeded |
5 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
62 ms |
15464 KB |
Output is correct |
3 |
Correct |
68 ms |
18272 KB |
Output is correct |
4 |
Correct |
60 ms |
18888 KB |
Output is correct |
5 |
Correct |
104 ms |
28240 KB |
Output is correct |
6 |
Correct |
107 ms |
28080 KB |
Output is correct |
7 |
Correct |
110 ms |
27796 KB |
Output is correct |
8 |
Correct |
120 ms |
27708 KB |
Output is correct |
9 |
Correct |
108 ms |
27592 KB |
Output is correct |
10 |
Correct |
116 ms |
27656 KB |
Output is correct |
11 |
Correct |
103 ms |
27572 KB |
Output is correct |
12 |
Correct |
119 ms |
27808 KB |
Output is correct |
13 |
Correct |
98 ms |
27676 KB |
Output is correct |
14 |
Correct |
98 ms |
27684 KB |
Output is correct |
15 |
Correct |
99 ms |
27856 KB |
Output is correct |
16 |
Correct |
117 ms |
27992 KB |
Output is correct |
17 |
Correct |
103 ms |
28048 KB |
Output is correct |
18 |
Correct |
113 ms |
28008 KB |
Output is correct |
19 |
Correct |
141 ms |
28296 KB |
Output is correct |
20 |
Correct |
117 ms |
28140 KB |
Output is correct |
21 |
Correct |
143 ms |
27876 KB |
Output is correct |
22 |
Correct |
117 ms |
27748 KB |
Output is correct |
23 |
Correct |
117 ms |
27668 KB |
Output is correct |
24 |
Correct |
148 ms |
27704 KB |
Output is correct |
25 |
Correct |
128 ms |
27564 KB |
Output is correct |
26 |
Correct |
136 ms |
27636 KB |
Output is correct |
27 |
Correct |
150 ms |
27588 KB |
Output is correct |
28 |
Correct |
156 ms |
27664 KB |
Output is correct |
29 |
Correct |
158 ms |
27672 KB |
Output is correct |
30 |
Correct |
167 ms |
27732 KB |
Output is correct |
31 |
Correct |
134 ms |
27704 KB |
Output is correct |
32 |
Correct |
128 ms |
27684 KB |
Output is correct |
33 |
Correct |
113 ms |
27776 KB |
Output is correct |
34 |
Correct |
101 ms |
27904 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
320 KB |
Output is correct |
2 |
Correct |
2 ms |
340 KB |
Output is correct |
3 |
Correct |
73 ms |
452 KB |
Output is correct |
4 |
Execution timed out |
5031 ms |
18424 KB |
Time limit exceeded |
5 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
3 ms |
320 KB |
Output is correct |
5 |
Correct |
2 ms |
340 KB |
Output is correct |
6 |
Correct |
2 ms |
340 KB |
Output is correct |
7 |
Correct |
3 ms |
340 KB |
Output is correct |
8 |
Correct |
2 ms |
340 KB |
Output is correct |
9 |
Correct |
5 ms |
404 KB |
Output is correct |
10 |
Correct |
4 ms |
404 KB |
Output is correct |
11 |
Correct |
4 ms |
340 KB |
Output is correct |
12 |
Correct |
10 ms |
340 KB |
Output is correct |
13 |
Correct |
9 ms |
404 KB |
Output is correct |
14 |
Correct |
10 ms |
340 KB |
Output is correct |
15 |
Correct |
5 ms |
340 KB |
Output is correct |
16 |
Correct |
4 ms |
340 KB |
Output is correct |
17 |
Correct |
3 ms |
340 KB |
Output is correct |
18 |
Correct |
4 ms |
408 KB |
Output is correct |
19 |
Correct |
4 ms |
340 KB |
Output is correct |
20 |
Correct |
4 ms |
340 KB |
Output is correct |
21 |
Correct |
4 ms |
340 KB |
Output is correct |
22 |
Correct |
3 ms |
340 KB |
Output is correct |
23 |
Correct |
3 ms |
340 KB |
Output is correct |
24 |
Correct |
3 ms |
340 KB |
Output is correct |
25 |
Correct |
3 ms |
340 KB |
Output is correct |
26 |
Correct |
4 ms |
404 KB |
Output is correct |
27 |
Correct |
2 ms |
340 KB |
Output is correct |
28 |
Correct |
1 ms |
340 KB |
Output is correct |
29 |
Correct |
1 ms |
340 KB |
Output is correct |
30 |
Correct |
92 ms |
724 KB |
Output is correct |
31 |
Correct |
139 ms |
724 KB |
Output is correct |
32 |
Correct |
192 ms |
872 KB |
Output is correct |
33 |
Correct |
227 ms |
880 KB |
Output is correct |
34 |
Correct |
208 ms |
992 KB |
Output is correct |
35 |
Correct |
517 ms |
868 KB |
Output is correct |
36 |
Correct |
591 ms |
1000 KB |
Output is correct |
37 |
Correct |
519 ms |
868 KB |
Output is correct |
38 |
Correct |
141 ms |
948 KB |
Output is correct |
39 |
Correct |
180 ms |
960 KB |
Output is correct |
40 |
Correct |
129 ms |
972 KB |
Output is correct |
41 |
Correct |
376 ms |
980 KB |
Output is correct |
42 |
Correct |
370 ms |
980 KB |
Output is correct |
43 |
Correct |
511 ms |
944 KB |
Output is correct |
44 |
Correct |
220 ms |
904 KB |
Output is correct |
45 |
Correct |
205 ms |
844 KB |
Output is correct |
46 |
Correct |
181 ms |
840 KB |
Output is correct |
47 |
Correct |
506 ms |
1016 KB |
Output is correct |
48 |
Correct |
583 ms |
888 KB |
Output is correct |
49 |
Correct |
588 ms |
852 KB |
Output is correct |
50 |
Correct |
129 ms |
852 KB |
Output is correct |
51 |
Correct |
135 ms |
876 KB |
Output is correct |
52 |
Correct |
134 ms |
872 KB |
Output is correct |
53 |
Correct |
128 ms |
852 KB |
Output is correct |
54 |
Correct |
131 ms |
872 KB |
Output is correct |
55 |
Correct |
133 ms |
872 KB |
Output is correct |
56 |
Correct |
73 ms |
340 KB |
Output is correct |
57 |
Correct |
2 ms |
724 KB |
Output is correct |
58 |
Correct |
2 ms |
852 KB |
Output is correct |
59 |
Correct |
1 ms |
212 KB |
Output is correct |
60 |
Correct |
2 ms |
340 KB |
Output is correct |
61 |
Correct |
85 ms |
340 KB |
Output is correct |
62 |
Execution timed out |
5076 ms |
21740 KB |
Time limit exceeded |
63 |
Halted |
0 ms |
0 KB |
- |