#include <bits/stdc++.h>
#define ll long long
#define str string
#define ins insert
#define ld long double
#define pb push_back
#define pf push_front
#define pof pop_front()
#define pob pop_back()
#define lb lower_bound
#define ub upper_bound
#define endl "\n"
#define fr first
#define sc second
#define all(x) x.begin(),x.end()
#define rall(x) x.rbegin(),x.rend()
#define sz size()
#define bc back()
#define ar array
#define vll vector<ll>
using namespace std;
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
template <class _T>
bool chmin(_T &x, const _T &y){
if(x>y){
x=y;
return true;
}
return false;
}
template <class _T>
bool chmax(_T &x, const _T &y){
bool flag=false;
if (x<y){
x=y;flag|=true;
}
return flag;
}
#define ordered_set tree<ll, null_type,less_equal<ll>, rb_tree_tag,tree_order_statistics_node_update>
void fre(string s){freopen((s+".in").c_str(),"r",stdin);freopen((s+".out").c_str(),"w",stdout);}
void start(){
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
}
const ll inf=2e18+7;
const ll mod=1e9+7;
const ll N=1e5+7;
const ld eps=1e-9;
vector<vll> g(N);
vector<pair<ll,ll>> t(N*4,{inf,0});
ll val[N];
void build(ll v,ll tl,ll tr){
if(tl==tr){
t[v]={val[tl],val[tl]};
return;
}
ll tm=(tl+tr)/2;
build(v*2,tl,tm);
build(v*2+1,tm+1,tr);
t[v]={min(t[v*2].fr,t[v*2+1].fr),max(t[v*2].sc,t[v*2+1].sc)};
}
pair<ll,ll> get(ll v,ll tl,ll tr,ll l,ll r){
if(tl>r || tr<l)return {inf,0ll};
if(l<=tl && tr<=r) return t[v];
ll tm=(tl+tr)/2;
pair<ll,ll> a=get(v*2,tl,tm,l,r);
pair<ll,ll> b=get(v*2+1,tm+1,tr,l,r);
return {min(a.fr,b.fr),max(a.sc,b.sc)};
}
bool f[2005];
ll go(ll v,ll p){
ll sum=0;
for(auto i : g[v]){
if(i==p)continue;
sum+=go(i,v);
}
if(f[v] || sum) return sum+1;
return sum;
}
ll d[N],p[N][20];
ll tin[N],tout[N],tiktak;
void dfs(ll v){
tin[v]=++tiktak;
for(ll i=1;i<18;i++){
p[v][i]=p[p[v][i-1]][i-1];
}
for(auto i :g[v]){
if(i==p[v][0])continue;
p[i][0]=v;
d[i]=d[v]+1;
dfs(i);
}
tout[v]=++tiktak;
}
ll lca(ll a,ll b){
ll i;
if(d[a]<d[b])swap(a,b);
for(i=17;i>=0;i--){
if(d[a]-d[b]>=(1ll<<i) && d[a]-d[p[a][i]]==(1ll<<i)){
a=p[a][i];
}
}
if(a==b)return a;
for(i=17;i>=0;i--){
if(p[a][i]!=p[b][i]){
a=p[a][i];
b=p[b][i];
}
}
return p[a][0];
}
void solve(){
ll i,j;
ll n,m,q;
ll a,b;
cin>>n>>m>>q;
bool line=true;
for(i=1;i<n;i++){
cin>>a>>b;
if(a>b)swap(a,b);
if(b-a!=1)line=false;
g[a].pb(b);
g[b].pb(a);
}
vector<ll> s(m+1);
for(i=1;i<=m;i++)cin>>s[i];
if(n<=2000 && m<=2000 && q<=2000){
for(i=1;i<=q;i++){
memset(f,false,sizeof(f));
cin>>a>>b;
for(j=a;j<=b;j++)f[s[j]]=true;
cout<<go(s[a],0)<<endl;
}
return;
}
if(line){
for(i=1;i<=m;i++)val[i]=s[i];
build(1,1,m);
for(i=0;i<q;i++){
cin>>a>>b;
pair<ll,ll> x=get(1,1,m,a,b);
cout<<x.sc-x.fr+1<<endl;
}
return;
}
dfs(1);
for(i=1;i<=q;i++){
cin>>a>>b;
vector<pair<ll,ll>> v;
map<ll,ll> mp;
for(j=a;j<=b;j++){
if(!mp[s[j]]) v.pb({tin[s[j]],s[j]});
mp[s[j]]++;
}
if(v.sz==1){
cout<<1<<endl;
continue;
}
sort(all(v));
ll lc=lca(v[0].sc,v[1].sc),sum=d[v[0].sc]-d[lc]+d[v[1].sc]-d[lc]+1;
for(j=2;j<(ll)v.sz;j++){
a=v[j-1].sc;
b=v[j].sc;
ll cur=lca(a,b);
if(cur==a){
sum+=d[b]-d[a];
continue;
}
ll nlc=lca(lc,b);
if(nlc==lc){
sum+=d[b]-d[cur];
}else{
sum+=d[b]-d[nlc]+d[lc]-d[nlc];
lc=nlc;
}
}
cout<<sum<<endl;
}
}
signed main(){
start();
ll t=1;
//cin>>t;
while(t--) solve();
return 0;
}
/*
1
6
5 4 3 2 1 0
*/
Compilation message
tourism.cpp: In function 'void fre(std::string)':
tourism.cpp:42:27: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
42 | void fre(string s){freopen((s+".in").c_str(),"r",stdin);freopen((s+".out").c_str(),"w",stdout);}
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~
tourism.cpp:42:64: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
42 | void fre(string s){freopen((s+".in").c_str(),"r",stdin);freopen((s+".out").c_str(),"w",stdout);}
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
11100 KB |
Output is correct |
2 |
Correct |
3 ms |
11100 KB |
Output is correct |
3 |
Correct |
3 ms |
11096 KB |
Output is correct |
4 |
Correct |
4 ms |
11100 KB |
Output is correct |
5 |
Correct |
4 ms |
11100 KB |
Output is correct |
6 |
Correct |
4 ms |
11156 KB |
Output is correct |
7 |
Correct |
3 ms |
11148 KB |
Output is correct |
8 |
Correct |
4 ms |
11152 KB |
Output is correct |
9 |
Correct |
4 ms |
11096 KB |
Output is correct |
10 |
Correct |
4 ms |
11100 KB |
Output is correct |
11 |
Correct |
4 ms |
11100 KB |
Output is correct |
12 |
Correct |
4 ms |
11100 KB |
Output is correct |
13 |
Correct |
4 ms |
11096 KB |
Output is correct |
14 |
Correct |
4 ms |
11012 KB |
Output is correct |
15 |
Correct |
4 ms |
11100 KB |
Output is correct |
16 |
Correct |
4 ms |
11056 KB |
Output is correct |
17 |
Correct |
3 ms |
11100 KB |
Output is correct |
18 |
Correct |
4 ms |
11100 KB |
Output is correct |
19 |
Correct |
4 ms |
11100 KB |
Output is correct |
20 |
Correct |
4 ms |
11100 KB |
Output is correct |
21 |
Correct |
3 ms |
11100 KB |
Output is correct |
22 |
Correct |
4 ms |
11100 KB |
Output is correct |
23 |
Correct |
4 ms |
11100 KB |
Output is correct |
24 |
Correct |
4 ms |
11100 KB |
Output is correct |
25 |
Correct |
4 ms |
11100 KB |
Output is correct |
26 |
Correct |
3 ms |
11100 KB |
Output is correct |
27 |
Correct |
3 ms |
11100 KB |
Output is correct |
28 |
Correct |
4 ms |
11100 KB |
Output is correct |
29 |
Correct |
5 ms |
11100 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
11100 KB |
Output is correct |
2 |
Correct |
3 ms |
11100 KB |
Output is correct |
3 |
Correct |
3 ms |
11096 KB |
Output is correct |
4 |
Correct |
4 ms |
11100 KB |
Output is correct |
5 |
Correct |
4 ms |
11100 KB |
Output is correct |
6 |
Correct |
4 ms |
11156 KB |
Output is correct |
7 |
Correct |
3 ms |
11148 KB |
Output is correct |
8 |
Correct |
4 ms |
11152 KB |
Output is correct |
9 |
Correct |
4 ms |
11096 KB |
Output is correct |
10 |
Correct |
4 ms |
11100 KB |
Output is correct |
11 |
Correct |
4 ms |
11100 KB |
Output is correct |
12 |
Correct |
4 ms |
11100 KB |
Output is correct |
13 |
Correct |
4 ms |
11096 KB |
Output is correct |
14 |
Correct |
4 ms |
11012 KB |
Output is correct |
15 |
Correct |
4 ms |
11100 KB |
Output is correct |
16 |
Correct |
4 ms |
11056 KB |
Output is correct |
17 |
Correct |
3 ms |
11100 KB |
Output is correct |
18 |
Correct |
4 ms |
11100 KB |
Output is correct |
19 |
Correct |
4 ms |
11100 KB |
Output is correct |
20 |
Correct |
4 ms |
11100 KB |
Output is correct |
21 |
Correct |
3 ms |
11100 KB |
Output is correct |
22 |
Correct |
4 ms |
11100 KB |
Output is correct |
23 |
Correct |
4 ms |
11100 KB |
Output is correct |
24 |
Correct |
4 ms |
11100 KB |
Output is correct |
25 |
Correct |
4 ms |
11100 KB |
Output is correct |
26 |
Correct |
3 ms |
11100 KB |
Output is correct |
27 |
Correct |
3 ms |
11100 KB |
Output is correct |
28 |
Correct |
4 ms |
11100 KB |
Output is correct |
29 |
Correct |
5 ms |
11100 KB |
Output is correct |
30 |
Correct |
28 ms |
11100 KB |
Output is correct |
31 |
Correct |
37 ms |
11212 KB |
Output is correct |
32 |
Correct |
57 ms |
11420 KB |
Output is correct |
33 |
Correct |
57 ms |
11216 KB |
Output is correct |
34 |
Correct |
57 ms |
11096 KB |
Output is correct |
35 |
Correct |
32 ms |
11360 KB |
Output is correct |
36 |
Correct |
33 ms |
11100 KB |
Output is correct |
37 |
Correct |
29 ms |
11240 KB |
Output is correct |
38 |
Correct |
50 ms |
11100 KB |
Output is correct |
39 |
Correct |
48 ms |
11096 KB |
Output is correct |
40 |
Correct |
45 ms |
11100 KB |
Output is correct |
41 |
Correct |
45 ms |
11308 KB |
Output is correct |
42 |
Correct |
51 ms |
11300 KB |
Output is correct |
43 |
Correct |
45 ms |
11096 KB |
Output is correct |
44 |
Correct |
44 ms |
11096 KB |
Output is correct |
45 |
Correct |
45 ms |
11100 KB |
Output is correct |
46 |
Correct |
45 ms |
11100 KB |
Output is correct |
47 |
Correct |
39 ms |
11244 KB |
Output is correct |
48 |
Correct |
40 ms |
11240 KB |
Output is correct |
49 |
Correct |
40 ms |
11100 KB |
Output is correct |
50 |
Correct |
25 ms |
11100 KB |
Output is correct |
51 |
Correct |
30 ms |
11240 KB |
Output is correct |
52 |
Correct |
27 ms |
11244 KB |
Output is correct |
53 |
Correct |
27 ms |
11236 KB |
Output is correct |
54 |
Correct |
26 ms |
11236 KB |
Output is correct |
55 |
Correct |
26 ms |
11096 KB |
Output is correct |
56 |
Correct |
6 ms |
11296 KB |
Output is correct |
57 |
Correct |
27 ms |
11224 KB |
Output is correct |
58 |
Correct |
27 ms |
11236 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
11100 KB |
Output is correct |
2 |
Correct |
3 ms |
11100 KB |
Output is correct |
3 |
Correct |
4 ms |
11100 KB |
Output is correct |
4 |
Correct |
56 ms |
13908 KB |
Output is correct |
5 |
Correct |
50 ms |
14416 KB |
Output is correct |
6 |
Correct |
52 ms |
14912 KB |
Output is correct |
7 |
Correct |
73 ms |
15756 KB |
Output is correct |
8 |
Correct |
76 ms |
15692 KB |
Output is correct |
9 |
Correct |
74 ms |
15560 KB |
Output is correct |
10 |
Correct |
74 ms |
15772 KB |
Output is correct |
11 |
Correct |
74 ms |
15696 KB |
Output is correct |
12 |
Correct |
67 ms |
15696 KB |
Output is correct |
13 |
Correct |
77 ms |
15756 KB |
Output is correct |
14 |
Correct |
74 ms |
15696 KB |
Output is correct |
15 |
Correct |
34 ms |
14416 KB |
Output is correct |
16 |
Correct |
61 ms |
15184 KB |
Output is correct |
17 |
Correct |
53 ms |
12080 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
11100 KB |
Output is correct |
2 |
Correct |
48 ms |
22432 KB |
Output is correct |
3 |
Correct |
66 ms |
24864 KB |
Output is correct |
4 |
Correct |
77 ms |
25376 KB |
Output is correct |
5 |
Correct |
105 ms |
36552 KB |
Output is correct |
6 |
Correct |
112 ms |
34064 KB |
Output is correct |
7 |
Correct |
108 ms |
32416 KB |
Output is correct |
8 |
Correct |
115 ms |
31828 KB |
Output is correct |
9 |
Correct |
105 ms |
31576 KB |
Output is correct |
10 |
Correct |
104 ms |
31572 KB |
Output is correct |
11 |
Correct |
106 ms |
31616 KB |
Output is correct |
12 |
Correct |
112 ms |
31580 KB |
Output is correct |
13 |
Correct |
93 ms |
33084 KB |
Output is correct |
14 |
Correct |
91 ms |
32848 KB |
Output is correct |
15 |
Correct |
89 ms |
33540 KB |
Output is correct |
16 |
Correct |
101 ms |
36552 KB |
Output is correct |
17 |
Correct |
105 ms |
35824 KB |
Output is correct |
18 |
Correct |
115 ms |
35820 KB |
Output is correct |
19 |
Correct |
105 ms |
37064 KB |
Output is correct |
20 |
Correct |
100 ms |
36452 KB |
Output is correct |
21 |
Correct |
101 ms |
33928 KB |
Output is correct |
22 |
Correct |
123 ms |
33016 KB |
Output is correct |
23 |
Correct |
106 ms |
32572 KB |
Output is correct |
24 |
Correct |
111 ms |
32336 KB |
Output is correct |
25 |
Correct |
107 ms |
32080 KB |
Output is correct |
26 |
Correct |
137 ms |
32336 KB |
Output is correct |
27 |
Correct |
110 ms |
32172 KB |
Output is correct |
28 |
Correct |
121 ms |
32136 KB |
Output is correct |
29 |
Correct |
140 ms |
32156 KB |
Output is correct |
30 |
Correct |
113 ms |
32168 KB |
Output is correct |
31 |
Correct |
126 ms |
32424 KB |
Output is correct |
32 |
Correct |
106 ms |
32340 KB |
Output is correct |
33 |
Correct |
95 ms |
32344 KB |
Output is correct |
34 |
Correct |
81 ms |
33156 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
11096 KB |
Output is correct |
2 |
Correct |
3 ms |
11100 KB |
Output is correct |
3 |
Correct |
4 ms |
11096 KB |
Output is correct |
4 |
Execution timed out |
5056 ms |
26464 KB |
Time limit exceeded |
5 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
11100 KB |
Output is correct |
2 |
Correct |
3 ms |
11100 KB |
Output is correct |
3 |
Correct |
3 ms |
11096 KB |
Output is correct |
4 |
Correct |
4 ms |
11100 KB |
Output is correct |
5 |
Correct |
4 ms |
11100 KB |
Output is correct |
6 |
Correct |
4 ms |
11156 KB |
Output is correct |
7 |
Correct |
3 ms |
11148 KB |
Output is correct |
8 |
Correct |
4 ms |
11152 KB |
Output is correct |
9 |
Correct |
4 ms |
11096 KB |
Output is correct |
10 |
Correct |
4 ms |
11100 KB |
Output is correct |
11 |
Correct |
4 ms |
11100 KB |
Output is correct |
12 |
Correct |
4 ms |
11100 KB |
Output is correct |
13 |
Correct |
4 ms |
11096 KB |
Output is correct |
14 |
Correct |
4 ms |
11012 KB |
Output is correct |
15 |
Correct |
4 ms |
11100 KB |
Output is correct |
16 |
Correct |
4 ms |
11056 KB |
Output is correct |
17 |
Correct |
3 ms |
11100 KB |
Output is correct |
18 |
Correct |
4 ms |
11100 KB |
Output is correct |
19 |
Correct |
4 ms |
11100 KB |
Output is correct |
20 |
Correct |
4 ms |
11100 KB |
Output is correct |
21 |
Correct |
3 ms |
11100 KB |
Output is correct |
22 |
Correct |
4 ms |
11100 KB |
Output is correct |
23 |
Correct |
4 ms |
11100 KB |
Output is correct |
24 |
Correct |
4 ms |
11100 KB |
Output is correct |
25 |
Correct |
4 ms |
11100 KB |
Output is correct |
26 |
Correct |
3 ms |
11100 KB |
Output is correct |
27 |
Correct |
3 ms |
11100 KB |
Output is correct |
28 |
Correct |
4 ms |
11100 KB |
Output is correct |
29 |
Correct |
5 ms |
11100 KB |
Output is correct |
30 |
Correct |
28 ms |
11100 KB |
Output is correct |
31 |
Correct |
37 ms |
11212 KB |
Output is correct |
32 |
Correct |
57 ms |
11420 KB |
Output is correct |
33 |
Correct |
57 ms |
11216 KB |
Output is correct |
34 |
Correct |
57 ms |
11096 KB |
Output is correct |
35 |
Correct |
32 ms |
11360 KB |
Output is correct |
36 |
Correct |
33 ms |
11100 KB |
Output is correct |
37 |
Correct |
29 ms |
11240 KB |
Output is correct |
38 |
Correct |
50 ms |
11100 KB |
Output is correct |
39 |
Correct |
48 ms |
11096 KB |
Output is correct |
40 |
Correct |
45 ms |
11100 KB |
Output is correct |
41 |
Correct |
45 ms |
11308 KB |
Output is correct |
42 |
Correct |
51 ms |
11300 KB |
Output is correct |
43 |
Correct |
45 ms |
11096 KB |
Output is correct |
44 |
Correct |
44 ms |
11096 KB |
Output is correct |
45 |
Correct |
45 ms |
11100 KB |
Output is correct |
46 |
Correct |
45 ms |
11100 KB |
Output is correct |
47 |
Correct |
39 ms |
11244 KB |
Output is correct |
48 |
Correct |
40 ms |
11240 KB |
Output is correct |
49 |
Correct |
40 ms |
11100 KB |
Output is correct |
50 |
Correct |
25 ms |
11100 KB |
Output is correct |
51 |
Correct |
30 ms |
11240 KB |
Output is correct |
52 |
Correct |
27 ms |
11244 KB |
Output is correct |
53 |
Correct |
27 ms |
11236 KB |
Output is correct |
54 |
Correct |
26 ms |
11236 KB |
Output is correct |
55 |
Correct |
26 ms |
11096 KB |
Output is correct |
56 |
Correct |
6 ms |
11296 KB |
Output is correct |
57 |
Correct |
27 ms |
11224 KB |
Output is correct |
58 |
Correct |
27 ms |
11236 KB |
Output is correct |
59 |
Correct |
3 ms |
11100 KB |
Output is correct |
60 |
Correct |
3 ms |
11100 KB |
Output is correct |
61 |
Correct |
4 ms |
11100 KB |
Output is correct |
62 |
Correct |
56 ms |
13908 KB |
Output is correct |
63 |
Correct |
50 ms |
14416 KB |
Output is correct |
64 |
Correct |
52 ms |
14912 KB |
Output is correct |
65 |
Correct |
73 ms |
15756 KB |
Output is correct |
66 |
Correct |
76 ms |
15692 KB |
Output is correct |
67 |
Correct |
74 ms |
15560 KB |
Output is correct |
68 |
Correct |
74 ms |
15772 KB |
Output is correct |
69 |
Correct |
74 ms |
15696 KB |
Output is correct |
70 |
Correct |
67 ms |
15696 KB |
Output is correct |
71 |
Correct |
77 ms |
15756 KB |
Output is correct |
72 |
Correct |
74 ms |
15696 KB |
Output is correct |
73 |
Correct |
34 ms |
14416 KB |
Output is correct |
74 |
Correct |
61 ms |
15184 KB |
Output is correct |
75 |
Correct |
53 ms |
12080 KB |
Output is correct |
76 |
Correct |
3 ms |
11100 KB |
Output is correct |
77 |
Correct |
48 ms |
22432 KB |
Output is correct |
78 |
Correct |
66 ms |
24864 KB |
Output is correct |
79 |
Correct |
77 ms |
25376 KB |
Output is correct |
80 |
Correct |
105 ms |
36552 KB |
Output is correct |
81 |
Correct |
112 ms |
34064 KB |
Output is correct |
82 |
Correct |
108 ms |
32416 KB |
Output is correct |
83 |
Correct |
115 ms |
31828 KB |
Output is correct |
84 |
Correct |
105 ms |
31576 KB |
Output is correct |
85 |
Correct |
104 ms |
31572 KB |
Output is correct |
86 |
Correct |
106 ms |
31616 KB |
Output is correct |
87 |
Correct |
112 ms |
31580 KB |
Output is correct |
88 |
Correct |
93 ms |
33084 KB |
Output is correct |
89 |
Correct |
91 ms |
32848 KB |
Output is correct |
90 |
Correct |
89 ms |
33540 KB |
Output is correct |
91 |
Correct |
101 ms |
36552 KB |
Output is correct |
92 |
Correct |
105 ms |
35824 KB |
Output is correct |
93 |
Correct |
115 ms |
35820 KB |
Output is correct |
94 |
Correct |
105 ms |
37064 KB |
Output is correct |
95 |
Correct |
100 ms |
36452 KB |
Output is correct |
96 |
Correct |
101 ms |
33928 KB |
Output is correct |
97 |
Correct |
123 ms |
33016 KB |
Output is correct |
98 |
Correct |
106 ms |
32572 KB |
Output is correct |
99 |
Correct |
111 ms |
32336 KB |
Output is correct |
100 |
Correct |
107 ms |
32080 KB |
Output is correct |
101 |
Correct |
137 ms |
32336 KB |
Output is correct |
102 |
Correct |
110 ms |
32172 KB |
Output is correct |
103 |
Correct |
121 ms |
32136 KB |
Output is correct |
104 |
Correct |
140 ms |
32156 KB |
Output is correct |
105 |
Correct |
113 ms |
32168 KB |
Output is correct |
106 |
Correct |
126 ms |
32424 KB |
Output is correct |
107 |
Correct |
106 ms |
32340 KB |
Output is correct |
108 |
Correct |
95 ms |
32344 KB |
Output is correct |
109 |
Correct |
81 ms |
33156 KB |
Output is correct |
110 |
Correct |
3 ms |
11096 KB |
Output is correct |
111 |
Correct |
3 ms |
11100 KB |
Output is correct |
112 |
Correct |
4 ms |
11096 KB |
Output is correct |
113 |
Execution timed out |
5056 ms |
26464 KB |
Time limit exceeded |
114 |
Halted |
0 ms |
0 KB |
- |