#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_SEG {
static constexpr T ID = {0, 0}; // or whatever ID
inline T comb(T a, T b) { return max(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_SEG<array<int, 2>> seg;
seg.init(n + 1);
vector<int> ord(n + 1), vals(n + 1);
iota(all(ord), 0);
sort(all(ord), [&](int x, int y) { return l[x] < l[y]; });
priority_queue<array<int, 2>, vector<array<int, 2>>, greater<>> q;
for (int i = 1; i <= n; i++) {
vals[i] = l[ord[i]];
seg.upd(i, {r[ord[i]], i});
cost[i] = costl[i] + costr[i] + 1;
q.push({cost[i], i});
}
while (q.size()) {
auto [c, i] = q.top();
q.pop();
int idx = upper_bound(all(vals), i) - vals.begin() - 1;
while (seg.query(1, idx)[0] >= i) {
int j = seg.query(1, idx)[1];
int x = ord[j];
if (cost[i] + 1 < cost[x]) {
cost[x] = cost[i] + 1;
q.push({cost[x], x});
}
seg.upd(j, {0, 0});
}
}
for (int i = 1; i <= n; i++)
if (cost[i] >= (1 << L))
cost[i] = -1;
int Q;
cin >> Q;
while (Q--) {
int x;
cin >> x;
cout << cost[x] << '\n';
}
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
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 |
400 ms |
48404 KB |
Output is correct |
5 |
Correct |
351 ms |
48576 KB |
Output is correct |
6 |
Correct |
338 ms |
48588 KB |
Output is correct |
7 |
Correct |
272 ms |
48336 KB |
Output is correct |
8 |
Correct |
215 ms |
42148 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
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 |
6488 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 |
2 ms |
6492 KB |
Output is correct |
9 |
Correct |
2 ms |
6492 KB |
Output is correct |
10 |
Correct |
1 ms |
6488 KB |
Output is correct |
11 |
Correct |
1 ms |
6628 KB |
Output is correct |
12 |
Correct |
2 ms |
6492 KB |
Output is correct |
13 |
Correct |
2 ms |
6492 KB |
Output is correct |
14 |
Correct |
2 ms |
6492 KB |
Output is correct |
15 |
Correct |
2 ms |
6488 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
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 |
6488 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 |
2 ms |
6492 KB |
Output is correct |
9 |
Correct |
2 ms |
6492 KB |
Output is correct |
10 |
Correct |
1 ms |
6488 KB |
Output is correct |
11 |
Correct |
1 ms |
6628 KB |
Output is correct |
12 |
Correct |
2 ms |
6492 KB |
Output is correct |
13 |
Correct |
2 ms |
6492 KB |
Output is correct |
14 |
Correct |
2 ms |
6492 KB |
Output is correct |
15 |
Correct |
2 ms |
6488 KB |
Output is correct |
16 |
Correct |
4 ms |
7000 KB |
Output is correct |
17 |
Correct |
4 ms |
6748 KB |
Output is correct |
18 |
Correct |
4 ms |
6748 KB |
Output is correct |
19 |
Correct |
5 ms |
6748 KB |
Output is correct |
20 |
Correct |
4 ms |
6748 KB |
Output is correct |
21 |
Correct |
4 ms |
6748 KB |
Output is correct |
22 |
Correct |
4 ms |
6748 KB |
Output is correct |
23 |
Correct |
4 ms |
6748 KB |
Output is correct |
24 |
Correct |
4 ms |
6820 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
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 |
6488 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 |
2 ms |
6492 KB |
Output is correct |
9 |
Correct |
2 ms |
6492 KB |
Output is correct |
10 |
Correct |
1 ms |
6488 KB |
Output is correct |
11 |
Correct |
1 ms |
6628 KB |
Output is correct |
12 |
Correct |
2 ms |
6492 KB |
Output is correct |
13 |
Correct |
2 ms |
6492 KB |
Output is correct |
14 |
Correct |
2 ms |
6492 KB |
Output is correct |
15 |
Correct |
2 ms |
6488 KB |
Output is correct |
16 |
Correct |
4 ms |
7000 KB |
Output is correct |
17 |
Correct |
4 ms |
6748 KB |
Output is correct |
18 |
Correct |
4 ms |
6748 KB |
Output is correct |
19 |
Correct |
5 ms |
6748 KB |
Output is correct |
20 |
Correct |
4 ms |
6748 KB |
Output is correct |
21 |
Correct |
4 ms |
6748 KB |
Output is correct |
22 |
Correct |
4 ms |
6748 KB |
Output is correct |
23 |
Correct |
4 ms |
6748 KB |
Output is correct |
24 |
Correct |
4 ms |
6820 KB |
Output is correct |
25 |
Correct |
1 ms |
6492 KB |
Output is correct |
26 |
Correct |
1 ms |
6492 KB |
Output is correct |
27 |
Correct |
5 ms |
6748 KB |
Output is correct |
28 |
Correct |
4 ms |
6748 KB |
Output is correct |
29 |
Correct |
4 ms |
6748 KB |
Output is correct |
30 |
Correct |
4 ms |
6748 KB |
Output is correct |
31 |
Correct |
4 ms |
6748 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
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 |
400 ms |
48404 KB |
Output is correct |
5 |
Correct |
351 ms |
48576 KB |
Output is correct |
6 |
Correct |
338 ms |
48588 KB |
Output is correct |
7 |
Correct |
272 ms |
48336 KB |
Output is correct |
8 |
Correct |
215 ms |
42148 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 |
6488 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 |
1 ms |
6488 KB |
Output is correct |
19 |
Correct |
1 ms |
6628 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 |
6488 KB |
Output is correct |
24 |
Correct |
4 ms |
7000 KB |
Output is correct |
25 |
Correct |
4 ms |
6748 KB |
Output is correct |
26 |
Correct |
4 ms |
6748 KB |
Output is correct |
27 |
Correct |
5 ms |
6748 KB |
Output is correct |
28 |
Correct |
4 ms |
6748 KB |
Output is correct |
29 |
Correct |
4 ms |
6748 KB |
Output is correct |
30 |
Correct |
4 ms |
6748 KB |
Output is correct |
31 |
Correct |
4 ms |
6748 KB |
Output is correct |
32 |
Correct |
4 ms |
6820 KB |
Output is correct |
33 |
Correct |
1 ms |
6492 KB |
Output is correct |
34 |
Correct |
1 ms |
6492 KB |
Output is correct |
35 |
Correct |
5 ms |
6748 KB |
Output is correct |
36 |
Correct |
4 ms |
6748 KB |
Output is correct |
37 |
Correct |
4 ms |
6748 KB |
Output is correct |
38 |
Correct |
4 ms |
6748 KB |
Output is correct |
39 |
Correct |
4 ms |
6748 KB |
Output is correct |
40 |
Correct |
448 ms |
51980 KB |
Output is correct |
41 |
Correct |
371 ms |
49352 KB |
Output is correct |
42 |
Correct |
406 ms |
49052 KB |
Output is correct |
43 |
Correct |
375 ms |
49016 KB |
Output is correct |
44 |
Correct |
362 ms |
49552 KB |
Output is correct |
45 |
Correct |
380 ms |
49352 KB |
Output is correct |
46 |
Correct |
112 ms |
22232 KB |
Output is correct |
47 |
Correct |
343 ms |
48588 KB |
Output is correct |
48 |
Correct |
343 ms |
48844 KB |
Output is correct |