n 個公交站 (1~n),相鄰站距離 di。m 個乘客,第 i 個從 xi 到 yi。第 i 輛車從站 ((i-1) mod n) + 1 出發,往右行駛,到達 n 折返往左,到達 1 再折返,循環往複。車速 1 米/秒。求每位乘客最早到達目的站的時間。
車輛軌跡:1 → 2 → ... → n → n-1 → ... → 1 → ...,週期長度 2*(n-1) 站段。
預計算前綴和 dist[i] = 站 1 到站 i 的距離(dist[1]=0)。
展開為長度 2n-2 的線性數組 pos[t] 表示時刻 t 車所在「虛擬站點」位置(距離原點距離)。
第 i 位乘客,車起點 s = (i-1)%n + 1。
情況分類:
s ≤ x:車先到 x,上車時間 dist[x] - dist[s],到達 y 再加 dist[y] - dist[x],總時間 dist[y] - dist[s]。s > x:車先跑到 n 折返再回到 x,上車時間 dist[n] - dist[s] + dist[n] + dist[x],總時間 2*dist[n] - dist[s] + dist[y]。dist[n] - dist[s] + dist[n] - dist[x],總時間 2*dist[n] - dist[s] - dist[y]。#includeusing namespace std; typedef long long LL; const int MAXN = 200010; LL dist[MAXN]; int main() { ios::sync_with_stdio(false); cin.tie(nullptr); int T; cin >> T; while (T--) { int n, m; cin >> n >> m; for (int i = 1; i < n; ++i) { LL d; cin >> d; dist[i+1] = dist[i] + d; } LL total = dist[n]; for (int i = 1; i <= m; ++i) { int x, y; cin >> x >> y; int s = (i - 1) % n + 1; LL ans; if (x <= y) { if (s <= x) ans = dist[y] - dist[s]; else ans = 2*total - dist[s] + dist[y]; } else { ans = 2*total - dist[s] - dist[y]; } cout << ans << '\n'; } } return 0; }
HDU 5163 是「週期性運動 + 前綴和 + 分類討論」的數學建模題。核心技巧:展開折返路徑為虛擬直線距離 → 利用前綴和 O(1) 計算區間距離 → 分類起點相對位置求上車/到達時間。同類思想用於 Codeforces 週期性移動問題。