#include <algorithm>
#include <iostream>
#include <numeric>
#include <cassert>
#include <vector>
#include <queue>
typedef long long llong;
const int MAXN = 200000 + 10;
const int INF = 1e9;
int n;
std::vector <std::pair <int,int>> g[2 * MAXN];
struct SegmentTree
{
int cnt;
int tree[4*MAXN];
void build(int l, int r, int node)
{
if (l == r)
{
tree[node] = l;
return;
}
tree[node] = ++cnt;
int mid = (l + r) / 2;
build(l, mid, 2*node);
build(mid + 1, r, 2*node + 1);
g[tree[2 * node]].push_back({tree[node], 0});
g[tree[2 * node + 1]].push_back({tree[node], 0});
}
void addEdges(int l, int r, int node, int queryL, int queryR, int queryNode)
{
if (queryL <= l && r <= queryR)
{
g[tree[node]].push_back({queryNode, 1});
return;
}
int mid = (l + r) / 2;
if (queryL <= mid) addEdges(l, mid, 2*node, queryL, queryR, queryNode);
if (mid + 1 <= queryR) addEdges(mid + 1, r, 2*node + 1, queryL, queryR, queryNode);
}
void build()
{
cnt = n;
build(1, n, 1);
}
void addEdges(int to, int l, int r)
{
addEdges(1, n, 1, l, r, to);
}
};
int queries;
int l[MAXN];
int r[MAXN];
bool vis[2 * MAXN];
int dist[2 * MAXN];
int distA[2 * MAXN];
int distB[2 * MAXN];
std::queue <int> q[2 * MAXN];
std::deque <int> dq;
SegmentTree tree;
void solve()
{
tree.build();
for (int i = 1 ; i <= n ; ++i)
{
tree.addEdges(i, l[i], r[i]);
}
std::fill(distA + 1, distA + 1 + tree.cnt, INF);
std::fill(distB + 1, distB + 1 + tree.cnt, INF);
dq.push_back(1);
distA[1] = 0;
while (!dq.empty())
{
int top = dq.front();
dq.pop_front();
for (const auto &[u, d] : g[top])
{
if (distA[u] > distA[top] + d)
{
distA[u] = distA[top] + d;
if (d == 0) dq.push_front(u);
else dq.push_back(u);
}
}
}
dq.push_back(n);
distB[n] = 0;
while (!dq.empty())
{
int top = dq.front();
dq.pop_front();
for (const auto &[u, d] : g[top])
{
if (distB[u] > distB[top] + d)
{
distB[u] = distB[top] + d;
if (d == 0) dq.push_front(u);
else dq.push_back(u);
}
}
}
for (int i = 1 ; i <= n ; ++i)
{
dist[i] = distA[i] + distB[i] - (distA[i] > 0 && distB[i] > 0);
if (dist[i] <= 2 * n) q[dist[i]].push(i);
}
std::fill(dist + n + 1, dist + tree.cnt + 1, INF);
for (int d = 0 ; d <= n ; ++d)
{
while (!q[d].empty())
{
int top = q[d].front();
q[d].pop();
if (vis[top])
{
continue;
}
vis[top] = true;
for (const auto &[u, d] : g[top])
{
if (dist[u] > dist[top] + d)
{
dist[u] = dist[top] + d;
q[dist[u]].push(u);
}
}
}
}
for (int i = 1 ; i <= queries ; ++i)
{
int node;
std::cin >> node;
if (dist[node] <= n) std::cout << dist[node] << '\n';
else std::cout << -1 << '\n';
}
}
void input()
{
std::cin >> n;
for (int i = 1 ; i <= n ; ++i)
{
std::cin >> l[i] >> r[i];
}
std::cin >> queries;
}
void fastIOI()
{
std::ios_base :: sync_with_stdio(0);
std::cout.tie(nullptr);
std::cin.tie(nullptr);
}
int main()
{
fastIOI();
input();
solve();
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
140 ms |
286548 KB |
Output is correct |
2 |
Correct |
134 ms |
286476 KB |
Output is correct |
3 |
Correct |
155 ms |
286560 KB |
Output is correct |
4 |
Correct |
631 ms |
330788 KB |
Output is correct |
5 |
Correct |
301 ms |
310352 KB |
Output is correct |
6 |
Correct |
236 ms |
305280 KB |
Output is correct |
7 |
Correct |
219 ms |
303336 KB |
Output is correct |
8 |
Correct |
228 ms |
310744 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
138 ms |
286548 KB |
Output is correct |
2 |
Correct |
130 ms |
286516 KB |
Output is correct |
3 |
Correct |
128 ms |
286544 KB |
Output is correct |
4 |
Correct |
126 ms |
286544 KB |
Output is correct |
5 |
Correct |
143 ms |
286676 KB |
Output is correct |
6 |
Correct |
130 ms |
286576 KB |
Output is correct |
7 |
Correct |
130 ms |
286880 KB |
Output is correct |
8 |
Correct |
133 ms |
286548 KB |
Output is correct |
9 |
Correct |
144 ms |
286728 KB |
Output is correct |
10 |
Correct |
161 ms |
286544 KB |
Output is correct |
11 |
Correct |
128 ms |
286544 KB |
Output is correct |
12 |
Correct |
139 ms |
286720 KB |
Output is correct |
13 |
Correct |
149 ms |
286548 KB |
Output is correct |
14 |
Correct |
128 ms |
286544 KB |
Output is correct |
15 |
Correct |
142 ms |
286632 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
138 ms |
286548 KB |
Output is correct |
2 |
Correct |
130 ms |
286516 KB |
Output is correct |
3 |
Correct |
128 ms |
286544 KB |
Output is correct |
4 |
Correct |
126 ms |
286544 KB |
Output is correct |
5 |
Correct |
143 ms |
286676 KB |
Output is correct |
6 |
Correct |
130 ms |
286576 KB |
Output is correct |
7 |
Correct |
130 ms |
286880 KB |
Output is correct |
8 |
Correct |
133 ms |
286548 KB |
Output is correct |
9 |
Correct |
144 ms |
286728 KB |
Output is correct |
10 |
Correct |
161 ms |
286544 KB |
Output is correct |
11 |
Correct |
128 ms |
286544 KB |
Output is correct |
12 |
Correct |
139 ms |
286720 KB |
Output is correct |
13 |
Correct |
149 ms |
286548 KB |
Output is correct |
14 |
Correct |
128 ms |
286544 KB |
Output is correct |
15 |
Correct |
142 ms |
286632 KB |
Output is correct |
16 |
Correct |
139 ms |
287176 KB |
Output is correct |
17 |
Correct |
138 ms |
287012 KB |
Output is correct |
18 |
Correct |
137 ms |
287056 KB |
Output is correct |
19 |
Correct |
136 ms |
287412 KB |
Output is correct |
20 |
Correct |
146 ms |
286800 KB |
Output is correct |
21 |
Correct |
132 ms |
286804 KB |
Output is correct |
22 |
Correct |
158 ms |
286800 KB |
Output is correct |
23 |
Correct |
171 ms |
287116 KB |
Output is correct |
24 |
Correct |
155 ms |
286980 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
138 ms |
286548 KB |
Output is correct |
2 |
Correct |
130 ms |
286516 KB |
Output is correct |
3 |
Correct |
128 ms |
286544 KB |
Output is correct |
4 |
Correct |
126 ms |
286544 KB |
Output is correct |
5 |
Correct |
143 ms |
286676 KB |
Output is correct |
6 |
Correct |
130 ms |
286576 KB |
Output is correct |
7 |
Correct |
130 ms |
286880 KB |
Output is correct |
8 |
Correct |
133 ms |
286548 KB |
Output is correct |
9 |
Correct |
144 ms |
286728 KB |
Output is correct |
10 |
Correct |
161 ms |
286544 KB |
Output is correct |
11 |
Correct |
128 ms |
286544 KB |
Output is correct |
12 |
Correct |
139 ms |
286720 KB |
Output is correct |
13 |
Correct |
149 ms |
286548 KB |
Output is correct |
14 |
Correct |
128 ms |
286544 KB |
Output is correct |
15 |
Correct |
142 ms |
286632 KB |
Output is correct |
16 |
Correct |
139 ms |
287176 KB |
Output is correct |
17 |
Correct |
138 ms |
287012 KB |
Output is correct |
18 |
Correct |
137 ms |
287056 KB |
Output is correct |
19 |
Correct |
136 ms |
287412 KB |
Output is correct |
20 |
Correct |
146 ms |
286800 KB |
Output is correct |
21 |
Correct |
132 ms |
286804 KB |
Output is correct |
22 |
Correct |
158 ms |
286800 KB |
Output is correct |
23 |
Correct |
171 ms |
287116 KB |
Output is correct |
24 |
Correct |
155 ms |
286980 KB |
Output is correct |
25 |
Correct |
126 ms |
286548 KB |
Output is correct |
26 |
Correct |
127 ms |
286548 KB |
Output is correct |
27 |
Correct |
154 ms |
287184 KB |
Output is correct |
28 |
Correct |
133 ms |
286800 KB |
Output is correct |
29 |
Correct |
142 ms |
286800 KB |
Output is correct |
30 |
Correct |
132 ms |
286932 KB |
Output is correct |
31 |
Correct |
141 ms |
286804 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
140 ms |
286548 KB |
Output is correct |
2 |
Correct |
134 ms |
286476 KB |
Output is correct |
3 |
Correct |
155 ms |
286560 KB |
Output is correct |
4 |
Correct |
631 ms |
330788 KB |
Output is correct |
5 |
Correct |
301 ms |
310352 KB |
Output is correct |
6 |
Correct |
236 ms |
305280 KB |
Output is correct |
7 |
Correct |
219 ms |
303336 KB |
Output is correct |
8 |
Correct |
228 ms |
310744 KB |
Output is correct |
9 |
Correct |
138 ms |
286548 KB |
Output is correct |
10 |
Correct |
130 ms |
286516 KB |
Output is correct |
11 |
Correct |
128 ms |
286544 KB |
Output is correct |
12 |
Correct |
126 ms |
286544 KB |
Output is correct |
13 |
Correct |
143 ms |
286676 KB |
Output is correct |
14 |
Correct |
130 ms |
286576 KB |
Output is correct |
15 |
Correct |
130 ms |
286880 KB |
Output is correct |
16 |
Correct |
133 ms |
286548 KB |
Output is correct |
17 |
Correct |
144 ms |
286728 KB |
Output is correct |
18 |
Correct |
161 ms |
286544 KB |
Output is correct |
19 |
Correct |
128 ms |
286544 KB |
Output is correct |
20 |
Correct |
139 ms |
286720 KB |
Output is correct |
21 |
Correct |
149 ms |
286548 KB |
Output is correct |
22 |
Correct |
128 ms |
286544 KB |
Output is correct |
23 |
Correct |
142 ms |
286632 KB |
Output is correct |
24 |
Correct |
139 ms |
287176 KB |
Output is correct |
25 |
Correct |
138 ms |
287012 KB |
Output is correct |
26 |
Correct |
137 ms |
287056 KB |
Output is correct |
27 |
Correct |
136 ms |
287412 KB |
Output is correct |
28 |
Correct |
146 ms |
286800 KB |
Output is correct |
29 |
Correct |
132 ms |
286804 KB |
Output is correct |
30 |
Correct |
158 ms |
286800 KB |
Output is correct |
31 |
Correct |
171 ms |
287116 KB |
Output is correct |
32 |
Correct |
155 ms |
286980 KB |
Output is correct |
33 |
Correct |
126 ms |
286548 KB |
Output is correct |
34 |
Correct |
127 ms |
286548 KB |
Output is correct |
35 |
Correct |
154 ms |
287184 KB |
Output is correct |
36 |
Correct |
133 ms |
286800 KB |
Output is correct |
37 |
Correct |
142 ms |
286800 KB |
Output is correct |
38 |
Correct |
132 ms |
286932 KB |
Output is correct |
39 |
Correct |
141 ms |
286804 KB |
Output is correct |
40 |
Correct |
634 ms |
331476 KB |
Output is correct |
41 |
Correct |
341 ms |
311852 KB |
Output is correct |
42 |
Correct |
411 ms |
338596 KB |
Output is correct |
43 |
Correct |
379 ms |
338340 KB |
Output is correct |
44 |
Correct |
270 ms |
306524 KB |
Output is correct |
45 |
Correct |
295 ms |
313104 KB |
Output is correct |
46 |
Correct |
201 ms |
297044 KB |
Output is correct |
47 |
Correct |
329 ms |
314368 KB |
Output is correct |
48 |
Correct |
328 ms |
321364 KB |
Output is correct |