#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
int tree[2 * BASE + 7][MAXK][2];
int wDol[2 * BASE + 7][MAXK][2];
int n, k;
void init(){
for(int i = BASE + 1; i <= BASE + n; i++){
for(int j = 0; j < MAXK; j++){
tree[i][j][0] = i - BASE;
tree[i][j][1] = i - BASE;
wDol[i][j][1] = 1e9;
}
}
for(int i = BASE - 1; i >= 1; i--){
for(int j = 0; j < MAXK; j++){
tree[i][j][0] = max(tree[2 * i][j][0], tree[2 * i + 1][j][0]);
tree[i][j][1] = min(tree[2 * i][j][1], tree[2 * i + 1][j][1]);
wDol[i][j][1] = 1e9;
}
}
}
void pchnij(int v, int dep){
tree[2 * v][dep][0] = max(tree[2 * v][dep][0], wDol[v][dep][0]);
tree[2 * v][dep][1] = min(tree[2 * v][dep][1], wDol[v][dep][1]);
tree[2 * v + 1][dep][0] = max(tree[2 * v + 1][dep][0], wDol[v][dep][0]);
tree[2 * v + 1][dep][1] = min(tree[2 * v + 1][dep][1], wDol[v][dep][1]);
wDol[v][dep][0] = 0;
wDol[v][dep][1] = 1e9;
}
void dociagnij(int v, int dep){
tree[v][dep][0] = max(tree[2 * v][dep][0], tree[2 * v + 1][dep][0]);
tree[v][dep][1] = min(tree[2 * v][dep][1], tree[2 * v + 1][dep][1]);
}
int depth, type, a, b, val;
void update(int v, int l, int p){
if(p < a || b < l){
return;
}
if(a <= l && p <= b){
if(type == 0){
tree[v][depth][type] = max(tree[v][depth][type], val);
wDol[v][depth][type] = max(wDol[v][depth][type], val);
}else{
tree[v][depth][type] = min(tree[v][depth][type], val);
wDol[v][depth][type] = min(wDol[v][depth][type], val);
}
return;
}
pchnij(v, depth);
int mid = (l + p) / 2;
update(2 * v, l, mid);
update(2 * v + 1, mid + 1, p);
dociagnij(v, depth);
}
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][1], tree[v][depth][0]};
}
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);
dociagnij(v, depth);
return {min(lewo.fi, prawo.fi), max(lewo.sec, prawo.sec)};
}
void getAns(int start, int cel){
int ans = 0;
pair<int, int> maksPrzedz = {start, start};
for(int i = MAXK - 1; i >= 0; i--){
a = maksPrzedz.fi;
b = maksPrzedz.sec;
depth = i;
pair<int, int> getAkt = query(1, 0, BASE - 1);
if(cel > getAkt.sec || cel < getAkt.fi){
ans = ans | (1 << i);
maksPrzedz.fi = min(maksPrzedz.fi, getAkt.fi);
maksPrzedz.sec = max(maksPrzedz.sec, getAkt.sec);
}
}
a = maksPrzedz.fi;
b = maksPrzedz.sec;
depth = 0;
pair<int, int> getAkt = query(1, 0, BASE - 1);
maksPrzedz.fi = min(maksPrzedz.fi, getAkt.fi);
maksPrzedz.sec = max(maksPrzedz.sec, getAkt.sec);
if(cel > maksPrzedz.sec || cel < maksPrzedz.fi){
cout << -1 << '\n';
}else{
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;
type = 0;
a = x;
b = min(x + k - 1, l);
val = l;
update(1, 0, BASE - 1);
}else{
depth = 0;
type = 1;
a = max(x - k + 1, l);
b = x;
val = l;
update(1, 0, BASE - 1);
}
}
for(int dep = 1; dep < MAXK; dep++){
for(int i = 1; i <= n; i++){
depth = dep - 1;
a = i;
b = i;
pair<int, int> jakDalekoSiegaI = query(1, 0, BASE - 1);
a = jakDalekoSiegaI.fi;
b = jakDalekoSiegaI.sec;
pair<int, int> jakDalekoMozeszDojscWaktDep = query(1, 0, BASE - 1);
//zmien maks
depth = dep;
type = 0;
a = i;
b = i;
val = jakDalekoMozeszDojscWaktDep.sec;
update(1, 0, BASE - 1);
//zmien min
type = 1;
a = i;
b = i;
val = jakDalekoMozeszDojscWaktDep.fi;
update(1, 0, BASE - 1);
}
/* 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;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
208 ms |
345676 KB |
Output is correct |
2 |
Correct |
191 ms |
345932 KB |
Output is correct |
3 |
Correct |
174 ms |
345684 KB |
Output is correct |
4 |
Correct |
176 ms |
346076 KB |
Output is correct |
5 |
Correct |
169 ms |
345668 KB |
Output is correct |
6 |
Incorrect |
155 ms |
345720 KB |
Output isn't correct |
7 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
208 ms |
345676 KB |
Output is correct |
2 |
Correct |
191 ms |
345932 KB |
Output is correct |
3 |
Correct |
174 ms |
345684 KB |
Output is correct |
4 |
Correct |
176 ms |
346076 KB |
Output is correct |
5 |
Correct |
169 ms |
345668 KB |
Output is correct |
6 |
Incorrect |
155 ms |
345720 KB |
Output isn't correct |
7 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
2044 ms |
379356 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
2069 ms |
379668 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
2049 ms |
381068 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
208 ms |
345676 KB |
Output is correct |
2 |
Correct |
191 ms |
345932 KB |
Output is correct |
3 |
Correct |
174 ms |
345684 KB |
Output is correct |
4 |
Correct |
176 ms |
346076 KB |
Output is correct |
5 |
Correct |
169 ms |
345668 KB |
Output is correct |
6 |
Incorrect |
155 ms |
345720 KB |
Output isn't correct |
7 |
Halted |
0 ms |
0 KB |
- |