// CODIGO DA ESTELA!!!!! (eu ajudei talvez)
#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 && 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";
}
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
389 ms |
43176 KB |
Output is correct |
3 |
Correct |
419 ms |
43780 KB |
Output is correct |
4 |
Correct |
450 ms |
43204 KB |
Output is correct |
5 |
Correct |
430 ms |
44576 KB |
Output is correct |
6 |
Correct |
422 ms |
45012 KB |
Output is correct |
7 |
Correct |
417 ms |
45456 KB |
Output is correct |
8 |
Correct |
411 ms |
53184 KB |
Output is correct |
9 |
Correct |
414 ms |
53188 KB |
Output is correct |
10 |
Correct |
425 ms |
44484 KB |
Output is correct |
11 |
Correct |
463 ms |
47444 KB |
Output is correct |
12 |
Correct |
304 ms |
43716 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
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 |
856 KB |
Output is correct |
7 |
Correct |
2 ms |
860 KB |
Output is correct |
8 |
Correct |
2 ms |
860 KB |
Output is correct |
9 |
Correct |
1 ms |
604 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
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 |
856 KB |
Output is correct |
7 |
Correct |
2 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 |
788 KB |
Output is correct |
14 |
Correct |
2 ms |
604 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 |
174 ms |
3076 KB |
Output is correct |
20 |
Correct |
170 ms |
2856 KB |
Output is correct |
21 |
Correct |
171 ms |
3216 KB |
Output is correct |
22 |
Correct |
163 ms |
3368 KB |
Output is correct |
23 |
Correct |
164 ms |
3608 KB |
Output is correct |
24 |
Correct |
172 ms |
3880 KB |
Output is correct |
25 |
Correct |
166 ms |
3132 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
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 |
856 KB |
Output is correct |
7 |
Correct |
2 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 |
604 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 |
232 ms |
43440 KB |
Output is correct |
20 |
Correct |
206 ms |
43204 KB |
Output is correct |
21 |
Correct |
241 ms |
42680 KB |
Output is correct |
22 |
Correct |
246 ms |
45968 KB |
Output is correct |
23 |
Correct |
295 ms |
51900 KB |
Output is correct |
24 |
Correct |
296 ms |
52172 KB |
Output is correct |
25 |
Correct |
109 ms |
33220 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
389 ms |
43176 KB |
Output is correct |
2 |
Correct |
407 ms |
43156 KB |
Output is correct |
3 |
Correct |
463 ms |
44196 KB |
Output is correct |
4 |
Correct |
408 ms |
53180 KB |
Output is correct |
5 |
Correct |
450 ms |
43948 KB |
Output is correct |
6 |
Correct |
487 ms |
52676 KB |
Output is correct |
7 |
Correct |
501 ms |
52616 KB |
Output is correct |
8 |
Correct |
482 ms |
53696 KB |
Output is correct |
9 |
Correct |
302 ms |
52052 KB |
Output is correct |
10 |
Correct |
481 ms |
52676 KB |
Output is correct |
11 |
Correct |
458 ms |
52708 KB |
Output is correct |
12 |
Correct |
480 ms |
52528 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
389 ms |
43176 KB |
Output is correct |
3 |
Correct |
419 ms |
43780 KB |
Output is correct |
4 |
Correct |
450 ms |
43204 KB |
Output is correct |
5 |
Correct |
430 ms |
44576 KB |
Output is correct |
6 |
Correct |
422 ms |
45012 KB |
Output is correct |
7 |
Correct |
417 ms |
45456 KB |
Output is correct |
8 |
Correct |
411 ms |
53184 KB |
Output is correct |
9 |
Correct |
414 ms |
53188 KB |
Output is correct |
10 |
Correct |
425 ms |
44484 KB |
Output is correct |
11 |
Correct |
463 ms |
47444 KB |
Output is correct |
12 |
Correct |
304 ms |
43716 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 |
856 KB |
Output is correct |
19 |
Correct |
2 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 |
860 KB |
Output is correct |
25 |
Correct |
2 ms |
788 KB |
Output is correct |
26 |
Correct |
2 ms |
604 KB |
Output is correct |
27 |
Correct |
2 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 |
1 ms |
604 KB |
Output is correct |
31 |
Correct |
174 ms |
3076 KB |
Output is correct |
32 |
Correct |
170 ms |
2856 KB |
Output is correct |
33 |
Correct |
171 ms |
3216 KB |
Output is correct |
34 |
Correct |
163 ms |
3368 KB |
Output is correct |
35 |
Correct |
164 ms |
3608 KB |
Output is correct |
36 |
Correct |
172 ms |
3880 KB |
Output is correct |
37 |
Correct |
166 ms |
3132 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 |
604 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 |
232 ms |
43440 KB |
Output is correct |
48 |
Correct |
206 ms |
43204 KB |
Output is correct |
49 |
Correct |
241 ms |
42680 KB |
Output is correct |
50 |
Correct |
246 ms |
45968 KB |
Output is correct |
51 |
Correct |
295 ms |
51900 KB |
Output is correct |
52 |
Correct |
296 ms |
52172 KB |
Output is correct |
53 |
Correct |
109 ms |
33220 KB |
Output is correct |
54 |
Correct |
389 ms |
43176 KB |
Output is correct |
55 |
Correct |
407 ms |
43156 KB |
Output is correct |
56 |
Correct |
463 ms |
44196 KB |
Output is correct |
57 |
Correct |
408 ms |
53180 KB |
Output is correct |
58 |
Correct |
450 ms |
43948 KB |
Output is correct |
59 |
Correct |
487 ms |
52676 KB |
Output is correct |
60 |
Correct |
501 ms |
52616 KB |
Output is correct |
61 |
Correct |
482 ms |
53696 KB |
Output is correct |
62 |
Correct |
302 ms |
52052 KB |
Output is correct |
63 |
Correct |
481 ms |
52676 KB |
Output is correct |
64 |
Correct |
458 ms |
52708 KB |
Output is correct |
65 |
Correct |
480 ms |
52528 KB |
Output is correct |
66 |
Correct |
0 ms |
348 KB |
Output is correct |
67 |
Correct |
399 ms |
44176 KB |
Output is correct |
68 |
Correct |
393 ms |
43204 KB |
Output is correct |
69 |
Correct |
467 ms |
43972 KB |
Output is correct |
70 |
Correct |
425 ms |
44228 KB |
Output is correct |
71 |
Correct |
419 ms |
45152 KB |
Output is correct |
72 |
Correct |
422 ms |
45320 KB |
Output is correct |
73 |
Correct |
412 ms |
53112 KB |
Output is correct |
74 |
Correct |
420 ms |
53232 KB |
Output is correct |
75 |
Correct |
424 ms |
43460 KB |
Output is correct |
76 |
Correct |
451 ms |
47388 KB |
Output is correct |
77 |
Correct |
335 ms |
43708 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 |
604 KB |
Output is correct |
81 |
Correct |
2 ms |
604 KB |
Output is correct |
82 |
Correct |
2 ms |
852 KB |
Output is correct |
83 |
Correct |
2 ms |
860 KB |
Output is correct |
84 |
Correct |
2 ms |
860 KB |
Output is correct |
85 |
Correct |
1 ms |
604 KB |
Output is correct |
86 |
Correct |
171 ms |
3092 KB |
Output is correct |
87 |
Correct |
171 ms |
2904 KB |
Output is correct |
88 |
Correct |
179 ms |
3544 KB |
Output is correct |
89 |
Correct |
163 ms |
3152 KB |
Output is correct |
90 |
Correct |
165 ms |
3608 KB |
Output is correct |
91 |
Correct |
166 ms |
3868 KB |
Output is correct |
92 |
Correct |
166 ms |
3092 KB |
Output is correct |
93 |
Correct |
223 ms |
42692 KB |
Output is correct |
94 |
Correct |
205 ms |
42688 KB |
Output is correct |
95 |
Correct |
236 ms |
42696 KB |
Output is correct |
96 |
Correct |
239 ms |
46156 KB |
Output is correct |
97 |
Correct |
313 ms |
52052 KB |
Output is correct |
98 |
Correct |
284 ms |
52016 KB |
Output is correct |
99 |
Correct |
97 ms |
33472 KB |
Output is correct |
100 |
Correct |
472 ms |
52668 KB |
Output is correct |
101 |
Correct |
487 ms |
52968 KB |
Output is correct |
102 |
Correct |
477 ms |
52932 KB |
Output is correct |
103 |
Correct |
483 ms |
52308 KB |
Output is correct |
104 |
Correct |
461 ms |
52164 KB |
Output is correct |
105 |
Correct |
480 ms |
52420 KB |
Output is correct |
106 |
Correct |
439 ms |
41520 KB |
Output is correct |
107 |
Correct |
430 ms |
43720 KB |
Output is correct |
108 |
Correct |
424 ms |
43540 KB |
Output is correct |
109 |
Correct |
416 ms |
43976 KB |
Output is correct |
110 |
Correct |
494 ms |
52676 KB |
Output is correct |
111 |
Correct |
510 ms |
52932 KB |
Output is correct |