#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
#define int long long
#define mp make_pair
#define mt make_tuple
#define pb push_back
#define ppb pop_back
#define eb emplace_back
#define g0(a) get<0>(a)
#define g1(a) get<1>(a)
#define g2(a) get<2>(a)
#define g3(a) get<3>(a)
#define g4(a) get<4>(a)
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
using db = double;
using ll = long long;
using ld = long double;
using ii = pair<int, int>;
using iii = tuple<int, int, int>;
using iiii = tuple<int, int, int, int>;
using iiiii = tuple<int, int, int, int, int>;
template<class key, class value = null_type, class cmp = less<key> >
using ordered_set = tree<key, value, cmp, rb_tree_tag, tree_order_statistics_node_update>;
int N, Q, _idx, L[200005], R[200005], distL[100005], distR[100005], distC[100005], mpp[100005];
vector<ii> adj[400005];
priority_queue<ii, vector<ii>, greater<ii> > pq;
void dijk(int dist[]) {
for (int i = 1; i <= _idx; i++) {
pq.emplace(dist[i], i);
}
while (!pq.empty()) {
auto [d, v] = pq.top();
pq.pop();
if (d != dist[v]) continue;
//~ cout << "@ " << v << ' ' << dist[v] << '\n';
for (auto [u, w] : adj[v]) {
if (d + w < dist[u]) {
dist[u] = d + w;
pq.emplace(dist[u], u);
}
}
}
}
struct node {
node *left, *right;
int S, E, idx;
node(int _s, int _e) : S(_s), E(_e), idx(++_idx) {
if (S == E) {
mpp[S] = idx;
return;
}
int M = (S + E) >> 1;
left = new node(S, M);
right = new node(M + 1, E);
adj[left->idx].eb(idx, 0);
adj[right->idx].eb(idx, 0);
}
void add(int l, int r, int v) {
if (l > E || r < S) return;
if (l <= S && E <= r) {
//~ cout << "ADD " << idx << ' ' << v << '\n';
adj[idx].eb(v, 1);
return;
}
left->add(l, r, v);
right->add(l, r, v);
}
} *root;
main() {
ios::sync_with_stdio(0);
cin.tie(0);
cin >> N;
root = new node(1, N);
for (int i = 1; i <= N; i++) {
cin >> L[i] >> R[i];
root->add(L[i], R[i], mpp[i]);
}
for (int i = 1; i <= _idx; i++) {
distL[i] = distR[i] = distC[i] = (int)1e9;
}
for (int i = 1; i <= N; i++) {
if (L[i] == 1) distL[mpp[i]] = 0;
if (R[i] == N) distR[mpp[i]] = 0;
}
dijk(distL);
dijk(distR);
for (int i = 1; i <= N; i++) {
//~ cout << distL[mpp[i]] << ' ' << distR[mpp[i]] << '\n';
distC[mpp[i]] = distL[mpp[i]] + distR[mpp[i]];
}
dijk(distC);
cin >> Q;
for (int x; Q--; ) {
cin >> x;
if (distC[mpp[x]] == (int)1e9) cout << "-1\n";
else cout << distC[mpp[x]] + 1 << '\n';
}
}
Compilation message
passport.cpp:76:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
76 | main() {
| ^~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
9684 KB |
Output is correct |
2 |
Correct |
5 ms |
9760 KB |
Output is correct |
3 |
Correct |
5 ms |
9684 KB |
Output is correct |
4 |
Runtime error |
581 ms |
215828 KB |
Execution killed with signal 11 |
5 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
9684 KB |
Output is correct |
2 |
Correct |
6 ms |
9684 KB |
Output is correct |
3 |
Correct |
6 ms |
9684 KB |
Output is correct |
4 |
Correct |
7 ms |
9736 KB |
Output is correct |
5 |
Correct |
5 ms |
9684 KB |
Output is correct |
6 |
Correct |
5 ms |
9684 KB |
Output is correct |
7 |
Correct |
6 ms |
9684 KB |
Output is correct |
8 |
Correct |
5 ms |
9744 KB |
Output is correct |
9 |
Correct |
5 ms |
9684 KB |
Output is correct |
10 |
Correct |
5 ms |
9684 KB |
Output is correct |
11 |
Correct |
6 ms |
9812 KB |
Output is correct |
12 |
Correct |
6 ms |
9812 KB |
Output is correct |
13 |
Correct |
5 ms |
9812 KB |
Output is correct |
14 |
Correct |
5 ms |
9812 KB |
Output is correct |
15 |
Correct |
5 ms |
9812 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
9684 KB |
Output is correct |
2 |
Correct |
6 ms |
9684 KB |
Output is correct |
3 |
Correct |
6 ms |
9684 KB |
Output is correct |
4 |
Correct |
7 ms |
9736 KB |
Output is correct |
5 |
Correct |
5 ms |
9684 KB |
Output is correct |
6 |
Correct |
5 ms |
9684 KB |
Output is correct |
7 |
Correct |
6 ms |
9684 KB |
Output is correct |
8 |
Correct |
5 ms |
9744 KB |
Output is correct |
9 |
Correct |
5 ms |
9684 KB |
Output is correct |
10 |
Correct |
5 ms |
9684 KB |
Output is correct |
11 |
Correct |
6 ms |
9812 KB |
Output is correct |
12 |
Correct |
6 ms |
9812 KB |
Output is correct |
13 |
Correct |
5 ms |
9812 KB |
Output is correct |
14 |
Correct |
5 ms |
9812 KB |
Output is correct |
15 |
Correct |
5 ms |
9812 KB |
Output is correct |
16 |
Correct |
11 ms |
10860 KB |
Output is correct |
17 |
Correct |
10 ms |
10772 KB |
Output is correct |
18 |
Correct |
10 ms |
11220 KB |
Output is correct |
19 |
Correct |
11 ms |
11092 KB |
Output is correct |
20 |
Correct |
9 ms |
10580 KB |
Output is correct |
21 |
Correct |
9 ms |
10708 KB |
Output is correct |
22 |
Correct |
7 ms |
10580 KB |
Output is correct |
23 |
Correct |
10 ms |
10836 KB |
Output is correct |
24 |
Correct |
9 ms |
10840 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
9684 KB |
Output is correct |
2 |
Correct |
6 ms |
9684 KB |
Output is correct |
3 |
Correct |
6 ms |
9684 KB |
Output is correct |
4 |
Correct |
7 ms |
9736 KB |
Output is correct |
5 |
Correct |
5 ms |
9684 KB |
Output is correct |
6 |
Correct |
5 ms |
9684 KB |
Output is correct |
7 |
Correct |
6 ms |
9684 KB |
Output is correct |
8 |
Correct |
5 ms |
9744 KB |
Output is correct |
9 |
Correct |
5 ms |
9684 KB |
Output is correct |
10 |
Correct |
5 ms |
9684 KB |
Output is correct |
11 |
Correct |
6 ms |
9812 KB |
Output is correct |
12 |
Correct |
6 ms |
9812 KB |
Output is correct |
13 |
Correct |
5 ms |
9812 KB |
Output is correct |
14 |
Correct |
5 ms |
9812 KB |
Output is correct |
15 |
Correct |
5 ms |
9812 KB |
Output is correct |
16 |
Correct |
11 ms |
10860 KB |
Output is correct |
17 |
Correct |
10 ms |
10772 KB |
Output is correct |
18 |
Correct |
10 ms |
11220 KB |
Output is correct |
19 |
Correct |
11 ms |
11092 KB |
Output is correct |
20 |
Correct |
9 ms |
10580 KB |
Output is correct |
21 |
Correct |
9 ms |
10708 KB |
Output is correct |
22 |
Correct |
7 ms |
10580 KB |
Output is correct |
23 |
Correct |
10 ms |
10836 KB |
Output is correct |
24 |
Correct |
9 ms |
10840 KB |
Output is correct |
25 |
Correct |
5 ms |
9684 KB |
Output is correct |
26 |
Correct |
5 ms |
9684 KB |
Output is correct |
27 |
Incorrect |
11 ms |
10964 KB |
Output isn't correct |
28 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
9684 KB |
Output is correct |
2 |
Correct |
5 ms |
9760 KB |
Output is correct |
3 |
Correct |
5 ms |
9684 KB |
Output is correct |
4 |
Runtime error |
581 ms |
215828 KB |
Execution killed with signal 11 |
5 |
Halted |
0 ms |
0 KB |
- |