# | 제출 시각 | 아이디 | 문제 | 언어 | 결과 | 실행 시간 | 메모리 |
---|---|---|---|---|---|---|---|
914059 | cig32 | Railway Trip 2 (JOI22_ho_t4) | C++17 | 0 ms | 0 KiB |
이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include "bits/stdc++.h"
using namespace std;
//#define int long long
#define double long double
const int MAXN = 2e5 + 10;
const int MOD = 1e9 + 7;
mt19937_64 rng((int)std::chrono::steady_clock::now().time_since_epoch().count());
int rnd(int x, int y) {
int u = uniform_int_distribution<int>(x, y)(rng); return u;
}
int bm(int b, int p) {
if(p==0) return 1 % MOD;
int r = bm(b, p >> 1);
if(p&1) return (((r*r) % MOD) * b) % MOD;
return (r*r) % MOD;
}
int inv(int b) {
return bm(b, MOD-2);
}
int fastlog(int x) {
return (x == 0 ? -1 : 64 - __builtin_clzll(x) - 1);
}
void printcase(int i) { cout << "Case #" << i << ": "; }
static void run_with_stack_size(void (*func)(void), size_t stsize) {
char *stack, *send;
stack = (char *)malloc(stsize);
send = stack + stsize - 16;
send = (char *)((uintptr_t)send / 16 * 16);
asm volatile(
"mov %%rsp, (%0)\n"
"mov %0, %%rsp\n"
:
: "r"(send));
func();
asm volatile("mov (%0), %%rsp\n" : : "r"(send));
free(stack);
}
struct segtree_basic {
struct node {
int lazyval, mi, ma, sum; char lazytag;
int len; // not changing
};
int stok;
vector<node> st;
void bu(int l, int r, int idx) {
st[idx].lazyval = st[idx].mi = st[idx].ma = st[idx].sum = 0;
st[idx].lazytag = '?';
st[idx].len = r - l + 1;
if(l == r) {
return;
}
int mid = (l + r) >> 1;
bu(l, mid, 2*idx+1);
bu(mid+1, r, 2*idx+2);
}
void push_down(int idx) {
if(st[idx].lazytag == '?') return;
if(st[idx].lazytag == ':') {
st[2*idx+1].lazyval = st[idx].lazyval;
st[2*idx+1].mi = st[idx].lazyval;
st[2*idx+1].ma = st[idx].lazyval;
st[2*idx+1].sum = st[idx].lazyval * st[2*idx+1].len;
st[2*idx+1].lazytag = ':';
st[2*idx+2].lazyval = st[idx].lazyval;
st[2*idx+2].mi = st[idx].lazyval;
st[2*idx+2].ma = st[idx].lazyval;
st[2*idx+2].sum = st[idx].lazyval * st[2*idx+2].len;
st[2*idx+2].lazytag = ':';
}
else {
st[2*idx+1].lazyval += st[idx].lazyval;
st[2*idx+1].mi += st[idx].lazyval;
st[2*idx+1].ma += st[idx].lazyval;
st[2*idx+1].sum += st[idx].lazyval * st[2*idx+1].len;
st[2*idx+1].lazytag = (st[2*idx+1].lazytag == ':' ? ':' : '+');
st[2*idx+2].lazyval += st[idx].lazyval;
st[2*idx+2].mi += st[idx].lazyval;
st[2*idx+2].ma += st[idx].lazyval;
st[2*idx+2].sum += st[idx].lazyval * st[2*idx+2].len;
st[2*idx+2].lazytag = (st[2*idx+2].lazytag == ':' ? ':' : '+');
}
st[idx].lazytag = '?';
st[idx].lazyval = 0;
}
void push_up(int idx) {
st[idx].mi = min(st[2*idx+1].mi, st[2*idx+2].mi);
st[idx].ma = max(st[2*idx+1].ma, st[2*idx+2].ma);
st[idx].sum = st[2*idx+1].sum + st[2*idx+2].sum;
}
void u1(int l, int r, int constl, int constr, int idx, int val) { // range := v
if(l <= constl && constr <= r) {
st[idx].lazyval = val;
st[idx].mi = val;
st[idx].ma = val;
st[idx].sum = val * st[idx].len;
st[idx].lazytag = ':';
return;
}
int mid = (constl + constr) >> 1;
push_down(idx);
if(mid < l || r < constl) u1(l, r, mid+1, constr, 2*idx+2, val);
else if(constr < l || r < mid + 1) u1(l, r, constl, mid, 2*idx+1, val);
else {
u1(l, r, constl, mid, 2*idx+1, val);
u1(l, r, mid+1, constr, 2*idx+2, val);
}
push_up(idx);
}
void u2(int l, int r, int constl, int constr, int idx, int val) { // range += v
if(l <= constl && constr <= r) {
st[idx].lazyval += val;
st[idx].mi += val;
st[idx].ma += val;
st[idx].sum += val * st[idx].len;
st[idx].lazytag = (st[idx].lazytag == ':' ? ':' : '+');
return;
}
int mid = (constl + constr) >> 1;
push_down(idx);
if(mid < l || r < constl) u2(l, r, mid+1, constr, 2*idx+2, val);
else if(constr < l || r < mid + 1) u2(l, r, constl, mid, 2*idx+1, val);
else {
u2(l, r, constl, mid, 2*idx+1, val);
u2(l, r, mid+1, constr, 2*idx+2, val);
}
push_up(idx);
}
int qu1(int l, int r, int constl, int constr, int idx) { // range min
if(l <= constl && constr <= r) return st[idx].mi;
int mid = (constl + constr) >> 1;
push_down(idx);
if(mid < l || r < constl) return qu1(l, r, mid+1, constr, 2*idx+2);
else if(constr < l || r < mid + 1) return qu1(l, r, constl, mid, 2*idx+1);
else {
return min(qu1(l, r, constl, mid, 2*idx+1), qu1(l, r, mid+1, constr, 2*idx+2));
}
}
int qu2(int l, int r, int constl, int constr, int idx) { // range max
if(l <= constl && constr <= r) return st[idx].ma;
int mid = (constl + constr) >> 1;
push_down(idx);
if(mid < l || r < constl) return qu2(l, r, mid+1, constr, 2*idx+2);
else if(constr < l || r < mid + 1) return qu2(l, r, constl, mid, 2*idx+1);
else {
return max(qu2(l, r, constl, mid, 2*idx+1), qu2(l, r, mid+1, constr, 2*idx+2));
}
}
int qu3(int l, int r, int constl, int constr, int idx) { // range sum
if(l <= constl && constr <= r) return st[idx].sum;
int mid = (constl + constr) >> 1;
push_down(idx);
if(mid < l || r < constl) return qu3(l, r, mid+1, constr, 2*idx+2);
else if(constr < l || r < mid + 1) return qu3(l, r, constl, mid, 2*idx+1);
else {
return qu3(l, r, constl, mid, 2*idx+1) + qu3(l, r, mid+1, constr, 2*idx+2);
}
}
int qu4(int l, int r, int constl, int constr, int idx, int val) { // first at least v
if(l <= constl && constr <= r) {
if(st[idx].ma < val) return -1;
while(constl < constr) {
int mid = (constl + constr) >> 1;
push_down(idx);
if(st[2*idx+1].ma >= val) constr = mid, idx = 2*idx + 1;
else constl = mid+1, idx = 2*idx + 2;
}
return constl;
}
int mid = (constl + constr) >> 1;
push_down(idx);
if(mid < l || r < constl) return qu4(l, r, mid+1, constr, 2*idx+2, val);
else if(constr < l || r < mid + 1) return qu4(l, r, constl, mid, 2*idx+1, val);
else {
int lchild = qu4(l, r, constl, mid, 2*idx+1, val);
if(lchild != -1) return lchild;
return qu4(l, r, mid+1, constr, 2*idx+2, val);
}
}
int qu5(int l, int r, int constl, int constr, int idx, int val) { // first at most v
if(l <= constl && constr <= r) {
if(st[idx].mi > val) return -1;
while(constl < constr) {
int mid = (constl + constr) >> 1;
push_down(idx);
if(st[2*idx+1].mi <= val) constr = mid, idx = 2*idx + 1;
else constl = mid+1, idx = 2*idx + 2;
}
return constl;
}
int mid = (constl + constr) >> 1;
push_down(idx);
if(mid < l || r < constl) return qu5(l, r, mid+1, constr, 2*idx+2, val);
else if(constr < l || r < mid + 1) return qu5(l, r, constl, mid, 2*idx+1, val);
else {
int lchild = qu5(l, r, constl, mid, 2*idx+1, val);
if(lchild != -1) return lchild;
return qu5(l, r, mid+1, constr, 2*idx+2, val);
}
}
int qu6(int l, int r, int constl, int constr, int idx, int val) { // last at least v
if(l <= constl && constr <= r) {
if(st[idx].ma < val) return -1;
while(constl < constr) {
int mid = (constl + constr) >> 1;
push_down(idx);
if(st[2*idx+2].ma >= val) constl = mid+1, idx = 2*idx + 2;
else constr = mid, idx = 2*idx + 1;
}
return constl;
}
int mid = (constl + constr) >> 1;
push_down(idx);
if(mid < l || r < constl) return qu6(l, r, mid+1, constr, 2*idx+2, val);
else if(constr < l || r < mid + 1) return qu6(l, r, constl, mid, 2*idx+1, val);
else {
int rchild = qu6(l, r, mid+1, constr, 2*idx+2, val);
if(rchild != -1) return rchild;
return qu6(l, r, constl, mid, 2*idx+1, val);
}
}
int qu7(int l, int r, int constl, int constr, int idx, int val) { // last at most v
if(l <= constl && constr <= r) {
if(st[idx].mi > val) return -1;
while(constl < constr) {
int mid = (constl + constr) >> 1;
push_down(idx);
if(st[2*idx+2].mi <= val) constl = mid+1, idx = 2*idx + 2;
else constr = mid, idx = 2*idx + 1;
}
return constl;
}
int mid = (constl + constr) >> 1;
push_down(idx);
if(mid < l || r < constl) return qu7(l, r, mid+1, constr, 2*idx+2, val);
else if(constr < l || r < mid + 1) return qu7(l, r, constl, mid, 2*idx+1, val);
else {
int rchild = qu7(l, r, mid+1, constr, 2*idx+2, val);
if(rchild != -1) return rchild;
return qu7(l, r, constl, mid, 2*idx+1, val);
}
}
public:
void resize(int k) {
st.resize(4*k + 10);
stok = k;
bu(0, k, 0);
}
void range_assign(int l, int r, int v) { u1(l, r, 0, stok, 0, v); }
void range_add(int l, int r, int v) { u2(l, r, 0, stok, 0, v); }
int query_min(int l, int r) { return qu1(l, r, 0, stok, 0); }
int query_max(int l, int r) { return qu2(l, r, 0, stok, 0); }
int query_sum(int l, int r) { return qu3(l, r, 0, stok, 0); }
int query_firstAtLeast(int l, int r, int v) { return qu4(l, r, 0, stok, 0, v); }
int query_firstAtMost(int l, int r, int v) { return qu5(l, r, 0, stok, 0, v); }
int query_lastAtLeast(int l, int r, int v) { return qu6(l, r, 0, stok, 0, v); }
int query_lastAtMost(int l, int r, int v) { return qu7(l, r, 0, stok, 0, v); }
};
struct segtree_small {
struct node {
int mn = 0, mx = 0;
};
vector<node> st;
int stok;
void u(int l, int r, int tar, int idx, int val) {
if(l == r) {
st[idx].mn = val;
st[idx].mx = val;
return;
}
int mid = (l + r) >> 1;
if(tar <= mid) u(l, mid, tar, (idx<<1) + 1, val);
else u(mid+1, r, tar, (idx<<1) + 2, val);
st[idx].mn = min(st[(idx<<1) + 1].mn, st[(idx<<1) + 2].mn);
st[idx].mx = max(st[(idx<<1) + 1].mx, st[(idx<<1) + 2].mx);
}
int qu1(int l, int r, int constl, int constr, int idx) {
if(l <= constl && constr <= r) return st[idx].mn;
int mid = (constl + constr) >> 1;
if(mid < l || r < constl) return qu1(l, r, mid+1, constr, (idx<<1) + 2);
else if(constr < l || r < mid+1) return qu1(l, r, constl, mid, (idx<<1) + 1, val);
else return min(qu1(l, r, constl, mid, (idx<<1) + 1), qu1(l, r, mid+1, constr, (idx<<1) + 2));
}
int qu2(int l, int r, int constl, int constr, int idx) {
if(l <= constl && constr <= r) return st[idx].mx;
int mid = (constl + constr) >> 1;
if(mid < l || r < constl) return qu2(l, r, mid+1, constr, (idx<<1) + 2);
else if(constr < l || r < mid+1) return qu2(l, r, constl, mid, (idx<<1) + 1, val);
else return max(qu2(l, r, constl, mid, (idx<<1) + 1), qu2(l, r, mid+1, constr, (idx<<1) + 2));
}
public:
void resize(int k) {
st.resize(4*k + 10);
stok = k;
}
void upd(int i, int v) {
u(0, stok, i, 0, v);
}
int query_min(int l, int r) {
return qu1(l, r, 0, stok, 0);
}
int query_max(int l, int r) {
return qu2(l, r, 0, stok, 0);
}
};
bool cmp(pair<int, int> x, pair<int, int> y) {
return x.second < y.second;
}
void solve(int tc) {
int n, k, m;
cin >> n >> k >> m;
segtree_basic M, P;
M.resize(n);
P.resize(n);
for(int i=1; i<=n; i++) {
M.range_add(i, i, i);
P.range_add(i, i, i);
}
vector<pair<int, int>> updates;
for(int i=1; i<=m; i++) {
int a, b;
cin >> a >> b;
updates.push_back({a, b});
}
sort(updates.begin(), updates.end(), cmp);
for(pair<int, int> x: updates) {
int a = x.first, b = x.second;
if(a > b) continue;
int l = a, r = min(b, a + k - 1);
M.range_assign(l, r, b);
}
reverse(updates.begin(), updates.end());
for(pair<int, int> x: updates) {
int a = x.first, b = x.second;
if(a < b) continue;
int l = max(b, a - k + 1), r = a;
P.range_assign(l, r, b);
}
pair<int, int> st[18][n+1];
for(int i=1; i<=n; i++) {
int v = M.query_sum(i, i);
st[0][i].second = v;
v = P.query_sum(i, i);
st[0][i].first = v;
}
segtree_small susopen[18][2];
for(int i=0; i<18; i++) {
for(int j=0; j<2; j++) {
susopen[i][j].resize(n);
}
}
for(int i=1; i<=n; i++) {
susopen[0][0].upd(i, st[0][i].first);
susopen[0][1].upd(i, st[0][i].second);
}
for(int i=1; i<18; i++) {
for(int j=1; j<=n; j++) {
pair<int, int> bruh = st[i-1][j];
int l = bruh.first, r = bruh.second;
int newl = susopen[i-1][0].query_min(l, r);
int newr = susopen[i-1][1].query_max(l, r);
st[i][j].second = newr;
susopen[i][1].upd(j, newr);
st[i][j].first = newl;
susopen[i][0].upd(j, newl);
}
}
int q;
cin >> q;
while(q--) {
int s, t;
cin >> s >> t;
if((M.query_max(s, s) >= t && s < t) || (P.query_min(s, s) <= t && s > t)) {
cout << "1\n"; continue;
}
int l = s, r = s, S = 0;
for(int i=17; i>=0; i--) {
int m1 = susopen[i][0].query_min(l, r);
int m2 = susopen[i][1].query_max(l, r);
int getl = P.query_min(m1, m2);
int getr = M.query_max(m1, m2);
if((s < t && getr < t) || (s > t && getl > t)) {
S += (1 << i);
l = m1;
r = m2;
}
}
S++;
int m1 = susopen[0][0].query_min(l, r);
int m2 = susopen[0][1].query_max(l, r);
l = m1;
r = m2;
int getr = M.query_max(l, r);
int getl = P.query_min(l, r);
if((s < t && getr < t) || (s > t && getl > t)) S = 1e9;
cout << (S >= 1e7 ? -1 : S + 1) << '\n';
}
//cout << st[0][9].first << ' ' << st[0][9].second << '\n';
//cout << st[1][9].first << ' ' << st[1][9].second << '\n';
}
void uwu() {
ios::sync_with_stdio(0); cin.tie(0);
int t = 1; //cin >> t;
for(int i=1; i<=t; i++) solve(i);
}
int32_t main() {
#ifdef ONLINE_JUDGE
uwu();
#endif
#ifndef ONLINE_JUDGE
run_with_stack_size(uwu, 1024 * 1024 * 1024); // run with a 1 GiB stack
#endif
}
/*
g++ A.cpp -std=c++17 -O2 -o A
./A < input.txt
*/