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>
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
#define vi vector<int>
#define ve vector
#define ll long long
#define vl vector<ll>
#define vll vector<pair<ll,ll>>
#define onbit __builtin_popcount
#define ii pair<int,int>
#define vvi vector<vi>
#define vii vector<ii>
#define gii greater<ii>
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define INF 1e18
#define eps 1e-7
#define eps1 1e-2
#define optimise ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
#define MAX_A 1e5+5
using namespace std;
using namespace __gnu_pbds;
template <class T>
using Tree = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
const ll MOD = 1e9+7;
const int nax = 1e5+5;
const int MAX_VAL = 1e6+1;
double PI=3.14159265359;
int arx[8]={1,0,0,-1,-1,-1, 1, 1};
int ary[8]={0,1,-1, 0, 1,-1,-1, 1};
void setIO(string s) {
freopen((s + ".in").c_str(), "r", stdin);
freopen((s + ".out").c_str(), "w", stdout);
}
int lg=18;
ii segtree[nax*4][18];
void update(int pos,int l,int r,int idx,ii value,int level){
if(l==r){
segtree[pos][level]=value;
return;
}
int mid=(r+l)/2;
if(idx<=mid) update(pos*2+1,l,mid,idx,value,level);
else update(pos*2+2,mid+1,r,idx,value,level);
segtree[pos][level].fi=max(segtree[pos*2+1][level].fi,segtree[pos*2+2][level].fi);
segtree[pos][level].se=min(segtree[pos*2+1][level].se,segtree[pos*2+2][level].se);
//cout <<l<<" "<<r<<" "<<segtree[pos][level].fi<<" "<<segtree[pos][level].se<<endl;
return;
}
ii query(int pos,int l,int r,int left,int right,int level){
if(l>r||l>right||r<left) return {-1e9,1e9};
if(l>=left&&r<=right) return segtree[pos][level];
int mid=(r+l)/2;
ii a=query(pos*2+1,l,mid,left,right,level);
ii b=query(pos*2+2,mid+1,r,left,right,level);
//cout <<"sd"<<" "<<l<<" "<<r<<" "<<max(a.fi,b.fi)<<" "<<min(a.se,b.se)<<endl;
return {max(a.fi,b.fi),min(a.se,b.se)};
}
int main(){
optimise;
//setIO("dec");
int n,k;
cin>>n>>k;
int m;
cin>>m;
int a[n+1];
int b[n+1];
for (int i = 0; i <= n; ++i)
{
a[i]=0;
b[i]=1e9;
}
for (int i = 0; i < m; ++i)
{
int x,y;
cin>>x>>y;
if(x<y) a[x]=max(a[x],y);
else b[x]=min(b[x],y);
}
multiset<int> st;
int mx[n+1];
int mn[n+1];
for (int i = 0; i <= n; ++i)
{
if(i>=k){
st.erase(st.find(a[i-k]));
}
st.insert(a[i]);
mx[i]=max(i,(*--st.end()));
}
st.clear();
for (int i = n; i >= 0; --i)
{
if(i<=n-k){
st.erase(st.find(b[i+k]));
}
st.insert(b[i]);
//cout <<*st.begin()<<endl;
mn[i]=min(i,(*st.begin()));
}
for (int i = 0; i <= nax*4; ++i)
{
for (int j = 0; j < 18; ++j)
{
segtree[i][j]={-1e9,1e9};
}
}
for (int i = 0; i <= n; ++i)
{
update(0,0,n,i,{mx[i],mn[i]},0);
//cout <<mx[i]<<" "<<mn[i]<<endl;
}
for (int i = 1; i < lg; ++i)
{
for (int j = 0; j <= n; ++j)
{
ii a=query(0,0,n,mn[j],mx[j],i-1);
//cout <<mn[j]<<" "<<mx[j]<<" "<<a.fi<<" "<<a.se<<endl;
update(0,0,n,j,a,i);
mx[j]=a.fi;
mn[j]=a.se;
//cout <<mn[j]<<" "<<mx[j]<<endl;
}
}
int q;
cin>>q;
while(q--){
int s,t;
cin>>s>>t;
if(t<mn[s]||t>mx[s]) {
cout <<-1<<'\n';
continue;
}
int ans=0;
ii cur={s,s};
for (int i = 16; i >=0 ; --i)
{
ii nxt=query(0,0,n,cur.se,cur.fi,i);
if(t>nxt.fi||t<nxt.se){
cur=nxt;
ans+=(1<<i);
}
}
cout <<ans+1<<'\n';
}
}
Compilation message (stderr)
Main.cpp: In function 'void setIO(std::string)':
Main.cpp:35:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
35 | freopen((s + ".in").c_str(), "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Main.cpp:36:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
36 | freopen((s + ".out").c_str(), "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... |