#include <algorithm>
#include <iostream>
#include <numeric>
const int MAXN = 100000 + 10;
const int MAXLOG = 18;
const int INF = 1e9 + 10;
struct Event
{
int s, e;
inline friend bool operator < (Event a, Event b)
{
return a.s < b.s || (a.s == b.s && a.e < b.e);
}
} ev[MAXN];
int n, q;
int getLog[MAXN];
int rmq[MAXLOG][MAXN];
int lift[MAXLOG][MAXN];
int perm[MAXN], newNum[MAXN];
int cmp(int x, int y)
{
if (ev[x].e >= ev[y].e) return x;
return y;
}
void buildRMQ()
{
std::iota(rmq[0]+1, rmq[0]+1+n, 1);
for (int log = 1 ; (1 << log) <= n ; ++log)
{
for (int i = 1 ; i + (1 << log) - 1 <= n ; ++i)
{
rmq[log][i] = cmp(rmq[log-1][i], rmq[log-1][i + (1 << log-1)]);
}
}
for (int i = 1 ; i <= n ; ++i)
{
getLog[i] = getLog[i-1];
if ((1 << getLog[i]+1) < i) ++getLog[i];
}
}
int upperBinSearch(int value)
{
int l = 1, r = n+1, mid;
while (l < r - 1)
{
mid = (l + r) / 2;
if (ev[mid].s <= value) l = mid;
else r = mid;
}
return l;
}
int lowerBinSearch(int idx)
{
int l = 0, r = n, mid;
while (l < r - 1)
{
mid = (l + r) / 2;
if (mid <= idx || ev[mid].s < ev[idx].s) l = mid;
else r = mid;
}
return r;
}
int findMaximum(int l, int r)
{
int log = getLog[r-l+1];
return cmp(rmq[log][l], rmq[log][r - (1 << log) + 1]);
}
void buildLift()
{
for (int log = 1 ; (1 << log) <= n ; ++log)
{
for (int i = 1 ; i <= n ; ++i)
{
lift[log][i] = lift[log-1][lift[log-1][i]];
}
}
}
int binaryLifting(int from, int value)
{
if (ev[from].s > value) return -1;
if (ev[from].e >= value) return 1;
int ans = 2;
for (int log = MAXLOG-1 ; log >= 0 ; --log)
{
if (lift[log][from] == 0) continue;
if (ev[lift[log][from]].e < value)
{
ans += (1 << log);
from = lift[log][from];
}
}
if (ev[lift[0][from]].e < value) return -1;
else return ans;
}
void solve()
{
std::iota(perm+1, perm+1+n, 1);
std::sort(perm+1, perm+1+n, [&](int x, int y)
{
return ev[x] < ev[y];
});
for (int i = 1 ; i <= n ; ++i)
{
newNum[perm[i]] = i;
}
std::sort(ev+1, ev+1+n);
buildRMQ();
for (int i = 1 ; i <= n ; ++i)
{
int from = lowerBinSearch(i);
int to = upperBinSearch(ev[i].e);
if (i+1 <= from && from <= to && from >= 1 && to <= n)
{
int jump = findMaximum(from, to);
lift[0][i] = jump;
if (i == jump) lift[0][i] = 0;
}
}
buildLift();
for (int i = 1 ; i <= q ; ++i)
{
int st, en;
std::cin >> st >> en;
std::swap(st, en);
if (st == en)
{
std::cout << 0 << '\n';
continue;
}
st = newNum[st];
en = ev[newNum[en]].s;
int res = binaryLifting(st, en);
if (res == -1) std::cout << "impossible\n";
else std::cout << res << '\n';
}
}
void read()
{
std::cin >> n >> q;
for (int i = 1 ; i <= n ; ++i)
{
std::cin >> ev[i].s >> ev[i].e;
ev[i].s = INF - ev[i].s;
ev[i].e = INF - ev[i].e;
std::swap(ev[i].s, ev[i].e);
}
}
void fastIO()
{
std::ios_base :: sync_with_stdio(0);
std::cout.tie(nullptr);
std::cin.tie(nullptr);
}
int main()
{
fastIO();
read();
solve();
return 0;
}
Compilation message
events.cpp: In function 'void buildRMQ()':
events.cpp:37:70: warning: suggest parentheses around '-' inside '<<' [-Wparentheses]
37 | rmq[log][i] = cmp(rmq[log-1][i], rmq[log-1][i + (1 << log-1)]);
| ~~~^~
events.cpp:44:28: warning: suggest parentheses around '+' inside '<<' [-Wparentheses]
44 | if ((1 << getLog[i]+1) < i) ++getLog[i];
| ~~~~~~~~~^~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
76 ms |
15700 KB |
Output is correct |
3 |
Correct |
78 ms |
15692 KB |
Output is correct |
4 |
Correct |
89 ms |
15744 KB |
Output is correct |
5 |
Correct |
82 ms |
15692 KB |
Output is correct |
6 |
Correct |
80 ms |
15748 KB |
Output is correct |
7 |
Correct |
78 ms |
15752 KB |
Output is correct |
8 |
Correct |
73 ms |
15712 KB |
Output is correct |
9 |
Correct |
74 ms |
15708 KB |
Output is correct |
10 |
Correct |
101 ms |
15948 KB |
Output is correct |
11 |
Correct |
95 ms |
16104 KB |
Output is correct |
12 |
Correct |
57 ms |
16204 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
540 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 |
340 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
540 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 |
340 KB |
Output is correct |
11 |
Correct |
0 ms |
340 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 |
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 |
1 ms |
468 KB |
Output is correct |
19 |
Correct |
28 ms |
1492 KB |
Output is correct |
20 |
Correct |
25 ms |
1500 KB |
Output is correct |
21 |
Correct |
30 ms |
1744 KB |
Output is correct |
22 |
Correct |
23 ms |
1748 KB |
Output is correct |
23 |
Correct |
24 ms |
2652 KB |
Output is correct |
24 |
Correct |
22 ms |
2644 KB |
Output is correct |
25 |
Correct |
24 ms |
2296 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
540 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 |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
340 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 |
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 |
1 ms |
468 KB |
Output is correct |
19 |
Correct |
52 ms |
15052 KB |
Output is correct |
20 |
Correct |
49 ms |
15060 KB |
Output is correct |
21 |
Correct |
59 ms |
15068 KB |
Output is correct |
22 |
Correct |
56 ms |
15072 KB |
Output is correct |
23 |
Correct |
50 ms |
15028 KB |
Output is correct |
24 |
Correct |
53 ms |
15052 KB |
Output is correct |
25 |
Correct |
36 ms |
15068 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
74 ms |
15636 KB |
Output is correct |
2 |
Correct |
78 ms |
15704 KB |
Output is correct |
3 |
Correct |
83 ms |
15684 KB |
Output is correct |
4 |
Correct |
72 ms |
15748 KB |
Output is correct |
5 |
Correct |
95 ms |
15968 KB |
Output is correct |
6 |
Correct |
97 ms |
15820 KB |
Output is correct |
7 |
Correct |
94 ms |
15820 KB |
Output is correct |
8 |
Correct |
92 ms |
15864 KB |
Output is correct |
9 |
Correct |
51 ms |
15000 KB |
Output is correct |
10 |
Correct |
77 ms |
15468 KB |
Output is correct |
11 |
Correct |
74 ms |
15228 KB |
Output is correct |
12 |
Correct |
79 ms |
15436 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
76 ms |
15700 KB |
Output is correct |
3 |
Correct |
78 ms |
15692 KB |
Output is correct |
4 |
Correct |
89 ms |
15744 KB |
Output is correct |
5 |
Correct |
82 ms |
15692 KB |
Output is correct |
6 |
Correct |
80 ms |
15748 KB |
Output is correct |
7 |
Correct |
78 ms |
15752 KB |
Output is correct |
8 |
Correct |
73 ms |
15712 KB |
Output is correct |
9 |
Correct |
74 ms |
15708 KB |
Output is correct |
10 |
Correct |
101 ms |
15948 KB |
Output is correct |
11 |
Correct |
95 ms |
16104 KB |
Output is correct |
12 |
Correct |
57 ms |
16204 KB |
Output is correct |
13 |
Correct |
0 ms |
340 KB |
Output is correct |
14 |
Correct |
0 ms |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
540 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 |
340 KB |
Output is correct |
23 |
Correct |
0 ms |
340 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 |
468 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 |
28 ms |
1492 KB |
Output is correct |
32 |
Correct |
25 ms |
1500 KB |
Output is correct |
33 |
Correct |
30 ms |
1744 KB |
Output is correct |
34 |
Correct |
23 ms |
1748 KB |
Output is correct |
35 |
Correct |
24 ms |
2652 KB |
Output is correct |
36 |
Correct |
22 ms |
2644 KB |
Output is correct |
37 |
Correct |
24 ms |
2296 KB |
Output is correct |
38 |
Correct |
1 ms |
340 KB |
Output is correct |
39 |
Correct |
1 ms |
340 KB |
Output is correct |
40 |
Correct |
1 ms |
468 KB |
Output is correct |
41 |
Correct |
1 ms |
468 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 |
1 ms |
468 KB |
Output is correct |
47 |
Correct |
52 ms |
15052 KB |
Output is correct |
48 |
Correct |
49 ms |
15060 KB |
Output is correct |
49 |
Correct |
59 ms |
15068 KB |
Output is correct |
50 |
Correct |
56 ms |
15072 KB |
Output is correct |
51 |
Correct |
50 ms |
15028 KB |
Output is correct |
52 |
Correct |
53 ms |
15052 KB |
Output is correct |
53 |
Correct |
36 ms |
15068 KB |
Output is correct |
54 |
Correct |
74 ms |
15636 KB |
Output is correct |
55 |
Correct |
78 ms |
15704 KB |
Output is correct |
56 |
Correct |
83 ms |
15684 KB |
Output is correct |
57 |
Correct |
72 ms |
15748 KB |
Output is correct |
58 |
Correct |
95 ms |
15968 KB |
Output is correct |
59 |
Correct |
97 ms |
15820 KB |
Output is correct |
60 |
Correct |
94 ms |
15820 KB |
Output is correct |
61 |
Correct |
92 ms |
15864 KB |
Output is correct |
62 |
Correct |
51 ms |
15000 KB |
Output is correct |
63 |
Correct |
77 ms |
15468 KB |
Output is correct |
64 |
Correct |
74 ms |
15228 KB |
Output is correct |
65 |
Correct |
79 ms |
15436 KB |
Output is correct |
66 |
Correct |
1 ms |
340 KB |
Output is correct |
67 |
Correct |
77 ms |
17008 KB |
Output is correct |
68 |
Correct |
80 ms |
17252 KB |
Output is correct |
69 |
Correct |
85 ms |
17120 KB |
Output is correct |
70 |
Correct |
79 ms |
17240 KB |
Output is correct |
71 |
Correct |
81 ms |
17276 KB |
Output is correct |
72 |
Correct |
82 ms |
17236 KB |
Output is correct |
73 |
Correct |
77 ms |
17328 KB |
Output is correct |
74 |
Correct |
74 ms |
17244 KB |
Output is correct |
75 |
Correct |
95 ms |
17532 KB |
Output is correct |
76 |
Correct |
106 ms |
17796 KB |
Output is correct |
77 |
Correct |
59 ms |
17872 KB |
Output is correct |
78 |
Correct |
0 ms |
340 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 |
476 KB |
Output is correct |
86 |
Correct |
29 ms |
2424 KB |
Output is correct |
87 |
Correct |
26 ms |
2508 KB |
Output is correct |
88 |
Correct |
30 ms |
2768 KB |
Output is correct |
89 |
Correct |
24 ms |
2736 KB |
Output is correct |
90 |
Correct |
24 ms |
2636 KB |
Output is correct |
91 |
Correct |
22 ms |
2568 KB |
Output is correct |
92 |
Correct |
27 ms |
2240 KB |
Output is correct |
93 |
Correct |
55 ms |
16692 KB |
Output is correct |
94 |
Correct |
49 ms |
16132 KB |
Output is correct |
95 |
Correct |
65 ms |
16772 KB |
Output is correct |
96 |
Correct |
55 ms |
16740 KB |
Output is correct |
97 |
Correct |
52 ms |
16724 KB |
Output is correct |
98 |
Correct |
55 ms |
16580 KB |
Output is correct |
99 |
Correct |
36 ms |
16276 KB |
Output is correct |
100 |
Correct |
101 ms |
17296 KB |
Output is correct |
101 |
Correct |
102 ms |
17356 KB |
Output is correct |
102 |
Correct |
96 ms |
17448 KB |
Output is correct |
103 |
Correct |
78 ms |
16972 KB |
Output is correct |
104 |
Correct |
80 ms |
16860 KB |
Output is correct |
105 |
Correct |
77 ms |
17152 KB |
Output is correct |
106 |
Correct |
92 ms |
17416 KB |
Output is correct |
107 |
Correct |
106 ms |
17484 KB |
Output is correct |
108 |
Correct |
91 ms |
17488 KB |
Output is correct |
109 |
Correct |
93 ms |
17376 KB |
Output is correct |
110 |
Correct |
91 ms |
17192 KB |
Output is correct |
111 |
Correct |
91 ms |
17372 KB |
Output is correct |