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;
typedef int64_t ll;
#define all(x) x.begin(), x.end()
#define size(x) (int)x.size()
template<class S, class T>
bool chmin(S &a, const T &b) {
return (a > b ? (a = b) == b : false);
}
template<class S, class T>
bool chmax(S &a, const T &b) {
return (a < b ? (a = b) == b : false);
}
const int inf = 1e9;
const ll INF = 1e18;
const int mod = 998244353;
const int N = 2500;
int n, l[N], r[N];
vector<vector<int>> d(N, vector<int> (N, inf));
void bfs(int init) {
queue<pair<int, int>> q;
d[l[init]][r[init]] = 1;
q.push({l[init], r[init]});
while (!q.empty()) {
auto [L, R] = q.front();
q.pop();
for (int i = L; i <= R; ++i) {
int new_l = min(L, l[i]), new_r = max(R, r[i]);
if (chmin(d[new_l][new_r], d[L][R] + 1)) {
q.push({new_l, new_r});
}
}
}
}
signed main() {
cin.tie(nullptr)->sync_with_stdio(false);
cin >> n;
for (int i = 0; i < n; ++i) {
cin >> l[i] >> r[i];
l[i]--, r[i]--;
}
int q;
cin >> q;
while (q--) {
int x;
cin >> x;
bfs(--x);
cout << (d[0][n - 1] == inf ? -1 : d[0][n - 1]) << '\n';
}
}
Compilation message (stderr)
passport.cpp: In function 'void bfs(int)':
passport.cpp:28:14: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
28 | auto [L, R] = q.front();
| ^
# | 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... |