Under construction

Still being built. Things may change

Magic Donut Game

Time limit2sMemory limit512 MB

Summary
Given a circular board with rotate, flip, and range-add cards, output the sum of the first X cells from the reference cell for each sum card, modulo 1e9+7.
Level

Medium6 of 10

Topics
Segment tree, Array, Simulation
Solved
No attempts yet

Problem

Albert plays the Magic Donut Game on a donut-shaped board made of NN cells of equal size.

First, one cell is chosen as the "reference cell" and placed at the 12 o'clock position. Then the integers V1,V2,…,VNV_1, V_2, \dots, V_N are written into the cells in clockwise order. The figure below shows the case N=7N = 7, V=[2,0,2,4,9,9,9]V = [2, 0, 2, 4, 9, 9, 9]. The arrow marks the reference cell.

Next, MM instruction cards are drawn in order, and each card's instruction is carried out. Each card has three integers. For the ii-th card, the integers are called Si,Xi,YiS_i, X_i, Y_i in that order. There are five kinds of instructions, and the first integer SiS_i decides which one applies (Si∈{1,2,3,4,5}S_i \in \{1, 2, 3, 4, 5\}).

  • Si=1S_i = 1: Rotate the board counterclockwise by XiX_i cells. YiY_i is always 00.
  • Si=2S_i = 2: Rotate the board clockwise by XiX_i cells. YiY_i is always 00.
  • Si=3S_i = 3: Flip the board over the axis through 12 o'clock and 6 o'clock. XiX_i and YiY_i are always 00.
  • Si=4S_i = 4: Starting from the reference cell and moving clockwise, add YiY_i to each of the next XiX_i cells.
  • Si=5S_i = 5: Starting from the reference cell and moving clockwise, add up the values in the next XiX_i cells and record the sum on a memo. The jj-th recorded value is called OjO_j.

For example, take M=9M = 9, S=[1,4,5,5,3,5,2,4,5]S = [1, 4, 5, 5, 3, 5, 2, 4, 5], X=[2,4,3,5,0,3,2,4,3]X = [2, 4, 3, 5, 0, 3, 2, 4, 3], and Y=[0,3,0,0,0,0,0,2,0]Y = [0, 3, 0, 0, 0, 0, 0, 2, 0]. The steps are shown in the table below.

Board stateDescription
The board at the start. The arrowed cell is the reference cell.
After card 1, the board is rotated counterclockwise by 2 cells.
After card 2, 3 is added to each of 4 cells.
Card 3 sums the values of 3 cells: 5+7+12=245 + 7 + 12 = 24, so O1=24O_1 = 24. The board does not change.
Card 4 sums the values of 5 cells: 5+7+12+12+9=455 + 7 + 12 + 12 + 9 = 45, so O2=45O_2 = 45. The board does not change.
After card 5, the board is flipped. The reference cell stays at 12 o'clock.
Card 6 sums the values of 3 cells: 5+0+2=75 + 0 + 2 = 7, so O3=7O_3 = 7. The board does not change.
After card 7, the board is rotated clockwise by 2 cells.
After card 8, 2 is added to each of 4 cells.
Card 9 sums the values of 3 cells: 14+9+7=3014 + 9 + 7 = 30, so O4=30O_4 = 30. The board does not change.

There are four type 5 cards, so O=[24,45,7,30]O = [24, 45, 7, 30].

Input

The first line contains the number of test cases TT.

The first line of each test case contains NN and MM, separated by a space. The second line contains the NN elements of array VV. The third line contains the MM elements of array SS. The fourth line contains the MM elements of array XX. The fifth line contains the MM elements of array YY. All values on each line are separated by spaces.

Output

For each test case, print the elements of array OO on one line, separated by spaces. Since the values can become very large, print each element of OO modulo 109+710^9 + 7.

Constraints

  • 1≤T≤101 \le T \le 10
  • 3≤N≤250,0003 \le N \le 250,000
  • 3≤M≤123,4563 \le M \le 123,456
  • For each ii with 1≤i≤N1 \le i \le N: 0≤Vi≤1090 \le V_i \le 10^9
  • For each jj with 1≤j≤M1 \le j \le M: Sj∈{1,2,3,4,5}S_j \in \{1, 2, 3, 4, 5\}. If Sj=3S_j = 3, then Xj=0X_j = 0. If Sj≠3S_j \neq 3, then 1≤Xj≤N1 \le X_j \le N. If Sj≠4S_j \neq 4, then Yj=0Y_j = 0. If Sj=4S_j = 4, then 1≤Yj≤1091 \le Y_j \le 10^9.
  • For each jj with 1≤j≤M1 \le j \le M: 1≤Vj≤1091 \le V_j \le 10^9
  • At least one element of SS equals 55. So the output array OO is never empty.

Examples1

  1. Example 1

    Input
    3
    7 9
    2 0 2 4 9 9 9
    1 4 5 5 3 5 2 4 5
    2 4 3 5 0 3 2 4 3
    0 3 0 0 0 0 0 2 0
    6 9
    2 0 2 4 0 0
    1 4 5 5 3 5 2 4 5
    2 4 3 5 0 3 2 4 3
    0 3 0 0 0 0 0 2 0
    6 5
    3 1 4 1 5 9
    1 2 3 4 5
    1 3 0 4 4
    0 0 0 1 0
    
    Expected output
    24 45 7 30
    15 20 7 21
    15