# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
1104825 | VinhLuu | Railway Trip 2 (JOI22_ho_t4) | C++17 | 2067 ms | 20176 KiB |
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>
//#define int long long
#define ll long long
using namespace std;
const int N = 1e6 + 5;
const int oo = 1e9;
int n, m, k, q, a[N], b[N], S[N], T[N];
namespace sub2{
int le[N], ri[N], f[N];
void solve(){
for(int i = 1; i <= n; i ++) le[i] = ri[i] = i;
for(int i = 1; i <= m; i ++){
if(a[i] < b[i]) for(int j = a[i]; j <= min(b[i] - 1, a[i] + k - 1); j ++){
ri[j] = max(ri[j], b[i]);
}else for(int j = a[i]; j >= max(a[i] - k + 1, b[i] + 1); j --){
le[j] = min(le[j], b[i]);
}
}
// for(int i = 1; i <= n; i ++) cerr << i << " " << le[i] << " " << ri[i] << " \n";
for(int k = 1; k <= q; k ++){
int x = S[k], y = T[k];
queue<int> q;
for(int i = 1; i <= n; i ++) f[i] = oo;
for(int i = le[x]; i <= ri[x]; i ++){
f[i] = 1;
q.push(i);
}
int pl = le[x], pr = ri[x];
f[x] = 0;
while(!q.empty()){
int u = q.front(); q.pop();
// cerr << u << " " << f[u] << " g\n";
if(le[u] < pl){
for(int i = le[u]; i <= pl - 1; i ++){
f[i] = f[u] + 1;
q.push(i);
}
pl = le[u];
}
if(ri[u] > pr){
for(int i = pr + 1; i <= ri[u]; i ++){
f[i] = f[u] + 1;
q.push(i);
}
pr = ri[u];
}
}
if(f[y] == oo){
cout << -1 << "\n";
continue;
}else cout << f[y] << "\n";
}
}
}
signed main(){
ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define task "v"
if(fopen(task ".inp","r")){
freopen(task ".inp","r",stdin);
freopen(task ".out","w",stdout);
}
cin >> n >> k >> m;
for(int i = 1; i <= m; i ++) cin >> a[i] >> b[i];
cin >> q;
for(int i = 1; i <= q; i ++) cin >> S[i] >> T[i];
sub2 :: solve();
}
Compilation message (stderr)
# | 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... |