#include <bits/stdc++.h>
#define fr(i, a, b) for (int i = (a); i <= (b); ++i)
#define rf(i, a, b) for (int i = (a); i >= (b); --i)
#define fe(x, y) for (auto& x : y)
#define fi first
#define se second
#define pb push_back
#define mp make_pair
#define mt make_tuple
#define all(x) (x).begin(), (x).end()
#define sz(x) (int)(x).size()
#define pw(x) (1LL << (x))
using namespace std;
mt19937_64 rng(228);
#include <ext/pb_ds/assoc_container.hpp>
using namespace __gnu_pbds;
template <typename T>
using oset = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
#define fbo find_by_order
#define ook order_of_key
template <typename T>
bool umn(T& a, T b) {
return a > b ? a = b, 1 : 0;
}
template <typename T>
bool umx(T& a, T b) {
return a < b ? a = b, 1 : 0;
}
using ll = long long;
using ld = long double;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
template <typename T>
using ve = vector<T>;
const int N = 1e5 + 5;
int n, q;
pii a[N];
ve<int> order;
ve<int> st;
int to[N];
set<pii> R;
int main() {
#ifdef LOCAL
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#else
ios::sync_with_stdio(0);
cin.tie(0);
#endif
cin >> n >> q;
fr (i, 1, n) {
cin >> a[i].fi >> a[i].se;
order.pb(i);
}
sort(all(order), [](int i, int j) {
return a[i].fi < a[j].fi || (a[i].fi == a[j].fi && a[i].se > a[j].se);
});
fe (i, order) {
if (sz(st) && a[st.back()].se >= a[i].se) {
to[i] = st.back();
continue;
}
st.pb(i);
}
fr (i, 1, sz(st) - 1) {
assert(a[st[i]].fi >= a[st[i - 1]].fi);
assert(a[st[i]].se >= a[st[i - 1]].se);
}
{
int mx = -1e9;
int mxI = -1;
int ptr = 0;
fe (i, st) {
while (ptr < sz(st) && a[st[ptr]].fi <= a[i].se) {
if (umx(mx, a[st[ptr]].se)) {
mxI = st[ptr];
}
ptr++;
}
if (mx >= a[i].se) {
to[i] = mxI;
} else {
to[i] = -1;
}
}
}
// fr (i, 1, n) {
// cout << i << " " << to[i] << "\n";
// }
// cout << "\n";
// fe (x, st) {
// cout << x << " " << a[x].fi << " " << a[x].se << "\n";
// }
fr (i, 1, q) {
int s, e;
cin >> s >> e;
if (s == e) {
cout << "0\n";
continue;
}
if (a[s].se > a[e].se) {
cout << "impossible\n";
continue;
}
int v = s;
int ans = 0;
while (1) {
if (a[to[v]].se == a[v].se || a[to[v]].se > a[e].se || a[v].se >= a[e].fi) {
break;
}
v = to[v];
ans++;
}
if (a[v].se >= a[e].fi) {
cout << ans + 1 << "\n";
} else {
cout << "impossible\n";
}
}
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Execution timed out |
1585 ms |
2496 KB |
Time limit exceeded |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Incorrect |
1 ms |
340 KB |
Output isn't correct |
6 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Incorrect |
1 ms |
340 KB |
Output isn't correct |
6 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Incorrect |
1 ms |
340 KB |
Output isn't correct |
6 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
1587 ms |
2484 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Execution timed out |
1585 ms |
2496 KB |
Time limit exceeded |
3 |
Halted |
0 ms |
0 KB |
- |