#include <bits/stdc++.h>
using namespace std;
const int nx=1e6+5;
int n, l[nx], r[nx], q, x, mn=INT_MAX;
vector<pair<int, int>> d[nx];
vector<int> vs(nx), st(nx, 1e9), ed(nx, 1e9), ans(nx, 1e9);
priority_queue<pair<int, int>, vector<pair<int, int>>, greater<pair<int, int>>> pq;
void build(int l, int r, int i)
{
if (l==r) return void(d[l].push_back({i+n, 0}));
int md=(l+r)/2;
d[2*i+n].push_back({i+n, 0});
d[2*i+n+1].push_back({i+n, 0});
build(l, md, 2*i);
build(md+1, r, 2*i+1);
}
void update(int l, int r, int i, int ql, int qr, int idx)
{
if (ql<=l&&r<=qr) return void(d[i+n].push_back({idx, 1}));
if (r<ql||qr<l) return;
int md=(l+r)/2;
update(l, md, 2*i, ql, qr, idx);
update(md+1, r, 2*i+1, ql, qr, idx);
}
void dijk(int st, vector<int> &res)
{
for (int i=0; i<nx; i++) vs[i]=0;
pq.push({0, st});
res[st]=0;
while (!pq.empty())
{
auto [cw, u]=pq.top();
pq.pop();
vs[u]=1;
for (auto [v, w]:d[u]) if (!vs[v]&&cw+w<res[v]) res[v]=cw+w, pq.push({cw+w, v});
}
}
int main()
{
cin.tie(NULL)->sync_with_stdio(false);
cin>>n;
build(1, n, 1);
for (int i=1; i<=n; i++) cin>>l[i]>>r[i], update(1, n, 1, l[i], r[i], i);
dijk(1, st);
dijk(n, ed);
for (int i=1; i<nx; i++) vs[i]=0;
for (int i=1; i<=n; i++) ans[i]=st[i]+ed[i]-(i>1&&i<n), pq.push({ans[i], i});
while (!pq.empty())
{
auto [cw, u]=pq.top();
pq.pop();
vs[u]=1;
for (auto [v, w]:d[u]) if (!vs[v]&&cw+w<ans[v]) ans[v]=cw+w, pq.push({cw+w, v});
}
cin>>q;
while (q--) cin>>x, cout<<(ans[x]>=1e9?-1:ans[x])<<'\n';
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
11 ms |
42840 KB |
Output is correct |
2 |
Correct |
11 ms |
43100 KB |
Output is correct |
3 |
Correct |
11 ms |
42888 KB |
Output is correct |
4 |
Correct |
690 ms |
92388 KB |
Output is correct |
5 |
Correct |
330 ms |
72604 KB |
Output is correct |
6 |
Correct |
199 ms |
66932 KB |
Output is correct |
7 |
Correct |
256 ms |
65748 KB |
Output is correct |
8 |
Correct |
144 ms |
71292 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
10 ms |
42844 KB |
Output is correct |
2 |
Correct |
10 ms |
42840 KB |
Output is correct |
3 |
Correct |
11 ms |
42844 KB |
Output is correct |
4 |
Correct |
11 ms |
42844 KB |
Output is correct |
5 |
Correct |
11 ms |
42844 KB |
Output is correct |
6 |
Correct |
11 ms |
42844 KB |
Output is correct |
7 |
Correct |
11 ms |
42844 KB |
Output is correct |
8 |
Correct |
10 ms |
42844 KB |
Output is correct |
9 |
Correct |
11 ms |
42844 KB |
Output is correct |
10 |
Correct |
11 ms |
42844 KB |
Output is correct |
11 |
Correct |
12 ms |
42852 KB |
Output is correct |
12 |
Correct |
12 ms |
43096 KB |
Output is correct |
13 |
Correct |
11 ms |
43028 KB |
Output is correct |
14 |
Correct |
11 ms |
42860 KB |
Output is correct |
15 |
Correct |
12 ms |
42956 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
10 ms |
42844 KB |
Output is correct |
2 |
Correct |
10 ms |
42840 KB |
Output is correct |
3 |
Correct |
11 ms |
42844 KB |
Output is correct |
4 |
Correct |
11 ms |
42844 KB |
Output is correct |
5 |
Correct |
11 ms |
42844 KB |
Output is correct |
6 |
Correct |
11 ms |
42844 KB |
Output is correct |
7 |
Correct |
11 ms |
42844 KB |
Output is correct |
8 |
Correct |
10 ms |
42844 KB |
Output is correct |
9 |
Correct |
11 ms |
42844 KB |
Output is correct |
10 |
Correct |
11 ms |
42844 KB |
Output is correct |
11 |
Correct |
12 ms |
42852 KB |
Output is correct |
12 |
Correct |
12 ms |
43096 KB |
Output is correct |
13 |
Correct |
11 ms |
43028 KB |
Output is correct |
14 |
Correct |
11 ms |
42860 KB |
Output is correct |
15 |
Correct |
12 ms |
42956 KB |
Output is correct |
16 |
Correct |
15 ms |
43356 KB |
Output is correct |
17 |
Correct |
14 ms |
43356 KB |
Output is correct |
18 |
Correct |
14 ms |
43588 KB |
Output is correct |
19 |
Correct |
15 ms |
43396 KB |
Output is correct |
20 |
Correct |
12 ms |
43100 KB |
Output is correct |
21 |
Correct |
13 ms |
43352 KB |
Output is correct |
22 |
Correct |
13 ms |
43100 KB |
Output is correct |
23 |
Correct |
15 ms |
43612 KB |
Output is correct |
24 |
Correct |
14 ms |
43368 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
10 ms |
42844 KB |
Output is correct |
2 |
Correct |
10 ms |
42840 KB |
Output is correct |
3 |
Correct |
11 ms |
42844 KB |
Output is correct |
4 |
Correct |
11 ms |
42844 KB |
Output is correct |
5 |
Correct |
11 ms |
42844 KB |
Output is correct |
6 |
Correct |
11 ms |
42844 KB |
Output is correct |
7 |
Correct |
11 ms |
42844 KB |
Output is correct |
8 |
Correct |
10 ms |
42844 KB |
Output is correct |
9 |
Correct |
11 ms |
42844 KB |
Output is correct |
10 |
Correct |
11 ms |
42844 KB |
Output is correct |
11 |
Correct |
12 ms |
42852 KB |
Output is correct |
12 |
Correct |
12 ms |
43096 KB |
Output is correct |
13 |
Correct |
11 ms |
43028 KB |
Output is correct |
14 |
Correct |
11 ms |
42860 KB |
Output is correct |
15 |
Correct |
12 ms |
42956 KB |
Output is correct |
16 |
Correct |
15 ms |
43356 KB |
Output is correct |
17 |
Correct |
14 ms |
43356 KB |
Output is correct |
18 |
Correct |
14 ms |
43588 KB |
Output is correct |
19 |
Correct |
15 ms |
43396 KB |
Output is correct |
20 |
Correct |
12 ms |
43100 KB |
Output is correct |
21 |
Correct |
13 ms |
43352 KB |
Output is correct |
22 |
Correct |
13 ms |
43100 KB |
Output is correct |
23 |
Correct |
15 ms |
43612 KB |
Output is correct |
24 |
Correct |
14 ms |
43368 KB |
Output is correct |
25 |
Correct |
11 ms |
42844 KB |
Output is correct |
26 |
Correct |
11 ms |
42728 KB |
Output is correct |
27 |
Correct |
15 ms |
43352 KB |
Output is correct |
28 |
Correct |
14 ms |
43352 KB |
Output is correct |
29 |
Correct |
13 ms |
43100 KB |
Output is correct |
30 |
Correct |
14 ms |
43356 KB |
Output is correct |
31 |
Correct |
14 ms |
43356 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
11 ms |
42840 KB |
Output is correct |
2 |
Correct |
11 ms |
43100 KB |
Output is correct |
3 |
Correct |
11 ms |
42888 KB |
Output is correct |
4 |
Correct |
690 ms |
92388 KB |
Output is correct |
5 |
Correct |
330 ms |
72604 KB |
Output is correct |
6 |
Correct |
199 ms |
66932 KB |
Output is correct |
7 |
Correct |
256 ms |
65748 KB |
Output is correct |
8 |
Correct |
144 ms |
71292 KB |
Output is correct |
9 |
Correct |
10 ms |
42844 KB |
Output is correct |
10 |
Correct |
10 ms |
42840 KB |
Output is correct |
11 |
Correct |
11 ms |
42844 KB |
Output is correct |
12 |
Correct |
11 ms |
42844 KB |
Output is correct |
13 |
Correct |
11 ms |
42844 KB |
Output is correct |
14 |
Correct |
11 ms |
42844 KB |
Output is correct |
15 |
Correct |
11 ms |
42844 KB |
Output is correct |
16 |
Correct |
10 ms |
42844 KB |
Output is correct |
17 |
Correct |
11 ms |
42844 KB |
Output is correct |
18 |
Correct |
11 ms |
42844 KB |
Output is correct |
19 |
Correct |
12 ms |
42852 KB |
Output is correct |
20 |
Correct |
12 ms |
43096 KB |
Output is correct |
21 |
Correct |
11 ms |
43028 KB |
Output is correct |
22 |
Correct |
11 ms |
42860 KB |
Output is correct |
23 |
Correct |
12 ms |
42956 KB |
Output is correct |
24 |
Correct |
15 ms |
43356 KB |
Output is correct |
25 |
Correct |
14 ms |
43356 KB |
Output is correct |
26 |
Correct |
14 ms |
43588 KB |
Output is correct |
27 |
Correct |
15 ms |
43396 KB |
Output is correct |
28 |
Correct |
12 ms |
43100 KB |
Output is correct |
29 |
Correct |
13 ms |
43352 KB |
Output is correct |
30 |
Correct |
13 ms |
43100 KB |
Output is correct |
31 |
Correct |
15 ms |
43612 KB |
Output is correct |
32 |
Correct |
14 ms |
43368 KB |
Output is correct |
33 |
Correct |
11 ms |
42844 KB |
Output is correct |
34 |
Correct |
11 ms |
42728 KB |
Output is correct |
35 |
Correct |
15 ms |
43352 KB |
Output is correct |
36 |
Correct |
14 ms |
43352 KB |
Output is correct |
37 |
Correct |
13 ms |
43100 KB |
Output is correct |
38 |
Correct |
14 ms |
43356 KB |
Output is correct |
39 |
Correct |
14 ms |
43356 KB |
Output is correct |
40 |
Correct |
728 ms |
94956 KB |
Output is correct |
41 |
Correct |
364 ms |
73616 KB |
Output is correct |
42 |
Correct |
444 ms |
99876 KB |
Output is correct |
43 |
Correct |
456 ms |
99876 KB |
Output is correct |
44 |
Correct |
225 ms |
67776 KB |
Output is correct |
45 |
Correct |
265 ms |
75272 KB |
Output is correct |
46 |
Correct |
145 ms |
54036 KB |
Output is correct |
47 |
Correct |
460 ms |
75448 KB |
Output is correct |
48 |
Correct |
307 ms |
83080 KB |
Output is correct |