#include <iostream>
#include <algorithm>
#include <string>
#include <vector>
#include <cmath>
#include <chrono>
#include <ctime>
#include <set>
#include <map>
#include <stack>
#include <queue>
#include <deque>
#include <limits>
#include <iomanip>
#include <unordered_set>
#include <unordered_map>
#include <fstream>
#include <functional>
#include <random>
#include <cassert>
using namespace std;
typedef long long ll;
typedef long double ld;
#define ff first
#define ss second
struct Point {
ll l, r;
ll ind;
bool st;
Point(ll _l, ll _r, bool _st, ll i) :l(_l), r(_r), st(_st), ind(i) {}
bool operator<(const Point& other) const {
ll m1 = (st ? l : r);
ll m2 = (other.st ? other.l : other.r);
if (m1 < m2)return true;
if (m1 > m2)return false;
if (st && !other.st)return true;
if (!st && other.st)return false;
return false;
}
};
struct Vertex {
pair<ll, ll> vl;
Vertex* l, * r;
Vertex(pair<ll, ll> value) :vl(value),l(nullptr),r(nullptr) {}
};
ll ttt;
const ll INF = 1e18;
const ll MOD = 1e9 + 7;
const ll N = 100007;
const ll LG = 23;
ll n, m, k;
vector<Point>a;
ll ls[N], rs[N];
ll up[N][LG];
Vertex* root = new Vertex({ MOD,MOD });
unordered_map<ll, multiset<pair<ll,ll>>>d;
pair<ll,ll> get_value(Vertex* v) {
if (!v)return { MOD,MOD };
return v->vl;
}
void update(Vertex* v, ll tl, ll tr, ll ind, pair<ll, ll> vl) {
//cout << tl << " " << tr << endl;
//cout << (v->vl.ff) << endl;
if (tl == tr) {
v->vl = vl;
}
else {
ll tm = (tl + tr) / 2;
if (ind <= tm) {
if (!v->l) {
v->l = new Vertex({ MOD,MOD });
}
update(v->l, tl, tm, ind, vl);
}
else {
if (!v->r) {
v->r = new Vertex({ MOD,MOD });
}
update(v->r, tm + 1, tr, ind, vl);
}
v->vl = min(get_value(v->l), get_value(v->r));
}
}
pair<ll, ll> get_min(Vertex* v, ll tl, ll tr, ll l, ll r) {
if (l > r || !v)return { MOD,MOD };
if (tl == l && tr == r) {
return get_value(v);
}
else {
ll tm = (tl + tr) / 2;
return min(get_min(v->l, tl, tm, l, min(r, tm)),
get_min(v->r, tm + 1, tr, max(l, tm + 1), r));
}
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
//freopen("in.txt", "r", stdin);
cin >> n >> m;
for (ll i = 0; i < n; i++) {
ll l, r;
cin >> l >> r;
a.push_back(Point(l, r, true, i));
a.push_back(Point(l, r, false, i));
ls[i] = l;
rs[i] = r;
}
sort(a.begin(), a.end());
for (ll i = 0; i < a.size(); i++) {
if (a[i].st) {
d[a[i].r].insert({ a[i].l,a[i].ind });
update(root, 0, MOD - 1, a[i].r, *d[a[i].r].begin());
up[a[i].ind][0] = get_min(root, 0, MOD - 1, 0, a[i].r).ss;
}
else {
d[a[i].r].erase(d[a[i].r].find({ a[i].l,a[i].ind }));
if (d[a[i].r].empty())update(root, 0, MOD - 1, a[i].r, { MOD,MOD });
else {
update(root, 0, MOD - 1, a[i].r, *d[a[i].r].begin());
}
}
}
/*for (ll i = 0; i < n; i++) {
cout << up[i][0] << " ";
}
cout << endl;*/
for (ll j = 1; j < LG; j++) {
for (ll i = 0; i < n; i++) {
up[i][j] = up[up[i][j - 1]][j - 1];
//cout << up[i][j] << " ";
}
//cout << endl;
}
for (ll i = 0; i < m; i++) {
ll l, r;
cin >> l >> r;
l--, r--;
if (rs[l] > rs[r]) {
cout << "impossible" << endl;
}
else if (ls[up[r][LG - 1]] > rs[l]) {
cout << "impossible" << endl;
//cout << up[r][0] << " " << ls[up[r][0]] << " "
// << up[r][LG - 1] << " " << ls[up[r][LG - 1]] << endl;
//cout << l << " " << ls[l] << " " << rs[l] << endl;
}
else if (l == r) {
cout << 0 << endl;
}
else if (rs[l] >= ls[r]) {
cout << 1 << endl;
}
else {
ll res = 0;
for (ll j = LG - 1; j >= 0; j--) {
if (ls[up[r][j]] > rs[l]) {
r = up[r][j];
res += (1ll << j);
}
}
r = up[r][0];
res++;
res++;
cout << res << endl;
}
}
}
/// ---- - -------- ------ -------- -- - - -
/// Just a reminder. Ubuntu password is I O I
/// ---- - -------- ------ -------- -- - - -
Compilation message
events.cpp: In constructor 'Point::Point(ll, ll, bool, ll)':
events.cpp:32:7: warning: 'Point::st' will be initialized after [-Wreorder]
32 | bool st;
| ^~
events.cpp:31:5: warning: 'll Point::ind' [-Wreorder]
31 | ll ind;
| ^~~
events.cpp:33:2: warning: when initialized here [-Wreorder]
33 | Point(ll _l, ll _r, bool _st, ll i) :l(_l), r(_r), st(_st), ind(i) {}
| ^~~~~
events.cpp: In function 'int main()':
events.cpp:119:19: warning: comparison of integer expressions of different signedness: 'll' {aka 'long long int'} and 'std::vector<Point>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
119 | for (ll i = 0; i < a.size(); i++) {
| ~~^~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
430 ms |
99552 KB |
Output is correct |
3 |
Correct |
445 ms |
103264 KB |
Output is correct |
4 |
Correct |
487 ms |
104588 KB |
Output is correct |
5 |
Correct |
528 ms |
105324 KB |
Output is correct |
6 |
Correct |
427 ms |
105152 KB |
Output is correct |
7 |
Correct |
470 ms |
102588 KB |
Output is correct |
8 |
Correct |
466 ms |
99768 KB |
Output is correct |
9 |
Correct |
442 ms |
107984 KB |
Output is correct |
10 |
Correct |
483 ms |
100244 KB |
Output is correct |
11 |
Correct |
477 ms |
99676 KB |
Output is correct |
12 |
Correct |
321 ms |
42880 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
384 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
3 ms |
1620 KB |
Output is correct |
4 |
Correct |
3 ms |
1580 KB |
Output is correct |
5 |
Correct |
3 ms |
720 KB |
Output is correct |
6 |
Correct |
4 ms |
1108 KB |
Output is correct |
7 |
Correct |
3 ms |
1620 KB |
Output is correct |
8 |
Correct |
5 ms |
1616 KB |
Output is correct |
9 |
Correct |
2 ms |
596 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
384 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
3 ms |
1620 KB |
Output is correct |
4 |
Correct |
3 ms |
1580 KB |
Output is correct |
5 |
Correct |
3 ms |
720 KB |
Output is correct |
6 |
Correct |
4 ms |
1108 KB |
Output is correct |
7 |
Correct |
3 ms |
1620 KB |
Output is correct |
8 |
Correct |
5 ms |
1616 KB |
Output is correct |
9 |
Correct |
2 ms |
596 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
4 ms |
1616 KB |
Output is correct |
13 |
Correct |
3 ms |
1620 KB |
Output is correct |
14 |
Correct |
3 ms |
724 KB |
Output is correct |
15 |
Correct |
3 ms |
1108 KB |
Output is correct |
16 |
Correct |
4 ms |
1620 KB |
Output is correct |
17 |
Correct |
4 ms |
1616 KB |
Output is correct |
18 |
Correct |
3 ms |
596 KB |
Output is correct |
19 |
Correct |
191 ms |
4288 KB |
Output is correct |
20 |
Correct |
151 ms |
7644 KB |
Output is correct |
21 |
Correct |
187 ms |
4304 KB |
Output is correct |
22 |
Correct |
171 ms |
5852 KB |
Output is correct |
23 |
Correct |
156 ms |
8236 KB |
Output is correct |
24 |
Correct |
169 ms |
8148 KB |
Output is correct |
25 |
Correct |
159 ms |
7972 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
384 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
3 ms |
1620 KB |
Output is correct |
4 |
Correct |
3 ms |
1580 KB |
Output is correct |
5 |
Correct |
3 ms |
720 KB |
Output is correct |
6 |
Correct |
4 ms |
1108 KB |
Output is correct |
7 |
Correct |
3 ms |
1620 KB |
Output is correct |
8 |
Correct |
5 ms |
1616 KB |
Output is correct |
9 |
Correct |
2 ms |
596 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
3 ms |
1592 KB |
Output is correct |
13 |
Correct |
3 ms |
1620 KB |
Output is correct |
14 |
Correct |
2 ms |
724 KB |
Output is correct |
15 |
Correct |
3 ms |
1108 KB |
Output is correct |
16 |
Correct |
4 ms |
1620 KB |
Output is correct |
17 |
Correct |
4 ms |
1612 KB |
Output is correct |
18 |
Correct |
3 ms |
724 KB |
Output is correct |
19 |
Correct |
287 ms |
101224 KB |
Output is correct |
20 |
Correct |
217 ms |
45880 KB |
Output is correct |
21 |
Correct |
271 ms |
65344 KB |
Output is correct |
22 |
Correct |
302 ms |
94868 KB |
Output is correct |
23 |
Correct |
337 ms |
106200 KB |
Output is correct |
24 |
Correct |
361 ms |
106784 KB |
Output is correct |
25 |
Correct |
146 ms |
33624 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
447 ms |
99540 KB |
Output is correct |
2 |
Correct |
406 ms |
103280 KB |
Output is correct |
3 |
Correct |
499 ms |
104476 KB |
Output is correct |
4 |
Correct |
440 ms |
107828 KB |
Output is correct |
5 |
Correct |
460 ms |
100220 KB |
Output is correct |
6 |
Correct |
504 ms |
107608 KB |
Output is correct |
7 |
Correct |
547 ms |
107552 KB |
Output is correct |
8 |
Correct |
458 ms |
105740 KB |
Output is correct |
9 |
Correct |
339 ms |
106372 KB |
Output is correct |
10 |
Correct |
481 ms |
107452 KB |
Output is correct |
11 |
Correct |
470 ms |
106872 KB |
Output is correct |
12 |
Correct |
475 ms |
107096 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
430 ms |
99552 KB |
Output is correct |
3 |
Correct |
445 ms |
103264 KB |
Output is correct |
4 |
Correct |
487 ms |
104588 KB |
Output is correct |
5 |
Correct |
528 ms |
105324 KB |
Output is correct |
6 |
Correct |
427 ms |
105152 KB |
Output is correct |
7 |
Correct |
470 ms |
102588 KB |
Output is correct |
8 |
Correct |
466 ms |
99768 KB |
Output is correct |
9 |
Correct |
442 ms |
107984 KB |
Output is correct |
10 |
Correct |
483 ms |
100244 KB |
Output is correct |
11 |
Correct |
477 ms |
99676 KB |
Output is correct |
12 |
Correct |
321 ms |
42880 KB |
Output is correct |
13 |
Correct |
1 ms |
384 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
3 ms |
1620 KB |
Output is correct |
16 |
Correct |
3 ms |
1580 KB |
Output is correct |
17 |
Correct |
3 ms |
720 KB |
Output is correct |
18 |
Correct |
4 ms |
1108 KB |
Output is correct |
19 |
Correct |
3 ms |
1620 KB |
Output is correct |
20 |
Correct |
5 ms |
1616 KB |
Output is correct |
21 |
Correct |
2 ms |
596 KB |
Output is correct |
22 |
Correct |
1 ms |
340 KB |
Output is correct |
23 |
Correct |
1 ms |
340 KB |
Output is correct |
24 |
Correct |
4 ms |
1616 KB |
Output is correct |
25 |
Correct |
3 ms |
1620 KB |
Output is correct |
26 |
Correct |
3 ms |
724 KB |
Output is correct |
27 |
Correct |
3 ms |
1108 KB |
Output is correct |
28 |
Correct |
4 ms |
1620 KB |
Output is correct |
29 |
Correct |
4 ms |
1616 KB |
Output is correct |
30 |
Correct |
3 ms |
596 KB |
Output is correct |
31 |
Correct |
191 ms |
4288 KB |
Output is correct |
32 |
Correct |
151 ms |
7644 KB |
Output is correct |
33 |
Correct |
187 ms |
4304 KB |
Output is correct |
34 |
Correct |
171 ms |
5852 KB |
Output is correct |
35 |
Correct |
156 ms |
8236 KB |
Output is correct |
36 |
Correct |
169 ms |
8148 KB |
Output is correct |
37 |
Correct |
159 ms |
7972 KB |
Output is correct |
38 |
Correct |
1 ms |
340 KB |
Output is correct |
39 |
Correct |
1 ms |
340 KB |
Output is correct |
40 |
Correct |
3 ms |
1592 KB |
Output is correct |
41 |
Correct |
3 ms |
1620 KB |
Output is correct |
42 |
Correct |
2 ms |
724 KB |
Output is correct |
43 |
Correct |
3 ms |
1108 KB |
Output is correct |
44 |
Correct |
4 ms |
1620 KB |
Output is correct |
45 |
Correct |
4 ms |
1612 KB |
Output is correct |
46 |
Correct |
3 ms |
724 KB |
Output is correct |
47 |
Correct |
287 ms |
101224 KB |
Output is correct |
48 |
Correct |
217 ms |
45880 KB |
Output is correct |
49 |
Correct |
271 ms |
65344 KB |
Output is correct |
50 |
Correct |
302 ms |
94868 KB |
Output is correct |
51 |
Correct |
337 ms |
106200 KB |
Output is correct |
52 |
Correct |
361 ms |
106784 KB |
Output is correct |
53 |
Correct |
146 ms |
33624 KB |
Output is correct |
54 |
Correct |
447 ms |
99540 KB |
Output is correct |
55 |
Correct |
406 ms |
103280 KB |
Output is correct |
56 |
Correct |
499 ms |
104476 KB |
Output is correct |
57 |
Correct |
440 ms |
107828 KB |
Output is correct |
58 |
Correct |
460 ms |
100220 KB |
Output is correct |
59 |
Correct |
504 ms |
107608 KB |
Output is correct |
60 |
Correct |
547 ms |
107552 KB |
Output is correct |
61 |
Correct |
458 ms |
105740 KB |
Output is correct |
62 |
Correct |
339 ms |
106372 KB |
Output is correct |
63 |
Correct |
481 ms |
107452 KB |
Output is correct |
64 |
Correct |
470 ms |
106872 KB |
Output is correct |
65 |
Correct |
475 ms |
107096 KB |
Output is correct |
66 |
Correct |
1 ms |
340 KB |
Output is correct |
67 |
Correct |
451 ms |
101228 KB |
Output is correct |
68 |
Correct |
427 ms |
103404 KB |
Output is correct |
69 |
Correct |
489 ms |
104508 KB |
Output is correct |
70 |
Correct |
448 ms |
105428 KB |
Output is correct |
71 |
Correct |
461 ms |
105140 KB |
Output is correct |
72 |
Correct |
477 ms |
102508 KB |
Output is correct |
73 |
Correct |
410 ms |
99628 KB |
Output is correct |
74 |
Correct |
462 ms |
107868 KB |
Output is correct |
75 |
Correct |
497 ms |
100416 KB |
Output is correct |
76 |
Correct |
457 ms |
99660 KB |
Output is correct |
77 |
Correct |
348 ms |
42820 KB |
Output is correct |
78 |
Correct |
1 ms |
340 KB |
Output is correct |
79 |
Correct |
4 ms |
1620 KB |
Output is correct |
80 |
Correct |
4 ms |
1620 KB |
Output is correct |
81 |
Correct |
2 ms |
724 KB |
Output is correct |
82 |
Correct |
3 ms |
1108 KB |
Output is correct |
83 |
Correct |
3 ms |
1616 KB |
Output is correct |
84 |
Correct |
4 ms |
1620 KB |
Output is correct |
85 |
Correct |
2 ms |
596 KB |
Output is correct |
86 |
Correct |
171 ms |
4376 KB |
Output is correct |
87 |
Correct |
147 ms |
7616 KB |
Output is correct |
88 |
Correct |
150 ms |
4300 KB |
Output is correct |
89 |
Correct |
154 ms |
5876 KB |
Output is correct |
90 |
Correct |
157 ms |
8152 KB |
Output is correct |
91 |
Correct |
146 ms |
8156 KB |
Output is correct |
92 |
Correct |
149 ms |
7880 KB |
Output is correct |
93 |
Correct |
290 ms |
101232 KB |
Output is correct |
94 |
Correct |
184 ms |
45836 KB |
Output is correct |
95 |
Correct |
253 ms |
65284 KB |
Output is correct |
96 |
Correct |
298 ms |
94796 KB |
Output is correct |
97 |
Correct |
310 ms |
106300 KB |
Output is correct |
98 |
Correct |
324 ms |
106712 KB |
Output is correct |
99 |
Correct |
132 ms |
33628 KB |
Output is correct |
100 |
Correct |
506 ms |
107500 KB |
Output is correct |
101 |
Correct |
523 ms |
107460 KB |
Output is correct |
102 |
Correct |
455 ms |
105656 KB |
Output is correct |
103 |
Correct |
455 ms |
107120 KB |
Output is correct |
104 |
Correct |
497 ms |
106796 KB |
Output is correct |
105 |
Correct |
472 ms |
107136 KB |
Output is correct |
106 |
Correct |
366 ms |
43804 KB |
Output is correct |
107 |
Correct |
357 ms |
47088 KB |
Output is correct |
108 |
Correct |
423 ms |
66152 KB |
Output is correct |
109 |
Correct |
415 ms |
66200 KB |
Output is correct |
110 |
Correct |
627 ms |
105388 KB |
Output is correct |
111 |
Correct |
571 ms |
105400 KB |
Output is correct |