#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;
int ans = 1 + (ev[from].e < value);
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 |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
85 ms |
15700 KB |
Output is correct |
3 |
Correct |
83 ms |
15692 KB |
Output is correct |
4 |
Correct |
84 ms |
15580 KB |
Output is correct |
5 |
Correct |
78 ms |
15712 KB |
Output is correct |
6 |
Correct |
82 ms |
15760 KB |
Output is correct |
7 |
Correct |
78 ms |
15704 KB |
Output is correct |
8 |
Correct |
81 ms |
15744 KB |
Output is correct |
9 |
Correct |
79 ms |
15756 KB |
Output is correct |
10 |
Correct |
94 ms |
16044 KB |
Output is correct |
11 |
Correct |
93 ms |
16208 KB |
Output is correct |
12 |
Correct |
59 ms |
16200 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
388 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
468 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 |
1 ms |
388 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
468 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 |
29 ms |
1500 KB |
Output is correct |
20 |
Correct |
33 ms |
1476 KB |
Output is correct |
21 |
Correct |
32 ms |
1740 KB |
Output is correct |
22 |
Incorrect |
24 ms |
1748 KB |
Output isn't correct |
23 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
388 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
468 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 |
69 ms |
15032 KB |
Output is correct |
20 |
Correct |
61 ms |
15052 KB |
Output is correct |
21 |
Correct |
61 ms |
15076 KB |
Output is correct |
22 |
Correct |
54 ms |
15056 KB |
Output is correct |
23 |
Correct |
52 ms |
15076 KB |
Output is correct |
24 |
Correct |
63 ms |
14976 KB |
Output is correct |
25 |
Correct |
36 ms |
15096 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
97 ms |
15680 KB |
Output is correct |
2 |
Correct |
78 ms |
15684 KB |
Output is correct |
3 |
Correct |
89 ms |
15592 KB |
Output is correct |
4 |
Correct |
75 ms |
15720 KB |
Output is correct |
5 |
Correct |
121 ms |
16048 KB |
Output is correct |
6 |
Correct |
102 ms |
15888 KB |
Output is correct |
7 |
Correct |
107 ms |
15772 KB |
Output is correct |
8 |
Correct |
125 ms |
15892 KB |
Output is correct |
9 |
Correct |
50 ms |
15056 KB |
Output is correct |
10 |
Correct |
87 ms |
15432 KB |
Output is correct |
11 |
Correct |
79 ms |
15224 KB |
Output is correct |
12 |
Correct |
79 ms |
15448 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
85 ms |
15700 KB |
Output is correct |
3 |
Correct |
83 ms |
15692 KB |
Output is correct |
4 |
Correct |
84 ms |
15580 KB |
Output is correct |
5 |
Correct |
78 ms |
15712 KB |
Output is correct |
6 |
Correct |
82 ms |
15760 KB |
Output is correct |
7 |
Correct |
78 ms |
15704 KB |
Output is correct |
8 |
Correct |
81 ms |
15744 KB |
Output is correct |
9 |
Correct |
79 ms |
15756 KB |
Output is correct |
10 |
Correct |
94 ms |
16044 KB |
Output is correct |
11 |
Correct |
93 ms |
16208 KB |
Output is correct |
12 |
Correct |
59 ms |
16200 KB |
Output is correct |
13 |
Correct |
1 ms |
388 KB |
Output is correct |
14 |
Correct |
0 ms |
340 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 |
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 |
29 ms |
1500 KB |
Output is correct |
32 |
Correct |
33 ms |
1476 KB |
Output is correct |
33 |
Correct |
32 ms |
1740 KB |
Output is correct |
34 |
Incorrect |
24 ms |
1748 KB |
Output isn't correct |
35 |
Halted |
0 ms |
0 KB |
- |