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 ar array
//#define int long long
using namespace std;
using ll = long long;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
const int mod = 1e9 + 7;
const int LOG = 20;
const int maxn = 2e5 + 5;
signed main() {
ios_base::sync_with_stdio(false);
cout.tie(0); cin.tie(0);
int n; cin >> n;
vector<int> G[n+1], L(n+1), R(n+1);
for(int i=1; i<=n; i++) {
cin >> L[i] >> R[i];
for(int j=L[i]; j<=R[i]; j++) G[i].push_back(j);
}
int q; cin >> q;
while(q--) {
int s; cin >> s;
int dist[n+1][n+1][n+1], vis[n+1][n+1][n+1];
memset(dist, 0, sizeof(dist));
memset(vis, 0, sizeof(vis));
vis[s][L[s]][R[s]] = 1;
queue<ar<int, 3> > q;
q.push({ s, L[s], R[s] });
while(!q.empty()) {
auto [u, l, r] = q.front(); q.pop();
for(int v : G[u]) {
if(vis[v][min(l, L[v])][max(r, R[v])]) continue;
vis[v][min(l, L[v])][max(r, R[v])] = 1;
dist[v][min(l, L[v])][max(r, R[v])] = dist[u][l][r] + 1;
q.push({ v, min(l, L[v]), max(r, R[v]) });
}
}
int ans = 1e9;
for(int i=1; i<=n; i++)
if(vis[i][1][n]) ans = min(ans, dist[i][1][n]);
cout << (ans == 1e9 ? -1 : ans + 1) << '\n';
}
return 0;
}
# | 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... |