This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
//Sylwia Sapkowska
#include <bits/stdc++.h>
#pragma GCC optimize("O3", "unroll-loops")
using namespace std;
void __print(int x) {cerr << x;}
void __print(long long x) {cerr << x;}
void __print(long double x) {cerr << x;}
void __print(char x) {cerr << "'" << x << "'";}
void __print(const char *x) {cerr << '"' << x << '"';}
void __print(const string &x) {cerr << '"' << x << '"';}
void __print(bool x) {cerr << (x ? "true" : "false");}
template<typename T, typename V>
void __print(const pair<T, V> &x) {cerr << '{'; __print(x.first); cerr << ", "; __print(x.second); cerr << '}';}
template<typename T>
void __print(const T &x) {int f = 0; cerr << '{'; for (auto &i: x) cerr << (f++ ? ", " : ""), __print(i); cerr << "}";}
void _print() {cerr << "]\n";}
template <typename T, typename... V>
void _print(T t, V... v) {__print(t); if (sizeof...(v)) cerr << ", "; _print(v...);}
#ifdef LOCAL
#define debug(x...) cerr << "[" << #x << "] = ["; _print(x)
#else
#define debug(x...)
#endif
#define int long long
typedef pair<int, int> T;
const int oo = 1e18, oo2 = 1e9+7, K = 20;
const int mod = 998244353;
struct TreeMin{
vector<int>tab;
int size = 1;
void init(int n){
while (size < n) size*=2;
tab.assign(2*size, oo);
}
void update(int x, int v){
x += size;
tab[x] = min(tab[x], v);
while (x){
x/=2;
tab[x] = min(tab[2*x], tab[2*x+1]);
}
}
int query(int l, int r){
int ans = oo;
for (l += size-1, r += size+1; r-l>1; l/=2, r/=2){
if (!(l&1)) ans = min(ans, tab[l+1]);
if (r&1) ans = min(ans, tab[r-1]);
}
return ans;
}
};
struct TreeMax{
vector<int>tab;
int size = 1;
void init(int n){
while (size < n) size*=2;
tab.assign(2*size, -oo);
}
void update(int x, int v){
x += size;
tab[x] = max(tab[x], v);
while (x){
x/=2;
tab[x] = max(tab[2*x], tab[2*x+1]);
}
}
int query(int l, int r){
int ans = -oo;
for (l += size-1, r += size+1; r-l>1; l/=2, r/=2){
if (!(l&1)) ans = max(ans, tab[l+1]);
if (r&1) ans = max(ans, tab[r-1]);
}
return ans;
}
};
void solve(){
int n, k; cin >> n >> k;
int m; cin >> m;
vector<T>where(m);
for (auto &[x, y]: where) cin >> x >> y;
multiset<int>s;
vector<T>sweep;
//left to right
for (int i = 0; i<m; i++){
if (where[i].first < where[i].second){
sweep.emplace_back(where[i].first, where[i].second);
sweep.emplace_back(min(where[i].first + k, where[i].second), -where[i].second);
}
}
sort(sweep.begin(), sweep.end());
debug(sweep);
vector<int>right(n+1);
int ptr = -1;
for (int i = 1; i<=n; i++){
while (ptr + 1 < (int)sweep.size() && sweep[ptr+1].first <= i){
ptr++;
if (sweep[ptr].second > 0) s.insert(sweep[ptr].second);
else s.erase(s.find(-sweep[ptr].second));
}
right[i] = (!s.empty() ? *s.rbegin() : i);
}
debug(right);
//right to left
s.clear(); sweep.clear();
for (int i = 0; i<m; i++){
if (where[i].first > where[i].second){
sweep.emplace_back(where[i].first, where[i].second);
sweep.emplace_back(max(where[i].first - k, where[i].second), -where[i].second);
}
}
sort(sweep.begin(), sweep.end());
debug(sweep);
vector<int>left(n+1);
ptr = (int)sweep.size();
for (int i = n; i>=1; i--){
while (ptr - 1 >= 0 && sweep[ptr-1].first >= i){
ptr--;
if (sweep[ptr].second > 0) s.insert(sweep[ptr].second);
else s.erase(s.find(-sweep[ptr].second));
}
left[i] = (!s.empty() ? *s.begin() : i);
}
debug(left);
vector<TreeMin>L(K);
vector<TreeMax>R(K);
for (int i = 0; i<K; i++){
L[i].init(n+2);
R[i].init(n+2);
}
for (int i = 1; i<=n; i++) {
L[0].update(i, left[i]);
R[0].update(i, right[i]);
}
for (int j = 1; j<K; j++){
debug(j);
for (int i = 1; i<=n; i++){
int ll = L[j-1].query(i, i);
int rr = R[j-1].query(i, i);
L[j].update(i, L[j-1].query(ll, rr));
R[j].update(i, R[j-1].query(ll, rr));
debug(i, L[j-1].query(ll, rr), R[j-1].query(ll, rr));
}
}
int q; cin >> q;
while (q--){
int a, b; cin >> a >> b;
int ans = 0;
int ll = a, rr = a;
bool any = 0;
for (int i = K-1; i>=0; i--){
int newL = L[i].query(ll, rr);
int newR = R[i].query(ll, rr);
if (b >= newL && b <= newR){
any = 1;
;
} else {
ans += (1<<i);
ll = newL;
rr = newR;
}
}
if (!any) cout << "-1\n";
else cout << ans+1 << "\n";
}
}
int32_t main(){
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
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... |