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>
#define ios ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0)
#define file(s) if (fopen(s".in", "r")) freopen(s".in", "r", stdin), freopen(s".out", "w", stdout)
#define all(a) a.begin() , a.end()
#define F first
#define S second
using namespace std;
using ll = long long;
const int N = 1e5+5 , inf = 2e9 + 7;
const ll INF = 1e18 , mod = 1e9+7 , P = 6547;
int L[N][20], R[N][20];
struct DO{
int mn, mx;
}t[20][4*N];
int n , k;
void build(int j, int u = 1, int ul = 1, int ur = n) {
if(ul == ur) {
t[j][u].mx = L[ul][j];
t[j][u].mn = R[ul][j];
return;
}
int um = ul+ur >> 1;
build(j, u<<1 ,ul, um);
build(j, u<<1|1 ,um+1, ur);
t[j][u].mn = min(t[j][u<<1].mn, t[j][u<<1|1].mn);
t[j][u].mx = max(t[j][u<<1].mx, t[j][u<<1|1].mx);
}
DO get(int j, int l, int r, int u = 1, int ul = 1, int ur = n) {
if(l <= ul && ur <= r) {
return t[j][u];
}
if(r < ul || ur < l) {
return {inf, -inf};
}
int um = ul+ur >> 1;
DO lef = get(j, l , r, u<<1 , ul, um);
DO rig = get(j, l , r, u<<1|1 , um+1, ur);
return {min(lef.mn,rig.mn) , max(lef.mx,rig.mx)};
}
vector<int> vecl[N];
vector<int> vecr[N];
void solve(int tc) {
cin >> n >> k;
int m;
cin >> m;
int a[m+1], b[m+1];
for(int i = 1; i <= m; i ++) {
cin >> a[i] >> b[i];
if(a[i] < b[i]) {
vecl[a[i]].push_back(b[i]);
vecl[min(a[i]+k, b[i])].push_back(-b[i]);
}
else {
vecr[a[i]].push_back(b[i]);
vecr[max(a[i]-k, b[i])].push_back(-b[i]);
}
}
multiset<int> st;
for(int i = 1; i <= n; i ++) {
for(auto x: vecl[i]) {
if(x > 0) {
st.insert(x);
}
else {
st.erase(st.find(-x));
}
}
st.insert(i);
L[i][0] = *(--st.end());
st.erase(st.find(i));
}
for(int i = n; i >= 1; i --) {
for(auto x: vecr[i]) {
if(x > 0) {
st.insert(x);
}
else {
st.erase(st.find(-x));
}
}
st.insert(i);
R[i][0] = *st.begin();
st.erase(st.find(i));
}
build(0);
for(int j = 1; j < 20; j ++) {
for(int i = 1; i <= n; i ++) {
L[i][j] = get(j-1, R[i][j-1], L[i][j-1]).mx;
R[i][j] = get(j-1, R[i][j-1], L[i][j-1]).mn;
}
build(j);
}
int q; cin >> q;
while(q --) {
int s, t;
cin >> s >> t;
int ok = 0;
int curl = s, curr = s;
int ans = 0;
for(int i = 19; i >= 0; i --) {
DO cur = get(i, curl, curr);
if(cur.mn <= t && t <= cur.mx) {
ok = 1;
}
else {
curl = cur.mn;
curr = cur.mx;
ans += (1 << i);
}
}
if(!ok) {
cout << "-1\n";
}
else {
cout << ans + 1 << '\n';
}
}
}
/*
*/
main() {
ios;
int tt = 1 , tc = 0;
// cin >> tt;
while(tt --) {
solve(++tc);
}
return 0;
}
Compilation message (stderr)
Main.cpp: In function 'void build(int, int, int, int)':
Main.cpp:28:13: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
28 | int um = ul+ur >> 1;
| ~~^~~
Main.cpp: In function 'DO get(int, int, int, int, int, int)':
Main.cpp:42:13: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
42 | int um = ul+ur >> 1;
| ~~^~~
Main.cpp: At global scope:
Main.cpp:133:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
133 | 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... |