Pinball

Time limit: 2.15s Memory limit: 512MB Input: Output:

— Calm yourselves a little...

We have a ball which lies on the XX axis, initially placed at the 00 coordinate. We also have NN sets of walls which lie on the XX axis. Each set is described as a tuple (dir,len,freq)(dir,len,freq) where:

  • dirdir indicates the direction in which the walls are placed, which can either be L (left) or R (right)
  • if dir=dir= L, then the walls in the set are placed at len−len, 2len−2 \cdot len, 3len−3 \cdot len, ..., freqlen−freq \cdot len
  • if dir=dir= R, then the walls in the set are placed at lenlen, 2len2 \cdot len, 3len3 \cdot len, ..., freqlenfreq \cdot len

Note that through the nature of these informations, there can be multiple walls placed at the same coordinate.

At time T=0T=0 the ball starts moving to the right with a constant speed of one unit per second. When the ball hits a wall, the wall is automatically destroyed and the ball reverses its direction. If there are multiple walls situated at the same coordinate, only one of the walls is destroyed.

Task

You are given QQ queries. For each query you are given an integer TT. Output the coordinate of the ball after TT seconds.

Input data

The first line of input will contain the integers NN and QQ, separated by one space.

The next NN lines contain three space-separated integers, dirdir, lenlen and freqfreq, describing how the walls are placed.

The next QQ lines contain an integer, TT, describing a query.

Output data

Output QQ lines, the ii−th line should contain the answer for ii−th query.

Constraints and clarifications

  • 1N,Q250 0001 \leq N, Q \leq 250 \ 000
  • 1T10121 \leq T \leq 10^{12}
  • dir{dir \in \{L,, R}\}
  • 1len,freq10121 \leq len, freq \leq 10^{12}
# Points Constraints
0 0 Examples
1 13 N,Q1 000N, Q \leq 1 \ 000
2 8 Q,T1 000Q, T \leq 1 \ 000
3 16 1len101 \leq len \leq 10
4 10 T106T \leq 10^6
5 11 lenfreq106len \cdot freq \leq 10^6
6 9 Let SS be the sum of all freqfreq in the input. S106S \leq 10^6
7 33 No additional constraints

Example

stdin

3 12
R 3 2
R 6 1
L 3 2
0
1
2
3
4
5
6
7
17
18
19
200

stdout

0
1
2
3
2
1
0
-1
5
6
5
-152

Log in or sign up to be able to send submissions!