Under construction

Still being built. Things may change

Interference

Interview

Time limit1sMemory limit1024 MB

Summary
Each wave adds a period-4 pattern of +a and -a over an interval, and each query asks for the summed height at one position.
Level

Medium5 of 10

Topics
Prefix sum, Math, Implementation
Solved
No attempts yet

Problem

Physics can be so much fun! Yesterday, your teacher explained how interference works. When two waves overlap, their heights add up over the whole length of the waves. If both waves have a peak, the resulting peak is even higher. Likewise, if both waves dip below the water surface, the resulting wave dips even further. Technically, a wave's height is called its amplitude, and the distance between two wave peaks is called its wavelength.

Today, your physics teacher describes the setup of an experiment she is about to perform. She will create stationary waves in a one-dimensional container of water. Because she controls the physical setup precisely, every wave has a precisely controlled amplitude and is created only in an interval of a given length. The wavelength of each wave is always 44, and the first positive peak is always at the first index of the interval. The amplitude is measured only at integer points. For example, a wave with amplitude 22 and length 99 can be described as 2 0 -2 0 2 0 -2 0 2. If there is no wave at a point, the amplitude there is 00. Your task is to predict how high the resulting wave is at given points in the container, taking into account all waves created up to that point.

Figure I.1: Interference of three waves in Sample Input 2. The black dots represent the resulting wave's height.

Input

The input consists of:

  • One line with two integers nn and ww (1≤n≤4 0001 \le n \le 4\,000, 1≤w≤1091 \le w \le 10^{9}), the number of lines and the width of the container.
  • nn lines, each containing either a wave description or a prediction task:
    • ! pp ℓ\ell aa: a wave description with starting position pp, length ℓ\ell (1≤p,ℓ≤w1 \le p, \ell \le w), and amplitude aa (1≤a≤1091 \le a \le 10^9). It is guaranteed that p+ℓ−1≤wp+\ell - 1 \le w.
    • ? pp: a prediction task for the resulting wave at position pp (1≤p≤w1 \le p \le w).

Output

For each prediction task, output one line containing a single integer: the height of the resulting wave at the requested position, produced by all waves described before it.

Examples4

  1. Example 1

    Input
    4 10
    ! 2 7 1
    ? 9
    ? 7
    ? 6
    
    Expected output
    0
    0
    1
    
  2. Example 2

    Input
    7 10
    ! 2 6 1
    ! 3 8 2
    ! 5 2 3
    ? 6
    ! 5 5 4
    ? 8
    ? 9
    
    Expected output
    1
    0
    2
    
  3. Example 3

    Input
    6 12
    ! 1 7 1
    ! 7 3 2
    ? 6
    ? 7
    ? 8
    ? 10
    
    Expected output
    0
    1
    0
    0
    
  4. Example 4

    Input
    6 11
    ! 1 6 1
    ? 6
    ! 5 7 4
    ? 6
    ! 6 3 2
    ? 6
    
    Expected output
    0
    0
    2