#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;
vector <pll> A[maxn*4];
priority_queue <pll, vector <pll>, greater<pll>> q, q1, q2;
ll le[maxn*4], ri[maxn*4], L[maxn*4], R[maxn*4], dis[maxn*4], idx[maxn*4];
void build(ll i, ll Start, ll End)
{
le[i]=ri[i]=dis[i]=inf;
if (Start==End)
{
idx[Start]=i;
return;
}
ll mid=(Start+End)/2;
build(i*2, Start, mid);
build(i*2+1, mid+1, End);
A[i*2].pb({i, 0}); A[i*2+1].pb({i, 0});
}
void push(ll i, ll Start, ll End, ll l, ll r, ll p)
{
if (Start>r || End<l)
return;
if (Start>=l && End<=r)
{
A[i].pb({idx[p], 1});
return;
}
ll mid=(Start+End)/2;
if (mid>=l) push(i*2, Start, mid, l, r, p);
if (mid+1<=r) push(i*2+1, mid+1, End, l, r, p);
}
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];
build(1, 1, n);
for (ll i=1; i<=n; i++)
push(1, 1, n, L[i], R[i], i);
for (ll i=1; i<=n; i++)
{
ll id=idx[i];
if (L[i]==1) le[id]=0, q1.push({0, id});
if (R[i]==n) ri[id]=0, q2.push({0, id});
}
while (q1.size())
{
ll u=q1.top().se, leu=q1.top().fi; q1.pop();
if (leu!=le[u]) continue;
for (pll child:A[u])
{
ll v=child.fi, w=child.se;
if (le[v]>leu+w)
le[v]=leu+w, q1.push({le[v], v});
}
}
while (q2.size())
{
ll u=q2.top().se, riu=q2.top().fi; q2.pop();
if (riu!=ri[u]) continue;
for (pll child:A[u])
{
ll v=child.fi, w=child.se;
if (ri[v]>riu+w)
ri[v]=riu+w, q2.push({ri[v], v});
}
}
for (ll i=1; i<=n; i++)
A[0].pb({i, le[i]+ri[i]});
q.push({1, 0}); dis[0]=1;
while (q.size())
{
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[idx[x]]>=inf) cout << "-1\n";
else cout << dis[idx[x]] << "\n";
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
29532 KB |
Output is correct |
2 |
Correct |
6 ms |
29632 KB |
Output is correct |
3 |
Correct |
6 ms |
29532 KB |
Output is correct |
4 |
Correct |
579 ms |
86064 KB |
Output is correct |
5 |
Correct |
262 ms |
63904 KB |
Output is correct |
6 |
Correct |
151 ms |
58564 KB |
Output is correct |
7 |
Incorrect |
179 ms |
60740 KB |
Output isn't correct |
8 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
29532 KB |
Output is correct |
2 |
Correct |
6 ms |
29532 KB |
Output is correct |
3 |
Correct |
6 ms |
29532 KB |
Output is correct |
4 |
Correct |
6 ms |
29532 KB |
Output is correct |
5 |
Incorrect |
5 ms |
29532 KB |
Output isn't correct |
6 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
29532 KB |
Output is correct |
2 |
Correct |
6 ms |
29532 KB |
Output is correct |
3 |
Correct |
6 ms |
29532 KB |
Output is correct |
4 |
Correct |
6 ms |
29532 KB |
Output is correct |
5 |
Incorrect |
5 ms |
29532 KB |
Output isn't correct |
6 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
29532 KB |
Output is correct |
2 |
Correct |
6 ms |
29532 KB |
Output is correct |
3 |
Correct |
6 ms |
29532 KB |
Output is correct |
4 |
Correct |
6 ms |
29532 KB |
Output is correct |
5 |
Incorrect |
5 ms |
29532 KB |
Output isn't correct |
6 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
29532 KB |
Output is correct |
2 |
Correct |
6 ms |
29632 KB |
Output is correct |
3 |
Correct |
6 ms |
29532 KB |
Output is correct |
4 |
Correct |
579 ms |
86064 KB |
Output is correct |
5 |
Correct |
262 ms |
63904 KB |
Output is correct |
6 |
Correct |
151 ms |
58564 KB |
Output is correct |
7 |
Incorrect |
179 ms |
60740 KB |
Output isn't correct |
8 |
Halted |
0 ms |
0 KB |
- |