#include <bits/stdc++.h>
//bad complexity to test idea
#define ll long long
const int MAXN = 1e5+10;
const int LOG = 20;
using namespace std;
int N, Q;
int S[MAXN], E[MAXN];
int dp[LOG][MAXN];
void findNxtArray(){
for(int i = 0; i < LOG; i++)
for(int j = 1; j <= N; j++)
dp[i][j] = -1;
vector<int> sweep(N);
iota(sweep.begin(),sweep.end(), 1);
sort(sweep.begin(),sweep.end(), [&](int a, int b){
if(E[a] != E[b])
return E[a] < E[b];
return S[a] < S[b];
});
vector< pair<int,int> > s;
for(auto &i : sweep){
bool coloca = true;
while(!s.empty()){
int last = s.back().second;
if(S[last] >= S[i]){
s.pop_back();
}
else{
if(E[last] == E[i]){
coloca = false;
}
break;
}
}
if(coloca)
s.push_back(make_pair(E[i], i));
int val = lower_bound(s.begin(),s.end(), make_pair(S[i],-1))->second;
dp[0][i] = (val == i) ? -1 : val;
}
for(int i = 1; i < LOG; i++)
for(int j = 1; j <= N; j++)
if(dp[i-1][j] != -1)
dp[i][j] = dp[i-1][ dp[i-1][j] ];
}
bool isInside(int a, int b){
return E[a] >= S[b] && E[a] <= E[b];
}
int main(){
ios_base::sync_with_stdio(false);
cin.tie(0);
cin >> N >> Q;
for(int i = 1; i <= N; i++)
cin >> S[i] >> E[i];
findNxtArray();
for(int i = 1, a, b; i <= Q; i++){
cin >> a >> b;
if(isInside(a,b)){
cout << (a != b) << "\n";
continue;
}
if(E[a] > S[b]){
cout << "impossible\n";
continue;
}
int ans = 0;
for(int i = LOG-1; i >= 0 && !isInside(a,b); i--){
if(dp[i][b] == -1)
continue;
if(S[dp[i][b]] > E[a]){
ans += (1<<i);
b = dp[i][b];
}
}
b = dp[0][b];
ans++;
ans += (a != b);
if(b == -1 || !isInside(a,b) ){
cout << "impossible\n";
}
else cout << ans << "\n";
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
65 ms |
10432 KB |
Output is correct |
3 |
Correct |
81 ms |
10460 KB |
Output is correct |
4 |
Correct |
142 ms |
10464 KB |
Output is correct |
5 |
Correct |
82 ms |
10388 KB |
Output is correct |
6 |
Correct |
89 ms |
10468 KB |
Output is correct |
7 |
Correct |
94 ms |
10512 KB |
Output is correct |
8 |
Correct |
72 ms |
10484 KB |
Output is correct |
9 |
Correct |
60 ms |
10468 KB |
Output is correct |
10 |
Correct |
107 ms |
10372 KB |
Output is correct |
11 |
Correct |
96 ms |
9948 KB |
Output is correct |
12 |
Correct |
50 ms |
10044 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
468 KB |
Output is correct |
4 |
Correct |
1 ms |
468 KB |
Output is correct |
5 |
Correct |
1 ms |
468 KB |
Output is correct |
6 |
Correct |
1 ms |
468 KB |
Output is correct |
7 |
Correct |
1 ms |
468 KB |
Output is correct |
8 |
Correct |
1 ms |
468 KB |
Output is correct |
9 |
Correct |
1 ms |
468 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
468 KB |
Output is correct |
4 |
Correct |
1 ms |
468 KB |
Output is correct |
5 |
Correct |
1 ms |
468 KB |
Output is correct |
6 |
Correct |
1 ms |
468 KB |
Output is correct |
7 |
Correct |
1 ms |
468 KB |
Output is correct |
8 |
Correct |
1 ms |
468 KB |
Output is correct |
9 |
Correct |
1 ms |
468 KB |
Output is correct |
10 |
Correct |
0 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
468 KB |
Output is correct |
13 |
Correct |
1 ms |
468 KB |
Output is correct |
14 |
Correct |
1 ms |
468 KB |
Output is correct |
15 |
Correct |
1 ms |
480 KB |
Output is correct |
16 |
Correct |
1 ms |
468 KB |
Output is correct |
17 |
Correct |
1 ms |
468 KB |
Output is correct |
18 |
Correct |
1 ms |
468 KB |
Output is correct |
19 |
Correct |
33 ms |
1332 KB |
Output is correct |
20 |
Correct |
31 ms |
1412 KB |
Output is correct |
21 |
Correct |
30 ms |
1628 KB |
Output is correct |
22 |
Correct |
31 ms |
1688 KB |
Output is correct |
23 |
Correct |
32 ms |
1384 KB |
Output is correct |
24 |
Correct |
20 ms |
1576 KB |
Output is correct |
25 |
Correct |
30 ms |
1228 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
468 KB |
Output is correct |
4 |
Correct |
1 ms |
468 KB |
Output is correct |
5 |
Correct |
1 ms |
468 KB |
Output is correct |
6 |
Correct |
1 ms |
468 KB |
Output is correct |
7 |
Correct |
1 ms |
468 KB |
Output is correct |
8 |
Correct |
1 ms |
468 KB |
Output is correct |
9 |
Correct |
1 ms |
468 KB |
Output is correct |
10 |
Correct |
0 ms |
340 KB |
Output is correct |
11 |
Correct |
0 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
468 KB |
Output is correct |
13 |
Correct |
1 ms |
468 KB |
Output is correct |
14 |
Correct |
1 ms |
468 KB |
Output is correct |
15 |
Correct |
1 ms |
468 KB |
Output is correct |
16 |
Correct |
1 ms |
468 KB |
Output is correct |
17 |
Correct |
1 ms |
468 KB |
Output is correct |
18 |
Correct |
1 ms |
468 KB |
Output is correct |
19 |
Correct |
40 ms |
10452 KB |
Output is correct |
20 |
Correct |
34 ms |
10492 KB |
Output is correct |
21 |
Correct |
37 ms |
9292 KB |
Output is correct |
22 |
Correct |
41 ms |
9936 KB |
Output is correct |
23 |
Correct |
38 ms |
10372 KB |
Output is correct |
24 |
Correct |
39 ms |
10444 KB |
Output is correct |
25 |
Correct |
28 ms |
9324 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
68 ms |
10488 KB |
Output is correct |
2 |
Correct |
107 ms |
10480 KB |
Output is correct |
3 |
Correct |
136 ms |
10476 KB |
Output is correct |
4 |
Correct |
72 ms |
10444 KB |
Output is correct |
5 |
Correct |
90 ms |
10484 KB |
Output is correct |
6 |
Correct |
107 ms |
10484 KB |
Output is correct |
7 |
Correct |
94 ms |
10476 KB |
Output is correct |
8 |
Correct |
74 ms |
10488 KB |
Output is correct |
9 |
Correct |
37 ms |
10456 KB |
Output is correct |
10 |
Correct |
77 ms |
10484 KB |
Output is correct |
11 |
Correct |
72 ms |
10496 KB |
Output is correct |
12 |
Correct |
81 ms |
10444 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
65 ms |
10432 KB |
Output is correct |
3 |
Correct |
81 ms |
10460 KB |
Output is correct |
4 |
Correct |
142 ms |
10464 KB |
Output is correct |
5 |
Correct |
82 ms |
10388 KB |
Output is correct |
6 |
Correct |
89 ms |
10468 KB |
Output is correct |
7 |
Correct |
94 ms |
10512 KB |
Output is correct |
8 |
Correct |
72 ms |
10484 KB |
Output is correct |
9 |
Correct |
60 ms |
10468 KB |
Output is correct |
10 |
Correct |
107 ms |
10372 KB |
Output is correct |
11 |
Correct |
96 ms |
9948 KB |
Output is correct |
12 |
Correct |
50 ms |
10044 KB |
Output is correct |
13 |
Correct |
0 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
468 KB |
Output is correct |
16 |
Correct |
1 ms |
468 KB |
Output is correct |
17 |
Correct |
1 ms |
468 KB |
Output is correct |
18 |
Correct |
1 ms |
468 KB |
Output is correct |
19 |
Correct |
1 ms |
468 KB |
Output is correct |
20 |
Correct |
1 ms |
468 KB |
Output is correct |
21 |
Correct |
1 ms |
468 KB |
Output is correct |
22 |
Correct |
0 ms |
340 KB |
Output is correct |
23 |
Correct |
1 ms |
340 KB |
Output is correct |
24 |
Correct |
1 ms |
468 KB |
Output is correct |
25 |
Correct |
1 ms |
468 KB |
Output is correct |
26 |
Correct |
1 ms |
468 KB |
Output is correct |
27 |
Correct |
1 ms |
480 KB |
Output is correct |
28 |
Correct |
1 ms |
468 KB |
Output is correct |
29 |
Correct |
1 ms |
468 KB |
Output is correct |
30 |
Correct |
1 ms |
468 KB |
Output is correct |
31 |
Correct |
33 ms |
1332 KB |
Output is correct |
32 |
Correct |
31 ms |
1412 KB |
Output is correct |
33 |
Correct |
30 ms |
1628 KB |
Output is correct |
34 |
Correct |
31 ms |
1688 KB |
Output is correct |
35 |
Correct |
32 ms |
1384 KB |
Output is correct |
36 |
Correct |
20 ms |
1576 KB |
Output is correct |
37 |
Correct |
30 ms |
1228 KB |
Output is correct |
38 |
Correct |
0 ms |
340 KB |
Output is correct |
39 |
Correct |
0 ms |
340 KB |
Output is correct |
40 |
Correct |
1 ms |
468 KB |
Output is correct |
41 |
Correct |
1 ms |
468 KB |
Output is correct |
42 |
Correct |
1 ms |
468 KB |
Output is correct |
43 |
Correct |
1 ms |
468 KB |
Output is correct |
44 |
Correct |
1 ms |
468 KB |
Output is correct |
45 |
Correct |
1 ms |
468 KB |
Output is correct |
46 |
Correct |
1 ms |
468 KB |
Output is correct |
47 |
Correct |
40 ms |
10452 KB |
Output is correct |
48 |
Correct |
34 ms |
10492 KB |
Output is correct |
49 |
Correct |
37 ms |
9292 KB |
Output is correct |
50 |
Correct |
41 ms |
9936 KB |
Output is correct |
51 |
Correct |
38 ms |
10372 KB |
Output is correct |
52 |
Correct |
39 ms |
10444 KB |
Output is correct |
53 |
Correct |
28 ms |
9324 KB |
Output is correct |
54 |
Correct |
68 ms |
10488 KB |
Output is correct |
55 |
Correct |
107 ms |
10480 KB |
Output is correct |
56 |
Correct |
136 ms |
10476 KB |
Output is correct |
57 |
Correct |
72 ms |
10444 KB |
Output is correct |
58 |
Correct |
90 ms |
10484 KB |
Output is correct |
59 |
Correct |
107 ms |
10484 KB |
Output is correct |
60 |
Correct |
94 ms |
10476 KB |
Output is correct |
61 |
Correct |
74 ms |
10488 KB |
Output is correct |
62 |
Correct |
37 ms |
10456 KB |
Output is correct |
63 |
Correct |
77 ms |
10484 KB |
Output is correct |
64 |
Correct |
72 ms |
10496 KB |
Output is correct |
65 |
Correct |
81 ms |
10444 KB |
Output is correct |
66 |
Correct |
1 ms |
340 KB |
Output is correct |
67 |
Correct |
66 ms |
10444 KB |
Output is correct |
68 |
Correct |
84 ms |
10364 KB |
Output is correct |
69 |
Correct |
140 ms |
10484 KB |
Output is correct |
70 |
Correct |
90 ms |
10452 KB |
Output is correct |
71 |
Correct |
86 ms |
10456 KB |
Output is correct |
72 |
Correct |
83 ms |
10444 KB |
Output is correct |
73 |
Correct |
63 ms |
10368 KB |
Output is correct |
74 |
Correct |
82 ms |
10456 KB |
Output is correct |
75 |
Correct |
115 ms |
10404 KB |
Output is correct |
76 |
Correct |
107 ms |
10056 KB |
Output is correct |
77 |
Correct |
51 ms |
10132 KB |
Output is correct |
78 |
Correct |
1 ms |
360 KB |
Output is correct |
79 |
Correct |
1 ms |
468 KB |
Output is correct |
80 |
Correct |
1 ms |
468 KB |
Output is correct |
81 |
Correct |
1 ms |
468 KB |
Output is correct |
82 |
Correct |
1 ms |
468 KB |
Output is correct |
83 |
Correct |
1 ms |
468 KB |
Output is correct |
84 |
Correct |
1 ms |
468 KB |
Output is correct |
85 |
Correct |
1 ms |
464 KB |
Output is correct |
86 |
Correct |
45 ms |
1336 KB |
Output is correct |
87 |
Correct |
36 ms |
1400 KB |
Output is correct |
88 |
Correct |
37 ms |
1708 KB |
Output is correct |
89 |
Correct |
24 ms |
1612 KB |
Output is correct |
90 |
Correct |
23 ms |
1492 KB |
Output is correct |
91 |
Correct |
30 ms |
1500 KB |
Output is correct |
92 |
Correct |
31 ms |
1240 KB |
Output is correct |
93 |
Correct |
37 ms |
10408 KB |
Output is correct |
94 |
Correct |
33 ms |
10476 KB |
Output is correct |
95 |
Correct |
39 ms |
9316 KB |
Output is correct |
96 |
Correct |
45 ms |
9936 KB |
Output is correct |
97 |
Correct |
46 ms |
10440 KB |
Output is correct |
98 |
Correct |
40 ms |
10440 KB |
Output is correct |
99 |
Correct |
24 ms |
9300 KB |
Output is correct |
100 |
Correct |
89 ms |
10380 KB |
Output is correct |
101 |
Correct |
90 ms |
10444 KB |
Output is correct |
102 |
Correct |
75 ms |
10456 KB |
Output is correct |
103 |
Correct |
82 ms |
10472 KB |
Output is correct |
104 |
Correct |
84 ms |
10480 KB |
Output is correct |
105 |
Correct |
75 ms |
10480 KB |
Output is correct |
106 |
Correct |
94 ms |
10508 KB |
Output is correct |
107 |
Correct |
110 ms |
10392 KB |
Output is correct |
108 |
Correct |
71 ms |
9720 KB |
Output is correct |
109 |
Correct |
68 ms |
9620 KB |
Output is correct |
110 |
Correct |
61 ms |
9468 KB |
Output is correct |
111 |
Correct |
66 ms |
9452 KB |
Output is correct |