#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);
auto [l, r] = intervalles[fin];
if (intervalles[debut].second >= l and intervalles[debut].second <= r) {
cout << sol + 1 << '\n';
continue;
}
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 |
212 KB |
Output is correct |
2 |
Correct |
99 ms |
23844 KB |
Output is correct |
3 |
Correct |
117 ms |
23724 KB |
Output is correct |
4 |
Correct |
191 ms |
23692 KB |
Output is correct |
5 |
Correct |
132 ms |
27032 KB |
Output is correct |
6 |
Correct |
143 ms |
27112 KB |
Output is correct |
7 |
Correct |
131 ms |
26992 KB |
Output is correct |
8 |
Correct |
141 ms |
31692 KB |
Output is correct |
9 |
Correct |
160 ms |
31488 KB |
Output is correct |
10 |
Correct |
148 ms |
27252 KB |
Output is correct |
11 |
Incorrect |
148 ms |
31360 KB |
Output isn't correct |
12 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
468 KB |
Output is correct |
4 |
Correct |
1 ms |
468 KB |
Output is correct |
5 |
Correct |
1 ms |
460 KB |
Output is correct |
6 |
Incorrect |
1 ms |
468 KB |
Output isn't correct |
7 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
468 KB |
Output is correct |
4 |
Correct |
1 ms |
468 KB |
Output is correct |
5 |
Correct |
1 ms |
460 KB |
Output is correct |
6 |
Incorrect |
1 ms |
468 KB |
Output isn't correct |
7 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
468 KB |
Output is correct |
4 |
Correct |
1 ms |
468 KB |
Output is correct |
5 |
Correct |
1 ms |
460 KB |
Output is correct |
6 |
Incorrect |
1 ms |
468 KB |
Output isn't correct |
7 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
99 ms |
23812 KB |
Output is correct |
2 |
Correct |
113 ms |
23820 KB |
Output is correct |
3 |
Correct |
183 ms |
23804 KB |
Output is correct |
4 |
Correct |
144 ms |
31480 KB |
Output is correct |
5 |
Correct |
146 ms |
27364 KB |
Output is correct |
6 |
Correct |
188 ms |
31096 KB |
Output is correct |
7 |
Correct |
161 ms |
31084 KB |
Output is correct |
8 |
Incorrect |
142 ms |
31184 KB |
Output isn't correct |
9 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
99 ms |
23844 KB |
Output is correct |
3 |
Correct |
117 ms |
23724 KB |
Output is correct |
4 |
Correct |
191 ms |
23692 KB |
Output is correct |
5 |
Correct |
132 ms |
27032 KB |
Output is correct |
6 |
Correct |
143 ms |
27112 KB |
Output is correct |
7 |
Correct |
131 ms |
26992 KB |
Output is correct |
8 |
Correct |
141 ms |
31692 KB |
Output is correct |
9 |
Correct |
160 ms |
31488 KB |
Output is correct |
10 |
Correct |
148 ms |
27252 KB |
Output is correct |
11 |
Incorrect |
148 ms |
31360 KB |
Output isn't correct |
12 |
Halted |
0 ms |
0 KB |
- |