#include <bits/stdc++.h>
#define tt cin.tie(0), cout.tie(0), ios_base::sync_with_stdio(0)
#define fo freopen((NAME+".INP").c_str(), "r", stdin), freopen((NAME+".OUT").c_str(), "w", stdout)
#define ll long long
#define ull unsigned long long
#define i128 __int128
#define db long double
#define sz(a) ((int)(a).size())
#define pb emplace_back
#define pf emplace_front
#define pob pop_back
#define pof pop_front
#define lb lower_bound
#define ub upper_bound
#define fi first
#define se second
#define ins emplace
#define mp make_pair
using namespace std;
const int MOD = 1e9+7, MAXN = 1e5+5, lim = 18;
const string NAME = "";
inline pair<int,int> combine(const pair<int,int>& a, const pair<int,int>& b){
return {min(a.fi,b.fi),max(a.se,b.se)};
}
struct SegmentTree{
pair<int,int> sgt[MAXN<<2];
void build(int id, int l, int r, pair<int,int> p[]){
if(l==r) return sgt[id]=p[l], void();
int mid=(l+r)>>1;
build(id<<1,l,mid,p);
build(id<<1|1,mid+1,r,p);
sgt[id]=combine(sgt[id<<1],sgt[id<<1|1]);
}
pair<int,int> get(int id, int l, int r, int u, int v){
if(v<l||r<u) return {1e9,0};
if(u<=l&&r<=v) return sgt[id];
int mid=(l+r)>>1;
return combine(get(id<<1,l,mid,u,v),get(id<<1|1,mid+1,r,u,v));
}
}ST[20];
int n,k,m,q;
pair<int,int> st[20][MAXN];
vector<int> upd[MAXN],upd2[MAXN];
void init(){
multiset<int> ms;
for(int i = 1; i<=n; ++i){
for(int& j : upd[i]){
if(j>0) ms.ins(j);
else ms.erase(ms.find(-j));
}
st[0][i].se=(ms.empty() ? i : *ms.rbegin());
}
ms.clear();
for(int i = n; i>0; --i){
for(int& j : upd2[i]){
if(j>0) ms.ins(j);
else ms.erase(ms.find(-j));
}
st[0][i].fi=(ms.empty() ? i : *ms.begin());
}
ST[0].build(1,1,n,st[0]);
}
int main()
{
tt;
if(fopen((NAME + ".INP").c_str(), "r")) fo;
cin >> n >> k >> m;
for(int i = 1; i<=m; ++i){
int x,y;
cin >> x >> y;
if(x<y) upd[x].pb(y), upd[min(x+k,y)].pb(-y);
else upd2[x].pb(y), upd2[max(x-k,y)].pb(-y);
}
init();
for(int i = 1; i<=lim; ++i){
for(int j = 1; j<=n; ++j)
st[i][j]=ST[i-1].get(1,1,n,st[i-1][j].fi,st[i-1][j].se);
ST[i].build(1,1,n,st[i]);
}
cin >> q;
while(q--){
int x,y,rs=0;
cin >> x >> y;
int l=x,r=x;
if(x<y){
if(ST[lim].get(1,1,n,l,r).se>=y){
for(int i = lim; i>=0; --i){
pair<int,int> p = ST[i].get(1,1,n,l,r);
if(p.se<y) rs+=1<<i, l=p.fi, r=p.se;
}
}else rs=-2;
}else{
if(ST[lim].get(1,1,n,l,r).fi<=y){
for(int i = lim; i>=0; --i){
pair<int,int> p = ST[i].get(1,1,n,l,r);
if(p.fi>y) rs+=1<<i, l=p.fi, r=p.se;
}
}else rs=-2;
}
cout << rs+1 << "\n";
}
}
Compilation message (stderr)
Main.cpp: In function 'int main()':
Main.cpp:3:19: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
3 | #define fo freopen((NAME+".INP").c_str(), "r", stdin), freopen((NAME+".OUT").c_str(), "w", stdout)
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Main.cpp:66:45: note: in expansion of macro 'fo'
66 | if(fopen((NAME + ".INP").c_str(), "r")) fo;
| ^~
Main.cpp:3:63: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
3 | #define fo freopen((NAME+".INP").c_str(), "r", stdin), freopen((NAME+".OUT").c_str(), "w", stdout)
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Main.cpp:66:45: note: in expansion of macro 'fo'
66 | if(fopen((NAME + ".INP").c_str(), "r")) fo;
| ^~
# | 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... |