#include <bits/stdc++.h>
using namespace std;
namespace
{
} // namespace
void solve()
{
const long INF = LONG_MAX >> 2;
int n;
cin >> n;
int m = 1 << __lg(2 * n - 1);
vector<vector<pair<int, long>>> adj(2 * m + n);
for (int i = 1; i < m; i++)
{
adj[2 * i].emplace_back(i, 0);
adj[2 * i + 1].emplace_back(i, 0);
}
for (int i = 0; i < n; i++)
{
int l, r;
cin >> l >> r, --l;
adj[2 * m + i].emplace_back(i + m, 1);
for (l += m, r += m; l < r; l /= 2, r /= 2)
{
if (l % 2 == 1)
adj[l++].emplace_back(2 * m + i, 0);
if (r % 2 == 1)
adj[--r].emplace_back(2 * m + i, 0);
}
}
auto dijkstra = [&](vector<long> &dist) -> void
{
priority_queue<pair<long, int>> pq;
for (int i = 1; i < 2 * m + n; i++)
{
if (dist[i] != INF)
pq.emplace(-dist[i], i);
}
while (!pq.empty())
{
auto [d, u] = pq.top();
pq.pop();
if (-d != dist[u])
continue;
for (auto [v, wt] : adj[u])
{
if (dist[u] + wt < dist[v])
{
dist[v] = dist[u] + wt;
pq.emplace(-dist[v], v);
}
}
}
};
vector<long> f(2 * m + n, INF), g(2 * m + n, INF), h(2 * m + n, INF);
g[m] = h[m + n - 1] = 0;
dijkstra(g);
dijkstra(h);
for (int i = 1; i < 2 * m + n; i++)
{
if (max(g[i], h[i]) != INF)
f[i] = g[i] + h[i];
}
dijkstra(f);
int q;
cin >> q;
for (int i = 0; i < q; i++)
{
int j;
cin >> j, --j;
cout << (f[j + m] != INF ? f[j + m] : -1) << '\n';
}
}
int main()
{
ios_base::sync_with_stdio(false), cin.tie(NULL);
solve();
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
1002 ms |
128660 KB |
Output is correct |
5 |
Correct |
475 ms |
91596 KB |
Output is correct |
6 |
Correct |
244 ms |
81420 KB |
Output is correct |
7 |
Correct |
398 ms |
106836 KB |
Output is correct |
8 |
Correct |
190 ms |
93668 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
324 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
292 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
316 KB |
Output is correct |
10 |
Correct |
0 ms |
212 KB |
Output is correct |
11 |
Correct |
1 ms |
468 KB |
Output is correct |
12 |
Correct |
1 ms |
468 KB |
Output is correct |
13 |
Correct |
1 ms |
468 KB |
Output is correct |
14 |
Correct |
1 ms |
468 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
324 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
292 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
316 KB |
Output is correct |
10 |
Correct |
0 ms |
212 KB |
Output is correct |
11 |
Correct |
1 ms |
468 KB |
Output is correct |
12 |
Correct |
1 ms |
468 KB |
Output is correct |
13 |
Correct |
1 ms |
468 KB |
Output is correct |
14 |
Correct |
1 ms |
468 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
6 ms |
1748 KB |
Output is correct |
17 |
Correct |
5 ms |
1616 KB |
Output is correct |
18 |
Correct |
7 ms |
1876 KB |
Output is correct |
19 |
Correct |
6 ms |
1876 KB |
Output is correct |
20 |
Correct |
3 ms |
1492 KB |
Output is correct |
21 |
Correct |
3 ms |
1492 KB |
Output is correct |
22 |
Correct |
3 ms |
1492 KB |
Output is correct |
23 |
Correct |
5 ms |
1620 KB |
Output is correct |
24 |
Correct |
4 ms |
1620 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
324 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
292 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
316 KB |
Output is correct |
10 |
Correct |
0 ms |
212 KB |
Output is correct |
11 |
Correct |
1 ms |
468 KB |
Output is correct |
12 |
Correct |
1 ms |
468 KB |
Output is correct |
13 |
Correct |
1 ms |
468 KB |
Output is correct |
14 |
Correct |
1 ms |
468 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
6 ms |
1748 KB |
Output is correct |
17 |
Correct |
5 ms |
1616 KB |
Output is correct |
18 |
Correct |
7 ms |
1876 KB |
Output is correct |
19 |
Correct |
6 ms |
1876 KB |
Output is correct |
20 |
Correct |
3 ms |
1492 KB |
Output is correct |
21 |
Correct |
3 ms |
1492 KB |
Output is correct |
22 |
Correct |
3 ms |
1492 KB |
Output is correct |
23 |
Correct |
5 ms |
1620 KB |
Output is correct |
24 |
Correct |
4 ms |
1620 KB |
Output is correct |
25 |
Correct |
1 ms |
212 KB |
Output is correct |
26 |
Correct |
1 ms |
212 KB |
Output is correct |
27 |
Correct |
9 ms |
1816 KB |
Output is correct |
28 |
Correct |
7 ms |
1500 KB |
Output is correct |
29 |
Correct |
4 ms |
1452 KB |
Output is correct |
30 |
Correct |
4 ms |
1492 KB |
Output is correct |
31 |
Correct |
6 ms |
1688 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
1002 ms |
128660 KB |
Output is correct |
5 |
Correct |
475 ms |
91596 KB |
Output is correct |
6 |
Correct |
244 ms |
81420 KB |
Output is correct |
7 |
Correct |
398 ms |
106836 KB |
Output is correct |
8 |
Correct |
190 ms |
93668 KB |
Output is correct |
9 |
Correct |
1 ms |
324 KB |
Output is correct |
10 |
Correct |
1 ms |
212 KB |
Output is correct |
11 |
Correct |
0 ms |
212 KB |
Output is correct |
12 |
Correct |
1 ms |
212 KB |
Output is correct |
13 |
Correct |
1 ms |
212 KB |
Output is correct |
14 |
Correct |
0 ms |
292 KB |
Output is correct |
15 |
Correct |
0 ms |
212 KB |
Output is correct |
16 |
Correct |
0 ms |
212 KB |
Output is correct |
17 |
Correct |
1 ms |
316 KB |
Output is correct |
18 |
Correct |
0 ms |
212 KB |
Output is correct |
19 |
Correct |
1 ms |
468 KB |
Output is correct |
20 |
Correct |
1 ms |
468 KB |
Output is correct |
21 |
Correct |
1 ms |
468 KB |
Output is correct |
22 |
Correct |
1 ms |
468 KB |
Output is correct |
23 |
Correct |
1 ms |
340 KB |
Output is correct |
24 |
Correct |
6 ms |
1748 KB |
Output is correct |
25 |
Correct |
5 ms |
1616 KB |
Output is correct |
26 |
Correct |
7 ms |
1876 KB |
Output is correct |
27 |
Correct |
6 ms |
1876 KB |
Output is correct |
28 |
Correct |
3 ms |
1492 KB |
Output is correct |
29 |
Correct |
3 ms |
1492 KB |
Output is correct |
30 |
Correct |
3 ms |
1492 KB |
Output is correct |
31 |
Correct |
5 ms |
1620 KB |
Output is correct |
32 |
Correct |
4 ms |
1620 KB |
Output is correct |
33 |
Correct |
1 ms |
212 KB |
Output is correct |
34 |
Correct |
1 ms |
212 KB |
Output is correct |
35 |
Correct |
9 ms |
1816 KB |
Output is correct |
36 |
Correct |
7 ms |
1500 KB |
Output is correct |
37 |
Correct |
4 ms |
1452 KB |
Output is correct |
38 |
Correct |
4 ms |
1492 KB |
Output is correct |
39 |
Correct |
6 ms |
1688 KB |
Output is correct |
40 |
Correct |
1055 ms |
128828 KB |
Output is correct |
41 |
Correct |
511 ms |
91832 KB |
Output is correct |
42 |
Correct |
650 ms |
141300 KB |
Output is correct |
43 |
Correct |
660 ms |
140968 KB |
Output is correct |
44 |
Correct |
278 ms |
81464 KB |
Output is correct |
45 |
Correct |
325 ms |
87168 KB |
Output is correct |
46 |
Correct |
230 ms |
39200 KB |
Output is correct |
47 |
Correct |
568 ms |
105136 KB |
Output is correct |
48 |
Correct |
479 ms |
103060 KB |
Output is correct |