#include <bits/stdc++.h>
using namespace std;
#define rep(i,s,e) for (int i = s; i <= e; ++i)
#define rrep(i,s,e) for (int i = s; i >= e; --i)
#define pb push_back
#define pf push_front
#define fi first
#define se second
#define all(a) a.begin(), a.end()
#define len(a) (int)a.size()
typedef long long ll;
typedef pair<int, int> ii;
typedef vector<ii> vii;
typedef vector<int> vi;
typedef vector<double> vd;
typedef vector<string> vs;
typedef vector<ll> vll;
const int mx = 2e5, mx2 = 18;
int p[mx2+1][mx+1], sparse[mx2+1][mx+1], l[mx], r[mx];
int qry (int G, int H) {
int P = __lg(H-G+1), a = sparse[P][G], b = sparse[P][H-(1<<P)+1];
if (!a) return b;
if (!b) return a;
return (l[a]<l[b]?a:b);
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
int n, q; cin >> n >> q;
set<int> st;
rep (i,1,n) {
cin >> l[i] >> r[i];
st.insert(l[i]);
st.insert(r[i]);
}
map<int,int> mp;
int c = 1;
for (int el : st) mp[el] = c++;
rep (i,1,n) l[i] = mp[l[i]], r[i] = mp[r[i]];
rep (i,1,n)
if (!sparse[0][r[i]] || l[i] < l[sparse[0][r[i]]])
sparse[0][r[i]] = i;
rep (i,1,mx2) {
rep (j,1,2*n-(1<<i)+1) {
int x = j, y = j+(1<<(i-1));
if (!sparse[i-1][x]) sparse[i][j] = sparse[i-1][y];
else if (!sparse[i-1][y]) sparse[i][j] = sparse[i-1][x];
else sparse[i][j] = (l[sparse[i-1][x]]<l[sparse[i-1][y]]?sparse[i-1][x]:sparse[i-1][y]);
}
}
rep (i,1,n) p[0][i] = qry (l[i], r[i]);
rep (i,1,mx2) rep (j,1,n) p[i][j] = p[i-1][p[i-1][j]];
while (q--) {
int s, e; cin >> s >> e;
if (r[s]>r[e]) {
cout << "impossible\n";
continue;
}
if (s==e) {
cout << "0\n";
continue;
}
if (l[e]<=r[s]) {
cout << "1\n";
continue;
}
int cur = e, ans = 0;
rrep (i,mx2,0) {
if (l[p[i][cur]]>r[s]) {
cur = p[i][cur];
ans += 1<<i;
}
}
if (l[p[0][cur]]<=r[s]) cout << ans+2 << "\n";
else cout << "impossible\n";
}
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
468 KB |
Output is correct |
2 |
Correct |
158 ms |
31260 KB |
Output is correct |
3 |
Correct |
187 ms |
31336 KB |
Output is correct |
4 |
Correct |
182 ms |
31252 KB |
Output is correct |
5 |
Correct |
198 ms |
32280 KB |
Output is correct |
6 |
Correct |
246 ms |
33228 KB |
Output is correct |
7 |
Correct |
192 ms |
33572 KB |
Output is correct |
8 |
Correct |
279 ms |
41580 KB |
Output is correct |
9 |
Correct |
232 ms |
41516 KB |
Output is correct |
10 |
Correct |
197 ms |
31644 KB |
Output is correct |
11 |
Correct |
222 ms |
35600 KB |
Output is correct |
12 |
Correct |
111 ms |
31796 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
0 ms |
468 KB |
Output is correct |
3 |
Correct |
1 ms |
724 KB |
Output is correct |
4 |
Correct |
2 ms |
744 KB |
Output is correct |
5 |
Correct |
1 ms |
724 KB |
Output is correct |
6 |
Correct |
1 ms |
848 KB |
Output is correct |
7 |
Correct |
1 ms |
852 KB |
Output is correct |
8 |
Correct |
2 ms |
852 KB |
Output is correct |
9 |
Correct |
1 ms |
596 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
0 ms |
468 KB |
Output is correct |
3 |
Correct |
1 ms |
724 KB |
Output is correct |
4 |
Correct |
2 ms |
744 KB |
Output is correct |
5 |
Correct |
1 ms |
724 KB |
Output is correct |
6 |
Correct |
1 ms |
848 KB |
Output is correct |
7 |
Correct |
1 ms |
852 KB |
Output is correct |
8 |
Correct |
2 ms |
852 KB |
Output is correct |
9 |
Correct |
1 ms |
596 KB |
Output is correct |
10 |
Correct |
0 ms |
468 KB |
Output is correct |
11 |
Correct |
0 ms |
468 KB |
Output is correct |
12 |
Correct |
1 ms |
724 KB |
Output is correct |
13 |
Correct |
2 ms |
724 KB |
Output is correct |
14 |
Correct |
1 ms |
724 KB |
Output is correct |
15 |
Correct |
1 ms |
724 KB |
Output is correct |
16 |
Correct |
2 ms |
852 KB |
Output is correct |
17 |
Correct |
2 ms |
852 KB |
Output is correct |
18 |
Correct |
1 ms |
596 KB |
Output is correct |
19 |
Correct |
32 ms |
2496 KB |
Output is correct |
20 |
Correct |
31 ms |
2252 KB |
Output is correct |
21 |
Correct |
34 ms |
2640 KB |
Output is correct |
22 |
Correct |
28 ms |
2640 KB |
Output is correct |
23 |
Correct |
26 ms |
2936 KB |
Output is correct |
24 |
Correct |
23 ms |
2900 KB |
Output is correct |
25 |
Correct |
33 ms |
2604 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
0 ms |
468 KB |
Output is correct |
3 |
Correct |
1 ms |
724 KB |
Output is correct |
4 |
Correct |
2 ms |
744 KB |
Output is correct |
5 |
Correct |
1 ms |
724 KB |
Output is correct |
6 |
Correct |
1 ms |
848 KB |
Output is correct |
7 |
Correct |
1 ms |
852 KB |
Output is correct |
8 |
Correct |
2 ms |
852 KB |
Output is correct |
9 |
Correct |
1 ms |
596 KB |
Output is correct |
10 |
Correct |
0 ms |
468 KB |
Output is correct |
11 |
Correct |
1 ms |
468 KB |
Output is correct |
12 |
Correct |
1 ms |
724 KB |
Output is correct |
13 |
Correct |
1 ms |
724 KB |
Output is correct |
14 |
Correct |
1 ms |
724 KB |
Output is correct |
15 |
Correct |
1 ms |
724 KB |
Output is correct |
16 |
Correct |
1 ms |
852 KB |
Output is correct |
17 |
Correct |
1 ms |
852 KB |
Output is correct |
18 |
Correct |
1 ms |
596 KB |
Output is correct |
19 |
Correct |
140 ms |
30656 KB |
Output is correct |
20 |
Correct |
161 ms |
30708 KB |
Output is correct |
21 |
Correct |
131 ms |
30664 KB |
Output is correct |
22 |
Correct |
185 ms |
34392 KB |
Output is correct |
23 |
Correct |
249 ms |
40044 KB |
Output is correct |
24 |
Correct |
235 ms |
40528 KB |
Output is correct |
25 |
Correct |
29 ms |
20952 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
159 ms |
31328 KB |
Output is correct |
2 |
Correct |
170 ms |
31232 KB |
Output is correct |
3 |
Correct |
188 ms |
31228 KB |
Output is correct |
4 |
Correct |
236 ms |
41584 KB |
Output is correct |
5 |
Correct |
242 ms |
31612 KB |
Output is correct |
6 |
Correct |
257 ms |
41196 KB |
Output is correct |
7 |
Correct |
258 ms |
41252 KB |
Output is correct |
8 |
Correct |
313 ms |
41396 KB |
Output is correct |
9 |
Correct |
226 ms |
40084 KB |
Output is correct |
10 |
Correct |
307 ms |
40952 KB |
Output is correct |
11 |
Correct |
286 ms |
40712 KB |
Output is correct |
12 |
Correct |
299 ms |
40908 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
468 KB |
Output is correct |
2 |
Correct |
158 ms |
31260 KB |
Output is correct |
3 |
Correct |
187 ms |
31336 KB |
Output is correct |
4 |
Correct |
182 ms |
31252 KB |
Output is correct |
5 |
Correct |
198 ms |
32280 KB |
Output is correct |
6 |
Correct |
246 ms |
33228 KB |
Output is correct |
7 |
Correct |
192 ms |
33572 KB |
Output is correct |
8 |
Correct |
279 ms |
41580 KB |
Output is correct |
9 |
Correct |
232 ms |
41516 KB |
Output is correct |
10 |
Correct |
197 ms |
31644 KB |
Output is correct |
11 |
Correct |
222 ms |
35600 KB |
Output is correct |
12 |
Correct |
111 ms |
31796 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
0 ms |
468 KB |
Output is correct |
15 |
Correct |
1 ms |
724 KB |
Output is correct |
16 |
Correct |
2 ms |
744 KB |
Output is correct |
17 |
Correct |
1 ms |
724 KB |
Output is correct |
18 |
Correct |
1 ms |
848 KB |
Output is correct |
19 |
Correct |
1 ms |
852 KB |
Output is correct |
20 |
Correct |
2 ms |
852 KB |
Output is correct |
21 |
Correct |
1 ms |
596 KB |
Output is correct |
22 |
Correct |
0 ms |
468 KB |
Output is correct |
23 |
Correct |
0 ms |
468 KB |
Output is correct |
24 |
Correct |
1 ms |
724 KB |
Output is correct |
25 |
Correct |
2 ms |
724 KB |
Output is correct |
26 |
Correct |
1 ms |
724 KB |
Output is correct |
27 |
Correct |
1 ms |
724 KB |
Output is correct |
28 |
Correct |
2 ms |
852 KB |
Output is correct |
29 |
Correct |
2 ms |
852 KB |
Output is correct |
30 |
Correct |
1 ms |
596 KB |
Output is correct |
31 |
Correct |
32 ms |
2496 KB |
Output is correct |
32 |
Correct |
31 ms |
2252 KB |
Output is correct |
33 |
Correct |
34 ms |
2640 KB |
Output is correct |
34 |
Correct |
28 ms |
2640 KB |
Output is correct |
35 |
Correct |
26 ms |
2936 KB |
Output is correct |
36 |
Correct |
23 ms |
2900 KB |
Output is correct |
37 |
Correct |
33 ms |
2604 KB |
Output is correct |
38 |
Correct |
0 ms |
468 KB |
Output is correct |
39 |
Correct |
1 ms |
468 KB |
Output is correct |
40 |
Correct |
1 ms |
724 KB |
Output is correct |
41 |
Correct |
1 ms |
724 KB |
Output is correct |
42 |
Correct |
1 ms |
724 KB |
Output is correct |
43 |
Correct |
1 ms |
724 KB |
Output is correct |
44 |
Correct |
1 ms |
852 KB |
Output is correct |
45 |
Correct |
1 ms |
852 KB |
Output is correct |
46 |
Correct |
1 ms |
596 KB |
Output is correct |
47 |
Correct |
140 ms |
30656 KB |
Output is correct |
48 |
Correct |
161 ms |
30708 KB |
Output is correct |
49 |
Correct |
131 ms |
30664 KB |
Output is correct |
50 |
Correct |
185 ms |
34392 KB |
Output is correct |
51 |
Correct |
249 ms |
40044 KB |
Output is correct |
52 |
Correct |
235 ms |
40528 KB |
Output is correct |
53 |
Correct |
29 ms |
20952 KB |
Output is correct |
54 |
Correct |
159 ms |
31328 KB |
Output is correct |
55 |
Correct |
170 ms |
31232 KB |
Output is correct |
56 |
Correct |
188 ms |
31228 KB |
Output is correct |
57 |
Correct |
236 ms |
41584 KB |
Output is correct |
58 |
Correct |
242 ms |
31612 KB |
Output is correct |
59 |
Correct |
257 ms |
41196 KB |
Output is correct |
60 |
Correct |
258 ms |
41252 KB |
Output is correct |
61 |
Correct |
313 ms |
41396 KB |
Output is correct |
62 |
Correct |
226 ms |
40084 KB |
Output is correct |
63 |
Correct |
307 ms |
40952 KB |
Output is correct |
64 |
Correct |
286 ms |
40712 KB |
Output is correct |
65 |
Correct |
299 ms |
40908 KB |
Output is correct |
66 |
Correct |
0 ms |
468 KB |
Output is correct |
67 |
Correct |
165 ms |
31308 KB |
Output is correct |
68 |
Correct |
196 ms |
31292 KB |
Output is correct |
69 |
Correct |
189 ms |
31228 KB |
Output is correct |
70 |
Correct |
185 ms |
32460 KB |
Output is correct |
71 |
Correct |
211 ms |
33228 KB |
Output is correct |
72 |
Correct |
181 ms |
33616 KB |
Output is correct |
73 |
Correct |
263 ms |
41600 KB |
Output is correct |
74 |
Correct |
247 ms |
41508 KB |
Output is correct |
75 |
Correct |
173 ms |
31624 KB |
Output is correct |
76 |
Correct |
217 ms |
35580 KB |
Output is correct |
77 |
Correct |
114 ms |
31820 KB |
Output is correct |
78 |
Correct |
1 ms |
340 KB |
Output is correct |
79 |
Correct |
1 ms |
752 KB |
Output is correct |
80 |
Correct |
2 ms |
724 KB |
Output is correct |
81 |
Correct |
1 ms |
724 KB |
Output is correct |
82 |
Correct |
1 ms |
724 KB |
Output is correct |
83 |
Correct |
1 ms |
852 KB |
Output is correct |
84 |
Correct |
2 ms |
852 KB |
Output is correct |
85 |
Correct |
1 ms |
596 KB |
Output is correct |
86 |
Correct |
35 ms |
2508 KB |
Output is correct |
87 |
Correct |
33 ms |
2260 KB |
Output is correct |
88 |
Correct |
33 ms |
2620 KB |
Output is correct |
89 |
Correct |
26 ms |
2576 KB |
Output is correct |
90 |
Correct |
24 ms |
2984 KB |
Output is correct |
91 |
Correct |
23 ms |
2940 KB |
Output is correct |
92 |
Correct |
33 ms |
2596 KB |
Output is correct |
93 |
Correct |
138 ms |
30700 KB |
Output is correct |
94 |
Correct |
118 ms |
30692 KB |
Output is correct |
95 |
Correct |
131 ms |
30716 KB |
Output is correct |
96 |
Correct |
206 ms |
34260 KB |
Output is correct |
97 |
Correct |
224 ms |
40120 KB |
Output is correct |
98 |
Correct |
226 ms |
40396 KB |
Output is correct |
99 |
Correct |
34 ms |
20828 KB |
Output is correct |
100 |
Correct |
299 ms |
41284 KB |
Output is correct |
101 |
Correct |
273 ms |
41144 KB |
Output is correct |
102 |
Correct |
274 ms |
41368 KB |
Output is correct |
103 |
Correct |
256 ms |
40920 KB |
Output is correct |
104 |
Correct |
270 ms |
40700 KB |
Output is correct |
105 |
Correct |
292 ms |
40884 KB |
Output is correct |
106 |
Correct |
148 ms |
29024 KB |
Output is correct |
107 |
Correct |
200 ms |
31456 KB |
Output is correct |
108 |
Correct |
216 ms |
31516 KB |
Output is correct |
109 |
Correct |
166 ms |
31536 KB |
Output is correct |
110 |
Correct |
273 ms |
41256 KB |
Output is correct |
111 |
Correct |
250 ms |
41128 KB |
Output is correct |