#include <bits/stdc++.h>
using namespace std;
#define db(...) " [" << #__VA_ARGS__ << " : " << (__VA_ARGS__) << "] "
const int N = 100005;
int n, q;
int s[N], e[N], p[N];
bool equal(int i, int j) {
return s[i] == s[j] && e[i] == e[j];
}
namespace sub3 {
bool valid_input() {
return n <= 5000;
}
int dp[5005][5005];
void solve() {
for (int i = 1; i <= n; ++i) {
for (int j = 1; j <= n; ++j) {
dp[i][j] = 1e9;
}
}
queue<int> qu;
for (int i = 1; i <= n; ++i) {
while (qu.size()) {
qu.pop();
}
dp[p[i]][p[i]] = 0; // just to remember
qu.emplace(p[i]);
for (int j = n; j >= 1; --j) {
if (e[p[j]] > e[p[i]]) continue;
int &res = dp[p[j]][p[i]];
while (qu.size() && s[qu.front()] > e[p[j]]) {
qu.pop();
}
if (qu.size()) {
res = min(res, dp[qu.front()][p[i]] + 1);
}
if (res != 1e9) {
qu.emplace(p[j]);
}
}
}
while (q--) {
int s, t;
cin >> s >> t;
if (dp[s][t] == 1e9) {
cout << "impossible\n";
} else {
cout << dp[s][t] << '\n';
}
}
}
}
signed main() {
ios::sync_with_stdio(false);
cin.tie(0);
cin >> n >> q;
for (int i = 1; i <= n; ++i) {
cin >> s[i] >> e[i];
}
vector<int> ord(n);
iota(ord.begin(), ord.end(), 1);
sort(ord.begin(), ord.end(), [&](int x, int y) {
return e[x] == e[y] ? s[x] < s[y] : e[x] < e[y];
});
for (int i = 0; i < n; ++i) {
p[i + 1] = ord[i];
}
if (sub3::valid_input()) {
sub3::solve();
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2392 KB |
Output is correct |
2 |
Incorrect |
24 ms |
3216 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2392 KB |
Output is correct |
2 |
Correct |
1 ms |
2396 KB |
Output is correct |
3 |
Correct |
10 ms |
22876 KB |
Output is correct |
4 |
Correct |
8 ms |
23112 KB |
Output is correct |
5 |
Correct |
10 ms |
22876 KB |
Output is correct |
6 |
Correct |
10 ms |
22876 KB |
Output is correct |
7 |
Correct |
10 ms |
22876 KB |
Output is correct |
8 |
Correct |
10 ms |
23108 KB |
Output is correct |
9 |
Correct |
13 ms |
23108 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2392 KB |
Output is correct |
2 |
Correct |
1 ms |
2396 KB |
Output is correct |
3 |
Correct |
10 ms |
22876 KB |
Output is correct |
4 |
Correct |
8 ms |
23112 KB |
Output is correct |
5 |
Correct |
10 ms |
22876 KB |
Output is correct |
6 |
Correct |
10 ms |
22876 KB |
Output is correct |
7 |
Correct |
10 ms |
22876 KB |
Output is correct |
8 |
Correct |
10 ms |
23108 KB |
Output is correct |
9 |
Correct |
13 ms |
23108 KB |
Output is correct |
10 |
Correct |
1 ms |
2396 KB |
Output is correct |
11 |
Correct |
1 ms |
2396 KB |
Output is correct |
12 |
Correct |
11 ms |
23104 KB |
Output is correct |
13 |
Correct |
8 ms |
22876 KB |
Output is correct |
14 |
Correct |
10 ms |
22876 KB |
Output is correct |
15 |
Correct |
8 ms |
23128 KB |
Output is correct |
16 |
Correct |
9 ms |
22876 KB |
Output is correct |
17 |
Correct |
9 ms |
22872 KB |
Output is correct |
18 |
Correct |
13 ms |
22876 KB |
Output is correct |
19 |
Correct |
690 ms |
99652 KB |
Output is correct |
20 |
Correct |
817 ms |
99388 KB |
Output is correct |
21 |
Correct |
780 ms |
99920 KB |
Output is correct |
22 |
Correct |
565 ms |
99920 KB |
Output is correct |
23 |
Correct |
630 ms |
100676 KB |
Output is correct |
24 |
Correct |
632 ms |
100660 KB |
Output is correct |
25 |
Correct |
811 ms |
100432 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2392 KB |
Output is correct |
2 |
Correct |
1 ms |
2396 KB |
Output is correct |
3 |
Correct |
10 ms |
22876 KB |
Output is correct |
4 |
Correct |
8 ms |
23112 KB |
Output is correct |
5 |
Correct |
10 ms |
22876 KB |
Output is correct |
6 |
Correct |
10 ms |
22876 KB |
Output is correct |
7 |
Correct |
10 ms |
22876 KB |
Output is correct |
8 |
Correct |
10 ms |
23108 KB |
Output is correct |
9 |
Correct |
13 ms |
23108 KB |
Output is correct |
10 |
Correct |
1 ms |
2396 KB |
Output is correct |
11 |
Correct |
0 ms |
2396 KB |
Output is correct |
12 |
Correct |
11 ms |
23100 KB |
Output is correct |
13 |
Correct |
9 ms |
22872 KB |
Output is correct |
14 |
Correct |
10 ms |
22876 KB |
Output is correct |
15 |
Correct |
8 ms |
22872 KB |
Output is correct |
16 |
Correct |
9 ms |
22876 KB |
Output is correct |
17 |
Correct |
10 ms |
23108 KB |
Output is correct |
18 |
Correct |
18 ms |
22876 KB |
Output is correct |
19 |
Incorrect |
23 ms |
3416 KB |
Output isn't correct |
20 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
23 ms |
3416 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2392 KB |
Output is correct |
2 |
Incorrect |
24 ms |
3216 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |