#include <bits/stdc++.h>
#define int long long
using namespace std;
string to_string(string s) { return s; }
template <typename T> string to_string(T v) {
bool first = true;
string res = "[";
for (const auto &x : v) {
if (!first)
res += ", ";
first = false;
res += to_string(x);
}
res += "]";
return res;
}
void dbg_out() { cout << endl; }
template <typename Head, typename... Tail> void dbg_out(Head H, Tail... T) {
cout << ' ' << to_string(H);
dbg_out(T...);
}
#ifdef DEBUG
#define dbg(...) cout << "(" << #__VA_ARGS__ << "):", dbg_out(__VA_ARGS__)
#else
#define dbg(...)
#endif
const int MAXP = 20;
signed main(void) {
ios_base::sync_with_stdio(false);
cin.tie(0);
int nbInt, nbRequetes;
cin >> nbInt >> nbRequetes;
vector<pair<int, int>> intervalles(nbInt);
vector<int> valeurs;
for (auto &[l, r] : intervalles) {
cin >> l >> r;
valeurs.push_back(l);
valeurs.push_back(r);
}
sort(valeurs.begin(), valeurs.end());
valeurs.resize(unique(valeurs.begin(), valeurs.end()) - valeurs.begin());
for (auto &[l, r] : intervalles) {
l = lower_bound(valeurs.begin(), valeurs.end(), l) - valeurs.begin();
r = lower_bound(valeurs.begin(), valeurs.end(), r) - valeurs.begin();
}
array<vector<int>, MAXP> prv;
for (int p = 0; p < MAXP; ++p)
prv[p].resize(nbInt);
int deb = 1;
int nbValeurs = valeurs.size();
while (deb < nbValeurs)
deb *= 2;
vector<pair<int, int>> seg(2 * deb, pair(1e18, 1e18));
for (int i = 0; i < nbInt; ++i) {
auto [l, r] = intervalles[i];
seg[deb + r] = min(seg[deb + r], pair(l, i));
}
for (int i = deb - 1; i; --i)
seg[i] = min(seg[2 * i], seg[2 * i + 1]);
auto query = [&](int l, int r) { // [l, r)
l += deb, r += deb;
pair<int, int> sol(1e18, 1e18);
while (l < r) {
if (l % 2)
sol = min(sol, seg[l++]);
if (r % 2)
sol = min(sol, seg[--r]);
l /= 2;
r /= 2;
}
return sol.second;
};
for (int i = 0; i < nbInt; ++i) {
auto [l, r] = intervalles[i];
prv[0][i] = query(l, r + 1);
// dbg(l, r, prv[0][i]);
}
dbg(prv[0]);
for (int p = 0; p < MAXP - 1; ++p)
for (int i = 0; i < nbInt; ++i)
prv[p + 1][i] = prv[p][prv[p][i]];
for (int iRequete = 0; iRequete < nbRequetes; ++iRequete) {
int debut, fin;
cin >> debut >> fin;
--debut, --fin;
if (debut == fin) {
cout << 0 << '\n';
continue;
}
int sol = 0;
for (int p = MAXP - 1; p >= 0; --p)
if (intervalles[prv[p][fin]].first > intervalles[debut].second)
sol += 1 << p, fin = prv[p][fin];
dbg(sol);
sol++;
fin = prv[0][fin];
if (intervalles[fin].first > intervalles[debut].second or
intervalles[fin].second < intervalles[debut].first)
cout << "impossible\n";
else
cout << sol + 1 << '\n';
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
103 ms |
26864 KB |
Output is correct |
3 |
Correct |
121 ms |
26864 KB |
Output is correct |
4 |
Incorrect |
180 ms |
26836 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
2 ms |
468 KB |
Output is correct |
4 |
Incorrect |
1 ms |
468 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
2 ms |
468 KB |
Output is correct |
4 |
Incorrect |
1 ms |
468 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
2 ms |
468 KB |
Output is correct |
4 |
Incorrect |
1 ms |
468 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
98 ms |
26980 KB |
Output is correct |
2 |
Correct |
122 ms |
26848 KB |
Output is correct |
3 |
Incorrect |
190 ms |
26864 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
103 ms |
26864 KB |
Output is correct |
3 |
Correct |
121 ms |
26864 KB |
Output is correct |
4 |
Incorrect |
180 ms |
26836 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |