#include <bits/stdc++.h>
using namespace std;
#define int long long
#define ld long double
#define mp make_pair
#define pb push_back
#define pii pair<int,int>
#define fi first
#define se second
#define all(x) x.begin(), x.end()
#define ar array
/*tourism
subtask 1&2: make root at l[i] then just find number of nodes passed through
subtask 3: since it is a chain, we just have to find max(c[l...r])-min(c[l...r])+1
*/
const int inf=1e18;
struct yl{
int l,r,val;
};
struct seg_mx{
vector<yl> tr;
vector<int> C;
seg_mx(){}
seg_mx(int n,vector<int> &c){
int S=4*n+600; C=c;
tr.resize(S);
build(1,1,n);
}
void build(int p,int l,int r){
tr[p].l=l; tr[p].r=r;
if(l==r){ tr[p].val=C[l]; return;}
int mid=(l+r)>>1;
build(p<<1,l,mid); build(p<<1|1,mid+1,r);
tr[p].val=max(tr[p<<1].val,tr[p<<1|1].val);
return;
}
int query(int p,int l,int r){
if(tr[p].l>r || tr[p].r<l) return -inf;
if(l<=tr[p].l && tr[p].r<=r) return tr[p].val;
return max(query(p<<1,l,r),query(p<<1|1,l,r));
}
};
struct seg_mn{
vector<yl> tr;
vector<int> C;
seg_mn(){}
seg_mn(int n,vector<int> &c){
int S=4*n+600; C=c;
tr.resize(S);
build(1,1,n);
}
void build(int p,int l,int r){
tr[p].l=l; tr[p].r=r;
if(l==r){ tr[p].val=C[l]; return;}
int mid=(l+r)>>1;
build(p<<1,l,mid); build(p<<1|1,mid+1,r);
tr[p].val=min(tr[p<<1].val,tr[p<<1|1].val);
return;
}
int query(int p,int l,int r){
if(tr[p].l>r || tr[p].r<l) return inf;
if(l<=tr[p].l && tr[p].r<=r) return tr[p].val;
return min(query(p<<1,l,r),query(p<<1|1,l,r));
}
};
void solve(){
int n,m,q; cin>>n>>m>>q;
int sub12=0;
if(n*q<=2e8) sub12=1;
vector<vector<int>> g(n+1);
for(int i=0;i<n-1;i++){
int u,v; cin>>u>>v;
g[u].pb(v); g[v].pb(u);
}
vector<int> c(m+1),d(n+1);
for(int i=1;i<=m;i++) cin>>c[i];
seg_mx mx(m,c);
seg_mn mn(m,c);
function<void(int,int)> dfs=[&](int u,int f){
for(auto&v:g[u]){
if(v==f) continue;
dfs(v,u);
d[u]+=d[v];
}
};
auto reset=[&](int l,int r){
for(int i=1;i<=n;i++) d[i]=0;
for(int i=l;i<=r;i++) d[c[i]]=1;
};
auto ans2=[&](int l,int r){
int rr=mx.query(1,l,r);
int ll=mn.query(1,l,r);
cout<<rr-ll+1<<'\n';
};
auto ans1=[&](int l,int r){
reset(l,r);
dfs(c[l],0);
int rs=0;
for(int i=1;i<=n;i++) if(d[i]>=1) rs++;
cout<<rs<<'\n';
};
while(q--){
int l,r; cin>>l>>r;
if(sub12) ans1(l,r);
else ans2(l,r);
}
}
signed main() {
ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
clock_t tStart = clock();
int local=0,multi=0,debug=1,tt=1;
if(local){
freopen("input.txt","r",stdin);
freopen("output.txt","w",stdout);
}
if(multi) cin>>tt;
for(int i=1;i<=tt;i++){
if(debug && multi && local) cout<<"样例 "<<i<<'\n';
solve();
}
fprintf(stderr, "\n>> Runtime: %.10fs\n", (double) (clock() - tStart) / CLOCKS_PER_SEC);
}
Compilation message
tourism.cpp: In function 'int main()':
tourism.cpp:117:13: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
117 | freopen("input.txt","r",stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~
tourism.cpp:118:13: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
118 | freopen("output.txt","w",stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
0 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
2 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
2 ms |
460 KB |
Output is correct |
16 |
Correct |
2 ms |
340 KB |
Output is correct |
17 |
Correct |
2 ms |
468 KB |
Output is correct |
18 |
Correct |
2 ms |
340 KB |
Output is correct |
19 |
Correct |
2 ms |
340 KB |
Output is correct |
20 |
Correct |
2 ms |
340 KB |
Output is correct |
21 |
Correct |
1 ms |
340 KB |
Output is correct |
22 |
Correct |
1 ms |
340 KB |
Output is correct |
23 |
Correct |
1 ms |
340 KB |
Output is correct |
24 |
Correct |
1 ms |
340 KB |
Output is correct |
25 |
Correct |
1 ms |
340 KB |
Output is correct |
26 |
Correct |
1 ms |
340 KB |
Output is correct |
27 |
Correct |
0 ms |
340 KB |
Output is correct |
28 |
Correct |
1 ms |
340 KB |
Output is correct |
29 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
0 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
2 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
2 ms |
460 KB |
Output is correct |
16 |
Correct |
2 ms |
340 KB |
Output is correct |
17 |
Correct |
2 ms |
468 KB |
Output is correct |
18 |
Correct |
2 ms |
340 KB |
Output is correct |
19 |
Correct |
2 ms |
340 KB |
Output is correct |
20 |
Correct |
2 ms |
340 KB |
Output is correct |
21 |
Correct |
1 ms |
340 KB |
Output is correct |
22 |
Correct |
1 ms |
340 KB |
Output is correct |
23 |
Correct |
1 ms |
340 KB |
Output is correct |
24 |
Correct |
1 ms |
340 KB |
Output is correct |
25 |
Correct |
1 ms |
340 KB |
Output is correct |
26 |
Correct |
1 ms |
340 KB |
Output is correct |
27 |
Correct |
0 ms |
340 KB |
Output is correct |
28 |
Correct |
1 ms |
340 KB |
Output is correct |
29 |
Correct |
1 ms |
340 KB |
Output is correct |
30 |
Correct |
21 ms |
768 KB |
Output is correct |
31 |
Correct |
27 ms |
724 KB |
Output is correct |
32 |
Correct |
42 ms |
928 KB |
Output is correct |
33 |
Correct |
43 ms |
852 KB |
Output is correct |
34 |
Correct |
43 ms |
924 KB |
Output is correct |
35 |
Correct |
43 ms |
920 KB |
Output is correct |
36 |
Correct |
44 ms |
928 KB |
Output is correct |
37 |
Correct |
43 ms |
928 KB |
Output is correct |
38 |
Correct |
56 ms |
1108 KB |
Output is correct |
39 |
Correct |
58 ms |
1108 KB |
Output is correct |
40 |
Correct |
54 ms |
1132 KB |
Output is correct |
41 |
Correct |
56 ms |
1108 KB |
Output is correct |
42 |
Correct |
66 ms |
1128 KB |
Output is correct |
43 |
Correct |
55 ms |
1124 KB |
Output is correct |
44 |
Correct |
54 ms |
1008 KB |
Output is correct |
45 |
Correct |
54 ms |
1000 KB |
Output is correct |
46 |
Correct |
57 ms |
1124 KB |
Output is correct |
47 |
Correct |
55 ms |
976 KB |
Output is correct |
48 |
Correct |
55 ms |
964 KB |
Output is correct |
49 |
Correct |
57 ms |
1008 KB |
Output is correct |
50 |
Correct |
32 ms |
852 KB |
Output is correct |
51 |
Correct |
32 ms |
940 KB |
Output is correct |
52 |
Correct |
32 ms |
928 KB |
Output is correct |
53 |
Correct |
33 ms |
940 KB |
Output is correct |
54 |
Correct |
32 ms |
940 KB |
Output is correct |
55 |
Correct |
37 ms |
928 KB |
Output is correct |
56 |
Correct |
2 ms |
724 KB |
Output is correct |
57 |
Correct |
41 ms |
484 KB |
Output is correct |
58 |
Correct |
41 ms |
924 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
724 KB |
Output is correct |
4 |
Correct |
97 ms |
22908 KB |
Output is correct |
5 |
Correct |
88 ms |
17704 KB |
Output is correct |
6 |
Correct |
107 ms |
23672 KB |
Output is correct |
7 |
Correct |
138 ms |
28264 KB |
Output is correct |
8 |
Correct |
138 ms |
28316 KB |
Output is correct |
9 |
Correct |
135 ms |
28296 KB |
Output is correct |
10 |
Correct |
149 ms |
28360 KB |
Output is correct |
11 |
Correct |
177 ms |
28336 KB |
Output is correct |
12 |
Correct |
95 ms |
28360 KB |
Output is correct |
13 |
Correct |
96 ms |
28284 KB |
Output is correct |
14 |
Correct |
96 ms |
28256 KB |
Output is correct |
15 |
Correct |
33 ms |
6728 KB |
Output is correct |
16 |
Correct |
99 ms |
27848 KB |
Output is correct |
17 |
Correct |
1539 ms |
21628 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
2701 ms |
15636 KB |
Output is correct |
3 |
Correct |
3131 ms |
23372 KB |
Output is correct |
4 |
Correct |
3427 ms |
19160 KB |
Output is correct |
5 |
Correct |
49 ms |
28368 KB |
Output is correct |
6 |
Correct |
54 ms |
28364 KB |
Output is correct |
7 |
Correct |
72 ms |
28360 KB |
Output is correct |
8 |
Correct |
150 ms |
28368 KB |
Output is correct |
9 |
Correct |
353 ms |
28364 KB |
Output is correct |
10 |
Correct |
1123 ms |
28364 KB |
Output is correct |
11 |
Correct |
3166 ms |
28380 KB |
Output is correct |
12 |
Incorrect |
49 ms |
28620 KB |
Output isn't correct |
13 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
2 ms |
724 KB |
Output is correct |
4 |
Incorrect |
141 ms |
24820 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
0 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
2 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
2 ms |
460 KB |
Output is correct |
16 |
Correct |
2 ms |
340 KB |
Output is correct |
17 |
Correct |
2 ms |
468 KB |
Output is correct |
18 |
Correct |
2 ms |
340 KB |
Output is correct |
19 |
Correct |
2 ms |
340 KB |
Output is correct |
20 |
Correct |
2 ms |
340 KB |
Output is correct |
21 |
Correct |
1 ms |
340 KB |
Output is correct |
22 |
Correct |
1 ms |
340 KB |
Output is correct |
23 |
Correct |
1 ms |
340 KB |
Output is correct |
24 |
Correct |
1 ms |
340 KB |
Output is correct |
25 |
Correct |
1 ms |
340 KB |
Output is correct |
26 |
Correct |
1 ms |
340 KB |
Output is correct |
27 |
Correct |
0 ms |
340 KB |
Output is correct |
28 |
Correct |
1 ms |
340 KB |
Output is correct |
29 |
Correct |
1 ms |
340 KB |
Output is correct |
30 |
Correct |
21 ms |
768 KB |
Output is correct |
31 |
Correct |
27 ms |
724 KB |
Output is correct |
32 |
Correct |
42 ms |
928 KB |
Output is correct |
33 |
Correct |
43 ms |
852 KB |
Output is correct |
34 |
Correct |
43 ms |
924 KB |
Output is correct |
35 |
Correct |
43 ms |
920 KB |
Output is correct |
36 |
Correct |
44 ms |
928 KB |
Output is correct |
37 |
Correct |
43 ms |
928 KB |
Output is correct |
38 |
Correct |
56 ms |
1108 KB |
Output is correct |
39 |
Correct |
58 ms |
1108 KB |
Output is correct |
40 |
Correct |
54 ms |
1132 KB |
Output is correct |
41 |
Correct |
56 ms |
1108 KB |
Output is correct |
42 |
Correct |
66 ms |
1128 KB |
Output is correct |
43 |
Correct |
55 ms |
1124 KB |
Output is correct |
44 |
Correct |
54 ms |
1008 KB |
Output is correct |
45 |
Correct |
54 ms |
1000 KB |
Output is correct |
46 |
Correct |
57 ms |
1124 KB |
Output is correct |
47 |
Correct |
55 ms |
976 KB |
Output is correct |
48 |
Correct |
55 ms |
964 KB |
Output is correct |
49 |
Correct |
57 ms |
1008 KB |
Output is correct |
50 |
Correct |
32 ms |
852 KB |
Output is correct |
51 |
Correct |
32 ms |
940 KB |
Output is correct |
52 |
Correct |
32 ms |
928 KB |
Output is correct |
53 |
Correct |
33 ms |
940 KB |
Output is correct |
54 |
Correct |
32 ms |
940 KB |
Output is correct |
55 |
Correct |
37 ms |
928 KB |
Output is correct |
56 |
Correct |
2 ms |
724 KB |
Output is correct |
57 |
Correct |
41 ms |
484 KB |
Output is correct |
58 |
Correct |
41 ms |
924 KB |
Output is correct |
59 |
Correct |
0 ms |
340 KB |
Output is correct |
60 |
Correct |
0 ms |
340 KB |
Output is correct |
61 |
Correct |
1 ms |
724 KB |
Output is correct |
62 |
Correct |
97 ms |
22908 KB |
Output is correct |
63 |
Correct |
88 ms |
17704 KB |
Output is correct |
64 |
Correct |
107 ms |
23672 KB |
Output is correct |
65 |
Correct |
138 ms |
28264 KB |
Output is correct |
66 |
Correct |
138 ms |
28316 KB |
Output is correct |
67 |
Correct |
135 ms |
28296 KB |
Output is correct |
68 |
Correct |
149 ms |
28360 KB |
Output is correct |
69 |
Correct |
177 ms |
28336 KB |
Output is correct |
70 |
Correct |
95 ms |
28360 KB |
Output is correct |
71 |
Correct |
96 ms |
28284 KB |
Output is correct |
72 |
Correct |
96 ms |
28256 KB |
Output is correct |
73 |
Correct |
33 ms |
6728 KB |
Output is correct |
74 |
Correct |
99 ms |
27848 KB |
Output is correct |
75 |
Correct |
1539 ms |
21628 KB |
Output is correct |
76 |
Correct |
0 ms |
340 KB |
Output is correct |
77 |
Correct |
2701 ms |
15636 KB |
Output is correct |
78 |
Correct |
3131 ms |
23372 KB |
Output is correct |
79 |
Correct |
3427 ms |
19160 KB |
Output is correct |
80 |
Correct |
49 ms |
28368 KB |
Output is correct |
81 |
Correct |
54 ms |
28364 KB |
Output is correct |
82 |
Correct |
72 ms |
28360 KB |
Output is correct |
83 |
Correct |
150 ms |
28368 KB |
Output is correct |
84 |
Correct |
353 ms |
28364 KB |
Output is correct |
85 |
Correct |
1123 ms |
28364 KB |
Output is correct |
86 |
Correct |
3166 ms |
28380 KB |
Output is correct |
87 |
Incorrect |
49 ms |
28620 KB |
Output isn't correct |
88 |
Halted |
0 ms |
0 KB |
- |