#include <bits/stdc++.h>
using namespace std;
namespace
{
} // namespace
void solve()
{
int n;
cin >> n;
int m = 1 << __lg(2 * n - 1);
vector<vector<pair<int, int>>> 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<int> &dist) -> void
{
priority_queue<pair<int, int>> pq;
for (int i = 1; i < 2 * m + n; i++)
{
if (dist[i] != 2 * n)
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 f(2 * m + n, 2 * n), g(2 * m + n, 2 * n), h(2 * m + n, 2 * n);
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]) != 2 * n)
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] != 2 * n ? 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 |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
896 ms |
81724 KB |
Output is correct |
5 |
Correct |
485 ms |
62668 KB |
Output is correct |
6 |
Correct |
222 ms |
58348 KB |
Output is correct |
7 |
Correct |
365 ms |
73248 KB |
Output is correct |
8 |
Correct |
164 ms |
65720 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
1 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 |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
212 KB |
Output is correct |
11 |
Correct |
2 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
1 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 |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
212 KB |
Output is correct |
11 |
Correct |
2 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
7 ms |
1252 KB |
Output is correct |
17 |
Correct |
5 ms |
1108 KB |
Output is correct |
18 |
Correct |
8 ms |
1364 KB |
Output is correct |
19 |
Correct |
7 ms |
1364 KB |
Output is correct |
20 |
Correct |
3 ms |
1108 KB |
Output is correct |
21 |
Correct |
3 ms |
1108 KB |
Output is correct |
22 |
Correct |
3 ms |
1236 KB |
Output is correct |
23 |
Correct |
5 ms |
1236 KB |
Output is correct |
24 |
Correct |
4 ms |
1236 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
1 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 |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
212 KB |
Output is correct |
11 |
Correct |
2 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
7 ms |
1252 KB |
Output is correct |
17 |
Correct |
5 ms |
1108 KB |
Output is correct |
18 |
Correct |
8 ms |
1364 KB |
Output is correct |
19 |
Correct |
7 ms |
1364 KB |
Output is correct |
20 |
Correct |
3 ms |
1108 KB |
Output is correct |
21 |
Correct |
3 ms |
1108 KB |
Output is correct |
22 |
Correct |
3 ms |
1236 KB |
Output is correct |
23 |
Correct |
5 ms |
1236 KB |
Output is correct |
24 |
Correct |
4 ms |
1236 KB |
Output is correct |
25 |
Correct |
0 ms |
212 KB |
Output is correct |
26 |
Correct |
0 ms |
212 KB |
Output is correct |
27 |
Correct |
6 ms |
1236 KB |
Output is correct |
28 |
Correct |
6 ms |
1236 KB |
Output is correct |
29 |
Correct |
4 ms |
1108 KB |
Output is correct |
30 |
Correct |
4 ms |
1108 KB |
Output is correct |
31 |
Correct |
5 ms |
1236 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
896 ms |
81724 KB |
Output is correct |
5 |
Correct |
485 ms |
62668 KB |
Output is correct |
6 |
Correct |
222 ms |
58348 KB |
Output is correct |
7 |
Correct |
365 ms |
73248 KB |
Output is correct |
8 |
Correct |
164 ms |
65720 KB |
Output is correct |
9 |
Correct |
0 ms |
212 KB |
Output is correct |
10 |
Correct |
0 ms |
212 KB |
Output is correct |
11 |
Correct |
1 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 |
1 ms |
212 KB |
Output is correct |
15 |
Correct |
1 ms |
212 KB |
Output is correct |
16 |
Correct |
1 ms |
212 KB |
Output is correct |
17 |
Correct |
1 ms |
212 KB |
Output is correct |
18 |
Correct |
1 ms |
212 KB |
Output is correct |
19 |
Correct |
2 ms |
340 KB |
Output is correct |
20 |
Correct |
1 ms |
340 KB |
Output is correct |
21 |
Correct |
1 ms |
340 KB |
Output is correct |
22 |
Correct |
1 ms |
340 KB |
Output is correct |
23 |
Correct |
1 ms |
340 KB |
Output is correct |
24 |
Correct |
7 ms |
1252 KB |
Output is correct |
25 |
Correct |
5 ms |
1108 KB |
Output is correct |
26 |
Correct |
8 ms |
1364 KB |
Output is correct |
27 |
Correct |
7 ms |
1364 KB |
Output is correct |
28 |
Correct |
3 ms |
1108 KB |
Output is correct |
29 |
Correct |
3 ms |
1108 KB |
Output is correct |
30 |
Correct |
3 ms |
1236 KB |
Output is correct |
31 |
Correct |
5 ms |
1236 KB |
Output is correct |
32 |
Correct |
4 ms |
1236 KB |
Output is correct |
33 |
Correct |
0 ms |
212 KB |
Output is correct |
34 |
Correct |
0 ms |
212 KB |
Output is correct |
35 |
Correct |
6 ms |
1236 KB |
Output is correct |
36 |
Correct |
6 ms |
1236 KB |
Output is correct |
37 |
Correct |
4 ms |
1108 KB |
Output is correct |
38 |
Correct |
4 ms |
1108 KB |
Output is correct |
39 |
Correct |
5 ms |
1236 KB |
Output is correct |
40 |
Correct |
962 ms |
81940 KB |
Output is correct |
41 |
Correct |
504 ms |
62864 KB |
Output is correct |
42 |
Correct |
599 ms |
89444 KB |
Output is correct |
43 |
Correct |
564 ms |
89328 KB |
Output is correct |
44 |
Correct |
252 ms |
58328 KB |
Output is correct |
45 |
Correct |
299 ms |
60224 KB |
Output is correct |
46 |
Correct |
211 ms |
27996 KB |
Output is correct |
47 |
Correct |
517 ms |
70112 KB |
Output is correct |
48 |
Correct |
402 ms |
70388 KB |
Output is correct |