#include <bits/stdc++.h>
using namespace std;
#define REP(i, j, k) for (int i = j; i < k; i++)
#define RREP(i, j, k) for (int i = j; i >= k; i--)
template <class T>
inline bool mnto(T &a, const T b) {return a > b ? a = b, 1 : 0;}
template <class T>
inline bool mxto(T &a, const T b) {return a < b ? a = b, 1 : 0;}
typedef long long ll;
#define FI first
#define SE second
typedef pair<int, int> ii;
typedef pair<ll, ll> pll;
#define ALL(x) x.begin(), x.end()
#define SZ(x) (int) x.size()
#define pb push_back
typedef vector<int> vi;
typedef vector<ii> vii;
typedef vector<ll> vll;
typedef tuple<int, int, int> iii;
#ifndef DEBUG
#define cerr if (0) cerr
#endif
const int MAXN = 200005;
const int INF = 1000000005;
const ll LINF = 1000000000000000005;
const int MAXL = 20;
struct event {
int s, e, i;
};
int n, q;
event se[MAXN];
int mp[MAXN];
int p[MAXL][MAXN];
int m;
int sp[MAXL][MAXN];
int qmn(int s, int e) {
int k = 31 - __builtin_clz(e - s + 1);
return min(sp[k][s], sp[k][e - (1 << k) + 1]);
}
int main() {
#ifndef DEBUG
ios::sync_with_stdio(0), cin.tie(0);
#endif
cin >> n >> q;
REP (i, 1, n + 1) {
cin >> se[i].s >> se[i].e;
se[i].i = i;
}
sort(se + 1, se + n + 1, [&] (event l, event r) {
return l.s < r.s;
});
vii disc;
REP (i, 1, n + 1) {
mp[se[i].i] = i;
disc.pb({se[i].s, i});
disc.pb({se[i].e, i + n});
}
sort(ALL(disc));
int prv = -1;
for (auto [x, i] : disc) {
if (x != prv) {
prv = x;
m++;
}
if (i <= n) {
se[i].s = m;
} else {
se[i - n].e = m;
}
}
REP (i, 1, n + 1) {
cerr << i << ": " << se[i].s << ' ' << se[i].e << '\n';
}
memset(p, -1, sizeof p);
REP (i, 1, m + 1) {
sp[0][i] = INF;
}
REP (i, 1, n + 1) {
mnto(sp[0][se[i].e], i);
}
REP (k, 1, MAXL) {
REP (i, 1, m + 1 - (1 << k - 1)) {
sp[k][i] = min(sp[k - 1][i], sp[k - 1][i + (1 << k - 1)]);
}
}
REP (i, 1, n + 1) {
p[0][i] = qmn(se[i].s, se[i].e);
cerr << i << " -> " << p[0][i] << '\n';
}
/*
REP (i, 1, n + 1) {
ii mn = {INF, -1};
REP (j, 1, n + 1) {
if (i == j) {
continue;
}
if (se[i].s <= se[j].e && se[j].e <= se[i].e) {
mnto(mn, {se[j].s, j});
}
}
p[0][i] = mn.SE;
cerr << i << " -> " << p[0][i] << '\n';
}
*/
REP (k, 1, MAXL) {
REP (i, 1, n + 1) {
if (p[k - 1][i] == -1) {
p[k][i] = -1;
} else {
p[k][i] = p[k - 1][p[k - 1][i]];
}
}
}
while (q--) {
int a, b; cin >> a >> b;
a = mp[a]; b = mp[b];
if (a == b) {
cout << 0 << '\n';
continue;
}
if (se[b].s <= se[a].e && se[a].e <= se[b].e) {
cout << 1 << '\n';
continue;
}
if (se[a].e > se[b].e) {
cout << "impossible\n";
continue;
}
int ans = 0;
RREP (k, MAXL - 1, 0) {
if (p[k][b] == -1) {
continue;
}
if (se[p[k][b]].s > se[a].e) {
b = p[k][b];
ans += 1 << k;
}
}
if (p[0][b] != -1 && se[p[0][b]].s <= se[a].e) {
ans += 2;
cout << ans << '\n';
} else {
cout << "impossible\n";
}
}
return 0;
}
Compilation message
events.cpp: In function 'int main()':
events.cpp:93:29: warning: suggest parentheses around '-' inside '<<' [-Wparentheses]
93 | REP (i, 1, m + 1 - (1 << k - 1)) {
| ~~^~~
events.cpp:4:42: note: in definition of macro 'REP'
4 | #define REP(i, j, k) for (int i = j; i < k; i++)
| ^
events.cpp:94:57: warning: suggest parentheses around '-' inside '<<' [-Wparentheses]
94 | sp[k][i] = min(sp[k - 1][i], sp[k - 1][i + (1 << k - 1)]);
| ~~^~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
15956 KB |
Output is correct |
2 |
Correct |
77 ms |
29480 KB |
Output is correct |
3 |
Correct |
79 ms |
29516 KB |
Output is correct |
4 |
Correct |
88 ms |
29380 KB |
Output is correct |
5 |
Correct |
93 ms |
30180 KB |
Output is correct |
6 |
Correct |
103 ms |
30968 KB |
Output is correct |
7 |
Correct |
106 ms |
31104 KB |
Output is correct |
8 |
Correct |
86 ms |
37220 KB |
Output is correct |
9 |
Correct |
89 ms |
37104 KB |
Output is correct |
10 |
Correct |
143 ms |
29868 KB |
Output is correct |
11 |
Correct |
115 ms |
32720 KB |
Output is correct |
12 |
Correct |
60 ms |
29180 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
15956 KB |
Output is correct |
2 |
Correct |
8 ms |
15956 KB |
Output is correct |
3 |
Correct |
7 ms |
16096 KB |
Output is correct |
4 |
Correct |
10 ms |
16084 KB |
Output is correct |
5 |
Correct |
10 ms |
16084 KB |
Output is correct |
6 |
Correct |
8 ms |
16084 KB |
Output is correct |
7 |
Correct |
9 ms |
16100 KB |
Output is correct |
8 |
Correct |
7 ms |
16084 KB |
Output is correct |
9 |
Correct |
9 ms |
15964 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
15956 KB |
Output is correct |
2 |
Correct |
8 ms |
15956 KB |
Output is correct |
3 |
Correct |
7 ms |
16096 KB |
Output is correct |
4 |
Correct |
10 ms |
16084 KB |
Output is correct |
5 |
Correct |
10 ms |
16084 KB |
Output is correct |
6 |
Correct |
8 ms |
16084 KB |
Output is correct |
7 |
Correct |
9 ms |
16100 KB |
Output is correct |
8 |
Correct |
7 ms |
16084 KB |
Output is correct |
9 |
Correct |
9 ms |
15964 KB |
Output is correct |
10 |
Correct |
6 ms |
15956 KB |
Output is correct |
11 |
Correct |
7 ms |
15940 KB |
Output is correct |
12 |
Correct |
6 ms |
16084 KB |
Output is correct |
13 |
Correct |
8 ms |
16084 KB |
Output is correct |
14 |
Correct |
8 ms |
16108 KB |
Output is correct |
15 |
Correct |
8 ms |
16084 KB |
Output is correct |
16 |
Correct |
8 ms |
16116 KB |
Output is correct |
17 |
Correct |
8 ms |
16100 KB |
Output is correct |
18 |
Correct |
7 ms |
15972 KB |
Output is correct |
19 |
Correct |
39 ms |
18124 KB |
Output is correct |
20 |
Correct |
35 ms |
17996 KB |
Output is correct |
21 |
Correct |
51 ms |
18344 KB |
Output is correct |
22 |
Correct |
32 ms |
18404 KB |
Output is correct |
23 |
Correct |
29 ms |
18468 KB |
Output is correct |
24 |
Correct |
30 ms |
18508 KB |
Output is correct |
25 |
Correct |
45 ms |
18168 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
15956 KB |
Output is correct |
2 |
Correct |
8 ms |
15956 KB |
Output is correct |
3 |
Correct |
7 ms |
16096 KB |
Output is correct |
4 |
Correct |
10 ms |
16084 KB |
Output is correct |
5 |
Correct |
10 ms |
16084 KB |
Output is correct |
6 |
Correct |
8 ms |
16084 KB |
Output is correct |
7 |
Correct |
9 ms |
16100 KB |
Output is correct |
8 |
Correct |
7 ms |
16084 KB |
Output is correct |
9 |
Correct |
9 ms |
15964 KB |
Output is correct |
10 |
Correct |
6 ms |
15956 KB |
Output is correct |
11 |
Correct |
6 ms |
15956 KB |
Output is correct |
12 |
Correct |
7 ms |
16084 KB |
Output is correct |
13 |
Correct |
9 ms |
16052 KB |
Output is correct |
14 |
Correct |
8 ms |
16148 KB |
Output is correct |
15 |
Correct |
9 ms |
16060 KB |
Output is correct |
16 |
Correct |
8 ms |
16212 KB |
Output is correct |
17 |
Correct |
7 ms |
16212 KB |
Output is correct |
18 |
Correct |
7 ms |
15956 KB |
Output is correct |
19 |
Correct |
51 ms |
27716 KB |
Output is correct |
20 |
Correct |
74 ms |
26936 KB |
Output is correct |
21 |
Correct |
72 ms |
27460 KB |
Output is correct |
22 |
Correct |
58 ms |
30224 KB |
Output is correct |
23 |
Correct |
56 ms |
34884 KB |
Output is correct |
24 |
Correct |
55 ms |
35048 KB |
Output is correct |
25 |
Correct |
52 ms |
20372 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
97 ms |
29504 KB |
Output is correct |
2 |
Correct |
72 ms |
29512 KB |
Output is correct |
3 |
Correct |
84 ms |
29308 KB |
Output is correct |
4 |
Correct |
89 ms |
37096 KB |
Output is correct |
5 |
Correct |
94 ms |
29744 KB |
Output is correct |
6 |
Correct |
121 ms |
36784 KB |
Output is correct |
7 |
Correct |
147 ms |
36792 KB |
Output is correct |
8 |
Correct |
105 ms |
37032 KB |
Output is correct |
9 |
Correct |
61 ms |
34904 KB |
Output is correct |
10 |
Correct |
83 ms |
36532 KB |
Output is correct |
11 |
Correct |
82 ms |
36360 KB |
Output is correct |
12 |
Correct |
78 ms |
36448 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
15956 KB |
Output is correct |
2 |
Correct |
77 ms |
29480 KB |
Output is correct |
3 |
Correct |
79 ms |
29516 KB |
Output is correct |
4 |
Correct |
88 ms |
29380 KB |
Output is correct |
5 |
Correct |
93 ms |
30180 KB |
Output is correct |
6 |
Correct |
103 ms |
30968 KB |
Output is correct |
7 |
Correct |
106 ms |
31104 KB |
Output is correct |
8 |
Correct |
86 ms |
37220 KB |
Output is correct |
9 |
Correct |
89 ms |
37104 KB |
Output is correct |
10 |
Correct |
143 ms |
29868 KB |
Output is correct |
11 |
Correct |
115 ms |
32720 KB |
Output is correct |
12 |
Correct |
60 ms |
29180 KB |
Output is correct |
13 |
Correct |
7 ms |
15956 KB |
Output is correct |
14 |
Correct |
8 ms |
15956 KB |
Output is correct |
15 |
Correct |
7 ms |
16096 KB |
Output is correct |
16 |
Correct |
10 ms |
16084 KB |
Output is correct |
17 |
Correct |
10 ms |
16084 KB |
Output is correct |
18 |
Correct |
8 ms |
16084 KB |
Output is correct |
19 |
Correct |
9 ms |
16100 KB |
Output is correct |
20 |
Correct |
7 ms |
16084 KB |
Output is correct |
21 |
Correct |
9 ms |
15964 KB |
Output is correct |
22 |
Correct |
6 ms |
15956 KB |
Output is correct |
23 |
Correct |
7 ms |
15940 KB |
Output is correct |
24 |
Correct |
6 ms |
16084 KB |
Output is correct |
25 |
Correct |
8 ms |
16084 KB |
Output is correct |
26 |
Correct |
8 ms |
16108 KB |
Output is correct |
27 |
Correct |
8 ms |
16084 KB |
Output is correct |
28 |
Correct |
8 ms |
16116 KB |
Output is correct |
29 |
Correct |
8 ms |
16100 KB |
Output is correct |
30 |
Correct |
7 ms |
15972 KB |
Output is correct |
31 |
Correct |
39 ms |
18124 KB |
Output is correct |
32 |
Correct |
35 ms |
17996 KB |
Output is correct |
33 |
Correct |
51 ms |
18344 KB |
Output is correct |
34 |
Correct |
32 ms |
18404 KB |
Output is correct |
35 |
Correct |
29 ms |
18468 KB |
Output is correct |
36 |
Correct |
30 ms |
18508 KB |
Output is correct |
37 |
Correct |
45 ms |
18168 KB |
Output is correct |
38 |
Correct |
6 ms |
15956 KB |
Output is correct |
39 |
Correct |
6 ms |
15956 KB |
Output is correct |
40 |
Correct |
7 ms |
16084 KB |
Output is correct |
41 |
Correct |
9 ms |
16052 KB |
Output is correct |
42 |
Correct |
8 ms |
16148 KB |
Output is correct |
43 |
Correct |
9 ms |
16060 KB |
Output is correct |
44 |
Correct |
8 ms |
16212 KB |
Output is correct |
45 |
Correct |
7 ms |
16212 KB |
Output is correct |
46 |
Correct |
7 ms |
15956 KB |
Output is correct |
47 |
Correct |
51 ms |
27716 KB |
Output is correct |
48 |
Correct |
74 ms |
26936 KB |
Output is correct |
49 |
Correct |
72 ms |
27460 KB |
Output is correct |
50 |
Correct |
58 ms |
30224 KB |
Output is correct |
51 |
Correct |
56 ms |
34884 KB |
Output is correct |
52 |
Correct |
55 ms |
35048 KB |
Output is correct |
53 |
Correct |
52 ms |
20372 KB |
Output is correct |
54 |
Correct |
97 ms |
29504 KB |
Output is correct |
55 |
Correct |
72 ms |
29512 KB |
Output is correct |
56 |
Correct |
84 ms |
29308 KB |
Output is correct |
57 |
Correct |
89 ms |
37096 KB |
Output is correct |
58 |
Correct |
94 ms |
29744 KB |
Output is correct |
59 |
Correct |
121 ms |
36784 KB |
Output is correct |
60 |
Correct |
147 ms |
36792 KB |
Output is correct |
61 |
Correct |
105 ms |
37032 KB |
Output is correct |
62 |
Correct |
61 ms |
34904 KB |
Output is correct |
63 |
Correct |
83 ms |
36532 KB |
Output is correct |
64 |
Correct |
82 ms |
36360 KB |
Output is correct |
65 |
Correct |
78 ms |
36448 KB |
Output is correct |
66 |
Correct |
7 ms |
15956 KB |
Output is correct |
67 |
Correct |
87 ms |
29488 KB |
Output is correct |
68 |
Correct |
81 ms |
29412 KB |
Output is correct |
69 |
Correct |
90 ms |
29408 KB |
Output is correct |
70 |
Correct |
77 ms |
30284 KB |
Output is correct |
71 |
Correct |
82 ms |
30960 KB |
Output is correct |
72 |
Correct |
92 ms |
31132 KB |
Output is correct |
73 |
Correct |
81 ms |
37144 KB |
Output is correct |
74 |
Correct |
75 ms |
37288 KB |
Output is correct |
75 |
Correct |
91 ms |
29848 KB |
Output is correct |
76 |
Correct |
134 ms |
32696 KB |
Output is correct |
77 |
Correct |
66 ms |
29172 KB |
Output is correct |
78 |
Correct |
6 ms |
15956 KB |
Output is correct |
79 |
Correct |
6 ms |
16084 KB |
Output is correct |
80 |
Correct |
7 ms |
16064 KB |
Output is correct |
81 |
Correct |
8 ms |
16148 KB |
Output is correct |
82 |
Correct |
7 ms |
16084 KB |
Output is correct |
83 |
Correct |
8 ms |
16096 KB |
Output is correct |
84 |
Correct |
7 ms |
16212 KB |
Output is correct |
85 |
Correct |
7 ms |
15956 KB |
Output is correct |
86 |
Correct |
40 ms |
18116 KB |
Output is correct |
87 |
Correct |
35 ms |
18036 KB |
Output is correct |
88 |
Correct |
38 ms |
18292 KB |
Output is correct |
89 |
Correct |
33 ms |
18352 KB |
Output is correct |
90 |
Correct |
30 ms |
18480 KB |
Output is correct |
91 |
Correct |
27 ms |
18488 KB |
Output is correct |
92 |
Correct |
31 ms |
18136 KB |
Output is correct |
93 |
Correct |
46 ms |
27716 KB |
Output is correct |
94 |
Correct |
52 ms |
26964 KB |
Output is correct |
95 |
Correct |
56 ms |
27656 KB |
Output is correct |
96 |
Correct |
50 ms |
30404 KB |
Output is correct |
97 |
Correct |
55 ms |
35004 KB |
Output is correct |
98 |
Correct |
52 ms |
34976 KB |
Output is correct |
99 |
Correct |
38 ms |
20436 KB |
Output is correct |
100 |
Correct |
106 ms |
36876 KB |
Output is correct |
101 |
Correct |
108 ms |
36836 KB |
Output is correct |
102 |
Correct |
91 ms |
36948 KB |
Output is correct |
103 |
Correct |
80 ms |
36484 KB |
Output is correct |
104 |
Correct |
96 ms |
36080 KB |
Output is correct |
105 |
Correct |
78 ms |
36476 KB |
Output is correct |
106 |
Correct |
89 ms |
27204 KB |
Output is correct |
107 |
Correct |
111 ms |
28964 KB |
Output is correct |
108 |
Correct |
109 ms |
29616 KB |
Output is correct |
109 |
Correct |
93 ms |
29596 KB |
Output is correct |
110 |
Correct |
86 ms |
36748 KB |
Output is correct |
111 |
Correct |
89 ms |
36840 KB |
Output is correct |