#include <bits/stdc++.h>
using namespace std;
#define fi first
#define sec second
const int BASE = 1024 * 1024;
const int MAXK = 21;
// 0 -> max, 1 -> min
pair<int, int> tree[2 * BASE + 7][MAXK];
pair<int, int> wDol[2 * BASE + 7];
pair<int, int> skacz[BASE + 7][MAXK];
int n, k;
void init(){
for(int i = BASE; i < BASE + n; i++){
for(int j = 0; j < MAXK; j++){
tree[i][j] = {i - BASE, i - BASE};
}
}
for(int i = BASE - 1; i >= 1; i--){
for(int j = 0; j < MAXK; j++){
tree[i][j].fi = min(tree[2 * i][j].fi, tree[2 * i + 1][j].fi);
tree[i][j].sec = max(tree[2 * i][j].sec, tree[2 * i + 1][j].sec);
}
}
for(int i = 0; i < 2 * BASE; i++){
wDol[i] = {1e9, -1e9};
}
}
void pchnij(int v, int dep){
tree[2 * v][dep].fi = min(tree[2 * v][dep].fi, wDol[v].fi);
tree[2 * v][dep].sec = max(tree[2 * v][dep].sec, wDol[v].sec);
wDol[2 * v].fi = min(wDol[2 * v].fi, wDol[v].fi);
wDol[2 * v].sec = max(wDol[2 * v].sec, wDol[v].sec);
tree[2 * v + 1][dep].fi = min(tree[2 * v + 1][dep].fi, wDol[v].fi);
tree[2 * v + 1][dep].sec = max(tree[2 * v + 1][dep].sec, wDol[v].sec);
wDol[2 * v + 1].fi = min(wDol[2 * v + 1].fi, wDol[v].fi);
wDol[2 * v + 1].sec = max(wDol[2 * v + 1].sec, wDol[v].sec);
wDol[v] = {1e9, -1e9};
}
int depth, a, b, val;
void update(int v, int l, int p){
if(p < a || b < l){
return;
}
if(a <= l && p <= b){
tree[v][depth].fi = min(tree[v][depth].fi, val);
tree[v][depth].sec = max(tree[v][depth].sec, val);
wDol[v].fi = min(wDol[v].fi, val);
wDol[v].sec = max(wDol[v].sec, val);
return;
}
pchnij(v, depth);
int mid = (l + p) / 2;
update(2 * v, l, mid);
update(2 * v + 1, mid + 1, p);
tree[v][depth].fi = min(tree[2 * v][depth].fi, tree[2 * v + 1][depth].fi);
tree[v][depth].sec = max(tree[2 * v][depth].sec, tree[2 * v + 1][depth].sec);
}
pair<int, int> query(int v, int l, int p){
if(p < a || b < l){
return {1e9, 0};
}
if(a <= l && p <= b){
return tree[v][depth];
}
pchnij(v, depth);
int mid = (l + p) / 2;
pair<int, int> lewo = query(2 * v, l, mid);
pair<int, int> prawo = query(2 * v + 1, mid + 1, p);
return {min(lewo.fi, prawo.fi), max(lewo.sec, prawo.sec)};
}
pair<int, int> q2(){
a = a + BASE;
a--;
b = b + BASE;
b++;
pair<int, int> ret = {1e9, -1e9};
while(a / 2 != b / 2){
if(!(a & 1)){
ret.fi = min(ret.fi, tree[a + 1][depth].fi);
ret.sec = max(ret.sec, tree[a + 1][depth].sec);
}
if(b & 1){
ret.fi = min(ret.fi, tree[b - 1][depth].fi);
ret.sec = max(ret.sec, tree[b - 1][depth].sec);
}
a = a / 2;
b = b / 2;
}
return ret;
}
void upd(int v){
v += BASE;
while(v >= 1){
tree[v][depth].fi = min(tree[v][depth].fi, a);
tree[v][depth].sec = max(tree[v][depth].sec, b);
v /= 2;
}
}
void getAns(int start, int cel){
if(start == cel){
cout << 0 << '\n';
return;
}
int ans = 0;
pair<int, int> akt = {start, start};
for(int i = MAXK - 1; i >= 0; i--){
depth = i;
a = akt.fi;
b = akt.sec;
pair<int, int> zapyt = q2();
if(cel < zapyt.fi || cel > zapyt.sec){
if(i == MAXK - 1){
cout << -1 << '\n';
return;
}
ans = ans + (1 << i);
akt = zapyt;
}
}
cout << ans + 1 << '\n';
}
int main()
{
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin >> n >> k;
init();
int m;
cin >> m;
int x, l;
for(int i = 1; i <= m; i++){
cin >> x >> l;
if(x <= l){
depth = 0;
a = x;
b = min(x + k - 1, l);
val = l;
update(1, 0, BASE - 1);
}else{
depth = 0;
a = max(x - k + 1, l);
b = x;
val = l;
update(1, 0, BASE - 1);
}
}
for(int i = 1; i <= n; i++){
a = i;
b = i;
skacz[i][0] = query(1, 0, BASE - 1);
}
for(int dep = 1; dep < MAXK; dep++){
for(int i = 1; i <= n; i++){
depth = dep;
a = skacz[i][dep - 1].fi;
b = skacz[i][dep - 1].sec;
skacz[i][dep] = q2();
a = skacz[i][dep].fi;
b = skacz[i][dep].sec;
upd(i);
}
cout << dep << ' ' << (1 << dep) << '\n';
for(int i = 1; i <= n; i++){
a = i;
b = i;
depth = dep;
pair<int, int> akt = query(1, 0, BASE - 1);
cout << akt.fi << ' ' << akt.sec << '\n';
}
cout << "----\n";
}
int q;
cin >> q;
int s, t;
for(int i = 1; i <= q; i++){
cin >> s >> t;
getAns(s, t);
}
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
89 ms |
191572 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
89 ms |
191572 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1343 ms |
245760 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1292 ms |
243764 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1483 ms |
247612 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
89 ms |
191572 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |