#P8165. [eJOI 2021] AddK

[eJOI 2021] AddK

Problem Description

You are given an array AA containing NN integers A1,,ANA_1,\dots,A_N and an integer KK. You need to perform QQ operations of the following two types:

  • 11 i1i_1 i2i_2 \dots iKi_K: Replace the values of Ai1,Ai2,,AiK1,AiKA_{i_1},A_{i_2},\dots,A_{i_{K-1}},A_{i_K} in order with the values of Ai2,Ai3,,AiK,Ai1A_{i_2},A_{i_3},\dots,A_{i_K},A_{i_1}. Here i1,,iki_1,\dots,i_k are pairwise distinct.
  • 22 ll rr mm: Output the sum of elements over all consecutive subsequences of length mm within the interval [l,r][l,r].

Input Format

The first line contains two integers N,KN,K.

The second line contains NN integers, representing the NN integers of array AA.

The third line contains one integer QQ, representing the number of operations.

The next QQ lines each contain several integers, describing one operation.

Output Format

Output several lines of integers, which are the results of all type 22 operations.

8 3
7 2 5 1 9 3 4 6
3
2 2 7 4
1 2 5 8
2 2 7 3
52
50

Hint

Sample Explanation

For the first query, you need to compute the sum of elements over all consecutive subsequences of length 44 in {2,5,1,9,3,4}\{2,5,1,9,3,4\}. These subsequences are {2,5,1,9},{5,1,9,3},{1,9,3,4}\{2,5,1,9\},\{5,1,9,3\},\{1,9,3,4\}. Therefore the answer is 5252.

For the second query, you need to replace A2,A5,A8A_2,A_5,A_8 in order with the values of A5,A8,A2A_5,A_8,A_2. After the replacement, the array becomes {7,9,5,1,6,3,4,2}\{7,9,5,1,6,3,4,2\}.

For the third query, you need to compute the sum of elements over all consecutive subsequences of length 33 in {9,5,1,6,3,4}\{9,5,1,6,3,4\}. These subsequences are {9,5,1},{5,1,6},{1,6,3},{6,3,4}\{9,5,1\},\{5,1,6\},\{1,6,3\},\{6,3,4\}. Therefore the answer is 5050.

Constraints

This problem uses bundled testdata.

  • Subtask 1 (36 pts): 1N,Q1041 \le N,Q \le 10^4 and K=1K=1.
  • Subtask 2 (56 pts): 10001N,Q10510001 \le N,Q \le 10^5 and K=1K=1.
  • Subtask 3 (8 pts): 1N,Q1051 \le N,Q \le 10^5 and 2K102 \le K \le 10.

For 100%100\% of the testdata, 0Ai1060 \le A_i \le 10^6, 1lrN1 \le l \le r \le N, 1mrl+11 \le m \le r-l+1.

Notes

This problem is translated from eJOI2021 Day 1 A AddK.

Translated by ChatGPT 5