이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define int ll
#define pii pair<int, int>
#define f first
#define s second
#define REP(i,n) for (int i = 0; i<n; ++i)
#define REP1(i,n) for (int i = 1; i<=n; ++i)
#define SZ(x) (int)((x).size())
#define ALL(x) (x).begin(), (x).end()
#define pb push_back
#define MX(a,b) a = max(a,b)
#define MN(a,b) a = min(a,b)
#ifdef BALBIT
#define bug(...) cerr<<"#"<<__LINE__<<" "<<#__VA_ARGS__<<" - ", _do(__VA_ARGS__)
template <typename T> void _do( T && x) {cerr<<x<<endl;}
template <typename T, typename ...S> void _do( T && x, S && ...y) {cerr<<x<<", "; _do(y...);}
#else
#define bug(...)
#define endl '\n'
#endif // BALBIT
const int maxn = 1e5+5;
int L[18][maxn], R[18][maxn];
ll lseg[18][2*maxn], rseg[18][2*maxn];
void modify(int lay, int p, int l, int r) {
p+=maxn;
for (lseg[lay][p] = l, rseg[lay][p]=r; p > 1; p >>=1) {
rseg[lay][p>>1] = max(rseg[lay][p], rseg[lay][p^1]);
lseg[lay][p>>1] = min(lseg[lay][p], lseg[lay][p^1]);
}
}
pii query(int lay, int l, int r) {
pii re = {maxn, -maxn};
for (l += maxn, r += maxn; l < r; l>>=1, r>>=1) {
if (l & 1) {
MN(re.f, lseg[lay][l]);
MX(re.s, rseg[lay][l]);
++l;
}
if (r & 1) {
--r;
MN(re.f, lseg[lay][r]);
MX(re.s, rseg[lay][r]);
}
}
return re;
}
int initl[maxn], initr[maxn];
vector<pair<int, bool> > el[maxn], er[maxn]; // event l, event r, {number to add, [1 is add, 0 is delete]}
signed main(){
ios::sync_with_stdio(0), cin.tie(0);
bug(1,2);
int n,k; cin>>n>>k;
int m; cin>>m;
REP(i,m) {
int a,b; cin>>a>>b; --a; --b;
if (a < b) {
int l = a, r = min(a+k-1, b);
el[l].pb({b, 1}); if (r+1 < n) el[r+1].pb({b, 0});
}else{
int r = a, l = max(a-k+1, b);
er[r].pb({b, 1});
if (l-1 >= 0) er[l-1].pb({b, 0});
}
}
{
multiset<int> hv;
REP(i, n) {
// work with el
for (auto e : el[i]) {
if (e.s) {
hv.insert(e.f);
}else{
hv.erase(hv.find(e.f));
}
}
int to = hv.empty()? i : *hv.rbegin();
bug(to);
initl[i] = to;
}
}
{
multiset<int> hv;
for (int i = n-1; i>=0; --i) {
// work with er
for (auto e : er[i]) {
if (e.s) {
hv.insert(e.f);
}else{
hv.erase(hv.find(e.f));
}
}
int to = hv.empty()? i : *hv.begin();
bug(i,to);
initr[i] = to;
}
}
REP(i,n) {
L[0][i] = initr[i];
R[0][i] = initl[i];
modify(0, i, L[0][i], R[0][i]);
bug(0, L[0][i], R[0][i]);
}
for (int j = 1; j<18; ++j) {
REP(i,n) {
tie(L[j][i], R[j][i]) = query(j-1, L[j-1][i], R[j-1][i] + 1);
}
REP(i,n) {
// if (j <= 2) {
// bug(j, L[j][i], R[j][i]);
// }
modify(j, i, L[j][i], R[j][i]);
}
}
int q; cin>>q;
while (q--) {
int s,t; cin>>s>>t;
--s; --t;
int nl = s, nr = s;
bool pos = 0;
int re = 0;
for (int j = 17; j>=0; --j) {
int tl, tr; tie(tl, tr) = query(j, nl, nr+1);
if (tl <= t && tr >= t) {
pos = 1;
}else{
re += (1<<j);
tie(nl, nr) = tie(tl, tr);
}
}
++re;
if (pos == 0) re = -1;
cout<<re<<endl;
}
}
# | 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... |