Official

E - 荷物の集積作業 / Cargo Accumulation Work Editorial by admin Editorial for printing

gpt-5.5-xhigh

Overview

For each operation with a specified interval \([L, R]\), gather the packages within the interval to the nearer endpoint \(L\) or \(R\), and output the cost.
We use a lazy segment tree that can manage the number of packages and the “position-weighted sum” to process this efficiently.

Analysis

For an operation \([L, R]\), the packages in each warehouse within the interval are moved as follows:

  • Packages in warehouses closer to \(L\), or equidistant from both endpoints, are moved to \(L\)
  • Packages in warehouses closer to \(R\) are moved to \(R\)

Since packages at equal distance are moved to \(L\), the boundary is

\(M = \left\lfloor \frac{L + R}{2} \right\rfloor\)

In other words,

  • Packages in warehouses \(L, L+1, \dots, M\) are moved to \(L\)
  • Packages in warehouses \(M+1, M+2, \dots, R\) are moved to \(R\)

For example, if \(L=2, R=7\),

\(M = \left\lfloor \frac{2+7}{2} \right\rfloor = 4\)

so,

  • Packages in warehouses \(2,3,4\) are moved to warehouse \(2\)
  • Packages in warehouses \(5,6,7\) are moved to warehouse \(7\)

If we naively look at all warehouses in the interval \([L, R]\) for each operation and move the packages, it takes \(O(NQ)\) in the worst case.

Since \(N \leq 2 \times 10^5\) and \(Q \leq 10^5\), this is too slow.

Therefore, we need to efficiently compute the total number of packages and the cost within an interval.

Let \(A_i\) be the number of packages in warehouse \(i\). The cost of moving the packages in the left half \([L, M]\) to \(L\) is

\(\sum_{i=L}^{M} A_i(i-L)\)

This can be transformed into

\(\sum_{i=L}^{M} A_i i - L \sum_{i=L}^{M} A_i\)

In other words, we can compute the cost if we know the following two values for an interval:

  • Total number of packages \(\sum A_i\)
  • Position-weighted sum \(\sum iA_i\)

Similarly, the cost of moving the packages in the right half \([M+1, R]\) to \(R\) is

\(\sum_{i=M+1}^{R} A_i(R-i)\)

which can be computed as

\(R \sum_{i=M+1}^{R} A_i - \sum_{i=M+1}^{R} iA_i\)

Algorithm

We use a lazy segment tree to manage the following values for each interval:

  • sum: total number of packages in the interval
  • wsum: total of \(i \times A_i\) in the interval

For each operation \([L, R]\), we perform the following steps:

  1. Compute \(M = \left\lfloor \frac{L+R}{2} \right\rfloor\)
  2. Retrieve sum and wsum for the interval \([L, M]\)
  3. Retrieve sum and wsum for the interval \([M+1, R]\)
  4. Compute the cost
  5. Reflect the package movement in the segment tree

Let the left side information be:

  • Total number of packages: \(S_l\)
  • Weighted sum: \(W_l\)

Then the cost for the left side is

\(W_l - L S_l\)

Let the right side information be:

  • Total number of packages: \(S_r\)
  • Weighted sum: \(W_r\)

Then the cost for the right side is

\(R S_r - W_r\)

Therefore, the total cost is

\((W_l - L S_l) + (R S_r - W_r)\)


After outputting the cost, we actually move the packages:

  • All packages in the interval \([L, M]\) are gathered at warehouse \(L\)
  • All packages in the interval \([M+1, R]\) are gathered at warehouse \(R\)

Therefore, we update the segment tree as follows:

  1. Set the entire interval \([L, M]\) to \(0\)
  2. Set the value of warehouse \(L\) to \(S_l\)
  3. Set the entire interval \([M+1, R]\) to \(0\)
  4. Set the value of warehouse \(R\) to \(S_r\)

Since we need the operation of setting an entire interval to \(0\), we use lazy propagation.

Complexity

  • Time complexity: \(O((N+Q)\log N)\)
  • Space complexity: \(O(N)\)

Implementation Notes

  • Since costs and total package counts can become large, use long long.

  • Each node of the segment tree holds the total number of packages sum and the weighted sum wsum.

  • Prepare a lazy flag lazyZero for setting an interval to \(0\).

  • pointSet is an “assignment”, not an “addition”. First set the interval to \(0\), then assign the gathered package count to the endpoint.

  • Since packages at equal distance are moved to the left endpoint \(L\), the split position is \(M = (L+R)/2\).

    Source Code

#include <bits/stdc++.h>
using namespace std;

struct SegTree {
    struct Node {
        long long sum = 0;
        long long wsum = 0;
        bool lazyZero = false;
    };

    int n;
    vector<Node> seg;

    SegTree(const vector<long long>& a) {
        n = (int)a.size() - 1;
        seg.resize(4 * n + 5);
        build(1, 1, n, a);
    }

    void build(int v, int l, int r, const vector<long long>& a) {
        if (l == r) {
            seg[v].sum = a[l];
            seg[v].wsum = a[l] * l;
            return;
        }
        int m = (l + r) / 2;
        build(v * 2, l, m, a);
        build(v * 2 + 1, m + 1, r, a);
        pull(v);
    }

    void pull(int v) {
        seg[v].sum = seg[v * 2].sum + seg[v * 2 + 1].sum;
        seg[v].wsum = seg[v * 2].wsum + seg[v * 2 + 1].wsum;
    }

    void applyZero(int v) {
        seg[v].sum = 0;
        seg[v].wsum = 0;
        seg[v].lazyZero = true;
    }

    void push(int v) {
        if (seg[v].lazyZero) {
            applyZero(v * 2);
            applyZero(v * 2 + 1);
            seg[v].lazyZero = false;
        }
    }

    pair<long long, long long> query(int ql, int qr) {
        if (ql > qr) return {0, 0};
        return query(1, 1, n, ql, qr);
    }

    pair<long long, long long> query(int v, int l, int r, int ql, int qr) {
        if (qr < l || r < ql) return {0, 0};
        if (ql <= l && r <= qr) return {seg[v].sum, seg[v].wsum};

        push(v);
        int m = (l + r) / 2;
        auto left = query(v * 2, l, m, ql, qr);
        auto right = query(v * 2 + 1, m + 1, r, ql, qr);
        return {left.first + right.first, left.second + right.second};
    }

    void rangeZero(int ql, int qr) {
        if (ql > qr) return;
        rangeZero(1, 1, n, ql, qr);
    }

    void rangeZero(int v, int l, int r, int ql, int qr) {
        if (qr < l || r < ql) return;
        if (ql <= l && r <= qr) {
            applyZero(v);
            return;
        }

        push(v);
        int m = (l + r) / 2;
        rangeZero(v * 2, l, m, ql, qr);
        rangeZero(v * 2 + 1, m + 1, r, ql, qr);
        pull(v);
    }

    void pointSet(int pos, long long val) {
        pointSet(1, 1, n, pos, val);
    }

    void pointSet(int v, int l, int r, int pos, long long val) {
        if (l == r) {
            seg[v].sum = val;
            seg[v].wsum = val * l;
            seg[v].lazyZero = false;
            return;
        }

        push(v);
        int m = (l + r) / 2;
        if (pos <= m) pointSet(v * 2, l, m, pos, val);
        else pointSet(v * 2 + 1, m + 1, r, pos, val);
        pull(v);
    }
};

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);

    int N, Q;
    cin >> N >> Q;

    vector<long long> A(N + 1);
    for (int i = 1; i <= N; i++) cin >> A[i];

    SegTree seg(A);

    while (Q--) {
        int L, R;
        cin >> L >> R;

        int M = (L + R) / 2;

        auto left = seg.query(L, M);
        auto right = seg.query(M + 1, R);

        long long cost = 0;
        cost += left.second - left.first * L;
        cost += right.first * R - right.second;

        cout << cost << '\n';

        seg.rangeZero(L, M);
        seg.pointSet(L, left.first);

        seg.rangeZero(M + 1, R);
        seg.pointSet(R, right.first);
    }

    return 0;
}

This editorial was generated by gpt-5.5-xhigh.

posted:
last update: