#include <bits/stdc++.h>
//#pragma GCC optimize("O3")
//#pragma GCC target("avx,avx2,fma")
#define sz(x) int((x).size())
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
using namespace std;
using ll = long long;
using ld = long double; // or double, if TL is tight
using str = string;
using ii = pair<int, int>;
using pl = pair<ll, ll>;
using vi = vector<int>;
using vll = vector<ll>;
const int INF = 1e9;
struct AIB {
///maxime pe sufix
int n;
vi V;
AIB(int N) : n(N + 1), V(N + 1, INF) {}
void insert(int p, int v) {
p = n - p - 1;
while(p < n) {
V[p] = min(V[p], v);
p += p & -p;
}
}
int query(int p) {
int re = INF;
p = n - p - 1;
while(p) {
re = min(re, V[p]);
p -= p & -p;
}
return re;
}
};
int main() {
cin.tie(0);
ios_base::sync_with_stdio(0);
int n;
cin >> n;
vi L(n), R(n);
for(int i = 0; i < n; ++i) {
cin >> L[i] >> R[i];
--L[i]; --R[i];
}
vi CS(n), CD(n);
int n0 = n;
while(n & (n - 1)) n += n & -n;
vector<vector<ii> > G(2 * n);
function<void(int, int, int, int, int, int, int)> add_edge_2 =
[&](int l, int r, int v, int w, int u, int s, int d) {
///adauga muchie de la segmentul (l, r) la v
if(r < s || d < l) return;
if(l <= s && d <= r) {
G[u].push_back({v, w});
return;
}
add_edge_2(l, r, v, w, u << 1, s, (s + d) >> 1);
add_edge_2(l, r, v, w, u << 1 | 1, ((s + d) >> 1) + 1, d);
};
auto add_edge = [&](int l, int r, int v, int w)
{ add_edge_2(l, r, v, w, 1, 0, n - 1); };
auto resetG = [&]() {
for(int i = 0; i < 2 * n; ++i) G[i].clear();
for(int i = 2; i < 2 * n; ++i)
G[i].push_back({i / 2, 0});
for(int i = 0; i < n0; ++i)
add_edge(L[i], R[i], i + n, 1);
};
vi Dist(2 * n);
auto Dijkstra = [&](int s) {
for(int i = 0; i < 2 * n; ++i) Dist[i] = INF;
Dist[s] = 0;
priority_queue<ii> PQ;
PQ.push({s, 0});
while(!PQ.empty()) {
auto [d, u] = PQ.top();
PQ.pop();
if(Dist[u] != -d) continue;
for(auto [it, w] : G[u]) {
if(Dist[it] > Dist[u] + w) {
Dist[it] = Dist[u] + w;
PQ.push({-Dist[it], it});
}
}
}
};
resetG();
for(int i = 0; i < n0; ++i)
if(L[i] == 0) G[0].push_back({i + n, 0});
Dijkstra(0);
for(int i = 0; i < n0; ++i) CS[i] = Dist[i + n];
resetG();
for(int i = 0; i < n0; ++i)
if(R[i] == n0 - 1) G[0].push_back({i + n, 0});
Dijkstra(0);
for(int i = 0; i < n0; ++i) CD[i] = Dist[i + n];
resetG();
for(int i = 0; i < n0; ++i)
G[0].push_back({i + n, CS[i] + CD[i]});
Dijkstra(0);
int q;
cin >> q;
for(int i = 0; i < q; ++i) {
int u;
cin >> u;
--u;
int re = Dist[u + n] + 1;
if(re >= INF) re = -1;
cout << re << "\n";
}
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
1024 ms |
69364 KB |
Output is correct |
5 |
Correct |
469 ms |
49448 KB |
Output is correct |
6 |
Correct |
320 ms |
46060 KB |
Output is correct |
7 |
Correct |
372 ms |
58480 KB |
Output is correct |
8 |
Correct |
296 ms |
57556 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
460 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
344 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
344 KB |
Output is correct |
10 |
Correct |
1 ms |
344 KB |
Output is correct |
11 |
Correct |
1 ms |
456 KB |
Output is correct |
12 |
Correct |
1 ms |
348 KB |
Output is correct |
13 |
Correct |
1 ms |
348 KB |
Output is correct |
14 |
Correct |
1 ms |
456 KB |
Output is correct |
15 |
Correct |
1 ms |
348 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
460 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
344 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
344 KB |
Output is correct |
10 |
Correct |
1 ms |
344 KB |
Output is correct |
11 |
Correct |
1 ms |
456 KB |
Output is correct |
12 |
Correct |
1 ms |
348 KB |
Output is correct |
13 |
Correct |
1 ms |
348 KB |
Output is correct |
14 |
Correct |
1 ms |
456 KB |
Output is correct |
15 |
Correct |
1 ms |
348 KB |
Output is correct |
16 |
Correct |
6 ms |
1240 KB |
Output is correct |
17 |
Correct |
5 ms |
976 KB |
Output is correct |
18 |
Correct |
6 ms |
1372 KB |
Output is correct |
19 |
Correct |
8 ms |
1368 KB |
Output is correct |
20 |
Correct |
4 ms |
1112 KB |
Output is correct |
21 |
Correct |
4 ms |
1112 KB |
Output is correct |
22 |
Correct |
4 ms |
1116 KB |
Output is correct |
23 |
Correct |
5 ms |
1116 KB |
Output is correct |
24 |
Correct |
6 ms |
1268 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
460 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
344 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
344 KB |
Output is correct |
10 |
Correct |
1 ms |
344 KB |
Output is correct |
11 |
Correct |
1 ms |
456 KB |
Output is correct |
12 |
Correct |
1 ms |
348 KB |
Output is correct |
13 |
Correct |
1 ms |
348 KB |
Output is correct |
14 |
Correct |
1 ms |
456 KB |
Output is correct |
15 |
Correct |
1 ms |
348 KB |
Output is correct |
16 |
Correct |
6 ms |
1240 KB |
Output is correct |
17 |
Correct |
5 ms |
976 KB |
Output is correct |
18 |
Correct |
6 ms |
1372 KB |
Output is correct |
19 |
Correct |
8 ms |
1368 KB |
Output is correct |
20 |
Correct |
4 ms |
1112 KB |
Output is correct |
21 |
Correct |
4 ms |
1112 KB |
Output is correct |
22 |
Correct |
4 ms |
1116 KB |
Output is correct |
23 |
Correct |
5 ms |
1116 KB |
Output is correct |
24 |
Correct |
6 ms |
1268 KB |
Output is correct |
25 |
Correct |
0 ms |
348 KB |
Output is correct |
26 |
Correct |
0 ms |
348 KB |
Output is correct |
27 |
Correct |
7 ms |
1112 KB |
Output is correct |
28 |
Correct |
7 ms |
1020 KB |
Output is correct |
29 |
Correct |
4 ms |
1116 KB |
Output is correct |
30 |
Correct |
4 ms |
1100 KB |
Output is correct |
31 |
Correct |
5 ms |
1220 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
1024 ms |
69364 KB |
Output is correct |
5 |
Correct |
469 ms |
49448 KB |
Output is correct |
6 |
Correct |
320 ms |
46060 KB |
Output is correct |
7 |
Correct |
372 ms |
58480 KB |
Output is correct |
8 |
Correct |
296 ms |
57556 KB |
Output is correct |
9 |
Correct |
0 ms |
344 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
460 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
344 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
1 ms |
344 KB |
Output is correct |
18 |
Correct |
1 ms |
344 KB |
Output is correct |
19 |
Correct |
1 ms |
456 KB |
Output is correct |
20 |
Correct |
1 ms |
348 KB |
Output is correct |
21 |
Correct |
1 ms |
348 KB |
Output is correct |
22 |
Correct |
1 ms |
456 KB |
Output is correct |
23 |
Correct |
1 ms |
348 KB |
Output is correct |
24 |
Correct |
6 ms |
1240 KB |
Output is correct |
25 |
Correct |
5 ms |
976 KB |
Output is correct |
26 |
Correct |
6 ms |
1372 KB |
Output is correct |
27 |
Correct |
8 ms |
1368 KB |
Output is correct |
28 |
Correct |
4 ms |
1112 KB |
Output is correct |
29 |
Correct |
4 ms |
1112 KB |
Output is correct |
30 |
Correct |
4 ms |
1116 KB |
Output is correct |
31 |
Correct |
5 ms |
1116 KB |
Output is correct |
32 |
Correct |
6 ms |
1268 KB |
Output is correct |
33 |
Correct |
0 ms |
348 KB |
Output is correct |
34 |
Correct |
0 ms |
348 KB |
Output is correct |
35 |
Correct |
7 ms |
1112 KB |
Output is correct |
36 |
Correct |
7 ms |
1020 KB |
Output is correct |
37 |
Correct |
4 ms |
1116 KB |
Output is correct |
38 |
Correct |
4 ms |
1100 KB |
Output is correct |
39 |
Correct |
5 ms |
1220 KB |
Output is correct |
40 |
Correct |
1103 ms |
75032 KB |
Output is correct |
41 |
Correct |
503 ms |
49864 KB |
Output is correct |
42 |
Correct |
706 ms |
79384 KB |
Output is correct |
43 |
Correct |
715 ms |
79112 KB |
Output is correct |
44 |
Correct |
317 ms |
46288 KB |
Output is correct |
45 |
Correct |
388 ms |
49768 KB |
Output is correct |
46 |
Correct |
191 ms |
24184 KB |
Output is correct |
47 |
Correct |
517 ms |
57584 KB |
Output is correct |
48 |
Correct |
611 ms |
60936 KB |
Output is correct |