#pragma GCC optimize("O3,unroll-loops")
#pragma GCC target("avx,popcnt,sse4,abm")
#include <bits/stdc++.h>
using namespace std;
#ifdef WAIMAI
#define debug(HEHE...) cout << "[" << #HEHE << "] : ", dout(HEHE)
void dout() {cout << '\n';}
template<typename T, typename...U>
void dout(T t, U...u) {cout << t << (sizeof... (u) ? ", " : ""), dout (u...);}
#else
#define debug(...) 7122
#endif
#define ll long long
#define Waimai ios::sync_with_stdio(false), cin.tie(0)
#define FOR(x,a,b) for (int x = a, I = b; x <= I; x++)
#define pb emplace_back
#define F first
#define S second
#define lpos pos*2
#define rpos pos*2+1
const int SIZE = 1e5 + 5;
const int H = __lg(SIZE) + 1;
int n, q;
pair<int, int> a[SIZE], p[SIZE];
vector<int> lis;
pair<int, int> node[4 * SIZE];
int to[SIZE][H + 1];
void build(int pos, int l, int r) {
if (l == r) {
node[pos] = a[l];
return;
}
int mid = (l + r) / 2;
build(lpos, l, mid);
build(rpos, mid + 1, r);
node[pos] = min(node[lpos], node[rpos]);
}
pair<int, int> que(int pos, int l, int r, int L, int R) {
if (l == L && r == R) return node[pos];
int mid = (L + R) / 2;
if (r <= mid) return que(lpos, l, r, L, mid);
else if (l > mid) return que(rpos, l, r, mid + 1, R);
return min(que(lpos, l, mid, L, mid), que(rpos, mid + 1, r, mid + 1, R));
}
void solve() {
cin >> n >> q;
FOR (i, 1, n) {
auto &[l, r] = p[i];
cin >> l >> r;
lis.pb(r);
}
sort(lis.begin(), lis.end());
lis.erase(unique(lis.begin(), lis.end()), lis.end());
fill(a + 1, a + n + 1, make_pair(INT_MAX, 0));
FOR (i, 1, n) {
int pos = lower_bound(lis.begin(), lis.end(), p[i].S) - lis.begin() + 1;
a[pos] = min(a[pos], {p[i].F, i});
}
build(1, 1, n);
FOR (i, 1, n) {
int l = lower_bound(lis.begin(), lis.end(), p[i].F) - lis.begin() + 1;
int r = lower_bound(lis.begin(), lis.end(), p[i].S) - lis.begin() + 1;
to[i][0] = que(1, l, r, 1, n).S;
debug(i, to[i][0]);
}
FOR (j, 1, H) FOR (i, 1, n) to[i][j] = to[to[i][j - 1]][j - 1];
while (q--) {
int x, y, ans = 1;
cin >> x >> y;
if (x == y || (p[y].F <= p[x].S && p[x].S <= p[y].S)) {
cout << (x != y) << '\n';
continue;
}
for (int i = H; ~i; i--) if (p[x].S < p[to[y][i]].F) {
ans += 1 << i;
y = to[y][i];
debug(ans, y);
}
y = to[y][0];
debug(ans, y);
if (p[x].S < p[y].F || p[x].S > p[y].S) cout << "impossible\n";
else cout << ans + (p[x].S >= p[y].F) << '\n';
}
}
int main() {
Waimai;
solve();
}
Compilation message
events.cpp: In function 'void solve()':
events.cpp:12:20: warning: statement has no effect [-Wunused-value]
12 | #define debug(...) 7122
| ^~~~
events.cpp:73:9: note: in expansion of macro 'debug'
73 | debug(i, to[i][0]);
| ^~~~~
events.cpp:12:20: warning: statement has no effect [-Wunused-value]
12 | #define debug(...) 7122
| ^~~~
events.cpp:86:13: note: in expansion of macro 'debug'
86 | debug(ans, y);
| ^~~~~
events.cpp:12:20: warning: statement has no effect [-Wunused-value]
12 | #define debug(...) 7122
| ^~~~
events.cpp:89:9: note: in expansion of macro 'debug'
89 | debug(ans, y);
| ^~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
125 ms |
15160 KB |
Output is correct |
3 |
Correct |
117 ms |
15148 KB |
Output is correct |
4 |
Correct |
136 ms |
15200 KB |
Output is correct |
5 |
Correct |
124 ms |
15256 KB |
Output is correct |
6 |
Correct |
156 ms |
15400 KB |
Output is correct |
7 |
Correct |
115 ms |
15244 KB |
Output is correct |
8 |
Correct |
104 ms |
15712 KB |
Output is correct |
9 |
Correct |
112 ms |
15676 KB |
Output is correct |
10 |
Correct |
141 ms |
15532 KB |
Output is correct |
11 |
Correct |
120 ms |
15584 KB |
Output is correct |
12 |
Correct |
69 ms |
14840 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
468 KB |
Output is correct |
4 |
Correct |
1 ms |
376 KB |
Output is correct |
5 |
Correct |
2 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
468 KB |
Output is correct |
7 |
Correct |
1 ms |
488 KB |
Output is correct |
8 |
Correct |
1 ms |
468 KB |
Output is correct |
9 |
Correct |
1 ms |
468 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
468 KB |
Output is correct |
4 |
Correct |
1 ms |
376 KB |
Output is correct |
5 |
Correct |
2 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
468 KB |
Output is correct |
7 |
Correct |
1 ms |
488 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 |
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 |
32 ms |
2388 KB |
Output is correct |
20 |
Correct |
31 ms |
2472 KB |
Output is correct |
21 |
Correct |
32 ms |
2640 KB |
Output is correct |
22 |
Correct |
35 ms |
2736 KB |
Output is correct |
23 |
Correct |
25 ms |
2600 KB |
Output is correct |
24 |
Correct |
26 ms |
2612 KB |
Output is correct |
25 |
Correct |
28 ms |
2256 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
468 KB |
Output is correct |
4 |
Correct |
1 ms |
376 KB |
Output is correct |
5 |
Correct |
2 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
468 KB |
Output is correct |
7 |
Correct |
1 ms |
488 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 |
344 KB |
Output is correct |
11 |
Correct |
1 ms |
336 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 |
472 KB |
Output is correct |
15 |
Correct |
1 ms |
468 KB |
Output is correct |
16 |
Correct |
2 ms |
540 KB |
Output is correct |
17 |
Correct |
2 ms |
476 KB |
Output is correct |
18 |
Correct |
1 ms |
476 KB |
Output is correct |
19 |
Correct |
84 ms |
13472 KB |
Output is correct |
20 |
Correct |
77 ms |
12580 KB |
Output is correct |
21 |
Correct |
94 ms |
13316 KB |
Output is correct |
22 |
Correct |
87 ms |
13396 KB |
Output is correct |
23 |
Correct |
79 ms |
13408 KB |
Output is correct |
24 |
Correct |
99 ms |
13400 KB |
Output is correct |
25 |
Correct |
38 ms |
12676 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
127 ms |
15220 KB |
Output is correct |
2 |
Correct |
141 ms |
15152 KB |
Output is correct |
3 |
Correct |
155 ms |
15156 KB |
Output is correct |
4 |
Correct |
104 ms |
15816 KB |
Output is correct |
5 |
Correct |
128 ms |
15588 KB |
Output is correct |
6 |
Correct |
155 ms |
15336 KB |
Output is correct |
7 |
Correct |
176 ms |
15264 KB |
Output is correct |
8 |
Correct |
147 ms |
15404 KB |
Output is correct |
9 |
Correct |
77 ms |
13416 KB |
Output is correct |
10 |
Correct |
128 ms |
14908 KB |
Output is correct |
11 |
Correct |
128 ms |
14792 KB |
Output is correct |
12 |
Correct |
128 ms |
14956 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
125 ms |
15160 KB |
Output is correct |
3 |
Correct |
117 ms |
15148 KB |
Output is correct |
4 |
Correct |
136 ms |
15200 KB |
Output is correct |
5 |
Correct |
124 ms |
15256 KB |
Output is correct |
6 |
Correct |
156 ms |
15400 KB |
Output is correct |
7 |
Correct |
115 ms |
15244 KB |
Output is correct |
8 |
Correct |
104 ms |
15712 KB |
Output is correct |
9 |
Correct |
112 ms |
15676 KB |
Output is correct |
10 |
Correct |
141 ms |
15532 KB |
Output is correct |
11 |
Correct |
120 ms |
15584 KB |
Output is correct |
12 |
Correct |
69 ms |
14840 KB |
Output is correct |
13 |
Correct |
0 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
468 KB |
Output is correct |
16 |
Correct |
1 ms |
376 KB |
Output is correct |
17 |
Correct |
2 ms |
340 KB |
Output is correct |
18 |
Correct |
1 ms |
468 KB |
Output is correct |
19 |
Correct |
1 ms |
488 KB |
Output is correct |
20 |
Correct |
1 ms |
468 KB |
Output is correct |
21 |
Correct |
1 ms |
468 KB |
Output is correct |
22 |
Correct |
1 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 |
32 ms |
2388 KB |
Output is correct |
32 |
Correct |
31 ms |
2472 KB |
Output is correct |
33 |
Correct |
32 ms |
2640 KB |
Output is correct |
34 |
Correct |
35 ms |
2736 KB |
Output is correct |
35 |
Correct |
25 ms |
2600 KB |
Output is correct |
36 |
Correct |
26 ms |
2612 KB |
Output is correct |
37 |
Correct |
28 ms |
2256 KB |
Output is correct |
38 |
Correct |
1 ms |
344 KB |
Output is correct |
39 |
Correct |
1 ms |
336 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 |
472 KB |
Output is correct |
43 |
Correct |
1 ms |
468 KB |
Output is correct |
44 |
Correct |
2 ms |
540 KB |
Output is correct |
45 |
Correct |
2 ms |
476 KB |
Output is correct |
46 |
Correct |
1 ms |
476 KB |
Output is correct |
47 |
Correct |
84 ms |
13472 KB |
Output is correct |
48 |
Correct |
77 ms |
12580 KB |
Output is correct |
49 |
Correct |
94 ms |
13316 KB |
Output is correct |
50 |
Correct |
87 ms |
13396 KB |
Output is correct |
51 |
Correct |
79 ms |
13408 KB |
Output is correct |
52 |
Correct |
99 ms |
13400 KB |
Output is correct |
53 |
Correct |
38 ms |
12676 KB |
Output is correct |
54 |
Correct |
127 ms |
15220 KB |
Output is correct |
55 |
Correct |
141 ms |
15152 KB |
Output is correct |
56 |
Correct |
155 ms |
15156 KB |
Output is correct |
57 |
Correct |
104 ms |
15816 KB |
Output is correct |
58 |
Correct |
128 ms |
15588 KB |
Output is correct |
59 |
Correct |
155 ms |
15336 KB |
Output is correct |
60 |
Correct |
176 ms |
15264 KB |
Output is correct |
61 |
Correct |
147 ms |
15404 KB |
Output is correct |
62 |
Correct |
77 ms |
13416 KB |
Output is correct |
63 |
Correct |
128 ms |
14908 KB |
Output is correct |
64 |
Correct |
128 ms |
14792 KB |
Output is correct |
65 |
Correct |
128 ms |
14956 KB |
Output is correct |
66 |
Correct |
1 ms |
344 KB |
Output is correct |
67 |
Correct |
108 ms |
15124 KB |
Output is correct |
68 |
Correct |
119 ms |
15216 KB |
Output is correct |
69 |
Correct |
156 ms |
15188 KB |
Output is correct |
70 |
Correct |
129 ms |
15196 KB |
Output is correct |
71 |
Correct |
119 ms |
15180 KB |
Output is correct |
72 |
Correct |
127 ms |
15284 KB |
Output is correct |
73 |
Correct |
112 ms |
15656 KB |
Output is correct |
74 |
Correct |
110 ms |
15704 KB |
Output is correct |
75 |
Correct |
137 ms |
15584 KB |
Output is correct |
76 |
Correct |
125 ms |
15556 KB |
Output is correct |
77 |
Correct |
74 ms |
14932 KB |
Output is correct |
78 |
Correct |
1 ms |
340 KB |
Output is correct |
79 |
Correct |
1 ms |
372 KB |
Output is correct |
80 |
Correct |
1 ms |
476 KB |
Output is correct |
81 |
Correct |
1 ms |
468 KB |
Output is correct |
82 |
Correct |
1 ms |
480 KB |
Output is correct |
83 |
Correct |
1 ms |
468 KB |
Output is correct |
84 |
Correct |
1 ms |
484 KB |
Output is correct |
85 |
Correct |
1 ms |
468 KB |
Output is correct |
86 |
Correct |
31 ms |
2364 KB |
Output is correct |
87 |
Correct |
29 ms |
2388 KB |
Output is correct |
88 |
Correct |
39 ms |
2624 KB |
Output is correct |
89 |
Correct |
36 ms |
2764 KB |
Output is correct |
90 |
Correct |
25 ms |
2560 KB |
Output is correct |
91 |
Correct |
25 ms |
2528 KB |
Output is correct |
92 |
Correct |
26 ms |
2240 KB |
Output is correct |
93 |
Correct |
83 ms |
13368 KB |
Output is correct |
94 |
Correct |
96 ms |
12564 KB |
Output is correct |
95 |
Correct |
106 ms |
13252 KB |
Output is correct |
96 |
Correct |
86 ms |
13396 KB |
Output is correct |
97 |
Correct |
79 ms |
13388 KB |
Output is correct |
98 |
Correct |
91 ms |
13420 KB |
Output is correct |
99 |
Correct |
32 ms |
12648 KB |
Output is correct |
100 |
Correct |
155 ms |
15452 KB |
Output is correct |
101 |
Correct |
134 ms |
15308 KB |
Output is correct |
102 |
Correct |
140 ms |
15400 KB |
Output is correct |
103 |
Correct |
129 ms |
14952 KB |
Output is correct |
104 |
Correct |
125 ms |
14764 KB |
Output is correct |
105 |
Correct |
125 ms |
14948 KB |
Output is correct |
106 |
Correct |
120 ms |
14548 KB |
Output is correct |
107 |
Correct |
135 ms |
14556 KB |
Output is correct |
108 |
Correct |
115 ms |
15252 KB |
Output is correct |
109 |
Correct |
132 ms |
15340 KB |
Output is correct |
110 |
Correct |
139 ms |
15128 KB |
Output is correct |
111 |
Correct |
129 ms |
15172 KB |
Output is correct |