/* 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 = 8e5+100, M = 1e5+10, K = 21;
struct Data{
int L, R;
};
int n, q, L[N][K], R[N][K];
Data up[N][K];
void build(int l, int r, int k, int bit){
if(l == r){
L[k][bit] = up[l][bit].L;
R[k][bit] = up[l][bit].R;
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 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;
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;
return max(getR(l, mid, ql, qr, k<<1, bit), getR(mid+1, r, ql, qr, k<<1|1, bit));
}
void solve(){
cin >> n;
for(int i = 1; i <= n; ++i){
int l, r; cin >> l >> r;
up[i][0] = {l, r};
}
build(1, n, 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)};
}
build(1, n, 1, j);
}
cin >> q;
for(int i = 1; i <= q; ++i){
int l; cin >> l;
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 == 1 && u.R == n){
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();
}
cerr<<"time taken : "<<(float)clock()/CLOCKS_PER_SEC<<" seconds\n";
return 0;
}
Compilation message
passport.cpp: In function 'void build(int, int, int, int)':
passport.cpp:25:14: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
25 | int mid = l+r>>1;
| ~^~
passport.cpp: In function 'void updateLL(int, int, int, int, int, int)':
passport.cpp:39:14: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
39 | int mid = l+r>>1;
| ~^~
passport.cpp: In function 'void updateRR(int, int, int, int, int, int)':
passport.cpp:51:14: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
51 | int mid = l+r>>1;
| ~^~
passport.cpp: In function 'int getL(int, int, int, int, int, int)':
passport.cpp:64:14: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
64 | int mid = l+r>>1;
| ~^~
passport.cpp: In function 'int getR(int, int, int, int, int, int)':
passport.cpp:70:14: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
70 | int mid = l+r>>1;
| ~^~
passport.cpp: In function 'int main()':
passport.cpp:113:15: warning: unused variable 'aa' [-Wunused-variable]
113 | int tt = 1, aa;
| ^~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
4696 KB |
Output is correct |
2 |
Correct |
1 ms |
4440 KB |
Output is correct |
3 |
Correct |
1 ms |
4440 KB |
Output is correct |
4 |
Correct |
477 ms |
123984 KB |
Output is correct |
5 |
Correct |
1217 ms |
123740 KB |
Output is correct |
6 |
Correct |
1785 ms |
123736 KB |
Output is correct |
7 |
Correct |
248 ms |
123592 KB |
Output is correct |
8 |
Correct |
763 ms |
117588 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
4440 KB |
Output is correct |
2 |
Correct |
1 ms |
4444 KB |
Output is correct |
3 |
Correct |
1 ms |
4444 KB |
Output is correct |
4 |
Correct |
1 ms |
4444 KB |
Output is correct |
5 |
Correct |
1 ms |
4444 KB |
Output is correct |
6 |
Correct |
1 ms |
4444 KB |
Output is correct |
7 |
Incorrect |
1 ms |
4444 KB |
Output isn't correct |
8 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
4440 KB |
Output is correct |
2 |
Correct |
1 ms |
4444 KB |
Output is correct |
3 |
Correct |
1 ms |
4444 KB |
Output is correct |
4 |
Correct |
1 ms |
4444 KB |
Output is correct |
5 |
Correct |
1 ms |
4444 KB |
Output is correct |
6 |
Correct |
1 ms |
4444 KB |
Output is correct |
7 |
Incorrect |
1 ms |
4444 KB |
Output isn't correct |
8 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
4440 KB |
Output is correct |
2 |
Correct |
1 ms |
4444 KB |
Output is correct |
3 |
Correct |
1 ms |
4444 KB |
Output is correct |
4 |
Correct |
1 ms |
4444 KB |
Output is correct |
5 |
Correct |
1 ms |
4444 KB |
Output is correct |
6 |
Correct |
1 ms |
4444 KB |
Output is correct |
7 |
Incorrect |
1 ms |
4444 KB |
Output isn't correct |
8 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
4696 KB |
Output is correct |
2 |
Correct |
1 ms |
4440 KB |
Output is correct |
3 |
Correct |
1 ms |
4440 KB |
Output is correct |
4 |
Correct |
477 ms |
123984 KB |
Output is correct |
5 |
Correct |
1217 ms |
123740 KB |
Output is correct |
6 |
Correct |
1785 ms |
123736 KB |
Output is correct |
7 |
Correct |
248 ms |
123592 KB |
Output is correct |
8 |
Correct |
763 ms |
117588 KB |
Output is correct |
9 |
Correct |
1 ms |
4440 KB |
Output is correct |
10 |
Correct |
1 ms |
4444 KB |
Output is correct |
11 |
Correct |
1 ms |
4444 KB |
Output is correct |
12 |
Correct |
1 ms |
4444 KB |
Output is correct |
13 |
Correct |
1 ms |
4444 KB |
Output is correct |
14 |
Correct |
1 ms |
4444 KB |
Output is correct |
15 |
Incorrect |
1 ms |
4444 KB |
Output isn't correct |
16 |
Halted |
0 ms |
0 KB |
- |