#include <bits/stdc++.h>
#define ll long long
#define name "Tickets"
#define _left id * 2, l, mid
#define _right id * 2 + 1, mid + 1, r
#define fi(i, a, b) for(int i = a; i <= b; ++i)
#define fid(i, a, b) for(int i = a; i >= b; --i)
#define maxn (int) (2e5 + 7)
using namespace std;
int n, k;
struct dl { int c, w, l, r; } a[maxn];
ll dist[3][2 * maxn];
struct IT {
int st[4 * maxn];
void build(int id = 1, int l = 1, int r = k) {
if(l == r) return st[id] = a[l].r, void();
int mid = (l + r) >> 1;
build(_left), build(_right);
st[id] = max(st[id * 2], st[id * 2 + 1]);
}
void get(int x, vector<int> &P, int id = 1, int l = 1, int r = k) {
if(a[l].l > x || st[id] < x) return;
if(l == r) {
P.push_back(l), st[id] = -1;
return ;
}
int mid = (l + r) >> 1;
get(x, P, _left), get(x, P, _right);
st[id] = max(st[id * 2], st[id * 2 + 1]);
}
} St;
void DJK(ll *dist) {
priority_queue<pair<ll, int>, vector<pair<ll, int>>, greater<pair<ll, int>> > q;
fi(i, 1, k) { /// corner case
dist[a[i].c] = min(dist[a[i].c], dist[n + i] + a[i].w);
}
St.build();
fi(i, 1, n) if(dist[i] < 1e18) q.push({dist[i], i});
while(!q.empty()) {
int u; ll g; tie(g, u) = q.top(); q.pop();
if(dist[u] < g) continue;
vector<int> P; St.get(u, P);
for(int x : P) {
dist[x + n] = dist[u];
if(dist[a[x].c] > dist[x + n] + a[x].w) {
dist[a[x].c] = dist[x + n] + a[x].w;
q.push({ dist[a[x].c], a[x].c });
}
}
}
}
void solve() {
cin >> n ; k = n;
fi(i, 1, k) {
a[i].c = i, a[i].w = 1;
cin >> a[i].l >> a[i].r;
}
sort(a + 1, a + 1 + k, [](dl x, dl y) {
return x.l < y.l;
});
memset(dist, 60, sizeof dist);
dist[0][1] = dist[1][n] = 0;
DJK(dist[0]), DJK(dist[1]);
fi(i, 1, n + k) dist[2][i] = dist[0][i] + dist[1][i];
DJK(dist[2]);
int q; cin >> q;
fi(_, 1, q) {
int i; cin >> i;
cout << (dist[2][i] < 1e18 ? dist[2][i] : -1) << '\n';
}
}
int main() {
ios_base::sync_with_stdio();
cin.tie(), cout.tie();
if(fopen(name".inp", "r")) {
freopen(name".inp", "r", stdin);
freopen(name".out", "w", stdout);
}
solve();
}
Compilation message
passport.cpp: In function 'int main()':
passport.cpp:105:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
105 | freopen(name".inp", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
passport.cpp:106:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
106 | freopen(name".out", "w", stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
12624 KB |
Output is correct |
2 |
Correct |
2 ms |
12792 KB |
Output is correct |
3 |
Correct |
2 ms |
12624 KB |
Output is correct |
4 |
Correct |
260 ms |
26644 KB |
Output is correct |
5 |
Correct |
193 ms |
24760 KB |
Output is correct |
6 |
Correct |
167 ms |
24764 KB |
Output is correct |
7 |
Correct |
136 ms |
26048 KB |
Output is correct |
8 |
Correct |
80 ms |
20516 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
12740 KB |
Output is correct |
2 |
Correct |
3 ms |
12624 KB |
Output is correct |
3 |
Correct |
2 ms |
12624 KB |
Output is correct |
4 |
Correct |
2 ms |
12792 KB |
Output is correct |
5 |
Correct |
3 ms |
12624 KB |
Output is correct |
6 |
Correct |
2 ms |
12624 KB |
Output is correct |
7 |
Correct |
2 ms |
12792 KB |
Output is correct |
8 |
Correct |
2 ms |
12796 KB |
Output is correct |
9 |
Correct |
3 ms |
12624 KB |
Output is correct |
10 |
Correct |
2 ms |
12624 KB |
Output is correct |
11 |
Correct |
3 ms |
12624 KB |
Output is correct |
12 |
Correct |
3 ms |
12624 KB |
Output is correct |
13 |
Correct |
3 ms |
12624 KB |
Output is correct |
14 |
Correct |
3 ms |
12624 KB |
Output is correct |
15 |
Correct |
2 ms |
12636 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
12740 KB |
Output is correct |
2 |
Correct |
3 ms |
12624 KB |
Output is correct |
3 |
Correct |
2 ms |
12624 KB |
Output is correct |
4 |
Correct |
2 ms |
12792 KB |
Output is correct |
5 |
Correct |
3 ms |
12624 KB |
Output is correct |
6 |
Correct |
2 ms |
12624 KB |
Output is correct |
7 |
Correct |
2 ms |
12792 KB |
Output is correct |
8 |
Correct |
2 ms |
12796 KB |
Output is correct |
9 |
Correct |
3 ms |
12624 KB |
Output is correct |
10 |
Correct |
2 ms |
12624 KB |
Output is correct |
11 |
Correct |
3 ms |
12624 KB |
Output is correct |
12 |
Correct |
3 ms |
12624 KB |
Output is correct |
13 |
Correct |
3 ms |
12624 KB |
Output is correct |
14 |
Correct |
3 ms |
12624 KB |
Output is correct |
15 |
Correct |
2 ms |
12636 KB |
Output is correct |
16 |
Correct |
5 ms |
12880 KB |
Output is correct |
17 |
Correct |
4 ms |
12924 KB |
Output is correct |
18 |
Correct |
5 ms |
12880 KB |
Output is correct |
19 |
Correct |
4 ms |
12880 KB |
Output is correct |
20 |
Correct |
5 ms |
12768 KB |
Output is correct |
21 |
Correct |
4 ms |
12928 KB |
Output is correct |
22 |
Correct |
3 ms |
12880 KB |
Output is correct |
23 |
Correct |
5 ms |
12880 KB |
Output is correct |
24 |
Correct |
4 ms |
12880 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
12740 KB |
Output is correct |
2 |
Correct |
3 ms |
12624 KB |
Output is correct |
3 |
Correct |
2 ms |
12624 KB |
Output is correct |
4 |
Correct |
2 ms |
12792 KB |
Output is correct |
5 |
Correct |
3 ms |
12624 KB |
Output is correct |
6 |
Correct |
2 ms |
12624 KB |
Output is correct |
7 |
Correct |
2 ms |
12792 KB |
Output is correct |
8 |
Correct |
2 ms |
12796 KB |
Output is correct |
9 |
Correct |
3 ms |
12624 KB |
Output is correct |
10 |
Correct |
2 ms |
12624 KB |
Output is correct |
11 |
Correct |
3 ms |
12624 KB |
Output is correct |
12 |
Correct |
3 ms |
12624 KB |
Output is correct |
13 |
Correct |
3 ms |
12624 KB |
Output is correct |
14 |
Correct |
3 ms |
12624 KB |
Output is correct |
15 |
Correct |
2 ms |
12636 KB |
Output is correct |
16 |
Correct |
5 ms |
12880 KB |
Output is correct |
17 |
Correct |
4 ms |
12924 KB |
Output is correct |
18 |
Correct |
5 ms |
12880 KB |
Output is correct |
19 |
Correct |
4 ms |
12880 KB |
Output is correct |
20 |
Correct |
5 ms |
12768 KB |
Output is correct |
21 |
Correct |
4 ms |
12928 KB |
Output is correct |
22 |
Correct |
3 ms |
12880 KB |
Output is correct |
23 |
Correct |
5 ms |
12880 KB |
Output is correct |
24 |
Correct |
4 ms |
12880 KB |
Output is correct |
25 |
Correct |
3 ms |
12624 KB |
Output is correct |
26 |
Correct |
2 ms |
12624 KB |
Output is correct |
27 |
Correct |
9 ms |
12780 KB |
Output is correct |
28 |
Correct |
9 ms |
12940 KB |
Output is correct |
29 |
Correct |
9 ms |
12880 KB |
Output is correct |
30 |
Correct |
8 ms |
12888 KB |
Output is correct |
31 |
Correct |
8 ms |
12880 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
12624 KB |
Output is correct |
2 |
Correct |
2 ms |
12792 KB |
Output is correct |
3 |
Correct |
2 ms |
12624 KB |
Output is correct |
4 |
Correct |
260 ms |
26644 KB |
Output is correct |
5 |
Correct |
193 ms |
24760 KB |
Output is correct |
6 |
Correct |
167 ms |
24764 KB |
Output is correct |
7 |
Correct |
136 ms |
26048 KB |
Output is correct |
8 |
Correct |
80 ms |
20516 KB |
Output is correct |
9 |
Correct |
3 ms |
12740 KB |
Output is correct |
10 |
Correct |
3 ms |
12624 KB |
Output is correct |
11 |
Correct |
2 ms |
12624 KB |
Output is correct |
12 |
Correct |
2 ms |
12792 KB |
Output is correct |
13 |
Correct |
3 ms |
12624 KB |
Output is correct |
14 |
Correct |
2 ms |
12624 KB |
Output is correct |
15 |
Correct |
2 ms |
12792 KB |
Output is correct |
16 |
Correct |
2 ms |
12796 KB |
Output is correct |
17 |
Correct |
3 ms |
12624 KB |
Output is correct |
18 |
Correct |
2 ms |
12624 KB |
Output is correct |
19 |
Correct |
3 ms |
12624 KB |
Output is correct |
20 |
Correct |
3 ms |
12624 KB |
Output is correct |
21 |
Correct |
3 ms |
12624 KB |
Output is correct |
22 |
Correct |
3 ms |
12624 KB |
Output is correct |
23 |
Correct |
2 ms |
12636 KB |
Output is correct |
24 |
Correct |
5 ms |
12880 KB |
Output is correct |
25 |
Correct |
4 ms |
12924 KB |
Output is correct |
26 |
Correct |
5 ms |
12880 KB |
Output is correct |
27 |
Correct |
4 ms |
12880 KB |
Output is correct |
28 |
Correct |
5 ms |
12768 KB |
Output is correct |
29 |
Correct |
4 ms |
12928 KB |
Output is correct |
30 |
Correct |
3 ms |
12880 KB |
Output is correct |
31 |
Correct |
5 ms |
12880 KB |
Output is correct |
32 |
Correct |
4 ms |
12880 KB |
Output is correct |
33 |
Correct |
3 ms |
12624 KB |
Output is correct |
34 |
Correct |
2 ms |
12624 KB |
Output is correct |
35 |
Correct |
9 ms |
12780 KB |
Output is correct |
36 |
Correct |
9 ms |
12940 KB |
Output is correct |
37 |
Correct |
9 ms |
12880 KB |
Output is correct |
38 |
Correct |
8 ms |
12888 KB |
Output is correct |
39 |
Correct |
8 ms |
12880 KB |
Output is correct |
40 |
Correct |
596 ms |
27136 KB |
Output is correct |
41 |
Correct |
524 ms |
25016 KB |
Output is correct |
42 |
Correct |
537 ms |
25008 KB |
Output is correct |
43 |
Correct |
505 ms |
26796 KB |
Output is correct |
44 |
Correct |
532 ms |
25016 KB |
Output is correct |
45 |
Correct |
503 ms |
21948 KB |
Output is correct |
46 |
Correct |
198 ms |
18952 KB |
Output is correct |
47 |
Correct |
516 ms |
26596 KB |
Output is correct |
48 |
Correct |
468 ms |
22548 KB |
Output is correct |