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>
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;
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);
auto ans=[&](int l,int r){
int rr=mx.query(1,l,r);
int ll=mn.query(1,l,r);
cout<<rr-ll+1<<'\n';
};
while(q--){
int l,r; cin>>l>>r;
ans(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 (stderr)
tourism.cpp: In function 'int main()':
tourism.cpp:96:13: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
96 | freopen("input.txt","r",stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~
tourism.cpp:97:13: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
97 | freopen("output.txt","w",stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
# | 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... |