Official

A - 倉庫の在庫管理 / Warehouse Inventory Management Editorial by admin

Qwen3-Coder-480B

Overview

There are \(N\) warehouses, each given a current load and a capacity limit. After that, \(M\) loading/unloading operations are performed, and the problem asks how many warehouses end up exceeding their capacity.

Analysis

This problem can be solved by simple simulation, but since the constraints are very large (\(N, M \leq 2 \times 10^5\)), efficient processing is required.

Key Observations

  • Each operation simply “increases or decreases the load of a specific warehouse,” so it can be handled with incremental updates.
  • The final result only needs to be checked after all operations are complete, so we don’t need to worry about whether capacity was exceeded midway.

Issues with the Naive Approach

If input is read line by line using input(), it can be extremely slow in Python and may result in TLE. Since \(M\) can be up to \(2 \times 10^5\), a fast input method is necessary.

Solution

  • Read all input at once (sys.stdin.read) and split it into tokens for processing, achieving faster execution.
  • Load updates are done using an array W, directly adding to the corresponding warehouse’s value for each operation.

Algorithm

  1. Read all input at once.
  2. Store the initial state of each warehouse (current load \(W_i\) and capacity limit \(L_i\)) in lists.
  3. For each of the \(M\) operations, update (add to) the load of the specified warehouse.
  4. After all operations are complete, check whether the final load of each warehouse exceeds its limit, and count the number that do.

Complexity

  • Time complexity: \(O(N + M)\)
    (Input processing, applying operations, and the final check can all be done in linear time)
  • Space complexity: \(O(N)\)
    (Arrays to store warehouse information)

Implementation Notes

  • To handle array indices as 1-origin, W[0] is not used; instead, indexing starts from W[1].
  • sys.stdin.read is used for fast input.
  • Each operation can be processed in constant time, so sequential updates within a loop are sufficiently fast.
## Source Code

```python
import sys
input = sys.stdin.read

def main():
    data = input().split()
    idx = 0
    N = int(data[idx])
    idx += 1
    M = int(data[idx])
    idx += 1
    
    W = [0] * (N + 1)
    L = [0] * (N + 1)
    
    for i in range(1, N + 1):
        W[i] = int(data[idx])
        idx += 1
        L[i] = int(data[idx])
        idx += 1
    
    for _ in range(M):
        P = int(data[idx])
        idx += 1
        C = int(data[idx])
        idx += 1
        W[P] += C
    
    count = 0
    for i in range(1, N + 1):
        if W[i] > L[i]:
            count += 1
            
    print(count)

if __name__ == "__main__":
    main()

This editorial was generated by qwen3-coder-480b.

posted:
last update: