This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#define int long long
#define pb push_back
#define ff first
#define ss second
#define all(v) v.begin(), v.end()
#define rall(v) v.rbegin(), v.rend()
#define ordered_set tree<ll, null_type, less<ll>, rb_tree_tag, tree_order_statistics_node_update>
#define ordered_multiset tree<type1, null_type, less_equal<type1>, rb_tree_tag, tree_order_statistics_node_update>;
using namespace std;
using namespace __gnu_pbds;
const int mod = 1e9+7;
const double PI = acos(-1.0);
const double epsilon = 1e-6;
const int N = 1e5+5;
vector<int> g[N];
int ans = 0;
int c[N],t2[N*4],t[N*4],dis[N],up[20][N],tin[N],tout[N];
bool vis[N];
int par[N];
vector<int> pos[N];
int timer;
void build(int v, int l, int r){
if(l == r){
t[v] = c[l];
t2[v] = c[l];
return;
}
int mid = (l + r) >> 1;
build(v * 2, l, mid);
build(v * 2 + 1, mid + 1, r);
t[v] = min(t[v * 2], t[v * 2 + 1]);
t2[v] = max(t2[v * 2], t2[v * 2 + 1]);
}
int getmn(int v, int tl, int tr, int l, int r) {
if (l > tr || tl > r) return 1e18;
if (l <= tl && tr <= r)return t[v];
int tm = (tl + tr) / 2;
return min(getmn(v * 2, tl, tm, l, r),getmn(v * 2 + 1, tm + 1, tr, l, r));
}
int getmx(int v, int tl, int tr, int l, int r) {
if (l > tr || tl > r) return 0;
if (l <= tl && tr <= r)return t2[v];
int tm = (tl + tr) / 2;
return max(getmx(v * 2, tl, tm, l, r),getmx(v * 2 + 1, tm + 1, tr, l, r));
}
void dfs(int v,int p,int l,int r){
par[v] = p;
for(int i : pos[v]){
if(l <= i && i <= r){
int h = v;
while(h != 0 && !vis[h]){
vis[h] = 1;
h = par[h];
}
break;
}
}
for(int to : g[v]){
if(to != p) dfs(to,v,l,r);
}
}
void solve(){
int n,m,q;
cin >> n >> m >> q;
bool ok = 1;
for(int i = 1; i < n; i++){
int u,v; cin >> u >> v;
g[u].pb(v); g[v].pb(u);
if(!(u == i && v == i+1)) ok = 0;
}
for(int i = 1; i <= m; i++){
cin >> c[i];
}
if(ok){
build(1,1,m);
while(q--){
int l,r; cin >> l >> r;
cout << getmx(1,1,m,l,r) - getmn(1,1,m,l,r) + 1 << '\n';
}
return;
}
for(int i = 1; i <= m; i++) pos[c[i]].pb(i);
while(q--){
int l,r; cin >> l >> r;
dfs(c[l],0,l,r);
int cnt = 0;
for(int i = 1; i <= n; i++) cnt += vis[i],vis[i] = 0;
cout << cnt << '\n';
}
}
main(){
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int tt = 1;
//cin >> tt;
while (tt--) {
solve();
}
}
Compilation message (stderr)
tourism.cpp:103:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
103 | main(){
| ^~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |