#include <bits/stdc++.h>
#define all(v) v.begin(), v.end()
#define gibon ios::sync_with_stdio(false); cin.tie(0);
#define fi first
#define se second
#define pdd pair<long double, long double>
#define pii pair<int, int>
#define pll pair<ll, ll>
#define ppi pair<pii, pii>
#pragma GCC optimize("O3")
#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")
typedef long long ll;
using namespace std;
const int mxN=200006;
const int mxM=10004;
const int MOD=1e9;
const ll INF=1e18;
int N;
int L[mxN], R[mxN];
vector <pii> v[6*mxN];
int dp[mxN];
int dis[6*mxN];
void input()
{
cin >> N;
for(int i=1;i<=N;i++) cin >> L[i] >> R[i];
}
void init(int idx, int s, int e)
{
if(s==e)
{
v[s].emplace_back(N+idx, 0);
return;
}
v[N+2*idx].emplace_back(N+idx, 0);
v[N+2*idx+1].emplace_back(N+idx, 0);
int mid=(s+e)/2;
init(2*idx, s, mid);
init(2*idx+1, mid+1, e);
}
void make_edge(int idx, int s1, int e1, int s2, int e2, int pos)
{
if(s2>e1 || s1>e2) return;
if(s2<=s1 && e1<=e2)
{
v[N+idx].emplace_back(pos, 1);
return;
}
int mid=(s1+e1)/2;
make_edge(2*idx, s1, mid, s2, e2, pos);
make_edge(2*idx+1, mid+1, e1, s2, e2, pos);
}
void bfs(int src)
{
for(int i=1;i<=6*N+1;i++) dis[i]=MOD;
deque <pii> deq;
deq.emplace_back(src, 0);
while(deq.size())
{
auto [now, x]=deq.front();
deq.pop_front();
if(dis[now]!=MOD) continue;
dis[now]=x;
for(auto [nxt, val] : v[now]) if(dis[nxt]==MOD)
{
if(val==0) deq.emplace_front(nxt, x);
else deq.emplace_back(nxt, x+1);
}
}
}
void make_graph()
{
init(1, 1, N);
for(int i=1;i<=N;i++) make_edge(1, 1, N, L[i], R[i], i);
v[6*N+1].emplace_back(5*N+1, 1);
for(int i=1;i<N;i++) v[5*N+i].emplace_back(5*N+i+1, 1);
}
void solv_dis()
{
bfs(N);
for(int i=1;i<=N;i++) dp[i]+=dis[i];
bfs(1);
for(int i=1;i<=N;i++) dp[i]+=dis[i];
for(int i=2;i<N;i++) dp[i]--;
for(int i=1;i<=N;i++)
{
if(dp[i]<=N) v[5*N+dp[i]].emplace_back(i, 0);
}
bfs(6*N+1);
}
int main()
{
gibon
input();
make_graph();
solv_dis();
int T;
cin >> T;
while(T--)
{
int a;
cin >> a;
cout << (dis[a]>=MOD ? -1 : dis[a]) << '\n';
}
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
13 ms |
28524 KB |
Output is correct |
2 |
Correct |
13 ms |
28500 KB |
Output is correct |
3 |
Correct |
13 ms |
28524 KB |
Output is correct |
4 |
Correct |
588 ms |
106976 KB |
Output is correct |
5 |
Correct |
244 ms |
71648 KB |
Output is correct |
6 |
Correct |
155 ms |
67408 KB |
Output is correct |
7 |
Correct |
105 ms |
67776 KB |
Output is correct |
8 |
Correct |
107 ms |
67564 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
15 ms |
28528 KB |
Output is correct |
2 |
Correct |
16 ms |
28524 KB |
Output is correct |
3 |
Correct |
13 ms |
28444 KB |
Output is correct |
4 |
Correct |
13 ms |
28500 KB |
Output is correct |
5 |
Correct |
14 ms |
28500 KB |
Output is correct |
6 |
Correct |
15 ms |
28420 KB |
Output is correct |
7 |
Correct |
15 ms |
28524 KB |
Output is correct |
8 |
Correct |
14 ms |
28500 KB |
Output is correct |
9 |
Correct |
14 ms |
28448 KB |
Output is correct |
10 |
Correct |
14 ms |
28412 KB |
Output is correct |
11 |
Correct |
14 ms |
28592 KB |
Output is correct |
12 |
Correct |
14 ms |
28500 KB |
Output is correct |
13 |
Correct |
16 ms |
28600 KB |
Output is correct |
14 |
Correct |
14 ms |
28500 KB |
Output is correct |
15 |
Correct |
14 ms |
28528 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
15 ms |
28528 KB |
Output is correct |
2 |
Correct |
16 ms |
28524 KB |
Output is correct |
3 |
Correct |
13 ms |
28444 KB |
Output is correct |
4 |
Correct |
13 ms |
28500 KB |
Output is correct |
5 |
Correct |
14 ms |
28500 KB |
Output is correct |
6 |
Correct |
15 ms |
28420 KB |
Output is correct |
7 |
Correct |
15 ms |
28524 KB |
Output is correct |
8 |
Correct |
14 ms |
28500 KB |
Output is correct |
9 |
Correct |
14 ms |
28448 KB |
Output is correct |
10 |
Correct |
14 ms |
28412 KB |
Output is correct |
11 |
Correct |
14 ms |
28592 KB |
Output is correct |
12 |
Correct |
14 ms |
28500 KB |
Output is correct |
13 |
Correct |
16 ms |
28600 KB |
Output is correct |
14 |
Correct |
14 ms |
28500 KB |
Output is correct |
15 |
Correct |
14 ms |
28528 KB |
Output is correct |
16 |
Correct |
16 ms |
29172 KB |
Output is correct |
17 |
Correct |
17 ms |
28980 KB |
Output is correct |
18 |
Correct |
16 ms |
29228 KB |
Output is correct |
19 |
Correct |
16 ms |
29268 KB |
Output is correct |
20 |
Correct |
15 ms |
28992 KB |
Output is correct |
21 |
Correct |
15 ms |
28920 KB |
Output is correct |
22 |
Correct |
15 ms |
28940 KB |
Output is correct |
23 |
Correct |
16 ms |
29084 KB |
Output is correct |
24 |
Correct |
15 ms |
29140 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
15 ms |
28528 KB |
Output is correct |
2 |
Correct |
16 ms |
28524 KB |
Output is correct |
3 |
Correct |
13 ms |
28444 KB |
Output is correct |
4 |
Correct |
13 ms |
28500 KB |
Output is correct |
5 |
Correct |
14 ms |
28500 KB |
Output is correct |
6 |
Correct |
15 ms |
28420 KB |
Output is correct |
7 |
Correct |
15 ms |
28524 KB |
Output is correct |
8 |
Correct |
14 ms |
28500 KB |
Output is correct |
9 |
Correct |
14 ms |
28448 KB |
Output is correct |
10 |
Correct |
14 ms |
28412 KB |
Output is correct |
11 |
Correct |
14 ms |
28592 KB |
Output is correct |
12 |
Correct |
14 ms |
28500 KB |
Output is correct |
13 |
Correct |
16 ms |
28600 KB |
Output is correct |
14 |
Correct |
14 ms |
28500 KB |
Output is correct |
15 |
Correct |
14 ms |
28528 KB |
Output is correct |
16 |
Correct |
16 ms |
29172 KB |
Output is correct |
17 |
Correct |
17 ms |
28980 KB |
Output is correct |
18 |
Correct |
16 ms |
29228 KB |
Output is correct |
19 |
Correct |
16 ms |
29268 KB |
Output is correct |
20 |
Correct |
15 ms |
28992 KB |
Output is correct |
21 |
Correct |
15 ms |
28920 KB |
Output is correct |
22 |
Correct |
15 ms |
28940 KB |
Output is correct |
23 |
Correct |
16 ms |
29084 KB |
Output is correct |
24 |
Correct |
15 ms |
29140 KB |
Output is correct |
25 |
Correct |
13 ms |
28520 KB |
Output is correct |
26 |
Correct |
13 ms |
28500 KB |
Output is correct |
27 |
Correct |
16 ms |
29268 KB |
Output is correct |
28 |
Correct |
15 ms |
28988 KB |
Output is correct |
29 |
Correct |
15 ms |
28952 KB |
Output is correct |
30 |
Correct |
15 ms |
29052 KB |
Output is correct |
31 |
Correct |
16 ms |
29104 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
13 ms |
28524 KB |
Output is correct |
2 |
Correct |
13 ms |
28500 KB |
Output is correct |
3 |
Correct |
13 ms |
28524 KB |
Output is correct |
4 |
Correct |
588 ms |
106976 KB |
Output is correct |
5 |
Correct |
244 ms |
71648 KB |
Output is correct |
6 |
Correct |
155 ms |
67408 KB |
Output is correct |
7 |
Correct |
105 ms |
67776 KB |
Output is correct |
8 |
Correct |
107 ms |
67564 KB |
Output is correct |
9 |
Correct |
15 ms |
28528 KB |
Output is correct |
10 |
Correct |
16 ms |
28524 KB |
Output is correct |
11 |
Correct |
13 ms |
28444 KB |
Output is correct |
12 |
Correct |
13 ms |
28500 KB |
Output is correct |
13 |
Correct |
14 ms |
28500 KB |
Output is correct |
14 |
Correct |
15 ms |
28420 KB |
Output is correct |
15 |
Correct |
15 ms |
28524 KB |
Output is correct |
16 |
Correct |
14 ms |
28500 KB |
Output is correct |
17 |
Correct |
14 ms |
28448 KB |
Output is correct |
18 |
Correct |
14 ms |
28412 KB |
Output is correct |
19 |
Correct |
14 ms |
28592 KB |
Output is correct |
20 |
Correct |
14 ms |
28500 KB |
Output is correct |
21 |
Correct |
16 ms |
28600 KB |
Output is correct |
22 |
Correct |
14 ms |
28500 KB |
Output is correct |
23 |
Correct |
14 ms |
28528 KB |
Output is correct |
24 |
Correct |
16 ms |
29172 KB |
Output is correct |
25 |
Correct |
17 ms |
28980 KB |
Output is correct |
26 |
Correct |
16 ms |
29228 KB |
Output is correct |
27 |
Correct |
16 ms |
29268 KB |
Output is correct |
28 |
Correct |
15 ms |
28992 KB |
Output is correct |
29 |
Correct |
15 ms |
28920 KB |
Output is correct |
30 |
Correct |
15 ms |
28940 KB |
Output is correct |
31 |
Correct |
16 ms |
29084 KB |
Output is correct |
32 |
Correct |
15 ms |
29140 KB |
Output is correct |
33 |
Correct |
13 ms |
28520 KB |
Output is correct |
34 |
Correct |
13 ms |
28500 KB |
Output is correct |
35 |
Correct |
16 ms |
29268 KB |
Output is correct |
36 |
Correct |
15 ms |
28988 KB |
Output is correct |
37 |
Correct |
15 ms |
28952 KB |
Output is correct |
38 |
Correct |
15 ms |
29052 KB |
Output is correct |
39 |
Correct |
16 ms |
29104 KB |
Output is correct |
40 |
Correct |
651 ms |
109372 KB |
Output is correct |
41 |
Correct |
269 ms |
74632 KB |
Output is correct |
42 |
Correct |
357 ms |
98468 KB |
Output is correct |
43 |
Correct |
345 ms |
101088 KB |
Output is correct |
44 |
Correct |
186 ms |
70160 KB |
Output is correct |
45 |
Correct |
237 ms |
73604 KB |
Output is correct |
46 |
Correct |
95 ms |
46024 KB |
Output is correct |
47 |
Correct |
317 ms |
79880 KB |
Output is correct |
48 |
Correct |
278 ms |
84120 KB |
Output is correct |