#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);
| ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 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 |
2640 KB |
Output is correct |
5 |
Correct |
3 ms |
2892 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 |
6 ms |
2896 KB |
Output is correct |
10 |
Correct |
4 ms |
2916 KB |
Output is correct |
11 |
Correct |
4 ms |
2896 KB |
Output is correct |
12 |
Correct |
3 ms |
2896 KB |
Output is correct |
13 |
Correct |
4 ms |
2896 KB |
Output is correct |
14 |
Correct |
4 ms |
2896 KB |
Output is correct |
15 |
Correct |
3 ms |
2896 KB |
Output is correct |
16 |
Correct |
4 ms |
2896 KB |
Output is correct |
17 |
Correct |
5 ms |
2920 KB |
Output is correct |
18 |
Correct |
5 ms |
2896 KB |
Output is correct |
19 |
Correct |
5 ms |
2896 KB |
Output is correct |
20 |
Correct |
4 ms |
2896 KB |
Output is correct |
21 |
Correct |
5 ms |
2896 KB |
Output is correct |
22 |
Correct |
5 ms |
2896 KB |
Output is correct |
23 |
Correct |
4 ms |
2896 KB |
Output is correct |
24 |
Correct |
6 ms |
2800 KB |
Output is correct |
25 |
Correct |
4 ms |
2896 KB |
Output is correct |
26 |
Correct |
6 ms |
2908 KB |
Output is correct |
27 |
Correct |
3 ms |
2640 KB |
Output is correct |
28 |
Correct |
4 ms |
2640 KB |
Output is correct |
29 |
Correct |
4 ms |
2896 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 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 |
2640 KB |
Output is correct |
5 |
Correct |
3 ms |
2892 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 |
6 ms |
2896 KB |
Output is correct |
10 |
Correct |
4 ms |
2916 KB |
Output is correct |
11 |
Correct |
4 ms |
2896 KB |
Output is correct |
12 |
Correct |
3 ms |
2896 KB |
Output is correct |
13 |
Correct |
4 ms |
2896 KB |
Output is correct |
14 |
Correct |
4 ms |
2896 KB |
Output is correct |
15 |
Correct |
3 ms |
2896 KB |
Output is correct |
16 |
Correct |
4 ms |
2896 KB |
Output is correct |
17 |
Correct |
5 ms |
2920 KB |
Output is correct |
18 |
Correct |
5 ms |
2896 KB |
Output is correct |
19 |
Correct |
5 ms |
2896 KB |
Output is correct |
20 |
Correct |
4 ms |
2896 KB |
Output is correct |
21 |
Correct |
5 ms |
2896 KB |
Output is correct |
22 |
Correct |
5 ms |
2896 KB |
Output is correct |
23 |
Correct |
4 ms |
2896 KB |
Output is correct |
24 |
Correct |
6 ms |
2800 KB |
Output is correct |
25 |
Correct |
4 ms |
2896 KB |
Output is correct |
26 |
Correct |
6 ms |
2908 KB |
Output is correct |
27 |
Correct |
3 ms |
2640 KB |
Output is correct |
28 |
Correct |
4 ms |
2640 KB |
Output is correct |
29 |
Correct |
4 ms |
2896 KB |
Output is correct |
30 |
Correct |
24 ms |
3164 KB |
Output is correct |
31 |
Correct |
20 ms |
3172 KB |
Output is correct |
32 |
Correct |
32 ms |
3272 KB |
Output is correct |
33 |
Correct |
37 ms |
3152 KB |
Output is correct |
34 |
Correct |
31 ms |
3276 KB |
Output is correct |
35 |
Correct |
9 ms |
3132 KB |
Output is correct |
36 |
Correct |
8 ms |
3176 KB |
Output is correct |
37 |
Correct |
9 ms |
3152 KB |
Output is correct |
38 |
Correct |
18 ms |
3152 KB |
Output is correct |
39 |
Correct |
18 ms |
3328 KB |
Output is correct |
40 |
Correct |
19 ms |
3376 KB |
Output is correct |
41 |
Correct |
6 ms |
3388 KB |
Output is correct |
42 |
Correct |
7 ms |
3152 KB |
Output is correct |
43 |
Correct |
7 ms |
3152 KB |
Output is correct |
44 |
Correct |
26 ms |
3320 KB |
Output is correct |
45 |
Correct |
22 ms |
3340 KB |
Output is correct |
46 |
Correct |
18 ms |
3152 KB |
Output is correct |
47 |
Correct |
7 ms |
3408 KB |
Output is correct |
48 |
Correct |
5 ms |
3152 KB |
Output is correct |
49 |
Correct |
8 ms |
3160 KB |
Output is correct |
50 |
Correct |
27 ms |
3152 KB |
Output is correct |
51 |
Correct |
31 ms |
3064 KB |
Output is correct |
52 |
Correct |
36 ms |
3280 KB |
Output is correct |
53 |
Correct |
26 ms |
3152 KB |
Output is correct |
54 |
Correct |
29 ms |
3152 KB |
Output is correct |
55 |
Correct |
33 ms |
3408 KB |
Output is correct |
56 |
Correct |
6 ms |
3068 KB |
Output is correct |
57 |
Correct |
5 ms |
3152 KB |
Output is correct |
58 |
Correct |
5 ms |
3152 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
2808 KB |
Output is correct |
2 |
Correct |
4 ms |
2640 KB |
Output is correct |
3 |
Correct |
5 ms |
2896 KB |
Output is correct |
4 |
Execution timed out |
5055 ms |
19152 KB |
Time limit exceeded |
5 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
2640 KB |
Output is correct |
2 |
Correct |
87 ms |
12432 KB |
Output is correct |
3 |
Correct |
103 ms |
12860 KB |
Output is correct |
4 |
Correct |
98 ms |
14160 KB |
Output is correct |
5 |
Correct |
135 ms |
22512 KB |
Output is correct |
6 |
Correct |
144 ms |
21832 KB |
Output is correct |
7 |
Correct |
194 ms |
20380 KB |
Output is correct |
8 |
Correct |
191 ms |
20212 KB |
Output is correct |
9 |
Correct |
194 ms |
20040 KB |
Output is correct |
10 |
Correct |
213 ms |
19444 KB |
Output is correct |
11 |
Correct |
206 ms |
19476 KB |
Output is correct |
12 |
Correct |
179 ms |
19528 KB |
Output is correct |
13 |
Correct |
171 ms |
20040 KB |
Output is correct |
14 |
Correct |
150 ms |
20184 KB |
Output is correct |
15 |
Correct |
201 ms |
22088 KB |
Output is correct |
16 |
Correct |
216 ms |
21704 KB |
Output is correct |
17 |
Correct |
165 ms |
21576 KB |
Output is correct |
18 |
Correct |
155 ms |
21576 KB |
Output is correct |
19 |
Correct |
99 ms |
22592 KB |
Output is correct |
20 |
Correct |
101 ms |
22344 KB |
Output is correct |
21 |
Correct |
137 ms |
21064 KB |
Output is correct |
22 |
Correct |
127 ms |
20236 KB |
Output is correct |
23 |
Correct |
116 ms |
20040 KB |
Output is correct |
24 |
Correct |
141 ms |
19580 KB |
Output is correct |
25 |
Correct |
164 ms |
19464 KB |
Output is correct |
26 |
Correct |
221 ms |
19596 KB |
Output is correct |
27 |
Correct |
234 ms |
19580 KB |
Output is correct |
28 |
Correct |
214 ms |
19496 KB |
Output is correct |
29 |
Correct |
240 ms |
19592 KB |
Output is correct |
30 |
Correct |
191 ms |
19504 KB |
Output is correct |
31 |
Correct |
185 ms |
20064 KB |
Output is correct |
32 |
Correct |
206 ms |
20032 KB |
Output is correct |
33 |
Correct |
237 ms |
20548 KB |
Output is correct |
34 |
Correct |
170 ms |
22172 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
2640 KB |
Output is correct |
2 |
Correct |
3 ms |
2640 KB |
Output is correct |
3 |
Correct |
4 ms |
2896 KB |
Output is correct |
4 |
Execution timed out |
5092 ms |
17116 KB |
Time limit exceeded |
5 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 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 |
2640 KB |
Output is correct |
5 |
Correct |
3 ms |
2892 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 |
6 ms |
2896 KB |
Output is correct |
10 |
Correct |
4 ms |
2916 KB |
Output is correct |
11 |
Correct |
4 ms |
2896 KB |
Output is correct |
12 |
Correct |
3 ms |
2896 KB |
Output is correct |
13 |
Correct |
4 ms |
2896 KB |
Output is correct |
14 |
Correct |
4 ms |
2896 KB |
Output is correct |
15 |
Correct |
3 ms |
2896 KB |
Output is correct |
16 |
Correct |
4 ms |
2896 KB |
Output is correct |
17 |
Correct |
5 ms |
2920 KB |
Output is correct |
18 |
Correct |
5 ms |
2896 KB |
Output is correct |
19 |
Correct |
5 ms |
2896 KB |
Output is correct |
20 |
Correct |
4 ms |
2896 KB |
Output is correct |
21 |
Correct |
5 ms |
2896 KB |
Output is correct |
22 |
Correct |
5 ms |
2896 KB |
Output is correct |
23 |
Correct |
4 ms |
2896 KB |
Output is correct |
24 |
Correct |
6 ms |
2800 KB |
Output is correct |
25 |
Correct |
4 ms |
2896 KB |
Output is correct |
26 |
Correct |
6 ms |
2908 KB |
Output is correct |
27 |
Correct |
3 ms |
2640 KB |
Output is correct |
28 |
Correct |
4 ms |
2640 KB |
Output is correct |
29 |
Correct |
4 ms |
2896 KB |
Output is correct |
30 |
Correct |
24 ms |
3164 KB |
Output is correct |
31 |
Correct |
20 ms |
3172 KB |
Output is correct |
32 |
Correct |
32 ms |
3272 KB |
Output is correct |
33 |
Correct |
37 ms |
3152 KB |
Output is correct |
34 |
Correct |
31 ms |
3276 KB |
Output is correct |
35 |
Correct |
9 ms |
3132 KB |
Output is correct |
36 |
Correct |
8 ms |
3176 KB |
Output is correct |
37 |
Correct |
9 ms |
3152 KB |
Output is correct |
38 |
Correct |
18 ms |
3152 KB |
Output is correct |
39 |
Correct |
18 ms |
3328 KB |
Output is correct |
40 |
Correct |
19 ms |
3376 KB |
Output is correct |
41 |
Correct |
6 ms |
3388 KB |
Output is correct |
42 |
Correct |
7 ms |
3152 KB |
Output is correct |
43 |
Correct |
7 ms |
3152 KB |
Output is correct |
44 |
Correct |
26 ms |
3320 KB |
Output is correct |
45 |
Correct |
22 ms |
3340 KB |
Output is correct |
46 |
Correct |
18 ms |
3152 KB |
Output is correct |
47 |
Correct |
7 ms |
3408 KB |
Output is correct |
48 |
Correct |
5 ms |
3152 KB |
Output is correct |
49 |
Correct |
8 ms |
3160 KB |
Output is correct |
50 |
Correct |
27 ms |
3152 KB |
Output is correct |
51 |
Correct |
31 ms |
3064 KB |
Output is correct |
52 |
Correct |
36 ms |
3280 KB |
Output is correct |
53 |
Correct |
26 ms |
3152 KB |
Output is correct |
54 |
Correct |
29 ms |
3152 KB |
Output is correct |
55 |
Correct |
33 ms |
3408 KB |
Output is correct |
56 |
Correct |
6 ms |
3068 KB |
Output is correct |
57 |
Correct |
5 ms |
3152 KB |
Output is correct |
58 |
Correct |
5 ms |
3152 KB |
Output is correct |
59 |
Correct |
4 ms |
2808 KB |
Output is correct |
60 |
Correct |
4 ms |
2640 KB |
Output is correct |
61 |
Correct |
5 ms |
2896 KB |
Output is correct |
62 |
Execution timed out |
5055 ms |
19152 KB |
Time limit exceeded |
63 |
Halted |
0 ms |
0 KB |
- |