#include<bits/stdc++.h>
using namespace std;
#define int long long
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
#define isz(x) ((int)(x).size())
#define sumof(x) accumulate(all(x), 0ll)
const int N=2e5+1;
int n, pos[N], dist[3][N*4];
vector<pair<int, int>> g[N*4];
queue<int> q[N*4];
void build(int k, int l, int r){
if (l==r){
pos[l]=k;
return;
}
int mid=(l+r)>>1;
build(k<<1, l, mid);
build(k<<1|1, mid+1, r);
g[k<<1].emplace_back(k, 0);
g[k<<1|1].emplace_back(k, 0);
}
void update(int k, int l, int r, int L, int R, int u){
if (r<L || R<l) return;
if (L<=l && r<=R){
g[k].emplace_back(pos[u], 1);
return;
}
int mid=(l+r)>>1;
update(k<<1, l, mid, L, R, u);
update(k<<1|1, mid+1, r, L, R, u);
}
void bfs(int t){
for (int i=1; i<=n*4; ++i) if (dist[t][i]<1e9) q[dist[t][i]].push(i);
for (int i=0; i<=n*4; ++i){
while (q[i].size()){
int u=q[i].front(); q[i].pop();
if (dist[t][u]!=i) continue;
for (auto &e:g[u]){
int v=e.first, w=e.second;
if (dist[t][v]>dist[t][u]+w) q[dist[t][v]=dist[t][u]+w].push(v);
}
}
}
}
void solve(){
cin >> n;
build(1, 1, n);
for (int i=1; i<=n; ++i){
int l, r; cin >> l >> r;
update(1, 1, n, l, r, i);
}
memset(dist, 0x3f, sizeof dist);
dist[0][pos[1]]=0;
dist[1][pos[n]]=0;
bfs(0);
bfs(1);
for (int i=1; i<=n*4; ++i) dist[2][i]=dist[0][i]+dist[1][i];
for (int i=2; i<n; ++i) --dist[2][pos[i]];
bfs(2);
int tc; cin >> tc;
while (tc--){
int x; cin >> x; x=pos[x];
cout << (dist[2][x]<1e9?dist[2][x]:-1) << '\n';
}
}
int32_t main(){
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
int ntests=1;
// cin >> ntests;
for (int i=1; i<=ntests; ++i) solve();
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
330 ms |
577092 KB |
Output is correct |
2 |
Correct |
253 ms |
576852 KB |
Output is correct |
3 |
Correct |
252 ms |
577008 KB |
Output is correct |
4 |
Correct |
792 ms |
651808 KB |
Output is correct |
5 |
Correct |
496 ms |
612204 KB |
Output is correct |
6 |
Correct |
466 ms |
601496 KB |
Output is correct |
7 |
Correct |
363 ms |
596752 KB |
Output is correct |
8 |
Correct |
373 ms |
610636 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
255 ms |
577088 KB |
Output is correct |
2 |
Correct |
253 ms |
577064 KB |
Output is correct |
3 |
Correct |
260 ms |
576852 KB |
Output is correct |
4 |
Correct |
252 ms |
576856 KB |
Output is correct |
5 |
Correct |
259 ms |
577108 KB |
Output is correct |
6 |
Correct |
268 ms |
577092 KB |
Output is correct |
7 |
Correct |
260 ms |
576852 KB |
Output is correct |
8 |
Correct |
261 ms |
577320 KB |
Output is correct |
9 |
Correct |
255 ms |
577260 KB |
Output is correct |
10 |
Correct |
257 ms |
577088 KB |
Output is correct |
11 |
Correct |
288 ms |
577108 KB |
Output is correct |
12 |
Correct |
281 ms |
576900 KB |
Output is correct |
13 |
Correct |
309 ms |
577012 KB |
Output is correct |
14 |
Correct |
284 ms |
577176 KB |
Output is correct |
15 |
Correct |
263 ms |
577084 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
255 ms |
577088 KB |
Output is correct |
2 |
Correct |
253 ms |
577064 KB |
Output is correct |
3 |
Correct |
260 ms |
576852 KB |
Output is correct |
4 |
Correct |
252 ms |
576856 KB |
Output is correct |
5 |
Correct |
259 ms |
577108 KB |
Output is correct |
6 |
Correct |
268 ms |
577092 KB |
Output is correct |
7 |
Correct |
260 ms |
576852 KB |
Output is correct |
8 |
Correct |
261 ms |
577320 KB |
Output is correct |
9 |
Correct |
255 ms |
577260 KB |
Output is correct |
10 |
Correct |
257 ms |
577088 KB |
Output is correct |
11 |
Correct |
288 ms |
577108 KB |
Output is correct |
12 |
Correct |
281 ms |
576900 KB |
Output is correct |
13 |
Correct |
309 ms |
577012 KB |
Output is correct |
14 |
Correct |
284 ms |
577176 KB |
Output is correct |
15 |
Correct |
263 ms |
577084 KB |
Output is correct |
16 |
Correct |
257 ms |
577480 KB |
Output is correct |
17 |
Correct |
302 ms |
577616 KB |
Output is correct |
18 |
Correct |
293 ms |
578000 KB |
Output is correct |
19 |
Correct |
312 ms |
577964 KB |
Output is correct |
20 |
Correct |
283 ms |
577360 KB |
Output is correct |
21 |
Correct |
266 ms |
577360 KB |
Output is correct |
22 |
Correct |
293 ms |
577376 KB |
Output is correct |
23 |
Correct |
259 ms |
577360 KB |
Output is correct |
24 |
Correct |
301 ms |
577872 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
255 ms |
577088 KB |
Output is correct |
2 |
Correct |
253 ms |
577064 KB |
Output is correct |
3 |
Correct |
260 ms |
576852 KB |
Output is correct |
4 |
Correct |
252 ms |
576856 KB |
Output is correct |
5 |
Correct |
259 ms |
577108 KB |
Output is correct |
6 |
Correct |
268 ms |
577092 KB |
Output is correct |
7 |
Correct |
260 ms |
576852 KB |
Output is correct |
8 |
Correct |
261 ms |
577320 KB |
Output is correct |
9 |
Correct |
255 ms |
577260 KB |
Output is correct |
10 |
Correct |
257 ms |
577088 KB |
Output is correct |
11 |
Correct |
288 ms |
577108 KB |
Output is correct |
12 |
Correct |
281 ms |
576900 KB |
Output is correct |
13 |
Correct |
309 ms |
577012 KB |
Output is correct |
14 |
Correct |
284 ms |
577176 KB |
Output is correct |
15 |
Correct |
263 ms |
577084 KB |
Output is correct |
16 |
Correct |
257 ms |
577480 KB |
Output is correct |
17 |
Correct |
302 ms |
577616 KB |
Output is correct |
18 |
Correct |
293 ms |
578000 KB |
Output is correct |
19 |
Correct |
312 ms |
577964 KB |
Output is correct |
20 |
Correct |
283 ms |
577360 KB |
Output is correct |
21 |
Correct |
266 ms |
577360 KB |
Output is correct |
22 |
Correct |
293 ms |
577376 KB |
Output is correct |
23 |
Correct |
259 ms |
577360 KB |
Output is correct |
24 |
Correct |
301 ms |
577872 KB |
Output is correct |
25 |
Correct |
272 ms |
577096 KB |
Output is correct |
26 |
Correct |
254 ms |
576852 KB |
Output is correct |
27 |
Correct |
260 ms |
577620 KB |
Output is correct |
28 |
Correct |
258 ms |
577432 KB |
Output is correct |
29 |
Correct |
287 ms |
577360 KB |
Output is correct |
30 |
Correct |
254 ms |
577340 KB |
Output is correct |
31 |
Correct |
304 ms |
577600 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
330 ms |
577092 KB |
Output is correct |
2 |
Correct |
253 ms |
576852 KB |
Output is correct |
3 |
Correct |
252 ms |
577008 KB |
Output is correct |
4 |
Correct |
792 ms |
651808 KB |
Output is correct |
5 |
Correct |
496 ms |
612204 KB |
Output is correct |
6 |
Correct |
466 ms |
601496 KB |
Output is correct |
7 |
Correct |
363 ms |
596752 KB |
Output is correct |
8 |
Correct |
373 ms |
610636 KB |
Output is correct |
9 |
Correct |
255 ms |
577088 KB |
Output is correct |
10 |
Correct |
253 ms |
577064 KB |
Output is correct |
11 |
Correct |
260 ms |
576852 KB |
Output is correct |
12 |
Correct |
252 ms |
576856 KB |
Output is correct |
13 |
Correct |
259 ms |
577108 KB |
Output is correct |
14 |
Correct |
268 ms |
577092 KB |
Output is correct |
15 |
Correct |
260 ms |
576852 KB |
Output is correct |
16 |
Correct |
261 ms |
577320 KB |
Output is correct |
17 |
Correct |
255 ms |
577260 KB |
Output is correct |
18 |
Correct |
257 ms |
577088 KB |
Output is correct |
19 |
Correct |
288 ms |
577108 KB |
Output is correct |
20 |
Correct |
281 ms |
576900 KB |
Output is correct |
21 |
Correct |
309 ms |
577012 KB |
Output is correct |
22 |
Correct |
284 ms |
577176 KB |
Output is correct |
23 |
Correct |
263 ms |
577084 KB |
Output is correct |
24 |
Correct |
257 ms |
577480 KB |
Output is correct |
25 |
Correct |
302 ms |
577616 KB |
Output is correct |
26 |
Correct |
293 ms |
578000 KB |
Output is correct |
27 |
Correct |
312 ms |
577964 KB |
Output is correct |
28 |
Correct |
283 ms |
577360 KB |
Output is correct |
29 |
Correct |
266 ms |
577360 KB |
Output is correct |
30 |
Correct |
293 ms |
577376 KB |
Output is correct |
31 |
Correct |
259 ms |
577360 KB |
Output is correct |
32 |
Correct |
301 ms |
577872 KB |
Output is correct |
33 |
Correct |
272 ms |
577096 KB |
Output is correct |
34 |
Correct |
254 ms |
576852 KB |
Output is correct |
35 |
Correct |
260 ms |
577620 KB |
Output is correct |
36 |
Correct |
258 ms |
577432 KB |
Output is correct |
37 |
Correct |
287 ms |
577360 KB |
Output is correct |
38 |
Correct |
254 ms |
577340 KB |
Output is correct |
39 |
Correct |
304 ms |
577600 KB |
Output is correct |
40 |
Correct |
777 ms |
656468 KB |
Output is correct |
41 |
Correct |
517 ms |
617284 KB |
Output is correct |
42 |
Correct |
619 ms |
668004 KB |
Output is correct |
43 |
Correct |
608 ms |
668756 KB |
Output is correct |
44 |
Correct |
530 ms |
606544 KB |
Output is correct |
45 |
Correct |
568 ms |
617552 KB |
Output is correct |
46 |
Correct |
391 ms |
590900 KB |
Output is correct |
47 |
Correct |
558 ms |
622792 KB |
Output is correct |
48 |
Correct |
540 ms |
635044 KB |
Output is correct |