#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];
namespace sub3 {
bool valid_input() {
return n <= 5000;
}
int dp[5005][5005];
void solve() {
multiset<int> cur;
priority_queue<pair<int, int>> pq;
for (int i = 1; i <= n; ++i) {
for (int j = 1; j <= n; ++j) {
dp[i][j] = 1e9;
}
}
for (int i = 1; i <= n; ++i) {
while (pq.size()) {
pq.pop();
}
cur.clear();
dp[p[i]][p[i]] = 0; // just to remember
cur.insert(0);
pq.emplace(s[p[i]], p[i]);
for (int j = i - 1; j >= 1; --j) {
int &res = dp[p[j]][p[i]];
while (pq.size() && pq.top().first > e[p[j]]) {
cur.erase(cur.find(dp[pq.top().second][p[i]]));
pq.pop();
}
if (cur.size()) {
res = min(res, *cur.begin() + 1);
}
cur.insert(res);
pq.emplace(s[p[j]], p[j]);
}
}
while (q--) {
int s, t;
cin >> s >> t;
if (dp[s][t] == 1e9) {
cout << "impossbile\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();
}
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1 ms |
2396 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1 ms |
2392 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1 ms |
2392 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1 ms |
2392 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
25 ms |
5460 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1 ms |
2396 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |