#include <bits/stdc++.h>
#define se second
#define fs first
#define pb push_back
#define ll long long
#define ii pair<ll,ll>
#define ld long double
#define SZ(v) (int)v.size()
#define ALL(v) v.begin(), v.end()
#define bit(msk, i) ((msk >> i) & 1)
#define iter(id, v) for(auto id : v)
#define rep(i,m,n) for(int i=(m); i<=(n); i++)
#define reb(i,m,n) for(int i=(m); i>=(n); i--)
using namespace std;
mt19937_64 rd(chrono :: steady_clock :: now().time_since_epoch().count());
ll Rand(ll l, ll r) { return uniform_int_distribution<ll> (l, r)(rd); }
const int N = 1e6 + 7;
const ll Mod = 1e9 + 7;
const int szBL = 916;
const ll INF = 1e9;
const int BASE = 137;
int n, Q;
pair<int,int> a[N];
vector<pair<int,int>> ke[N];
int dist1[N], distn[N], distc[N];
int indx[N];
void build (int id, int l, int r) {
if (l == r) {
indx[l] = id;
return;
}
int mid = l + r>> 1;
build (id << 1, l, mid);
build (id << 1 | 1, mid + 1, r);
ke[id << 1].push_back({id, 0});
ke[id << 1 | 1].push_back({id, 0});
}
void update (int id, int l, int r, int u, int v, int idx) {
if (l > v || r < u) return;
if (l >= u && r <= v) {
ke[id].push_back({idx, 1});
return;
}
int mid = l + r >> 1;
update (id << 1, l, mid, u, v, idx);
update (id << 1 | 1, mid + 1, r, u, v, idx);
}
void Dijkstra (int dist[]) {
priority_queue<pair<int,int>, vector<pair<int,int>>, greater<pair<int,int>>> pq;
rep (i, 1, n << 2) {
if (dist[i] < INF) pq.push({dist[i], i});
}
while (!pq.empty()) {
int u = pq.top().se;
pq.pop();
iter (&id, ke[u]) {
if (dist[id.fs] > dist[u] + id.se) {
dist[id.fs] = dist[u] + id.se;
pq.push({dist[id.fs], id.fs});
}
}
}
}
void solution () {
cin >> n;
build (1, 1, n);
rep (i, 1, n) {
cin >> a[i].fs >> a[i].se;
update (1, 1, n, a[i].fs, a[i].se, indx[i]);
}
memset(dist1, 0x3f, sizeof dist1);
memset(distn, 0x3f, sizeof distn);
memset(distc, 0x3f, sizeof distc);
rep (i, 1, n) {
if (a[i].fs == 1) {
dist1[indx[i]] = 0;
}
if (a[i].se == n) {
distn[indx[i]] = 0;
}
}
Dijkstra(dist1);
Dijkstra(distn);
rep (i, 1, n) {
distc[indx[i]] = dist1[indx[i]] + distn[indx[i]];
// cout << i<<" "<<distc[indx[i]] <<"\n";
}
Dijkstra(distc);
cin >> Q;
rep (i, 1, Q) {
int X;
cin >> X;
if (distc[indx[X]] < INF)
cout << distc[indx[X]] + 1 <<"\n";
else cout << -1 <<"\n";
}
}
#define file(name) freopen(name".inp", "r", stdin); freopen(name".out", "w", stdout);
int main () {
// file("c");
ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
ll num_Test = 1;
// cin >> num_Test;
while(num_Test--)
solution();
}
/*
18 3
2 5
6 21
13 19
9 17
14 17
19 20
2 16
2 10
9 14
19 20
14 16
1 3
17 19
14 21
18 19
4 7
5 12
1 13
*/
Compilation message
passport.cpp: In function 'void build(int, int, int)':
passport.cpp:37:17: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
37 | int mid = l + r>> 1;
| ~~^~~
passport.cpp: In function 'void update(int, int, int, int, int, int)':
passport.cpp:50:17: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
50 | int mid = l + r >> 1;
| ~~^~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
10 ms |
37212 KB |
Output is correct |
2 |
Correct |
8 ms |
37212 KB |
Output is correct |
3 |
Correct |
7 ms |
37212 KB |
Output is correct |
4 |
Correct |
550 ms |
86532 KB |
Output is correct |
5 |
Correct |
264 ms |
67020 KB |
Output is correct |
6 |
Correct |
155 ms |
61844 KB |
Output is correct |
7 |
Correct |
180 ms |
60616 KB |
Output is correct |
8 |
Correct |
109 ms |
66240 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
8 ms |
37208 KB |
Output is correct |
2 |
Correct |
8 ms |
37352 KB |
Output is correct |
3 |
Correct |
8 ms |
37212 KB |
Output is correct |
4 |
Correct |
8 ms |
37400 KB |
Output is correct |
5 |
Correct |
8 ms |
37212 KB |
Output is correct |
6 |
Correct |
7 ms |
37212 KB |
Output is correct |
7 |
Correct |
8 ms |
37380 KB |
Output is correct |
8 |
Correct |
8 ms |
37208 KB |
Output is correct |
9 |
Correct |
8 ms |
37212 KB |
Output is correct |
10 |
Correct |
8 ms |
37208 KB |
Output is correct |
11 |
Correct |
10 ms |
37212 KB |
Output is correct |
12 |
Correct |
8 ms |
37208 KB |
Output is correct |
13 |
Correct |
8 ms |
37212 KB |
Output is correct |
14 |
Correct |
8 ms |
37212 KB |
Output is correct |
15 |
Correct |
7 ms |
37428 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
8 ms |
37208 KB |
Output is correct |
2 |
Correct |
8 ms |
37352 KB |
Output is correct |
3 |
Correct |
8 ms |
37212 KB |
Output is correct |
4 |
Correct |
8 ms |
37400 KB |
Output is correct |
5 |
Correct |
8 ms |
37212 KB |
Output is correct |
6 |
Correct |
7 ms |
37212 KB |
Output is correct |
7 |
Correct |
8 ms |
37380 KB |
Output is correct |
8 |
Correct |
8 ms |
37208 KB |
Output is correct |
9 |
Correct |
8 ms |
37212 KB |
Output is correct |
10 |
Correct |
8 ms |
37208 KB |
Output is correct |
11 |
Correct |
10 ms |
37212 KB |
Output is correct |
12 |
Correct |
8 ms |
37208 KB |
Output is correct |
13 |
Correct |
8 ms |
37212 KB |
Output is correct |
14 |
Correct |
8 ms |
37212 KB |
Output is correct |
15 |
Correct |
7 ms |
37428 KB |
Output is correct |
16 |
Correct |
13 ms |
37976 KB |
Output is correct |
17 |
Correct |
12 ms |
37604 KB |
Output is correct |
18 |
Correct |
13 ms |
37980 KB |
Output is correct |
19 |
Correct |
11 ms |
37724 KB |
Output is correct |
20 |
Correct |
11 ms |
37676 KB |
Output is correct |
21 |
Correct |
10 ms |
37468 KB |
Output is correct |
22 |
Correct |
10 ms |
37468 KB |
Output is correct |
23 |
Correct |
10 ms |
37760 KB |
Output is correct |
24 |
Correct |
11 ms |
37612 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
8 ms |
37208 KB |
Output is correct |
2 |
Correct |
8 ms |
37352 KB |
Output is correct |
3 |
Correct |
8 ms |
37212 KB |
Output is correct |
4 |
Correct |
8 ms |
37400 KB |
Output is correct |
5 |
Correct |
8 ms |
37212 KB |
Output is correct |
6 |
Correct |
7 ms |
37212 KB |
Output is correct |
7 |
Correct |
8 ms |
37380 KB |
Output is correct |
8 |
Correct |
8 ms |
37208 KB |
Output is correct |
9 |
Correct |
8 ms |
37212 KB |
Output is correct |
10 |
Correct |
8 ms |
37208 KB |
Output is correct |
11 |
Correct |
10 ms |
37212 KB |
Output is correct |
12 |
Correct |
8 ms |
37208 KB |
Output is correct |
13 |
Correct |
8 ms |
37212 KB |
Output is correct |
14 |
Correct |
8 ms |
37212 KB |
Output is correct |
15 |
Correct |
7 ms |
37428 KB |
Output is correct |
16 |
Correct |
13 ms |
37976 KB |
Output is correct |
17 |
Correct |
12 ms |
37604 KB |
Output is correct |
18 |
Correct |
13 ms |
37980 KB |
Output is correct |
19 |
Correct |
11 ms |
37724 KB |
Output is correct |
20 |
Correct |
11 ms |
37676 KB |
Output is correct |
21 |
Correct |
10 ms |
37468 KB |
Output is correct |
22 |
Correct |
10 ms |
37468 KB |
Output is correct |
23 |
Correct |
10 ms |
37760 KB |
Output is correct |
24 |
Correct |
11 ms |
37612 KB |
Output is correct |
25 |
Correct |
7 ms |
37212 KB |
Output is correct |
26 |
Correct |
8 ms |
37212 KB |
Output is correct |
27 |
Correct |
11 ms |
37720 KB |
Output is correct |
28 |
Correct |
10 ms |
37744 KB |
Output is correct |
29 |
Correct |
11 ms |
37464 KB |
Output is correct |
30 |
Correct |
9 ms |
37468 KB |
Output is correct |
31 |
Correct |
11 ms |
37656 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
10 ms |
37212 KB |
Output is correct |
2 |
Correct |
8 ms |
37212 KB |
Output is correct |
3 |
Correct |
7 ms |
37212 KB |
Output is correct |
4 |
Correct |
550 ms |
86532 KB |
Output is correct |
5 |
Correct |
264 ms |
67020 KB |
Output is correct |
6 |
Correct |
155 ms |
61844 KB |
Output is correct |
7 |
Correct |
180 ms |
60616 KB |
Output is correct |
8 |
Correct |
109 ms |
66240 KB |
Output is correct |
9 |
Correct |
8 ms |
37208 KB |
Output is correct |
10 |
Correct |
8 ms |
37352 KB |
Output is correct |
11 |
Correct |
8 ms |
37212 KB |
Output is correct |
12 |
Correct |
8 ms |
37400 KB |
Output is correct |
13 |
Correct |
8 ms |
37212 KB |
Output is correct |
14 |
Correct |
7 ms |
37212 KB |
Output is correct |
15 |
Correct |
8 ms |
37380 KB |
Output is correct |
16 |
Correct |
8 ms |
37208 KB |
Output is correct |
17 |
Correct |
8 ms |
37212 KB |
Output is correct |
18 |
Correct |
8 ms |
37208 KB |
Output is correct |
19 |
Correct |
10 ms |
37212 KB |
Output is correct |
20 |
Correct |
8 ms |
37208 KB |
Output is correct |
21 |
Correct |
8 ms |
37212 KB |
Output is correct |
22 |
Correct |
8 ms |
37212 KB |
Output is correct |
23 |
Correct |
7 ms |
37428 KB |
Output is correct |
24 |
Correct |
13 ms |
37976 KB |
Output is correct |
25 |
Correct |
12 ms |
37604 KB |
Output is correct |
26 |
Correct |
13 ms |
37980 KB |
Output is correct |
27 |
Correct |
11 ms |
37724 KB |
Output is correct |
28 |
Correct |
11 ms |
37676 KB |
Output is correct |
29 |
Correct |
10 ms |
37468 KB |
Output is correct |
30 |
Correct |
10 ms |
37468 KB |
Output is correct |
31 |
Correct |
10 ms |
37760 KB |
Output is correct |
32 |
Correct |
11 ms |
37612 KB |
Output is correct |
33 |
Correct |
7 ms |
37212 KB |
Output is correct |
34 |
Correct |
8 ms |
37212 KB |
Output is correct |
35 |
Correct |
11 ms |
37720 KB |
Output is correct |
36 |
Correct |
10 ms |
37744 KB |
Output is correct |
37 |
Correct |
11 ms |
37464 KB |
Output is correct |
38 |
Correct |
9 ms |
37468 KB |
Output is correct |
39 |
Correct |
11 ms |
37656 KB |
Output is correct |
40 |
Correct |
709 ms |
91516 KB |
Output is correct |
41 |
Correct |
290 ms |
67820 KB |
Output is correct |
42 |
Correct |
392 ms |
94720 KB |
Output is correct |
43 |
Correct |
383 ms |
95648 KB |
Output is correct |
44 |
Correct |
180 ms |
62380 KB |
Output is correct |
45 |
Correct |
201 ms |
69676 KB |
Output is correct |
46 |
Correct |
138 ms |
51600 KB |
Output is correct |
47 |
Correct |
346 ms |
71152 KB |
Output is correct |
48 |
Correct |
270 ms |
77712 KB |
Output is correct |