/* Author : Mychecksdead */
#include<bits/stdc++.h>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
using namespace std;
typedef long long int ll;
typedef long double ld;
#define MOD1 (1000000000+7)
#define MOD (998244353)
#define PI 3.1415926535
#define pb push_back
#define setp() cout << setprecision(15)
#define all(x) x.begin(), x.end()
#define debug(x) cerr << #x << " is " << x << '\n';
const int N = 1e6+100, M = 1e5+10, F = 2147483646, K = 20;
int n, q, t[N], up[N][K];
array<int, 3> a[N], b[N]; // original, sorted by r;
vector<int> all;
void build(int l, int r, int k){
if(l==r){
t[k] = MOD;
return;
}
int m = (l+r)>>1;
build(l, m, k<<1);
build(m+1, r, k<<1|1);
t[k] = MOD;
}
int get(int l, int r, int ql, int qr, int k){
if(ql > r || l > qr) return MOD;
if(ql <= l && r <= qr) return t[k];
int m = (l+r)>>1;
return min(get(l, m, ql, qr, k<<1), get(m + 1, r, ql, qr, k<<1|1));
}
void update(int l, int r, int p, int k, int val){
if(p > r || l > p) return;
if(l == r){
t[k] = min(t[k], val);
return;
}
int m = (l+r)>>1;
update(l, m, p, k<<1, val);
update(m+1, r, p, k<<1|1, val);
t[k] = min(t[k<<1], t[k<<1|1]);
}
bool comp(const array<int, 3> &a, const array<int, 3> &b){
if(a[1] != b[1])
return a[1] < b[1];
return a[0] < b[0];
}
void solve(){
cin >> n >> q;
for(int i = 1; i <= n; ++i){
cin >> a[i][0] >> a[i][1];
all.pb(a[i][0]);
all.pb(a[i][1]);
a[i][2] = i;
}
sort(all(all));
unique(all(all));
build(1, 2*n, 1);
for(int i = 1; i <= n; ++i){
a[i][0] = lower_bound(all(all), a[i][0]) - all.begin() + 1;
a[i][1] = lower_bound(all(all), a[i][1]) - all.begin() + 1;
b[i] = a[i];
}
sort(b + 1, b + 1 + n);
for(int i = 1; i <= n; ++i){
update(1, 2*n, b[i][1], 1, i);
a[b[i][2]][2] = i;
}
for(int i = 1; i <= n; ++i){
int p = get(1, 2*n, b[i][0], b[i][1], 1);
up[i][0] = p;
}
for(int j = 1; j < K; ++j) for(int i = 1; i <= n; ++i) up[i][j] = up[up[i][j - 1]][j - 1];
for(;q--;){
int start, end; cin >> start >> end;
if(start == end) cout << "0\n";
else if(a[start][1] > a[end][1]) cout << "impossible\n";
else if(a[start][1] >= a[end][0]) cout << "1\n";
else{
int v = a[end][2], ans = 0;
for(int j = K - 1; j >= 0; --j){
if(up[v][j] == v) break;
if(b[up[v][j]][0] > a[start][1]) v = up[v][j], ans += (1<<j);
}
if(b[up[v][0]][0] > a[start][1]){
cout << "impossible\n";
}else
cout << ans + 2 << '\n';
}
}
}
int main(){
cin.tie(0); ios::sync_with_stdio(0);
int T = 1, aa;
// cin >> T;aa=T;
while(T--){
// cout << "Case #" << aa-T << ": ";
solve();
}
return 0;
}
Compilation message
events.cpp: In function 'int main()':
events.cpp:113:16: warning: unused variable 'aa' [-Wunused-variable]
113 | int T = 1, aa;
| ^~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
6488 KB |
Output is correct |
2 |
Correct |
109 ms |
25624 KB |
Output is correct |
3 |
Correct |
105 ms |
25296 KB |
Output is correct |
4 |
Correct |
110 ms |
25288 KB |
Output is correct |
5 |
Correct |
107 ms |
25548 KB |
Output is correct |
6 |
Correct |
115 ms |
25652 KB |
Output is correct |
7 |
Correct |
110 ms |
25548 KB |
Output is correct |
8 |
Correct |
101 ms |
25808 KB |
Output is correct |
9 |
Correct |
102 ms |
25800 KB |
Output is correct |
10 |
Correct |
110 ms |
25808 KB |
Output is correct |
11 |
Correct |
106 ms |
22988 KB |
Output is correct |
12 |
Correct |
68 ms |
22992 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
6608 KB |
Output is correct |
2 |
Correct |
1 ms |
6492 KB |
Output is correct |
3 |
Correct |
1 ms |
6492 KB |
Output is correct |
4 |
Correct |
2 ms |
6492 KB |
Output is correct |
5 |
Correct |
1 ms |
6488 KB |
Output is correct |
6 |
Correct |
2 ms |
6488 KB |
Output is correct |
7 |
Correct |
2 ms |
6492 KB |
Output is correct |
8 |
Correct |
2 ms |
6492 KB |
Output is correct |
9 |
Correct |
1 ms |
6488 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
6608 KB |
Output is correct |
2 |
Correct |
1 ms |
6492 KB |
Output is correct |
3 |
Correct |
1 ms |
6492 KB |
Output is correct |
4 |
Correct |
2 ms |
6492 KB |
Output is correct |
5 |
Correct |
1 ms |
6488 KB |
Output is correct |
6 |
Correct |
2 ms |
6488 KB |
Output is correct |
7 |
Correct |
2 ms |
6492 KB |
Output is correct |
8 |
Correct |
2 ms |
6492 KB |
Output is correct |
9 |
Correct |
1 ms |
6488 KB |
Output is correct |
10 |
Correct |
1 ms |
6492 KB |
Output is correct |
11 |
Correct |
1 ms |
6492 KB |
Output is correct |
12 |
Correct |
1 ms |
6492 KB |
Output is correct |
13 |
Correct |
2 ms |
6492 KB |
Output is correct |
14 |
Correct |
2 ms |
6492 KB |
Output is correct |
15 |
Correct |
2 ms |
6492 KB |
Output is correct |
16 |
Correct |
2 ms |
6492 KB |
Output is correct |
17 |
Correct |
2 ms |
6628 KB |
Output is correct |
18 |
Correct |
1 ms |
6492 KB |
Output is correct |
19 |
Correct |
27 ms |
8024 KB |
Output is correct |
20 |
Correct |
24 ms |
8284 KB |
Output is correct |
21 |
Correct |
27 ms |
8540 KB |
Output is correct |
22 |
Correct |
20 ms |
8540 KB |
Output is correct |
23 |
Correct |
20 ms |
8348 KB |
Output is correct |
24 |
Correct |
18 ms |
8284 KB |
Output is correct |
25 |
Correct |
23 ms |
8028 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
6608 KB |
Output is correct |
2 |
Correct |
1 ms |
6492 KB |
Output is correct |
3 |
Correct |
1 ms |
6492 KB |
Output is correct |
4 |
Correct |
2 ms |
6492 KB |
Output is correct |
5 |
Correct |
1 ms |
6488 KB |
Output is correct |
6 |
Correct |
2 ms |
6488 KB |
Output is correct |
7 |
Correct |
2 ms |
6492 KB |
Output is correct |
8 |
Correct |
2 ms |
6492 KB |
Output is correct |
9 |
Correct |
1 ms |
6488 KB |
Output is correct |
10 |
Correct |
1 ms |
6624 KB |
Output is correct |
11 |
Correct |
1 ms |
6492 KB |
Output is correct |
12 |
Correct |
2 ms |
6492 KB |
Output is correct |
13 |
Correct |
2 ms |
6492 KB |
Output is correct |
14 |
Correct |
1 ms |
6492 KB |
Output is correct |
15 |
Correct |
2 ms |
6492 KB |
Output is correct |
16 |
Correct |
2 ms |
6492 KB |
Output is correct |
17 |
Correct |
2 ms |
6492 KB |
Output is correct |
18 |
Correct |
1 ms |
6492 KB |
Output is correct |
19 |
Correct |
89 ms |
24012 KB |
Output is correct |
20 |
Correct |
84 ms |
23204 KB |
Output is correct |
21 |
Correct |
95 ms |
24284 KB |
Output is correct |
22 |
Correct |
86 ms |
23992 KB |
Output is correct |
23 |
Correct |
97 ms |
24000 KB |
Output is correct |
24 |
Correct |
99 ms |
23760 KB |
Output is correct |
25 |
Correct |
50 ms |
23248 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
112 ms |
25296 KB |
Output is correct |
2 |
Correct |
105 ms |
25480 KB |
Output is correct |
3 |
Correct |
112 ms |
25224 KB |
Output is correct |
4 |
Correct |
103 ms |
26008 KB |
Output is correct |
5 |
Correct |
111 ms |
25736 KB |
Output is correct |
6 |
Correct |
121 ms |
22736 KB |
Output is correct |
7 |
Correct |
131 ms |
22728 KB |
Output is correct |
8 |
Correct |
118 ms |
22732 KB |
Output is correct |
9 |
Correct |
97 ms |
21904 KB |
Output is correct |
10 |
Correct |
114 ms |
22220 KB |
Output is correct |
11 |
Correct |
118 ms |
22220 KB |
Output is correct |
12 |
Correct |
114 ms |
22392 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
6488 KB |
Output is correct |
2 |
Correct |
109 ms |
25624 KB |
Output is correct |
3 |
Correct |
105 ms |
25296 KB |
Output is correct |
4 |
Correct |
110 ms |
25288 KB |
Output is correct |
5 |
Correct |
107 ms |
25548 KB |
Output is correct |
6 |
Correct |
115 ms |
25652 KB |
Output is correct |
7 |
Correct |
110 ms |
25548 KB |
Output is correct |
8 |
Correct |
101 ms |
25808 KB |
Output is correct |
9 |
Correct |
102 ms |
25800 KB |
Output is correct |
10 |
Correct |
110 ms |
25808 KB |
Output is correct |
11 |
Correct |
106 ms |
22988 KB |
Output is correct |
12 |
Correct |
68 ms |
22992 KB |
Output is correct |
13 |
Correct |
1 ms |
6608 KB |
Output is correct |
14 |
Correct |
1 ms |
6492 KB |
Output is correct |
15 |
Correct |
1 ms |
6492 KB |
Output is correct |
16 |
Correct |
2 ms |
6492 KB |
Output is correct |
17 |
Correct |
1 ms |
6488 KB |
Output is correct |
18 |
Correct |
2 ms |
6488 KB |
Output is correct |
19 |
Correct |
2 ms |
6492 KB |
Output is correct |
20 |
Correct |
2 ms |
6492 KB |
Output is correct |
21 |
Correct |
1 ms |
6488 KB |
Output is correct |
22 |
Correct |
1 ms |
6492 KB |
Output is correct |
23 |
Correct |
1 ms |
6492 KB |
Output is correct |
24 |
Correct |
1 ms |
6492 KB |
Output is correct |
25 |
Correct |
2 ms |
6492 KB |
Output is correct |
26 |
Correct |
2 ms |
6492 KB |
Output is correct |
27 |
Correct |
2 ms |
6492 KB |
Output is correct |
28 |
Correct |
2 ms |
6492 KB |
Output is correct |
29 |
Correct |
2 ms |
6628 KB |
Output is correct |
30 |
Correct |
1 ms |
6492 KB |
Output is correct |
31 |
Correct |
27 ms |
8024 KB |
Output is correct |
32 |
Correct |
24 ms |
8284 KB |
Output is correct |
33 |
Correct |
27 ms |
8540 KB |
Output is correct |
34 |
Correct |
20 ms |
8540 KB |
Output is correct |
35 |
Correct |
20 ms |
8348 KB |
Output is correct |
36 |
Correct |
18 ms |
8284 KB |
Output is correct |
37 |
Correct |
23 ms |
8028 KB |
Output is correct |
38 |
Correct |
1 ms |
6624 KB |
Output is correct |
39 |
Correct |
1 ms |
6492 KB |
Output is correct |
40 |
Correct |
2 ms |
6492 KB |
Output is correct |
41 |
Correct |
2 ms |
6492 KB |
Output is correct |
42 |
Correct |
1 ms |
6492 KB |
Output is correct |
43 |
Correct |
2 ms |
6492 KB |
Output is correct |
44 |
Correct |
2 ms |
6492 KB |
Output is correct |
45 |
Correct |
2 ms |
6492 KB |
Output is correct |
46 |
Correct |
1 ms |
6492 KB |
Output is correct |
47 |
Correct |
89 ms |
24012 KB |
Output is correct |
48 |
Correct |
84 ms |
23204 KB |
Output is correct |
49 |
Correct |
95 ms |
24284 KB |
Output is correct |
50 |
Correct |
86 ms |
23992 KB |
Output is correct |
51 |
Correct |
97 ms |
24000 KB |
Output is correct |
52 |
Correct |
99 ms |
23760 KB |
Output is correct |
53 |
Correct |
50 ms |
23248 KB |
Output is correct |
54 |
Correct |
112 ms |
25296 KB |
Output is correct |
55 |
Correct |
105 ms |
25480 KB |
Output is correct |
56 |
Correct |
112 ms |
25224 KB |
Output is correct |
57 |
Correct |
103 ms |
26008 KB |
Output is correct |
58 |
Correct |
111 ms |
25736 KB |
Output is correct |
59 |
Correct |
121 ms |
22736 KB |
Output is correct |
60 |
Correct |
131 ms |
22728 KB |
Output is correct |
61 |
Correct |
118 ms |
22732 KB |
Output is correct |
62 |
Correct |
97 ms |
21904 KB |
Output is correct |
63 |
Correct |
114 ms |
22220 KB |
Output is correct |
64 |
Correct |
118 ms |
22220 KB |
Output is correct |
65 |
Correct |
114 ms |
22392 KB |
Output is correct |
66 |
Correct |
1 ms |
6488 KB |
Output is correct |
67 |
Correct |
105 ms |
25552 KB |
Output is correct |
68 |
Correct |
105 ms |
25548 KB |
Output is correct |
69 |
Correct |
118 ms |
25756 KB |
Output is correct |
70 |
Correct |
107 ms |
25740 KB |
Output is correct |
71 |
Correct |
106 ms |
25804 KB |
Output is correct |
72 |
Correct |
107 ms |
25804 KB |
Output is correct |
73 |
Correct |
100 ms |
26060 KB |
Output is correct |
74 |
Correct |
101 ms |
26248 KB |
Output is correct |
75 |
Correct |
109 ms |
26056 KB |
Output is correct |
76 |
Correct |
107 ms |
26064 KB |
Output is correct |
77 |
Correct |
68 ms |
25500 KB |
Output is correct |
78 |
Correct |
1 ms |
6492 KB |
Output is correct |
79 |
Correct |
2 ms |
6492 KB |
Output is correct |
80 |
Correct |
1 ms |
6620 KB |
Output is correct |
81 |
Correct |
1 ms |
6492 KB |
Output is correct |
82 |
Correct |
2 ms |
6492 KB |
Output is correct |
83 |
Correct |
2 ms |
6492 KB |
Output is correct |
84 |
Correct |
2 ms |
6492 KB |
Output is correct |
85 |
Correct |
1 ms |
6492 KB |
Output is correct |
86 |
Correct |
28 ms |
8152 KB |
Output is correct |
87 |
Correct |
24 ms |
8288 KB |
Output is correct |
88 |
Correct |
27 ms |
8720 KB |
Output is correct |
89 |
Correct |
20 ms |
8532 KB |
Output is correct |
90 |
Correct |
20 ms |
8276 KB |
Output is correct |
91 |
Correct |
19 ms |
8284 KB |
Output is correct |
92 |
Correct |
24 ms |
8276 KB |
Output is correct |
93 |
Correct |
86 ms |
24004 KB |
Output is correct |
94 |
Correct |
91 ms |
23480 KB |
Output is correct |
95 |
Correct |
96 ms |
23892 KB |
Output is correct |
96 |
Correct |
86 ms |
24012 KB |
Output is correct |
97 |
Correct |
98 ms |
23804 KB |
Output is correct |
98 |
Correct |
95 ms |
24008 KB |
Output is correct |
99 |
Correct |
51 ms |
23244 KB |
Output is correct |
100 |
Correct |
120 ms |
25808 KB |
Output is correct |
101 |
Correct |
120 ms |
25808 KB |
Output is correct |
102 |
Correct |
115 ms |
25996 KB |
Output is correct |
103 |
Correct |
117 ms |
25496 KB |
Output is correct |
104 |
Correct |
121 ms |
25292 KB |
Output is correct |
105 |
Correct |
115 ms |
25296 KB |
Output is correct |
106 |
Correct |
114 ms |
25292 KB |
Output is correct |
107 |
Correct |
112 ms |
25028 KB |
Output is correct |
108 |
Correct |
109 ms |
25888 KB |
Output is correct |
109 |
Correct |
112 ms |
25884 KB |
Output is correct |
110 |
Correct |
123 ms |
25544 KB |
Output is correct |
111 |
Correct |
125 ms |
25548 KB |
Output is correct |