#include<bits/stdc++.h>
#include<fstream>
using namespace std;
#define ll long long
#define pb push_back
#define forr(i, a, b) for(int i = a; i < b; i++)
#define dorr(i, a, b) for(int i = a; i >= b; i--)
#define ld long double
#define vt vector
#include<fstream>
#define fi first
#define se second
#define pll pair<ll, ll>
#define pii pair<int, int>
const ld PI = 3.14159265359;
const int x[4] = {1, -1, 0, 0};
const int y[4] = {0, 0, 1, -1};
const ll mod = 1e9 + 7;
const int mxn = 5e5, mxm = 1e5, sq = 400;
const int base = (1 << 18);
const ll inf = 1e9;
const ld pre = 1e-6;
//cool problem!
int n;
int l[mxn + 1], r[mxn + 1], id, lson[2 * mxn + 5], rson[2 * mxn + 5];
vt<pii>adj[2 * mxn + 5];
vt<ll>d1, d2, d3;
int build(int nd, int l, int r){
if(l == r)return(l);
int mid = (l + r) >> 1;
int ls = build(nd << 1, l, mid), rs = build(nd << 1 | 1, mid + 1, r);
++id;
lson[id] = ls; rson[id] = rs;
adj[lson[id]].emplace_back(id, 0); adj[rson[id]].emplace_back(id, 0);
return(id);
}
void upd(int nd, int l, int r, int ql, int qr, int u){
if(ql > r || qr < l)return;
if(ql <= l && qr >= r){
adj[nd].emplace_back(u, 1);
return;
}
int mid = (l + r) >> 1;
upd(lson[nd], l, mid, ql, qr, u); upd(rson[nd], mid + 1, r, ql, qr, u);
}
void find(vt<ll>&d){
deque<int>dq;
for(int i = 1; i <= n; i++){
if(d[i] < inf)dq.pb(i);
}
while(!dq.empty()){
int u = dq.front(); dq.pop_front();
for(auto [v, w]: adj[u]){
if(d[v] > d[u] + w){
d[v] = d[u] + w;
if(w){
dq.pb(v);
}else{
dq.push_front(v);
}
}
}
}
}
signed main(){
ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
cin >> n;
id = n;
int root = build(1, 1, n);
forr(i, 1, n + 1){
cin >> l[i] >> r[i];
upd(root, 1, n, l[i], r[i], i);
}
d1.resize(2 * n + 5, inf); d1[1] = 0;
find(d1);
d1[1] = 1;
d2.resize(2 * n + 5, inf); d2[n] = 0;
find(d2);
d2[n] = 1;
int q; cin >> q;
d3.resize(2 * n + 5, inf);
for(int i = 1; i <= n; i++){
d3[i] = min(d1[i] + d2[i], inf);
}
priority_queue<pll, vt<pll>, greater<pll>>pq;
forr(i, 1, n + 1){
if(d3[i] < inf)pq.push({d3[i], i});
}
while(!pq.empty()){
auto [dd, u] = pq.top(); pq.pop();
if(d3[u] != dd)continue;
for(auto [v, w]: adj[u]){
if(d3[v] > d3[u] + w){
d3[v] = d3[u] + w;
pq.push({d3[v], v});
}
}
}
forr(i, 0, q){
int x; cin >> x;
if(d3[x] >= 1e9)cout << -1 << "\n";
else cout << d3[x] - 1 << "\n";
}
return(0);
}
Compilation message
passport.cpp: In function 'void find(std::vector<long long int>&)':
passport.cpp:58:18: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
58 | for(auto [v, w]: adj[u]){
| ^
passport.cpp: In function 'int main()':
passport.cpp:98:14: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
98 | auto [dd, u] = pq.top(); pq.pop();
| ^
passport.cpp:100:18: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
100 | for(auto [v, w]: adj[u]){
| ^
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
12 ms |
23764 KB |
Output is correct |
2 |
Correct |
12 ms |
23832 KB |
Output is correct |
3 |
Correct |
14 ms |
23764 KB |
Output is correct |
4 |
Correct |
623 ms |
81292 KB |
Output is correct |
5 |
Correct |
274 ms |
61264 KB |
Output is correct |
6 |
Correct |
210 ms |
56248 KB |
Output is correct |
7 |
Correct |
150 ms |
53952 KB |
Output is correct |
8 |
Correct |
97 ms |
55656 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
15 ms |
23776 KB |
Output is correct |
2 |
Correct |
12 ms |
23764 KB |
Output is correct |
3 |
Correct |
12 ms |
23764 KB |
Output is correct |
4 |
Correct |
12 ms |
23764 KB |
Output is correct |
5 |
Correct |
12 ms |
23764 KB |
Output is correct |
6 |
Correct |
13 ms |
23760 KB |
Output is correct |
7 |
Correct |
12 ms |
23764 KB |
Output is correct |
8 |
Correct |
12 ms |
23764 KB |
Output is correct |
9 |
Correct |
12 ms |
23772 KB |
Output is correct |
10 |
Correct |
11 ms |
23764 KB |
Output is correct |
11 |
Correct |
13 ms |
23892 KB |
Output is correct |
12 |
Correct |
12 ms |
23892 KB |
Output is correct |
13 |
Correct |
13 ms |
23796 KB |
Output is correct |
14 |
Correct |
12 ms |
23892 KB |
Output is correct |
15 |
Correct |
12 ms |
23892 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
15 ms |
23776 KB |
Output is correct |
2 |
Correct |
12 ms |
23764 KB |
Output is correct |
3 |
Correct |
12 ms |
23764 KB |
Output is correct |
4 |
Correct |
12 ms |
23764 KB |
Output is correct |
5 |
Correct |
12 ms |
23764 KB |
Output is correct |
6 |
Correct |
13 ms |
23760 KB |
Output is correct |
7 |
Correct |
12 ms |
23764 KB |
Output is correct |
8 |
Correct |
12 ms |
23764 KB |
Output is correct |
9 |
Correct |
12 ms |
23772 KB |
Output is correct |
10 |
Correct |
11 ms |
23764 KB |
Output is correct |
11 |
Correct |
13 ms |
23892 KB |
Output is correct |
12 |
Correct |
12 ms |
23892 KB |
Output is correct |
13 |
Correct |
13 ms |
23796 KB |
Output is correct |
14 |
Correct |
12 ms |
23892 KB |
Output is correct |
15 |
Correct |
12 ms |
23892 KB |
Output is correct |
16 |
Correct |
15 ms |
24404 KB |
Output is correct |
17 |
Correct |
18 ms |
24276 KB |
Output is correct |
18 |
Correct |
17 ms |
24532 KB |
Output is correct |
19 |
Correct |
15 ms |
24556 KB |
Output is correct |
20 |
Correct |
13 ms |
24240 KB |
Output is correct |
21 |
Correct |
13 ms |
24276 KB |
Output is correct |
22 |
Correct |
14 ms |
24084 KB |
Output is correct |
23 |
Correct |
15 ms |
24344 KB |
Output is correct |
24 |
Correct |
13 ms |
24412 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
15 ms |
23776 KB |
Output is correct |
2 |
Correct |
12 ms |
23764 KB |
Output is correct |
3 |
Correct |
12 ms |
23764 KB |
Output is correct |
4 |
Correct |
12 ms |
23764 KB |
Output is correct |
5 |
Correct |
12 ms |
23764 KB |
Output is correct |
6 |
Correct |
13 ms |
23760 KB |
Output is correct |
7 |
Correct |
12 ms |
23764 KB |
Output is correct |
8 |
Correct |
12 ms |
23764 KB |
Output is correct |
9 |
Correct |
12 ms |
23772 KB |
Output is correct |
10 |
Correct |
11 ms |
23764 KB |
Output is correct |
11 |
Correct |
13 ms |
23892 KB |
Output is correct |
12 |
Correct |
12 ms |
23892 KB |
Output is correct |
13 |
Correct |
13 ms |
23796 KB |
Output is correct |
14 |
Correct |
12 ms |
23892 KB |
Output is correct |
15 |
Correct |
12 ms |
23892 KB |
Output is correct |
16 |
Correct |
15 ms |
24404 KB |
Output is correct |
17 |
Correct |
18 ms |
24276 KB |
Output is correct |
18 |
Correct |
17 ms |
24532 KB |
Output is correct |
19 |
Correct |
15 ms |
24556 KB |
Output is correct |
20 |
Correct |
13 ms |
24240 KB |
Output is correct |
21 |
Correct |
13 ms |
24276 KB |
Output is correct |
22 |
Correct |
14 ms |
24084 KB |
Output is correct |
23 |
Correct |
15 ms |
24344 KB |
Output is correct |
24 |
Correct |
13 ms |
24412 KB |
Output is correct |
25 |
Correct |
12 ms |
23764 KB |
Output is correct |
26 |
Correct |
11 ms |
23764 KB |
Output is correct |
27 |
Correct |
17 ms |
24432 KB |
Output is correct |
28 |
Correct |
17 ms |
24276 KB |
Output is correct |
29 |
Correct |
16 ms |
24212 KB |
Output is correct |
30 |
Correct |
14 ms |
24276 KB |
Output is correct |
31 |
Correct |
18 ms |
24276 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
12 ms |
23764 KB |
Output is correct |
2 |
Correct |
12 ms |
23832 KB |
Output is correct |
3 |
Correct |
14 ms |
23764 KB |
Output is correct |
4 |
Correct |
623 ms |
81292 KB |
Output is correct |
5 |
Correct |
274 ms |
61264 KB |
Output is correct |
6 |
Correct |
210 ms |
56248 KB |
Output is correct |
7 |
Correct |
150 ms |
53952 KB |
Output is correct |
8 |
Correct |
97 ms |
55656 KB |
Output is correct |
9 |
Correct |
15 ms |
23776 KB |
Output is correct |
10 |
Correct |
12 ms |
23764 KB |
Output is correct |
11 |
Correct |
12 ms |
23764 KB |
Output is correct |
12 |
Correct |
12 ms |
23764 KB |
Output is correct |
13 |
Correct |
12 ms |
23764 KB |
Output is correct |
14 |
Correct |
13 ms |
23760 KB |
Output is correct |
15 |
Correct |
12 ms |
23764 KB |
Output is correct |
16 |
Correct |
12 ms |
23764 KB |
Output is correct |
17 |
Correct |
12 ms |
23772 KB |
Output is correct |
18 |
Correct |
11 ms |
23764 KB |
Output is correct |
19 |
Correct |
13 ms |
23892 KB |
Output is correct |
20 |
Correct |
12 ms |
23892 KB |
Output is correct |
21 |
Correct |
13 ms |
23796 KB |
Output is correct |
22 |
Correct |
12 ms |
23892 KB |
Output is correct |
23 |
Correct |
12 ms |
23892 KB |
Output is correct |
24 |
Correct |
15 ms |
24404 KB |
Output is correct |
25 |
Correct |
18 ms |
24276 KB |
Output is correct |
26 |
Correct |
17 ms |
24532 KB |
Output is correct |
27 |
Correct |
15 ms |
24556 KB |
Output is correct |
28 |
Correct |
13 ms |
24240 KB |
Output is correct |
29 |
Correct |
13 ms |
24276 KB |
Output is correct |
30 |
Correct |
14 ms |
24084 KB |
Output is correct |
31 |
Correct |
15 ms |
24344 KB |
Output is correct |
32 |
Correct |
13 ms |
24412 KB |
Output is correct |
33 |
Correct |
12 ms |
23764 KB |
Output is correct |
34 |
Correct |
11 ms |
23764 KB |
Output is correct |
35 |
Correct |
17 ms |
24432 KB |
Output is correct |
36 |
Correct |
17 ms |
24276 KB |
Output is correct |
37 |
Correct |
16 ms |
24212 KB |
Output is correct |
38 |
Correct |
14 ms |
24276 KB |
Output is correct |
39 |
Correct |
18 ms |
24276 KB |
Output is correct |
40 |
Correct |
738 ms |
85532 KB |
Output is correct |
41 |
Correct |
323 ms |
61760 KB |
Output is correct |
42 |
Correct |
416 ms |
88844 KB |
Output is correct |
43 |
Correct |
423 ms |
89012 KB |
Output is correct |
44 |
Correct |
216 ms |
56676 KB |
Output is correct |
45 |
Correct |
228 ms |
62408 KB |
Output is correct |
46 |
Correct |
111 ms |
38488 KB |
Output is correct |
47 |
Correct |
302 ms |
65060 KB |
Output is correct |
48 |
Correct |
301 ms |
70064 KB |
Output is correct |