Interference
InterviewTime limit1sMemory limit1024 MB
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 , 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 and length 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 . 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 and (, ), the number of lines and the width of the container.
- lines, each containing either a wave description or a prediction task:
!: a wave description with starting position , length (), and amplitude (). It is guaranteed that .?: a prediction task for the resulting wave at position ().
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.