이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
int main(){
int n, q;
cin >> n >> q;
vector<pair<int, int> > inter(n);
for (int i = 0; i < n; i++){
cin >> inter[i].first >> inter[i].second;
}
if (n > 1000){
vector<pair<pair<int, int>, int> > elso(n);
vector<int> hely(n);
for (int i = 0; i < n; i++){
elso[i] = make_pair(inter[i], i);
}
sort(elso.begin(), elso.end());
for (int i = 0; i < n; i++){
hely[elso[i].second] = i;
}
vector<ll> tav(n, -1);
for (ll i = 0; i < n; i++){
if (tav[i] != -1) continue;
queue<ll> q;
q.push(i);
tav[i] = i*10000000;
while(!q.empty()){
ll pos = q.front();
q.pop();
if (pos != n - 1 && elso[pos].first.second <= elso[pos + 1].first.second && elso[pos].first.second >= elso[pos + 1].first.first){
tav[pos + 1] = tav[pos] + 1;
q.push(pos + 1);
}
}
}
for (int i = 0; i < q; i++){
int a, b;
cin >> a >> b;
a--, b--;
a = hely[a];
b = hely[b];
if (tav[b] - tav[a] < 0 || tav[b] - tav[a] > 999999){
cout << "impossible" << endl;
}
else cout << tav[b] - tav[a] << endl;
}
}
vector<vector<int> > graph(n);
for (int i = 0; i < n; i++){
for (int j = 0; j < n; j++){
if (i == j) continue;
if (inter[i].second <= inter[j].second && inter[i].second >= inter[j].first){
graph[i].push_back(j);
}
}
}
// for (int i = 0; i < n; i++){
// for (int x : graph[i]) cout << x << " ";
// cout << endl;
// }
for (int i = 0; i < q; i++){
int s, e;
cin >> s >> e;
s--, e--;
queue<int> q;
q.push(s);
vector<int> tav(n, -1);
tav [s] = 0;
while(!q.empty()){
int pos = q.front();
q.pop();
for (int x : graph[pos]){
if (tav[x] == -1){
tav[x] = tav[pos] + 1;
q.push(x);
}
}
}
if (tav[e] == -1) {
cout << "impossible" << endl;
}
else cout << tav[e] << endl;
}
return 0;
}
/*
5 2
1 3
2 4
4 7
7 9
3 7
1 4
3 2
*/
# | 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... |