PHƯỚC HƯNG OJ · THỬ THÁCH THUẬT TOÁN

#SRD0000043. Marisa is happy

HỌC THEO LỘ TRÌNH

Bài tập thuộc lộ trình

1 lộ trình

Marisa is happy

Marisa is happy

Source: MarisaOJ

Version: Phuoc Hung OJ Extended

Problem Statement

You are given a permutation p1,…,pnp_1,\ldots,p_n of 1,…,n1,\ldots,n and an array a1,…,ana_1,\ldots,a_n initially containing all zeros. The same array is accessed either in natural index order or in permutation order. Process:

  • 0 l r x: add xx to al,…,ara_l,\ldots,a_r.
  • 1 l r x: add xx to apl,…,apra_{p_l},\ldots,a_{p_r}.
  • 2 l r: output al+⋯+ara_l+\cdots+a_r.
  • 3 l r: output apl+⋯+apra_{p_l}+\cdots+a_{p_r}.

Both coordinate systems access one shared array, so updates in either order affect both query types.

Input

The first line contains n,qn,q, the next a permutation of 1..n1..n, then exactly qq operation lines of the four types above.

Output

Print one 64-bit sum for each operation of type 2 or 3.

Subtasks

  • Subtask 1 (15 points): The limits are n,q≤1000n,q\le1000; every operation type is permitted.
  • Subtask 2 (15 points): Only operation types 1 and 3 occur; 1≤n,q≤1051\le n,q\le10^5.
  • Subtask 3 (15 points): At most 2 operations of type 2 or 3 combined occur in the whole input; 1≤n,q≤1051\le n,q\le10^5.
  • Subtask 4 (15 points): For each operation type, all its appearances use the same pair (l,r)(l,r); 1≤n,q≤1051\le n,q\le10^5.
  • Subtask 5 (40 points): Queries can be of any of the four types; 1≤n,q≤1051\le n,q\le10^5.

Examples

Input

5 6
3 1 5 4 2
0 2 4 2
1 2 4 3
2 3 4
0 2 5 1
3 2 4
3 1 3

Output

7
13
10

Explanation

For p=[3,1,5,4,2]p=[3,1,5,4,2], the first update adds 22 to natural indices 2..42..4, and the next adds 33 at indices p2,p3,p4=[1,5,4]p_2,p_3,p_4=[1,5,4]. The natural sum on positions 3..43..4 becomes 77. Subsequent operations produce permutation-order sums 1313 and 1010.