/* Author : Mychecksdead */
#include<bits/stdc++.h>
using namespace std;
#define ll long long int
#define MOD (1000000000+7)
#define MOD1 (998244353)
#define pb push_back
#define all(x) x.begin(), x.end()
#define en cout << '\n'
const int N = 4e5+100, M = 1e5+10, K = 20;
struct Data{
int L, R;
};
int n, k, m, q, L[N][K], R[N][K], lazyL[N][K], lazyR[N][K];
Data up[N][K];
void build(int l, int r, int k, int bit){
lazyL[k][bit] = MOD;
lazyR[k][bit] = 0;
if(l == r){
L[k][bit] = l;
R[k][bit] = l;
return;
}
int mid = l+r>>1;
build(l, mid, k<<1, bit);
build(mid+1, r, k<<1|1, bit);
L[k][bit] = min(L[k<<1][bit], L[k<<1|1][bit]);
R[k][bit] = max(R[k<<1][bit], R[k<<1|1][bit]);
}
void build2(int l, int r, int k, int bit){
lazyL[k][bit] = MOD;
lazyR[k][bit] = 0;
if(l == r){
L[k][bit] = up[l][bit].L;
R[k][bit] = up[l][bit].R;
return;
}
int mid = l+r>>1;
build2(l, mid, k<<1, bit);
build2(mid+1, r, k<<1|1, bit);
L[k][bit] = min(L[k<<1][bit], L[k<<1|1][bit]);
R[k][bit] = max(R[k<<1][bit], R[k<<1|1][bit]);
}
void pushL(int k, int bit){
L[k<<1][bit] = min(L[k<<1][bit], lazyL[k][bit]);
L[k<<1|1][bit] = min(L[k<<1|1][bit], lazyL[k][bit]);
lazyL[k<<1][bit] = min(lazyL[k<<1][bit], lazyL[k][bit]);
lazyL[k<<1|1][bit] = min(lazyL[k<<1|1][bit], lazyL[k][bit]);
lazyL[k][bit] = MOD;
}
void pushR(int k, int bit){
R[k<<1][bit] = max(R[k<<1][bit], lazyR[k][bit]);
R[k<<1|1][bit] = max(R[k<<1|1][bit], lazyR[k][bit]);
lazyR[k<<1][bit] = max(lazyR[k<<1][bit], lazyR[k][bit]);
lazyR[k<<1|1][bit] = max(lazyR[k<<1|1][bit], lazyR[k][bit]);
lazyR[k][bit] = 0;
}
void updateL(int l, int r, int ql, int qr, int k, int bit, int val){
if(ql > r || l > qr) return;
if(ql <= l && r <= qr){
lazyL[k][bit] = min(lazyL[k][bit], val);
L[k][bit] = min(L[k][bit], val);
return;
}
pushL(k, bit);
int mid = l+r>>1;
updateL(l, mid, ql, qr, k<<1, bit, val);
updateL(mid+1, r, ql, qr, k<<1|1, bit, val);
L[k][bit] = min(L[k<<1][bit], L[k<<1|1][bit]);
}
void updateR(int l, int r, int ql, int qr, int k, int bit, int val){
if(ql > r || l > qr) return;
if(ql <= l && r <= qr){
lazyR[k][bit] = max(lazyR[k][bit], val);
R[k][bit] = max(R[k][bit], val);
return;
}
pushR(k, bit);
int mid = l+r>>1;
updateR(l, mid, ql, qr, k<<1, bit, val);
updateR(mid+1, r, ql, qr, k<<1|1, bit, val);
R[k][bit] = max(R[k<<1][bit], R[k<<1|1][bit]);
}
void updateLL(int l, int r, int p, int k, int bit, int val){
if(l == r){
L[k][bit] = min(L[k][bit], val);
return;
}
int mid = l+r>>1;
if(p <= mid)
updateLL(l, mid, p, k<<1, bit, val);
else
updateLL(mid+1, r, p, k<<1|1, bit, val);
L[k][bit] = min(L[k<<1][bit], L[k<<1|1][bit]);
}
void updateRR(int l, int r, int p, int k, int bit, int val){
if(l == r){
R[k][bit] = max(R[k][bit], val);
return;
}
int mid = l+r>>1;
if(p <= mid)
updateRR(l, mid, p, k<<1, bit, val);
else
updateRR(mid+1, r, p, k<<1|1, bit, val);
R[k][bit] = max(R[k<<1][bit], R[k<<1|1][bit]);
}
int getL(int l, int r, int ql, int qr, int k, int bit){
if(ql > r || l > qr) return MOD;
if(ql <= l && r <= qr) return L[k][bit];
int mid = l+r>>1;
if(bit == 0) pushL(k, bit);
return min(getL(l, mid, ql, qr, k<<1, bit), getL(mid+1, r, ql, qr, k<<1|1, bit));
}
int getR(int l, int r, int ql, int qr, int k, int bit){
if(ql > r || l > qr) return 0;
if(ql <= l && r <= qr) return R[k][bit];
int mid = l+r>>1;
if(bit == 0) pushR(k, bit);
return max(getR(l, mid, ql, qr, k<<1, bit), getR(mid+1, r, ql, qr, k<<1|1, bit));
}
void solve(){
cin >> n >> k >> m;
build(1, n, 1, 0);
for(int i = 1; i <= m; ++i){
int l, r; cin >> l >> r;
if(l < r){
int rk = min(r - 1, l + k - 1);
updateR(1, n, l, rk, 1, 0, r);
// cout << l << ' ' << rk << '\n';
}else{
int rk = max(r + 1, l - k + 1);
updateL(1, n, rk, l, 1, 0, r);
// cout << rk << ' ' << l << '\n';
}
}
for(int i = 1; i <= n; ++i) up[i][0] = {getL(1, n, i, i, 1, 0), getR(1, n, i, i, 1, 0)};
for(int j = 1; j < K; ++j){
for(int i = 1; i <= n; ++i){
up[i][j] = {getL(1, n, up[i][j - 1].L, up[i][j - 1].R, 1, j - 1), getR(1, n, up[i][j - 1].L, up[i][j - 1].R, 1, j - 1)};
}
build2(1, n, 1, j);
}
cin >> q;
for(int i = 1; i <= q; ++i){
int l, r; cin >> l >> r;
Data x = {l, l};
bool ok = 0;
int ans = 0;
for(int j = K - 1; j >= 0; --j){
Data u = {getL(1, n, x.L, x.R, 1, j), getR(1, n, x.L, x.R, 1, j)};
if(u.L <= r && u.R >= r){
ok = 1;
}else{
x = u;
ans += (1<<j);
}
// cout << x.L << ' ' << x.R << '\n';
}
if(!ok) cout << "-1\n";
else{
cout << ans + 1 << '\n';
}
}
}
int main(){
cin.tie(0); ios::sync_with_stdio(0);
int tt = 1, aa;
// freopen("in.txt", "r", stdin);
// freopen("out.txt", "w", stdout);
while(tt--){
solve();
en;
}
cerr<<"time taken : "<<(float)clock()/CLOCKS_PER_SEC<<" seconds\n";
return 0;
}
Compilation message
Main.cpp: In function 'void build(int, int, int, int)':
Main.cpp:27:14: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
27 | int mid = l+r>>1;
| ~^~
Main.cpp: In function 'void build2(int, int, int, int)':
Main.cpp:41:14: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
41 | int mid = l+r>>1;
| ~^~
Main.cpp: In function 'void updateL(int, int, int, int, int, int, int)':
Main.cpp:70:14: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
70 | int mid = l+r>>1;
| ~^~
Main.cpp: In function 'void updateR(int, int, int, int, int, int, int)':
Main.cpp:83:14: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
83 | int mid = l+r>>1;
| ~^~
Main.cpp: In function 'void updateLL(int, int, int, int, int, int)':
Main.cpp:94:14: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
94 | int mid = l+r>>1;
| ~^~
Main.cpp: In function 'void updateRR(int, int, int, int, int, int)':
Main.cpp:106:14: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
106 | int mid = l+r>>1;
| ~^~
Main.cpp: In function 'int getL(int, int, int, int, int, int)':
Main.cpp:119:14: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
119 | int mid = l+r>>1;
| ~^~
Main.cpp: In function 'int getR(int, int, int, int, int, int)':
Main.cpp:126:14: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
126 | int mid = l+r>>1;
| ~^~
Main.cpp: In function 'int main()':
Main.cpp:179:15: warning: unused variable 'aa' [-Wunused-variable]
179 | int tt = 1, aa;
| ^~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
10588 KB |
Output is correct |
2 |
Correct |
4 ms |
10588 KB |
Output is correct |
3 |
Correct |
2 ms |
10588 KB |
Output is correct |
4 |
Correct |
3 ms |
10588 KB |
Output is correct |
5 |
Correct |
3 ms |
10588 KB |
Output is correct |
6 |
Correct |
2 ms |
10588 KB |
Output is correct |
7 |
Correct |
3 ms |
10588 KB |
Output is correct |
8 |
Correct |
2 ms |
10588 KB |
Output is correct |
9 |
Correct |
3 ms |
10588 KB |
Output is correct |
10 |
Correct |
1 ms |
8668 KB |
Output is correct |
11 |
Correct |
3 ms |
10588 KB |
Output is correct |
12 |
Correct |
3 ms |
10588 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
10588 KB |
Output is correct |
2 |
Correct |
4 ms |
10588 KB |
Output is correct |
3 |
Correct |
2 ms |
10588 KB |
Output is correct |
4 |
Correct |
3 ms |
10588 KB |
Output is correct |
5 |
Correct |
3 ms |
10588 KB |
Output is correct |
6 |
Correct |
2 ms |
10588 KB |
Output is correct |
7 |
Correct |
3 ms |
10588 KB |
Output is correct |
8 |
Correct |
2 ms |
10588 KB |
Output is correct |
9 |
Correct |
3 ms |
10588 KB |
Output is correct |
10 |
Correct |
1 ms |
8668 KB |
Output is correct |
11 |
Correct |
3 ms |
10588 KB |
Output is correct |
12 |
Correct |
3 ms |
10588 KB |
Output is correct |
13 |
Correct |
18 ms |
12952 KB |
Output is correct |
14 |
Correct |
17 ms |
12892 KB |
Output is correct |
15 |
Correct |
10 ms |
12892 KB |
Output is correct |
16 |
Correct |
11 ms |
12956 KB |
Output is correct |
17 |
Correct |
18 ms |
12956 KB |
Output is correct |
18 |
Correct |
9 ms |
12892 KB |
Output is correct |
19 |
Correct |
13 ms |
12888 KB |
Output is correct |
20 |
Correct |
10 ms |
12892 KB |
Output is correct |
21 |
Correct |
4 ms |
12892 KB |
Output is correct |
22 |
Correct |
15 ms |
12948 KB |
Output is correct |
23 |
Correct |
16 ms |
12892 KB |
Output is correct |
24 |
Correct |
18 ms |
12888 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1084 ms |
107344 KB |
Output is correct |
2 |
Correct |
1006 ms |
107344 KB |
Output is correct |
3 |
Correct |
1186 ms |
107324 KB |
Output is correct |
4 |
Correct |
1022 ms |
107320 KB |
Output is correct |
5 |
Correct |
424 ms |
107108 KB |
Output is correct |
6 |
Correct |
1052 ms |
107368 KB |
Output is correct |
7 |
Correct |
339 ms |
107092 KB |
Output is correct |
8 |
Correct |
325 ms |
107088 KB |
Output is correct |
9 |
Correct |
296 ms |
107088 KB |
Output is correct |
10 |
Correct |
877 ms |
107324 KB |
Output is correct |
11 |
Correct |
752 ms |
107324 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1063 ms |
107348 KB |
Output is correct |
2 |
Correct |
1038 ms |
107604 KB |
Output is correct |
3 |
Correct |
1525 ms |
107604 KB |
Output is correct |
4 |
Correct |
614 ms |
110064 KB |
Output is correct |
5 |
Correct |
615 ms |
110212 KB |
Output is correct |
6 |
Correct |
610 ms |
110416 KB |
Output is correct |
7 |
Correct |
1282 ms |
110296 KB |
Output is correct |
8 |
Correct |
1038 ms |
110128 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1128 ms |
107368 KB |
Output is correct |
2 |
Correct |
1498 ms |
107600 KB |
Output is correct |
3 |
Correct |
1485 ms |
107632 KB |
Output is correct |
4 |
Correct |
1443 ms |
107572 KB |
Output is correct |
5 |
Correct |
1217 ms |
107572 KB |
Output is correct |
6 |
Correct |
1281 ms |
107344 KB |
Output is correct |
7 |
Correct |
991 ms |
107332 KB |
Output is correct |
8 |
Correct |
3 ms |
10588 KB |
Output is correct |
9 |
Correct |
15 ms |
12892 KB |
Output is correct |
10 |
Correct |
868 ms |
107348 KB |
Output is correct |
11 |
Correct |
1094 ms |
107388 KB |
Output is correct |
12 |
Correct |
1080 ms |
107344 KB |
Output is correct |
13 |
Correct |
1087 ms |
107584 KB |
Output is correct |
14 |
Correct |
4 ms |
10588 KB |
Output is correct |
15 |
Correct |
18 ms |
12892 KB |
Output is correct |
16 |
Correct |
1035 ms |
107368 KB |
Output is correct |
17 |
Correct |
1412 ms |
107348 KB |
Output is correct |
18 |
Correct |
1375 ms |
107396 KB |
Output is correct |
19 |
Correct |
1332 ms |
107320 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
10588 KB |
Output is correct |
2 |
Correct |
4 ms |
10588 KB |
Output is correct |
3 |
Correct |
2 ms |
10588 KB |
Output is correct |
4 |
Correct |
3 ms |
10588 KB |
Output is correct |
5 |
Correct |
3 ms |
10588 KB |
Output is correct |
6 |
Correct |
2 ms |
10588 KB |
Output is correct |
7 |
Correct |
3 ms |
10588 KB |
Output is correct |
8 |
Correct |
2 ms |
10588 KB |
Output is correct |
9 |
Correct |
3 ms |
10588 KB |
Output is correct |
10 |
Correct |
1 ms |
8668 KB |
Output is correct |
11 |
Correct |
3 ms |
10588 KB |
Output is correct |
12 |
Correct |
3 ms |
10588 KB |
Output is correct |
13 |
Correct |
18 ms |
12952 KB |
Output is correct |
14 |
Correct |
17 ms |
12892 KB |
Output is correct |
15 |
Correct |
10 ms |
12892 KB |
Output is correct |
16 |
Correct |
11 ms |
12956 KB |
Output is correct |
17 |
Correct |
18 ms |
12956 KB |
Output is correct |
18 |
Correct |
9 ms |
12892 KB |
Output is correct |
19 |
Correct |
13 ms |
12888 KB |
Output is correct |
20 |
Correct |
10 ms |
12892 KB |
Output is correct |
21 |
Correct |
4 ms |
12892 KB |
Output is correct |
22 |
Correct |
15 ms |
12948 KB |
Output is correct |
23 |
Correct |
16 ms |
12892 KB |
Output is correct |
24 |
Correct |
18 ms |
12888 KB |
Output is correct |
25 |
Correct |
1084 ms |
107344 KB |
Output is correct |
26 |
Correct |
1006 ms |
107344 KB |
Output is correct |
27 |
Correct |
1186 ms |
107324 KB |
Output is correct |
28 |
Correct |
1022 ms |
107320 KB |
Output is correct |
29 |
Correct |
424 ms |
107108 KB |
Output is correct |
30 |
Correct |
1052 ms |
107368 KB |
Output is correct |
31 |
Correct |
339 ms |
107092 KB |
Output is correct |
32 |
Correct |
325 ms |
107088 KB |
Output is correct |
33 |
Correct |
296 ms |
107088 KB |
Output is correct |
34 |
Correct |
877 ms |
107324 KB |
Output is correct |
35 |
Correct |
752 ms |
107324 KB |
Output is correct |
36 |
Correct |
1063 ms |
107348 KB |
Output is correct |
37 |
Correct |
1038 ms |
107604 KB |
Output is correct |
38 |
Correct |
1525 ms |
107604 KB |
Output is correct |
39 |
Correct |
614 ms |
110064 KB |
Output is correct |
40 |
Correct |
615 ms |
110212 KB |
Output is correct |
41 |
Correct |
610 ms |
110416 KB |
Output is correct |
42 |
Correct |
1282 ms |
110296 KB |
Output is correct |
43 |
Correct |
1038 ms |
110128 KB |
Output is correct |
44 |
Correct |
1128 ms |
107368 KB |
Output is correct |
45 |
Correct |
1498 ms |
107600 KB |
Output is correct |
46 |
Correct |
1485 ms |
107632 KB |
Output is correct |
47 |
Correct |
1443 ms |
107572 KB |
Output is correct |
48 |
Correct |
1217 ms |
107572 KB |
Output is correct |
49 |
Correct |
1281 ms |
107344 KB |
Output is correct |
50 |
Correct |
991 ms |
107332 KB |
Output is correct |
51 |
Correct |
3 ms |
10588 KB |
Output is correct |
52 |
Correct |
15 ms |
12892 KB |
Output is correct |
53 |
Correct |
868 ms |
107348 KB |
Output is correct |
54 |
Correct |
1094 ms |
107388 KB |
Output is correct |
55 |
Correct |
1080 ms |
107344 KB |
Output is correct |
56 |
Correct |
1087 ms |
107584 KB |
Output is correct |
57 |
Correct |
4 ms |
10588 KB |
Output is correct |
58 |
Correct |
18 ms |
12892 KB |
Output is correct |
59 |
Correct |
1035 ms |
107368 KB |
Output is correct |
60 |
Correct |
1412 ms |
107348 KB |
Output is correct |
61 |
Correct |
1375 ms |
107396 KB |
Output is correct |
62 |
Correct |
1332 ms |
107320 KB |
Output is correct |
63 |
Correct |
1401 ms |
108844 KB |
Output is correct |
64 |
Correct |
1680 ms |
109120 KB |
Output is correct |
65 |
Correct |
1399 ms |
108880 KB |
Output is correct |
66 |
Correct |
443 ms |
110348 KB |
Output is correct |
67 |
Correct |
1450 ms |
110432 KB |
Output is correct |
68 |
Correct |
1006 ms |
110040 KB |
Output is correct |
69 |
Correct |
639 ms |
110160 KB |
Output is correct |
70 |
Correct |
1369 ms |
110312 KB |
Output is correct |
71 |
Correct |
1271 ms |
110400 KB |
Output is correct |