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>
#define ll long long
#define ld long double
#define sp ' '
#define en '\n'
#define smin(a, b) a = min(a, b)
#define smax(a, b) a = max(a, b)
using namespace std;
const int N = 2500 + 2;
const int M = 2e5 + 2;
const int inf = 1e9;
int l[M], r[M], dp[N][N];
vector<int> g[N], ig[N];
void Uradi(int x) {
queue<int> q;
q.push(x);
dp[x][x] = 0;
while (!q.empty()) {
int s = q.front(); q.pop();
for (int u : g[s]) {
if (dp[x][u] > dp[x][s] + 1) {
q.push(u);
dp[x][u] = dp[x][s] + 1;
}
}
}
}
void Kontra(int x) {
queue<int> q;
q.push(x);
dp[x][x] = 0;
while (!q.empty()) {
int s = q.front(); q.pop();
for (int u : ig[s]) {
if (dp[u][x] > dp[s][x] + 1) {
q.push(u);
dp[u][x] = dp[s][x] + 1;
}
}
}
}
int st[4 * M], dpp[M];
void Add(int node, int l, int r, int x, int y) {
smin(st[node], y);
if (l == r) return;
int mid = l + r >> 1;
if (x <= mid) Add(2 * node, l, mid, x, y);
else Add(2 * node + 1, mid + 1, r, x, y);
}
int Get(int node, int l, int r, int ql, int qr) {
if (r < ql || qr < l) return inf;
if (ql <= l && r <= qr) return st[node];
int mid = l + r >> 1;
return min(Get(2 * node, l, mid, ql, qr), Get(2 * node + 1, mid + 1, r, ql, qr));
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int n;
cin >> n;
for (int i = 1; i <= n; i++) {
cin >> l[i] >> r[i];
}
int qqq, x;
cin >> qqq >> x;
if (x == 1) {
for (int i = 1; i < 4 * M; i++) st[i] = inf;
for (int i = n; i >= 1; i--) {
if (r[i] == n) dpp[i] = 1;
else dpp[i] = Get(1, 1, n, i, r[i]) + 1;
smin(dpp[i], inf);
Add(1, 1, n, i, dpp[i]);
}
if (dpp[1] == inf) dpp[1] = -1;
cout << dpp[1] << en;
return 0;
}
for (int i = 1; i <= n; i++) for (int j = 1; j <= n; j++) dp[i][j] = inf;
for (int i = 1; i <= n; i++) {
for (int j = l[i]; j < i; j++) {
g[i].push_back(j);
ig[j].push_back(i);
}
for (int j = i + 1; j <= r[i]; j++) {
g[i].push_back(j);
ig[j].push_back(i);
}
}
Uradi(x);
Kontra(1);
Kontra(n);
int ans = inf;
if (x == 1 || x == n) {
if (dp[x][n + 1 - x] == inf) dp[x][n + 1 - x] = -1;
cout << dp[x][n + 1 - x] << en;
return 0;
}
for (int i = 2; i < n; i++) smin(ans, dp[x][i] + dp[i][1] + dp[i][n] - 1);
smin(ans, dp[x][1] + dp[1][n]);
smin(ans, dp[x][n] + dp[n][1]);
if (ans == inf) ans = -1;
cout << ans << en;
return 0;
}
Compilation message (stderr)
passport.cpp: In function 'void Add(int, int, int, int, int)':
passport.cpp:46:17: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
46 | int mid = l + r >> 1;
| ~~^~~
passport.cpp: In function 'int Get(int, int, int, int, int)':
passport.cpp:53:17: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
53 | int mid = l + r >> 1;
| ~~^~~
# | 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... |