Score : 500 points
Problem Statement
Given are integer sequences A=(a1,a2,…,aN), T=(t1,t2,…,tN), and X=(x1,x2,…,xQ).
Let us define N functions f1(x),f2(x),…,fN(x) as follows:
$f_i(x) = \begin{cases} x + a_i & (t_i = 1)\\ \max(x, a_i) & (t_i = 2)\\ \min(x, a_i) & (t_i = 3)\\ \end{cases}$
For each i=1,2,…,Q, find fN(…f2(f1(xi))…).
Constraints
- All values in input are integers.
- 1≤N≤2×105
- 1≤Q≤2×105
- ∣ai∣≤109
- 1≤ti≤3
- ∣xi∣≤109
Input is given from Standard Input in the following format:
N
a1 t1
a2 t2
⋮
aN tN
Q
x1 x2 ⋯ xQ
Output
Print Q lines. The i-th line should contain fN(…f2(f1(xi))…).
3
-10 2
10 1
10 3
5
-15 -10 -5 0 5
0
0
5
10
10
We have $f_1(x) = \max(x, -10), f_2(x) = x + 10, f_3(x) = \min(x, 10)$, thus:
- f3(f2(f1(−15)))=0
- f3(f2(f1(−10)))=0
- f3(f2(f1(−5)))=5
- f3(f2(f1(0)))=10
- f3(f2(f1(5)))=10