Official

E - 街灯の照らす範囲 / Area Illuminated by Street Lights Editorial by admin Editorial for printing

gpt-5.5-xhigh

Overview

Street lights within distance \(d\) from a lit street light are illuminated.
The problem asks us to efficiently compute the “number of illuminated street lights” while toggling the on/off state with each operation.

Analysis

Suppose we arrange the positions of lit street lights in ascending order.

For example, if the lit street lights are at positions

\[ 2, 6, 10 \]

and \(d = 1\), the range each one illuminates is:

  • Street light \(2\): \([1, 3]\)
  • Street light \(6\): \([5, 7]\)
  • Street light \(10\): \([9, 11]\)

In this case, the unilluminated street lights can be categorized as follows:

  1. To the left of the first lit street light
  2. To the right of the last lit street light
  3. Between adjacent lit street lights

In other words, instead of directly counting the illuminated ones, we consider:

\[ \text{Number illuminated} = N - \text{Number not illuminated} \]


Let the positions of two adjacent lit street lights be \(a, b\), where \(a < b\).

Street light \(a\) illuminates \(d\) to the right, so it illuminates up to \(a+d\).
Street light \(b\) illuminates \(d\) to the left, so it illuminates from \(b-d\).

Therefore, unilluminated street lights exist in between at positions where

\[ a+d < x < b-d \]

The count of such positions is

\[ \max(0, b-a-(2d+1)) \]

Here, if we define the distance between adjacent lit street lights as

\[ g = b-a \]

then the number of unilluminated street lights between them is

\[ \max(0, g-(2d+1)) \]


If we naively examine all street lights for each query 2 d, it costs \(O(N)\).
Since \(N \leq 2 \times 10^5\) and \(Q \leq 10^5\), the worst case is \(O(NQ)\), which is too slow.

Therefore, we manage the “gap lengths” between lit street lights.

Algorithm

We manage the positions of lit street lights using a set.

We also manage the distances between adjacent lit street lights using a Fenwick Tree.

For a gap of distance \(g\), the number of unilluminated positions for query 2 d is

\[ \max(0, g-(2d+1)) \]

Here, let

\[ T = 2d+1 \]

Then what we want to compute is, over all gaps:

\[ \sum_{g > T} (g - T) \]

To compute this efficiently, we prepare 2 Fenwick Trees:

  • bitCnt[g]: how many gaps have distance \(g\)
  • bitSum[g]: the total length of gaps with distance \(g\)

Then we can obtain the following using Fenwick Tree prefix sums:

  • The number of gaps with distance \(T\) or less
  • The total length of gaps with distance \(T\) or less

Let totalCnt be the total number of gaps and totalSum be the total length, then:

\[ \text{Number of gaps with distance greater than } T = totalCnt - cntLE \]

\[ \text{Total length of gaps with distance greater than } T = totalSum - sumLE \]

Therefore, the number of unilluminated street lights within the gaps is

\[ sumGT - T \times cntGT \]


Also, let first be the first lit street light and last be the last lit street light. The number of unilluminated street lights on the outside is as follows:

Left side:

\[ \max(0, first-d-1) \]

Right side:

\[ \max(0, N-(last+d)) \]

Therefore, the total number of unilluminated street lights is

\[ \max(0, first-d-1) + \max(0, N-(last+d)) + \sum_{g > 2d+1}(g-(2d+1)) \]

The answer is

\[ N - \text{Total number of unilluminated street lights} \]


When toggling street light \(x\) with operation 1 x, only the gaps involving the lit street lights to the left and right of \(x\) are affected.

Turning off → on

Let \(L\) be the lit street light to the left of \(x\), and \(R\) be the lit street light to the right of \(x\).

If \(L\) and \(R\) were originally adjacent, remove the gap \(R-L\).

Instead, add:

  • \(x-L\)
  • \(R-x\)

Turning on → off

Let \(L\) be the lit street light to the left of \(x\), and \(R\) be the lit street light to the right of \(x\).

Since \(x\) is being turned off, remove:

  • \(x-L\)
  • \(R-x\)

Instead, since \(L\) and \(R\) become adjacent, add the gap \(R-L\).

If either the left or right neighbor doesn’t exist, only update the gaps that exist.

Complexity

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

Implementation Notes

  • If there are no lit street lights at all, the answer is always \(0\).

  • By using a set, we can retrieve the lit street lights to the left and right of a given street light \(x\) in \(O(\log N)\).

  • We maintain the following 2 types of Fenwick Trees:

    • One managing the count of gaps
    • One managing the total length of gaps
  • When adding or removing a gap of distance \(g\), update both Fenwick Trees simultaneously.

  • For query 2 d, note that using the threshold \(T=2d+1\), only gaps with \(g>T\) produce unilluminated portions.

    Source Code

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

struct Fenwick {
    int n;
    vector<long long> bit;

    Fenwick(int n = 0) : n(n), bit(n + 1, 0) {}

    void add(int idx, long long val) {
        for (; idx <= n; idx += idx & -idx) bit[idx] += val;
    }

    long long sum(int idx) const {
        if (idx > n) idx = n;
        if (idx <= 0) return 0;
        long long res = 0;
        for (; idx > 0; idx -= idx & -idx) res += bit[idx];
        return res;
    }
};

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

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

    string S;
    cin >> S;

    set<int> on;
    vector<int> state(N + 1, 0);

    Fenwick bitCnt(N + 2), bitSum(N + 2);
    long long totalCnt = 0, totalSum = 0;

    auto addGap = [&](int g, long long delta) {
        bitCnt.add(g, delta);
        bitSum.add(g, delta * g);
        totalCnt += delta;
        totalSum += delta * g;
    };

    for (int i = 1; i <= N; i++) {
        if (S[i - 1] == '1') {
            state[i] = 1;
            on.insert(i);
        }
    }

    if (!on.empty()) {
        auto it = on.begin();
        auto prev_it = it++;
        for (; it != on.end(); ++it, ++prev_it) {
            addGap(*it - *prev_it, 1);
        }
    }

    while (Q--) {
        int t, v;
        cin >> t >> v;

        if (t == 1) {
            int x = v;

            if (state[x] == 0) {
                auto r = on.lower_bound(x);
                bool hasR = (r != on.end());
                bool hasL = (r != on.begin());

                int L = -1, R = -1;
                if (hasL) {
                    auto l = prev(r);
                    L = *l;
                }
                if (hasR) R = *r;

                if (hasL && hasR) addGap(R - L, -1);
                if (hasL) addGap(x - L, 1);
                if (hasR) addGap(R - x, 1);

                on.insert(x);
                state[x] = 1;
            } else {
                auto it = on.find(x);
                bool hasL = (it != on.begin());
                bool hasR = (next(it) != on.end());

                int L = -1, R = -1;
                if (hasL) L = *prev(it);
                if (hasR) R = *next(it);

                if (hasL) addGap(x - L, -1);
                if (hasR) addGap(R - x, -1);
                if (hasL && hasR) addGap(R - L, 1);

                on.erase(it);
                state[x] = 0;
            }
        } else {
            long long d = v;

            if (on.empty()) {
                cout << 0 << '\n';
                continue;
            }

            long long first = *on.begin();
            long long last = *on.rbegin();

            long long uncovered = 0;
            uncovered += max(0LL, first - d - 1);
            uncovered += max(0LL, (long long)N - (last + d));

            long long T = 2 * d + 1;
            long long cntLE = bitCnt.sum((int)min<long long>(T, N + 2));
            long long sumLE = bitSum.sum((int)min<long long>(T, N + 2));

            long long cntGT = totalCnt - cntLE;
            long long sumGT = totalSum - sumLE;

            uncovered += sumGT - T * cntGT;

            cout << (long long)N - uncovered << '\n';
        }
    }

    return 0;
}

This editorial was generated by gpt-5.5-xhigh.

posted:
last update: