#include<bits/stdc++.h>
#define ll int
#define pll pair<ll, ll>
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define ld long double
using namespace std;
const ll maxn=200005, inf=1e9;
ll le[maxn], ri[maxn], L[maxn], R[maxn], dis[maxn];
priority_queue <pll, vector <pll>, greater<pll>> q;
vector <pll> A[maxn];
int main()
{
ios_base::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
ll n; cin >> n;
for (ll i=1; i<=n; i++)
{
cin >> L[i] >> R[i];
le[i]=ri[i]=dis[i]=inf;
}
for (ll i=1; i<=n; i++)
for (ll j=L[i]; j<=R[i]; j++)
A[j].pb({i, 1});
queue <ll> q1, q2;
for (ll i=1; i<=n; i++)
{
if (L[i]==1) le[i]=0, q1.push(i);
if (R[i]==n) ri[i]=0, q2.push(i);
}
while (q1.size())
{
ll u=q1.front(); q1.pop();
for (pll v:A[u])
if (le[v.fi]==inf)
le[v.fi]=le[u]+1, q1.push(v.fi);
}
while (q2.size())
{
ll u=q2.front(); q2.pop();
for (pll v:A[u])
if (ri[v.fi]==inf)
ri[v.fi]=ri[u]+1, q2.push(v.fi);
}
for (ll i=1; i<=n; i++)
A[n+1].pb({i, le[i]+ri[i]});
dis[n+1]=0; q.push({0, n+1});
while (!q.empty())
{
ll u=q.top().se, disu=q.top().fi; q.pop();
if (disu!=dis[u])
continue;
for (pll child:A[u])
{
ll v=child.fi, w=child.se;
if (dis[v]>disu+w)
{
dis[v]=disu+w;
q.push({dis[v], v});
}
}
}
ll q; cin >> q;
for (ll i=1; i<=q; i++)
{
ll x; cin >> x;
if (dis[x]==inf) cout << "-1\n";
else cout << dis[x]+1 << "\n";
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
8796 KB |
Output is correct |
2 |
Correct |
1 ms |
8796 KB |
Output is correct |
3 |
Correct |
1 ms |
8792 KB |
Output is correct |
4 |
Execution timed out |
2139 ms |
1022692 KB |
Time limit exceeded |
5 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
8796 KB |
Output is correct |
2 |
Correct |
2 ms |
8796 KB |
Output is correct |
3 |
Correct |
2 ms |
9048 KB |
Output is correct |
4 |
Correct |
2 ms |
8796 KB |
Output is correct |
5 |
Correct |
2 ms |
8796 KB |
Output is correct |
6 |
Correct |
1 ms |
8796 KB |
Output is correct |
7 |
Correct |
2 ms |
8796 KB |
Output is correct |
8 |
Correct |
1 ms |
8796 KB |
Output is correct |
9 |
Correct |
2 ms |
8796 KB |
Output is correct |
10 |
Correct |
1 ms |
8840 KB |
Output is correct |
11 |
Correct |
2 ms |
9052 KB |
Output is correct |
12 |
Correct |
2 ms |
8844 KB |
Output is correct |
13 |
Correct |
3 ms |
9308 KB |
Output is correct |
14 |
Correct |
3 ms |
9308 KB |
Output is correct |
15 |
Correct |
2 ms |
8796 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
8796 KB |
Output is correct |
2 |
Correct |
2 ms |
8796 KB |
Output is correct |
3 |
Correct |
2 ms |
9048 KB |
Output is correct |
4 |
Correct |
2 ms |
8796 KB |
Output is correct |
5 |
Correct |
2 ms |
8796 KB |
Output is correct |
6 |
Correct |
1 ms |
8796 KB |
Output is correct |
7 |
Correct |
2 ms |
8796 KB |
Output is correct |
8 |
Correct |
1 ms |
8796 KB |
Output is correct |
9 |
Correct |
2 ms |
8796 KB |
Output is correct |
10 |
Correct |
1 ms |
8840 KB |
Output is correct |
11 |
Correct |
2 ms |
9052 KB |
Output is correct |
12 |
Correct |
2 ms |
8844 KB |
Output is correct |
13 |
Correct |
3 ms |
9308 KB |
Output is correct |
14 |
Correct |
3 ms |
9308 KB |
Output is correct |
15 |
Correct |
2 ms |
8796 KB |
Output is correct |
16 |
Correct |
25 ms |
25180 KB |
Output is correct |
17 |
Correct |
3 ms |
9112 KB |
Output is correct |
18 |
Correct |
56 ms |
41600 KB |
Output is correct |
19 |
Correct |
56 ms |
39672 KB |
Output is correct |
20 |
Correct |
2 ms |
9048 KB |
Output is correct |
21 |
Correct |
9 ms |
13524 KB |
Output is correct |
22 |
Correct |
50 ms |
46288 KB |
Output is correct |
23 |
Correct |
44 ms |
35668 KB |
Output is correct |
24 |
Correct |
42 ms |
36568 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
8796 KB |
Output is correct |
2 |
Correct |
2 ms |
8796 KB |
Output is correct |
3 |
Correct |
2 ms |
9048 KB |
Output is correct |
4 |
Correct |
2 ms |
8796 KB |
Output is correct |
5 |
Correct |
2 ms |
8796 KB |
Output is correct |
6 |
Correct |
1 ms |
8796 KB |
Output is correct |
7 |
Correct |
2 ms |
8796 KB |
Output is correct |
8 |
Correct |
1 ms |
8796 KB |
Output is correct |
9 |
Correct |
2 ms |
8796 KB |
Output is correct |
10 |
Correct |
1 ms |
8840 KB |
Output is correct |
11 |
Correct |
2 ms |
9052 KB |
Output is correct |
12 |
Correct |
2 ms |
8844 KB |
Output is correct |
13 |
Correct |
3 ms |
9308 KB |
Output is correct |
14 |
Correct |
3 ms |
9308 KB |
Output is correct |
15 |
Correct |
2 ms |
8796 KB |
Output is correct |
16 |
Correct |
25 ms |
25180 KB |
Output is correct |
17 |
Correct |
3 ms |
9112 KB |
Output is correct |
18 |
Correct |
56 ms |
41600 KB |
Output is correct |
19 |
Correct |
56 ms |
39672 KB |
Output is correct |
20 |
Correct |
2 ms |
9048 KB |
Output is correct |
21 |
Correct |
9 ms |
13524 KB |
Output is correct |
22 |
Correct |
50 ms |
46288 KB |
Output is correct |
23 |
Correct |
44 ms |
35668 KB |
Output is correct |
24 |
Correct |
42 ms |
36568 KB |
Output is correct |
25 |
Correct |
2 ms |
8792 KB |
Output is correct |
26 |
Correct |
1 ms |
8796 KB |
Output is correct |
27 |
Correct |
28 ms |
26288 KB |
Output is correct |
28 |
Correct |
3 ms |
9308 KB |
Output is correct |
29 |
Correct |
3 ms |
9052 KB |
Output is correct |
30 |
Correct |
9 ms |
13148 KB |
Output is correct |
31 |
Correct |
29 ms |
28816 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
8796 KB |
Output is correct |
2 |
Correct |
1 ms |
8796 KB |
Output is correct |
3 |
Correct |
1 ms |
8792 KB |
Output is correct |
4 |
Execution timed out |
2139 ms |
1022692 KB |
Time limit exceeded |
5 |
Halted |
0 ms |
0 KB |
- |