#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;
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];
if (nl == cl && nr == cr) {
mv = ans = 1e9;
break;
}
cl = nl, cr = nr;
mv++;
}
ans = min(ans, mv);
cout << (ans < (1 << L) ? ans : -1) << '\n';
}
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
6488 KB |
Output is correct |
2 |
Correct |
1 ms |
6492 KB |
Output is correct |
3 |
Correct |
1 ms |
6492 KB |
Output is correct |
4 |
Correct |
209 ms |
41628 KB |
Output is correct |
5 |
Correct |
1559 ms |
41916 KB |
Output is correct |
6 |
Execution timed out |
2039 ms |
41760 KB |
Time limit exceeded |
7 |
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 |
6572 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 |
6488 KB |
Output is correct |
7 |
Incorrect |
1 ms |
6492 KB |
Output isn't correct |
8 |
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 |
6572 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 |
6488 KB |
Output is correct |
7 |
Incorrect |
1 ms |
6492 KB |
Output isn't correct |
8 |
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 |
6572 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 |
6488 KB |
Output is correct |
7 |
Incorrect |
1 ms |
6492 KB |
Output isn't correct |
8 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
6488 KB |
Output is correct |
2 |
Correct |
1 ms |
6492 KB |
Output is correct |
3 |
Correct |
1 ms |
6492 KB |
Output is correct |
4 |
Correct |
209 ms |
41628 KB |
Output is correct |
5 |
Correct |
1559 ms |
41916 KB |
Output is correct |
6 |
Execution timed out |
2039 ms |
41760 KB |
Time limit exceeded |
7 |
Halted |
0 ms |
0 KB |
- |