#include <iostream>
#include <vector>
#include <algorithm>
#include <cassert>
#include <cstring>
#include <functional>
#include <queue>
#warning That's the baby, that's not my baby
typedef long long ll;
const int NMAX = 2500;
/**
ce cred eu ca se intampla defapt e ca mereu voi putea accesa un interval de tari
si atunci o sa am o dinamica de genu
dp[i][l] =def= care e cel mai din dreapta r la care pot ajunge dupa i operatii, a.i. am capatul din stanga pe pozitia l
asta pare greu de optimizat
dp[l][r] =def= #min de operatii ca sa ajung cu capatul stanga in l si capatul dreapta in r
ok si facem asta gen invers si iese 0-1 bfs
**/
struct Country {
int l, r;
};
int dist[NMAX * NMAX + 1];
std::vector<std::pair<int, int>> g[NMAX * NMAX + 1];
int main() {
std::ios_base::sync_with_stdio(false);
std::cin.tie(0);
int n;
std::cin >> n;
std::vector<Country> v(n + 1);
int rMax[n + 1] = {};
for (int i = 1; i <= n; i++) {
std::cin >> v[i].l >> v[i].r;
rMax[i] = std::max(rMax[i - 1], v[i].r);
}
std::function<int(int, int)> encode = [&] (int i, int j) -> int {
return (i - 1) * n + j;
};
if (n <= 2500) {
for (int i = 1; i <= n * n; i++) {
dist[i] = 1e9;
}
dist[encode(1, n)] = 0;
for (int i = 1; i <= n; i++) {
int newL = n + 1, newR = 0;
for (int j = i; j <= n; j++) {
newL = std::min(newL, v[j].l);
newR = std::max(newR, v[j].r);
g[encode(newL, j)].push_back({encode(i, j), 1});
g[encode(i, newR)].push_back({encode(i, j), 1});
g[encode(v[i].l, v[i].r)].push_back({encode(i, j), 1});
if (i != j) {
g[encode(i + 1, j)].push_back({encode(i, j), 0});
}
}
}
std::deque<int> deq;
deq.push_back(encode(1, n));
while (!deq.empty()) {
int u = deq.front();
deq.pop_front();
for (const auto &[v, c] : g[u]) {
if (dist[u] + c < dist[v]) {
if (c == 0) {
deq.push_front(v);
} else {
deq.push_back(v);
}
dist[v] = dist[u] + c;
}
}
}
for (int i = 1; i <= n * n; i++) {
dist[i] = (dist[i] == 1e9? -1 : dist[i]);
}
}
int Q;
std::cin >> Q;
while (Q--) {
int x;
std::cin >> x;
if (x == 1) {
int answer = 0;
while (x != n && rMax[x] > x) {
x = rMax[x];
answer++;
}
if (x != n) {
answer = -1;
}
std::cout << answer << '\n';
} else {
std::cout << dist[encode(x, x)] << '\n';
}
}
return 0;
}
Compilation message
passport.cpp:8:2: warning: #warning That's the baby, that's not my baby [-Wcpp]
8 | #warning That's the baby, that's not my baby
| ^~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
32 ms |
148304 KB |
Output is correct |
2 |
Correct |
32 ms |
148324 KB |
Output is correct |
3 |
Correct |
32 ms |
148316 KB |
Output is correct |
4 |
Correct |
56 ms |
150612 KB |
Output is correct |
5 |
Correct |
55 ms |
150620 KB |
Output is correct |
6 |
Correct |
55 ms |
150620 KB |
Output is correct |
7 |
Correct |
52 ms |
150620 KB |
Output is correct |
8 |
Correct |
49 ms |
150112 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
31 ms |
148316 KB |
Output is correct |
2 |
Correct |
32 ms |
148316 KB |
Output is correct |
3 |
Correct |
31 ms |
148312 KB |
Output is correct |
4 |
Correct |
32 ms |
148316 KB |
Output is correct |
5 |
Correct |
35 ms |
148316 KB |
Output is correct |
6 |
Correct |
34 ms |
148252 KB |
Output is correct |
7 |
Correct |
32 ms |
148316 KB |
Output is correct |
8 |
Correct |
32 ms |
148308 KB |
Output is correct |
9 |
Correct |
33 ms |
148312 KB |
Output is correct |
10 |
Correct |
31 ms |
148568 KB |
Output is correct |
11 |
Correct |
39 ms |
151004 KB |
Output is correct |
12 |
Correct |
39 ms |
150876 KB |
Output is correct |
13 |
Correct |
38 ms |
151124 KB |
Output is correct |
14 |
Correct |
39 ms |
151128 KB |
Output is correct |
15 |
Correct |
38 ms |
150868 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
31 ms |
148316 KB |
Output is correct |
2 |
Correct |
32 ms |
148316 KB |
Output is correct |
3 |
Correct |
31 ms |
148312 KB |
Output is correct |
4 |
Correct |
32 ms |
148316 KB |
Output is correct |
5 |
Correct |
35 ms |
148316 KB |
Output is correct |
6 |
Correct |
34 ms |
148252 KB |
Output is correct |
7 |
Correct |
32 ms |
148316 KB |
Output is correct |
8 |
Correct |
32 ms |
148308 KB |
Output is correct |
9 |
Correct |
33 ms |
148312 KB |
Output is correct |
10 |
Correct |
31 ms |
148568 KB |
Output is correct |
11 |
Correct |
39 ms |
151004 KB |
Output is correct |
12 |
Correct |
39 ms |
150876 KB |
Output is correct |
13 |
Correct |
38 ms |
151124 KB |
Output is correct |
14 |
Correct |
39 ms |
151128 KB |
Output is correct |
15 |
Correct |
38 ms |
150868 KB |
Output is correct |
16 |
Correct |
431 ms |
349156 KB |
Output is correct |
17 |
Correct |
694 ms |
351572 KB |
Output is correct |
18 |
Correct |
503 ms |
361296 KB |
Output is correct |
19 |
Correct |
434 ms |
359508 KB |
Output is correct |
20 |
Correct |
578 ms |
349404 KB |
Output is correct |
21 |
Correct |
497 ms |
341076 KB |
Output is correct |
22 |
Correct |
397 ms |
371868 KB |
Output is correct |
23 |
Correct |
481 ms |
373824 KB |
Output is correct |
24 |
Correct |
464 ms |
374672 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
31 ms |
148316 KB |
Output is correct |
2 |
Correct |
32 ms |
148316 KB |
Output is correct |
3 |
Correct |
31 ms |
148312 KB |
Output is correct |
4 |
Correct |
32 ms |
148316 KB |
Output is correct |
5 |
Correct |
35 ms |
148316 KB |
Output is correct |
6 |
Correct |
34 ms |
148252 KB |
Output is correct |
7 |
Correct |
32 ms |
148316 KB |
Output is correct |
8 |
Correct |
32 ms |
148308 KB |
Output is correct |
9 |
Correct |
33 ms |
148312 KB |
Output is correct |
10 |
Correct |
31 ms |
148568 KB |
Output is correct |
11 |
Correct |
39 ms |
151004 KB |
Output is correct |
12 |
Correct |
39 ms |
150876 KB |
Output is correct |
13 |
Correct |
38 ms |
151124 KB |
Output is correct |
14 |
Correct |
39 ms |
151128 KB |
Output is correct |
15 |
Correct |
38 ms |
150868 KB |
Output is correct |
16 |
Correct |
431 ms |
349156 KB |
Output is correct |
17 |
Correct |
694 ms |
351572 KB |
Output is correct |
18 |
Correct |
503 ms |
361296 KB |
Output is correct |
19 |
Correct |
434 ms |
359508 KB |
Output is correct |
20 |
Correct |
578 ms |
349404 KB |
Output is correct |
21 |
Correct |
497 ms |
341076 KB |
Output is correct |
22 |
Correct |
397 ms |
371868 KB |
Output is correct |
23 |
Correct |
481 ms |
373824 KB |
Output is correct |
24 |
Correct |
464 ms |
374672 KB |
Output is correct |
25 |
Correct |
32 ms |
148312 KB |
Output is correct |
26 |
Correct |
31 ms |
148380 KB |
Output is correct |
27 |
Correct |
439 ms |
364008 KB |
Output is correct |
28 |
Correct |
676 ms |
354132 KB |
Output is correct |
29 |
Correct |
583 ms |
349320 KB |
Output is correct |
30 |
Correct |
478 ms |
340816 KB |
Output is correct |
31 |
Correct |
429 ms |
361700 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
32 ms |
148304 KB |
Output is correct |
2 |
Correct |
32 ms |
148324 KB |
Output is correct |
3 |
Correct |
32 ms |
148316 KB |
Output is correct |
4 |
Correct |
56 ms |
150612 KB |
Output is correct |
5 |
Correct |
55 ms |
150620 KB |
Output is correct |
6 |
Correct |
55 ms |
150620 KB |
Output is correct |
7 |
Correct |
52 ms |
150620 KB |
Output is correct |
8 |
Correct |
49 ms |
150112 KB |
Output is correct |
9 |
Correct |
31 ms |
148316 KB |
Output is correct |
10 |
Correct |
32 ms |
148316 KB |
Output is correct |
11 |
Correct |
31 ms |
148312 KB |
Output is correct |
12 |
Correct |
32 ms |
148316 KB |
Output is correct |
13 |
Correct |
35 ms |
148316 KB |
Output is correct |
14 |
Correct |
34 ms |
148252 KB |
Output is correct |
15 |
Correct |
32 ms |
148316 KB |
Output is correct |
16 |
Correct |
32 ms |
148308 KB |
Output is correct |
17 |
Correct |
33 ms |
148312 KB |
Output is correct |
18 |
Correct |
31 ms |
148568 KB |
Output is correct |
19 |
Correct |
39 ms |
151004 KB |
Output is correct |
20 |
Correct |
39 ms |
150876 KB |
Output is correct |
21 |
Correct |
38 ms |
151124 KB |
Output is correct |
22 |
Correct |
39 ms |
151128 KB |
Output is correct |
23 |
Correct |
38 ms |
150868 KB |
Output is correct |
24 |
Correct |
431 ms |
349156 KB |
Output is correct |
25 |
Correct |
694 ms |
351572 KB |
Output is correct |
26 |
Correct |
503 ms |
361296 KB |
Output is correct |
27 |
Correct |
434 ms |
359508 KB |
Output is correct |
28 |
Correct |
578 ms |
349404 KB |
Output is correct |
29 |
Correct |
497 ms |
341076 KB |
Output is correct |
30 |
Correct |
397 ms |
371868 KB |
Output is correct |
31 |
Correct |
481 ms |
373824 KB |
Output is correct |
32 |
Correct |
464 ms |
374672 KB |
Output is correct |
33 |
Correct |
32 ms |
148312 KB |
Output is correct |
34 |
Correct |
31 ms |
148380 KB |
Output is correct |
35 |
Correct |
439 ms |
364008 KB |
Output is correct |
36 |
Correct |
676 ms |
354132 KB |
Output is correct |
37 |
Correct |
583 ms |
349320 KB |
Output is correct |
38 |
Correct |
478 ms |
340816 KB |
Output is correct |
39 |
Correct |
429 ms |
361700 KB |
Output is correct |
40 |
Runtime error |
165 ms |
307848 KB |
Execution killed with signal 11 |
41 |
Halted |
0 ms |
0 KB |
- |