#include <bits/stdc++.h>
using namespace std;
typedef pair<int,int> ii;
typedef pair<int,ii> iii;
const int maxx = 1e9+5;
const int LOG = 20;
vector<ii> tree;
void update(int pos, int i, int f, int idx, int val, int id){
if(i == f){
tree[pos] =min(tree[pos], {val,id});
return;
}
int meio = (i+f)/2;
if(idx <= meio) update(2*pos+1, i, meio, idx, val, id);
else update(2*pos+2, meio + 1, f, idx, val, id);
tree[pos] = min(tree[2*pos+1], tree[2*pos+2]);
return;
}
ii query(int pos, int i, int f, int l, int r){
if(l<=i and f<=r) return tree[pos];
int meio = (i+f)/2;
ii aa = {maxx, -1};
if(l <=meio) aa = min(aa, query(2*pos+1, i, meio, l, r));
if( r > meio) aa = min(aa, query(2*pos+2, meio + 1, f, l, r));
return aa;
}
int main(){
int n,q; cin >> n >> q;
vector<ii> v(n+2);
vector<iii> event;
tree.resize(8*n, {maxx, -1});
set<int> S;
for(int i=1; i<=n; i++){
int s,e; cin >> s >> e;
v[i] = {s,e};
S.insert(s);
S.insert(e);
}
map<int,int> COMP;
for(auto i: S){
COMP[i] = COMP.size();
}
for(auto &[a,b]: v) {a = COMP[a]; b = COMP[b];}
for(int i=1; i<=n; i++){ event.push_back({v[i].first, {0,i}}); event.push_back({v[i].second, {1,i}}); update(0,0,COMP.size(), v[i].second, v[i].first, i);}
sort(event.begin(), event.end());
set<ii> aux;
vector sparse1(n+2, vector (LOG,0));
for(auto [t,b]: event){
auto [type,idx] = b;
if(!type) aux.insert({v[idx].second, idx});
else{
auto [maior, i] = *prev(aux.end());
sparse1[idx][0] = i;
aux.erase({t, idx});
}
}
for(int i=1; i<LOG; i++) for(int j = 1; j<=n; j++){
sparse1[j][i] = sparse1[sparse1[j][i-1]][i-1];
}
vector sparse2(n+2, vector (LOG,0));
for(int i=1; i<=n; i++){
sparse2[i][0] = query(0,0,COMP.size(), v[i].first, v[i].second).second;
}
for(int i=1; i<LOG; i++) for(int j = 1; j<=n; j++){
sparse2[j][i] = sparse2[sparse2[j][i-1]][i-1];
}
while(q--){
int s,e; cin >> s >> e;
if(s==e){cout << 0 << endl; continue;}
if(v[e].first <= v[s].second and v[s].second <= v[e].second){cout << 1 << endl; continue;}
int ss = s, ee = e;
int qnt = 0;
for(int i = LOG - 1; i>=0; i--){
int idx = sparse1[ss][i];
if(v[idx].second >= v[e].first) continue;
ss = idx;
qnt += (1<<i);
}
for(int i = LOG - 1; i>=0; i--){
int idx = sparse2[ee][i];
if(v[idx].first > v[ss].second){
ee = idx;
qnt += (1<<i);
}
}
ee = sparse2[ee][0];
qnt++;
if(v[ee].first <= v[ss].second and v[ee].second >= v[ss].second) cout << qnt+1 << endl;
else cout << "impossible\n";
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
390 ms |
43468 KB |
Output is correct |
3 |
Correct |
402 ms |
43204 KB |
Output is correct |
4 |
Correct |
503 ms |
43972 KB |
Output is correct |
5 |
Correct |
432 ms |
47208 KB |
Output is correct |
6 |
Correct |
424 ms |
48364 KB |
Output is correct |
7 |
Correct |
425 ms |
48404 KB |
Output is correct |
8 |
Correct |
451 ms |
56092 KB |
Output is correct |
9 |
Correct |
430 ms |
56260 KB |
Output is correct |
10 |
Correct |
434 ms |
46584 KB |
Output is correct |
11 |
Correct |
446 ms |
50540 KB |
Output is correct |
12 |
Correct |
304 ms |
46148 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
344 KB |
Output is correct |
3 |
Correct |
2 ms |
860 KB |
Output is correct |
4 |
Correct |
2 ms |
860 KB |
Output is correct |
5 |
Correct |
2 ms |
860 KB |
Output is correct |
6 |
Correct |
2 ms |
860 KB |
Output is correct |
7 |
Correct |
3 ms |
860 KB |
Output is correct |
8 |
Correct |
2 ms |
860 KB |
Output is correct |
9 |
Correct |
1 ms |
604 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
344 KB |
Output is correct |
3 |
Correct |
2 ms |
860 KB |
Output is correct |
4 |
Correct |
2 ms |
860 KB |
Output is correct |
5 |
Correct |
2 ms |
860 KB |
Output is correct |
6 |
Correct |
2 ms |
860 KB |
Output is correct |
7 |
Correct |
3 ms |
860 KB |
Output is correct |
8 |
Correct |
2 ms |
860 KB |
Output is correct |
9 |
Correct |
1 ms |
604 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
2 ms |
756 KB |
Output is correct |
13 |
Correct |
2 ms |
860 KB |
Output is correct |
14 |
Correct |
2 ms |
860 KB |
Output is correct |
15 |
Correct |
3 ms |
860 KB |
Output is correct |
16 |
Correct |
2 ms |
860 KB |
Output is correct |
17 |
Correct |
2 ms |
860 KB |
Output is correct |
18 |
Correct |
2 ms |
924 KB |
Output is correct |
19 |
Correct |
172 ms |
3852 KB |
Output is correct |
20 |
Correct |
171 ms |
3904 KB |
Output is correct |
21 |
Correct |
173 ms |
4116 KB |
Output is correct |
22 |
Correct |
165 ms |
4380 KB |
Output is correct |
23 |
Correct |
168 ms |
4648 KB |
Output is correct |
24 |
Correct |
185 ms |
4556 KB |
Output is correct |
25 |
Correct |
165 ms |
4200 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
344 KB |
Output is correct |
3 |
Correct |
2 ms |
860 KB |
Output is correct |
4 |
Correct |
2 ms |
860 KB |
Output is correct |
5 |
Correct |
2 ms |
860 KB |
Output is correct |
6 |
Correct |
2 ms |
860 KB |
Output is correct |
7 |
Correct |
3 ms |
860 KB |
Output is correct |
8 |
Correct |
2 ms |
860 KB |
Output is correct |
9 |
Correct |
1 ms |
604 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
2 ms |
860 KB |
Output is correct |
13 |
Correct |
2 ms |
604 KB |
Output is correct |
14 |
Correct |
2 ms |
860 KB |
Output is correct |
15 |
Correct |
2 ms |
860 KB |
Output is correct |
16 |
Correct |
2 ms |
860 KB |
Output is correct |
17 |
Correct |
2 ms |
860 KB |
Output is correct |
18 |
Correct |
1 ms |
604 KB |
Output is correct |
19 |
Correct |
218 ms |
42576 KB |
Output is correct |
20 |
Correct |
204 ms |
43964 KB |
Output is correct |
21 |
Correct |
234 ms |
44648 KB |
Output is correct |
22 |
Correct |
261 ms |
48160 KB |
Output is correct |
23 |
Correct |
298 ms |
53956 KB |
Output is correct |
24 |
Correct |
296 ms |
53952 KB |
Output is correct |
25 |
Correct |
114 ms |
35172 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
391 ms |
43200 KB |
Output is correct |
2 |
Correct |
464 ms |
43392 KB |
Output is correct |
3 |
Correct |
444 ms |
43260 KB |
Output is correct |
4 |
Correct |
412 ms |
56144 KB |
Output is correct |
5 |
Correct |
441 ms |
46780 KB |
Output is correct |
6 |
Correct |
506 ms |
55904 KB |
Output is correct |
7 |
Correct |
512 ms |
55812 KB |
Output is correct |
8 |
Correct |
490 ms |
56024 KB |
Output is correct |
9 |
Correct |
348 ms |
54424 KB |
Output is correct |
10 |
Correct |
478 ms |
55568 KB |
Output is correct |
11 |
Correct |
507 ms |
55176 KB |
Output is correct |
12 |
Correct |
487 ms |
55828 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
390 ms |
43468 KB |
Output is correct |
3 |
Correct |
402 ms |
43204 KB |
Output is correct |
4 |
Correct |
503 ms |
43972 KB |
Output is correct |
5 |
Correct |
432 ms |
47208 KB |
Output is correct |
6 |
Correct |
424 ms |
48364 KB |
Output is correct |
7 |
Correct |
425 ms |
48404 KB |
Output is correct |
8 |
Correct |
451 ms |
56092 KB |
Output is correct |
9 |
Correct |
430 ms |
56260 KB |
Output is correct |
10 |
Correct |
434 ms |
46584 KB |
Output is correct |
11 |
Correct |
446 ms |
50540 KB |
Output is correct |
12 |
Correct |
304 ms |
46148 KB |
Output is correct |
13 |
Correct |
0 ms |
344 KB |
Output is correct |
14 |
Correct |
0 ms |
344 KB |
Output is correct |
15 |
Correct |
2 ms |
860 KB |
Output is correct |
16 |
Correct |
2 ms |
860 KB |
Output is correct |
17 |
Correct |
2 ms |
860 KB |
Output is correct |
18 |
Correct |
2 ms |
860 KB |
Output is correct |
19 |
Correct |
3 ms |
860 KB |
Output is correct |
20 |
Correct |
2 ms |
860 KB |
Output is correct |
21 |
Correct |
1 ms |
604 KB |
Output is correct |
22 |
Correct |
0 ms |
348 KB |
Output is correct |
23 |
Correct |
0 ms |
348 KB |
Output is correct |
24 |
Correct |
2 ms |
756 KB |
Output is correct |
25 |
Correct |
2 ms |
860 KB |
Output is correct |
26 |
Correct |
2 ms |
860 KB |
Output is correct |
27 |
Correct |
3 ms |
860 KB |
Output is correct |
28 |
Correct |
2 ms |
860 KB |
Output is correct |
29 |
Correct |
2 ms |
860 KB |
Output is correct |
30 |
Correct |
2 ms |
924 KB |
Output is correct |
31 |
Correct |
172 ms |
3852 KB |
Output is correct |
32 |
Correct |
171 ms |
3904 KB |
Output is correct |
33 |
Correct |
173 ms |
4116 KB |
Output is correct |
34 |
Correct |
165 ms |
4380 KB |
Output is correct |
35 |
Correct |
168 ms |
4648 KB |
Output is correct |
36 |
Correct |
185 ms |
4556 KB |
Output is correct |
37 |
Correct |
165 ms |
4200 KB |
Output is correct |
38 |
Correct |
0 ms |
348 KB |
Output is correct |
39 |
Correct |
0 ms |
348 KB |
Output is correct |
40 |
Correct |
2 ms |
860 KB |
Output is correct |
41 |
Correct |
2 ms |
604 KB |
Output is correct |
42 |
Correct |
2 ms |
860 KB |
Output is correct |
43 |
Correct |
2 ms |
860 KB |
Output is correct |
44 |
Correct |
2 ms |
860 KB |
Output is correct |
45 |
Correct |
2 ms |
860 KB |
Output is correct |
46 |
Correct |
1 ms |
604 KB |
Output is correct |
47 |
Correct |
218 ms |
42576 KB |
Output is correct |
48 |
Correct |
204 ms |
43964 KB |
Output is correct |
49 |
Correct |
234 ms |
44648 KB |
Output is correct |
50 |
Correct |
261 ms |
48160 KB |
Output is correct |
51 |
Correct |
298 ms |
53956 KB |
Output is correct |
52 |
Correct |
296 ms |
53952 KB |
Output is correct |
53 |
Correct |
114 ms |
35172 KB |
Output is correct |
54 |
Correct |
391 ms |
43200 KB |
Output is correct |
55 |
Correct |
464 ms |
43392 KB |
Output is correct |
56 |
Correct |
444 ms |
43260 KB |
Output is correct |
57 |
Correct |
412 ms |
56144 KB |
Output is correct |
58 |
Correct |
441 ms |
46780 KB |
Output is correct |
59 |
Correct |
506 ms |
55904 KB |
Output is correct |
60 |
Correct |
512 ms |
55812 KB |
Output is correct |
61 |
Correct |
490 ms |
56024 KB |
Output is correct |
62 |
Correct |
348 ms |
54424 KB |
Output is correct |
63 |
Correct |
478 ms |
55568 KB |
Output is correct |
64 |
Correct |
507 ms |
55176 KB |
Output is correct |
65 |
Correct |
487 ms |
55828 KB |
Output is correct |
66 |
Correct |
0 ms |
348 KB |
Output is correct |
67 |
Correct |
389 ms |
47204 KB |
Output is correct |
68 |
Correct |
399 ms |
46272 KB |
Output is correct |
69 |
Correct |
472 ms |
46812 KB |
Output is correct |
70 |
Correct |
420 ms |
47296 KB |
Output is correct |
71 |
Correct |
421 ms |
48180 KB |
Output is correct |
72 |
Correct |
419 ms |
48980 KB |
Output is correct |
73 |
Correct |
411 ms |
56252 KB |
Output is correct |
74 |
Correct |
438 ms |
56200 KB |
Output is correct |
75 |
Correct |
416 ms |
46984 KB |
Output is correct |
76 |
Correct |
436 ms |
51340 KB |
Output is correct |
77 |
Correct |
332 ms |
46060 KB |
Output is correct |
78 |
Correct |
0 ms |
344 KB |
Output is correct |
79 |
Correct |
2 ms |
860 KB |
Output is correct |
80 |
Correct |
2 ms |
860 KB |
Output is correct |
81 |
Correct |
2 ms |
860 KB |
Output is correct |
82 |
Correct |
2 ms |
860 KB |
Output is correct |
83 |
Correct |
3 ms |
860 KB |
Output is correct |
84 |
Correct |
2 ms |
836 KB |
Output is correct |
85 |
Correct |
1 ms |
604 KB |
Output is correct |
86 |
Correct |
172 ms |
4184 KB |
Output is correct |
87 |
Correct |
180 ms |
4132 KB |
Output is correct |
88 |
Correct |
171 ms |
4116 KB |
Output is correct |
89 |
Correct |
164 ms |
4372 KB |
Output is correct |
90 |
Correct |
171 ms |
4628 KB |
Output is correct |
91 |
Correct |
166 ms |
4628 KB |
Output is correct |
92 |
Correct |
168 ms |
4200 KB |
Output is correct |
93 |
Correct |
243 ms |
44912 KB |
Output is correct |
94 |
Correct |
197 ms |
43712 KB |
Output is correct |
95 |
Correct |
241 ms |
44428 KB |
Output is correct |
96 |
Correct |
243 ms |
47984 KB |
Output is correct |
97 |
Correct |
306 ms |
53952 KB |
Output is correct |
98 |
Correct |
295 ms |
54196 KB |
Output is correct |
99 |
Correct |
100 ms |
34592 KB |
Output is correct |
100 |
Correct |
485 ms |
56484 KB |
Output is correct |
101 |
Correct |
516 ms |
55944 KB |
Output is correct |
102 |
Correct |
503 ms |
56232 KB |
Output is correct |
103 |
Correct |
470 ms |
55748 KB |
Output is correct |
104 |
Correct |
471 ms |
55420 KB |
Output is correct |
105 |
Correct |
461 ms |
56108 KB |
Output is correct |
106 |
Correct |
412 ms |
43460 KB |
Output is correct |
107 |
Correct |
421 ms |
46020 KB |
Output is correct |
108 |
Correct |
407 ms |
46528 KB |
Output is correct |
109 |
Correct |
443 ms |
46760 KB |
Output is correct |
110 |
Correct |
511 ms |
55684 KB |
Output is correct |
111 |
Correct |
504 ms |
56440 KB |
Output is correct |