E - 街灯の照らす範囲 / Area Illuminated by Street Lights Editorial by admin Editorial for printing
gemini-3-flash-thinkingOverview
This problem involves managing the on/off state of streetlights arranged in a straight line, and computing the total number of streetlights within distance \(d\) from any lit streetlight. By managing the set of lit streetlights and focusing on the “gaps” between adjacent lit streetlights, we can perform the computation efficiently.
Analysis
1. Properties of the Illuminated Range
Let the indices of the lit streetlights be \(p_1, p_2, \dots, p_k\) (\(p_1 < p_2 < \dots < p_k\)). Each streetlight \(p_i\) illuminates the range \([p_i - d, p_i + d]\). Considering that the total range of streetlights is from \(1\) to \(N\), the overall illuminated range is the following union: $\(\bigcup_{i=1}^k [ \max(1, p_i - d), \min(N, p_i + d) ]\)$
If we try to naively compute the number of elements in this union, it would take \(O(N)\) or \(O(k)\) per query, which is too slow.
2. Focusing on Gaps
The total illuminated range can be computed by taking “the length from the leftmost point illuminated by the leftmost streetlight to the rightmost point illuminated by the rightmost streetlight” and subtracting “the total length of unilluminated gaps between streetlights”.
- Overall cover range: \([\max(1, p_1 - d), \min(N, p_k + d)]\) The length of this range is \(L = \min(N, p_k + d) - \max(1, p_1 - d) + 1\).
- Gap between adjacent streetlights \(p_i, p_{i+1}\): The rightward illumination of \(p_i\) extends to \(p_i + d\), and the leftward illumination of \(p_{i+1}\) extends to \(p_{i+1} - d\). The condition for this interval to be unilluminated is \(p_i + d < p_{i+1} - d - 1\), i.e., letting the spacing be \(g = p_{i+1} - p_i\), this occurs when \(g > 2d + 1\). In this case, the gap length is \((p_{i+1} - d - 1) - (p_i + d) = g - (2d + 1)\).
Therefore, the answer for operation 2 is: $\(\text{Ans} = L - \sum_{g_i > 2d+1} (g_i - (2d+1))\)\( where \)g_i$ represents all spacings between adjacent lit streetlights.
3. Handling Dynamic Updates
Operation 1 toggles the state of a streetlight. This corresponds to adding/removing elements from the “set of lit streetlights.” When an element is added or removed, the spacings (\(g\)) between adjacent streetlights also change. For example, when newly adding streetlight \(x\), the original spacing \(next - prev\) between the previously adjacent pair \((prev, next)\) disappears, and new spacings \((prev, x)\) and \((x, next)\) are created.
To efficiently manage the set of these spacings \(g\) and quickly obtain “the sum of all \(g > 2d+1\)” and “the count of such \(g\)”, we use two Binary Indexed Trees (BIT / Fenwick Trees).
Algorithm
- Data Structure Preparation:
std::set<int> lit: Manages the indices of currently lit streetlights in ascending order.BIT1 (cnt_bit): Counts how many spacings of each value \(g\) exist.BIT2 (sum_bit): Manages the sum of spacing values for each \(g\).
- Operation 1 (
1 x):- If streetlight \(x\) is in
lit(turning it off):- Find the previous and next streetlights \(prev, next\) relative to \(x\).
- Remove spacings \((x - prev)\) and \((next - x)\) from the BITs, and add spacing \((next - prev)\).
- If streetlight \(x\) is not in
lit(turning it on):- Find the previous and next streetlights \(prev, next\) at the insertion position of \(x\).
- Remove spacing \((next - prev)\) from the BITs, and add spacings \((x - prev)\) and \((next - x)\).
- If streetlight \(x\) is in
- Operation 2 (
2 d):- If
litis empty, output \(0\). - Let \(M = 2d + 1\).
- Compute the overall cover range \(L\).
- Using the BITs, obtain the count \(count\) and sum \(sum\) of spacings where \(g > M\).
- Output \(L - (sum - M \times count)\).
- If
Complexity
- Time Complexity: \(O((N + Q) \log N)\)
std::setoperations (insertion, deletion, search): \(O(\log N)\)- BIT updates and queries: \(O(\log N)\)
- Since these are performed for each query, the overall complexity is \(O(Q \log N)\). Initialization takes \(O(N \log N)\).
- Space Complexity: \(O(N)\)
- The
setholding streetlight states and the BIT sizes are proportional to \(N\).
- The
Implementation Notes
Boundary conditions: When referencing
lit.begin()orlit.end(), it is necessary to check whether previous/next elements exist.BIT range: Since the spacing \(g\) can be at most \(N\), the BIT size needs to be allocated to approximately \(N\).
Types: Since the answer and BIT cumulative values can exceed \(N\), use
long longtype.Source Code
#include <iostream>
#include <vector>
#include <string>
#include <set>
#include <algorithm>
using namespace std;
/**
* A Binary Indexed Tree (Fenwick Tree) to maintain gap counts and gap sums.
*/
struct FenwickTree {
int n;
vector<long long> tree;
FenwickTree(int n) : n(n), tree(n + 1, 0) {}
// Adds val to the element at idx
void add(int idx, long long val) {
if (idx <= 0 || idx > n) return;
for (; idx <= n; idx += idx & -idx) {
tree[idx] += val;
}
}
// Returns the prefix sum up to idx
long long prefix_sum(int idx) {
if (idx <= 0) return 0;
idx = min(idx, n);
long long res = 0;
for (; idx > 0; idx -= idx & -idx) {
res += tree[idx];
}
return res;
}
// Returns the sum in range [l, r]
long long query_range(int l, int r) {
if (l > r || l > n) return 0;
return prefix_sum(r) - prefix_sum(l - 1);
}
};
int main() {
// Fast I/O
ios::sync_with_stdio(false);
cin.tie(nullptr);
int N, Q;
if (!(cin >> N >> Q)) return 0;
string S;
cin >> S;
// Two BITs: one for the count of each gap value, one for the sum of each gap value.
// Gaps are distances between consecutive lit streetlights.
FenwickTree cnt_bit(N), sum_bit(N);
set<int> lit;
auto add_gap = [&](int g) {
if (g > 0) {
cnt_bit.add(g, 1);
sum_bit.add(g, g);
}
};
auto remove_gap = [&](int g) {
if (g > 0) {
cnt_bit.add(g, -1);
sum_bit.add(g, -g);
}
};
// Initialize the lit set and the gaps from the initial string S.
int last = -1;
for (int i = 0; i < N; ++i) {
if (S[i] == '1') {
int curr = i + 1;
lit.insert(curr);
if (last != -1) {
add_gap(curr - last);
}
last = curr;
}
}
for (int q = 0; q < Q; ++q) {
int t, v;
cin >> t >> v;
if (t == 1) {
// Type 1 query: toggle streetlight x
int x = v;
auto it = lit.find(x);
if (it != lit.end()) {
// Streetlight x is currently lit, we turn it off
int prev_p = -1, next_p = -1;
if (it != lit.begin()) prev_p = *prev(it);
if (next(it) != lit.end()) next_p = *next(it);
if (prev_p != -1) remove_gap(x - prev_p);
if (next_p != -1) remove_gap(next_p - x);
if (prev_p != -1 && next_p != -1) add_gap(next_p - prev_p);
lit.erase(it);
} else {
// Streetlight x is currently off, we turn it on
auto it_next = lit.lower_bound(x);
int prev_p = -1, next_p = -1;
if (it_next != lit.end()) next_p = *it_next;
if (it_next != lit.begin()) prev_p = *prev(it_next);
if (prev_p != -1 && next_p != -1) remove_gap(next_p - prev_p);
if (prev_p != -1) add_gap(x - prev_p);
if (next_p != -1) add_gap(next_p - x);
lit.insert(x);
}
} else {
// Type 2 query: count illuminated streetlights given distance d
long long d = v;
if (lit.empty()) {
cout << 0 << "\n";
} else {
int p1 = *lit.begin();
int pk = *lit.rbegin();
// A gap g = p_{i+1} - p_i is not fully illuminated if g > 2d + 1.
// The number of unlit streetlights in such a gap is g - (2d + 1).
long long m = 2LL * d + 1;
long long total_range = min((long long)N, (long long)pk + d) - max(1LL, (long long)p1 - d) + 1;
long long excess = 0;
if (m < N) {
// Sum up (g - m) for all gaps g > m.
long long num_g = cnt_bit.query_range((int)m + 1, N);
long long sum_g = sum_bit.query_range((int)m + 1, N);
excess = sum_g - m * num_g;
}
cout << total_range - excess << "\n";
}
}
}
return 0;
}
This editorial was generated by gemini-3-flash-thinking.
posted:
last update: