#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 = 0; i < n; i++){
for(int j = 0; j < MAXK; j++){
tree[i + BASE][j] = {i, i};
}
}
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);
}
int type;
pair<int, int> query(int v, int l, int p){
if(p < a || b < l){
return {1e9, -1e9};
}
if(a <= l && p <= b){
return tree[v][depth];
}
if(type == 1){
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);
if(type == 1){
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);
}
return {min(lewo.fi, prawo.fi), max(lewo.sec, prawo.sec)};
}
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;
type = 2;
pair<int, int> zapyt = query(1, 0, BASE - 1);
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;
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;
depth = 0;
type = 1;
skacz[i][0] = query(1, 0, BASE - 1);
}
for(int dep = 1; dep < MAXK; dep++){
for(int i = 1; i <= n; i++){
depth = dep - 1;
a = skacz[i][dep - 1].fi;
b = skacz[i][dep - 1].sec;
type = 2;
skacz[i][dep] = query(1, 0, BASE - 1);
a = skacz[i][dep].fi;
b = skacz[i][dep].sec;
depth = dep;
upd(i);
}
}
int q;
cin >> q;
int s, t;
for(int i = 1; i <= q; i++){
cin >> s >> t;
s--;
t--;
getAns(s, t);
}
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
80 ms |
191560 KB |
Output is correct |
2 |
Correct |
56 ms |
191656 KB |
Output is correct |
3 |
Correct |
56 ms |
191568 KB |
Output is correct |
4 |
Correct |
62 ms |
191520 KB |
Output is correct |
5 |
Incorrect |
55 ms |
191552 KB |
Output isn't correct |
6 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
80 ms |
191560 KB |
Output is correct |
2 |
Correct |
56 ms |
191656 KB |
Output is correct |
3 |
Correct |
56 ms |
191568 KB |
Output is correct |
4 |
Correct |
62 ms |
191520 KB |
Output is correct |
5 |
Incorrect |
55 ms |
191552 KB |
Output isn't correct |
6 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
835 ms |
224584 KB |
Output is correct |
2 |
Correct |
783 ms |
224592 KB |
Output is correct |
3 |
Correct |
866 ms |
224848 KB |
Output is correct |
4 |
Correct |
774 ms |
224584 KB |
Output is correct |
5 |
Correct |
728 ms |
224588 KB |
Output is correct |
6 |
Incorrect |
793 ms |
224588 KB |
Output isn't correct |
7 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
825 ms |
224588 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
877 ms |
224588 KB |
Output is correct |
2 |
Incorrect |
1051 ms |
225124 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
80 ms |
191560 KB |
Output is correct |
2 |
Correct |
56 ms |
191656 KB |
Output is correct |
3 |
Correct |
56 ms |
191568 KB |
Output is correct |
4 |
Correct |
62 ms |
191520 KB |
Output is correct |
5 |
Incorrect |
55 ms |
191552 KB |
Output isn't correct |
6 |
Halted |
0 ms |
0 KB |
- |