#pragma GCC optimize("Ofast")
#pragma GCC optimize ("O3")
#pragma GCC optimization ("unroll-loops")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,avx2,fma")
#include <bits/stdc++.h>
using namespace std;
#define ff first
#define ss second
#define pb push_back
//#define int long long
int n,k,u,v,tin[200005],m,num[200005],val[200005],l,r,ans[200005],a[200005],sz,timer=0,dst[200005],lg,up[200005][25],tout[200005],q;
pair <pair <int,int> ,int> query[200005];
vector <int> adj[100005];
set <int> s;
void dfs(int x,int p,int ds){
tin[x]=timer++;
val[tin[x]]=x;
dst[x]=ds;
up[x][0]=p;
for (int i = 1; i <= lg; ++i)
up[x][i] = up[up[x][i-1]][i-1];
for(int u=0;u<adj[x].size();u++){
if(adj[x][u]!=p){
dfs(adj[x][u],x,ds+1);
}
}
tout[x] = timer++;
}
bool is_ancestor(int u, int v)
{
return tin[u] <= tin[v] && tout[u] >= tout[v];
}
int lca(int u, int v)
{
if (is_ancestor(u, v))
return u;
if (is_ancestor(v, u))
return v;
for (int i = lg; i >= 0; --i) {
if (!is_ancestor(up[u][i], v))
u = up[u][i];
}
return up[u][0];
}
int dist(int x,int y){
return dst[x]+dst[y]-2*dst[lca(x,y)];
}
inline void add(int x){
// cout<<x<<"-"<<tin[x]<<endl;
num[x]++;
if(num[x]>1){
return;
}
if(s.size()==0){
s.insert(tin[x]);
sz=0;
return;
}
s.insert(tin[x]);
set <int> :: iterator it=s.lower_bound(tin[x]);
set <int> :: iterator it1=it;
set <int> :: iterator it2=it;
if(it1==s.begin()){
it1=s.end();
}
it1--;
it2++;
if(it2==s.end()){
it2=s.begin();
}
// cout<<(*it1)<<" "<<(*it)<<" "<<(*it2)<<" "<<val[(*it1)]<<" "<<val[(*it)]<<" "<<val[(*it2)]<<" "<<s.size()<<" "<<dist(val[(*it1)],val[(*it)])<<" "<<dist(val[(*it)],val[(*it2)])<<" "<<dist(val[(*it1)],val[(*it2)])<<" add\n";
sz+=(dist(val[(*it1)],val[(*it)])+dist(val[(*it)],val[(*it2)])-dist(val[(*it1)],val[(*it2)]));
// cout<<" "<<sz<<endl;
}
inline void remove(int x){
num[x]--;
if(num[x]>0){
return;
}
set <int> :: iterator it=s.lower_bound(tin[x]);
if(s.size()==1){
s.erase(it);
// cout<<s.size()<<endl;
sz=0;
return;
}
set <int> :: iterator it1=it;
set <int> :: iterator it2=it;
if(it1==s.begin()){
it1=s.end();
}
it1--;
it2++;
if(it2==s.end()){
it2=s.begin();
}
// cout<<(*it1)<<" "<<(*it)<<" "<<(*it2)<<" "<<val[(*it1)]<<" "<<val[(*it)]<<" "<<val[(*it2)]<<" "<<s.size()<<" "<<dist(val[(*it1)],val[(*it)])<<" "<<dist(val[(*it)],val[(*it2)])<<" "<<dist(val[(*it1)],val[(*it2)])<<" remove";
sz-=(dist(val[(*it1)],val[(*it)])+dist(val[(*it)],val[(*it2)])-dist(val[(*it1)],val[(*it2)]));
s.erase(it);
// cout<<" "<<sz<<endl;
}
bool comp(pair <pair <int,int>,int> a, pair <pair <int,int> ,int > b){
if(a.ff.ff/k ==b.ff.ff/k){
return a.ff.ss<b.ff.ss;
}
return (a.ff.ff)/k < (b.ff.ff)/k;
}
int main(){
scanf("%d%d%d",&n,&m,&q);
lg=ceil(log2(n));
k=ceil(sqrt(m));
for(int i=1;i<=n-1;i++){
scanf("%d%d",&u,&v);
adj[u].pb(v);
adj[v].pb(u);
}
for(int i=1;i<=m;i++){
scanf("%d",&a[i]);
}
for(int i=1;i<=q;i++){
scanf("%d%d",&query[i].ff.ff,&query[i].ff.ss);
query[i].ss=i;
}
dfs(1,1,0);
sort(query+1,query+q+1,comp);
int tl=1;
int tr=1;
int l,r;
add(a[tl]);
for(int i=1;i<=q;i++){
l=query[i].ff.ff;
r=query[i].ff.ss;
while(tr<r){
tr++;
add(a[tr]);
}
while(l<tl){
tl--;
add(a[tl]);
}
while(r<tr){
remove(a[tr]);
tr--;
}
while(tl<l){
remove(a[tl]);
tl++;
}
ans[query[i].ss]=(sz/2)+1;
}
for(int i=1;i<=q;i++){
printf("%d ",ans[i]);
}
}
Compilation message
tourism.cpp:3: warning: ignoring '#pragma GCC optimization' [-Wunknown-pragmas]
3 | #pragma GCC optimization ("unroll-loops")
|
tourism.cpp: In function 'void dfs(int, int, int)':
tourism.cpp:22:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
22 | for(int u=0;u<adj[x].size();u++){
| ~^~~~~~~~~~~~~~
tourism.cpp: In function 'int main()':
tourism.cpp:120:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
120 | scanf("%d%d%d",&n,&m,&q);
| ~~~~~^~~~~~~~~~~~~~~~~~~
tourism.cpp:126:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
126 | scanf("%d%d",&u,&v);
| ~~~~~^~~~~~~~~~~~~~
tourism.cpp:132:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
132 | scanf("%d",&a[i]);
| ~~~~~^~~~~~~~~~~~
tourism.cpp:135:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
135 | scanf("%d%d",&query[i].ff.ff,&query[i].ff.ss);
| ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
2640 KB |
Output is correct |
2 |
Correct |
3 ms |
2640 KB |
Output is correct |
3 |
Correct |
3 ms |
2640 KB |
Output is correct |
4 |
Correct |
3 ms |
2796 KB |
Output is correct |
5 |
Correct |
4 ms |
2640 KB |
Output is correct |
6 |
Correct |
3 ms |
2640 KB |
Output is correct |
7 |
Correct |
3 ms |
2640 KB |
Output is correct |
8 |
Correct |
3 ms |
2896 KB |
Output is correct |
9 |
Correct |
5 ms |
2896 KB |
Output is correct |
10 |
Correct |
4 ms |
2896 KB |
Output is correct |
11 |
Correct |
3 ms |
2896 KB |
Output is correct |
12 |
Correct |
3 ms |
2896 KB |
Output is correct |
13 |
Correct |
3 ms |
2900 KB |
Output is correct |
14 |
Correct |
3 ms |
2896 KB |
Output is correct |
15 |
Correct |
4 ms |
2896 KB |
Output is correct |
16 |
Correct |
3 ms |
3076 KB |
Output is correct |
17 |
Correct |
3 ms |
2896 KB |
Output is correct |
18 |
Correct |
4 ms |
2896 KB |
Output is correct |
19 |
Correct |
3 ms |
2896 KB |
Output is correct |
20 |
Correct |
4 ms |
2896 KB |
Output is correct |
21 |
Correct |
4 ms |
2896 KB |
Output is correct |
22 |
Correct |
4 ms |
2896 KB |
Output is correct |
23 |
Correct |
4 ms |
2896 KB |
Output is correct |
24 |
Correct |
4 ms |
2896 KB |
Output is correct |
25 |
Correct |
3 ms |
2896 KB |
Output is correct |
26 |
Correct |
5 ms |
2896 KB |
Output is correct |
27 |
Correct |
3 ms |
2808 KB |
Output is correct |
28 |
Correct |
2 ms |
2640 KB |
Output is correct |
29 |
Correct |
4 ms |
2848 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
2640 KB |
Output is correct |
2 |
Correct |
3 ms |
2640 KB |
Output is correct |
3 |
Correct |
3 ms |
2640 KB |
Output is correct |
4 |
Correct |
3 ms |
2796 KB |
Output is correct |
5 |
Correct |
4 ms |
2640 KB |
Output is correct |
6 |
Correct |
3 ms |
2640 KB |
Output is correct |
7 |
Correct |
3 ms |
2640 KB |
Output is correct |
8 |
Correct |
3 ms |
2896 KB |
Output is correct |
9 |
Correct |
5 ms |
2896 KB |
Output is correct |
10 |
Correct |
4 ms |
2896 KB |
Output is correct |
11 |
Correct |
3 ms |
2896 KB |
Output is correct |
12 |
Correct |
3 ms |
2896 KB |
Output is correct |
13 |
Correct |
3 ms |
2900 KB |
Output is correct |
14 |
Correct |
3 ms |
2896 KB |
Output is correct |
15 |
Correct |
4 ms |
2896 KB |
Output is correct |
16 |
Correct |
3 ms |
3076 KB |
Output is correct |
17 |
Correct |
3 ms |
2896 KB |
Output is correct |
18 |
Correct |
4 ms |
2896 KB |
Output is correct |
19 |
Correct |
3 ms |
2896 KB |
Output is correct |
20 |
Correct |
4 ms |
2896 KB |
Output is correct |
21 |
Correct |
4 ms |
2896 KB |
Output is correct |
22 |
Correct |
4 ms |
2896 KB |
Output is correct |
23 |
Correct |
4 ms |
2896 KB |
Output is correct |
24 |
Correct |
4 ms |
2896 KB |
Output is correct |
25 |
Correct |
3 ms |
2896 KB |
Output is correct |
26 |
Correct |
5 ms |
2896 KB |
Output is correct |
27 |
Correct |
3 ms |
2808 KB |
Output is correct |
28 |
Correct |
2 ms |
2640 KB |
Output is correct |
29 |
Correct |
4 ms |
2848 KB |
Output is correct |
30 |
Correct |
15 ms |
3152 KB |
Output is correct |
31 |
Correct |
26 ms |
3160 KB |
Output is correct |
32 |
Correct |
23 ms |
3152 KB |
Output is correct |
33 |
Correct |
33 ms |
3152 KB |
Output is correct |
34 |
Correct |
31 ms |
3232 KB |
Output is correct |
35 |
Correct |
5 ms |
3152 KB |
Output is correct |
36 |
Correct |
7 ms |
3184 KB |
Output is correct |
37 |
Correct |
7 ms |
3152 KB |
Output is correct |
38 |
Correct |
14 ms |
3316 KB |
Output is correct |
39 |
Correct |
19 ms |
3152 KB |
Output is correct |
40 |
Correct |
18 ms |
3152 KB |
Output is correct |
41 |
Correct |
5 ms |
3320 KB |
Output is correct |
42 |
Correct |
7 ms |
3408 KB |
Output is correct |
43 |
Correct |
5 ms |
3152 KB |
Output is correct |
44 |
Correct |
19 ms |
3336 KB |
Output is correct |
45 |
Correct |
22 ms |
3152 KB |
Output is correct |
46 |
Correct |
20 ms |
3340 KB |
Output is correct |
47 |
Correct |
6 ms |
3152 KB |
Output is correct |
48 |
Correct |
7 ms |
3152 KB |
Output is correct |
49 |
Correct |
7 ms |
3152 KB |
Output is correct |
50 |
Correct |
29 ms |
3240 KB |
Output is correct |
51 |
Correct |
34 ms |
3244 KB |
Output is correct |
52 |
Correct |
30 ms |
3280 KB |
Output is correct |
53 |
Correct |
34 ms |
3152 KB |
Output is correct |
54 |
Correct |
30 ms |
3152 KB |
Output is correct |
55 |
Correct |
28 ms |
3280 KB |
Output is correct |
56 |
Correct |
3 ms |
2640 KB |
Output is correct |
57 |
Correct |
5 ms |
3152 KB |
Output is correct |
58 |
Correct |
5 ms |
3152 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
2640 KB |
Output is correct |
2 |
Correct |
4 ms |
2640 KB |
Output is correct |
3 |
Correct |
4 ms |
2640 KB |
Output is correct |
4 |
Execution timed out |
5070 ms |
17992 KB |
Time limit exceeded |
5 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
2640 KB |
Output is correct |
2 |
Correct |
112 ms |
11664 KB |
Output is correct |
3 |
Correct |
171 ms |
13268 KB |
Output is correct |
4 |
Correct |
138 ms |
13384 KB |
Output is correct |
5 |
Correct |
162 ms |
21300 KB |
Output is correct |
6 |
Correct |
221 ms |
20876 KB |
Output is correct |
7 |
Correct |
261 ms |
20288 KB |
Output is correct |
8 |
Correct |
260 ms |
18892 KB |
Output is correct |
9 |
Correct |
263 ms |
19692 KB |
Output is correct |
10 |
Correct |
240 ms |
19576 KB |
Output is correct |
11 |
Correct |
202 ms |
18504 KB |
Output is correct |
12 |
Correct |
137 ms |
19596 KB |
Output is correct |
13 |
Correct |
143 ms |
18724 KB |
Output is correct |
14 |
Correct |
178 ms |
19016 KB |
Output is correct |
15 |
Correct |
190 ms |
20296 KB |
Output is correct |
16 |
Correct |
183 ms |
20324 KB |
Output is correct |
17 |
Correct |
157 ms |
20296 KB |
Output is correct |
18 |
Correct |
159 ms |
20296 KB |
Output is correct |
19 |
Correct |
89 ms |
21328 KB |
Output is correct |
20 |
Correct |
89 ms |
21320 KB |
Output is correct |
21 |
Correct |
107 ms |
19892 KB |
Output is correct |
22 |
Correct |
130 ms |
19448 KB |
Output is correct |
23 |
Correct |
145 ms |
19052 KB |
Output is correct |
24 |
Correct |
147 ms |
18760 KB |
Output is correct |
25 |
Correct |
173 ms |
18660 KB |
Output is correct |
26 |
Correct |
211 ms |
18672 KB |
Output is correct |
27 |
Correct |
258 ms |
18852 KB |
Output is correct |
28 |
Correct |
255 ms |
18592 KB |
Output is correct |
29 |
Correct |
248 ms |
18396 KB |
Output is correct |
30 |
Correct |
242 ms |
18480 KB |
Output is correct |
31 |
Correct |
213 ms |
18692 KB |
Output is correct |
32 |
Correct |
196 ms |
18984 KB |
Output is correct |
33 |
Correct |
264 ms |
19436 KB |
Output is correct |
34 |
Correct |
236 ms |
20272 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
2660 KB |
Output is correct |
2 |
Correct |
3 ms |
2640 KB |
Output is correct |
3 |
Correct |
6 ms |
2640 KB |
Output is correct |
4 |
Execution timed out |
5045 ms |
15268 KB |
Time limit exceeded |
5 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
2640 KB |
Output is correct |
2 |
Correct |
3 ms |
2640 KB |
Output is correct |
3 |
Correct |
3 ms |
2640 KB |
Output is correct |
4 |
Correct |
3 ms |
2796 KB |
Output is correct |
5 |
Correct |
4 ms |
2640 KB |
Output is correct |
6 |
Correct |
3 ms |
2640 KB |
Output is correct |
7 |
Correct |
3 ms |
2640 KB |
Output is correct |
8 |
Correct |
3 ms |
2896 KB |
Output is correct |
9 |
Correct |
5 ms |
2896 KB |
Output is correct |
10 |
Correct |
4 ms |
2896 KB |
Output is correct |
11 |
Correct |
3 ms |
2896 KB |
Output is correct |
12 |
Correct |
3 ms |
2896 KB |
Output is correct |
13 |
Correct |
3 ms |
2900 KB |
Output is correct |
14 |
Correct |
3 ms |
2896 KB |
Output is correct |
15 |
Correct |
4 ms |
2896 KB |
Output is correct |
16 |
Correct |
3 ms |
3076 KB |
Output is correct |
17 |
Correct |
3 ms |
2896 KB |
Output is correct |
18 |
Correct |
4 ms |
2896 KB |
Output is correct |
19 |
Correct |
3 ms |
2896 KB |
Output is correct |
20 |
Correct |
4 ms |
2896 KB |
Output is correct |
21 |
Correct |
4 ms |
2896 KB |
Output is correct |
22 |
Correct |
4 ms |
2896 KB |
Output is correct |
23 |
Correct |
4 ms |
2896 KB |
Output is correct |
24 |
Correct |
4 ms |
2896 KB |
Output is correct |
25 |
Correct |
3 ms |
2896 KB |
Output is correct |
26 |
Correct |
5 ms |
2896 KB |
Output is correct |
27 |
Correct |
3 ms |
2808 KB |
Output is correct |
28 |
Correct |
2 ms |
2640 KB |
Output is correct |
29 |
Correct |
4 ms |
2848 KB |
Output is correct |
30 |
Correct |
15 ms |
3152 KB |
Output is correct |
31 |
Correct |
26 ms |
3160 KB |
Output is correct |
32 |
Correct |
23 ms |
3152 KB |
Output is correct |
33 |
Correct |
33 ms |
3152 KB |
Output is correct |
34 |
Correct |
31 ms |
3232 KB |
Output is correct |
35 |
Correct |
5 ms |
3152 KB |
Output is correct |
36 |
Correct |
7 ms |
3184 KB |
Output is correct |
37 |
Correct |
7 ms |
3152 KB |
Output is correct |
38 |
Correct |
14 ms |
3316 KB |
Output is correct |
39 |
Correct |
19 ms |
3152 KB |
Output is correct |
40 |
Correct |
18 ms |
3152 KB |
Output is correct |
41 |
Correct |
5 ms |
3320 KB |
Output is correct |
42 |
Correct |
7 ms |
3408 KB |
Output is correct |
43 |
Correct |
5 ms |
3152 KB |
Output is correct |
44 |
Correct |
19 ms |
3336 KB |
Output is correct |
45 |
Correct |
22 ms |
3152 KB |
Output is correct |
46 |
Correct |
20 ms |
3340 KB |
Output is correct |
47 |
Correct |
6 ms |
3152 KB |
Output is correct |
48 |
Correct |
7 ms |
3152 KB |
Output is correct |
49 |
Correct |
7 ms |
3152 KB |
Output is correct |
50 |
Correct |
29 ms |
3240 KB |
Output is correct |
51 |
Correct |
34 ms |
3244 KB |
Output is correct |
52 |
Correct |
30 ms |
3280 KB |
Output is correct |
53 |
Correct |
34 ms |
3152 KB |
Output is correct |
54 |
Correct |
30 ms |
3152 KB |
Output is correct |
55 |
Correct |
28 ms |
3280 KB |
Output is correct |
56 |
Correct |
3 ms |
2640 KB |
Output is correct |
57 |
Correct |
5 ms |
3152 KB |
Output is correct |
58 |
Correct |
5 ms |
3152 KB |
Output is correct |
59 |
Correct |
3 ms |
2640 KB |
Output is correct |
60 |
Correct |
4 ms |
2640 KB |
Output is correct |
61 |
Correct |
4 ms |
2640 KB |
Output is correct |
62 |
Execution timed out |
5070 ms |
17992 KB |
Time limit exceeded |
63 |
Halted |
0 ms |
0 KB |
- |