#include<bits/stdc++.h>
using namespace std;
using ll = long long;
#define all(x) (x).begin(), (x).end()
template<class T> struct ST {
static constexpr T ID = {{{(int) 1e9, 0}, {0, 0}}}; // or whatever ID
inline T comb(T a, T b) { return {min(a[0], b[0]), max(a[1], b[1])}; } // or whatever function
int sz;
vector<T> t;
void init(int _sz, T val = ID) {
t.assign((sz = _sz) * 2, ID);
}
void init(vector<T> &v) {
t.resize((sz = v.size()) * 2);
for (int i = 0; i < sz; ++i)
t[i + sz] = v[i];
for (int i = sz - 1; i; --i)
t[i] = comb(t[i * 2], t[(i * 2) | 1]);
}
void upd(int i, T x) {
for (t[i += sz] = x; i > 1; i >>= 1)
t[i >> 1] = comb(t[i], t[i ^ 1]);
}
T query(int l, int r) {
T ql = ID, qr = ID;
for (l += sz, r += sz + 1; l < r; l >>= 1, r >>= 1) {
if (l & 1) ql = comb(ql, t[l++]);
if (r & 1) qr = comb(t[--r], qr);
}
return comb(ql, qr);
}
};
template<class T> struct ST_MN {
static constexpr T ID = 1e9; // or whatever ID
inline T comb(T a, T b) { return min(a, b); } // or whatever function
int sz;
vector<T> t;
void init(int _sz, T val = ID) {
t.assign((sz = _sz) * 2, ID);
}
void init(vector<T> &v) {
t.resize((sz = v.size()) * 2);
for (int i = 0; i < sz; ++i)
t[i + sz] = v[i];
for (int i = sz - 1; i; --i)
t[i] = comb(t[i * 2], t[(i * 2) | 1]);
}
void upd(int i, T x) {
for (t[i += sz] = x; i > 1; i >>= 1)
t[i >> 1] = comb(t[i], t[i ^ 1]);
}
T query(int l, int r) {
T ql = ID, qr = ID;
for (l += sz, r += sz + 1; l < r; l >>= 1, r >>= 1) {
if (l & 1) ql = comb(ql, t[l++]);
if (r & 1) qr = comb(t[--r], qr);
}
return comb(ql, qr);
}
};
const int N = 2e5 + 7, L = 20;
int n;
int l[N], r[N];
int lift[N][L][2];
int costl[N], costr[N], cost[N];
int bmin(int a, int b) {
return (l[a] < l[b] ? a : b);
}
int bmax(int a, int b) {
return (r[a] > r[b] ? a : b);
}
int main() {
cin.tie(0)->sync_with_stdio(false);
cin >> n;
for (int i = 1; i <= n; i++)
cin >> l[i] >> r[i];
ST<array<array<int, 2>, 2>> st;
st.init(n + 1);
for (int i = 1; i <= n; i++)
st.upd(i, {{{l[i], i}, {r[i], i}}});
for (int i = 1; i <= n; i++) {
lift[i][0][0] = st.query(l[i], r[i])[0][1];
lift[i][0][1] = st.query(l[i], r[i])[1][1];
}
for (int j = 1; j < L; j++) {
for (int i = 1; i <= n; i++) {
lift[i][j][0] = bmin(lift[lift[i][j - 1][0]][j - 1][0], lift[lift[i][j - 1][1]][j - 1][0]);
lift[i][j][1] = bmax(lift[lift[i][j - 1][0]][j - 1][1], lift[lift[i][j - 1][1]][j - 1][1]);
}
}
for (int i = 1; i <= n; i++) {
int cl = i, cr = i, cost = 0;
for (int j = L - 1; ~j; j--) {
int nl = bmin(lift[cl][j][0], lift[cr][j][0]), nr = bmax(lift[cl][j][1], lift[cr][j][1]);
if (l[nl] != 1)
cl = nl, cr = nr, cost += 1 << j;
}
costl[i] = cost + (l[cl] != 1);
}
for (int i = 1; i <= n; i++) {
int cl = i, cr = i, cost = 0;
for (int j = L - 1; ~j; j--) {
int nl = bmin(lift[cl][j][0], lift[cr][j][0]), nr = bmax(lift[cl][j][1], lift[cr][j][1]);
if (r[nr] != n)
cl = nl, cr = nr, cost += 1 << j;
}
costr[i] = cost + (r[cr] != n);
}
ST_MN<int> cost;
cost.init(n + 1);
for (int i = 1; i <= n; i++)
cost.upd(i, costl[i] + costr[i] + 1);
int q;
cin >> q;
while (q--) {
int x;
cin >> x;
int here = cost.query(x, x);
if (here >= (1 << L)) {
cout << -1 << '\n';
continue;
}
auto val = [&](int rep) {
int cl = x, cr = x;
for (int j = 0; j < L; j++) {
if (rep >> j & 1) {
int nl = bmin(lift[cl][j][0], lift[cr][j][0]), nr = bmax(lift[cl][j][1], lift[cr][j][1]);
cl = nl, cr = nr;
}
}
return rep + cost.query(l[cl], r[cr]) + 1;
};
int lo = 0, hi = (1 << L) - 2;
while (lo < hi) {
int mid = (lo + hi) / 2;
if (val(mid) < val(mid + 1))
hi = mid;
else
lo = mid + 1;
}
cout << min(here, val(lo)) << '\n';
}
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
6492 KB |
Output is correct |
2 |
Correct |
1 ms |
6492 KB |
Output is correct |
3 |
Correct |
1 ms |
6492 KB |
Output is correct |
4 |
Correct |
238 ms |
43108 KB |
Output is correct |
5 |
Correct |
184 ms |
43088 KB |
Output is correct |
6 |
Correct |
187 ms |
43092 KB |
Output is correct |
7 |
Correct |
133 ms |
43088 KB |
Output is correct |
8 |
Correct |
117 ms |
37200 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
6492 KB |
Output is correct |
2 |
Correct |
1 ms |
6492 KB |
Output is correct |
3 |
Correct |
1 ms |
6492 KB |
Output is correct |
4 |
Correct |
1 ms |
6492 KB |
Output is correct |
5 |
Correct |
1 ms |
6492 KB |
Output is correct |
6 |
Correct |
1 ms |
6492 KB |
Output is correct |
7 |
Correct |
1 ms |
6492 KB |
Output is correct |
8 |
Correct |
1 ms |
6492 KB |
Output is correct |
9 |
Correct |
1 ms |
6492 KB |
Output is correct |
10 |
Correct |
1 ms |
6492 KB |
Output is correct |
11 |
Correct |
2 ms |
6492 KB |
Output is correct |
12 |
Correct |
1 ms |
6492 KB |
Output is correct |
13 |
Correct |
1 ms |
6492 KB |
Output is correct |
14 |
Correct |
1 ms |
6492 KB |
Output is correct |
15 |
Correct |
1 ms |
6492 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
6492 KB |
Output is correct |
2 |
Correct |
1 ms |
6492 KB |
Output is correct |
3 |
Correct |
1 ms |
6492 KB |
Output is correct |
4 |
Correct |
1 ms |
6492 KB |
Output is correct |
5 |
Correct |
1 ms |
6492 KB |
Output is correct |
6 |
Correct |
1 ms |
6492 KB |
Output is correct |
7 |
Correct |
1 ms |
6492 KB |
Output is correct |
8 |
Correct |
1 ms |
6492 KB |
Output is correct |
9 |
Correct |
1 ms |
6492 KB |
Output is correct |
10 |
Correct |
1 ms |
6492 KB |
Output is correct |
11 |
Correct |
2 ms |
6492 KB |
Output is correct |
12 |
Correct |
1 ms |
6492 KB |
Output is correct |
13 |
Correct |
1 ms |
6492 KB |
Output is correct |
14 |
Correct |
1 ms |
6492 KB |
Output is correct |
15 |
Correct |
1 ms |
6492 KB |
Output is correct |
16 |
Correct |
2 ms |
6492 KB |
Output is correct |
17 |
Correct |
2 ms |
6492 KB |
Output is correct |
18 |
Correct |
2 ms |
6492 KB |
Output is correct |
19 |
Correct |
2 ms |
6492 KB |
Output is correct |
20 |
Correct |
2 ms |
6492 KB |
Output is correct |
21 |
Correct |
2 ms |
6492 KB |
Output is correct |
22 |
Correct |
2 ms |
6492 KB |
Output is correct |
23 |
Correct |
2 ms |
6492 KB |
Output is correct |
24 |
Correct |
2 ms |
6492 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
6492 KB |
Output is correct |
2 |
Correct |
1 ms |
6492 KB |
Output is correct |
3 |
Correct |
1 ms |
6492 KB |
Output is correct |
4 |
Correct |
1 ms |
6492 KB |
Output is correct |
5 |
Correct |
1 ms |
6492 KB |
Output is correct |
6 |
Correct |
1 ms |
6492 KB |
Output is correct |
7 |
Correct |
1 ms |
6492 KB |
Output is correct |
8 |
Correct |
1 ms |
6492 KB |
Output is correct |
9 |
Correct |
1 ms |
6492 KB |
Output is correct |
10 |
Correct |
1 ms |
6492 KB |
Output is correct |
11 |
Correct |
2 ms |
6492 KB |
Output is correct |
12 |
Correct |
1 ms |
6492 KB |
Output is correct |
13 |
Correct |
1 ms |
6492 KB |
Output is correct |
14 |
Correct |
1 ms |
6492 KB |
Output is correct |
15 |
Correct |
1 ms |
6492 KB |
Output is correct |
16 |
Correct |
2 ms |
6492 KB |
Output is correct |
17 |
Correct |
2 ms |
6492 KB |
Output is correct |
18 |
Correct |
2 ms |
6492 KB |
Output is correct |
19 |
Correct |
2 ms |
6492 KB |
Output is correct |
20 |
Correct |
2 ms |
6492 KB |
Output is correct |
21 |
Correct |
2 ms |
6492 KB |
Output is correct |
22 |
Correct |
2 ms |
6492 KB |
Output is correct |
23 |
Correct |
2 ms |
6492 KB |
Output is correct |
24 |
Correct |
2 ms |
6492 KB |
Output is correct |
25 |
Correct |
1 ms |
6488 KB |
Output is correct |
26 |
Correct |
1 ms |
6492 KB |
Output is correct |
27 |
Correct |
8 ms |
6492 KB |
Output is correct |
28 |
Incorrect |
9 ms |
6504 KB |
Output isn't correct |
29 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
6492 KB |
Output is correct |
2 |
Correct |
1 ms |
6492 KB |
Output is correct |
3 |
Correct |
1 ms |
6492 KB |
Output is correct |
4 |
Correct |
238 ms |
43108 KB |
Output is correct |
5 |
Correct |
184 ms |
43088 KB |
Output is correct |
6 |
Correct |
187 ms |
43092 KB |
Output is correct |
7 |
Correct |
133 ms |
43088 KB |
Output is correct |
8 |
Correct |
117 ms |
37200 KB |
Output is correct |
9 |
Correct |
1 ms |
6492 KB |
Output is correct |
10 |
Correct |
1 ms |
6492 KB |
Output is correct |
11 |
Correct |
1 ms |
6492 KB |
Output is correct |
12 |
Correct |
1 ms |
6492 KB |
Output is correct |
13 |
Correct |
1 ms |
6492 KB |
Output is correct |
14 |
Correct |
1 ms |
6492 KB |
Output is correct |
15 |
Correct |
1 ms |
6492 KB |
Output is correct |
16 |
Correct |
1 ms |
6492 KB |
Output is correct |
17 |
Correct |
1 ms |
6492 KB |
Output is correct |
18 |
Correct |
1 ms |
6492 KB |
Output is correct |
19 |
Correct |
2 ms |
6492 KB |
Output is correct |
20 |
Correct |
1 ms |
6492 KB |
Output is correct |
21 |
Correct |
1 ms |
6492 KB |
Output is correct |
22 |
Correct |
1 ms |
6492 KB |
Output is correct |
23 |
Correct |
1 ms |
6492 KB |
Output is correct |
24 |
Correct |
2 ms |
6492 KB |
Output is correct |
25 |
Correct |
2 ms |
6492 KB |
Output is correct |
26 |
Correct |
2 ms |
6492 KB |
Output is correct |
27 |
Correct |
2 ms |
6492 KB |
Output is correct |
28 |
Correct |
2 ms |
6492 KB |
Output is correct |
29 |
Correct |
2 ms |
6492 KB |
Output is correct |
30 |
Correct |
2 ms |
6492 KB |
Output is correct |
31 |
Correct |
2 ms |
6492 KB |
Output is correct |
32 |
Correct |
2 ms |
6492 KB |
Output is correct |
33 |
Correct |
1 ms |
6488 KB |
Output is correct |
34 |
Correct |
1 ms |
6492 KB |
Output is correct |
35 |
Correct |
8 ms |
6492 KB |
Output is correct |
36 |
Incorrect |
9 ms |
6504 KB |
Output isn't correct |
37 |
Halted |
0 ms |
0 KB |
- |