#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const long long inf=1e16;
struct Tree {
typedef pair<int,int> T;
static constexpr T unit = {INT_MAX,INT_MAX};
T f(T a, T b) { return min(a, b); } // (any associative fn)
vector<T> s; int n;
Tree(int _n) {
n=_n;
s.assign(2*n,{INT_MAX,INT_MAX});
}
void update(int pos, T val) {
for (s[pos += n] = val; pos /= 2;)
s[pos] = f(s[pos * 2], s[pos * 2 + 1]);
}
T query(int b, int e) { // query [b, e)
T ra = unit, rb = unit;
for (b += n, e += n; b < e; b /= 2, e /= 2) {
if (b % 2) ra = f(ra, s[b++]);
if (e % 2) rb = f(s[--e], rb);
}
return f(ra, rb);
}
};
int main(){
ll n,q;
cin>>n>>q;
map<ll,ll> mapping;
Tree tree(2*n+10);
vector<pair<ll,ll>> v(n);
set<ll> coords;
for(int i=0;i<n;i++)cin>>v[i].first>>v[i].second;
for(int i=0;i<n;i++)coords.insert(v[i].first),coords.insert(v[i].second);
ll l=0;
for(auto a: coords)mapping[a]=l++;
for(int i=0;i<n;i++){
v[i].first=mapping[v[i].first];
v[i].second=mapping[v[i].second];
if(tree.query(v[i].second,v[i].second+1).first>v[i].first)tree.update(v[i].second,{v[i].first,i});
}
ll LOG=23;
// Building the edges doesn't work properly.
//Some intervals are getting self loops and I am not adding end points correcctly
vector<vector<ll>> p(n,vector<ll>(LOG));
for(int i=0;i<n;i++){
pair<ll,ll> cur=tree.query(v[i].first,v[i].second+1);
if(cur.first>=v[i].first){
p[i][0]=i;
}
else p[i][0]=cur.second;
}
for(int j=1;j<LOG;j++){
for(int i=0;i<n;i++)p[i][j]=p[p[i][j-1]][j-1];
}
for(int i=0;i<q;i++){
ll s,f;
cin>>s>>f;
s--,f--;
if(s==f){
cout<<0<<endl;
continue;
}
if(v[s].second>v[f].second){
cout<<"impossible"<<endl;
continue;
}
if(v[s].second>=v[f].first){
cout<<1<<endl;
continue;
}
ll ans=2,cur=f;
for(int i=LOG-1;i>=0;i--){
if(v[p[cur][i]].first>v[s].second){
ans+=(1<<i);
cur=p[cur][i];
}
}
if(v[p[cur][0]].first<=v[s].second)cout<<ans<<endl;
else cout<<"impossible"<<endl;
}
return 0;
}
/*
3 1
7 14
1 6
3 8
2 1
*/
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
441 ms |
37628 KB |
Output is correct |
3 |
Correct |
454 ms |
37640 KB |
Output is correct |
4 |
Correct |
512 ms |
37612 KB |
Output is correct |
5 |
Correct |
508 ms |
38716 KB |
Output is correct |
6 |
Correct |
550 ms |
39804 KB |
Output is correct |
7 |
Correct |
533 ms |
40272 KB |
Output is correct |
8 |
Correct |
529 ms |
49100 KB |
Output is correct |
9 |
Correct |
511 ms |
49048 KB |
Output is correct |
10 |
Correct |
502 ms |
38004 KB |
Output is correct |
11 |
Correct |
498 ms |
42444 KB |
Output is correct |
12 |
Correct |
353 ms |
38196 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
2 ms |
596 KB |
Output is correct |
4 |
Correct |
2 ms |
596 KB |
Output is correct |
5 |
Correct |
2 ms |
596 KB |
Output is correct |
6 |
Correct |
2 ms |
608 KB |
Output is correct |
7 |
Correct |
3 ms |
724 KB |
Output is correct |
8 |
Correct |
3 ms |
724 KB |
Output is correct |
9 |
Correct |
2 ms |
468 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
2 ms |
596 KB |
Output is correct |
4 |
Correct |
2 ms |
596 KB |
Output is correct |
5 |
Correct |
2 ms |
596 KB |
Output is correct |
6 |
Correct |
2 ms |
608 KB |
Output is correct |
7 |
Correct |
3 ms |
724 KB |
Output is correct |
8 |
Correct |
3 ms |
724 KB |
Output is correct |
9 |
Correct |
2 ms |
468 KB |
Output is correct |
10 |
Correct |
0 ms |
212 KB |
Output is correct |
11 |
Correct |
0 ms |
212 KB |
Output is correct |
12 |
Correct |
2 ms |
596 KB |
Output is correct |
13 |
Correct |
2 ms |
596 KB |
Output is correct |
14 |
Correct |
2 ms |
596 KB |
Output is correct |
15 |
Correct |
2 ms |
596 KB |
Output is correct |
16 |
Correct |
3 ms |
724 KB |
Output is correct |
17 |
Correct |
2 ms |
724 KB |
Output is correct |
18 |
Correct |
1 ms |
480 KB |
Output is correct |
19 |
Correct |
214 ms |
2784 KB |
Output is correct |
20 |
Correct |
185 ms |
2588 KB |
Output is correct |
21 |
Correct |
197 ms |
2864 KB |
Output is correct |
22 |
Correct |
216 ms |
3020 KB |
Output is correct |
23 |
Correct |
180 ms |
3296 KB |
Output is correct |
24 |
Correct |
209 ms |
3388 KB |
Output is correct |
25 |
Correct |
185 ms |
3108 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
2 ms |
596 KB |
Output is correct |
4 |
Correct |
2 ms |
596 KB |
Output is correct |
5 |
Correct |
2 ms |
596 KB |
Output is correct |
6 |
Correct |
2 ms |
608 KB |
Output is correct |
7 |
Correct |
3 ms |
724 KB |
Output is correct |
8 |
Correct |
3 ms |
724 KB |
Output is correct |
9 |
Correct |
2 ms |
468 KB |
Output is correct |
10 |
Correct |
0 ms |
212 KB |
Output is correct |
11 |
Correct |
0 ms |
212 KB |
Output is correct |
12 |
Correct |
2 ms |
596 KB |
Output is correct |
13 |
Correct |
2 ms |
596 KB |
Output is correct |
14 |
Correct |
2 ms |
596 KB |
Output is correct |
15 |
Correct |
2 ms |
596 KB |
Output is correct |
16 |
Correct |
3 ms |
724 KB |
Output is correct |
17 |
Correct |
2 ms |
724 KB |
Output is correct |
18 |
Correct |
1 ms |
468 KB |
Output is correct |
19 |
Correct |
241 ms |
38528 KB |
Output is correct |
20 |
Correct |
225 ms |
37388 KB |
Output is correct |
21 |
Correct |
262 ms |
37096 KB |
Output is correct |
22 |
Correct |
269 ms |
41084 KB |
Output is correct |
23 |
Correct |
405 ms |
48264 KB |
Output is correct |
24 |
Correct |
371 ms |
48032 KB |
Output is correct |
25 |
Correct |
93 ms |
26060 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
461 ms |
37584 KB |
Output is correct |
2 |
Correct |
455 ms |
37672 KB |
Output is correct |
3 |
Correct |
519 ms |
37624 KB |
Output is correct |
4 |
Correct |
503 ms |
49104 KB |
Output is correct |
5 |
Correct |
479 ms |
38056 KB |
Output is correct |
6 |
Correct |
603 ms |
48808 KB |
Output is correct |
7 |
Correct |
587 ms |
48740 KB |
Output is correct |
8 |
Correct |
629 ms |
48840 KB |
Output is correct |
9 |
Correct |
392 ms |
48152 KB |
Output is correct |
10 |
Correct |
570 ms |
48468 KB |
Output is correct |
11 |
Correct |
633 ms |
48328 KB |
Output is correct |
12 |
Correct |
575 ms |
48636 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
441 ms |
37628 KB |
Output is correct |
3 |
Correct |
454 ms |
37640 KB |
Output is correct |
4 |
Correct |
512 ms |
37612 KB |
Output is correct |
5 |
Correct |
508 ms |
38716 KB |
Output is correct |
6 |
Correct |
550 ms |
39804 KB |
Output is correct |
7 |
Correct |
533 ms |
40272 KB |
Output is correct |
8 |
Correct |
529 ms |
49100 KB |
Output is correct |
9 |
Correct |
511 ms |
49048 KB |
Output is correct |
10 |
Correct |
502 ms |
38004 KB |
Output is correct |
11 |
Correct |
498 ms |
42444 KB |
Output is correct |
12 |
Correct |
353 ms |
38196 KB |
Output is correct |
13 |
Correct |
0 ms |
212 KB |
Output is correct |
14 |
Correct |
0 ms |
212 KB |
Output is correct |
15 |
Correct |
2 ms |
596 KB |
Output is correct |
16 |
Correct |
2 ms |
596 KB |
Output is correct |
17 |
Correct |
2 ms |
596 KB |
Output is correct |
18 |
Correct |
2 ms |
608 KB |
Output is correct |
19 |
Correct |
3 ms |
724 KB |
Output is correct |
20 |
Correct |
3 ms |
724 KB |
Output is correct |
21 |
Correct |
2 ms |
468 KB |
Output is correct |
22 |
Correct |
0 ms |
212 KB |
Output is correct |
23 |
Correct |
0 ms |
212 KB |
Output is correct |
24 |
Correct |
2 ms |
596 KB |
Output is correct |
25 |
Correct |
2 ms |
596 KB |
Output is correct |
26 |
Correct |
2 ms |
596 KB |
Output is correct |
27 |
Correct |
2 ms |
596 KB |
Output is correct |
28 |
Correct |
3 ms |
724 KB |
Output is correct |
29 |
Correct |
2 ms |
724 KB |
Output is correct |
30 |
Correct |
1 ms |
480 KB |
Output is correct |
31 |
Correct |
214 ms |
2784 KB |
Output is correct |
32 |
Correct |
185 ms |
2588 KB |
Output is correct |
33 |
Correct |
197 ms |
2864 KB |
Output is correct |
34 |
Correct |
216 ms |
3020 KB |
Output is correct |
35 |
Correct |
180 ms |
3296 KB |
Output is correct |
36 |
Correct |
209 ms |
3388 KB |
Output is correct |
37 |
Correct |
185 ms |
3108 KB |
Output is correct |
38 |
Correct |
0 ms |
212 KB |
Output is correct |
39 |
Correct |
0 ms |
212 KB |
Output is correct |
40 |
Correct |
2 ms |
596 KB |
Output is correct |
41 |
Correct |
2 ms |
596 KB |
Output is correct |
42 |
Correct |
2 ms |
596 KB |
Output is correct |
43 |
Correct |
2 ms |
596 KB |
Output is correct |
44 |
Correct |
3 ms |
724 KB |
Output is correct |
45 |
Correct |
2 ms |
724 KB |
Output is correct |
46 |
Correct |
1 ms |
468 KB |
Output is correct |
47 |
Correct |
241 ms |
38528 KB |
Output is correct |
48 |
Correct |
225 ms |
37388 KB |
Output is correct |
49 |
Correct |
262 ms |
37096 KB |
Output is correct |
50 |
Correct |
269 ms |
41084 KB |
Output is correct |
51 |
Correct |
405 ms |
48264 KB |
Output is correct |
52 |
Correct |
371 ms |
48032 KB |
Output is correct |
53 |
Correct |
93 ms |
26060 KB |
Output is correct |
54 |
Correct |
461 ms |
37584 KB |
Output is correct |
55 |
Correct |
455 ms |
37672 KB |
Output is correct |
56 |
Correct |
519 ms |
37624 KB |
Output is correct |
57 |
Correct |
503 ms |
49104 KB |
Output is correct |
58 |
Correct |
479 ms |
38056 KB |
Output is correct |
59 |
Correct |
603 ms |
48808 KB |
Output is correct |
60 |
Correct |
587 ms |
48740 KB |
Output is correct |
61 |
Correct |
629 ms |
48840 KB |
Output is correct |
62 |
Correct |
392 ms |
48152 KB |
Output is correct |
63 |
Correct |
570 ms |
48468 KB |
Output is correct |
64 |
Correct |
633 ms |
48328 KB |
Output is correct |
65 |
Correct |
575 ms |
48636 KB |
Output is correct |
66 |
Correct |
0 ms |
212 KB |
Output is correct |
67 |
Correct |
423 ms |
37680 KB |
Output is correct |
68 |
Correct |
437 ms |
37812 KB |
Output is correct |
69 |
Correct |
494 ms |
37580 KB |
Output is correct |
70 |
Correct |
484 ms |
38740 KB |
Output is correct |
71 |
Correct |
468 ms |
39944 KB |
Output is correct |
72 |
Correct |
468 ms |
40224 KB |
Output is correct |
73 |
Correct |
480 ms |
49016 KB |
Output is correct |
74 |
Correct |
487 ms |
49236 KB |
Output is correct |
75 |
Correct |
469 ms |
38220 KB |
Output is correct |
76 |
Correct |
527 ms |
42488 KB |
Output is correct |
77 |
Correct |
336 ms |
38172 KB |
Output is correct |
78 |
Correct |
0 ms |
212 KB |
Output is correct |
79 |
Correct |
2 ms |
596 KB |
Output is correct |
80 |
Correct |
2 ms |
596 KB |
Output is correct |
81 |
Correct |
2 ms |
596 KB |
Output is correct |
82 |
Correct |
2 ms |
596 KB |
Output is correct |
83 |
Correct |
2 ms |
724 KB |
Output is correct |
84 |
Correct |
3 ms |
724 KB |
Output is correct |
85 |
Correct |
1 ms |
468 KB |
Output is correct |
86 |
Correct |
181 ms |
2788 KB |
Output is correct |
87 |
Correct |
187 ms |
2648 KB |
Output is correct |
88 |
Correct |
186 ms |
2836 KB |
Output is correct |
89 |
Correct |
181 ms |
2980 KB |
Output is correct |
90 |
Correct |
183 ms |
3364 KB |
Output is correct |
91 |
Correct |
175 ms |
3304 KB |
Output is correct |
92 |
Correct |
184 ms |
2976 KB |
Output is correct |
93 |
Correct |
254 ms |
37116 KB |
Output is correct |
94 |
Correct |
234 ms |
37004 KB |
Output is correct |
95 |
Correct |
250 ms |
37120 KB |
Output is correct |
96 |
Correct |
283 ms |
41344 KB |
Output is correct |
97 |
Correct |
406 ms |
47996 KB |
Output is correct |
98 |
Correct |
390 ms |
48128 KB |
Output is correct |
99 |
Correct |
90 ms |
26124 KB |
Output is correct |
100 |
Correct |
619 ms |
48836 KB |
Output is correct |
101 |
Correct |
586 ms |
48660 KB |
Output is correct |
102 |
Correct |
558 ms |
48872 KB |
Output is correct |
103 |
Correct |
561 ms |
48436 KB |
Output is correct |
104 |
Correct |
562 ms |
48208 KB |
Output is correct |
105 |
Correct |
557 ms |
48332 KB |
Output is correct |
106 |
Correct |
420 ms |
34976 KB |
Output is correct |
107 |
Correct |
469 ms |
37796 KB |
Output is correct |
108 |
Correct |
434 ms |
38056 KB |
Output is correct |
109 |
Correct |
419 ms |
37892 KB |
Output is correct |
110 |
Correct |
601 ms |
48672 KB |
Output is correct |
111 |
Correct |
583 ms |
48588 KB |
Output is correct |