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;
typedef long long ll;
typedef pair<int,int> ii;
typedef pair<ll,ll> pll;
typedef vector<int> vi;
typedef vector<ii> vii;
typedef vector<ll> vll;
typedef vector<pll> vpll;
#define PB push_back
#define PF push_front
#define PPB pop_back
#define PPF pop_front
#define X first
#define Y second
#define MP make_pair
#define all(x) (x).begin(), (x).end()
const int mod = 1e9 + 7; //998244353;
const int inf = 1e9 + 7;
const ll INF = 1e18 + 7;
const int logo = 20;
const int MAXN = 2e5 + 7;
const int off = 1 << logo;
const int trsz = off << 1;
const int dx[] = {1, -1, 0, 0, -1, -1, 1, 1};
const int dy[] = {0, 0, -1, 1, 1, -1, 1, -1};
ii merge(ii a, ii b){
return {min(a.X, b.X), max(a.Y, b.Y)};
}
struct t1{
ii seg[trsz];
t1(){
fill(seg, seg + trsz, (ii){inf, -inf});
for(int i=off+1; i<trsz; i++) seg[i] = {i - off, i - off};
}
void update(int l, int r, ii vl){
for(l += off, r += off; l < r; l >>= 1, r >>= 1){
if(l & 1) seg[l] = merge(seg[l], vl), l++;
if(r & 1) --r, seg[r] = merge(seg[r], vl);
}
}
ii query(int x){
x += off;
ii ret = seg[x];
for(x /= 2; x; x /= 2) ret = merge(ret, seg[x]);
return ret;
}
}st;
struct sparse{
ii seg[trsz];
sparse(){
for(int i=off; i<trsz; i++) seg[i] = {i - off, i - off};
}
void update(int x, ii vl){
x += off;
seg[x] = vl;
for(x /= 2; x; x /= 2) seg[x] = merge(seg[x * 2], seg[x * 2 + 1]);
}
ii query(int l, int r){
ii ret = {inf, -inf};
for(l += off, r += off; l < r; l >>= 1, r >>= 1){
if(l & 1) ret = merge(ret, seg[l++]);
if(r & 1) ret = merge(ret, seg[--r]);
}
return ret;
}
}t[logo];
void solve(){
int n, m, k;
cin >> n >> k >> m;
while(m--){
int l, r;
cin >> l >> r;
if(l < r){
int li = l;
int ri = min(l + k - 1, r);
st.update(li, ri + 1, {inf, r});
}else{
swap(l, r);
int li = max(r - k + 1, l);
int ri = r;
st.update(li, ri + 1, {l, -inf});
}
}
for(int i=1; i<=n; i++) t[0].update(i, st.query(i));
for(int j=1; j<logo; j++) for(int i=1; i<=n; i++){
ii pr = t[j - 1].query(i, i + 1);
t[j].update(i, t[j - 1].query(pr.X, pr.Y + 1));
}
int q;
cin >> q;
while(q--){
int u, v;
cin >> u >> v;
int ans = 0, l = u, r = u;
for(int i=logo-1; i>=0; i--){
ii nov = t[i].query(l, r + 1);
if(!(nov.X <= v and nov.Y >= v)){
ans += (1 << i);
l = nov.X, r = nov.Y;
}
}
if(ans > MAXN) cout << -1 << "\n";
else cout << ans + 1 << "\n";
}
}
int main(){
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
int tt = 1;
//cin >> tt;
while(tt--) solve();
return 0;
}
# | 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... |