This page is still under construction.

Parts of this page are still being built. What you see may change.

AddK

Time limit2sMemory limit1024 MB

Summary
Maintain an array under queries that circularly permute K chosen positions and queries that sum all length-m windows inside a range.
Level

Medium7 of 10

Topics
Array, Prefix sum, Math, Implementation
Solved
No attempts yet

Problem

You are given an array AA of NN integers A1,…,ANA_1, \dots, A_N and an integer KK. You must process QQ queries of the following two types:

  • 1 i1 i2 … iK1 \, i_1 \, i_2 \, \dots \, i_K: circularly permute Ai1,…,AiKA_{i_1}, \dots, A_{i_K} to the left. That is, the new values of Ai1,Ai2,…,AiK−1,AiKA_{i_1}, A_{i_2}, \dots, A_{i_{K-1}}, A_{i_K} are Ai2,Ai3,…,AiK,Ai1A_{i_2}, A_{i_3}, \dots, A_{i_K}, A_{i_1}. The indices i1,…,iKi_1, \dots, i_K are distinct and not necessarily in increasing order.
  • 2 l r m2 \, l \, r \, m: sum the elements of all contiguous subsequences of length mm from the sequence Al,Al+1,…,Ar−1,ArA_l, A_{l+1}, \dots, A_{r-1}, A_r. An element that appears in multiple subsequences is added once for each subsequence it appears in.

Input

The first line contains two integers, NN and KK. The second line contains NN integers: the elements of array AA. The third line contains an integer QQ, the number of queries, and the next QQ lines contain the queries, each of one of the two types described above.

Output

Output the answer to each type 2 query, one per line.

Constraints

  • 0≤Ai≤1060 \le A_i \le 10^6
  • 1≤l≤r≤N1 \le l \le r \le N
  • 1≤m≤r−l+11 \le m \le r - l + 1

Hint

The first query is of type 2, and we must sum the elements of all contiguous subsequences of length m=4m = 4 from the sequence (2,5,1,9,3,4)(2, 5, 1, 9, 3, 4). Those subsequences are (2,5,1,9)(2, 5, 1, 9), (5,1,9,3)(5, 1, 9, 3), (1,9,3,4)(1, 9, 3, 4), and the sum of their elements is 52.

The second query is of type 1, and it circularly permutes the elements of array AA at indices 22, 55, 88. Array AA becomes (7,9,5,1,6,3,4,2)(7, 9, 5, 1, 6, 3, 4, 2).

The third query is of type 2, and we must sum the elements of all contiguous subsequences of length m=3m = 3 from the sequence (9,5,1,6,3,4)(9, 5, 1, 6, 3, 4). Those subsequences are (9,5,1)(9, 5, 1), (5,1,6)(5, 1, 6), (1,6,3)(1, 6, 3), (6,3,4)(6, 3, 4), and the sum of their elements is 5050.

Examples1

  1. Example 1

    Input
    8 3
    7 2 5 1 9 3 4 6
    3
    2 2 7 4
    1 2 5 8
    2 2 7 3
    
    Expected output
    52
    50