#include<bits/stdc++.h>
using namespace std;
int const N = 1e5+10;
struct interval {
int s, e;
interval() = default;
interval(int a, int b) : s(a), e(b) {}
} IT[N];
bool it_cmp(interval& a, interval& b) {
if (a.s == b.s) return a.e > b.e;
return a.s < b.s;
}
bool it_cmp2(interval& a, interval& b) {
if (a.e == b.e) return a.s > b.s;
return a.e < b.e;
}
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
struct node {
interval it;
int idx, meta, prior;
node *l, *r;
node() = default;
node(int i) : it(IT[i]), idx(i), meta(i), prior(rng()), l(0), r(0) {}
};
using pnode = node*;
pnode rt = nullptr, rt2=nullptr;
void update(pnode u) {
if (!u) return;
u->meta = u->idx;
if (u->l && IT[u->l->meta].e > IT[u->meta].e) u->meta = u->l->meta;
if (u->r && IT[u->r->meta].e > IT[u->meta].e) u->meta = u->r->meta;
}
void update2(pnode u) {
if (!u) return;
u->meta = u->idx;
if (u->l && IT[u->l->meta].s < IT[u->meta].s) u->meta = u->l->meta;
if (u->r && IT[u->r->meta].s < IT[u->meta].s) u->meta = u->r->meta;
}
void split(pnode rt, pnode& l, pnode& r, int v, int const type = 0) {
if (!rt) return (void)(l=r=nullptr);
int flag = type?(v<rt->it.e):(v<rt->it.s);
if (flag) split(rt->l, l, rt->l, v, type), r = rt;
else split(rt->r, rt->r, r, v, type), l = rt;
if (type) update2(rt);
else update(rt);
}
void split(pnode rt, pnode& l, pnode& r, interval it, int const type = 0) {
if (!rt) return (void)(l=r=nullptr);
int flag = type?it_cmp2(it, rt->it):it_cmp(it, rt->it);
if (flag) split(rt->l, l, rt->l, it, type), r = rt;
else split(rt->r, rt->r, r, it, type), l = rt;
if (type) update2(rt);
else update(rt);
}
void join(pnode& rt, pnode l, pnode r, int const type = 0) {
if (!l || !r) rt = l?:r;
else if (l->prior > r->prior) join(l->r, l->r, r, type), rt = l;
else join(r->l, l, r->l, type), rt = r;
if (type) update2(rt);
else update(rt);
}
void add(int idx) {
if (rt == nullptr) {
rt = new node(idx);
rt2 = new node(idx);
}
else {
pnode nn = new node(idx);
pnode lv, rv;
split(rt, lv, rv, nn->it, 0);
join(rt, lv, nn, 0); join(rt, rt, rv, 0);
pnode nn2 = new node(idx);
split(rt2, lv, rv, nn2->it, 1);
join(rt2, lv, nn2, 1); join(rt2, rt2, rv, 1);
}
}
int sparse[18][N];
int border[18][N];
void dfs(pnode r) {
if (!r) return;
dfs(r->l);
cerr << "[" << r->it.s << ", " << r->it.e << "] ";
dfs(r->r);
}
int main() {
ios::sync_with_stdio(false); cin.tie(nullptr);
int n, q; cin >> n >> q;
for (int i = 1; i <= n; ++i) {
cin >> IT[i].s >> IT[i].e;
add(i);
}
//dfs(rt); cerr << '\n';
for (int i = 1; i <= n; ++i) {
pnode lv, mv, rv; split(rt, lv, rv, IT[i].e);
//dfs(lv);
//cerr << lv->meta << ' ';
sparse[0][i] = lv->meta;
join(rt, lv, rv);
split(rt2, lv, rv, IT[i].e, 1);
split(lv, lv, mv, IT[i].s-1, 1);
border[0][i] = mv->meta;
join(rt2, lv, mv, 1);
join(rt2, rt2, rv, 1);
}
//cerr << '\n';
for (int b = 1; b < 18; ++b) {
for (int i = 1; i <= n; ++i) sparse[b][i] = sparse[b-1][sparse[b-1][i]];
for (int i = 1; i <= n; ++i) border[b][i] = border[b-1][border[b-1][i]];
}
for (int i = 0; i < q; ++i) {
int a, b; cin >> a >> b;
if (a == b) {
cout << 0 << '\n';
continue;
}
if (IT[a].e > IT[b].e) {
cout << "impossible\n";
continue;
}
else if (IT[sparse[17][a]].e < IT[b].s) {
cout << "impossible\n";
continue;
}
int st = 0;
for (int bb = 16; ~bb; --bb) {
int idx = sparse[bb][a];
if (IT[idx].e < IT[b].e) {
st += 1<<bb;
a = idx;
}
}
if (IT[a].e >= IT[b].s) {
++st;
cout << st << '\n';
continue;
}
if (IT[border[17][b]].s > IT[a].e) {
cout << "impossible\n";
continue;
}
for (int bb = 16; ~bb; --bb) {
int idx = border[bb][b];
if (IT[idx].s > IT[a].e) {
st += 1<<bb;
b = idx;
}
}
//if (border[0][b] > IT[a].e) {
// cout << "impossible\n";
// continue;
//}
cout << (st+2) << '\n';
}
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
856 KB |
Output is correct |
2 |
Correct |
368 ms |
28328 KB |
Output is correct |
3 |
Correct |
397 ms |
28188 KB |
Output is correct |
4 |
Correct |
388 ms |
28424 KB |
Output is correct |
5 |
Correct |
395 ms |
28244 KB |
Output is correct |
6 |
Correct |
407 ms |
28440 KB |
Output is correct |
7 |
Correct |
417 ms |
28500 KB |
Output is correct |
8 |
Correct |
363 ms |
28756 KB |
Output is correct |
9 |
Correct |
383 ms |
28752 KB |
Output is correct |
10 |
Correct |
376 ms |
28744 KB |
Output is correct |
11 |
Correct |
391 ms |
28752 KB |
Output is correct |
12 |
Correct |
132 ms |
27988 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
604 KB |
Output is correct |
2 |
Correct |
1 ms |
604 KB |
Output is correct |
3 |
Correct |
2 ms |
860 KB |
Output is correct |
4 |
Correct |
2 ms |
872 KB |
Output is correct |
5 |
Correct |
2 ms |
1112 KB |
Output is correct |
6 |
Correct |
2 ms |
860 KB |
Output is correct |
7 |
Correct |
3 ms |
860 KB |
Output is correct |
8 |
Correct |
1 ms |
860 KB |
Output is correct |
9 |
Correct |
1 ms |
860 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
604 KB |
Output is correct |
2 |
Correct |
1 ms |
604 KB |
Output is correct |
3 |
Correct |
2 ms |
860 KB |
Output is correct |
4 |
Correct |
2 ms |
872 KB |
Output is correct |
5 |
Correct |
2 ms |
1112 KB |
Output is correct |
6 |
Correct |
2 ms |
860 KB |
Output is correct |
7 |
Correct |
3 ms |
860 KB |
Output is correct |
8 |
Correct |
1 ms |
860 KB |
Output is correct |
9 |
Correct |
1 ms |
860 KB |
Output is correct |
10 |
Correct |
0 ms |
604 KB |
Output is correct |
11 |
Correct |
0 ms |
604 KB |
Output is correct |
12 |
Correct |
2 ms |
832 KB |
Output is correct |
13 |
Correct |
2 ms |
860 KB |
Output is correct |
14 |
Correct |
3 ms |
860 KB |
Output is correct |
15 |
Correct |
2 ms |
860 KB |
Output is correct |
16 |
Correct |
2 ms |
852 KB |
Output is correct |
17 |
Correct |
2 ms |
752 KB |
Output is correct |
18 |
Correct |
1 ms |
860 KB |
Output is correct |
19 |
Correct |
32 ms |
3256 KB |
Output is correct |
20 |
Correct |
31 ms |
3164 KB |
Output is correct |
21 |
Correct |
35 ms |
3496 KB |
Output is correct |
22 |
Correct |
29 ms |
3412 KB |
Output is correct |
23 |
Correct |
29 ms |
3404 KB |
Output is correct |
24 |
Correct |
22 ms |
3408 KB |
Output is correct |
25 |
Correct |
26 ms |
3164 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
604 KB |
Output is correct |
2 |
Correct |
1 ms |
604 KB |
Output is correct |
3 |
Correct |
2 ms |
860 KB |
Output is correct |
4 |
Correct |
2 ms |
872 KB |
Output is correct |
5 |
Correct |
2 ms |
1112 KB |
Output is correct |
6 |
Correct |
2 ms |
860 KB |
Output is correct |
7 |
Correct |
3 ms |
860 KB |
Output is correct |
8 |
Correct |
1 ms |
860 KB |
Output is correct |
9 |
Correct |
1 ms |
860 KB |
Output is correct |
10 |
Correct |
0 ms |
604 KB |
Output is correct |
11 |
Correct |
0 ms |
480 KB |
Output is correct |
12 |
Correct |
2 ms |
860 KB |
Output is correct |
13 |
Correct |
2 ms |
744 KB |
Output is correct |
14 |
Correct |
3 ms |
856 KB |
Output is correct |
15 |
Correct |
2 ms |
808 KB |
Output is correct |
16 |
Correct |
3 ms |
860 KB |
Output is correct |
17 |
Correct |
2 ms |
856 KB |
Output is correct |
18 |
Correct |
1 ms |
860 KB |
Output is correct |
19 |
Correct |
338 ms |
26456 KB |
Output is correct |
20 |
Correct |
343 ms |
25684 KB |
Output is correct |
21 |
Correct |
475 ms |
26440 KB |
Output is correct |
22 |
Correct |
375 ms |
26452 KB |
Output is correct |
23 |
Correct |
267 ms |
26448 KB |
Output is correct |
24 |
Correct |
386 ms |
26452 KB |
Output is correct |
25 |
Correct |
73 ms |
25976 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
362 ms |
28208 KB |
Output is correct |
2 |
Correct |
364 ms |
28244 KB |
Output is correct |
3 |
Correct |
371 ms |
28244 KB |
Output is correct |
4 |
Correct |
342 ms |
28696 KB |
Output is correct |
5 |
Correct |
358 ms |
28752 KB |
Output is correct |
6 |
Correct |
420 ms |
28496 KB |
Output is correct |
7 |
Correct |
415 ms |
28496 KB |
Output is correct |
8 |
Correct |
395 ms |
28500 KB |
Output is correct |
9 |
Correct |
257 ms |
26448 KB |
Output is correct |
10 |
Correct |
412 ms |
28164 KB |
Output is correct |
11 |
Correct |
374 ms |
27988 KB |
Output is correct |
12 |
Correct |
419 ms |
28108 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
856 KB |
Output is correct |
2 |
Correct |
368 ms |
28328 KB |
Output is correct |
3 |
Correct |
397 ms |
28188 KB |
Output is correct |
4 |
Correct |
388 ms |
28424 KB |
Output is correct |
5 |
Correct |
395 ms |
28244 KB |
Output is correct |
6 |
Correct |
407 ms |
28440 KB |
Output is correct |
7 |
Correct |
417 ms |
28500 KB |
Output is correct |
8 |
Correct |
363 ms |
28756 KB |
Output is correct |
9 |
Correct |
383 ms |
28752 KB |
Output is correct |
10 |
Correct |
376 ms |
28744 KB |
Output is correct |
11 |
Correct |
391 ms |
28752 KB |
Output is correct |
12 |
Correct |
132 ms |
27988 KB |
Output is correct |
13 |
Correct |
1 ms |
604 KB |
Output is correct |
14 |
Correct |
1 ms |
604 KB |
Output is correct |
15 |
Correct |
2 ms |
860 KB |
Output is correct |
16 |
Correct |
2 ms |
872 KB |
Output is correct |
17 |
Correct |
2 ms |
1112 KB |
Output is correct |
18 |
Correct |
2 ms |
860 KB |
Output is correct |
19 |
Correct |
3 ms |
860 KB |
Output is correct |
20 |
Correct |
1 ms |
860 KB |
Output is correct |
21 |
Correct |
1 ms |
860 KB |
Output is correct |
22 |
Correct |
0 ms |
604 KB |
Output is correct |
23 |
Correct |
0 ms |
604 KB |
Output is correct |
24 |
Correct |
2 ms |
832 KB |
Output is correct |
25 |
Correct |
2 ms |
860 KB |
Output is correct |
26 |
Correct |
3 ms |
860 KB |
Output is correct |
27 |
Correct |
2 ms |
860 KB |
Output is correct |
28 |
Correct |
2 ms |
852 KB |
Output is correct |
29 |
Correct |
2 ms |
752 KB |
Output is correct |
30 |
Correct |
1 ms |
860 KB |
Output is correct |
31 |
Correct |
32 ms |
3256 KB |
Output is correct |
32 |
Correct |
31 ms |
3164 KB |
Output is correct |
33 |
Correct |
35 ms |
3496 KB |
Output is correct |
34 |
Correct |
29 ms |
3412 KB |
Output is correct |
35 |
Correct |
29 ms |
3404 KB |
Output is correct |
36 |
Correct |
22 ms |
3408 KB |
Output is correct |
37 |
Correct |
26 ms |
3164 KB |
Output is correct |
38 |
Correct |
0 ms |
604 KB |
Output is correct |
39 |
Correct |
0 ms |
480 KB |
Output is correct |
40 |
Correct |
2 ms |
860 KB |
Output is correct |
41 |
Correct |
2 ms |
744 KB |
Output is correct |
42 |
Correct |
3 ms |
856 KB |
Output is correct |
43 |
Correct |
2 ms |
808 KB |
Output is correct |
44 |
Correct |
3 ms |
860 KB |
Output is correct |
45 |
Correct |
2 ms |
856 KB |
Output is correct |
46 |
Correct |
1 ms |
860 KB |
Output is correct |
47 |
Correct |
338 ms |
26456 KB |
Output is correct |
48 |
Correct |
343 ms |
25684 KB |
Output is correct |
49 |
Correct |
475 ms |
26440 KB |
Output is correct |
50 |
Correct |
375 ms |
26452 KB |
Output is correct |
51 |
Correct |
267 ms |
26448 KB |
Output is correct |
52 |
Correct |
386 ms |
26452 KB |
Output is correct |
53 |
Correct |
73 ms |
25976 KB |
Output is correct |
54 |
Correct |
362 ms |
28208 KB |
Output is correct |
55 |
Correct |
364 ms |
28244 KB |
Output is correct |
56 |
Correct |
371 ms |
28244 KB |
Output is correct |
57 |
Correct |
342 ms |
28696 KB |
Output is correct |
58 |
Correct |
358 ms |
28752 KB |
Output is correct |
59 |
Correct |
420 ms |
28496 KB |
Output is correct |
60 |
Correct |
415 ms |
28496 KB |
Output is correct |
61 |
Correct |
395 ms |
28500 KB |
Output is correct |
62 |
Correct |
257 ms |
26448 KB |
Output is correct |
63 |
Correct |
412 ms |
28164 KB |
Output is correct |
64 |
Correct |
374 ms |
27988 KB |
Output is correct |
65 |
Correct |
419 ms |
28108 KB |
Output is correct |
66 |
Correct |
0 ms |
604 KB |
Output is correct |
67 |
Correct |
344 ms |
28256 KB |
Output is correct |
68 |
Correct |
366 ms |
28244 KB |
Output is correct |
69 |
Correct |
388 ms |
28284 KB |
Output is correct |
70 |
Correct |
383 ms |
28244 KB |
Output is correct |
71 |
Correct |
379 ms |
28472 KB |
Output is correct |
72 |
Correct |
384 ms |
28500 KB |
Output is correct |
73 |
Correct |
360 ms |
28776 KB |
Output is correct |
74 |
Correct |
337 ms |
28752 KB |
Output is correct |
75 |
Correct |
374 ms |
28756 KB |
Output is correct |
76 |
Correct |
388 ms |
28672 KB |
Output is correct |
77 |
Correct |
133 ms |
27988 KB |
Output is correct |
78 |
Correct |
0 ms |
600 KB |
Output is correct |
79 |
Correct |
2 ms |
860 KB |
Output is correct |
80 |
Correct |
2 ms |
860 KB |
Output is correct |
81 |
Correct |
2 ms |
860 KB |
Output is correct |
82 |
Correct |
2 ms |
860 KB |
Output is correct |
83 |
Correct |
2 ms |
860 KB |
Output is correct |
84 |
Correct |
2 ms |
856 KB |
Output is correct |
85 |
Correct |
1 ms |
860 KB |
Output is correct |
86 |
Correct |
30 ms |
3164 KB |
Output is correct |
87 |
Correct |
29 ms |
3300 KB |
Output is correct |
88 |
Correct |
34 ms |
3408 KB |
Output is correct |
89 |
Correct |
28 ms |
3420 KB |
Output is correct |
90 |
Correct |
30 ms |
3408 KB |
Output is correct |
91 |
Correct |
22 ms |
3408 KB |
Output is correct |
92 |
Correct |
27 ms |
2892 KB |
Output is correct |
93 |
Correct |
323 ms |
26448 KB |
Output is correct |
94 |
Correct |
336 ms |
25680 KB |
Output is correct |
95 |
Correct |
466 ms |
26448 KB |
Output is correct |
96 |
Correct |
360 ms |
26452 KB |
Output is correct |
97 |
Correct |
259 ms |
26404 KB |
Output is correct |
98 |
Correct |
377 ms |
26452 KB |
Output is correct |
99 |
Correct |
68 ms |
25936 KB |
Output is correct |
100 |
Correct |
411 ms |
28428 KB |
Output is correct |
101 |
Correct |
410 ms |
28500 KB |
Output is correct |
102 |
Correct |
397 ms |
28504 KB |
Output is correct |
103 |
Correct |
415 ms |
27988 KB |
Output is correct |
104 |
Correct |
387 ms |
27912 KB |
Output is correct |
105 |
Correct |
413 ms |
28192 KB |
Output is correct |
106 |
Correct |
374 ms |
27732 KB |
Output is correct |
107 |
Correct |
392 ms |
27728 KB |
Output is correct |
108 |
Correct |
511 ms |
28336 KB |
Output is correct |
109 |
Correct |
487 ms |
28752 KB |
Output is correct |
110 |
Correct |
535 ms |
28372 KB |
Output is correct |
111 |
Correct |
528 ms |
28360 KB |
Output is correct |