Submission #1104825

#TimeUsernameProblemLanguageResultExecution timeMemory
1104825VinhLuuRailway Trip 2 (JOI22_ho_t4)C++17
16 / 100
2067 ms20176 KiB
#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)

Main.cpp: In function 'int main()':
Main.cpp:65:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   65 |     freopen(task ".inp","r",stdin);
      |     ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~
Main.cpp:66:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   66 |     freopen(task ".out","w",stdout);
      |     ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...