#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using pii = pair<int, int>;
const int inf = numeric_limits<int> :: max();
struct ST{
vector<pii> t;
int n;
ST(int ns){
n = ns;
t.assign(4 * n, {inf, 0});
}
pii min(pii x, pii y){
if (x.first < y.first){
return x;
}
return y;
}
void upd(int v, int tl, int tr, int& p, pii& k){
if (tl == tr){
t[v] = min(t[v], k);
return;
}
int tm = (tl + tr) / 2, vv = 2 * v;
if (p <= tm){
upd(vv, tl, tm, p, k);
}
else {
upd(vv + 1, tm + 1, tr, p, k);
}
t[v] = min(t[vv], t[vv + 1]);
}
void upd(int p, pii k){
upd(1, 1, n, p, k);
}
pii get(int v, int tl, int tr, int& l, int& r){
if (l > tr || r < tl) return {inf, 0};
if (l <= tl && tr <= r) return t[v];
int tm = (tl + tr) / 2, vv = 2 * v;
return min(get(vv, tl, tm, l, r), get(vv + 1, tm + 1, tr, l, r));
}
pii get(int l, int r){
return get(1, 1, n, l, r);
}
};
int main(){
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int n, q; cin>>n>>q;
vector<int> l(n + 1), r(n + 1);
for (int i = 1; i <= n; i++){
cin>>l[i]>>r[i];
}
vector<int> all;
for (int i = 1; i <= n; i++) all.push_back(l[i]);
for (int i = 1; i <= n; i++) all.push_back(r[i]);
sort(all.begin(), all.end());
int i = 0, cnt = 0;
map<int, int> mp;
while (i < all.size()){
int j = i;
while (j < all.size() && all[i] == all[j]){
j++;
}
mp[all[i]] = ++cnt;
i = j;
}
for (int i = 1; i <= n; i++){
l[i] = mp[l[i]];
r[i] = mp[r[i]];
}
ST T(cnt);
for (int i = 1; i <= n; i++){
T.upd(r[i], {l[i], i});
}
const int lg = (int) ceil(log2(n));
vector<vector<int>> sp(n + 1, vector<int>(lg + 1));
for (int i = 1; i <= n; i++){
sp[i][0] = (T.get(l[i], r[i])).second;
}
for (int k = 1; k <= lg; k++){
for (int i = 1; i <= n; i++){
sp[i][k] = sp[sp[i][k - 1]][k - 1];
}
}
function<void(int, int)> solve = [&](int a, int b){
if (a == b){
cout<<0<<"\n";
return;
}
if (l[b] <= r[a] && r[a] <= r[b]){
cout<<1<<"\n";
return;
}
if (r[b] < r[a] || l[sp[b][lg]] > r[a]){
cout<<"impossible"<<"\n";
return;
}
int out = 1;
for (int i = lg; i >= 0; i--){
if (r[a] < l[sp[b][i]]){
b = sp[b][i];
out += (1 << i);
}
}
out += (a != b);
cout<<out<<"\n";
};
while (q--){
int s, f; cin>>s>>f;
solve(s, f);
}
}
Compilation message
events.cpp: In function 'int main()':
events.cpp:65:14: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
65 | while (i < all.size()){
| ~~^~~~~~~~~~~~
events.cpp:67:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
67 | while (j < all.size() && all[i] == all[j]){
| ~~^~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
136 ms |
20640 KB |
Output is correct |
3 |
Correct |
170 ms |
20576 KB |
Output is correct |
4 |
Correct |
165 ms |
20432 KB |
Output is correct |
5 |
Correct |
167 ms |
21304 KB |
Output is correct |
6 |
Correct |
160 ms |
21964 KB |
Output is correct |
7 |
Correct |
163 ms |
22476 KB |
Output is correct |
8 |
Correct |
159 ms |
28880 KB |
Output is correct |
9 |
Correct |
149 ms |
28904 KB |
Output is correct |
10 |
Correct |
165 ms |
21104 KB |
Output is correct |
11 |
Correct |
158 ms |
23984 KB |
Output is correct |
12 |
Correct |
100 ms |
21048 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
344 KB |
Output is correct |
3 |
Correct |
1 ms |
604 KB |
Output is correct |
4 |
Correct |
1 ms |
604 KB |
Output is correct |
5 |
Correct |
1 ms |
600 KB |
Output is correct |
6 |
Correct |
1 ms |
604 KB |
Output is correct |
7 |
Correct |
1 ms |
604 KB |
Output is correct |
8 |
Correct |
1 ms |
572 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
344 KB |
Output is correct |
3 |
Correct |
1 ms |
604 KB |
Output is correct |
4 |
Correct |
1 ms |
604 KB |
Output is correct |
5 |
Correct |
1 ms |
600 KB |
Output is correct |
6 |
Correct |
1 ms |
604 KB |
Output is correct |
7 |
Correct |
1 ms |
604 KB |
Output is correct |
8 |
Correct |
1 ms |
572 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
344 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
2 ms |
856 KB |
Output is correct |
13 |
Correct |
1 ms |
600 KB |
Output is correct |
14 |
Correct |
1 ms |
604 KB |
Output is correct |
15 |
Correct |
1 ms |
604 KB |
Output is correct |
16 |
Correct |
1 ms |
604 KB |
Output is correct |
17 |
Correct |
1 ms |
604 KB |
Output is correct |
18 |
Correct |
1 ms |
344 KB |
Output is correct |
19 |
Correct |
29 ms |
1848 KB |
Output is correct |
20 |
Correct |
27 ms |
1784 KB |
Output is correct |
21 |
Correct |
29 ms |
2140 KB |
Output is correct |
22 |
Correct |
22 ms |
2140 KB |
Output is correct |
23 |
Correct |
23 ms |
2648 KB |
Output is correct |
24 |
Correct |
22 ms |
2596 KB |
Output is correct |
25 |
Correct |
27 ms |
2024 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
344 KB |
Output is correct |
3 |
Correct |
1 ms |
604 KB |
Output is correct |
4 |
Correct |
1 ms |
604 KB |
Output is correct |
5 |
Correct |
1 ms |
600 KB |
Output is correct |
6 |
Correct |
1 ms |
604 KB |
Output is correct |
7 |
Correct |
1 ms |
604 KB |
Output is correct |
8 |
Correct |
1 ms |
572 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
1 ms |
348 KB |
Output is correct |
12 |
Correct |
1 ms |
604 KB |
Output is correct |
13 |
Correct |
1 ms |
604 KB |
Output is correct |
14 |
Correct |
1 ms |
604 KB |
Output is correct |
15 |
Correct |
1 ms |
604 KB |
Output is correct |
16 |
Correct |
1 ms |
604 KB |
Output is correct |
17 |
Correct |
1 ms |
604 KB |
Output is correct |
18 |
Correct |
1 ms |
348 KB |
Output is correct |
19 |
Correct |
125 ms |
19800 KB |
Output is correct |
20 |
Correct |
118 ms |
20176 KB |
Output is correct |
21 |
Correct |
129 ms |
19804 KB |
Output is correct |
22 |
Correct |
127 ms |
22732 KB |
Output is correct |
23 |
Correct |
168 ms |
27792 KB |
Output is correct |
24 |
Correct |
151 ms |
27856 KB |
Output is correct |
25 |
Correct |
26 ms |
12244 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
136 ms |
20536 KB |
Output is correct |
2 |
Correct |
151 ms |
20536 KB |
Output is correct |
3 |
Correct |
168 ms |
20432 KB |
Output is correct |
4 |
Correct |
154 ms |
28784 KB |
Output is correct |
5 |
Correct |
147 ms |
20796 KB |
Output is correct |
6 |
Correct |
188 ms |
28368 KB |
Output is correct |
7 |
Correct |
188 ms |
28372 KB |
Output is correct |
8 |
Correct |
210 ms |
28896 KB |
Output is correct |
9 |
Correct |
159 ms |
27600 KB |
Output is correct |
10 |
Correct |
187 ms |
28108 KB |
Output is correct |
11 |
Correct |
186 ms |
27856 KB |
Output is correct |
12 |
Correct |
188 ms |
28112 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
136 ms |
20640 KB |
Output is correct |
3 |
Correct |
170 ms |
20576 KB |
Output is correct |
4 |
Correct |
165 ms |
20432 KB |
Output is correct |
5 |
Correct |
167 ms |
21304 KB |
Output is correct |
6 |
Correct |
160 ms |
21964 KB |
Output is correct |
7 |
Correct |
163 ms |
22476 KB |
Output is correct |
8 |
Correct |
159 ms |
28880 KB |
Output is correct |
9 |
Correct |
149 ms |
28904 KB |
Output is correct |
10 |
Correct |
165 ms |
21104 KB |
Output is correct |
11 |
Correct |
158 ms |
23984 KB |
Output is correct |
12 |
Correct |
100 ms |
21048 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
344 KB |
Output is correct |
15 |
Correct |
1 ms |
604 KB |
Output is correct |
16 |
Correct |
1 ms |
604 KB |
Output is correct |
17 |
Correct |
1 ms |
600 KB |
Output is correct |
18 |
Correct |
1 ms |
604 KB |
Output is correct |
19 |
Correct |
1 ms |
604 KB |
Output is correct |
20 |
Correct |
1 ms |
572 KB |
Output is correct |
21 |
Correct |
1 ms |
348 KB |
Output is correct |
22 |
Correct |
0 ms |
344 KB |
Output is correct |
23 |
Correct |
0 ms |
348 KB |
Output is correct |
24 |
Correct |
2 ms |
856 KB |
Output is correct |
25 |
Correct |
1 ms |
600 KB |
Output is correct |
26 |
Correct |
1 ms |
604 KB |
Output is correct |
27 |
Correct |
1 ms |
604 KB |
Output is correct |
28 |
Correct |
1 ms |
604 KB |
Output is correct |
29 |
Correct |
1 ms |
604 KB |
Output is correct |
30 |
Correct |
1 ms |
344 KB |
Output is correct |
31 |
Correct |
29 ms |
1848 KB |
Output is correct |
32 |
Correct |
27 ms |
1784 KB |
Output is correct |
33 |
Correct |
29 ms |
2140 KB |
Output is correct |
34 |
Correct |
22 ms |
2140 KB |
Output is correct |
35 |
Correct |
23 ms |
2648 KB |
Output is correct |
36 |
Correct |
22 ms |
2596 KB |
Output is correct |
37 |
Correct |
27 ms |
2024 KB |
Output is correct |
38 |
Correct |
0 ms |
348 KB |
Output is correct |
39 |
Correct |
1 ms |
348 KB |
Output is correct |
40 |
Correct |
1 ms |
604 KB |
Output is correct |
41 |
Correct |
1 ms |
604 KB |
Output is correct |
42 |
Correct |
1 ms |
604 KB |
Output is correct |
43 |
Correct |
1 ms |
604 KB |
Output is correct |
44 |
Correct |
1 ms |
604 KB |
Output is correct |
45 |
Correct |
1 ms |
604 KB |
Output is correct |
46 |
Correct |
1 ms |
348 KB |
Output is correct |
47 |
Correct |
125 ms |
19800 KB |
Output is correct |
48 |
Correct |
118 ms |
20176 KB |
Output is correct |
49 |
Correct |
129 ms |
19804 KB |
Output is correct |
50 |
Correct |
127 ms |
22732 KB |
Output is correct |
51 |
Correct |
168 ms |
27792 KB |
Output is correct |
52 |
Correct |
151 ms |
27856 KB |
Output is correct |
53 |
Correct |
26 ms |
12244 KB |
Output is correct |
54 |
Correct |
136 ms |
20536 KB |
Output is correct |
55 |
Correct |
151 ms |
20536 KB |
Output is correct |
56 |
Correct |
168 ms |
20432 KB |
Output is correct |
57 |
Correct |
154 ms |
28784 KB |
Output is correct |
58 |
Correct |
147 ms |
20796 KB |
Output is correct |
59 |
Correct |
188 ms |
28368 KB |
Output is correct |
60 |
Correct |
188 ms |
28372 KB |
Output is correct |
61 |
Correct |
210 ms |
28896 KB |
Output is correct |
62 |
Correct |
159 ms |
27600 KB |
Output is correct |
63 |
Correct |
187 ms |
28108 KB |
Output is correct |
64 |
Correct |
186 ms |
27856 KB |
Output is correct |
65 |
Correct |
188 ms |
28112 KB |
Output is correct |
66 |
Correct |
0 ms |
348 KB |
Output is correct |
67 |
Correct |
139 ms |
20592 KB |
Output is correct |
68 |
Correct |
153 ms |
20544 KB |
Output is correct |
69 |
Correct |
170 ms |
20428 KB |
Output is correct |
70 |
Correct |
181 ms |
21452 KB |
Output is correct |
71 |
Correct |
169 ms |
22116 KB |
Output is correct |
72 |
Correct |
193 ms |
22476 KB |
Output is correct |
73 |
Correct |
150 ms |
28876 KB |
Output is correct |
74 |
Correct |
160 ms |
29068 KB |
Output is correct |
75 |
Correct |
147 ms |
20948 KB |
Output is correct |
76 |
Correct |
150 ms |
23896 KB |
Output is correct |
77 |
Correct |
96 ms |
20944 KB |
Output is correct |
78 |
Correct |
1 ms |
348 KB |
Output is correct |
79 |
Correct |
1 ms |
600 KB |
Output is correct |
80 |
Correct |
1 ms |
604 KB |
Output is correct |
81 |
Correct |
1 ms |
604 KB |
Output is correct |
82 |
Correct |
1 ms |
604 KB |
Output is correct |
83 |
Correct |
1 ms |
604 KB |
Output is correct |
84 |
Correct |
1 ms |
604 KB |
Output is correct |
85 |
Correct |
1 ms |
348 KB |
Output is correct |
86 |
Correct |
35 ms |
1824 KB |
Output is correct |
87 |
Correct |
41 ms |
1808 KB |
Output is correct |
88 |
Correct |
30 ms |
2140 KB |
Output is correct |
89 |
Correct |
24 ms |
2140 KB |
Output is correct |
90 |
Correct |
22 ms |
2172 KB |
Output is correct |
91 |
Correct |
22 ms |
2396 KB |
Output is correct |
92 |
Correct |
26 ms |
1968 KB |
Output is correct |
93 |
Correct |
117 ms |
20020 KB |
Output is correct |
94 |
Correct |
119 ms |
19920 KB |
Output is correct |
95 |
Correct |
124 ms |
19920 KB |
Output is correct |
96 |
Correct |
136 ms |
23032 KB |
Output is correct |
97 |
Correct |
163 ms |
27736 KB |
Output is correct |
98 |
Correct |
175 ms |
27852 KB |
Output is correct |
99 |
Correct |
26 ms |
11984 KB |
Output is correct |
100 |
Correct |
183 ms |
28364 KB |
Output is correct |
101 |
Correct |
185 ms |
28368 KB |
Output is correct |
102 |
Correct |
203 ms |
28620 KB |
Output is correct |
103 |
Correct |
193 ms |
28104 KB |
Output is correct |
104 |
Correct |
193 ms |
27980 KB |
Output is correct |
105 |
Correct |
193 ms |
28024 KB |
Output is correct |
106 |
Correct |
137 ms |
18764 KB |
Output is correct |
107 |
Correct |
159 ms |
20832 KB |
Output is correct |
108 |
Correct |
154 ms |
20808 KB |
Output is correct |
109 |
Correct |
146 ms |
20940 KB |
Output is correct |
110 |
Correct |
191 ms |
28328 KB |
Output is correct |
111 |
Correct |
191 ms |
28268 KB |
Output is correct |