#include <bits/extc++.h>
using namespace __gnu_pbds;
using namespace std;
// #define int long long
#define ll long long
// #define double long double
#define all(x) x.begin(), x.end()
#define debug(x) do{auto _x = x; cerr << #x << " = " << _x << endl;} while(0)
#define f first
#define s second
// #define endl '\n'
using pii = pair<int, int>;
using pll = pair<ll, ll>;
const int inf = 1 << 29;
// const ll inf = 1ll << 50;
const int MOD = 1e9 + 7;
signed main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
int n; cin >> n;
vector<int> l(n), r(n);
for(int i = 0; i < n; i++) {
cin >> l[i] >> r[i];
l[i]--;
}
if(n > 2500) {
vector<int> pref(n + 1);
for(int i = 0; i < n; i++) {
pref[i + 1] = max(pref[i], r[i]);
}
int ans = 1;
int cur = r[0];
while(true) {
if(cur == n) {
cout << ans << endl;
return 0;
}
int nxt = pref[cur];
if(nxt <= cur) {
cout << -1 << endl;
return 0;
}
cur = nxt;
ans++;
}
}
vector<vector<vector<pii>>> adj(n, vector<vector<pii>>(n + 1));
for(int i = 0; i < n; i++) {
for(int j = i + 1; j <= n; j++) {
if(i + 1 < j) {
adj[i + 1][j].push_back({i, j});
adj[i][j - 1].push_back({i, j});
}
adj[min(i, l[i])][max(j, r[i])].push_back({i, j});
adj[min(i, l[j - 1])][max(j, r[j - 1])].push_back({i, j});
}
}
vector<vector<int>> dist(n, vector<int>(n + 1, inf));
deque<pair<int, pii>> pq;
pq.push_back({0, {0, n}});
while(pq.size()) {
auto cur = pq.front(); pq.pop_front();
int d = cur.f, i = cur.s.f, j = cur.s.s;
if(dist[i][j] <= d) continue;
dist[i][j] = d;
for(auto nxt : adj[i][j]) {
if(nxt.s - nxt.f < j - i) {
pq.push_back({d + 1, nxt});
} else {
pq.push_front({d, nxt});
}
}
}
int q; cin >> q;
while(q--) {
int x; cin >> x; x--;
int ans = dist[x][x + 1];
if(ans == inf) ans = -1;
cout << ans << endl;
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
23 ms |
2648 KB |
Output is correct |
5 |
Correct |
27 ms |
5052 KB |
Output is correct |
6 |
Correct |
26 ms |
5200 KB |
Output is correct |
7 |
Correct |
21 ms |
4432 KB |
Output is correct |
8 |
Correct |
18 ms |
3928 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
344 KB |
Output is correct |
3 |
Correct |
0 ms |
348 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 |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
8 ms |
5468 KB |
Output is correct |
12 |
Correct |
9 ms |
5212 KB |
Output is correct |
13 |
Correct |
9 ms |
5468 KB |
Output is correct |
14 |
Correct |
8 ms |
5452 KB |
Output is correct |
15 |
Correct |
8 ms |
5212 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
344 KB |
Output is correct |
3 |
Correct |
0 ms |
348 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 |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
8 ms |
5468 KB |
Output is correct |
12 |
Correct |
9 ms |
5212 KB |
Output is correct |
13 |
Correct |
9 ms |
5468 KB |
Output is correct |
14 |
Correct |
8 ms |
5452 KB |
Output is correct |
15 |
Correct |
8 ms |
5212 KB |
Output is correct |
16 |
Correct |
844 ms |
356600 KB |
Output is correct |
17 |
Correct |
822 ms |
341424 KB |
Output is correct |
18 |
Correct |
759 ms |
346628 KB |
Output is correct |
19 |
Correct |
651 ms |
343812 KB |
Output is correct |
20 |
Correct |
1113 ms |
318980 KB |
Output is correct |
21 |
Correct |
844 ms |
336944 KB |
Output is correct |
22 |
Correct |
475 ms |
371168 KB |
Output is correct |
23 |
Correct |
611 ms |
386960 KB |
Output is correct |
24 |
Correct |
678 ms |
377724 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
344 KB |
Output is correct |
3 |
Correct |
0 ms |
348 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 |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
8 ms |
5468 KB |
Output is correct |
12 |
Correct |
9 ms |
5212 KB |
Output is correct |
13 |
Correct |
9 ms |
5468 KB |
Output is correct |
14 |
Correct |
8 ms |
5452 KB |
Output is correct |
15 |
Correct |
8 ms |
5212 KB |
Output is correct |
16 |
Correct |
844 ms |
356600 KB |
Output is correct |
17 |
Correct |
822 ms |
341424 KB |
Output is correct |
18 |
Correct |
759 ms |
346628 KB |
Output is correct |
19 |
Correct |
651 ms |
343812 KB |
Output is correct |
20 |
Correct |
1113 ms |
318980 KB |
Output is correct |
21 |
Correct |
844 ms |
336944 KB |
Output is correct |
22 |
Correct |
475 ms |
371168 KB |
Output is correct |
23 |
Correct |
611 ms |
386960 KB |
Output is correct |
24 |
Correct |
678 ms |
377724 KB |
Output is correct |
25 |
Correct |
1 ms |
344 KB |
Output is correct |
26 |
Correct |
0 ms |
344 KB |
Output is correct |
27 |
Correct |
931 ms |
381876 KB |
Output is correct |
28 |
Correct |
839 ms |
345384 KB |
Output is correct |
29 |
Correct |
1142 ms |
319024 KB |
Output is correct |
30 |
Correct |
868 ms |
336392 KB |
Output is correct |
31 |
Correct |
590 ms |
368196 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
23 ms |
2648 KB |
Output is correct |
5 |
Correct |
27 ms |
5052 KB |
Output is correct |
6 |
Correct |
26 ms |
5200 KB |
Output is correct |
7 |
Correct |
21 ms |
4432 KB |
Output is correct |
8 |
Correct |
18 ms |
3928 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
344 KB |
Output is correct |
11 |
Correct |
0 ms |
348 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 |
348 KB |
Output is correct |
16 |
Correct |
1 ms |
348 KB |
Output is correct |
17 |
Correct |
0 ms |
348 KB |
Output is correct |
18 |
Correct |
0 ms |
348 KB |
Output is correct |
19 |
Correct |
8 ms |
5468 KB |
Output is correct |
20 |
Correct |
9 ms |
5212 KB |
Output is correct |
21 |
Correct |
9 ms |
5468 KB |
Output is correct |
22 |
Correct |
8 ms |
5452 KB |
Output is correct |
23 |
Correct |
8 ms |
5212 KB |
Output is correct |
24 |
Correct |
844 ms |
356600 KB |
Output is correct |
25 |
Correct |
822 ms |
341424 KB |
Output is correct |
26 |
Correct |
759 ms |
346628 KB |
Output is correct |
27 |
Correct |
651 ms |
343812 KB |
Output is correct |
28 |
Correct |
1113 ms |
318980 KB |
Output is correct |
29 |
Correct |
844 ms |
336944 KB |
Output is correct |
30 |
Correct |
475 ms |
371168 KB |
Output is correct |
31 |
Correct |
611 ms |
386960 KB |
Output is correct |
32 |
Correct |
678 ms |
377724 KB |
Output is correct |
33 |
Correct |
1 ms |
344 KB |
Output is correct |
34 |
Correct |
0 ms |
344 KB |
Output is correct |
35 |
Correct |
931 ms |
381876 KB |
Output is correct |
36 |
Correct |
839 ms |
345384 KB |
Output is correct |
37 |
Correct |
1142 ms |
319024 KB |
Output is correct |
38 |
Correct |
868 ms |
336392 KB |
Output is correct |
39 |
Correct |
590 ms |
368196 KB |
Output is correct |
40 |
Incorrect |
26 ms |
5324 KB |
Output isn't correct |
41 |
Halted |
0 ms |
0 KB |
- |