#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].second)
cout << "impossible\n";
else
cout << sol + 1 << '\n';
}
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
87 ms |
23736 KB |
Output is correct |
3 |
Correct |
139 ms |
23812 KB |
Output is correct |
4 |
Correct |
191 ms |
23748 KB |
Output is correct |
5 |
Correct |
124 ms |
23792 KB |
Output is correct |
6 |
Correct |
122 ms |
23820 KB |
Output is correct |
7 |
Correct |
136 ms |
24012 KB |
Output is correct |
8 |
Correct |
161 ms |
28412 KB |
Output is correct |
9 |
Correct |
133 ms |
28432 KB |
Output is correct |
10 |
Correct |
147 ms |
24092 KB |
Output is correct |
11 |
Correct |
150 ms |
28288 KB |
Output is correct |
12 |
Correct |
58 ms |
24260 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
1 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 |
468 KB |
Output is correct |
6 |
Correct |
1 ms |
468 KB |
Output is correct |
7 |
Correct |
1 ms |
468 KB |
Output is correct |
8 |
Correct |
1 ms |
468 KB |
Output is correct |
9 |
Correct |
1 ms |
468 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
1 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 |
468 KB |
Output is correct |
6 |
Correct |
1 ms |
468 KB |
Output is correct |
7 |
Correct |
1 ms |
468 KB |
Output is correct |
8 |
Correct |
1 ms |
468 KB |
Output is correct |
9 |
Correct |
1 ms |
468 KB |
Output is correct |
10 |
Correct |
0 ms |
212 KB |
Output is correct |
11 |
Correct |
1 ms |
212 KB |
Output is correct |
12 |
Correct |
1 ms |
468 KB |
Output is correct |
13 |
Correct |
1 ms |
468 KB |
Output is correct |
14 |
Correct |
1 ms |
496 KB |
Output is correct |
15 |
Correct |
1 ms |
468 KB |
Output is correct |
16 |
Correct |
1 ms |
468 KB |
Output is correct |
17 |
Correct |
1 ms |
468 KB |
Output is correct |
18 |
Correct |
1 ms |
468 KB |
Output is correct |
19 |
Correct |
39 ms |
2008 KB |
Output is correct |
20 |
Correct |
41 ms |
1956 KB |
Output is correct |
21 |
Correct |
34 ms |
2296 KB |
Output is correct |
22 |
Correct |
28 ms |
2260 KB |
Output is correct |
23 |
Correct |
27 ms |
2356 KB |
Output is correct |
24 |
Correct |
33 ms |
2388 KB |
Output is correct |
25 |
Correct |
28 ms |
1968 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
1 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 |
468 KB |
Output is correct |
6 |
Correct |
1 ms |
468 KB |
Output is correct |
7 |
Correct |
1 ms |
468 KB |
Output is correct |
8 |
Correct |
1 ms |
468 KB |
Output is correct |
9 |
Correct |
1 ms |
468 KB |
Output is correct |
10 |
Correct |
0 ms |
212 KB |
Output is correct |
11 |
Correct |
1 ms |
212 KB |
Output is correct |
12 |
Correct |
1 ms |
468 KB |
Output is correct |
13 |
Correct |
1 ms |
596 KB |
Output is correct |
14 |
Correct |
1 ms |
468 KB |
Output is correct |
15 |
Correct |
1 ms |
468 KB |
Output is correct |
16 |
Correct |
1 ms |
468 KB |
Output is correct |
17 |
Correct |
1 ms |
468 KB |
Output is correct |
18 |
Correct |
2 ms |
468 KB |
Output is correct |
19 |
Correct |
66 ms |
23136 KB |
Output is correct |
20 |
Correct |
64 ms |
23164 KB |
Output is correct |
21 |
Correct |
93 ms |
23112 KB |
Output is correct |
22 |
Correct |
71 ms |
27276 KB |
Output is correct |
23 |
Correct |
93 ms |
27264 KB |
Output is correct |
24 |
Correct |
85 ms |
27388 KB |
Output is correct |
25 |
Correct |
33 ms |
19084 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
94 ms |
23820 KB |
Output is correct |
2 |
Correct |
114 ms |
23748 KB |
Output is correct |
3 |
Correct |
177 ms |
23708 KB |
Output is correct |
4 |
Correct |
163 ms |
28356 KB |
Output is correct |
5 |
Correct |
155 ms |
24128 KB |
Output is correct |
6 |
Correct |
168 ms |
28088 KB |
Output is correct |
7 |
Correct |
163 ms |
28100 KB |
Output is correct |
8 |
Correct |
139 ms |
28096 KB |
Output is correct |
9 |
Correct |
88 ms |
27204 KB |
Output is correct |
10 |
Correct |
131 ms |
27648 KB |
Output is correct |
11 |
Correct |
123 ms |
27388 KB |
Output is correct |
12 |
Correct |
130 ms |
27824 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
87 ms |
23736 KB |
Output is correct |
3 |
Correct |
139 ms |
23812 KB |
Output is correct |
4 |
Correct |
191 ms |
23748 KB |
Output is correct |
5 |
Correct |
124 ms |
23792 KB |
Output is correct |
6 |
Correct |
122 ms |
23820 KB |
Output is correct |
7 |
Correct |
136 ms |
24012 KB |
Output is correct |
8 |
Correct |
161 ms |
28412 KB |
Output is correct |
9 |
Correct |
133 ms |
28432 KB |
Output is correct |
10 |
Correct |
147 ms |
24092 KB |
Output is correct |
11 |
Correct |
150 ms |
28288 KB |
Output is correct |
12 |
Correct |
58 ms |
24260 KB |
Output is correct |
13 |
Correct |
0 ms |
212 KB |
Output is correct |
14 |
Correct |
1 ms |
212 KB |
Output is correct |
15 |
Correct |
1 ms |
468 KB |
Output is correct |
16 |
Correct |
1 ms |
468 KB |
Output is correct |
17 |
Correct |
1 ms |
468 KB |
Output is correct |
18 |
Correct |
1 ms |
468 KB |
Output is correct |
19 |
Correct |
1 ms |
468 KB |
Output is correct |
20 |
Correct |
1 ms |
468 KB |
Output is correct |
21 |
Correct |
1 ms |
468 KB |
Output is correct |
22 |
Correct |
0 ms |
212 KB |
Output is correct |
23 |
Correct |
1 ms |
212 KB |
Output is correct |
24 |
Correct |
1 ms |
468 KB |
Output is correct |
25 |
Correct |
1 ms |
468 KB |
Output is correct |
26 |
Correct |
1 ms |
496 KB |
Output is correct |
27 |
Correct |
1 ms |
468 KB |
Output is correct |
28 |
Correct |
1 ms |
468 KB |
Output is correct |
29 |
Correct |
1 ms |
468 KB |
Output is correct |
30 |
Correct |
1 ms |
468 KB |
Output is correct |
31 |
Correct |
39 ms |
2008 KB |
Output is correct |
32 |
Correct |
41 ms |
1956 KB |
Output is correct |
33 |
Correct |
34 ms |
2296 KB |
Output is correct |
34 |
Correct |
28 ms |
2260 KB |
Output is correct |
35 |
Correct |
27 ms |
2356 KB |
Output is correct |
36 |
Correct |
33 ms |
2388 KB |
Output is correct |
37 |
Correct |
28 ms |
1968 KB |
Output is correct |
38 |
Correct |
0 ms |
212 KB |
Output is correct |
39 |
Correct |
1 ms |
212 KB |
Output is correct |
40 |
Correct |
1 ms |
468 KB |
Output is correct |
41 |
Correct |
1 ms |
596 KB |
Output is correct |
42 |
Correct |
1 ms |
468 KB |
Output is correct |
43 |
Correct |
1 ms |
468 KB |
Output is correct |
44 |
Correct |
1 ms |
468 KB |
Output is correct |
45 |
Correct |
1 ms |
468 KB |
Output is correct |
46 |
Correct |
2 ms |
468 KB |
Output is correct |
47 |
Correct |
66 ms |
23136 KB |
Output is correct |
48 |
Correct |
64 ms |
23164 KB |
Output is correct |
49 |
Correct |
93 ms |
23112 KB |
Output is correct |
50 |
Correct |
71 ms |
27276 KB |
Output is correct |
51 |
Correct |
93 ms |
27264 KB |
Output is correct |
52 |
Correct |
85 ms |
27388 KB |
Output is correct |
53 |
Correct |
33 ms |
19084 KB |
Output is correct |
54 |
Correct |
94 ms |
23820 KB |
Output is correct |
55 |
Correct |
114 ms |
23748 KB |
Output is correct |
56 |
Correct |
177 ms |
23708 KB |
Output is correct |
57 |
Correct |
163 ms |
28356 KB |
Output is correct |
58 |
Correct |
155 ms |
24128 KB |
Output is correct |
59 |
Correct |
168 ms |
28088 KB |
Output is correct |
60 |
Correct |
163 ms |
28100 KB |
Output is correct |
61 |
Correct |
139 ms |
28096 KB |
Output is correct |
62 |
Correct |
88 ms |
27204 KB |
Output is correct |
63 |
Correct |
131 ms |
27648 KB |
Output is correct |
64 |
Correct |
123 ms |
27388 KB |
Output is correct |
65 |
Correct |
130 ms |
27824 KB |
Output is correct |
66 |
Correct |
1 ms |
212 KB |
Output is correct |
67 |
Correct |
88 ms |
23800 KB |
Output is correct |
68 |
Correct |
120 ms |
23792 KB |
Output is correct |
69 |
Correct |
178 ms |
23804 KB |
Output is correct |
70 |
Correct |
120 ms |
23820 KB |
Output is correct |
71 |
Correct |
132 ms |
23852 KB |
Output is correct |
72 |
Correct |
127 ms |
23804 KB |
Output is correct |
73 |
Correct |
140 ms |
28384 KB |
Output is correct |
74 |
Correct |
166 ms |
28416 KB |
Output is correct |
75 |
Correct |
140 ms |
24088 KB |
Output is correct |
76 |
Correct |
138 ms |
28368 KB |
Output is correct |
77 |
Correct |
66 ms |
24228 KB |
Output is correct |
78 |
Correct |
0 ms |
212 KB |
Output is correct |
79 |
Correct |
1 ms |
468 KB |
Output is correct |
80 |
Correct |
1 ms |
468 KB |
Output is correct |
81 |
Correct |
1 ms |
468 KB |
Output is correct |
82 |
Correct |
1 ms |
468 KB |
Output is correct |
83 |
Correct |
1 ms |
468 KB |
Output is correct |
84 |
Correct |
1 ms |
468 KB |
Output is correct |
85 |
Correct |
1 ms |
468 KB |
Output is correct |
86 |
Correct |
31 ms |
2004 KB |
Output is correct |
87 |
Correct |
33 ms |
1912 KB |
Output is correct |
88 |
Correct |
39 ms |
2276 KB |
Output is correct |
89 |
Correct |
27 ms |
2320 KB |
Output is correct |
90 |
Correct |
28 ms |
2428 KB |
Output is correct |
91 |
Correct |
27 ms |
2376 KB |
Output is correct |
92 |
Correct |
25 ms |
2004 KB |
Output is correct |
93 |
Correct |
80 ms |
23148 KB |
Output is correct |
94 |
Correct |
66 ms |
23164 KB |
Output is correct |
95 |
Correct |
75 ms |
23200 KB |
Output is correct |
96 |
Correct |
72 ms |
27268 KB |
Output is correct |
97 |
Correct |
89 ms |
27236 KB |
Output is correct |
98 |
Correct |
93 ms |
27264 KB |
Output is correct |
99 |
Correct |
43 ms |
19060 KB |
Output is correct |
100 |
Correct |
166 ms |
28000 KB |
Output is correct |
101 |
Correct |
163 ms |
28052 KB |
Output is correct |
102 |
Correct |
149 ms |
28108 KB |
Output is correct |
103 |
Correct |
128 ms |
27784 KB |
Output is correct |
104 |
Correct |
143 ms |
27524 KB |
Output is correct |
105 |
Correct |
133 ms |
27660 KB |
Output is correct |
106 |
Correct |
145 ms |
23932 KB |
Output is correct |
107 |
Correct |
186 ms |
24036 KB |
Output is correct |
108 |
Correct |
200 ms |
23944 KB |
Output is correct |
109 |
Correct |
146 ms |
23940 KB |
Output is correct |
110 |
Correct |
178 ms |
27912 KB |
Output is correct |
111 |
Correct |
152 ms |
27960 KB |
Output is correct |