//#pragma GCC optimize("O3,unroll-loops,inline")
#include <bits/stdc++.h>
//#define int long long
#define all(a) a.begin(), a.end()
using namespace std;
constexpr int inf = 1e9;
struct segment_tree_upd_segment_get_point {
vector<int> tree;
int n;
segment_tree_upd_segment_get_point(int n_) {
n = n_;
tree.resize(4 * n, inf);
}
void upd(int v, int tl, int tr, int l, int r, int x) {
if (r < tl || tr < l)
return;
if (l <= tl && tr <= r) {
tree[v] = min(tree[v], x);
return;
}
int tm = (tl + tr) / 2;
upd(v * 2, tl, tm, l, r, x), upd(v * 2 + 1, tm + 1, tr, l, r, x);
}
int get(int v, int tl, int tr, int pos) {
if (tl == tr)
return tree[v];
int tm = (tl + tr) / 2;
if (pos <= tm)
return min(tree[v], get(v * 2, tl, tm, pos));
else
return min(tree[v], get(v * 2 + 1, tm + 1, tr, pos));
}
void upd(int l, int r, int x) {
upd(1, 0, n - 1, l, r, x);
}
int get(int i) {
return get(1, 0, n - 1, i);
}
};
struct fenwick_tree {
vector<int> tree;
int n;
fenwick_tree(int n_) {
n = n_;
tree.resize(n + 1, inf);
}
void upd(int i, int x) {
++i;
for (; i > 0; i -= i & -i)
tree[i] = min(tree[i], x);
}
int get(int i) {
++i;
int x = inf;
for (; i <= n; i += i & -i)
x = min(x, tree[i]);
return x;
}
};
struct segment_tree_upd_point_get_segment {
vector<int> tree;
int n;
segment_tree_upd_point_get_segment(int n_) {
n = n_;
tree.resize(4 * n, inf);
}
void upd(int v, int tl, int tr, int pos, int x) {
if (tl == tr) {
tree[v] = x;
return;
}
int tm = (tl + tr) / 2;
if (pos <= tm)
upd(v * 2, tl, tm, pos, x);
else
upd(v * 2 + 1, tm + 1, tr, pos, x);
tree[v] = min(tree[v * 2], tree[v * 2 + 1]);
}
int get(int v, int tl, int tr, int l, int r) {
if (r < tl || tr < l)
return inf;
if (l <= tl && tr <= r)
return tree[v];
int tm = (tl + tr) / 2;
return min(get(v * 2, tl, tm, l, r), get(v * 2 + 1, tm + 1, tr, l, r));
}
void upd(int pos, int x) {
upd(1, 0, n - 1, pos, x);
}
int get(int l, int r) {
return get(1, 0, n - 1, l, r);
}
};
signed main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
int n;
cin >> n;
vector<pair<int, int>> p(n);
for (auto &i: p)
cin >> i.first >> i.second, --i.first, --i.second;
int q;
cin >> q;
vector<int> k(q);
for (int &i : k)
cin >> i, --i;
if (q == 1 && k[0] == 0) {
multiset<int> now = {1};
vector<vector<int>> upd(n + 1);
upd[p[0].second + 1] = {1};
for (int i = 0; i < n; ++i) {
for (int j: upd[i])
now.extract(j);
if (now.empty()) {
cout << -1;
return 0;
}
int res = *now.begin();
if (i == n - 1)
cout << res;
else {
now.insert(res + 1);
upd[p[i].second + 1].push_back(res + 1);
}
}
return 0;
}
vector<vector<int>> left(n), right(n);
for (int i = 0; i < n; ++i)
left[p[i].first].push_back(i), right[p[i].second].push_back(i);
vector<vector<int>> dp(n, vector<int>(n, inf));
vector<fenwick_tree> dpl(n, fenwick_tree(n));
vector<fenwick_tree> dpr(n, fenwick_tree(n));
segment_tree_upd_point_get_segment mn(n);
dp[0][n - 1] = 0;
dpl[0].upd(n - 1 - (n - 1), 0);
dpr[n - 1].upd(0, 0);
for (int len = n; len >= 1; --len) {
for (int l = 0; l < n; ++l) {
int r = l + len - 1;
if (r >= n)
continue;
dp[l][r] = min(min(dpl[l].get(n - 1 - r), dpr[r].get(l)), mn.get(l, r));
for (int i: left[l]) {
if (i <= r)
dpr[r].upd(i, dp[l][r] + 1);
if (p[i].second == r)
mn.upd(i, dp[l][r] + 1);
}
for (int i: right[r])
if (i >= l)
dpl[l].upd(n - 1 - i, dp[l][r] + 1);
}
}
for (int i : k)
cout << (dp[p[i].first][p[i].second] == inf ? -1 : dp[p[i].first][p[i].second] + 1) << ' ';
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
68 ms |
13840 KB |
Output is correct |
5 |
Correct |
48 ms |
13124 KB |
Output is correct |
6 |
Correct |
50 ms |
15436 KB |
Output is correct |
7 |
Correct |
52 ms |
18120 KB |
Output is correct |
8 |
Correct |
33 ms |
10892 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
452 KB |
Output is correct |
8 |
Correct |
0 ms |
344 KB |
Output is correct |
9 |
Correct |
1 ms |
344 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
11 |
Correct |
7 ms |
1476 KB |
Output is correct |
12 |
Correct |
7 ms |
1372 KB |
Output is correct |
13 |
Correct |
7 ms |
1572 KB |
Output is correct |
14 |
Correct |
7 ms |
1368 KB |
Output is correct |
15 |
Correct |
7 ms |
1480 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
452 KB |
Output is correct |
8 |
Correct |
0 ms |
344 KB |
Output is correct |
9 |
Correct |
1 ms |
344 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
11 |
Correct |
7 ms |
1476 KB |
Output is correct |
12 |
Correct |
7 ms |
1372 KB |
Output is correct |
13 |
Correct |
7 ms |
1572 KB |
Output is correct |
14 |
Correct |
7 ms |
1368 KB |
Output is correct |
15 |
Correct |
7 ms |
1480 KB |
Output is correct |
16 |
Correct |
726 ms |
65128 KB |
Output is correct |
17 |
Correct |
733 ms |
73808 KB |
Output is correct |
18 |
Correct |
743 ms |
74432 KB |
Output is correct |
19 |
Correct |
761 ms |
70508 KB |
Output is correct |
20 |
Correct |
750 ms |
74516 KB |
Output is correct |
21 |
Correct |
729 ms |
74496 KB |
Output is correct |
22 |
Correct |
706 ms |
74408 KB |
Output is correct |
23 |
Correct |
739 ms |
74580 KB |
Output is correct |
24 |
Correct |
761 ms |
74444 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
452 KB |
Output is correct |
8 |
Correct |
0 ms |
344 KB |
Output is correct |
9 |
Correct |
1 ms |
344 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
11 |
Correct |
7 ms |
1476 KB |
Output is correct |
12 |
Correct |
7 ms |
1372 KB |
Output is correct |
13 |
Correct |
7 ms |
1572 KB |
Output is correct |
14 |
Correct |
7 ms |
1368 KB |
Output is correct |
15 |
Correct |
7 ms |
1480 KB |
Output is correct |
16 |
Correct |
726 ms |
65128 KB |
Output is correct |
17 |
Correct |
733 ms |
73808 KB |
Output is correct |
18 |
Correct |
743 ms |
74432 KB |
Output is correct |
19 |
Correct |
761 ms |
70508 KB |
Output is correct |
20 |
Correct |
750 ms |
74516 KB |
Output is correct |
21 |
Correct |
729 ms |
74496 KB |
Output is correct |
22 |
Correct |
706 ms |
74408 KB |
Output is correct |
23 |
Correct |
739 ms |
74580 KB |
Output is correct |
24 |
Correct |
761 ms |
74444 KB |
Output is correct |
25 |
Correct |
1 ms |
348 KB |
Output is correct |
26 |
Correct |
0 ms |
348 KB |
Output is correct |
27 |
Correct |
753 ms |
69800 KB |
Output is correct |
28 |
Correct |
767 ms |
74324 KB |
Output is correct |
29 |
Correct |
748 ms |
74576 KB |
Output is correct |
30 |
Correct |
735 ms |
74520 KB |
Output is correct |
31 |
Correct |
694 ms |
69976 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
68 ms |
13840 KB |
Output is correct |
5 |
Correct |
48 ms |
13124 KB |
Output is correct |
6 |
Correct |
50 ms |
15436 KB |
Output is correct |
7 |
Correct |
52 ms |
18120 KB |
Output is correct |
8 |
Correct |
33 ms |
10892 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
1 ms |
348 KB |
Output is correct |
13 |
Correct |
1 ms |
348 KB |
Output is correct |
14 |
Correct |
1 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
452 KB |
Output is correct |
16 |
Correct |
0 ms |
344 KB |
Output is correct |
17 |
Correct |
1 ms |
344 KB |
Output is correct |
18 |
Correct |
1 ms |
348 KB |
Output is correct |
19 |
Correct |
7 ms |
1476 KB |
Output is correct |
20 |
Correct |
7 ms |
1372 KB |
Output is correct |
21 |
Correct |
7 ms |
1572 KB |
Output is correct |
22 |
Correct |
7 ms |
1368 KB |
Output is correct |
23 |
Correct |
7 ms |
1480 KB |
Output is correct |
24 |
Correct |
726 ms |
65128 KB |
Output is correct |
25 |
Correct |
733 ms |
73808 KB |
Output is correct |
26 |
Correct |
743 ms |
74432 KB |
Output is correct |
27 |
Correct |
761 ms |
70508 KB |
Output is correct |
28 |
Correct |
750 ms |
74516 KB |
Output is correct |
29 |
Correct |
729 ms |
74496 KB |
Output is correct |
30 |
Correct |
706 ms |
74408 KB |
Output is correct |
31 |
Correct |
739 ms |
74580 KB |
Output is correct |
32 |
Correct |
761 ms |
74444 KB |
Output is correct |
33 |
Correct |
1 ms |
348 KB |
Output is correct |
34 |
Correct |
0 ms |
348 KB |
Output is correct |
35 |
Correct |
753 ms |
69800 KB |
Output is correct |
36 |
Correct |
767 ms |
74324 KB |
Output is correct |
37 |
Correct |
748 ms |
74576 KB |
Output is correct |
38 |
Correct |
735 ms |
74520 KB |
Output is correct |
39 |
Correct |
694 ms |
69976 KB |
Output is correct |
40 |
Runtime error |
871 ms |
1048576 KB |
Execution killed with signal 9 |
41 |
Halted |
0 ms |
0 KB |
- |