#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 1e5 + 12, MOD = (int)1e9 + 7;
const int L = 19;
vector<int> g[N];
int n, m, q, c[N], tin[N], tout[N], timer, s[N], head[N], ver[N];
int up[N][19], dep[N];
void bb(int v, int pr = -1) {
s[v] = 1;
for(int i = 0; i < (int)g[v].size(); i++) {
int to = g[v][i];
if(to == pr) continue;
bb(to, v);
s[v] += s[to];
if(s[to] > s[g[v][0]] || g[v][0] == pr) {
swap(g[v][i], g[v][0]);
}
}
}
void hld(int v, int pr = -1) {
tin[v] = ++timer;
ver[timer] = v;
for(int to:g[v]) {
if(to == pr) continue;
if(to == g[v][0]) {
head[to] = head[v];
} else {
head[to] = to;
}
hld(to, v);
}
tout[v] = timer;
}
vector<pair<int,int>> qr[N];
void dfs(int v, int pr = 1) {
up[v][0] = pr;
for(int i = 1; i < L; ++i) {
up[v][i] = up[up[v][i - 1]][i - 1];
}
for(int to:g[v]) {
if(to != pr) {
dep[to] = dep[v] + 1;
dfs(to, v);
}
}
}
bool is(int v, int u) {
return (tin[v] <= tin[u] && tout[v] >= tout[u]);
}
int lca(int v, int u) {
if(is(v, u)) return v;
if(is(u, v)) return u;
for(int i = L - 1; i >= 0; i--) {
if(!is(up[v][i], u)) {
v = up[v][i];
}
}
return up[v][0];
}
pair<int, int> t[N * 4];
void build(int v = 1, int tl = 1, int tr = m - 1) {
if(tl == tr) {
int x = lca(c[tl], c[tl + 1]);
t[v] = {dep[x], x};
} else {
int tm = (tl + tr) >> 1;
build(v + v, tl, tm);
build(v + v + 1, tm + 1, tr);
t[v] = min(t[v + v], t[v + v + 1]);
}
}
const int inf = 1e9;
pair<int, int> get(int l, int r, int v = 1, int tl = 1, int tr = m - 1) {
if(l > r || tl > r || l > tr) return {inf, inf};
if(tl >= l && tr <= r) return t[v];
int tm = (tl + tr) >> 1;
return min(get(l, r, v + v, tl, tm), get(l, r, v + v + 1, tm + 1, tr));
}
int mn[N * 4], mod[N * 4], mx[N * 4];
void make(int v, int val) {
mod[v] = mx[v] = mn[v] = val;
}
void push(int v) {
if(mod[v]) {
make(v + v, mod[v]);
make(v + v + 1, mod[v]);
mod[v] = 0;
}
}
void upd(int l, int r, int val, int v = 1, int tl = 1, int tr = n) {
if(l > r|| tl > r || l > tr) return;
if(tl >= l && tr <= r) {
make(v, val);
} else {
push(v);
int tm = (tl + tr) >> 1;
upd(l, r, val, v + v, tl, tm);
upd(l, r, val, v + v + 1, tm + 1, tr);
mx[v] = max(mx[v + v], mx[v + v + 1]);
mn[v] = min(mn[v + v], mn[v + v + 1]);
}
}
int find(int val, int v = 1, int tl = 1, int tr = n) {
if(mn[v] >= val) {
return (tr - tl + 1);
}
if(mx[v] < val) {
return 0;
}
push(v);
int tm = (tl + tr) >> 1;
return find(val,v + v, tl, tm) + find(val, v + v + 1, tm + 1, tr);
}
void nv(int v, int i) {
do {
upd(tin[head[v]], tin[v], i);
v = up[head[v]][0];
}while(v > 1);
}
int res[N];
void test() {
cin >> n >> m >> q;
for(int i = 1; i <= n - 1; i++) {
int a, b;
cin >> a >> b;
g[a].push_back(b);
g[b].push_back(a);
}
for(int i = 1; i <= m; i++) {
cin >> c[i];
}
dep[1] = 1;
bb(1);
head[1] = 1;
hld(1);
dfs(1);
if(m != 1)build();
for(int i = 1; i <= q; i++) {
int l, r;
cin >> l >> r;
if(l == r) {
res[i] = 1;
continue;
}
qr[r].push_back({l, i});
}
for(int i = 1; i <= m; i++) {
nv(c[i], i);
for(auto [l, id]:qr[i]) {
int x = get(l, i - 1).second;
res[id] = find(l) - dep[x] + 1;
}
}
for(int i = 1; i <= q; i++) {
cout << res[i] << '\n';
}
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
int t = 1;
// cin >> t;
while(t--)
test();
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
16988 KB |
Output is correct |
2 |
Correct |
2 ms |
19036 KB |
Output is correct |
3 |
Correct |
2 ms |
16988 KB |
Output is correct |
4 |
Correct |
2 ms |
17176 KB |
Output is correct |
5 |
Correct |
2 ms |
17244 KB |
Output is correct |
6 |
Correct |
2 ms |
17244 KB |
Output is correct |
7 |
Correct |
2 ms |
17244 KB |
Output is correct |
8 |
Correct |
2 ms |
17244 KB |
Output is correct |
9 |
Correct |
3 ms |
17244 KB |
Output is correct |
10 |
Correct |
2 ms |
17244 KB |
Output is correct |
11 |
Correct |
3 ms |
17248 KB |
Output is correct |
12 |
Correct |
3 ms |
17240 KB |
Output is correct |
13 |
Correct |
3 ms |
17244 KB |
Output is correct |
14 |
Correct |
3 ms |
17244 KB |
Output is correct |
15 |
Correct |
2 ms |
17244 KB |
Output is correct |
16 |
Correct |
3 ms |
17240 KB |
Output is correct |
17 |
Correct |
3 ms |
17244 KB |
Output is correct |
18 |
Correct |
3 ms |
17240 KB |
Output is correct |
19 |
Correct |
3 ms |
17244 KB |
Output is correct |
20 |
Correct |
3 ms |
17244 KB |
Output is correct |
21 |
Correct |
3 ms |
17244 KB |
Output is correct |
22 |
Correct |
3 ms |
17244 KB |
Output is correct |
23 |
Correct |
3 ms |
17244 KB |
Output is correct |
24 |
Correct |
3 ms |
17244 KB |
Output is correct |
25 |
Correct |
3 ms |
17244 KB |
Output is correct |
26 |
Correct |
3 ms |
17244 KB |
Output is correct |
27 |
Correct |
2 ms |
19036 KB |
Output is correct |
28 |
Correct |
2 ms |
15196 KB |
Output is correct |
29 |
Correct |
3 ms |
17496 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
16988 KB |
Output is correct |
2 |
Correct |
2 ms |
19036 KB |
Output is correct |
3 |
Correct |
2 ms |
16988 KB |
Output is correct |
4 |
Correct |
2 ms |
17176 KB |
Output is correct |
5 |
Correct |
2 ms |
17244 KB |
Output is correct |
6 |
Correct |
2 ms |
17244 KB |
Output is correct |
7 |
Correct |
2 ms |
17244 KB |
Output is correct |
8 |
Correct |
2 ms |
17244 KB |
Output is correct |
9 |
Correct |
3 ms |
17244 KB |
Output is correct |
10 |
Correct |
2 ms |
17244 KB |
Output is correct |
11 |
Correct |
3 ms |
17248 KB |
Output is correct |
12 |
Correct |
3 ms |
17240 KB |
Output is correct |
13 |
Correct |
3 ms |
17244 KB |
Output is correct |
14 |
Correct |
3 ms |
17244 KB |
Output is correct |
15 |
Correct |
2 ms |
17244 KB |
Output is correct |
16 |
Correct |
3 ms |
17240 KB |
Output is correct |
17 |
Correct |
3 ms |
17244 KB |
Output is correct |
18 |
Correct |
3 ms |
17240 KB |
Output is correct |
19 |
Correct |
3 ms |
17244 KB |
Output is correct |
20 |
Correct |
3 ms |
17244 KB |
Output is correct |
21 |
Correct |
3 ms |
17244 KB |
Output is correct |
22 |
Correct |
3 ms |
17244 KB |
Output is correct |
23 |
Correct |
3 ms |
17244 KB |
Output is correct |
24 |
Correct |
3 ms |
17244 KB |
Output is correct |
25 |
Correct |
3 ms |
17244 KB |
Output is correct |
26 |
Correct |
3 ms |
17244 KB |
Output is correct |
27 |
Correct |
2 ms |
19036 KB |
Output is correct |
28 |
Correct |
2 ms |
15196 KB |
Output is correct |
29 |
Correct |
3 ms |
17496 KB |
Output is correct |
30 |
Correct |
10 ms |
17384 KB |
Output is correct |
31 |
Correct |
12 ms |
17244 KB |
Output is correct |
32 |
Correct |
16 ms |
17428 KB |
Output is correct |
33 |
Correct |
24 ms |
17244 KB |
Output is correct |
34 |
Correct |
16 ms |
17240 KB |
Output is correct |
35 |
Correct |
15 ms |
17416 KB |
Output is correct |
36 |
Correct |
14 ms |
17244 KB |
Output is correct |
37 |
Correct |
14 ms |
17244 KB |
Output is correct |
38 |
Correct |
4 ms |
17500 KB |
Output is correct |
39 |
Correct |
5 ms |
17536 KB |
Output is correct |
40 |
Correct |
4 ms |
17424 KB |
Output is correct |
41 |
Correct |
5 ms |
17500 KB |
Output is correct |
42 |
Correct |
4 ms |
17500 KB |
Output is correct |
43 |
Correct |
4 ms |
17500 KB |
Output is correct |
44 |
Correct |
6 ms |
17244 KB |
Output is correct |
45 |
Incorrect |
6 ms |
17244 KB |
Output isn't correct |
46 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
19036 KB |
Output is correct |
2 |
Correct |
2 ms |
19036 KB |
Output is correct |
3 |
Correct |
4 ms |
19292 KB |
Output is correct |
4 |
Correct |
85 ms |
30348 KB |
Output is correct |
5 |
Correct |
80 ms |
33108 KB |
Output is correct |
6 |
Correct |
86 ms |
34440 KB |
Output is correct |
7 |
Correct |
117 ms |
36944 KB |
Output is correct |
8 |
Correct |
124 ms |
36948 KB |
Output is correct |
9 |
Correct |
122 ms |
36436 KB |
Output is correct |
10 |
Correct |
125 ms |
36448 KB |
Output is correct |
11 |
Correct |
125 ms |
36448 KB |
Output is correct |
12 |
Correct |
101 ms |
35664 KB |
Output is correct |
13 |
Correct |
99 ms |
35664 KB |
Output is correct |
14 |
Correct |
100 ms |
35684 KB |
Output is correct |
15 |
Correct |
44 ms |
33964 KB |
Output is correct |
16 |
Correct |
77 ms |
37456 KB |
Output is correct |
17 |
Correct |
57 ms |
22764 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
16984 KB |
Output is correct |
2 |
Correct |
100 ms |
23892 KB |
Output is correct |
3 |
Correct |
154 ms |
23892 KB |
Output is correct |
4 |
Correct |
121 ms |
24660 KB |
Output is correct |
5 |
Correct |
173 ms |
25172 KB |
Output is correct |
6 |
Correct |
171 ms |
25832 KB |
Output is correct |
7 |
Correct |
168 ms |
25236 KB |
Output is correct |
8 |
Correct |
165 ms |
25800 KB |
Output is correct |
9 |
Correct |
179 ms |
25256 KB |
Output is correct |
10 |
Correct |
192 ms |
25808 KB |
Output is correct |
11 |
Correct |
193 ms |
25428 KB |
Output is correct |
12 |
Correct |
194 ms |
25248 KB |
Output is correct |
13 |
Correct |
190 ms |
26196 KB |
Output is correct |
14 |
Correct |
214 ms |
26768 KB |
Output is correct |
15 |
Correct |
167 ms |
26964 KB |
Output is correct |
16 |
Correct |
180 ms |
26092 KB |
Output is correct |
17 |
Correct |
175 ms |
26192 KB |
Output is correct |
18 |
Correct |
172 ms |
26080 KB |
Output is correct |
19 |
Correct |
135 ms |
25172 KB |
Output is correct |
20 |
Correct |
146 ms |
25172 KB |
Output is correct |
21 |
Correct |
137 ms |
25680 KB |
Output is correct |
22 |
Correct |
149 ms |
25376 KB |
Output is correct |
23 |
Correct |
142 ms |
25344 KB |
Output is correct |
24 |
Correct |
154 ms |
25740 KB |
Output is correct |
25 |
Correct |
146 ms |
25856 KB |
Output is correct |
26 |
Correct |
163 ms |
25896 KB |
Output is correct |
27 |
Correct |
161 ms |
25916 KB |
Output is correct |
28 |
Correct |
170 ms |
25680 KB |
Output is correct |
29 |
Correct |
164 ms |
25428 KB |
Output is correct |
30 |
Correct |
177 ms |
25684 KB |
Output is correct |
31 |
Correct |
177 ms |
25596 KB |
Output is correct |
32 |
Correct |
184 ms |
25944 KB |
Output is correct |
33 |
Correct |
192 ms |
27756 KB |
Output is correct |
34 |
Correct |
177 ms |
26452 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
17240 KB |
Output is correct |
2 |
Correct |
3 ms |
19036 KB |
Output is correct |
3 |
Correct |
3 ms |
19292 KB |
Output is correct |
4 |
Execution timed out |
5055 ms |
26964 KB |
Time limit exceeded |
5 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
16988 KB |
Output is correct |
2 |
Correct |
2 ms |
19036 KB |
Output is correct |
3 |
Correct |
2 ms |
16988 KB |
Output is correct |
4 |
Correct |
2 ms |
17176 KB |
Output is correct |
5 |
Correct |
2 ms |
17244 KB |
Output is correct |
6 |
Correct |
2 ms |
17244 KB |
Output is correct |
7 |
Correct |
2 ms |
17244 KB |
Output is correct |
8 |
Correct |
2 ms |
17244 KB |
Output is correct |
9 |
Correct |
3 ms |
17244 KB |
Output is correct |
10 |
Correct |
2 ms |
17244 KB |
Output is correct |
11 |
Correct |
3 ms |
17248 KB |
Output is correct |
12 |
Correct |
3 ms |
17240 KB |
Output is correct |
13 |
Correct |
3 ms |
17244 KB |
Output is correct |
14 |
Correct |
3 ms |
17244 KB |
Output is correct |
15 |
Correct |
2 ms |
17244 KB |
Output is correct |
16 |
Correct |
3 ms |
17240 KB |
Output is correct |
17 |
Correct |
3 ms |
17244 KB |
Output is correct |
18 |
Correct |
3 ms |
17240 KB |
Output is correct |
19 |
Correct |
3 ms |
17244 KB |
Output is correct |
20 |
Correct |
3 ms |
17244 KB |
Output is correct |
21 |
Correct |
3 ms |
17244 KB |
Output is correct |
22 |
Correct |
3 ms |
17244 KB |
Output is correct |
23 |
Correct |
3 ms |
17244 KB |
Output is correct |
24 |
Correct |
3 ms |
17244 KB |
Output is correct |
25 |
Correct |
3 ms |
17244 KB |
Output is correct |
26 |
Correct |
3 ms |
17244 KB |
Output is correct |
27 |
Correct |
2 ms |
19036 KB |
Output is correct |
28 |
Correct |
2 ms |
15196 KB |
Output is correct |
29 |
Correct |
3 ms |
17496 KB |
Output is correct |
30 |
Correct |
10 ms |
17384 KB |
Output is correct |
31 |
Correct |
12 ms |
17244 KB |
Output is correct |
32 |
Correct |
16 ms |
17428 KB |
Output is correct |
33 |
Correct |
24 ms |
17244 KB |
Output is correct |
34 |
Correct |
16 ms |
17240 KB |
Output is correct |
35 |
Correct |
15 ms |
17416 KB |
Output is correct |
36 |
Correct |
14 ms |
17244 KB |
Output is correct |
37 |
Correct |
14 ms |
17244 KB |
Output is correct |
38 |
Correct |
4 ms |
17500 KB |
Output is correct |
39 |
Correct |
5 ms |
17536 KB |
Output is correct |
40 |
Correct |
4 ms |
17424 KB |
Output is correct |
41 |
Correct |
5 ms |
17500 KB |
Output is correct |
42 |
Correct |
4 ms |
17500 KB |
Output is correct |
43 |
Correct |
4 ms |
17500 KB |
Output is correct |
44 |
Correct |
6 ms |
17244 KB |
Output is correct |
45 |
Incorrect |
6 ms |
17244 KB |
Output isn't correct |
46 |
Halted |
0 ms |
0 KB |
- |