Array and Operations

Interview

Time limit2sMemory limit512 MB

Summary
Apply a sequence of global 'add index to each position' updates and range reversals to a zero array, then report the values at m queried positions.
Level

Medium7 of 10

Topics
Array, Implementation, Math, Simulation
Solved
No attempts yet

Problem

You have an integer array XX of size nn, initialized with all zeros. You will apply operations to this array qq times.

There are two kinds of operations.

  • Add: add ii to every X[i]X[i]. (0≤i<n0 \le i < n)
  • Reverse: a reverse operation needs two indices ii and jj with i≤ji \le j. It reverses the order of the elements from X[i]X[i] through X[j]X[j]. That is, the order of X[i],X[i+1],…,X[j−1],X[j]X[i], X[i+1], \ldots, X[j-1], X[j] is reversed.

For example, let n=6n = 6 and consider applying the following five operations in order.

  1. Add
  2. Add
  3. Reverse with i=0i = 0, j=4j = 4
  4. Add
  5. Reverse with i=2i = 2, j=5j = 5

Initially XX is [0,0,0,0,0,0][0, 0, 0, 0, 0, 0]. The result after each operation is as follows.

  1. [0,1,2,3,4,5][0, 1, 2, 3, 4, 5]
  2. [0,2,4,6,8,10][0, 2, 4, 6, 8, 10]
  3. [8,6,4,2,0,10][8, 6, 4, 2, 0, 10]
  4. [8,7,6,5,4,15][8, 7, 6, 5, 4, 15]
  5. [8,7,15,4,5,6][8, 7, 15, 4, 5, 6]

Given the size nn of the array, the number qq of operations applied, and the operations themselves, you want to determine the values stored in XX.

Given an array YY of size mm, find the value of X[Y[i]]X[Y[i]] after all operations have been applied.

Input

The first line contains three integers nn, qq, mm. (1≤n≤1,000,0001 \le n \le 1{,}000{,}000, 0≤q≤10,0000 \le q \le 10{,}000, 1≤m≤5,0001 \le m \le 5{,}000)

The next qq lines contain the operations, one per line. Each line starts with 'a' or 'r'. 'a' denotes an add operation and 'r' denotes a reverse operation. When the line starts with 'r', it is followed by two space-separated integers ii and jj. (0≤i≤j<n0 \le i \le j < n)

The last line contains mm integers that describe the array YY, in order. (0≤Y[i]<n0 \le Y[i] < n)

Output

Print the value of X[Y[i]]X[Y[i]] on each of mm lines, one per line, in order.

Examples5

  1. Example 1

    Input
    6 5 6
    a
    a
    r 0 4
    a
    r 2 5
    0 1 2 3 4 5
    
    Expected output
    8 
    7 
    15 
    4 
    5 
    6
    
  2. Example 2

    Input
    3 0 3
    2 1 0
    
    Expected output
    0
    0
    0
    
  3. Example 3

    Input
    7 6 7
    a
    a
    r 0 3
    a
    a
    r 3 5
    0 1 2 3 4 5 6
    
    Expected output
    6
    6
    6
    20
    16
    6
    24
    
  4. Example 4

    Input
    2 7 2
    a
    r 0 1
    a
    a
    r 0 1
    a
    r 0 1
    0 1
    
    Expected output
    2
    2
    
  5. Example 5

    Input
    2 10 2
    a
    r 0 1
    a
    a
    r 0 1
    a
    r 0 1
    r 0 0
    r 1 1
    r 0 1
    0 1
    
    Expected output
    2
    2