#include<bits/stdc++.h>
using namespace std;
const int N = 1e5 + 1;
int n , m , q , T = 0 , t[N] , C[N] , id[N] , ans[N] , sz[N] , d[N] , up[N] , top[N];
vector<int> g[N];
vector<pair<int , int>> query[N];
set<tuple<int , int , int>> S;
void update(int u , int x){
while(u < N){
t[u] += x;
u += u&-u;
}
}
int get(int u){
int sum = 0;
while(u > 0){
sum += t[u];
u -= u&-u;
}
return sum;
}
void dfs(int u , int p = 1){
if(u != 1){
g[u].erase(find(g[u].begin() , g[u].end() , p));
}
up[u] = p;
d[u] = d[p] + 1;
for(int x = 0;x < (int)g[u].size();x ++){
dfs(g[u][x] , u);
sz[u] += sz[g[u][x]];
if(sz[g[u][0]] < sz[g[u][x]]){
swap(g[u][0] , g[u][x]);
}
}
++sz[u];
}
void hld(int u){
id[u] = ++T;
for(int x = 0;x < (int)g[u].size();x ++){
top[g[u][x]] = (x == 0 ? top[u] : g[u][x]);
hld(g[u][x]);
}
}
void upd(int l , int r , int x){
//cout << "make " << l << " " << r << " " << x << endl;
while(true){
auto it = S.lower_bound({l , -1 , -1});
if(it == S.end() || get<0>(*it) < l || get<1>(*it) > r){
break;
}
int L = get<1>(*it) , R = get<0>(*it) , v = get<2>(*it);
//cout << "xx " << L << " " << R << " " << v << endl;
S.erase(it);
update(v , -(min(r , R) - max(L , l) + 1));
if(L < l){
S.insert({l - 1 , L , v});
}
if(r < R){
S.insert({R , r + 1 , v});
}
}
S.insert({r , l , x});
update(x , r - l + 1);
}
void UPD(int u , int v , int x){
while(top[u] != top[v]){
//cout << u << " da " << v << endl;
if(d[top[u]] < d[top[v]]){
swap(u , v);
}
upd(id[top[u]] , id[u] , x);
u = up[top[u]];
}
//cout << u << " das " << v << endl;
if(d[u] > d[v]){
swap(u , v);
}
upd(id[u] , id[v] , x);
}
int main(){
ios_base::sync_with_stdio(false);
cout.tie(0);
cin.tie(0);
cin >> n >> m >> q;
for(int i = 1;i < n;i ++){
int u , v;
cin >> u >> v;
g[u].push_back(v);
g[v].push_back(u);
}
dfs(1);
top[1] = 1;
hld(1);
for(int i = 1;i <= m;i ++){
cin >> C[i];
}
for(int i = 1;i <= q;i ++){
int l , r;
cin >> l >> r;
query[r].push_back({l , i});
}
for(int i = 1;i <= m;i ++){
if(i > 1){
//cout << C[i - 1] << " -> " << C[i] << endl;
UPD(C[i - 1] , C[i] , i - 1);
}
upd(id[C[i]] , id[C[i]] , i);
for(auto [l , id] : query[i]){
ans[id] = get(N - 1) - get(l - 1);
}
}
for(int i = 1;i <= q;i ++){
cout << ans[i] << " ";
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
5212 KB |
Output is correct |
2 |
Correct |
2 ms |
5064 KB |
Output is correct |
3 |
Correct |
2 ms |
5212 KB |
Output is correct |
4 |
Correct |
2 ms |
5212 KB |
Output is correct |
5 |
Correct |
2 ms |
5164 KB |
Output is correct |
6 |
Correct |
2 ms |
5212 KB |
Output is correct |
7 |
Correct |
2 ms |
5212 KB |
Output is correct |
8 |
Correct |
2 ms |
5164 KB |
Output is correct |
9 |
Correct |
2 ms |
5212 KB |
Output is correct |
10 |
Correct |
3 ms |
5204 KB |
Output is correct |
11 |
Correct |
2 ms |
5212 KB |
Output is correct |
12 |
Correct |
2 ms |
5212 KB |
Output is correct |
13 |
Correct |
2 ms |
5240 KB |
Output is correct |
14 |
Correct |
3 ms |
5212 KB |
Output is correct |
15 |
Correct |
2 ms |
5216 KB |
Output is correct |
16 |
Correct |
2 ms |
5212 KB |
Output is correct |
17 |
Correct |
3 ms |
5216 KB |
Output is correct |
18 |
Correct |
2 ms |
5212 KB |
Output is correct |
19 |
Correct |
2 ms |
5236 KB |
Output is correct |
20 |
Correct |
3 ms |
5212 KB |
Output is correct |
21 |
Correct |
2 ms |
5212 KB |
Output is correct |
22 |
Correct |
2 ms |
5208 KB |
Output is correct |
23 |
Correct |
2 ms |
5212 KB |
Output is correct |
24 |
Correct |
2 ms |
5212 KB |
Output is correct |
25 |
Correct |
2 ms |
5212 KB |
Output is correct |
26 |
Correct |
3 ms |
5212 KB |
Output is correct |
27 |
Correct |
2 ms |
5212 KB |
Output is correct |
28 |
Correct |
2 ms |
5208 KB |
Output is correct |
29 |
Correct |
2 ms |
5212 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
5212 KB |
Output is correct |
2 |
Correct |
2 ms |
5064 KB |
Output is correct |
3 |
Correct |
2 ms |
5212 KB |
Output is correct |
4 |
Correct |
2 ms |
5212 KB |
Output is correct |
5 |
Correct |
2 ms |
5164 KB |
Output is correct |
6 |
Correct |
2 ms |
5212 KB |
Output is correct |
7 |
Correct |
2 ms |
5212 KB |
Output is correct |
8 |
Correct |
2 ms |
5164 KB |
Output is correct |
9 |
Correct |
2 ms |
5212 KB |
Output is correct |
10 |
Correct |
3 ms |
5204 KB |
Output is correct |
11 |
Correct |
2 ms |
5212 KB |
Output is correct |
12 |
Correct |
2 ms |
5212 KB |
Output is correct |
13 |
Correct |
2 ms |
5240 KB |
Output is correct |
14 |
Correct |
3 ms |
5212 KB |
Output is correct |
15 |
Correct |
2 ms |
5216 KB |
Output is correct |
16 |
Correct |
2 ms |
5212 KB |
Output is correct |
17 |
Correct |
3 ms |
5216 KB |
Output is correct |
18 |
Correct |
2 ms |
5212 KB |
Output is correct |
19 |
Correct |
2 ms |
5236 KB |
Output is correct |
20 |
Correct |
3 ms |
5212 KB |
Output is correct |
21 |
Correct |
2 ms |
5212 KB |
Output is correct |
22 |
Correct |
2 ms |
5208 KB |
Output is correct |
23 |
Correct |
2 ms |
5212 KB |
Output is correct |
24 |
Correct |
2 ms |
5212 KB |
Output is correct |
25 |
Correct |
2 ms |
5212 KB |
Output is correct |
26 |
Correct |
3 ms |
5212 KB |
Output is correct |
27 |
Correct |
2 ms |
5212 KB |
Output is correct |
28 |
Correct |
2 ms |
5208 KB |
Output is correct |
29 |
Correct |
2 ms |
5212 KB |
Output is correct |
30 |
Correct |
5 ms |
5172 KB |
Output is correct |
31 |
Correct |
5 ms |
5412 KB |
Output is correct |
32 |
Correct |
6 ms |
5468 KB |
Output is correct |
33 |
Correct |
6 ms |
5488 KB |
Output is correct |
34 |
Correct |
6 ms |
5268 KB |
Output is correct |
35 |
Correct |
6 ms |
5440 KB |
Output is correct |
36 |
Correct |
6 ms |
5468 KB |
Output is correct |
37 |
Correct |
8 ms |
5496 KB |
Output is correct |
38 |
Correct |
4 ms |
5468 KB |
Output is correct |
39 |
Correct |
4 ms |
5468 KB |
Output is correct |
40 |
Correct |
4 ms |
5464 KB |
Output is correct |
41 |
Correct |
3 ms |
5508 KB |
Output is correct |
42 |
Correct |
3 ms |
5468 KB |
Output is correct |
43 |
Correct |
3 ms |
5468 KB |
Output is correct |
44 |
Correct |
4 ms |
5412 KB |
Output is correct |
45 |
Correct |
5 ms |
5208 KB |
Output is correct |
46 |
Correct |
5 ms |
5464 KB |
Output is correct |
47 |
Correct |
4 ms |
5212 KB |
Output is correct |
48 |
Correct |
5 ms |
5212 KB |
Output is correct |
49 |
Correct |
4 ms |
5468 KB |
Output is correct |
50 |
Correct |
4 ms |
5468 KB |
Output is correct |
51 |
Correct |
4 ms |
5464 KB |
Output is correct |
52 |
Correct |
4 ms |
5428 KB |
Output is correct |
53 |
Correct |
4 ms |
5468 KB |
Output is correct |
54 |
Correct |
5 ms |
5468 KB |
Output is correct |
55 |
Correct |
4 ms |
5468 KB |
Output is correct |
56 |
Correct |
3 ms |
5212 KB |
Output is correct |
57 |
Correct |
3 ms |
5180 KB |
Output is correct |
58 |
Correct |
6 ms |
5468 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
5212 KB |
Output is correct |
2 |
Correct |
2 ms |
5212 KB |
Output is correct |
3 |
Correct |
3 ms |
5208 KB |
Output is correct |
4 |
Correct |
75 ms |
16464 KB |
Output is correct |
5 |
Correct |
60 ms |
18516 KB |
Output is correct |
6 |
Correct |
75 ms |
20052 KB |
Output is correct |
7 |
Correct |
91 ms |
23084 KB |
Output is correct |
8 |
Correct |
95 ms |
23100 KB |
Output is correct |
9 |
Correct |
91 ms |
22864 KB |
Output is correct |
10 |
Correct |
93 ms |
23120 KB |
Output is correct |
11 |
Correct |
97 ms |
23048 KB |
Output is correct |
12 |
Correct |
92 ms |
22424 KB |
Output is correct |
13 |
Correct |
109 ms |
22440 KB |
Output is correct |
14 |
Correct |
96 ms |
22520 KB |
Output is correct |
15 |
Correct |
34 ms |
19532 KB |
Output is correct |
16 |
Correct |
87 ms |
22608 KB |
Output is correct |
17 |
Correct |
37 ms |
9808 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
5212 KB |
Output is correct |
2 |
Correct |
180 ms |
11196 KB |
Output is correct |
3 |
Correct |
272 ms |
12368 KB |
Output is correct |
4 |
Correct |
216 ms |
12516 KB |
Output is correct |
5 |
Correct |
351 ms |
15952 KB |
Output is correct |
6 |
Correct |
348 ms |
16004 KB |
Output is correct |
7 |
Correct |
319 ms |
15956 KB |
Output is correct |
8 |
Correct |
354 ms |
15952 KB |
Output is correct |
9 |
Correct |
344 ms |
15956 KB |
Output is correct |
10 |
Correct |
340 ms |
16016 KB |
Output is correct |
11 |
Correct |
340 ms |
16212 KB |
Output is correct |
12 |
Correct |
347 ms |
16156 KB |
Output is correct |
13 |
Correct |
362 ms |
16440 KB |
Output is correct |
14 |
Correct |
330 ms |
17488 KB |
Output is correct |
15 |
Correct |
371 ms |
20820 KB |
Output is correct |
16 |
Correct |
333 ms |
16468 KB |
Output is correct |
17 |
Correct |
353 ms |
16700 KB |
Output is correct |
18 |
Correct |
337 ms |
16468 KB |
Output is correct |
19 |
Correct |
223 ms |
12924 KB |
Output is correct |
20 |
Correct |
222 ms |
12968 KB |
Output is correct |
21 |
Correct |
242 ms |
12988 KB |
Output is correct |
22 |
Correct |
238 ms |
12880 KB |
Output is correct |
23 |
Correct |
215 ms |
12840 KB |
Output is correct |
24 |
Correct |
213 ms |
12884 KB |
Output is correct |
25 |
Correct |
250 ms |
13336 KB |
Output is correct |
26 |
Correct |
216 ms |
12884 KB |
Output is correct |
27 |
Correct |
208 ms |
13136 KB |
Output is correct |
28 |
Correct |
223 ms |
13136 KB |
Output is correct |
29 |
Correct |
205 ms |
13136 KB |
Output is correct |
30 |
Correct |
222 ms |
13140 KB |
Output is correct |
31 |
Correct |
223 ms |
13384 KB |
Output is correct |
32 |
Correct |
225 ms |
14112 KB |
Output is correct |
33 |
Correct |
237 ms |
15440 KB |
Output is correct |
34 |
Correct |
235 ms |
18004 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
5212 KB |
Output is correct |
2 |
Correct |
2 ms |
5212 KB |
Output is correct |
3 |
Correct |
3 ms |
5212 KB |
Output is correct |
4 |
Correct |
428 ms |
16156 KB |
Output is correct |
5 |
Correct |
440 ms |
16212 KB |
Output is correct |
6 |
Correct |
528 ms |
18992 KB |
Output is correct |
7 |
Correct |
555 ms |
20656 KB |
Output is correct |
8 |
Correct |
574 ms |
20560 KB |
Output is correct |
9 |
Correct |
531 ms |
20564 KB |
Output is correct |
10 |
Correct |
536 ms |
20732 KB |
Output is correct |
11 |
Correct |
548 ms |
20560 KB |
Output is correct |
12 |
Correct |
535 ms |
20560 KB |
Output is correct |
13 |
Correct |
562 ms |
20756 KB |
Output is correct |
14 |
Correct |
41 ms |
9812 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
5212 KB |
Output is correct |
2 |
Correct |
2 ms |
5064 KB |
Output is correct |
3 |
Correct |
2 ms |
5212 KB |
Output is correct |
4 |
Correct |
2 ms |
5212 KB |
Output is correct |
5 |
Correct |
2 ms |
5164 KB |
Output is correct |
6 |
Correct |
2 ms |
5212 KB |
Output is correct |
7 |
Correct |
2 ms |
5212 KB |
Output is correct |
8 |
Correct |
2 ms |
5164 KB |
Output is correct |
9 |
Correct |
2 ms |
5212 KB |
Output is correct |
10 |
Correct |
3 ms |
5204 KB |
Output is correct |
11 |
Correct |
2 ms |
5212 KB |
Output is correct |
12 |
Correct |
2 ms |
5212 KB |
Output is correct |
13 |
Correct |
2 ms |
5240 KB |
Output is correct |
14 |
Correct |
3 ms |
5212 KB |
Output is correct |
15 |
Correct |
2 ms |
5216 KB |
Output is correct |
16 |
Correct |
2 ms |
5212 KB |
Output is correct |
17 |
Correct |
3 ms |
5216 KB |
Output is correct |
18 |
Correct |
2 ms |
5212 KB |
Output is correct |
19 |
Correct |
2 ms |
5236 KB |
Output is correct |
20 |
Correct |
3 ms |
5212 KB |
Output is correct |
21 |
Correct |
2 ms |
5212 KB |
Output is correct |
22 |
Correct |
2 ms |
5208 KB |
Output is correct |
23 |
Correct |
2 ms |
5212 KB |
Output is correct |
24 |
Correct |
2 ms |
5212 KB |
Output is correct |
25 |
Correct |
2 ms |
5212 KB |
Output is correct |
26 |
Correct |
3 ms |
5212 KB |
Output is correct |
27 |
Correct |
2 ms |
5212 KB |
Output is correct |
28 |
Correct |
2 ms |
5208 KB |
Output is correct |
29 |
Correct |
2 ms |
5212 KB |
Output is correct |
30 |
Correct |
5 ms |
5172 KB |
Output is correct |
31 |
Correct |
5 ms |
5412 KB |
Output is correct |
32 |
Correct |
6 ms |
5468 KB |
Output is correct |
33 |
Correct |
6 ms |
5488 KB |
Output is correct |
34 |
Correct |
6 ms |
5268 KB |
Output is correct |
35 |
Correct |
6 ms |
5440 KB |
Output is correct |
36 |
Correct |
6 ms |
5468 KB |
Output is correct |
37 |
Correct |
8 ms |
5496 KB |
Output is correct |
38 |
Correct |
4 ms |
5468 KB |
Output is correct |
39 |
Correct |
4 ms |
5468 KB |
Output is correct |
40 |
Correct |
4 ms |
5464 KB |
Output is correct |
41 |
Correct |
3 ms |
5508 KB |
Output is correct |
42 |
Correct |
3 ms |
5468 KB |
Output is correct |
43 |
Correct |
3 ms |
5468 KB |
Output is correct |
44 |
Correct |
4 ms |
5412 KB |
Output is correct |
45 |
Correct |
5 ms |
5208 KB |
Output is correct |
46 |
Correct |
5 ms |
5464 KB |
Output is correct |
47 |
Correct |
4 ms |
5212 KB |
Output is correct |
48 |
Correct |
5 ms |
5212 KB |
Output is correct |
49 |
Correct |
4 ms |
5468 KB |
Output is correct |
50 |
Correct |
4 ms |
5468 KB |
Output is correct |
51 |
Correct |
4 ms |
5464 KB |
Output is correct |
52 |
Correct |
4 ms |
5428 KB |
Output is correct |
53 |
Correct |
4 ms |
5468 KB |
Output is correct |
54 |
Correct |
5 ms |
5468 KB |
Output is correct |
55 |
Correct |
4 ms |
5468 KB |
Output is correct |
56 |
Correct |
3 ms |
5212 KB |
Output is correct |
57 |
Correct |
3 ms |
5180 KB |
Output is correct |
58 |
Correct |
6 ms |
5468 KB |
Output is correct |
59 |
Correct |
2 ms |
5212 KB |
Output is correct |
60 |
Correct |
2 ms |
5212 KB |
Output is correct |
61 |
Correct |
3 ms |
5208 KB |
Output is correct |
62 |
Correct |
75 ms |
16464 KB |
Output is correct |
63 |
Correct |
60 ms |
18516 KB |
Output is correct |
64 |
Correct |
75 ms |
20052 KB |
Output is correct |
65 |
Correct |
91 ms |
23084 KB |
Output is correct |
66 |
Correct |
95 ms |
23100 KB |
Output is correct |
67 |
Correct |
91 ms |
22864 KB |
Output is correct |
68 |
Correct |
93 ms |
23120 KB |
Output is correct |
69 |
Correct |
97 ms |
23048 KB |
Output is correct |
70 |
Correct |
92 ms |
22424 KB |
Output is correct |
71 |
Correct |
109 ms |
22440 KB |
Output is correct |
72 |
Correct |
96 ms |
22520 KB |
Output is correct |
73 |
Correct |
34 ms |
19532 KB |
Output is correct |
74 |
Correct |
87 ms |
22608 KB |
Output is correct |
75 |
Correct |
37 ms |
9808 KB |
Output is correct |
76 |
Correct |
2 ms |
5212 KB |
Output is correct |
77 |
Correct |
180 ms |
11196 KB |
Output is correct |
78 |
Correct |
272 ms |
12368 KB |
Output is correct |
79 |
Correct |
216 ms |
12516 KB |
Output is correct |
80 |
Correct |
351 ms |
15952 KB |
Output is correct |
81 |
Correct |
348 ms |
16004 KB |
Output is correct |
82 |
Correct |
319 ms |
15956 KB |
Output is correct |
83 |
Correct |
354 ms |
15952 KB |
Output is correct |
84 |
Correct |
344 ms |
15956 KB |
Output is correct |
85 |
Correct |
340 ms |
16016 KB |
Output is correct |
86 |
Correct |
340 ms |
16212 KB |
Output is correct |
87 |
Correct |
347 ms |
16156 KB |
Output is correct |
88 |
Correct |
362 ms |
16440 KB |
Output is correct |
89 |
Correct |
330 ms |
17488 KB |
Output is correct |
90 |
Correct |
371 ms |
20820 KB |
Output is correct |
91 |
Correct |
333 ms |
16468 KB |
Output is correct |
92 |
Correct |
353 ms |
16700 KB |
Output is correct |
93 |
Correct |
337 ms |
16468 KB |
Output is correct |
94 |
Correct |
223 ms |
12924 KB |
Output is correct |
95 |
Correct |
222 ms |
12968 KB |
Output is correct |
96 |
Correct |
242 ms |
12988 KB |
Output is correct |
97 |
Correct |
238 ms |
12880 KB |
Output is correct |
98 |
Correct |
215 ms |
12840 KB |
Output is correct |
99 |
Correct |
213 ms |
12884 KB |
Output is correct |
100 |
Correct |
250 ms |
13336 KB |
Output is correct |
101 |
Correct |
216 ms |
12884 KB |
Output is correct |
102 |
Correct |
208 ms |
13136 KB |
Output is correct |
103 |
Correct |
223 ms |
13136 KB |
Output is correct |
104 |
Correct |
205 ms |
13136 KB |
Output is correct |
105 |
Correct |
222 ms |
13140 KB |
Output is correct |
106 |
Correct |
223 ms |
13384 KB |
Output is correct |
107 |
Correct |
225 ms |
14112 KB |
Output is correct |
108 |
Correct |
237 ms |
15440 KB |
Output is correct |
109 |
Correct |
235 ms |
18004 KB |
Output is correct |
110 |
Correct |
2 ms |
5212 KB |
Output is correct |
111 |
Correct |
2 ms |
5212 KB |
Output is correct |
112 |
Correct |
3 ms |
5212 KB |
Output is correct |
113 |
Correct |
428 ms |
16156 KB |
Output is correct |
114 |
Correct |
440 ms |
16212 KB |
Output is correct |
115 |
Correct |
528 ms |
18992 KB |
Output is correct |
116 |
Correct |
555 ms |
20656 KB |
Output is correct |
117 |
Correct |
574 ms |
20560 KB |
Output is correct |
118 |
Correct |
531 ms |
20564 KB |
Output is correct |
119 |
Correct |
536 ms |
20732 KB |
Output is correct |
120 |
Correct |
548 ms |
20560 KB |
Output is correct |
121 |
Correct |
535 ms |
20560 KB |
Output is correct |
122 |
Correct |
562 ms |
20756 KB |
Output is correct |
123 |
Correct |
41 ms |
9812 KB |
Output is correct |
124 |
Correct |
307 ms |
19544 KB |
Output is correct |
125 |
Correct |
238 ms |
17236 KB |
Output is correct |
126 |
Correct |
361 ms |
20304 KB |
Output is correct |
127 |
Correct |
393 ms |
20216 KB |
Output is correct |
128 |
Correct |
346 ms |
20048 KB |
Output is correct |
129 |
Correct |
394 ms |
20068 KB |
Output is correct |
130 |
Correct |
386 ms |
20304 KB |
Output is correct |
131 |
Correct |
95 ms |
22100 KB |
Output is correct |
132 |
Correct |
99 ms |
22868 KB |
Output is correct |
133 |
Correct |
107 ms |
19796 KB |
Output is correct |
134 |
Correct |
256 ms |
17032 KB |
Output is correct |
135 |
Correct |
252 ms |
17236 KB |
Output is correct |
136 |
Correct |
246 ms |
17164 KB |
Output is correct |
137 |
Correct |
137 ms |
21120 KB |
Output is correct |
138 |
Correct |
137 ms |
21196 KB |
Output is correct |
139 |
Correct |
133 ms |
21188 KB |
Output is correct |
140 |
Correct |
134 ms |
21196 KB |
Output is correct |
141 |
Correct |
139 ms |
20996 KB |
Output is correct |
142 |
Correct |
133 ms |
21060 KB |
Output is correct |
143 |
Correct |
38 ms |
13252 KB |
Output is correct |
144 |
Correct |
356 ms |
19912 KB |
Output is correct |