E - 荷物の集積作業 / Cargo Accumulation Work Editorial by admin
gemini-3-flash-thinking概要
\(N\) 個の倉庫に置かれた荷物を、指定された区間 \([L_j, R_j]\) ごとに「近い方の端(\(L_j\) または \(R_j\))」へ集約する作業を \(Q\) 回繰り返します。荷物の移動によって倉庫の状態が変化していく中で、各作業の移動費用(荷物数 \(\times\) 距離)を求める問題です。
考察
1. 荷物の移動先の境界
倉庫 \(i\) にある荷物を \(L_j\) と \(R_j\) のどちらに運ぶかは、距離の比較で決まります。 - \(L_j\) への距離: \(i - L_j\) - \(R_j\) への距離: \(R_j - i\)
「距離が同じ場合は \(L_j\)」という条件を含めると、\(i - L_j \leq R_j - i\) すなわち \(2i \leq L_j + R_j\) を満たす倉庫 \(i\) の荷物は \(L_j\) へ、それ以外(\(2i > L_j + R_j\))は \(R_j\) へ運ばれます。 境界となるインデックスを \(M = \lfloor (L_j + R_j) / 2 \rfloor\) とすると、\([L_j, M]\) の荷物は \(L_j\) へ、\([M+1, R_j]\) の荷物は \(R_j\) へ移動することになります。
2. 費用の計算式
ある区間 \([a, b]\) の荷物を地点 \(P\) に集める際の費用は \(\sum_{i=a}^{b} A_i \times |i - P|\) です。 今回の作業では、以下の 2 つのグループの合計が費用となります。
- グループ1(\(L_j\) へ移動): \(\sum_{i=L_j}^{M} A_i (i - L_j) = \left( \sum_{i=L_j}^{M} A_i \cdot i \right) - L_j \cdot \left( \sum_{i=L_j}^{M} A_i \right)\)
- グループ2(\(R_j\) へ移動): \(\sum_{i=M+1}^{R_j} A_i (R_j - i) = R_j \cdot \left( \sum_{i=M+1}^{R_j} A_i \right) - \left( \sum_{i=M+1}^{R_j} A_i \cdot i \right)\)
この式から、任意の区間について 「荷物量の総和 \(\sum A_i\)」 と 「位置を考慮した荷物量の総和 \(\sum (A_i \cdot i)\)」 の 2 つの値が高速に取得できれば、費用を計算できることがわかります。
3. 状態の更新
作業が終わると、区間 \((L_j, R_j)\) 内の倉庫の荷物はすべて \(0\) になり、移動先の \(L_j\) と \(R_j\) に荷物が加算されます。 素朴に配列を更新すると、1 回の作業に \(O(N)\) かかり、全体で \(O(NQ)\) となって間に合いません。 「区間内の値を \(0\) にする(区間更新)」と「特定の地点の値を書き換える(点更新)」、および「区間和の取得」を高速に行えるデータ構造が必要です。
アルゴリズム
遅延評価セグメント木(Lazy Segment Tree)を用いることで、各操作を \(O(\log N)\) で処理できます。
- 保持する情報:
各ノードに以下の 2 つの値を持ちます。
sumA: その区間の \(A_i\) の総和sumAi: その区間の \(A_i \cdot i\) の総和
- 操作の詳細:
- クエリ: 区間 \([L_j, M]\) と \([M+1, R_j]\) について
sumAとsumAiを取得し、上記の式で費用を計算します。 - 区間更新(Clear): 区間 \([L_j, R_j]\) の
sumAとsumAiをすべて \(0\) にします。これは遅延フラグを用いて効率化します。 - 点更新(Set): 倉庫 \(L_j\) に「移動してきた荷物の総和」を、倉庫 \(R_j\) に「移動してきた荷物の総和」をセットします。
- クエリ: 区間 \([L_j, M]\) と \([M+1, R_j]\) について
計算量
- 時間計算量: \(O((N + Q) \log N)\)
- セグメント木の構築に \(O(N)\)、各クエリ(計算・区間削除・点更新)に \(O(\log N)\) かかります。
- 空間計算量: \(O(N)\)
- セグメント木のノード数は最大 \(4N\) 程度です。
実装のポイント
オーバーフローへの注意: 費用や
sumAiは非常に大きな値(最大で \(10^5 \times 10^6 \times 2 \times 10^5 = 2 \times 10^{16}\) 程度)になるため、必ず 64bit 整数型(C++ ならlong long)を使用してください。境界条件: \(L_j = R_j\) のケースは制約(\(L_j < R_j\))により考慮不要ですが、区間が空になる場合などの例外処理をセグメント木の関数内で適切にハンドルする必要があります。
遅延伝搬の順序: 区間を \(0\) にする操作の後に特定の地点へ値をセットするため、更新の順番を間違えないように注意しましょう。今回のコードでは、範囲を \(0\) にした後に
point_setで値を上書きしています。ソースコード
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
/**
* Problem: Logistics Collection Work
* We have N warehouses in a row. Each operation specifies a range [L, R].
* Items in the range are moved to either L or R, whichever is closer.
* If distances are equal, they move to L.
* The cost is the distance each item is moved.
* The move is actual, affecting future operations.
*/
typedef long long ll;
// Segment Tree Node to store sum of items (sumA) and sum of (items * warehouse index) (sumAi)
struct Node {
ll sumA;
ll sumAi;
bool lazy; // Lazy flag for range clearing (setting counts to 0)
};
const int MAXN = 200005;
Node tree[MAXN * 4];
// Standard segment tree pull (push-up) function
void pull(int v) {
tree[v].sumA = tree[2 * v].sumA + tree[2 * v + 1].sumA;
tree[v].sumAi = tree[2 * v].sumAi + tree[2 * v + 1].sumAi;
}
// Standard segment tree push (push-down) function for lazy propagation
void push(int v, int tl, int tr) {
if (tree[v].lazy) {
int tm = (tl + tr) / 2;
tree[2 * v].sumA = 0;
tree[2 * v].sumAi = 0;
tree[2 * v].lazy = true;
tree[2 * v + 1].sumA = 0;
tree[2 * v + 1].sumAi = 0;
tree[2 * v + 1].lazy = true;
tree[v].lazy = false;
}
}
// Build the segment tree from initial item counts
void build(const vector<ll>& A, int v, int tl, int tr) {
tree[v].lazy = false;
if (tl == tr) {
tree[v].sumA = A[tl];
tree[v].sumAi = A[tl] * (ll)tl;
} else {
int tm = (tl + tr) / 2;
build(A, 2 * v, tl, tm);
build(A, 2 * v + 1, tm + 1, tr);
pull(v);
}
}
// Range clear: sets item counts to 0 for all warehouses in range [l, r]
void range_clear(int v, int tl, int tr, int l, int r) {
if (l > r) return;
if (l == tl && r == tr) {
tree[v].sumA = 0;
tree[v].sumAi = 0;
tree[v].lazy = true;
} else {
push(v, tl, tr);
int tm = (tl + tr) / 2;
range_clear(2 * v, tl, tm, l, min(r, tm));
range_clear(2 * v + 1, tm + 1, tr, max(l, tm + 1), r);
pull(v);
}
}
// Point set: sets the item count for a specific warehouse
void point_set(int v, int tl, int tr, int pos, ll valA) {
if (tl == tr) {
tree[v].sumA = valA;
tree[v].sumAi = valA * (ll)pos;
tree[v].lazy = false;
} else {
push(v, tl, tr);
int tm = (tl + tr) / 2;
if (pos <= tm)
point_set(2 * v, tl, tm, pos, valA);
else
point_set(2 * v + 1, tm + 1, tr, pos, valA);
pull(v);
}
}
// Range query: returns {sum of A_i, sum of A_i * i} for range [l, r]
pair<ll, ll> query(int v, int tl, int tr, int l, int r) {
if (l > r) return {0, 0};
if (l == tl && r == tr) {
return {tree[v].sumA, tree[v].sumAi};
}
push(v, tl, tr);
int tm = (tl + tr) / 2;
pair<ll, ll> p1 = query(2 * v, tl, tm, l, min(r, tm));
pair<ll, ll> p2 = query(2 * v + 1, tm + 1, tr, max(l, tm + 1), r);
return {p1.first + p2.first, p1.second + p2.second};
}
int main() {
// Fast I/O
ios::sync_with_stdio(false);
cin.tie(nullptr);
int N, Q;
if (!(cin >> N >> Q)) return 0;
vector<ll> A(N + 1);
for (int i = 1; i <= N; ++i) {
cin >> A[i];
}
build(A, 1, 1, N);
for (int j = 0; j < Q; ++j) {
int L, R;
cin >> L >> R;
// Midpoint calculation: distance to L is i-L, distance to R is R-i.
// i-L <= R-i => 2i <= L+R => i <= (L+R)/2
int M = (L + R) / 2;
// Group 1: Warehouses [L, M] move to L
pair<ll, ll> res1 = query(1, 1, N, L, M);
ll S1 = res1.first;
ll W1 = res1.second;
// Group 2: Warehouses [M+1, R] move to R
pair<ll, ll> res2 = query(1, 1, N, M + 1, R);
ll S2 = res2.first;
ll W2 = res2.second;
// Cost = Sum over [L, M] of A_i * (i - L) + Sum over [M+1, R] of A_i * (R - i)
// Cost = (W1 - L * S1) + (R * S2 - W2)
ll cost = (W1 - (ll)L * S1) + ((ll)R * S2 - W2);
cout << cost << "\n";
// Update warehouse item counts:
// Clear all warehouses in [L, R], then set counts at L and R.
range_clear(1, 1, N, L, R);
point_set(1, 1, N, L, S1);
point_set(1, 1, N, R, S2);
}
return 0;
}
この解説は gemini-3-flash-thinking によって生成されました。