This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
using namespace std;
const int maxn = 2500+8;
int n, q, L[maxn], R[maxn];
struct segtree {
int size = 1;
vector<int> seg;
void init(int n) {
while (size < n) size *= 2;
seg.assign(2 * size + 5, 0);
}
void update(int pos, int val, int l, int r, int id) {
if (l == r) {
seg[id] = max(seg[id], val);
return;
}
int mid = (l + r) >> 1;
if (pos <= mid) update(pos, val, l, mid, id * 2);
else update(pos, val, mid + 1, r, id * 2 + 1);
seg[id] = max(seg[id * 2], seg[id * 2 + 1]);
}
int query(int ql, int qr, int l, int r, int id) {
if (ql <= l && r <= qr) return seg[id];
if (qr < l || r < ql) return 0;
int mid = (l + r) >> 1;
return max(query(ql, qr, l, mid, id * 2), query(ql, qr, mid + 1, r, id * 2 + 1));
}
} stmax;
struct segtree2 {
int size = 1;
vector<int> seg;
void init(int n) {
while (size < n) size *= 2;
seg.assign(2 * size + 5, 1e9);
}
void update(int pos, int val, int l, int r, int id) {
if (l == r) {
seg[id] = min(seg[id], val);
return;
}
int mid = (l + r) >> 1;
if (pos <= mid) update(pos, val, l, mid, id * 2);
else update(pos, val, mid + 1, r, id * 2 + 1);
seg[id] = min(seg[id * 2], seg[id * 2 + 1]);
}
int query(int ql, int qr, int l, int r, int id) {
if (ql <= l && r <= qr) return seg[id];
if (qr < l || r < ql) return 1e9;
int mid = (l + r) >> 1;
return min(query(ql, qr, l, mid, id * 2), query(ql, qr, mid + 1, r, id * 2 + 1));
}
} stmin;
struct passport {
int l, r, id;
bool operator < (const passport& o) const {
return id < o.id;
}
};
int dp[maxn][maxn];
int32_t main() {
cin >> n;
for (int i = 1; i <= n; i++) cin >> L[i] >> R[i];
vector<passport> A;
stmax.init(n + 1);
stmin.init(n + 1);
// sort(A.begin(), A.end());
cin >> q;
int x;
cin >> x;
for (int i = 1; i <= n; i++) {
stmin.update(i, L[i], 1, n, 1);
stmax.update(i, R[i], 1, n, 1);
}
for (int i = 1; i <= n; i++) for (int j = 1; j <= n; j++) dp[i][j] = 1e9;
dp[x][x] = 0;
for (int len = 0; len <= n - 1; len++) {
for (int l = 1; l <= n - len; l++) {
int r = l + len;
int rr = stmax.query(l, r, 1, n, 1);
int ll = stmin.query(l, r, 1, n, 1);
if (rr != 1e9) dp[l][rr] = min(dp[l][rr], dp[l][r] + 1);
if (ll != 0) dp[ll][r] = min(dp[ll][r], dp[l][r] + 1);
// cout << l << " " << r << " " << dp[l][r] << "\n";
}
}
if (dp[1][n] != 1e9) cout << dp[1][n] << "\n";
else cout << "-1\n";
bye:{}
}
Compilation message (stderr)
passport.cpp: In function 'int32_t main()':
passport.cpp:94:5: warning: label 'bye' defined but not used [-Wunused-label]
94 | bye:{}
| ^~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |