Official

B - 街灯の明るさ / Brightness of Street Lights Editorial by admin

Qwen3-Coder-480B

Overview

This problem involves simulating brightness changes caused by replacing streetlight bulbs. Each replacement operation increases the brightness of the target streetlight and its two neighbors by 1 each, so we need to efficiently calculate the final brightness of each streetlight.

Analysis

Understanding the Problem

When the bulb of streetlight \(i\) is replaced, the brightness of streetlights \(i-1\), \(i\), and \(i+1\) each increases by 1. For example, with \(N=5\) streetlights, if the bulb of streetlight 3 is replaced: - Brightness of streetlight 2 +1 - Brightness of streetlight 3 +1 - Brightness of streetlight 4 +1

When replacing bulbs at the ends (streetlight 1 or streetlight N), non-existent streetlights are ignored.

Naive Approach

A straightforward method is to directly add to the brightness of the target streetlight and its neighbors in an array for each replacement operation.

Up to 3 positions updated per operation → O(M) operations results in O(3M) = O(M)

Since \(M \leq 2 \times 10^5\) in this problem, the naive approach is sufficiently fast.

Key Insights

  • We only need to count “how many times each streetlight’s brightness is increased”
  • The order of replacement operations does not affect the result (addition is order-independent)
  • Final brightness = Initial brightness \(A_i\) + number of times that streetlight was increased

Algorithm

  1. Prepare Count Array: Create an array count to record the number of additions to each streetlight

  2. Process Replacement Operations: For each replacement operation \(B_j\):

    • Increment count[B_j] by 1 (itself)
    • Increment count[B_j - 1] by 1 (left neighbor, if it exists)
    • Increment count[B_j + 1] by 1 (right neighbor, if it exists)
  3. Calculate Final Results: For each streetlight \(i\), final brightness = \(A_i\) + count[i]

Concrete Example

For \(N=4\), \(M=2\), \(A = [1, 2, 3, 4]\), \(B = [2, 3]\):

Operation Affected Streetlights
\(B_1 = 2\) Streetlights 1, 2, 3
\(B_2 = 3\) Streetlights 2, 3, 4

Count result: count = [1, 2, 2, 1]

Final brightness: \([1+1, 2+2, 3+2, 4+1] = [2, 4, 5, 5]\)

Complexity

  • Time Complexity: \(O(N + M)\)
    • Array initialization: \(O(N)\)
    • Processing \(M\) replacement operations: \(O(M)\)
    • Result calculation: \(O(N)\)
  • Space Complexity: \(O(N)\)
    • Count array and result array: \(O(N)\)

Implementation Notes

  1. Index Management:

    • In the problem statement, streetlights start from 1 (1-indexed)
    • Python arrays start from 0 (0-indexed)
    • Use the count array as 1-indexed, while keeping the A array as 0-indexed
  2. Preventing Out-of-Bounds Access:

    • By setting the size of the count array to N + 2, boundary conditions at the ends can be written concisely
    • Alternatively, explicitly check boundaries with if b - 1 >= 1 or if b + 1 <= N
  3. Handling Large Numbers:

    • \(A_i\) can be up to \(10^9\), and can increase by up to \(3M\) through \(M\) additions
    • In Python, there is no need to worry about integer overflow

Source Code

def main():
    import sys
    input = sys.stdin.readline
    
    N, M = map(int, input().split())
    A = list(map(int, input().split()))
    B = list(map(int, input().split()))
    
    # Count the number of additions to each streetlight
    count = [0] * (N + 2)  # Used as 1-indexed, +2 to prevent out-of-bounds access
    
    for b in B:
        # Replacing the bulb of streetlight b increases brightness of b-1, b, b+1 by 1
        count[b] += 1  # Streetlight b itself
        if b - 1 >= 1:
            count[b - 1] += 1  # Left neighbor
        if b + 1 <= N:
            count[b + 1] += 1  # Right neighbor
    
    # Calculate results
    result = []
    for i in range(N):
        result.append(A[i] + count[i + 1])  # A is 0-indexed, count is 1-indexed
    
    print(' '.join(map(str, result)))

if __name__ == '__main__':
    main()

This editorial was generated by claude4.5opus.

posted:
last update: