#include<bits/stdc++.h>
#define endl '\n'
using namespace std;
typedef long long ll;
void speed()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
}
const int maxn = 2e5 + 10;
vector < pair < int, int > > adj[maxn * 2];
int dist[maxn * 2], lf[maxn * 2], rf[maxn * 2];
int fict, root, toi;
int build(int left, int right)
{
///cout << left << " :: " << right << " " << fict << endl;
if (left == right)
return left;
int mid = (left + right) / 2;
int left_child = build(left, mid);
int right_child = build(mid + 1, right);
int ver = ++ fict;
adj[left_child].push_back({ver, 0});
adj[right_child].push_back({ver, 0});
//cout << left_child << " " << ver << " " << 0 << endl;
//cout << right_child << " " << ver << " " << 0 << endl;
lf[ver] = left_child;
rf[ver] = right_child;
return ver;
}
void graph(int _n)
{
toi = _n;
fict = _n;
root = build(1, _n);
}
void range_update(int node, int l, int r, int ql, int qr, int v)
{
if (l > qr || r < ql)
return;
if (l >= ql && r <= qr)
{
adj[node].push_back({v, 1});
///cout << "make edge " << node << " " << l << " " << r << " " << v << endl;
///cout << node << " " << v << " " << 1 << endl;
return;
}
int m = (l + r) / 2;
range_update(lf[node], l, m, ql, qr, v);
range_update(rf[node], m + 1, r, ql, qr, v);
}
void add_range_edge(int l, int r, int v)
{
range_update(root, 1, toi, l, r, v);
}
void add_edge(int v, int u, int w)
{
adj[v].push_back({u, w});
///adj[u].push_back({v, w});
}
int used[4 * maxn];
struct edge
{
int v, len;
edge(int _v = 0, int _len = 0)
{
v = _v;
len = _len;
}
bool operator < (const edge &e) const
{
return len > e.len;
}
};
queue < int > qs[4 * maxn];
void bfs(int st, bool tf = false)
{
for (int i = 1; i <= fict; i ++)
{
dist[i] = 1e9;
used[i] = 0;
}
qs[0].push(st);
dist[st] = 0;
for (int i = 0; i < 4 * maxn; i ++)
while(!qs[i].empty())
{
int cur = qs[i].front();
qs[i].pop();
//cout << cur << " " << used[cur] << endl;
if (used[cur])
continue;
used[cur] = 1;
//if (tf)
// cout << "here " << v << " " << dist[v] << endl;
for (pair < int, int > nb : adj[cur])
{
if (dist[nb.first] > dist[cur] + nb.second)
{
dist[nb.first] = dist[cur] + nb.second;
qs[dist[cur] + nb.second].push(nb.first);
}
}
}
}
int n, q, l[maxn], r[maxn];
int a[maxn], b[maxn];
void solve()
{
cin >> n;
for (int i = 1; i <= n; i ++)
{
cin >> l[i] >> r[i];
}
graph(n + 1);
for (int i = 1; i <= n; i ++)
{
add_range_edge(l[i], r[i], i);
///for (int j = l[i]; j <= r[i]; j ++)
///add_edge(j, i, 1);
///cout << j << " " << i << " " << 1 << endl;
}
bfs(1);
for (int i = 1; i <= n; i ++)
a[i] = dist[i];
bfs(n);
for (int i = 1; i <= n; i ++)
b[i] = dist[i];
/**for (int i = 1; i <= n; i ++)
cout << b[i] << " ";
cout << endl;
return;*/
///cout << "------------" << endl;
for (int i = 1; i <= n; i ++)
{
int edge = a[i] + b[i];
if (a[i] > 0 && b[i] > 0)
edge --;
add_edge(n + 1, i, edge);
///cout << n + 1 << " " << i << " " << a[i] << " " << b[i] << endl;
}
bfs(n + 1, 1);
/**for (int i = 1; i <= n; i ++)
cout << dist[i] << " ";
cout << endl;*/
cin >> q;
for (int i = 1; i <= q; i ++)
{
int x;
cin >> x;
int ans = dist[x];
if (ans > n)
cout << -1 << endl;
else
cout << ans << endl;
}
}
int main()
{
speed();
///freopen("text.txt", "r", stdin);
solve();
return 0;
}
/**
9
1 1
2 2
3 3
1 4
2 8
5 7
4 9
8 8
9 9
1
6
*/
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
313 ms |
548324 KB |
Output is correct |
2 |
Correct |
309 ms |
548264 KB |
Output is correct |
3 |
Correct |
292 ms |
548428 KB |
Output is correct |
4 |
Correct |
682 ms |
600116 KB |
Output is correct |
5 |
Correct |
514 ms |
579488 KB |
Output is correct |
6 |
Correct |
454 ms |
574396 KB |
Output is correct |
7 |
Correct |
430 ms |
598324 KB |
Output is correct |
8 |
Correct |
426 ms |
592160 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
275 ms |
548328 KB |
Output is correct |
2 |
Correct |
275 ms |
548324 KB |
Output is correct |
3 |
Correct |
296 ms |
548248 KB |
Output is correct |
4 |
Correct |
267 ms |
548272 KB |
Output is correct |
5 |
Correct |
291 ms |
548368 KB |
Output is correct |
6 |
Correct |
273 ms |
548220 KB |
Output is correct |
7 |
Correct |
266 ms |
548204 KB |
Output is correct |
8 |
Correct |
276 ms |
548320 KB |
Output is correct |
9 |
Correct |
272 ms |
548324 KB |
Output is correct |
10 |
Correct |
335 ms |
548324 KB |
Output is correct |
11 |
Correct |
307 ms |
548344 KB |
Output is correct |
12 |
Correct |
298 ms |
548368 KB |
Output is correct |
13 |
Correct |
307 ms |
548320 KB |
Output is correct |
14 |
Correct |
327 ms |
548272 KB |
Output is correct |
15 |
Correct |
310 ms |
548348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
275 ms |
548328 KB |
Output is correct |
2 |
Correct |
275 ms |
548324 KB |
Output is correct |
3 |
Correct |
296 ms |
548248 KB |
Output is correct |
4 |
Correct |
267 ms |
548272 KB |
Output is correct |
5 |
Correct |
291 ms |
548368 KB |
Output is correct |
6 |
Correct |
273 ms |
548220 KB |
Output is correct |
7 |
Correct |
266 ms |
548204 KB |
Output is correct |
8 |
Correct |
276 ms |
548320 KB |
Output is correct |
9 |
Correct |
272 ms |
548324 KB |
Output is correct |
10 |
Correct |
335 ms |
548324 KB |
Output is correct |
11 |
Correct |
307 ms |
548344 KB |
Output is correct |
12 |
Correct |
298 ms |
548368 KB |
Output is correct |
13 |
Correct |
307 ms |
548320 KB |
Output is correct |
14 |
Correct |
327 ms |
548272 KB |
Output is correct |
15 |
Correct |
310 ms |
548348 KB |
Output is correct |
16 |
Correct |
306 ms |
548848 KB |
Output is correct |
17 |
Correct |
306 ms |
548792 KB |
Output is correct |
18 |
Correct |
311 ms |
548980 KB |
Output is correct |
19 |
Correct |
311 ms |
548940 KB |
Output is correct |
20 |
Correct |
276 ms |
548684 KB |
Output is correct |
21 |
Correct |
303 ms |
548728 KB |
Output is correct |
22 |
Correct |
324 ms |
549044 KB |
Output is correct |
23 |
Correct |
310 ms |
548760 KB |
Output is correct |
24 |
Correct |
310 ms |
548872 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
275 ms |
548328 KB |
Output is correct |
2 |
Correct |
275 ms |
548324 KB |
Output is correct |
3 |
Correct |
296 ms |
548248 KB |
Output is correct |
4 |
Correct |
267 ms |
548272 KB |
Output is correct |
5 |
Correct |
291 ms |
548368 KB |
Output is correct |
6 |
Correct |
273 ms |
548220 KB |
Output is correct |
7 |
Correct |
266 ms |
548204 KB |
Output is correct |
8 |
Correct |
276 ms |
548320 KB |
Output is correct |
9 |
Correct |
272 ms |
548324 KB |
Output is correct |
10 |
Correct |
335 ms |
548324 KB |
Output is correct |
11 |
Correct |
307 ms |
548344 KB |
Output is correct |
12 |
Correct |
298 ms |
548368 KB |
Output is correct |
13 |
Correct |
307 ms |
548320 KB |
Output is correct |
14 |
Correct |
327 ms |
548272 KB |
Output is correct |
15 |
Correct |
310 ms |
548348 KB |
Output is correct |
16 |
Correct |
306 ms |
548848 KB |
Output is correct |
17 |
Correct |
306 ms |
548792 KB |
Output is correct |
18 |
Correct |
311 ms |
548980 KB |
Output is correct |
19 |
Correct |
311 ms |
548940 KB |
Output is correct |
20 |
Correct |
276 ms |
548684 KB |
Output is correct |
21 |
Correct |
303 ms |
548728 KB |
Output is correct |
22 |
Correct |
324 ms |
549044 KB |
Output is correct |
23 |
Correct |
310 ms |
548760 KB |
Output is correct |
24 |
Correct |
310 ms |
548872 KB |
Output is correct |
25 |
Correct |
268 ms |
548336 KB |
Output is correct |
26 |
Correct |
284 ms |
548252 KB |
Output is correct |
27 |
Correct |
308 ms |
548876 KB |
Output is correct |
28 |
Correct |
332 ms |
548748 KB |
Output is correct |
29 |
Correct |
283 ms |
548752 KB |
Output is correct |
30 |
Correct |
303 ms |
548680 KB |
Output is correct |
31 |
Correct |
301 ms |
548812 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
313 ms |
548324 KB |
Output is correct |
2 |
Correct |
309 ms |
548264 KB |
Output is correct |
3 |
Correct |
292 ms |
548428 KB |
Output is correct |
4 |
Correct |
682 ms |
600116 KB |
Output is correct |
5 |
Correct |
514 ms |
579488 KB |
Output is correct |
6 |
Correct |
454 ms |
574396 KB |
Output is correct |
7 |
Correct |
430 ms |
598324 KB |
Output is correct |
8 |
Correct |
426 ms |
592160 KB |
Output is correct |
9 |
Correct |
275 ms |
548328 KB |
Output is correct |
10 |
Correct |
275 ms |
548324 KB |
Output is correct |
11 |
Correct |
296 ms |
548248 KB |
Output is correct |
12 |
Correct |
267 ms |
548272 KB |
Output is correct |
13 |
Correct |
291 ms |
548368 KB |
Output is correct |
14 |
Correct |
273 ms |
548220 KB |
Output is correct |
15 |
Correct |
266 ms |
548204 KB |
Output is correct |
16 |
Correct |
276 ms |
548320 KB |
Output is correct |
17 |
Correct |
272 ms |
548324 KB |
Output is correct |
18 |
Correct |
335 ms |
548324 KB |
Output is correct |
19 |
Correct |
307 ms |
548344 KB |
Output is correct |
20 |
Correct |
298 ms |
548368 KB |
Output is correct |
21 |
Correct |
307 ms |
548320 KB |
Output is correct |
22 |
Correct |
327 ms |
548272 KB |
Output is correct |
23 |
Correct |
310 ms |
548348 KB |
Output is correct |
24 |
Correct |
306 ms |
548848 KB |
Output is correct |
25 |
Correct |
306 ms |
548792 KB |
Output is correct |
26 |
Correct |
311 ms |
548980 KB |
Output is correct |
27 |
Correct |
311 ms |
548940 KB |
Output is correct |
28 |
Correct |
276 ms |
548684 KB |
Output is correct |
29 |
Correct |
303 ms |
548728 KB |
Output is correct |
30 |
Correct |
324 ms |
549044 KB |
Output is correct |
31 |
Correct |
310 ms |
548760 KB |
Output is correct |
32 |
Correct |
310 ms |
548872 KB |
Output is correct |
33 |
Correct |
268 ms |
548336 KB |
Output is correct |
34 |
Correct |
284 ms |
548252 KB |
Output is correct |
35 |
Correct |
308 ms |
548876 KB |
Output is correct |
36 |
Correct |
332 ms |
548748 KB |
Output is correct |
37 |
Correct |
283 ms |
548752 KB |
Output is correct |
38 |
Correct |
303 ms |
548680 KB |
Output is correct |
39 |
Correct |
301 ms |
548812 KB |
Output is correct |
40 |
Correct |
697 ms |
600468 KB |
Output is correct |
41 |
Correct |
554 ms |
581008 KB |
Output is correct |
42 |
Correct |
563 ms |
607724 KB |
Output is correct |
43 |
Correct |
536 ms |
607948 KB |
Output is correct |
44 |
Correct |
452 ms |
575772 KB |
Output is correct |
45 |
Correct |
489 ms |
582556 KB |
Output is correct |
46 |
Correct |
390 ms |
562188 KB |
Output is correct |
47 |
Correct |
530 ms |
590204 KB |
Output is correct |
48 |
Correct |
533 ms |
591472 KB |
Output is correct |