#include<bits/stdc++.h>
#include<queue>
using namespace std;
const int N=1e5+5;
const int M=3e5+5;
const int LG=20;
int n,m,q;
int c[N];
vector<int> adj[N];
int sz[N],hv[N],hd[N],pa[N],lv[N],tin[N],tout[N],pos[M];
int timer;
vector<pair<int,int>> qr[N],chain[N];
int ans[N];
struct Fenwick{
int t[N];
void update(int i,int v){
for(;i<N;i+=i&-i)t[i]+=v;
}
int query(int i){
int res=0;
for(;i;i-=i&-i)res+=t[i];
return res;
}
}f;
template<class T,class cmp>
struct RMQ{
T t[LG][M];
T get_min(T a,T b){
return cmp()(a,b)?a:b;
}
void build(){
for(int i=0;i<LG-1;i++){
for(int j=1;j+(2<<i)-1<M;j++){
t[i+1][j]=get_min(t[i][j],t[i][j+(1<<i)]);
}
}
}
T query(int l,int r){
int k=31-__builtin_clz(r-l+1);
return get_min(t[k][l],t[k][r-(1<<k)+1]);
}
};
RMQ<int,less<int>> rtin,rdep;
RMQ<int,greater<int>> rtout;
void dfs(int u,int p=0){
tin[u]=++timer;
pos[timer]=u;
pa[u]=p;
sz[u]=1;
for(auto v:adj[u])if(v!=p){
lv[v]=lv[u]+1;
dfs(v,u);
sz[u]+=sz[v];
if(sz[v]>sz[hv[u]])hv[u]=v;
pos[++timer]=u;
}
tout[u]=timer;
}
void hld(int u,int p=0){
if(!hd[u])hd[u]=u;
if(hv[u])hd[hv[u]]=hd[u],hld(hv[u],u);
for(auto v:adj[u])if(v!=p&&v!=hv[u])hld(v,u);
}
int main(){
cin.tie(nullptr)->sync_with_stdio(false);
cin >> n >> m >> q;
for(int i=1;i<n;i++){
int u,v;
cin >> u >> v;
adj[u].emplace_back(v);
adj[v].emplace_back(u);
}
for(int i=1;i<=m;i++)cin >> c[i];
for(int i=1;i<=q;i++){
int l,r;
cin >> l >> r;
qr[r].emplace_back(l,i);
}
dfs(1);
hld(1);
for(int i=1;i<=timer;i++)rdep.t[0][i]=lv[pos[i]];
for(int i=1;i<=m;i++){
rtin.t[0][i]=tin[c[i]];
rtout.t[0][i]=tout[c[i]];
}
rdep.build();
rtin.build();
rtout.build();
for(int r=1;r<=m;r++){
for(int u=c[r];u;u=pa[hd[u]]){
int h=hd[u];
auto &ch=chain[h];
while(!ch.empty()&&lv[ch.back().first]<=lv[u]){
auto [v,i]=ch.back();
ch.pop_back();
int val=lv[v]-lv[h]+1;
f.update(i,-val);
if(!ch.empty())f.update(ch.back().second,val);
}
int val=lv[u]-lv[h]+1;
if(!ch.empty())f.update(ch.back().second,-val);
ch.emplace_back(u,r);
f.update(r,lv[u]-lv[h]+1);
}
for(auto [l,i]:qr[r]){
int u=rtin.query(l,r),v=rtout.query(l,r);
ans[i]=f.query(r)-f.query(l-1)-rdep.query(u,v);
}
}
for(int i=1;i<=q;i++)cout << ans[i] << "\n";
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
40 ms |
82512 KB |
Output is correct |
2 |
Correct |
24 ms |
82480 KB |
Output is correct |
3 |
Correct |
23 ms |
82520 KB |
Output is correct |
4 |
Correct |
23 ms |
82524 KB |
Output is correct |
5 |
Correct |
22 ms |
82472 KB |
Output is correct |
6 |
Correct |
22 ms |
82524 KB |
Output is correct |
7 |
Correct |
22 ms |
82520 KB |
Output is correct |
8 |
Correct |
22 ms |
82512 KB |
Output is correct |
9 |
Correct |
22 ms |
82516 KB |
Output is correct |
10 |
Correct |
22 ms |
82520 KB |
Output is correct |
11 |
Correct |
23 ms |
82300 KB |
Output is correct |
12 |
Correct |
22 ms |
82524 KB |
Output is correct |
13 |
Correct |
22 ms |
82504 KB |
Output is correct |
14 |
Correct |
22 ms |
82524 KB |
Output is correct |
15 |
Correct |
23 ms |
82548 KB |
Output is correct |
16 |
Correct |
22 ms |
82452 KB |
Output is correct |
17 |
Correct |
22 ms |
82524 KB |
Output is correct |
18 |
Correct |
23 ms |
82516 KB |
Output is correct |
19 |
Correct |
24 ms |
82560 KB |
Output is correct |
20 |
Correct |
23 ms |
82524 KB |
Output is correct |
21 |
Correct |
23 ms |
82536 KB |
Output is correct |
22 |
Correct |
22 ms |
82520 KB |
Output is correct |
23 |
Correct |
22 ms |
82544 KB |
Output is correct |
24 |
Correct |
22 ms |
82524 KB |
Output is correct |
25 |
Correct |
23 ms |
82444 KB |
Output is correct |
26 |
Correct |
24 ms |
82512 KB |
Output is correct |
27 |
Correct |
22 ms |
82520 KB |
Output is correct |
28 |
Correct |
22 ms |
82524 KB |
Output is correct |
29 |
Correct |
23 ms |
82520 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
40 ms |
82512 KB |
Output is correct |
2 |
Correct |
24 ms |
82480 KB |
Output is correct |
3 |
Correct |
23 ms |
82520 KB |
Output is correct |
4 |
Correct |
23 ms |
82524 KB |
Output is correct |
5 |
Correct |
22 ms |
82472 KB |
Output is correct |
6 |
Correct |
22 ms |
82524 KB |
Output is correct |
7 |
Correct |
22 ms |
82520 KB |
Output is correct |
8 |
Correct |
22 ms |
82512 KB |
Output is correct |
9 |
Correct |
22 ms |
82516 KB |
Output is correct |
10 |
Correct |
22 ms |
82520 KB |
Output is correct |
11 |
Correct |
23 ms |
82300 KB |
Output is correct |
12 |
Correct |
22 ms |
82524 KB |
Output is correct |
13 |
Correct |
22 ms |
82504 KB |
Output is correct |
14 |
Correct |
22 ms |
82524 KB |
Output is correct |
15 |
Correct |
23 ms |
82548 KB |
Output is correct |
16 |
Correct |
22 ms |
82452 KB |
Output is correct |
17 |
Correct |
22 ms |
82524 KB |
Output is correct |
18 |
Correct |
23 ms |
82516 KB |
Output is correct |
19 |
Correct |
24 ms |
82560 KB |
Output is correct |
20 |
Correct |
23 ms |
82524 KB |
Output is correct |
21 |
Correct |
23 ms |
82536 KB |
Output is correct |
22 |
Correct |
22 ms |
82520 KB |
Output is correct |
23 |
Correct |
22 ms |
82544 KB |
Output is correct |
24 |
Correct |
22 ms |
82524 KB |
Output is correct |
25 |
Correct |
23 ms |
82444 KB |
Output is correct |
26 |
Correct |
24 ms |
82512 KB |
Output is correct |
27 |
Correct |
22 ms |
82520 KB |
Output is correct |
28 |
Correct |
22 ms |
82524 KB |
Output is correct |
29 |
Correct |
23 ms |
82520 KB |
Output is correct |
30 |
Correct |
24 ms |
82512 KB |
Output is correct |
31 |
Correct |
23 ms |
82524 KB |
Output is correct |
32 |
Correct |
24 ms |
82720 KB |
Output is correct |
33 |
Correct |
24 ms |
82524 KB |
Output is correct |
34 |
Correct |
24 ms |
82516 KB |
Output is correct |
35 |
Correct |
24 ms |
82516 KB |
Output is correct |
36 |
Correct |
24 ms |
82520 KB |
Output is correct |
37 |
Correct |
24 ms |
82656 KB |
Output is correct |
38 |
Correct |
24 ms |
83040 KB |
Output is correct |
39 |
Correct |
24 ms |
82712 KB |
Output is correct |
40 |
Correct |
23 ms |
82776 KB |
Output is correct |
41 |
Correct |
23 ms |
83016 KB |
Output is correct |
42 |
Correct |
24 ms |
82772 KB |
Output is correct |
43 |
Correct |
24 ms |
82768 KB |
Output is correct |
44 |
Correct |
23 ms |
82724 KB |
Output is correct |
45 |
Correct |
23 ms |
82516 KB |
Output is correct |
46 |
Correct |
23 ms |
82780 KB |
Output is correct |
47 |
Correct |
23 ms |
82520 KB |
Output is correct |
48 |
Correct |
23 ms |
82524 KB |
Output is correct |
49 |
Correct |
23 ms |
82512 KB |
Output is correct |
50 |
Correct |
24 ms |
82512 KB |
Output is correct |
51 |
Correct |
23 ms |
82524 KB |
Output is correct |
52 |
Correct |
24 ms |
82640 KB |
Output is correct |
53 |
Correct |
23 ms |
82516 KB |
Output is correct |
54 |
Correct |
24 ms |
82512 KB |
Output is correct |
55 |
Correct |
24 ms |
82516 KB |
Output is correct |
56 |
Correct |
23 ms |
82524 KB |
Output is correct |
57 |
Correct |
23 ms |
82524 KB |
Output is correct |
58 |
Correct |
24 ms |
82516 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
22 ms |
82512 KB |
Output is correct |
2 |
Correct |
22 ms |
82520 KB |
Output is correct |
3 |
Correct |
23 ms |
82524 KB |
Output is correct |
4 |
Correct |
72 ms |
93120 KB |
Output is correct |
5 |
Correct |
70 ms |
96544 KB |
Output is correct |
6 |
Correct |
74 ms |
97988 KB |
Output is correct |
7 |
Correct |
93 ms |
100948 KB |
Output is correct |
8 |
Correct |
92 ms |
100944 KB |
Output is correct |
9 |
Correct |
94 ms |
100988 KB |
Output is correct |
10 |
Correct |
102 ms |
100944 KB |
Output is correct |
11 |
Correct |
95 ms |
100856 KB |
Output is correct |
12 |
Correct |
78 ms |
99920 KB |
Output is correct |
13 |
Correct |
78 ms |
99936 KB |
Output is correct |
14 |
Correct |
77 ms |
99924 KB |
Output is correct |
15 |
Correct |
61 ms |
97476 KB |
Output is correct |
16 |
Correct |
89 ms |
100720 KB |
Output is correct |
17 |
Correct |
59 ms |
86320 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
22 ms |
82508 KB |
Output is correct |
2 |
Correct |
70 ms |
85920 KB |
Output is correct |
3 |
Correct |
88 ms |
86608 KB |
Output is correct |
4 |
Correct |
79 ms |
86752 KB |
Output is correct |
5 |
Correct |
108 ms |
88404 KB |
Output is correct |
6 |
Correct |
109 ms |
88468 KB |
Output is correct |
7 |
Correct |
107 ms |
88412 KB |
Output is correct |
8 |
Correct |
106 ms |
88468 KB |
Output is correct |
9 |
Correct |
106 ms |
88404 KB |
Output is correct |
10 |
Correct |
108 ms |
88404 KB |
Output is correct |
11 |
Correct |
106 ms |
88536 KB |
Output is correct |
12 |
Correct |
111 ms |
88592 KB |
Output is correct |
13 |
Correct |
109 ms |
89168 KB |
Output is correct |
14 |
Correct |
116 ms |
89748 KB |
Output is correct |
15 |
Correct |
132 ms |
92500 KB |
Output is correct |
16 |
Correct |
107 ms |
89172 KB |
Output is correct |
17 |
Correct |
115 ms |
89056 KB |
Output is correct |
18 |
Correct |
123 ms |
89068 KB |
Output is correct |
19 |
Correct |
88 ms |
87268 KB |
Output is correct |
20 |
Correct |
86 ms |
87376 KB |
Output is correct |
21 |
Correct |
86 ms |
87380 KB |
Output is correct |
22 |
Correct |
85 ms |
87376 KB |
Output is correct |
23 |
Correct |
82 ms |
87292 KB |
Output is correct |
24 |
Correct |
86 ms |
87260 KB |
Output is correct |
25 |
Correct |
90 ms |
87312 KB |
Output is correct |
26 |
Correct |
87 ms |
87472 KB |
Output is correct |
27 |
Correct |
85 ms |
87376 KB |
Output is correct |
28 |
Correct |
85 ms |
87232 KB |
Output is correct |
29 |
Correct |
86 ms |
87380 KB |
Output is correct |
30 |
Correct |
89 ms |
87636 KB |
Output is correct |
31 |
Correct |
89 ms |
87892 KB |
Output is correct |
32 |
Correct |
93 ms |
88400 KB |
Output is correct |
33 |
Correct |
102 ms |
89164 KB |
Output is correct |
34 |
Correct |
112 ms |
91384 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
22 ms |
82524 KB |
Output is correct |
2 |
Correct |
23 ms |
82544 KB |
Output is correct |
3 |
Correct |
23 ms |
82524 KB |
Output is correct |
4 |
Correct |
127 ms |
89128 KB |
Output is correct |
5 |
Correct |
130 ms |
89172 KB |
Output is correct |
6 |
Correct |
155 ms |
91052 KB |
Output is correct |
7 |
Correct |
153 ms |
91832 KB |
Output is correct |
8 |
Correct |
152 ms |
91728 KB |
Output is correct |
9 |
Correct |
167 ms |
91976 KB |
Output is correct |
10 |
Correct |
154 ms |
91908 KB |
Output is correct |
11 |
Correct |
159 ms |
91732 KB |
Output is correct |
12 |
Correct |
151 ms |
91732 KB |
Output is correct |
13 |
Correct |
154 ms |
91928 KB |
Output is correct |
14 |
Correct |
58 ms |
86100 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
40 ms |
82512 KB |
Output is correct |
2 |
Correct |
24 ms |
82480 KB |
Output is correct |
3 |
Correct |
23 ms |
82520 KB |
Output is correct |
4 |
Correct |
23 ms |
82524 KB |
Output is correct |
5 |
Correct |
22 ms |
82472 KB |
Output is correct |
6 |
Correct |
22 ms |
82524 KB |
Output is correct |
7 |
Correct |
22 ms |
82520 KB |
Output is correct |
8 |
Correct |
22 ms |
82512 KB |
Output is correct |
9 |
Correct |
22 ms |
82516 KB |
Output is correct |
10 |
Correct |
22 ms |
82520 KB |
Output is correct |
11 |
Correct |
23 ms |
82300 KB |
Output is correct |
12 |
Correct |
22 ms |
82524 KB |
Output is correct |
13 |
Correct |
22 ms |
82504 KB |
Output is correct |
14 |
Correct |
22 ms |
82524 KB |
Output is correct |
15 |
Correct |
23 ms |
82548 KB |
Output is correct |
16 |
Correct |
22 ms |
82452 KB |
Output is correct |
17 |
Correct |
22 ms |
82524 KB |
Output is correct |
18 |
Correct |
23 ms |
82516 KB |
Output is correct |
19 |
Correct |
24 ms |
82560 KB |
Output is correct |
20 |
Correct |
23 ms |
82524 KB |
Output is correct |
21 |
Correct |
23 ms |
82536 KB |
Output is correct |
22 |
Correct |
22 ms |
82520 KB |
Output is correct |
23 |
Correct |
22 ms |
82544 KB |
Output is correct |
24 |
Correct |
22 ms |
82524 KB |
Output is correct |
25 |
Correct |
23 ms |
82444 KB |
Output is correct |
26 |
Correct |
24 ms |
82512 KB |
Output is correct |
27 |
Correct |
22 ms |
82520 KB |
Output is correct |
28 |
Correct |
22 ms |
82524 KB |
Output is correct |
29 |
Correct |
23 ms |
82520 KB |
Output is correct |
30 |
Correct |
24 ms |
82512 KB |
Output is correct |
31 |
Correct |
23 ms |
82524 KB |
Output is correct |
32 |
Correct |
24 ms |
82720 KB |
Output is correct |
33 |
Correct |
24 ms |
82524 KB |
Output is correct |
34 |
Correct |
24 ms |
82516 KB |
Output is correct |
35 |
Correct |
24 ms |
82516 KB |
Output is correct |
36 |
Correct |
24 ms |
82520 KB |
Output is correct |
37 |
Correct |
24 ms |
82656 KB |
Output is correct |
38 |
Correct |
24 ms |
83040 KB |
Output is correct |
39 |
Correct |
24 ms |
82712 KB |
Output is correct |
40 |
Correct |
23 ms |
82776 KB |
Output is correct |
41 |
Correct |
23 ms |
83016 KB |
Output is correct |
42 |
Correct |
24 ms |
82772 KB |
Output is correct |
43 |
Correct |
24 ms |
82768 KB |
Output is correct |
44 |
Correct |
23 ms |
82724 KB |
Output is correct |
45 |
Correct |
23 ms |
82516 KB |
Output is correct |
46 |
Correct |
23 ms |
82780 KB |
Output is correct |
47 |
Correct |
23 ms |
82520 KB |
Output is correct |
48 |
Correct |
23 ms |
82524 KB |
Output is correct |
49 |
Correct |
23 ms |
82512 KB |
Output is correct |
50 |
Correct |
24 ms |
82512 KB |
Output is correct |
51 |
Correct |
23 ms |
82524 KB |
Output is correct |
52 |
Correct |
24 ms |
82640 KB |
Output is correct |
53 |
Correct |
23 ms |
82516 KB |
Output is correct |
54 |
Correct |
24 ms |
82512 KB |
Output is correct |
55 |
Correct |
24 ms |
82516 KB |
Output is correct |
56 |
Correct |
23 ms |
82524 KB |
Output is correct |
57 |
Correct |
23 ms |
82524 KB |
Output is correct |
58 |
Correct |
24 ms |
82516 KB |
Output is correct |
59 |
Correct |
22 ms |
82512 KB |
Output is correct |
60 |
Correct |
22 ms |
82520 KB |
Output is correct |
61 |
Correct |
23 ms |
82524 KB |
Output is correct |
62 |
Correct |
72 ms |
93120 KB |
Output is correct |
63 |
Correct |
70 ms |
96544 KB |
Output is correct |
64 |
Correct |
74 ms |
97988 KB |
Output is correct |
65 |
Correct |
93 ms |
100948 KB |
Output is correct |
66 |
Correct |
92 ms |
100944 KB |
Output is correct |
67 |
Correct |
94 ms |
100988 KB |
Output is correct |
68 |
Correct |
102 ms |
100944 KB |
Output is correct |
69 |
Correct |
95 ms |
100856 KB |
Output is correct |
70 |
Correct |
78 ms |
99920 KB |
Output is correct |
71 |
Correct |
78 ms |
99936 KB |
Output is correct |
72 |
Correct |
77 ms |
99924 KB |
Output is correct |
73 |
Correct |
61 ms |
97476 KB |
Output is correct |
74 |
Correct |
89 ms |
100720 KB |
Output is correct |
75 |
Correct |
59 ms |
86320 KB |
Output is correct |
76 |
Correct |
22 ms |
82508 KB |
Output is correct |
77 |
Correct |
70 ms |
85920 KB |
Output is correct |
78 |
Correct |
88 ms |
86608 KB |
Output is correct |
79 |
Correct |
79 ms |
86752 KB |
Output is correct |
80 |
Correct |
108 ms |
88404 KB |
Output is correct |
81 |
Correct |
109 ms |
88468 KB |
Output is correct |
82 |
Correct |
107 ms |
88412 KB |
Output is correct |
83 |
Correct |
106 ms |
88468 KB |
Output is correct |
84 |
Correct |
106 ms |
88404 KB |
Output is correct |
85 |
Correct |
108 ms |
88404 KB |
Output is correct |
86 |
Correct |
106 ms |
88536 KB |
Output is correct |
87 |
Correct |
111 ms |
88592 KB |
Output is correct |
88 |
Correct |
109 ms |
89168 KB |
Output is correct |
89 |
Correct |
116 ms |
89748 KB |
Output is correct |
90 |
Correct |
132 ms |
92500 KB |
Output is correct |
91 |
Correct |
107 ms |
89172 KB |
Output is correct |
92 |
Correct |
115 ms |
89056 KB |
Output is correct |
93 |
Correct |
123 ms |
89068 KB |
Output is correct |
94 |
Correct |
88 ms |
87268 KB |
Output is correct |
95 |
Correct |
86 ms |
87376 KB |
Output is correct |
96 |
Correct |
86 ms |
87380 KB |
Output is correct |
97 |
Correct |
85 ms |
87376 KB |
Output is correct |
98 |
Correct |
82 ms |
87292 KB |
Output is correct |
99 |
Correct |
86 ms |
87260 KB |
Output is correct |
100 |
Correct |
90 ms |
87312 KB |
Output is correct |
101 |
Correct |
87 ms |
87472 KB |
Output is correct |
102 |
Correct |
85 ms |
87376 KB |
Output is correct |
103 |
Correct |
85 ms |
87232 KB |
Output is correct |
104 |
Correct |
86 ms |
87380 KB |
Output is correct |
105 |
Correct |
89 ms |
87636 KB |
Output is correct |
106 |
Correct |
89 ms |
87892 KB |
Output is correct |
107 |
Correct |
93 ms |
88400 KB |
Output is correct |
108 |
Correct |
102 ms |
89164 KB |
Output is correct |
109 |
Correct |
112 ms |
91384 KB |
Output is correct |
110 |
Correct |
22 ms |
82524 KB |
Output is correct |
111 |
Correct |
23 ms |
82544 KB |
Output is correct |
112 |
Correct |
23 ms |
82524 KB |
Output is correct |
113 |
Correct |
127 ms |
89128 KB |
Output is correct |
114 |
Correct |
130 ms |
89172 KB |
Output is correct |
115 |
Correct |
155 ms |
91052 KB |
Output is correct |
116 |
Correct |
153 ms |
91832 KB |
Output is correct |
117 |
Correct |
152 ms |
91728 KB |
Output is correct |
118 |
Correct |
167 ms |
91976 KB |
Output is correct |
119 |
Correct |
154 ms |
91908 KB |
Output is correct |
120 |
Correct |
159 ms |
91732 KB |
Output is correct |
121 |
Correct |
151 ms |
91732 KB |
Output is correct |
122 |
Correct |
154 ms |
91928 KB |
Output is correct |
123 |
Correct |
58 ms |
86100 KB |
Output is correct |
124 |
Correct |
129 ms |
92496 KB |
Output is correct |
125 |
Correct |
110 ms |
90544 KB |
Output is correct |
126 |
Correct |
141 ms |
92932 KB |
Output is correct |
127 |
Correct |
141 ms |
92804 KB |
Output is correct |
128 |
Correct |
151 ms |
92820 KB |
Output is correct |
129 |
Correct |
138 ms |
92980 KB |
Output is correct |
130 |
Correct |
140 ms |
93028 KB |
Output is correct |
131 |
Correct |
105 ms |
99356 KB |
Output is correct |
132 |
Correct |
103 ms |
100692 KB |
Output is correct |
133 |
Correct |
100 ms |
96292 KB |
Output is correct |
134 |
Correct |
126 ms |
91712 KB |
Output is correct |
135 |
Correct |
118 ms |
91888 KB |
Output is correct |
136 |
Correct |
119 ms |
91728 KB |
Output is correct |
137 |
Correct |
102 ms |
93936 KB |
Output is correct |
138 |
Correct |
98 ms |
93984 KB |
Output is correct |
139 |
Correct |
106 ms |
94156 KB |
Output is correct |
140 |
Correct |
98 ms |
93900 KB |
Output is correct |
141 |
Correct |
97 ms |
93764 KB |
Output is correct |
142 |
Correct |
106 ms |
93792 KB |
Output is correct |
143 |
Correct |
65 ms |
88228 KB |
Output is correct |
144 |
Correct |
139 ms |
92756 KB |
Output is correct |