#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);
}
};
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);
}
for (int i = 1; i <= n; i++)
cost[i] = costl[i] + costr[i] + 1;
int q;
cin >> q;
while (q--) {
int x;
cin >> x;
if (cost[x] >= (1 << L)) {
cout << -1 << '\n';
continue;
}
int cl = l[x], cr = r[x], mv = 1, ans = cost[x];
while (cl > 1 || cr < n) {
for (int j = cl; j <= cr; j++)
ans = min(ans, mv + cost[j]);
int nl = st.query(cl, cr)[0][0];
int nr = st.query(cl, cr)[1][0];
cl = nl, cr = nr;
mv++;
}
cout << ans << '\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 |
216 ms |
41552 KB |
Output is correct |
5 |
Correct |
1578 ms |
41812 KB |
Output is correct |
6 |
Execution timed out |
2036 ms |
41808 KB |
Time limit exceeded |
7 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
6488 KB |
Output is correct |
2 |
Correct |
1 ms |
6744 KB |
Output is correct |
3 |
Correct |
1 ms |
6488 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 |
6744 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 |
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 |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
6488 KB |
Output is correct |
2 |
Correct |
1 ms |
6744 KB |
Output is correct |
3 |
Correct |
1 ms |
6488 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 |
6744 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 |
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 |
2 ms |
6492 KB |
Output is correct |
17 |
Correct |
3 ms |
6492 KB |
Output is correct |
18 |
Correct |
3 ms |
6492 KB |
Output is correct |
19 |
Correct |
3 ms |
6492 KB |
Output is correct |
20 |
Correct |
5 ms |
6492 KB |
Output is correct |
21 |
Correct |
5 ms |
6488 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 |
6488 KB |
Output is correct |
2 |
Correct |
1 ms |
6744 KB |
Output is correct |
3 |
Correct |
1 ms |
6488 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 |
6744 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 |
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 |
2 ms |
6492 KB |
Output is correct |
17 |
Correct |
3 ms |
6492 KB |
Output is correct |
18 |
Correct |
3 ms |
6492 KB |
Output is correct |
19 |
Correct |
3 ms |
6492 KB |
Output is correct |
20 |
Correct |
5 ms |
6492 KB |
Output is correct |
21 |
Correct |
5 ms |
6488 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 |
2 ms |
6492 KB |
Output is correct |
26 |
Correct |
1 ms |
6492 KB |
Output is correct |
27 |
Correct |
8 ms |
6492 KB |
Output is correct |
28 |
Correct |
839 ms |
6700 KB |
Output is correct |
29 |
Execution timed out |
2067 ms |
6688 KB |
Time limit exceeded |
30 |
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 |
216 ms |
41552 KB |
Output is correct |
5 |
Correct |
1578 ms |
41812 KB |
Output is correct |
6 |
Execution timed out |
2036 ms |
41808 KB |
Time limit exceeded |
7 |
Halted |
0 ms |
0 KB |
- |