#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(){
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;
}
}
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;
int ans = 0;
while(b != -1 && !isInside(a,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 |
1 ms |
212 KB |
Output is correct |
2 |
Execution timed out |
1583 ms |
3036 KB |
Time limit exceeded |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
0 ms |
340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
0 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
0 ms |
212 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
1 ms |
340 KB |
Output is correct |
17 |
Correct |
1 ms |
340 KB |
Output is correct |
18 |
Correct |
1 ms |
340 KB |
Output is correct |
19 |
Correct |
449 ms |
928 KB |
Output is correct |
20 |
Correct |
1331 ms |
980 KB |
Output is correct |
21 |
Correct |
598 ms |
1228 KB |
Output is correct |
22 |
Correct |
20 ms |
1236 KB |
Output is correct |
23 |
Correct |
26 ms |
1052 KB |
Output is correct |
24 |
Correct |
20 ms |
1168 KB |
Output is correct |
25 |
Correct |
54 ms |
744 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
0 ms |
340 KB |
Output is correct |
10 |
Correct |
0 ms |
340 KB |
Output is correct |
11 |
Correct |
0 ms |
212 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
1 ms |
340 KB |
Output is correct |
17 |
Correct |
1 ms |
340 KB |
Output is correct |
18 |
Correct |
1 ms |
340 KB |
Output is correct |
19 |
Correct |
134 ms |
3036 KB |
Output is correct |
20 |
Correct |
75 ms |
3028 KB |
Output is correct |
21 |
Correct |
32 ms |
1872 KB |
Output is correct |
22 |
Correct |
44 ms |
2540 KB |
Output is correct |
23 |
Correct |
32 ms |
2992 KB |
Output is correct |
24 |
Correct |
31 ms |
3056 KB |
Output is correct |
25 |
Correct |
20 ms |
1876 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
1584 ms |
3028 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Execution timed out |
1583 ms |
3036 KB |
Time limit exceeded |
3 |
Halted |
0 ms |
0 KB |
- |