#include <bits/stdc++.h>
using namespace std;
template <typename A, typename B>
string to_string( pair<A, B> p ) {
return "(" + to_string(p.first) + ", " + to_string(p.second) + ")";
}
template <typename T>
string to_string (T x) {
string S = "[";
bool first = true;
for (const auto v : x) {
if (!first) S += ", ";
S += to_string(v);
first = false;
}
S += "]";
return S;
}
string to_string( string s) {
return s;
}
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 TDEBUG
# define dbg(...) { cout << "(" << #__VA_ARGS__ << "): "; dbg_out(__VA_ARGS__); }
#else
# define dbg(...)
#endif
using Event = pair<int, int>;
using VEvent = vector<Event>;
using Time = pair<pair<int, int>, pair<int, bool>>;
using VTime = vector<Time>;
using idata = vector<int>;
using igrid = vector<idata>;
struct CNT {
set<pair<pair<int, int>, int>> m;
void insert (int index, int left, int right) {
while (m.size() != 0) {
auto it = m.end(); it --;
const auto data = *it;
if (left <= data.first.second && data.first.first <= right)
m.erase(data);
else break ;
}
if (m.size() != 0) {
auto it = m.end(); it --;
const auto data = *it;
if (right <= data.first.first && data.first.second <= left) return ;
}
m.insert({ {right, left}, index });
dbg(index, left, right, m);
}
int query (int start) {
dbg(start, m);
auto it =m.lower_bound({{ start, -1 }, -1});
if (it == m.end()) return -1;
return (*it).second;
}
};
const bool START = true;
const bool END = false;
VEvent events;
VTime timings;
idata parent;
igrid parent_2k;
const int MAXK = 20;
void initBLF () {
parent_2k.resize(parent.size(), idata(MAXK, -1));
for (int i = 0; i < parent.size(); i ++)
parent_2k[i][0] = parent[i];
for (int k = 0; k + 1 < MAXK; k ++) {
for (int i = 0; i < parent.size(); i ++) {
if (parent_2k[i][k] == -1) continue ;
parent_2k[i][k + 1] = parent_2k[parent_2k[i][k]][k];
}
}
}
pair<int, int> jump (int node, int finalEnd) {
int count = 0;
for (int k = MAXK - 1; k >= 0; k --) {
int pot = parent_2k[node][k];
if (pot == -1 || events[pot].first <= finalEnd) continue ;
node = pot;
count += 1 << k;
}
if (events[node].first <= finalEnd) return { node, 0 };
return { parent[node], count + 1 };
}
map<int, int> compress (idata coordinates) {
map<int, int> ans;
set<int> h;
for (int u : coordinates) h.insert(u);
int i = 0;
for (int u : h) ans.insert({ u, i ++ });
return ans;
}
int main () {
int N, Q;
cin >> N >> Q;
events.resize(N);
idata coords;
for (int i = 0; i < N; i ++) {
int a, b;
cin >> a >> b;
events[i] = { a, b };
coords.push_back(a);
coords.push_back(b);
}
map<int, int> mC = compress(coords);
for (int i = 0; i < N; i ++) {
//events[i].first = (*mC.find(events[i].first )).second;
//events[i].second = (*mC.find(events[i].second)).second;
timings.push_back({ { events[i].first, events[i].first }, { i, START } });
timings.push_back({ { events[i].second, events[i].first }, { i, END } });
}
sort(timings.begin(), timings.end());
CNT cnt;
parent.resize(N);
for (const auto &timing : timings) {
int id = timing.second.first;
bool F = timing.second.second;
if (F == START) {
} else {
parent[id] = cnt.query( events[id].first );
if (parent[id] != -1 && events[parent[id]].first >= events[id].first)
parent[id] = -1;
cnt.insert(id, events[id].first, events[id].second);
}
}
initBLF();
for (int q = 0; q < Q; q ++) {
int a, b;
cin >> a >> b;
a --;
b --;
if (a == b) {
cout << "0\n";
continue ;
}
if (events[a].second > events[b].second) {
cout << "impossible\n";
continue ;
}
pair<int, int> F = jump( b, events[a].second );
if (F.first == -1) cout << "impossible\n";
else cout << F.second + 1 << "\n";
}
}
Compilation message
events.cpp: In function 'void initBLF()':
events.cpp:92:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
92 | for (int i = 0; i < parent.size(); i ++)
| ~~^~~~~~~~~~~~~~~
events.cpp:96:27: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
96 | for (int i = 0; i < parent.size(); i ++) {
| ~~^~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
296 KB |
Output is correct |
2 |
Correct |
327 ms |
28924 KB |
Output is correct |
3 |
Correct |
345 ms |
28940 KB |
Output is correct |
4 |
Correct |
376 ms |
28916 KB |
Output is correct |
5 |
Correct |
351 ms |
28836 KB |
Output is correct |
6 |
Correct |
349 ms |
28720 KB |
Output is correct |
7 |
Correct |
344 ms |
28672 KB |
Output is correct |
8 |
Correct |
344 ms |
30640 KB |
Output is correct |
9 |
Correct |
342 ms |
36992 KB |
Output is correct |
10 |
Correct |
346 ms |
32068 KB |
Output is correct |
11 |
Correct |
346 ms |
31480 KB |
Output is correct |
12 |
Correct |
266 ms |
28392 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
300 KB |
Output is correct |
3 |
Correct |
2 ms |
596 KB |
Output is correct |
4 |
Correct |
2 ms |
596 KB |
Output is correct |
5 |
Correct |
2 ms |
468 KB |
Output is correct |
6 |
Correct |
2 ms |
468 KB |
Output is correct |
7 |
Correct |
2 ms |
564 KB |
Output is correct |
8 |
Correct |
2 ms |
596 KB |
Output is correct |
9 |
Correct |
1 ms |
468 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
300 KB |
Output is correct |
3 |
Correct |
2 ms |
596 KB |
Output is correct |
4 |
Correct |
2 ms |
596 KB |
Output is correct |
5 |
Correct |
2 ms |
468 KB |
Output is correct |
6 |
Correct |
2 ms |
468 KB |
Output is correct |
7 |
Correct |
2 ms |
564 KB |
Output is correct |
8 |
Correct |
2 ms |
596 KB |
Output is correct |
9 |
Correct |
1 ms |
468 KB |
Output is correct |
10 |
Correct |
1 ms |
212 KB |
Output is correct |
11 |
Correct |
1 ms |
212 KB |
Output is correct |
12 |
Correct |
2 ms |
596 KB |
Output is correct |
13 |
Correct |
2 ms |
548 KB |
Output is correct |
14 |
Correct |
2 ms |
468 KB |
Output is correct |
15 |
Correct |
2 ms |
468 KB |
Output is correct |
16 |
Correct |
2 ms |
468 KB |
Output is correct |
17 |
Correct |
2 ms |
596 KB |
Output is correct |
18 |
Correct |
1 ms |
440 KB |
Output is correct |
19 |
Correct |
168 ms |
2956 KB |
Output is correct |
20 |
Correct |
167 ms |
3128 KB |
Output is correct |
21 |
Correct |
164 ms |
3108 KB |
Output is correct |
22 |
Correct |
148 ms |
3228 KB |
Output is correct |
23 |
Correct |
149 ms |
3256 KB |
Output is correct |
24 |
Correct |
148 ms |
3532 KB |
Output is correct |
25 |
Correct |
150 ms |
3152 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
300 KB |
Output is correct |
3 |
Correct |
2 ms |
596 KB |
Output is correct |
4 |
Correct |
2 ms |
596 KB |
Output is correct |
5 |
Correct |
2 ms |
468 KB |
Output is correct |
6 |
Correct |
2 ms |
468 KB |
Output is correct |
7 |
Correct |
2 ms |
564 KB |
Output is correct |
8 |
Correct |
2 ms |
596 KB |
Output is correct |
9 |
Correct |
1 ms |
468 KB |
Output is correct |
10 |
Correct |
1 ms |
212 KB |
Output is correct |
11 |
Correct |
1 ms |
212 KB |
Output is correct |
12 |
Correct |
2 ms |
560 KB |
Output is correct |
13 |
Correct |
2 ms |
600 KB |
Output is correct |
14 |
Correct |
2 ms |
468 KB |
Output is correct |
15 |
Correct |
2 ms |
468 KB |
Output is correct |
16 |
Correct |
2 ms |
568 KB |
Output is correct |
17 |
Correct |
2 ms |
596 KB |
Output is correct |
18 |
Correct |
1 ms |
468 KB |
Output is correct |
19 |
Correct |
163 ms |
29944 KB |
Output is correct |
20 |
Correct |
153 ms |
22984 KB |
Output is correct |
21 |
Correct |
150 ms |
23756 KB |
Output is correct |
22 |
Correct |
173 ms |
29396 KB |
Output is correct |
23 |
Correct |
208 ms |
34640 KB |
Output is correct |
24 |
Correct |
208 ms |
34608 KB |
Output is correct |
25 |
Correct |
72 ms |
18352 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
314 ms |
28916 KB |
Output is correct |
2 |
Correct |
328 ms |
28888 KB |
Output is correct |
3 |
Correct |
348 ms |
28848 KB |
Output is correct |
4 |
Correct |
337 ms |
34132 KB |
Output is correct |
5 |
Correct |
345 ms |
29276 KB |
Output is correct |
6 |
Correct |
417 ms |
33712 KB |
Output is correct |
7 |
Correct |
386 ms |
33788 KB |
Output is correct |
8 |
Correct |
359 ms |
33916 KB |
Output is correct |
9 |
Correct |
220 ms |
33016 KB |
Output is correct |
10 |
Correct |
378 ms |
33392 KB |
Output is correct |
11 |
Correct |
356 ms |
33132 KB |
Output is correct |
12 |
Correct |
386 ms |
33600 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
296 KB |
Output is correct |
2 |
Correct |
327 ms |
28924 KB |
Output is correct |
3 |
Correct |
345 ms |
28940 KB |
Output is correct |
4 |
Correct |
376 ms |
28916 KB |
Output is correct |
5 |
Correct |
351 ms |
28836 KB |
Output is correct |
6 |
Correct |
349 ms |
28720 KB |
Output is correct |
7 |
Correct |
344 ms |
28672 KB |
Output is correct |
8 |
Correct |
344 ms |
30640 KB |
Output is correct |
9 |
Correct |
342 ms |
36992 KB |
Output is correct |
10 |
Correct |
346 ms |
32068 KB |
Output is correct |
11 |
Correct |
346 ms |
31480 KB |
Output is correct |
12 |
Correct |
266 ms |
28392 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
300 KB |
Output is correct |
15 |
Correct |
2 ms |
596 KB |
Output is correct |
16 |
Correct |
2 ms |
596 KB |
Output is correct |
17 |
Correct |
2 ms |
468 KB |
Output is correct |
18 |
Correct |
2 ms |
468 KB |
Output is correct |
19 |
Correct |
2 ms |
564 KB |
Output is correct |
20 |
Correct |
2 ms |
596 KB |
Output is correct |
21 |
Correct |
1 ms |
468 KB |
Output is correct |
22 |
Correct |
1 ms |
212 KB |
Output is correct |
23 |
Correct |
1 ms |
212 KB |
Output is correct |
24 |
Correct |
2 ms |
596 KB |
Output is correct |
25 |
Correct |
2 ms |
548 KB |
Output is correct |
26 |
Correct |
2 ms |
468 KB |
Output is correct |
27 |
Correct |
2 ms |
468 KB |
Output is correct |
28 |
Correct |
2 ms |
468 KB |
Output is correct |
29 |
Correct |
2 ms |
596 KB |
Output is correct |
30 |
Correct |
1 ms |
440 KB |
Output is correct |
31 |
Correct |
168 ms |
2956 KB |
Output is correct |
32 |
Correct |
167 ms |
3128 KB |
Output is correct |
33 |
Correct |
164 ms |
3108 KB |
Output is correct |
34 |
Correct |
148 ms |
3228 KB |
Output is correct |
35 |
Correct |
149 ms |
3256 KB |
Output is correct |
36 |
Correct |
148 ms |
3532 KB |
Output is correct |
37 |
Correct |
150 ms |
3152 KB |
Output is correct |
38 |
Correct |
1 ms |
212 KB |
Output is correct |
39 |
Correct |
1 ms |
212 KB |
Output is correct |
40 |
Correct |
2 ms |
560 KB |
Output is correct |
41 |
Correct |
2 ms |
600 KB |
Output is correct |
42 |
Correct |
2 ms |
468 KB |
Output is correct |
43 |
Correct |
2 ms |
468 KB |
Output is correct |
44 |
Correct |
2 ms |
568 KB |
Output is correct |
45 |
Correct |
2 ms |
596 KB |
Output is correct |
46 |
Correct |
1 ms |
468 KB |
Output is correct |
47 |
Correct |
163 ms |
29944 KB |
Output is correct |
48 |
Correct |
153 ms |
22984 KB |
Output is correct |
49 |
Correct |
150 ms |
23756 KB |
Output is correct |
50 |
Correct |
173 ms |
29396 KB |
Output is correct |
51 |
Correct |
208 ms |
34640 KB |
Output is correct |
52 |
Correct |
208 ms |
34608 KB |
Output is correct |
53 |
Correct |
72 ms |
18352 KB |
Output is correct |
54 |
Correct |
314 ms |
28916 KB |
Output is correct |
55 |
Correct |
328 ms |
28888 KB |
Output is correct |
56 |
Correct |
348 ms |
28848 KB |
Output is correct |
57 |
Correct |
337 ms |
34132 KB |
Output is correct |
58 |
Correct |
345 ms |
29276 KB |
Output is correct |
59 |
Correct |
417 ms |
33712 KB |
Output is correct |
60 |
Correct |
386 ms |
33788 KB |
Output is correct |
61 |
Correct |
359 ms |
33916 KB |
Output is correct |
62 |
Correct |
220 ms |
33016 KB |
Output is correct |
63 |
Correct |
378 ms |
33392 KB |
Output is correct |
64 |
Correct |
356 ms |
33132 KB |
Output is correct |
65 |
Correct |
386 ms |
33600 KB |
Output is correct |
66 |
Correct |
1 ms |
212 KB |
Output is correct |
67 |
Correct |
321 ms |
31736 KB |
Output is correct |
68 |
Correct |
363 ms |
31844 KB |
Output is correct |
69 |
Correct |
362 ms |
31736 KB |
Output is correct |
70 |
Correct |
356 ms |
31628 KB |
Output is correct |
71 |
Correct |
384 ms |
31568 KB |
Output is correct |
72 |
Correct |
387 ms |
31508 KB |
Output is correct |
73 |
Correct |
337 ms |
30648 KB |
Output is correct |
74 |
Correct |
347 ms |
37032 KB |
Output is correct |
75 |
Correct |
362 ms |
32120 KB |
Output is correct |
76 |
Correct |
353 ms |
31512 KB |
Output is correct |
77 |
Correct |
264 ms |
28400 KB |
Output is correct |
78 |
Correct |
0 ms |
212 KB |
Output is correct |
79 |
Correct |
2 ms |
596 KB |
Output is correct |
80 |
Correct |
2 ms |
596 KB |
Output is correct |
81 |
Correct |
2 ms |
468 KB |
Output is correct |
82 |
Correct |
2 ms |
468 KB |
Output is correct |
83 |
Correct |
2 ms |
564 KB |
Output is correct |
84 |
Correct |
3 ms |
568 KB |
Output is correct |
85 |
Correct |
1 ms |
468 KB |
Output is correct |
86 |
Correct |
154 ms |
2936 KB |
Output is correct |
87 |
Correct |
154 ms |
3100 KB |
Output is correct |
88 |
Correct |
154 ms |
3048 KB |
Output is correct |
89 |
Correct |
148 ms |
3148 KB |
Output is correct |
90 |
Correct |
148 ms |
3240 KB |
Output is correct |
91 |
Correct |
148 ms |
3496 KB |
Output is correct |
92 |
Correct |
158 ms |
3224 KB |
Output is correct |
93 |
Correct |
160 ms |
30048 KB |
Output is correct |
94 |
Correct |
151 ms |
22968 KB |
Output is correct |
95 |
Correct |
144 ms |
23724 KB |
Output is correct |
96 |
Correct |
175 ms |
29444 KB |
Output is correct |
97 |
Correct |
191 ms |
34608 KB |
Output is correct |
98 |
Correct |
227 ms |
34708 KB |
Output is correct |
99 |
Correct |
74 ms |
18332 KB |
Output is correct |
100 |
Correct |
374 ms |
36620 KB |
Output is correct |
101 |
Correct |
385 ms |
36636 KB |
Output is correct |
102 |
Correct |
357 ms |
36676 KB |
Output is correct |
103 |
Correct |
370 ms |
36272 KB |
Output is correct |
104 |
Correct |
351 ms |
36016 KB |
Output is correct |
105 |
Correct |
389 ms |
36288 KB |
Output is correct |
106 |
Correct |
302 ms |
23752 KB |
Output is correct |
107 |
Correct |
343 ms |
24852 KB |
Output is correct |
108 |
Correct |
290 ms |
25596 KB |
Output is correct |
109 |
Correct |
301 ms |
25608 KB |
Output is correct |
110 |
Correct |
341 ms |
30172 KB |
Output is correct |
111 |
Correct |
395 ms |
30212 KB |
Output is correct |