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;
using ll = long long;
#define all(x) (x).begin(), (x).end()
template<class T> struct ST {
static constexpr T ID = {(int) -1e9, 0}; // or whatever ID
inline T comb(T a, T b) { return max(a, b); } // or whatever function
int sz;
vector<T> t;
void init(int _sz, T val = ID) {
t.assign((sz = _sz) * 2, ID);
}
void init(vector<T> &v) {
t.resize((sz = v.size()) * 2);
for (int i = 0; i < sz; ++i)
t[i + sz] = v[i];
for (int i = sz - 1; i; --i)
t[i] = comb(t[i * 2], t[(i * 2) | 1]);
}
void upd(int i, T x) {
for (t[i += sz] = x; i > 1; i >>= 1)
t[i >> 1] = comb(t[i], t[i ^ 1]);
}
T query(int l, int r) {
T ql = ID, qr = ID;
for (l += sz, r += sz + 1; l < r; l >>= 1, r >>= 1) {
if (l & 1) ql = comb(ql, t[l++]);
if (r & 1) qr = comb(t[--r], qr);
}
return comb(ql, qr);
}
};
const int N = 1e5 + 7, L = 20;
int n, k, m;
int r[N], lift[N][L];
vector<int> e[N];
int main() {
cin.tie(0)->sync_with_stdio(false);
cin >> n >> k >> m;
for (int i = 1; i <= m; i++) {
int a, b;
cin >> a >> b;
int s = min(b, a + k - 1);
e[a].push_back(b);
e[s + 1].push_back(-b);
}
ST<array<int, 2>> st;
st.init(n + 1);
multiset<int> s;
for (int i = 1; i <= n; i++) {
for (int x : e[i]) {
if (x > 0)
s.insert(x);
else
s.erase(s.find(-x));
}
if (s.size())
r[i] = *s.rbegin();
st.upd(i, {r[i], i});
}
for (int i = n; i >= 1; i--) {
if (r[i])
lift[i][0] = st.query(i, r[i])[1];
else
lift[i][0] = i;
for (int j = 1; j < L; j++)
lift[i][j] = lift[lift[i][j - 1]][j - 1];
}
for (int i = 1; i <= n; i++)
for (int j = 0; j < 4; j++)
cout << lift[i][j] << " \n"[j == 3];
int q;
cin >> q;
while (q--) {
int s, t;
cin >> s >> t;
if (r[s] >= t) {
cout << 1 << '\n';
continue;
}
int ans = 1;
for (int i = L - 1; ~i; i--) {
if (r[lift[s][i]] < t)
ans += (1 << i), s = lift[s][i];
}
s = lift[s][0];
cout << (r[s] >= t ? ans + 1 : -1) << '\n';
}
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... |