#include<bits/stdc++.h>
using namespace std;
#define int long long
const int inf=1e18;
struct segtree{
vector<pair<int,int> > tree;
segtree(int n){
tree.resize(n*4, {inf, -1});
}
pair<int,int> merge(pair<int,int> a, pair<int,int> b){
if(a.first<b.first){
return a;
}
return b;
}
pair<int,int> update(int l, int r, int v, int x, int val, int ind){
if(r<=x || x<l) return tree[v];
if(l+1==r){
return tree[v]=merge(tree[v], {val, ind});
}
int m=l+(r-l)/2;
return tree[v]=merge(update(l, m, v*2+1, x, val, ind), update(m, r, v*2+2, x, val, ind));
}
pair<int,int> query(int l, int r, int v, int ql, int qr){
if(qr<=l || r<=ql) return {inf, -1};
if(ql<=l && r<=qr) return tree[v];
int m=l+(r-l)/2;
return merge(query(l, m, v*2+1, ql, qr), query(m, r, v*2+2, ql, qr));
}
};
int dist(int a, int b, vector<vector<int> >& up, vector<int>& s, vector<int>& t){
if(a==b) return 0;
if(t[a]<t[b]) return -1;
if(s[a]<=t[b]){
return 1;
}
int l=25;
int ans=0;
for(int i=l-1; i>=0; i--){
if(up[a][i]!=-1 && t[b]<s[up[a][i]]){
a=up[a][i];
ans+=((int)1<<i);
}
}
if(up[a][0]!=-1){
return ans+2;
}
return -1;
}
signed main(){
ios_base::sync_with_stdio(false);
cin.tie(0);
int n, q;
cin >> n >> q;
vector<int> s(n);
vector<int> t(n);
set<int> nums;
for(int i=0; i<n; i++){
cin >> s[i] >> t[i];
nums.insert(s[i]);
nums.insert(t[i]);
}
int g=nums.size();
map<int,int> num; int ind=0;
for(auto e: nums){
num[e]=ind;
ind++;
}
segtree seg(g);
for(int i=0; i<n; i++){
seg.update(0, g, 0, num[t[i]], s[i], i);
}
vector<int> p(n, -1);
for(int i=0; i<n; i++){
auto x=seg.query(0, g, 0, num[s[i]], num[t[i]]+1);
if(x.first<s[i]){
p[i]=x.second;
}
}
int l=25;
vector<vector<int> > up(n, vector<int> (l, -1));
for(int i=0; i<n; i++){
up[i][0]=p[i];
}
for(int x=1; x<l; x++){
for(int i=0; i<n; i++){
if(up[i][x-1]!=-1){
up[i][x]=up[up[i][x-1]][x-1];
}
}
}
for(int i=0; i<q; i++){
int a, b;
cin >> a >> b;
int ans=dist(b-1, a-1, up, s, t);
if(ans==-1){
cout << "impossible\n";
}
else{
cout << ans << "\n";
}
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
383 ms |
43300 KB |
Output is correct |
3 |
Correct |
451 ms |
43496 KB |
Output is correct |
4 |
Correct |
427 ms |
43316 KB |
Output is correct |
5 |
Correct |
402 ms |
45068 KB |
Output is correct |
6 |
Correct |
471 ms |
46712 KB |
Output is correct |
7 |
Correct |
435 ms |
47332 KB |
Output is correct |
8 |
Correct |
448 ms |
61212 KB |
Output is correct |
9 |
Correct |
424 ms |
61152 KB |
Output is correct |
10 |
Correct |
441 ms |
43948 KB |
Output is correct |
11 |
Correct |
459 ms |
50656 KB |
Output is correct |
12 |
Correct |
185 ms |
43776 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
2 ms |
624 KB |
Output is correct |
4 |
Correct |
2 ms |
724 KB |
Output is correct |
5 |
Correct |
2 ms |
724 KB |
Output is correct |
6 |
Correct |
3 ms |
724 KB |
Output is correct |
7 |
Correct |
2 ms |
852 KB |
Output is correct |
8 |
Correct |
2 ms |
852 KB |
Output is correct |
9 |
Correct |
1 ms |
468 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
2 ms |
624 KB |
Output is correct |
4 |
Correct |
2 ms |
724 KB |
Output is correct |
5 |
Correct |
2 ms |
724 KB |
Output is correct |
6 |
Correct |
3 ms |
724 KB |
Output is correct |
7 |
Correct |
2 ms |
852 KB |
Output is correct |
8 |
Correct |
2 ms |
852 KB |
Output is correct |
9 |
Correct |
1 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 |
1 ms |
724 KB |
Output is correct |
13 |
Correct |
3 ms |
724 KB |
Output is correct |
14 |
Correct |
2 ms |
724 KB |
Output is correct |
15 |
Correct |
2 ms |
724 KB |
Output is correct |
16 |
Correct |
2 ms |
852 KB |
Output is correct |
17 |
Correct |
2 ms |
852 KB |
Output is correct |
18 |
Correct |
1 ms |
468 KB |
Output is correct |
19 |
Correct |
53 ms |
3224 KB |
Output is correct |
20 |
Correct |
40 ms |
2952 KB |
Output is correct |
21 |
Correct |
42 ms |
3144 KB |
Output is correct |
22 |
Correct |
33 ms |
3296 KB |
Output is correct |
23 |
Correct |
36 ms |
3916 KB |
Output is correct |
24 |
Correct |
36 ms |
3972 KB |
Output is correct |
25 |
Correct |
37 ms |
3620 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
2 ms |
624 KB |
Output is correct |
4 |
Correct |
2 ms |
724 KB |
Output is correct |
5 |
Correct |
2 ms |
724 KB |
Output is correct |
6 |
Correct |
3 ms |
724 KB |
Output is correct |
7 |
Correct |
2 ms |
852 KB |
Output is correct |
8 |
Correct |
2 ms |
852 KB |
Output is correct |
9 |
Correct |
1 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 |
724 KB |
Output is correct |
13 |
Correct |
2 ms |
724 KB |
Output is correct |
14 |
Correct |
2 ms |
724 KB |
Output is correct |
15 |
Correct |
3 ms |
724 KB |
Output is correct |
16 |
Correct |
2 ms |
852 KB |
Output is correct |
17 |
Correct |
3 ms |
852 KB |
Output is correct |
18 |
Correct |
1 ms |
468 KB |
Output is correct |
19 |
Correct |
284 ms |
42708 KB |
Output is correct |
20 |
Correct |
348 ms |
42696 KB |
Output is correct |
21 |
Correct |
326 ms |
42704 KB |
Output is correct |
22 |
Correct |
432 ms |
49136 KB |
Output is correct |
23 |
Correct |
459 ms |
59840 KB |
Output is correct |
24 |
Correct |
528 ms |
60216 KB |
Output is correct |
25 |
Correct |
51 ms |
25420 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
418 ms |
43236 KB |
Output is correct |
2 |
Correct |
322 ms |
43420 KB |
Output is correct |
3 |
Correct |
451 ms |
43324 KB |
Output is correct |
4 |
Correct |
401 ms |
61004 KB |
Output is correct |
5 |
Correct |
419 ms |
43740 KB |
Output is correct |
6 |
Correct |
596 ms |
60776 KB |
Output is correct |
7 |
Correct |
566 ms |
60660 KB |
Output is correct |
8 |
Correct |
547 ms |
60844 KB |
Output is correct |
9 |
Correct |
459 ms |
59920 KB |
Output is correct |
10 |
Correct |
550 ms |
60396 KB |
Output is correct |
11 |
Correct |
551 ms |
60252 KB |
Output is correct |
12 |
Correct |
592 ms |
60560 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
383 ms |
43300 KB |
Output is correct |
3 |
Correct |
451 ms |
43496 KB |
Output is correct |
4 |
Correct |
427 ms |
43316 KB |
Output is correct |
5 |
Correct |
402 ms |
45068 KB |
Output is correct |
6 |
Correct |
471 ms |
46712 KB |
Output is correct |
7 |
Correct |
435 ms |
47332 KB |
Output is correct |
8 |
Correct |
448 ms |
61212 KB |
Output is correct |
9 |
Correct |
424 ms |
61152 KB |
Output is correct |
10 |
Correct |
441 ms |
43948 KB |
Output is correct |
11 |
Correct |
459 ms |
50656 KB |
Output is correct |
12 |
Correct |
185 ms |
43776 KB |
Output is correct |
13 |
Correct |
1 ms |
212 KB |
Output is correct |
14 |
Correct |
0 ms |
212 KB |
Output is correct |
15 |
Correct |
2 ms |
624 KB |
Output is correct |
16 |
Correct |
2 ms |
724 KB |
Output is correct |
17 |
Correct |
2 ms |
724 KB |
Output is correct |
18 |
Correct |
3 ms |
724 KB |
Output is correct |
19 |
Correct |
2 ms |
852 KB |
Output is correct |
20 |
Correct |
2 ms |
852 KB |
Output is correct |
21 |
Correct |
1 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 |
1 ms |
724 KB |
Output is correct |
25 |
Correct |
3 ms |
724 KB |
Output is correct |
26 |
Correct |
2 ms |
724 KB |
Output is correct |
27 |
Correct |
2 ms |
724 KB |
Output is correct |
28 |
Correct |
2 ms |
852 KB |
Output is correct |
29 |
Correct |
2 ms |
852 KB |
Output is correct |
30 |
Correct |
1 ms |
468 KB |
Output is correct |
31 |
Correct |
53 ms |
3224 KB |
Output is correct |
32 |
Correct |
40 ms |
2952 KB |
Output is correct |
33 |
Correct |
42 ms |
3144 KB |
Output is correct |
34 |
Correct |
33 ms |
3296 KB |
Output is correct |
35 |
Correct |
36 ms |
3916 KB |
Output is correct |
36 |
Correct |
36 ms |
3972 KB |
Output is correct |
37 |
Correct |
37 ms |
3620 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 |
724 KB |
Output is correct |
41 |
Correct |
2 ms |
724 KB |
Output is correct |
42 |
Correct |
2 ms |
724 KB |
Output is correct |
43 |
Correct |
3 ms |
724 KB |
Output is correct |
44 |
Correct |
2 ms |
852 KB |
Output is correct |
45 |
Correct |
3 ms |
852 KB |
Output is correct |
46 |
Correct |
1 ms |
468 KB |
Output is correct |
47 |
Correct |
284 ms |
42708 KB |
Output is correct |
48 |
Correct |
348 ms |
42696 KB |
Output is correct |
49 |
Correct |
326 ms |
42704 KB |
Output is correct |
50 |
Correct |
432 ms |
49136 KB |
Output is correct |
51 |
Correct |
459 ms |
59840 KB |
Output is correct |
52 |
Correct |
528 ms |
60216 KB |
Output is correct |
53 |
Correct |
51 ms |
25420 KB |
Output is correct |
54 |
Correct |
418 ms |
43236 KB |
Output is correct |
55 |
Correct |
322 ms |
43420 KB |
Output is correct |
56 |
Correct |
451 ms |
43324 KB |
Output is correct |
57 |
Correct |
401 ms |
61004 KB |
Output is correct |
58 |
Correct |
419 ms |
43740 KB |
Output is correct |
59 |
Correct |
596 ms |
60776 KB |
Output is correct |
60 |
Correct |
566 ms |
60660 KB |
Output is correct |
61 |
Correct |
547 ms |
60844 KB |
Output is correct |
62 |
Correct |
459 ms |
59920 KB |
Output is correct |
63 |
Correct |
550 ms |
60396 KB |
Output is correct |
64 |
Correct |
551 ms |
60252 KB |
Output is correct |
65 |
Correct |
592 ms |
60560 KB |
Output is correct |
66 |
Correct |
1 ms |
212 KB |
Output is correct |
67 |
Correct |
407 ms |
43392 KB |
Output is correct |
68 |
Correct |
429 ms |
43656 KB |
Output is correct |
69 |
Correct |
443 ms |
43340 KB |
Output is correct |
70 |
Correct |
432 ms |
45248 KB |
Output is correct |
71 |
Correct |
446 ms |
46688 KB |
Output is correct |
72 |
Correct |
417 ms |
47572 KB |
Output is correct |
73 |
Correct |
500 ms |
61008 KB |
Output is correct |
74 |
Correct |
425 ms |
61080 KB |
Output is correct |
75 |
Correct |
409 ms |
43728 KB |
Output is correct |
76 |
Correct |
425 ms |
50580 KB |
Output is correct |
77 |
Correct |
170 ms |
43804 KB |
Output is correct |
78 |
Correct |
0 ms |
212 KB |
Output is correct |
79 |
Correct |
2 ms |
724 KB |
Output is correct |
80 |
Correct |
2 ms |
724 KB |
Output is correct |
81 |
Correct |
2 ms |
724 KB |
Output is correct |
82 |
Correct |
2 ms |
724 KB |
Output is correct |
83 |
Correct |
3 ms |
892 KB |
Output is correct |
84 |
Correct |
3 ms |
852 KB |
Output is correct |
85 |
Correct |
1 ms |
468 KB |
Output is correct |
86 |
Correct |
59 ms |
3336 KB |
Output is correct |
87 |
Correct |
38 ms |
2960 KB |
Output is correct |
88 |
Correct |
40 ms |
3152 KB |
Output is correct |
89 |
Correct |
31 ms |
3420 KB |
Output is correct |
90 |
Correct |
37 ms |
3972 KB |
Output is correct |
91 |
Correct |
36 ms |
4012 KB |
Output is correct |
92 |
Correct |
37 ms |
3576 KB |
Output is correct |
93 |
Correct |
390 ms |
42668 KB |
Output is correct |
94 |
Correct |
376 ms |
42648 KB |
Output is correct |
95 |
Correct |
335 ms |
42692 KB |
Output is correct |
96 |
Correct |
417 ms |
49008 KB |
Output is correct |
97 |
Correct |
531 ms |
59956 KB |
Output is correct |
98 |
Correct |
519 ms |
59948 KB |
Output is correct |
99 |
Correct |
71 ms |
25420 KB |
Output is correct |
100 |
Correct |
582 ms |
60656 KB |
Output is correct |
101 |
Correct |
663 ms |
60900 KB |
Output is correct |
102 |
Correct |
626 ms |
61452 KB |
Output is correct |
103 |
Correct |
564 ms |
61032 KB |
Output is correct |
104 |
Correct |
551 ms |
60808 KB |
Output is correct |
105 |
Correct |
562 ms |
60968 KB |
Output is correct |
106 |
Correct |
329 ms |
39804 KB |
Output is correct |
107 |
Correct |
527 ms |
44168 KB |
Output is correct |
108 |
Correct |
361 ms |
44240 KB |
Output is correct |
109 |
Correct |
407 ms |
44268 KB |
Output is correct |
110 |
Correct |
540 ms |
60868 KB |
Output is correct |
111 |
Correct |
547 ms |
60628 KB |
Output is correct |