// BEGIN BOILERPLATE CODE
#include <bits/stdc++.h>
using namespace std;
#ifdef KAMIRULEZ
const bool kami_loc = true;
const long long kami_seed = 69420;
#else
const bool kami_loc = false;
const long long kami_seed = chrono::steady_clock::now().time_since_epoch().count();
#endif
const string kami_fi = "kamirulez.inp";
const string kami_fo = "kamirulez.out";
mt19937_64 kami_gen(kami_seed);
long long rand_range(long long rmin, long long rmax) {
uniform_int_distribution<long long> rdist(rmin, rmax);
return rdist(kami_gen);
}
long double rand_real(long double rmin, long double rmax) {
uniform_real_distribution<long double> rdist(rmin, rmax);
return rdist(kami_gen);
}
void file_io(string fi, string fo) {
if (fi != "" && (!kami_loc || fi == kami_fi)) {
freopen(fi.c_str(), "r", stdin);
}
if (fo != "" && (!kami_loc || fo == kami_fo)) {
freopen(fo.c_str(), "w", stdout);
}
}
void set_up() {
if (kami_loc) {
file_io(kami_fi, kami_fo);
}
ios_base::sync_with_stdio(0);
cin.tie(0);
}
void just_do_it();
void just_exec_it() {
if (kami_loc) {
auto pstart = chrono::steady_clock::now();
just_do_it();
auto pend = chrono::steady_clock::now();
long long ptime = chrono::duration_cast<chrono::milliseconds>(pend - pstart).count();
string bar(50, '=');
cout << '\n' << bar << '\n';
cout << "Time: " << ptime << " ms" << '\n';
}
else {
just_do_it();
}
}
int main() {
set_up();
just_exec_it();
return 0;
}
// END BOILERPLATE CODE
// BEGIN MAIN CODE
const int ms = 1e5 + 20;
const int inf = 1e9 + 20;
const int st = 20;
int lg[ms * 2];
int lt[ms];
int rt[ms];
pair<int, int> p[ms * 2];
pair<int, int> mi[ms * 2][st];
int nxt[ms][st];
int get(int lt, int rt) {
int k = lg[rt - lt + 1];
return min(mi[lt][k], mi[rt - (1 << k) + 1][k]).second;
}
void query() {
int u, v;
cin >> u >> v;
if (u == v) {
cout << 0 << '\n';
return;
}
if (lt[v] <= rt[u] && rt[u] <= rt[v]) {
cout << 1 << '\n';
return;
}
if (rt[u] > rt[v]) {
cout << "impossible" << '\n';
return;
}
int res = 0;
for (int k = st - 1; k >= 0; k--) {
if (lt[nxt[v][k]] > rt[u]) {
v = nxt[v][k];
res += (1 << k);
}
}
res += 2;
v = nxt[v][0];
if (lt[v] <= rt[u] && rt[u] <= rt[v]) {
cout << res << '\n';
}
else {
cout << "impossible" << '\n';
}
}
void just_do_it() {
for (int i = 2; i < ms * 2; i++) {
lg[i] = lg[i / 2] + 1;
}
int n, q;
cin >> n >> q;
for (int i = 1; i <= n; i++) {
cin >> lt[i] >> rt[i];
}
for (int i = 1; i <= n; i++) {
p[i * 2 - 1] = {lt[i], i};
p[i * 2] = {rt[i], -i};
}
sort(p + 1, p + n * 2 + 1);
int cnt = 0;
for (int i = 1; i <= n * 2; i++) {
if (p[i].first != p[i - 1].first) {
cnt++;
}
if (p[i].second > 0) {
lt[p[i].second] = cnt;
}
else {
rt[-p[i].second] = cnt;
}
}
for (int i = 1; i <= cnt; i++) {
mi[i][0] = {inf, -1};
}
for (int i = 1; i <= n; i++) {
mi[rt[i]][0] = min(mi[rt[i]][0], {lt[i], i});
}
for (int k = 1; k < st; k++) {
for (int i = 1; i + (1 << k) - 1 <= cnt; i++) {
mi[i][k] = min(mi[i][k - 1], mi[i + (1 << (k - 1))][k - 1]);
}
}
for (int i = 1; i <= n; i++) {
nxt[i][0] = get(lt[i], rt[i]);
}
for (int k = 1; k < st; k++) {
for (int i = 1; i <= n; i++) {
nxt[i][k] = nxt[nxt[i][k - 1]][k - 1];
}
}
for (int i = 0; i < q; i++) {
query();
}
}
// END MAIN CODE
Compilation message
events.cpp: In function 'void file_io(std::string, std::string)':
events.cpp:30:10: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
30 | freopen(fi.c_str(), "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
events.cpp:33:10: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
33 | freopen(fo.c_str(), "w", stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
1108 KB |
Output is correct |
2 |
Correct |
115 ms |
28488 KB |
Output is correct |
3 |
Correct |
114 ms |
30732 KB |
Output is correct |
4 |
Correct |
132 ms |
30608 KB |
Output is correct |
5 |
Correct |
118 ms |
32196 KB |
Output is correct |
6 |
Correct |
111 ms |
33716 KB |
Output is correct |
7 |
Correct |
123 ms |
34148 KB |
Output is correct |
8 |
Correct |
119 ms |
46732 KB |
Output is correct |
9 |
Correct |
138 ms |
46688 KB |
Output is correct |
10 |
Correct |
135 ms |
30952 KB |
Output is correct |
11 |
Correct |
116 ms |
37196 KB |
Output is correct |
12 |
Correct |
76 ms |
30332 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
1108 KB |
Output is correct |
2 |
Correct |
1 ms |
1108 KB |
Output is correct |
3 |
Correct |
1 ms |
1364 KB |
Output is correct |
4 |
Correct |
1 ms |
1364 KB |
Output is correct |
5 |
Correct |
1 ms |
1364 KB |
Output is correct |
6 |
Correct |
1 ms |
1364 KB |
Output is correct |
7 |
Correct |
1 ms |
1492 KB |
Output is correct |
8 |
Correct |
2 ms |
1492 KB |
Output is correct |
9 |
Correct |
1 ms |
1236 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
1108 KB |
Output is correct |
2 |
Correct |
1 ms |
1108 KB |
Output is correct |
3 |
Correct |
1 ms |
1364 KB |
Output is correct |
4 |
Correct |
1 ms |
1364 KB |
Output is correct |
5 |
Correct |
1 ms |
1364 KB |
Output is correct |
6 |
Correct |
1 ms |
1364 KB |
Output is correct |
7 |
Correct |
1 ms |
1492 KB |
Output is correct |
8 |
Correct |
2 ms |
1492 KB |
Output is correct |
9 |
Correct |
1 ms |
1236 KB |
Output is correct |
10 |
Correct |
1 ms |
1108 KB |
Output is correct |
11 |
Correct |
1 ms |
1108 KB |
Output is correct |
12 |
Correct |
1 ms |
1364 KB |
Output is correct |
13 |
Correct |
1 ms |
1364 KB |
Output is correct |
14 |
Correct |
1 ms |
1364 KB |
Output is correct |
15 |
Correct |
1 ms |
1364 KB |
Output is correct |
16 |
Correct |
1 ms |
1492 KB |
Output is correct |
17 |
Correct |
1 ms |
1492 KB |
Output is correct |
18 |
Correct |
1 ms |
1236 KB |
Output is correct |
19 |
Correct |
31 ms |
3916 KB |
Output is correct |
20 |
Correct |
35 ms |
3940 KB |
Output is correct |
21 |
Correct |
31 ms |
4176 KB |
Output is correct |
22 |
Correct |
28 ms |
4176 KB |
Output is correct |
23 |
Correct |
25 ms |
4824 KB |
Output is correct |
24 |
Correct |
23 ms |
4820 KB |
Output is correct |
25 |
Correct |
26 ms |
4536 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
1108 KB |
Output is correct |
2 |
Correct |
1 ms |
1108 KB |
Output is correct |
3 |
Correct |
1 ms |
1364 KB |
Output is correct |
4 |
Correct |
1 ms |
1364 KB |
Output is correct |
5 |
Correct |
1 ms |
1364 KB |
Output is correct |
6 |
Correct |
1 ms |
1364 KB |
Output is correct |
7 |
Correct |
1 ms |
1492 KB |
Output is correct |
8 |
Correct |
2 ms |
1492 KB |
Output is correct |
9 |
Correct |
1 ms |
1236 KB |
Output is correct |
10 |
Correct |
1 ms |
1108 KB |
Output is correct |
11 |
Correct |
1 ms |
1108 KB |
Output is correct |
12 |
Correct |
1 ms |
1364 KB |
Output is correct |
13 |
Correct |
1 ms |
1364 KB |
Output is correct |
14 |
Correct |
1 ms |
1364 KB |
Output is correct |
15 |
Correct |
1 ms |
1364 KB |
Output is correct |
16 |
Correct |
2 ms |
1492 KB |
Output is correct |
17 |
Correct |
1 ms |
1492 KB |
Output is correct |
18 |
Correct |
1 ms |
1236 KB |
Output is correct |
19 |
Correct |
73 ms |
26848 KB |
Output is correct |
20 |
Correct |
79 ms |
28088 KB |
Output is correct |
21 |
Correct |
86 ms |
28728 KB |
Output is correct |
22 |
Correct |
78 ms |
34600 KB |
Output is correct |
23 |
Correct |
101 ms |
44512 KB |
Output is correct |
24 |
Correct |
98 ms |
44492 KB |
Output is correct |
25 |
Correct |
39 ms |
12488 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
96 ms |
27516 KB |
Output is correct |
2 |
Correct |
102 ms |
27480 KB |
Output is correct |
3 |
Correct |
124 ms |
27496 KB |
Output is correct |
4 |
Correct |
116 ms |
43660 KB |
Output is correct |
5 |
Correct |
107 ms |
27848 KB |
Output is correct |
6 |
Correct |
140 ms |
43340 KB |
Output is correct |
7 |
Correct |
137 ms |
43284 KB |
Output is correct |
8 |
Correct |
124 ms |
43448 KB |
Output is correct |
9 |
Correct |
101 ms |
42504 KB |
Output is correct |
10 |
Correct |
130 ms |
42868 KB |
Output is correct |
11 |
Correct |
116 ms |
42708 KB |
Output is correct |
12 |
Correct |
127 ms |
42944 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
1108 KB |
Output is correct |
2 |
Correct |
115 ms |
28488 KB |
Output is correct |
3 |
Correct |
114 ms |
30732 KB |
Output is correct |
4 |
Correct |
132 ms |
30608 KB |
Output is correct |
5 |
Correct |
118 ms |
32196 KB |
Output is correct |
6 |
Correct |
111 ms |
33716 KB |
Output is correct |
7 |
Correct |
123 ms |
34148 KB |
Output is correct |
8 |
Correct |
119 ms |
46732 KB |
Output is correct |
9 |
Correct |
138 ms |
46688 KB |
Output is correct |
10 |
Correct |
135 ms |
30952 KB |
Output is correct |
11 |
Correct |
116 ms |
37196 KB |
Output is correct |
12 |
Correct |
76 ms |
30332 KB |
Output is correct |
13 |
Correct |
1 ms |
1108 KB |
Output is correct |
14 |
Correct |
1 ms |
1108 KB |
Output is correct |
15 |
Correct |
1 ms |
1364 KB |
Output is correct |
16 |
Correct |
1 ms |
1364 KB |
Output is correct |
17 |
Correct |
1 ms |
1364 KB |
Output is correct |
18 |
Correct |
1 ms |
1364 KB |
Output is correct |
19 |
Correct |
1 ms |
1492 KB |
Output is correct |
20 |
Correct |
2 ms |
1492 KB |
Output is correct |
21 |
Correct |
1 ms |
1236 KB |
Output is correct |
22 |
Correct |
1 ms |
1108 KB |
Output is correct |
23 |
Correct |
1 ms |
1108 KB |
Output is correct |
24 |
Correct |
1 ms |
1364 KB |
Output is correct |
25 |
Correct |
1 ms |
1364 KB |
Output is correct |
26 |
Correct |
1 ms |
1364 KB |
Output is correct |
27 |
Correct |
1 ms |
1364 KB |
Output is correct |
28 |
Correct |
1 ms |
1492 KB |
Output is correct |
29 |
Correct |
1 ms |
1492 KB |
Output is correct |
30 |
Correct |
1 ms |
1236 KB |
Output is correct |
31 |
Correct |
31 ms |
3916 KB |
Output is correct |
32 |
Correct |
35 ms |
3940 KB |
Output is correct |
33 |
Correct |
31 ms |
4176 KB |
Output is correct |
34 |
Correct |
28 ms |
4176 KB |
Output is correct |
35 |
Correct |
25 ms |
4824 KB |
Output is correct |
36 |
Correct |
23 ms |
4820 KB |
Output is correct |
37 |
Correct |
26 ms |
4536 KB |
Output is correct |
38 |
Correct |
1 ms |
1108 KB |
Output is correct |
39 |
Correct |
1 ms |
1108 KB |
Output is correct |
40 |
Correct |
1 ms |
1364 KB |
Output is correct |
41 |
Correct |
1 ms |
1364 KB |
Output is correct |
42 |
Correct |
1 ms |
1364 KB |
Output is correct |
43 |
Correct |
1 ms |
1364 KB |
Output is correct |
44 |
Correct |
2 ms |
1492 KB |
Output is correct |
45 |
Correct |
1 ms |
1492 KB |
Output is correct |
46 |
Correct |
1 ms |
1236 KB |
Output is correct |
47 |
Correct |
73 ms |
26848 KB |
Output is correct |
48 |
Correct |
79 ms |
28088 KB |
Output is correct |
49 |
Correct |
86 ms |
28728 KB |
Output is correct |
50 |
Correct |
78 ms |
34600 KB |
Output is correct |
51 |
Correct |
101 ms |
44512 KB |
Output is correct |
52 |
Correct |
98 ms |
44492 KB |
Output is correct |
53 |
Correct |
39 ms |
12488 KB |
Output is correct |
54 |
Correct |
96 ms |
27516 KB |
Output is correct |
55 |
Correct |
102 ms |
27480 KB |
Output is correct |
56 |
Correct |
124 ms |
27496 KB |
Output is correct |
57 |
Correct |
116 ms |
43660 KB |
Output is correct |
58 |
Correct |
107 ms |
27848 KB |
Output is correct |
59 |
Correct |
140 ms |
43340 KB |
Output is correct |
60 |
Correct |
137 ms |
43284 KB |
Output is correct |
61 |
Correct |
124 ms |
43448 KB |
Output is correct |
62 |
Correct |
101 ms |
42504 KB |
Output is correct |
63 |
Correct |
130 ms |
42868 KB |
Output is correct |
64 |
Correct |
116 ms |
42708 KB |
Output is correct |
65 |
Correct |
127 ms |
42944 KB |
Output is correct |
66 |
Correct |
1 ms |
1108 KB |
Output is correct |
67 |
Correct |
102 ms |
30648 KB |
Output is correct |
68 |
Correct |
109 ms |
30664 KB |
Output is correct |
69 |
Correct |
135 ms |
30636 KB |
Output is correct |
70 |
Correct |
117 ms |
32224 KB |
Output is correct |
71 |
Correct |
131 ms |
33700 KB |
Output is correct |
72 |
Correct |
116 ms |
34220 KB |
Output is correct |
73 |
Correct |
132 ms |
46664 KB |
Output is correct |
74 |
Correct |
135 ms |
46668 KB |
Output is correct |
75 |
Correct |
122 ms |
31180 KB |
Output is correct |
76 |
Correct |
119 ms |
37208 KB |
Output is correct |
77 |
Correct |
80 ms |
30324 KB |
Output is correct |
78 |
Correct |
1 ms |
1108 KB |
Output is correct |
79 |
Correct |
1 ms |
1364 KB |
Output is correct |
80 |
Correct |
1 ms |
1364 KB |
Output is correct |
81 |
Correct |
1 ms |
1364 KB |
Output is correct |
82 |
Correct |
1 ms |
1364 KB |
Output is correct |
83 |
Correct |
1 ms |
1492 KB |
Output is correct |
84 |
Correct |
2 ms |
1492 KB |
Output is correct |
85 |
Correct |
1 ms |
1236 KB |
Output is correct |
86 |
Correct |
32 ms |
4084 KB |
Output is correct |
87 |
Correct |
29 ms |
3916 KB |
Output is correct |
88 |
Correct |
34 ms |
4076 KB |
Output is correct |
89 |
Correct |
26 ms |
4184 KB |
Output is correct |
90 |
Correct |
25 ms |
4812 KB |
Output is correct |
91 |
Correct |
27 ms |
4868 KB |
Output is correct |
92 |
Correct |
29 ms |
4512 KB |
Output is correct |
93 |
Correct |
74 ms |
28888 KB |
Output is correct |
94 |
Correct |
80 ms |
28028 KB |
Output is correct |
95 |
Correct |
75 ms |
28752 KB |
Output is correct |
96 |
Correct |
83 ms |
34708 KB |
Output is correct |
97 |
Correct |
102 ms |
44416 KB |
Output is correct |
98 |
Correct |
102 ms |
44548 KB |
Output is correct |
99 |
Correct |
39 ms |
12484 KB |
Output is correct |
100 |
Correct |
150 ms |
46396 KB |
Output is correct |
101 |
Correct |
157 ms |
46292 KB |
Output is correct |
102 |
Correct |
150 ms |
46532 KB |
Output is correct |
103 |
Correct |
151 ms |
46068 KB |
Output is correct |
104 |
Correct |
125 ms |
45788 KB |
Output is correct |
105 |
Correct |
144 ms |
46012 KB |
Output is correct |
106 |
Correct |
105 ms |
26060 KB |
Output is correct |
107 |
Correct |
142 ms |
29984 KB |
Output is correct |
108 |
Correct |
102 ms |
30668 KB |
Output is correct |
109 |
Correct |
115 ms |
30740 KB |
Output is correct |
110 |
Correct |
119 ms |
46312 KB |
Output is correct |
111 |
Correct |
137 ms |
46284 KB |
Output is correct |