#include <bits/stdc++.h>
#define ll long long
#define ull unsigned ll
#define f first
#define s second
#define pii pair<int,int>
#define pll pair<ll,ll>
#define pb push_back
#define epb emplace_back
using namespace std;
const int nmax = 100001;
int mx[nmax][19][19];
int mn[nmax][19][19];
int get_mn(int l, int r, int ind){
int x = r - l + 1;
int u = log2(x);
return min(mn[l][u][ind], mn[r - (1 << u) + 1][u][ind]);
}
int get_mx(int l, int r, int ind){
int x = r - l + 1;
int u = log2(x);
return max(mx[l][u][ind], mx[r - (1 << u) + 1][u][ind]);
}
int main(){
ios_base::sync_with_stdio(false); cin.tie(0);
int n; cin >> n;
int k; cin >> k;
int m; cin >> m;
int a[m + 1], b[m + 1];
for(int i = 1; i <= n; i++){
for(int j = 0; j < 19; j++){
for(int x = 0; x < 19; x++)
mx[i][j][x] = -1e9, mn[i][j][x] = 1e9;
}
}
for(int i = 1; i <= m; i++){
int a, b; cin >> a >> b;
if(a < b){
mx[a][0][18] = max(mx[a][0][18], b);
}
if(a > b){
mn[a][0][18] = min(mn[a][0][18], b);
}
}
for(int i = 1; i < 19; i++){
for(int j = 1; j <= n; j++){
int R = min(j + (1 << i) - 1, n);
mn[j][i][18] = min(mn[j][i - 1][18], mn[R][i - 1][18]);
mx[j][i][18] = max(mx[j][i - 1][18], mx[R][i - 1][18]);
}
}
for(int i = 1; i <= n; i++){
mn[i][0][0] = get_mn(i, min(n, i + k - 1), 18);
mx[i][0][0] = get_mx(max(i - k + 1, 1), i, 18);
mn[i][0][0] = min(mn[i][0][0], i);
mx[i][0][0] = max(mx[i][0][0], i);
}
for(int i = 1; i < 19; i++){
for(int j = 1; j <= n; j++){
int R = min(j + (1 << i) - 1, n);
mn[j][i][0] = min(mn[j][i - 1][0], mn[R][i - 1][0]);
mx[j][i][0] = max(mx[j][i - 1][0], mx[R][i - 1][0]);
}
}
for(int i = 1; i < 18; i++){
for(int j = 1; j <= n; j++){
int L = mn[j][0][i - 1], R = mx[j][0][i - 1];
mn[j][0][i] = get_mn(L, R, i - 1);
mx[j][0][i] = get_mx(L, R, i - 1);
}
for(int x = 1; x < 19; x++){
for(int j = 1; j <= n; j++){
int R= min(j + (1 << x) - 1, n);
mn[j][x][i] = min(mn[j][x - 1][i], mn[R][x - 1][i]);
mx[j][x][i] = max(mx[j][x - 1][i], mx[R][x - 1][i]);
}
}
}
int q; cin >> q;
while(q--){
int a, b; cin >> a >> b;
int L = a, R = a;
int ans = 0;
for(int j = 17; j >= 0; j--){
int l = get_mn(L, R, j);
int r = get_mx(L, R, j);
if(l <= b && r >= b) continue;
L = l, R = r;
ans += (1 << j);
}
if(ans > (1 << 17)) cout << -1 << "\n";
else cout << ans + 1 << "\n";
}
return 0;
}
Compilation message
Main.cpp: In function 'int main()':
Main.cpp:35:9: warning: unused variable 'a' [-Wunused-variable]
35 | int a[m + 1], b[m + 1];
| ^
Main.cpp:35:19: warning: unused variable 'b' [-Wunused-variable]
35 | int a[m + 1], b[m + 1];
| ^
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
6 ms |
1108 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
6 ms |
1108 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1306 ms |
283828 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1354 ms |
284412 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1327 ms |
285816 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
6 ms |
1108 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |