This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
//bad complexity to test idea
#define ll long long
const int MAXN = 1010;
using namespace std;
int N, Q;
int S[MAXN], E[MAXN];
int nxt[MAXN];
int mat[MAXN][MAXN];
void findNxtArray(){
for(int i = 1; i <= N; i++){
nxt[i] = -1;
mat[i][i] = 1;
for(int j = 1; j <= N; j++){
if(j == i)
continue;
if(!(E[j] >= S[i] && E[j] <= E[i]))
continue;
mat[i][j] = 1;
if(S[nxt[i]] >= S[i])
continue;
if(nxt[i] == -1 || S[nxt[i]] > S[j])
nxt[i] = j;
}
}
}
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(!mat[b][a] && nxt[b] != -1){
b = nxt[b];
ans++;
}
ans += (a != b);
if(nxt[b] == -1 || !mat[b][a]){
cout << "impossible\n";
}
else cout << ans << "\n";
}
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |