E - 荷物の集積作業 / Cargo Accumulation Work Editorial by admin
Claude 4.6 Opus (Thinking)Overview
This problem involves warehouses arranged in a line from east to west, where we repeatedly perform operations that move goods in a specified interval toward both endpoints, and we need to compute the transportation cost of each operation. We use a segment tree to efficiently manage the distribution of goods.
Analysis
Formulating the Cost Calculation
When an interval \([L, R]\) is specified, using the midpoint \(m = \lfloor (L+R)/2 \rfloor\) as the boundary: - Goods at positions \(L \leq i \leq m\) are moved to warehouse \(L\) (also moved to \(L\) when distances are equal) - Goods at positions \(m+1 \leq i \leq R\) are moved to warehouse \(R\)
The cost can be decomposed as follows:
\[\text{cost} = \underbrace{\sum_{i=L}^{m} A_i \cdot (i - L)}_{\text{cost of left half}} + \underbrace{\sum_{i=m+1}^{R} A_i \cdot (R - i)}_{\text{cost of right half}}\]
Expanding this:
\[\text{cost} = \left(\sum_{i=L}^{m} A_i \cdot i - L \sum_{i=L}^{m} A_i\right) + \left(R \sum_{i=m+1}^{R} A_i - \sum_{i=m+1}^{R} A_i \cdot i\right)\]
In other words, we can compute the cost if we know two quantities for a given interval: \(\sum A_i\) (total number of goods) and \(\sum A_i \cdot i\) (weighted position sum of goods).
Problem with the Naive Approach
If we inspect every warehouse in the interval for each operation, it takes \(O(N)\) in the worst case, resulting in \(O(NQ)\) over \(Q\) operations, which causes TLE.
Solution
We store \(\sum A_i\) and \(\sum A_i \cdot i\) in a segment tree, and perform all operations in \(O(\log N)\).
Algorithm
Values managed in the segment tree (two per node):
- sa: \(\sum A_i\) within the interval
- sai: \(\sum A_i \cdot i\) within the interval
Required operations:
1. Range query: Retrieve sa and sai for \([L, m]\) and \([m+1, R]\) respectively
2. Range zero-clear: Erase all goods in \([L, R]\) (implemented with lazy propagation)
3. Point addition: Add the total number of goods from the left half to warehouse \(L\), and the total from the right half to warehouse \(R\)
Processing steps for each query:
- Compute \(m = \lfloor (L+R)/2 \rfloor\)
- Retrieve \((S_L, S_{Li}) = (\sum A_i,\ \sum A_i \cdot i)\) from the left half \([L, m]\)
- Retrieve \((S_R, S_{Ri}) = (\sum A_i,\ \sum A_i \cdot i)\) from the right half \([m+1, R]\)
- Compute cost: \((S_{Li} - L \cdot S_L) + (R \cdot S_R - S_{Ri})\)
- Zero-clear the interval \([L, R]\)
- Add \(S_L\) at position \(L\), add \(S_R\) at position \(R\)
Concrete example: With 5 warehouses \(A = [0, 3, 5, 2, 4]\) (0-indexed), for interval \([1, 4]\): - \(m = \lfloor(1+4)/2\rfloor = 2\) - Left half \([1,2]\): \(S_L = 3+5 = 8\), \(S_{Li} = 3 \cdot 1 + 5 \cdot 2 = 13\) - Right half \([3,4]\): \(S_R = 2+4 = 6\), \(S_{Ri} = 2 \cdot 3 + 4 \cdot 4 = 22\) - Cost \(= (13 - 1 \cdot 8) + (4 \cdot 6 - 22) = 5 + 2 = 7\) - After operation: \(A = [0, 8, 0, 0, 6]\)
Complexity
- Time complexity: \(O(N + Q \log N)\)
- Building the segment tree takes \(O(N)\), and each query performs range queries, range zero-clear, and point additions each in \(O(\log N)\)
- Space complexity: \(O(N)\)
- The number of nodes in the segment tree is \(O(N)\)
Implementation Notes
Conversion to 0-indexed: Since the input is 1-indexed, we subtract 1 from both \(L\) and \(R\) to convert to 0-indexed.
Lazy propagation: The “zero-clear an interval” operation is managed with a lazy flag (
lz). Since zero-clearing simply setssa = sai = 0, the lazy propagation implementation is straightforward.Overflow prevention: Since \(A_i\) can be up to \(10^6\) and positions can be up to \(2 \times 10^5\), and goods may accumulate at a single location, we use
long long.Source Code
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
struct SegTree {
int n;
vector<ll> sa, sai;
vector<bool> lz;
void build(int nd, int l, int r, vector<ll>& a) {
lz[nd] = false;
if (l == r) { sa[nd] = a[l]; sai[nd] = a[l]*l; return; }
int m = (l+r)/2;
build(2*nd,l,m,a); build(2*nd+1,m+1,r,a);
sa[nd]=sa[2*nd]+sa[2*nd+1]; sai[nd]=sai[2*nd]+sai[2*nd+1];
}
SegTree(vector<ll>& a): n(a.size()), sa(4*a.size()), sai(4*a.size()), lz(4*a.size(),false) {
build(1,0,n-1,a);
}
void push(int nd) {
if (lz[nd]) {
for (int c:{2*nd,2*nd+1}) { sa[c]=sai[c]=0; lz[c]=true; }
lz[nd]=false;
}
}
void setZero(int nd, int l, int r, int ql, int qr) {
if (ql>qr||l>qr||r<ql) return;
if (ql<=l&&r<=qr) { sa[nd]=sai[nd]=0; lz[nd]=true; return; }
push(nd); int m=(l+r)/2;
setZero(2*nd,l,m,ql,qr); setZero(2*nd+1,m+1,r,ql,qr);
sa[nd]=sa[2*nd]+sa[2*nd+1]; sai[nd]=sai[2*nd]+sai[2*nd+1];
}
void addPt(int nd, int l, int r, int p, ll v) {
if (l==r) { sa[nd]+=v; sai[nd]+=v*p; return; }
push(nd); int m=(l+r)/2;
if (p<=m) addPt(2*nd,l,m,p,v); else addPt(2*nd+1,m+1,r,p,v);
sa[nd]=sa[2*nd]+sa[2*nd+1]; sai[nd]=sai[2*nd]+sai[2*nd+1];
}
pair<ll,ll> query(int nd, int l, int r, int ql, int qr) {
if (ql>qr||l>qr||r<ql) return {0,0};
if (ql<=l&&r<=qr) return {sa[nd],sai[nd]};
push(nd); int m=(l+r)/2;
auto [a1,b1]=query(2*nd,l,m,ql,qr);
auto [a2,b2]=query(2*nd+1,m+1,r,ql,qr);
return {a1+a2,b1+b2};
}
void setZero(int l, int r) { setZero(1,0,n-1,l,r); }
void addPt(int p, ll v) { addPt(1,0,n-1,p,v); }
pair<ll,ll> qry(int l, int r) { return query(1,0,n-1,l,r); }
};
int main(){
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int N,Q; cin>>N>>Q;
vector<ll> A(N);
for(int i=0;i<N;i++) cin>>A[i];
SegTree seg(A);
while(Q--){
int L,R; cin>>L>>R; L--; R--;
int m=(L+R)/2;
auto [sal,sail]=seg.qry(L,m);
auto [sar,sair]=seg.qry(m+1,R);
ll cost=(sail-(ll)L*sal)+((ll)R*sar-sair);
cout<<cost<<"\n";
seg.setZero(L,R);
seg.addPt(L,sal);
seg.addPt(R,sar);
}
}
This editorial was generated by claude4.6opus-thinking.