이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
#include <bits/extc++.h>
#define StarBurstStream ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
#define iter(a) a.begin(), a.end()
#define riter(a) a.rbegin(), a.rend()
#define lsort(a) sort(iter(a))
#define gsort(a) sort(riter(a))
#define pb(a) push_back(a)
#define eb(a) emplace_back(a)
#define pf(a) push_front(a)
#define ef(a) emplace_front(a)
#define pob pop_back()
#define pof pop_front()
#define mp(a, b) make_pair(a, b)
#define F first
#define S second
#define mt make_tuple
#define gt(t, i) get<i>(t)
#define tomax(a, b) ((a) = max((a), (b)))
#define tomin(a, b) ((a) = min((a), (b)))
#define topos(a) ((a) = (((a) % MOD + MOD) % MOD))
#define uni(a) a.resize(unique(iter(a)) - a.begin())
#define printv(a, b) {bool pvaspace=false; \
for(auto pva : a){ \
if(pvaspace) b << " "; pvaspace=true;\
b << pva;\
}\
b << "\n";}
using namespace std;
using namespace __gnu_pbds;
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
using pdd = pair<ld, ld>;
using tiii = tuple<int, int, int>;
const ll MOD = 1000000007;
const ll MAX = 2147483647;
template<typename A, typename B>
ostream& operator<<(ostream& o, pair<A, B> p){
return o << '(' << p.F << ',' << p.S << ')';
}
ll ifloor(ll a, ll b){
if(b < 0) a *= -1, b *= -1;
if(a < 0) return (a - b + 1) / b;
else return a / b;
}
ll iceil(ll a, ll b){
if(b < 0) a *= -1, b *= -1;
if(a > 0) return (a + b - 1) / b;
else return a / b;
}
#define lc (2 * id + 1)
#define rc (2 * id + 2)
int n;
pii onion(pii a, pii b){
return mp(min(a.F, b.F), max(a.S, b.S));
}
bool inside(pii r, int p){
return r.F <= p && p <= r.S;
}
struct SegmentTree{
vector<pii> st;
void build(vector<pii>& v, int L = 1, int R = n, int id = 0){
if(L == R){
st[id] = v[L];
return;
}
int M = (L + R) / 2;
build(v, L, M, lc);
build(v, M + 1, R, rc);
st[id] = onion(st[lc], st[rc]);
}
pii query(int l, int r, int L = 1, int R = n, int id = 0){
//assert(max(l, L) <= min(r, R));
if(l <= L && R <= r) return st[id];
int M = (L + R) / 2;
if(r <= M) return query(l, r, L, M, lc);
else if(l > M) return query(l, r, M + 1, R, rc);
else return onion(query(l, r, L, M, lc), query(l, r, M + 1, R, rc));
}
void init(vector<pii>& v){
st.resize(4 * n);
build(v);
}
};
int main(){
StarBurstStream
int K;
cin >> n >> K;
vector<vector<int>> el(n + 2), er(n + 2);
int m;
cin >> m;
for(int i = 1; i <= m; i++){
int a, b;
cin >> a >> b;
if(a < b){
er[a].eb(b);
er[min(n + 1, a + K)].eb(-b);
}
else{
el[max(1, a - K + 1)].eb(b);
el[a + 1].eb(-b);
}
}
const int SZ = 20;
vector<vector<pii>> go(SZ, vector<pii>(n + 1));
multiset<int> sl, sr;
for(int i = 1; i <= n; i++){
for(int j : el[i]){
if(j > 0) sl.insert(j);
else sl.erase(sl.find(-j));
}
for(int j : er[i]){
if(j > 0) sr.insert(j);
else sr.erase(sr.find(-j));
}
int l = sl.empty() ? i : *sl.begin();
int r = sr.empty() ? i : *sr.rbegin();
assert(l <= r);
go[0][i] = mp(l, r);
}
vector<SegmentTree> st(SZ);
st[0].init(go[0]);
//cerr << "test " << 0 << " ";
//printv(go[0], cerr);
for(int i = 1; i < SZ; i++){
for(int j = 1; j <= n; j++){
go[i][j] = st[i - 1].query(go[i - 1][j].F, go[i - 1][j].S);
}
st[i].init(go[i]);
//cerr << "test " << i << " ";
//printv(go[i], cerr);
}
int q;
cin >> q;
while(q--){
int s, t;
cin >> s >> t;
if(!inside(go[SZ - 1][s], t)){
cout << "-1\n";
continue;
}
int ans = 0;
pii now = mp(s, s);
for(int i = SZ - 1; i >= 0; i--){
pii nxt = st[i].query(now.F, now.S);
if(!inside(nxt, t)){
ans += 1 << i;
now = nxt;
}
}
cout << ans + 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... |