QOJ.ac

QOJ

Time Limit: 2 s Memory Limit: 512 MB Total points: 100
[0]

# 10143. Pinball

Statistics

— Calm yourselves a little...

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

  • dir indicates the direction in which the walls are placed, which can either be L (left) or R (right)
  • if dir= L, then the walls in the set are placed at len, 2len, 3len, ..., freqlen
  • if dir= R, then the walls in the set are placed at len, 2len, 3len, ..., freqlen

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

At time T=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 Q queries. For each query you are given an integer T. Output the coordinate of the ball after T seconds.

Input data

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

The next N lines contain three space-separated integers, dir, len and freq, describing how the walls are placed.

The next Q lines contain an integer, T, describing a query.

Output data

Output Q lines, the i−th line should contain the answer for i−th query.

Constraints and clarifications

  • 1N,Q250 000
  • 1T1012
  • dir{L, R}
  • 1len,freq1012
# Points Constraints
0 0 Examples
1 13 N,Q1 000
2 8 Q,T1 000
3 16 1len10
4 10 T106
5 11 lenfreq106
6 9 Let S be the sum of all freq in the input. S106
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

— Liniștiți-vă puțin...

Avem o bilă ce se află pe axa X, inițial plasată la coordonata 0. Mai există și N mulțimi de ziduri ce se află pe axa X. Fiecare mulțime este descrisă ca un triplet (dir,len,freq), unde:

  • dir indică direcția în care zidurile sunt plasate, aceasta putând fi fie L (stânga) sau R (dreapta)
  • dacă dir= L, atunci zidurile din mulțime sunt puse la pozițiile len, 2len, 3len, ..., freqlen
  • dacă dir= R, atunci zidurile din mulțime sunt puse la pozițiile len, 2len, 3len, ..., freqlen

De observat că prin natura acestor informații, pot fi multiple ziduri plasate la aceeași coordonată.

La timpul T=0 bila începe să se miște la dreapta cu o viteză constantă de o unitate pe secundă. Când bila se lovește de un zid, zidul este automat distrus și bila își schimbă direcția. Dacă se află mai multe ziduri la aceeași coordonată, doar unul dintre ele va fi distrus.

Cerință

Se dau Q întrebări. La fiecare întrebare se va da un număr întreg T. Afișați coordonata bilei după T secunde.

Date de intrare

Prima linie a intrării va conține numerele întregi N și Q, separate de un spațiu.

Următoarele N linii conțin câte trei numere întregi separate prin spații, dir, len și freq, descriind cum sunt plasați pereții.

Următoarele Q linii conțin un număr întreg, T, descriind o întrebare.

Date de ieșire

Afișați Q linii, a i−a linie conținând răspunsul pentru cea de a i−a întrebare.

Restricții

  • 1N,Q250 000
  • 1T1012
  • dir{L, R}
  • 1len,freq1012
# Punctaj Restricții
0 0 Exemplele
1 13 N,Q1 000
2 8 Q,T1 000
3 16 1len10
4 10 T106
5 11 lenfreq106
6 9 Fie S suma tuturor freq din input. S106
7 33 Fără restricții suplimentare

Exemplu

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