#include<bits/stdc++.h>
#define ll long long
#define pll pair<ll, ll>
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define ld long double
using namespace std;
const ll maxn=100005;
vector <ll> A[maxn];
vector <pll> L[maxn];
ll n, m, q, St[maxn*4], bit[maxn], c[maxn], ans[maxn];
ll Time, tin[maxn], h[maxn], pa[maxn], dep[maxn], child[maxn], big[maxn];
void update(ll i, ll val)
{
for (; i<=m; i+=i&(-i))
bit[i]+=val;
}
ll query(ll l, ll r)
{
ll ans=0; l--;
for (; r; r-=r&(-r))
ans+=bit[r];
for (; l; l-=l&(-l))
ans-=bit[l];
return ans;
}
void update(ll i, ll Start, ll End, ll l, ll r, ll val)
{
if (Start>r || End<l)
return;
if (Start>=l && End<=r && St[i]!=-1)
{
if (St[i]) update(St[i], -(End-Start+1));
update(val, End-Start+1);
St[i]=val;
return;
}
if (St[i]!=-1) St[i*2]=St[i*2+1]=St[i];
ll mid=(Start+End)/2;
if (mid>=l) update(i*2, Start, mid, l, r, val);
if (mid+1<=r) update(i*2+1, mid+1, End, l, r, val);
if (St[i*2]==St[i*2+1]) St[i]=St[i*2];
else St[i]=-1;
}
void dfs(ll u)
{
child[u]=1;
for (ll v:A[u])
if (v!=pa[u])
{
dep[v]=dep[u]+1, pa[v]=u;
dfs(v), child[u]+=child[v];
if (child[v]>child[big[u]])
big[u]=v;
}
}
void dfs2(ll u)
{
tin[u]=++Time;
if (u==big[pa[u]])
h[u]=h[pa[u]];
else h[u]=u;
if (big[u]) dfs2(big[u]);
for (ll v:A[u])
if (v!=pa[u] && v!=big[u])
dfs2(v);
}
void update(ll u, ll v, ll val)
{
for (; h[u]!=h[v]; u=pa[h[u]])
{
if (dep[h[u]]<dep[h[v]]) swap(u, v);
update(1, 1, n, tin[h[u]], tin[u], val);
}
if (dep[u]<dep[v]) swap(u, v);
update(1, 1, n, tin[v], tin[u], val);
}
int main()
{
ios_base::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
cin >> n >> m >> q;
for (ll i=1; i<n; i++)
{
ll u, v; cin >> u >> v;
A[u].pb(v); A[v].pb(u);
}
dfs(1); dfs2(1);
for (ll i=1; i<=m; i++)
cin >> c[i];
for (ll i=1; i<=q; i++)
{
ll l, r; cin >> l >> r;
L[r].pb({l, i});
}
for (ll i=1; i<=m; i++)
{
if (i>1) update(c[i-1], c[i], i-1);
update(c[i], c[i], i);
for (auto &[l, idx]:L[i])
ans[idx]=query(l, m);
}
for (ll i=1; i<=q; i++)
cout << ans[i] << "\n";
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
8796 KB |
Output is correct |
2 |
Correct |
2 ms |
8796 KB |
Output is correct |
3 |
Correct |
2 ms |
8796 KB |
Output is correct |
4 |
Correct |
2 ms |
9052 KB |
Output is correct |
5 |
Correct |
2 ms |
9052 KB |
Output is correct |
6 |
Correct |
2 ms |
9052 KB |
Output is correct |
7 |
Correct |
3 ms |
9052 KB |
Output is correct |
8 |
Correct |
2 ms |
9044 KB |
Output is correct |
9 |
Correct |
2 ms |
9048 KB |
Output is correct |
10 |
Correct |
2 ms |
9052 KB |
Output is correct |
11 |
Correct |
2 ms |
8860 KB |
Output is correct |
12 |
Correct |
2 ms |
9052 KB |
Output is correct |
13 |
Correct |
2 ms |
9048 KB |
Output is correct |
14 |
Correct |
2 ms |
9052 KB |
Output is correct |
15 |
Correct |
2 ms |
9052 KB |
Output is correct |
16 |
Correct |
2 ms |
9068 KB |
Output is correct |
17 |
Correct |
2 ms |
9048 KB |
Output is correct |
18 |
Correct |
3 ms |
9048 KB |
Output is correct |
19 |
Correct |
2 ms |
9052 KB |
Output is correct |
20 |
Correct |
2 ms |
9052 KB |
Output is correct |
21 |
Correct |
2 ms |
9052 KB |
Output is correct |
22 |
Correct |
2 ms |
9052 KB |
Output is correct |
23 |
Correct |
2 ms |
9052 KB |
Output is correct |
24 |
Correct |
2 ms |
8880 KB |
Output is correct |
25 |
Correct |
2 ms |
9048 KB |
Output is correct |
26 |
Correct |
2 ms |
9052 KB |
Output is correct |
27 |
Correct |
2 ms |
8796 KB |
Output is correct |
28 |
Correct |
2 ms |
9052 KB |
Output is correct |
29 |
Correct |
2 ms |
9052 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
8796 KB |
Output is correct |
2 |
Correct |
2 ms |
8796 KB |
Output is correct |
3 |
Correct |
2 ms |
8796 KB |
Output is correct |
4 |
Correct |
2 ms |
9052 KB |
Output is correct |
5 |
Correct |
2 ms |
9052 KB |
Output is correct |
6 |
Correct |
2 ms |
9052 KB |
Output is correct |
7 |
Correct |
3 ms |
9052 KB |
Output is correct |
8 |
Correct |
2 ms |
9044 KB |
Output is correct |
9 |
Correct |
2 ms |
9048 KB |
Output is correct |
10 |
Correct |
2 ms |
9052 KB |
Output is correct |
11 |
Correct |
2 ms |
8860 KB |
Output is correct |
12 |
Correct |
2 ms |
9052 KB |
Output is correct |
13 |
Correct |
2 ms |
9048 KB |
Output is correct |
14 |
Correct |
2 ms |
9052 KB |
Output is correct |
15 |
Correct |
2 ms |
9052 KB |
Output is correct |
16 |
Correct |
2 ms |
9068 KB |
Output is correct |
17 |
Correct |
2 ms |
9048 KB |
Output is correct |
18 |
Correct |
3 ms |
9048 KB |
Output is correct |
19 |
Correct |
2 ms |
9052 KB |
Output is correct |
20 |
Correct |
2 ms |
9052 KB |
Output is correct |
21 |
Correct |
2 ms |
9052 KB |
Output is correct |
22 |
Correct |
2 ms |
9052 KB |
Output is correct |
23 |
Correct |
2 ms |
9052 KB |
Output is correct |
24 |
Correct |
2 ms |
8880 KB |
Output is correct |
25 |
Correct |
2 ms |
9048 KB |
Output is correct |
26 |
Correct |
2 ms |
9052 KB |
Output is correct |
27 |
Correct |
2 ms |
8796 KB |
Output is correct |
28 |
Correct |
2 ms |
9052 KB |
Output is correct |
29 |
Correct |
2 ms |
9052 KB |
Output is correct |
30 |
Correct |
4 ms |
9052 KB |
Output is correct |
31 |
Correct |
4 ms |
9304 KB |
Output is correct |
32 |
Correct |
7 ms |
9304 KB |
Output is correct |
33 |
Correct |
5 ms |
9308 KB |
Output is correct |
34 |
Correct |
5 ms |
9308 KB |
Output is correct |
35 |
Correct |
5 ms |
9308 KB |
Output is correct |
36 |
Correct |
5 ms |
9308 KB |
Output is correct |
37 |
Correct |
4 ms |
9308 KB |
Output is correct |
38 |
Correct |
5 ms |
9320 KB |
Output is correct |
39 |
Correct |
4 ms |
9304 KB |
Output is correct |
40 |
Correct |
4 ms |
9308 KB |
Output is correct |
41 |
Correct |
4 ms |
9308 KB |
Output is correct |
42 |
Correct |
4 ms |
9508 KB |
Output is correct |
43 |
Correct |
5 ms |
9308 KB |
Output is correct |
44 |
Correct |
5 ms |
9304 KB |
Output is correct |
45 |
Correct |
7 ms |
9308 KB |
Output is correct |
46 |
Correct |
5 ms |
9304 KB |
Output is correct |
47 |
Correct |
5 ms |
9308 KB |
Output is correct |
48 |
Correct |
6 ms |
9308 KB |
Output is correct |
49 |
Correct |
5 ms |
9308 KB |
Output is correct |
50 |
Correct |
3 ms |
9052 KB |
Output is correct |
51 |
Correct |
4 ms |
9052 KB |
Output is correct |
52 |
Correct |
4 ms |
9308 KB |
Output is correct |
53 |
Correct |
4 ms |
9308 KB |
Output is correct |
54 |
Correct |
4 ms |
9204 KB |
Output is correct |
55 |
Correct |
4 ms |
9308 KB |
Output is correct |
56 |
Correct |
3 ms |
9052 KB |
Output is correct |
57 |
Correct |
2 ms |
9048 KB |
Output is correct |
58 |
Correct |
5 ms |
9220 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
8796 KB |
Output is correct |
2 |
Correct |
2 ms |
8796 KB |
Output is correct |
3 |
Correct |
2 ms |
9052 KB |
Output is correct |
4 |
Correct |
154 ms |
20988 KB |
Output is correct |
5 |
Correct |
123 ms |
25460 KB |
Output is correct |
6 |
Correct |
164 ms |
26976 KB |
Output is correct |
7 |
Correct |
201 ms |
30752 KB |
Output is correct |
8 |
Correct |
196 ms |
30804 KB |
Output is correct |
9 |
Correct |
194 ms |
30544 KB |
Output is correct |
10 |
Correct |
205 ms |
30656 KB |
Output is correct |
11 |
Correct |
195 ms |
30616 KB |
Output is correct |
12 |
Correct |
132 ms |
30284 KB |
Output is correct |
13 |
Correct |
120 ms |
30332 KB |
Output is correct |
14 |
Correct |
121 ms |
30584 KB |
Output is correct |
15 |
Correct |
43 ms |
25188 KB |
Output is correct |
16 |
Correct |
198 ms |
30288 KB |
Output is correct |
17 |
Correct |
35 ms |
15344 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
8796 KB |
Output is correct |
2 |
Correct |
136 ms |
14420 KB |
Output is correct |
3 |
Correct |
192 ms |
15184 KB |
Output is correct |
4 |
Correct |
155 ms |
15656 KB |
Output is correct |
5 |
Correct |
280 ms |
18256 KB |
Output is correct |
6 |
Correct |
256 ms |
18340 KB |
Output is correct |
7 |
Correct |
251 ms |
18160 KB |
Output is correct |
8 |
Correct |
241 ms |
18256 KB |
Output is correct |
9 |
Correct |
268 ms |
18116 KB |
Output is correct |
10 |
Correct |
264 ms |
18044 KB |
Output is correct |
11 |
Correct |
253 ms |
18532 KB |
Output is correct |
12 |
Correct |
248 ms |
18284 KB |
Output is correct |
13 |
Correct |
245 ms |
18772 KB |
Output is correct |
14 |
Correct |
261 ms |
19748 KB |
Output is correct |
15 |
Correct |
279 ms |
22356 KB |
Output is correct |
16 |
Correct |
245 ms |
20472 KB |
Output is correct |
17 |
Correct |
247 ms |
20420 KB |
Output is correct |
18 |
Correct |
258 ms |
20304 KB |
Output is correct |
19 |
Correct |
321 ms |
19512 KB |
Output is correct |
20 |
Correct |
318 ms |
19500 KB |
Output is correct |
21 |
Correct |
338 ms |
19308 KB |
Output is correct |
22 |
Correct |
323 ms |
19384 KB |
Output is correct |
23 |
Correct |
321 ms |
19540 KB |
Output is correct |
24 |
Correct |
324 ms |
19368 KB |
Output is correct |
25 |
Correct |
331 ms |
19520 KB |
Output is correct |
26 |
Correct |
324 ms |
19428 KB |
Output is correct |
27 |
Correct |
325 ms |
19536 KB |
Output is correct |
28 |
Correct |
326 ms |
19432 KB |
Output is correct |
29 |
Correct |
323 ms |
19536 KB |
Output is correct |
30 |
Correct |
329 ms |
19804 KB |
Output is correct |
31 |
Correct |
325 ms |
19936 KB |
Output is correct |
32 |
Correct |
327 ms |
20564 KB |
Output is correct |
33 |
Correct |
339 ms |
22156 KB |
Output is correct |
34 |
Correct |
348 ms |
24804 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
8792 KB |
Output is correct |
2 |
Correct |
2 ms |
9052 KB |
Output is correct |
3 |
Correct |
3 ms |
9140 KB |
Output is correct |
4 |
Correct |
297 ms |
18768 KB |
Output is correct |
5 |
Correct |
325 ms |
18772 KB |
Output is correct |
6 |
Correct |
359 ms |
21180 KB |
Output is correct |
7 |
Correct |
372 ms |
25280 KB |
Output is correct |
8 |
Correct |
410 ms |
25236 KB |
Output is correct |
9 |
Correct |
373 ms |
25172 KB |
Output is correct |
10 |
Correct |
391 ms |
25440 KB |
Output is correct |
11 |
Correct |
377 ms |
25124 KB |
Output is correct |
12 |
Correct |
378 ms |
25408 KB |
Output is correct |
13 |
Correct |
367 ms |
25168 KB |
Output is correct |
14 |
Correct |
40 ms |
15640 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
8796 KB |
Output is correct |
2 |
Correct |
2 ms |
8796 KB |
Output is correct |
3 |
Correct |
2 ms |
8796 KB |
Output is correct |
4 |
Correct |
2 ms |
9052 KB |
Output is correct |
5 |
Correct |
2 ms |
9052 KB |
Output is correct |
6 |
Correct |
2 ms |
9052 KB |
Output is correct |
7 |
Correct |
3 ms |
9052 KB |
Output is correct |
8 |
Correct |
2 ms |
9044 KB |
Output is correct |
9 |
Correct |
2 ms |
9048 KB |
Output is correct |
10 |
Correct |
2 ms |
9052 KB |
Output is correct |
11 |
Correct |
2 ms |
8860 KB |
Output is correct |
12 |
Correct |
2 ms |
9052 KB |
Output is correct |
13 |
Correct |
2 ms |
9048 KB |
Output is correct |
14 |
Correct |
2 ms |
9052 KB |
Output is correct |
15 |
Correct |
2 ms |
9052 KB |
Output is correct |
16 |
Correct |
2 ms |
9068 KB |
Output is correct |
17 |
Correct |
2 ms |
9048 KB |
Output is correct |
18 |
Correct |
3 ms |
9048 KB |
Output is correct |
19 |
Correct |
2 ms |
9052 KB |
Output is correct |
20 |
Correct |
2 ms |
9052 KB |
Output is correct |
21 |
Correct |
2 ms |
9052 KB |
Output is correct |
22 |
Correct |
2 ms |
9052 KB |
Output is correct |
23 |
Correct |
2 ms |
9052 KB |
Output is correct |
24 |
Correct |
2 ms |
8880 KB |
Output is correct |
25 |
Correct |
2 ms |
9048 KB |
Output is correct |
26 |
Correct |
2 ms |
9052 KB |
Output is correct |
27 |
Correct |
2 ms |
8796 KB |
Output is correct |
28 |
Correct |
2 ms |
9052 KB |
Output is correct |
29 |
Correct |
2 ms |
9052 KB |
Output is correct |
30 |
Correct |
4 ms |
9052 KB |
Output is correct |
31 |
Correct |
4 ms |
9304 KB |
Output is correct |
32 |
Correct |
7 ms |
9304 KB |
Output is correct |
33 |
Correct |
5 ms |
9308 KB |
Output is correct |
34 |
Correct |
5 ms |
9308 KB |
Output is correct |
35 |
Correct |
5 ms |
9308 KB |
Output is correct |
36 |
Correct |
5 ms |
9308 KB |
Output is correct |
37 |
Correct |
4 ms |
9308 KB |
Output is correct |
38 |
Correct |
5 ms |
9320 KB |
Output is correct |
39 |
Correct |
4 ms |
9304 KB |
Output is correct |
40 |
Correct |
4 ms |
9308 KB |
Output is correct |
41 |
Correct |
4 ms |
9308 KB |
Output is correct |
42 |
Correct |
4 ms |
9508 KB |
Output is correct |
43 |
Correct |
5 ms |
9308 KB |
Output is correct |
44 |
Correct |
5 ms |
9304 KB |
Output is correct |
45 |
Correct |
7 ms |
9308 KB |
Output is correct |
46 |
Correct |
5 ms |
9304 KB |
Output is correct |
47 |
Correct |
5 ms |
9308 KB |
Output is correct |
48 |
Correct |
6 ms |
9308 KB |
Output is correct |
49 |
Correct |
5 ms |
9308 KB |
Output is correct |
50 |
Correct |
3 ms |
9052 KB |
Output is correct |
51 |
Correct |
4 ms |
9052 KB |
Output is correct |
52 |
Correct |
4 ms |
9308 KB |
Output is correct |
53 |
Correct |
4 ms |
9308 KB |
Output is correct |
54 |
Correct |
4 ms |
9204 KB |
Output is correct |
55 |
Correct |
4 ms |
9308 KB |
Output is correct |
56 |
Correct |
3 ms |
9052 KB |
Output is correct |
57 |
Correct |
2 ms |
9048 KB |
Output is correct |
58 |
Correct |
5 ms |
9220 KB |
Output is correct |
59 |
Correct |
2 ms |
8796 KB |
Output is correct |
60 |
Correct |
2 ms |
8796 KB |
Output is correct |
61 |
Correct |
2 ms |
9052 KB |
Output is correct |
62 |
Correct |
154 ms |
20988 KB |
Output is correct |
63 |
Correct |
123 ms |
25460 KB |
Output is correct |
64 |
Correct |
164 ms |
26976 KB |
Output is correct |
65 |
Correct |
201 ms |
30752 KB |
Output is correct |
66 |
Correct |
196 ms |
30804 KB |
Output is correct |
67 |
Correct |
194 ms |
30544 KB |
Output is correct |
68 |
Correct |
205 ms |
30656 KB |
Output is correct |
69 |
Correct |
195 ms |
30616 KB |
Output is correct |
70 |
Correct |
132 ms |
30284 KB |
Output is correct |
71 |
Correct |
120 ms |
30332 KB |
Output is correct |
72 |
Correct |
121 ms |
30584 KB |
Output is correct |
73 |
Correct |
43 ms |
25188 KB |
Output is correct |
74 |
Correct |
198 ms |
30288 KB |
Output is correct |
75 |
Correct |
35 ms |
15344 KB |
Output is correct |
76 |
Correct |
2 ms |
8796 KB |
Output is correct |
77 |
Correct |
136 ms |
14420 KB |
Output is correct |
78 |
Correct |
192 ms |
15184 KB |
Output is correct |
79 |
Correct |
155 ms |
15656 KB |
Output is correct |
80 |
Correct |
280 ms |
18256 KB |
Output is correct |
81 |
Correct |
256 ms |
18340 KB |
Output is correct |
82 |
Correct |
251 ms |
18160 KB |
Output is correct |
83 |
Correct |
241 ms |
18256 KB |
Output is correct |
84 |
Correct |
268 ms |
18116 KB |
Output is correct |
85 |
Correct |
264 ms |
18044 KB |
Output is correct |
86 |
Correct |
253 ms |
18532 KB |
Output is correct |
87 |
Correct |
248 ms |
18284 KB |
Output is correct |
88 |
Correct |
245 ms |
18772 KB |
Output is correct |
89 |
Correct |
261 ms |
19748 KB |
Output is correct |
90 |
Correct |
279 ms |
22356 KB |
Output is correct |
91 |
Correct |
245 ms |
20472 KB |
Output is correct |
92 |
Correct |
247 ms |
20420 KB |
Output is correct |
93 |
Correct |
258 ms |
20304 KB |
Output is correct |
94 |
Correct |
321 ms |
19512 KB |
Output is correct |
95 |
Correct |
318 ms |
19500 KB |
Output is correct |
96 |
Correct |
338 ms |
19308 KB |
Output is correct |
97 |
Correct |
323 ms |
19384 KB |
Output is correct |
98 |
Correct |
321 ms |
19540 KB |
Output is correct |
99 |
Correct |
324 ms |
19368 KB |
Output is correct |
100 |
Correct |
331 ms |
19520 KB |
Output is correct |
101 |
Correct |
324 ms |
19428 KB |
Output is correct |
102 |
Correct |
325 ms |
19536 KB |
Output is correct |
103 |
Correct |
326 ms |
19432 KB |
Output is correct |
104 |
Correct |
323 ms |
19536 KB |
Output is correct |
105 |
Correct |
329 ms |
19804 KB |
Output is correct |
106 |
Correct |
325 ms |
19936 KB |
Output is correct |
107 |
Correct |
327 ms |
20564 KB |
Output is correct |
108 |
Correct |
339 ms |
22156 KB |
Output is correct |
109 |
Correct |
348 ms |
24804 KB |
Output is correct |
110 |
Correct |
2 ms |
8792 KB |
Output is correct |
111 |
Correct |
2 ms |
9052 KB |
Output is correct |
112 |
Correct |
3 ms |
9140 KB |
Output is correct |
113 |
Correct |
297 ms |
18768 KB |
Output is correct |
114 |
Correct |
325 ms |
18772 KB |
Output is correct |
115 |
Correct |
359 ms |
21180 KB |
Output is correct |
116 |
Correct |
372 ms |
25280 KB |
Output is correct |
117 |
Correct |
410 ms |
25236 KB |
Output is correct |
118 |
Correct |
373 ms |
25172 KB |
Output is correct |
119 |
Correct |
391 ms |
25440 KB |
Output is correct |
120 |
Correct |
377 ms |
25124 KB |
Output is correct |
121 |
Correct |
378 ms |
25408 KB |
Output is correct |
122 |
Correct |
367 ms |
25168 KB |
Output is correct |
123 |
Correct |
40 ms |
15640 KB |
Output is correct |
124 |
Correct |
241 ms |
24620 KB |
Output is correct |
125 |
Correct |
185 ms |
21844 KB |
Output is correct |
126 |
Correct |
293 ms |
25028 KB |
Output is correct |
127 |
Correct |
329 ms |
25052 KB |
Output is correct |
128 |
Correct |
300 ms |
25212 KB |
Output is correct |
129 |
Correct |
308 ms |
24912 KB |
Output is correct |
130 |
Correct |
294 ms |
24920 KB |
Output is correct |
131 |
Correct |
207 ms |
29748 KB |
Output is correct |
132 |
Correct |
209 ms |
30544 KB |
Output is correct |
133 |
Correct |
223 ms |
27472 KB |
Output is correct |
134 |
Correct |
365 ms |
24660 KB |
Output is correct |
135 |
Correct |
367 ms |
24660 KB |
Output is correct |
136 |
Correct |
399 ms |
24668 KB |
Output is correct |
137 |
Correct |
129 ms |
25080 KB |
Output is correct |
138 |
Correct |
136 ms |
25024 KB |
Output is correct |
139 |
Correct |
130 ms |
25032 KB |
Output is correct |
140 |
Correct |
139 ms |
25288 KB |
Output is correct |
141 |
Correct |
148 ms |
25280 KB |
Output is correct |
142 |
Correct |
130 ms |
25284 KB |
Output is correct |
143 |
Correct |
47 ms |
19428 KB |
Output is correct |
144 |
Correct |
279 ms |
24656 KB |
Output is correct |