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 ll long long
#define fi first
#define se second
#define sz(x) (int)x.size()
#define ALL(v) v.begin(),v.end()
#define MASK(k) (1LL << (k))
#define BIT(x, i) (((x) >> (i)) & 1)
#define oo (ll)1e18
#define INF (ll)1e9
#define MOD (ll)(1e9 + 7)
using namespace std;
template<class T1, class T2>
bool maximize(T1 &a, T2 b){if(a < b){a = b; return true;} return false;}
template<class T1, class T2>
bool minimize(T1 &a, T2 b){if(a > b){a = b; return true;} return false;}
template<class T1, class T2>
void add(T1 &a, T2 b){a += b; if(a >= MOD) a -= MOD;}
template<class T1, class T2>
void sub(T1 &a, T2 b){a -= b; if(a < 0) a += MOD;}
template<class T>
void cps(T &v){sort(ALL(v)); v.resize(unique(ALL(v)) - v.begin());}
const int maxN = 1e5 + 10, LOG = 17;
int N, K, M, Q;
pair<int, int> laz[maxN * 4];
pair<int, int> seg[20][maxN * 4];
pair<int, int> combine(pair<int, int> a, pair<int, int> b){
return {min(a.fi, b.fi), max(a.se, b.se)};
}
void push(int i, int l, int r, int u, int v, int c){
if(r < u || v < l) return;
if(u <= l && r <= v){
laz[i] = combine(laz[i], {c, c});
return;
}
int m = (l + r) >> 1;
push(i * 2, l, m, u, v, c);
push(i * 2 + 1, m + 1, r, u, v, c);
}
void run(int i, int l, int r, pair<int, int> prs){
prs = combine(prs, laz[i]);
if(l == r){
seg[0][i] = prs;
return;
}
int m = (l + r) >> 1;
run(i * 2, l, m, prs);
run(i * 2 + 1, m + 1, r, prs);
seg[0][i] = combine(seg[0][i * 2], seg[0][i * 2 + 1]);
// cout << l << ' ' << r << ' ' << seg[0][i].fi << ' ' << seg[0][i].se << '\n';
}
pair<int, int> range(int t, int i, int l, int r, int u, int v){
if(r < u || v < l) return {N + 1, 0};
if(u <= l && r <= v) return seg[t][i];
int m = (l + r) >> 1;
return combine(range(t, i * 2, l, m, u, v), range(t, i * 2 + 1, m + 1, r, u, v));
}
void build(int t, int i, int l, int r){
if(l == r){
pair<int, int> prs = seg[t - 1][i];
seg[t][i] = range(t - 1, 1, 1, N, prs.fi, prs.se);
return;
}
int m = (l + r) >> 1;
build(t, i * 2, l, m);
build(t, i * 2 + 1, m + 1, r);
seg[t][i] = combine(seg[t][i * 2], seg[t][i * 2 + 1]);
}
void solve(){
cin >> N >> K;
cin >> M;
for(int i = 0; i < 4 * N; i++){
laz[i].fi = N + 1;
}
for(int i = 1; i <= N; i++){
push(1, 1, N, i, i, i);
}
for(int i = 1; i <= M; i++){
int s, t; cin >> s >> t;
if(s <= t){
push(1, 1, N, s, min(t, s + K - 1), t);
}
else{
push(1, 1, N, max(t, s - K + 1), s, t);
}
}
run(1, 1, N, {N + 1, 0});
for(int i = 1; i < LOG; i++){
build(i, 1, 1, N);
}
cin >> Q;
while(Q--){
int s, t; cin >> s >> t;
pair<int, int> tmp = range(LOG - 1, 1, 1, N, s, s);
if(t < tmp.fi || t > tmp.se){
cout << -1 << '\n';
continue;
}
pair<int, int> cur = {s, s};
int ans = 1;
for(int i = LOG - 1; i >= 0; i--){
pair<int, int> tmp = range(i, 1, 1, N, cur.fi, cur.se);
if(tmp.fi <= t && t <= tmp.se) continue;
ans += MASK(i);
cur = tmp;
}
cout << ans << '\n';
}
}
int main(){
ios_base::sync_with_stdio(0); cin.tie(0);
// freopen("partner.inp","r",stdin);
// freopen("partner.out","w",stdout);
int t = 1;
// cin >> t;
while(t--){
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... |