Ants

Time limit3sMemory limit128 MB

Summary
Given a forest of towns formed by persistent range-add copies of parent towns, answer range-sum queries on each newly created version using online, XOR-derived parameters that depend on previous answers.
Level

Hard8 of 10

Topics
Segment tree, Prefix sum, Tree, Binary search
Solved
No attempts yet

Problem

Ant-land keeps expanding, and new towns are founded like this: when a town becomes overcrowded, some of its residents leave and found a new town (some ants always stay behind in the old town). At the very start, Ant-land has just one town.

By ancient tradition, every town must contain exactly MM anthills, numbered 11 through MM, and each anthill holds a known number of ants. When a new town is founded, all MM of its anthills are built at once. Out of respect for tradition, the founders model the new town on the town they left: anthill kk in the new town starts with the same capacity as anthill kk in the old town, for every kk.

Some innovator ants, however, tweak the plan. At the moment a new town is founded, the chieftains order that the capacities of anthills LL through RR (inclusive) each be increased by the same amount VV.

Once the new town's anthills are built, its prestigious quarter turns out to consist of all anthills numbered ii through jj (inclusive), and the chieftains ask: how many ants can the prestigious quarter hold in total?

Write a program that answers this question every time a new town is founded.

Input

The first line contains two integers NN and MM: NN is the total number of towns in Ant-land after every new town has been founded, and MM is the number of anthills in each town.

The second line contains MM integers A1,A2,…,AMA_1, A_2, \ldots, A_M, where AiA_i is the capacity of anthill ii in the first town.

Each of the next N−1N-1 lines describes the founding of one new town with six integers P,X,Y,V,Z,TP, X, Y, V, Z, T:

  • PP is the index of the town the new town is founded from. The first town has index 11. Each newly founded town receives the smallest positive integer not yet used as a town index (so towns are indexed 2,3,…,N2, 3, \ldots, N in the order they are founded).
  • The values L,R,i,jL, R, i, j are derived from a running value SS:
    • L=((X+S) mod M)+1L = ((X + S) \bmod M) + 1
    • R=((Y+S) mod M)+1R = ((Y + S) \bmod M) + 1
    • i=((Z+S) mod M)+1i = ((Z + S) \bmod M) + 1
    • j=((T+S) mod M)+1j = ((T + S) \bmod M) + 1

SS starts at 00. After a new town is founded, SS is set to that town's answer (the total capacity of its prestigious quarter, anthills ii through jj), and this updated SS is used when deriving the parameters of the next founding. It is guaranteed that L≤RL \le R and i≤ji \le j for every town.

Output

For each newly founded town, print a single integer on its own line: the total number of ants its prestigious quarter can hold.

Constraints

  • 1≤N,M≤1000001 \le N, M \le 100000
  • 0≤Ai≤1000000 \le A_i \le 100000 for every ii, and 0≤V≤1000000 \le V \le 100000 for every founding
  • 1≤L≤R≤M1 \le L \le R \le M for every newly founded town
  • 1≤i≤j≤M1 \le i \le j \le M for every newly founded town
  • 0≤X,Y,Z,T<M0 \le X, Y, Z, T < M

Note

Worked example (this matches the first sample). Town 11 has capacities {3,6,7,5}\{3, 6, 7, 5\}.

  • Town 2, founded from town 11: S=0S = 0, so L=3,R=4,V=1,i=1,j=2L = 3, R = 4, V = 1, i = 1, j = 2. Adding 11 to anthills 33–44 gives {3,6,8,6}\{3, 6, 8, 6\}. The prestigious quarter (anthills 11–22) holds 3+6=93 + 6 = 9, so the answer is 99 and SS becomes 99.
  • Town 3, founded from town 22: with S=9S = 9, L=((1+9) mod 4)+1=3,R=4,i=((2+9) mod 4)+1=4,j=4,V=6L = ((1+9) \bmod 4)+1 = 3, R = 4, i = ((2+9) \bmod 4)+1 = 4, j = 4, V = 6. Starting from town 22's {3,6,8,6}\{3, 6, 8, 6\} and adding 66 to anthills 33–44 gives {3,6,14,12}\{3, 6, 14, 12\}. The prestigious quarter (anthill 44) holds 1212, so the answer is 1212 and SS becomes 1212.
  • Town 4, founded from town 11: with S=12S = 12, L=1,R=3,i=1,j=4,V=8L = 1, R = 3, i = 1, j = 4, V = 8. Starting from town 11's {3,6,7,5}\{3, 6, 7, 5\} and adding 88 to anthills 11–33 gives {11,14,15,5}\{11, 14, 15, 5\}. The prestigious quarter (anthills 11–44) holds 11+14+15+5=4511 + 14 + 15 + 5 = 45, so the answer is 4545.

Each new town is an independent copy of the town it was founded from, so a founding never changes the capacities of any earlier town.

Examples3

  1. Example 1

    Input
    4 4
    3 6 7 5
    1 2 3 1 0 1
    2 1 2 6 2 2
    1 0 2 8 0 3
    
    Expected output
    9
    12
    45
    
  2. Example 2

    Input
    2 1
    17611
    1 0 0 61898 0 0
    
    Expected output
    79509
    
  3. Example 3

    Input
    8 6
    31190 77678 71333 17094 48490 79157
    1 5 5 61503 4 4
    2 0 0 70906 3 0
    3 0 1 30398 2 2
    2 2 3 88003 3 3
    1 4 2 4064 3 5
    3 2 4 93602 4 4
    7 5 0 17583 1 1
    
    Expected output
    48490
    285501
    140660
    228663
    188329
    234262
    234262