AddK
Time limit2sMemory limit1024 MB
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 of integers and an integer . You must process queries of the following two types:
- : circularly permute to the left. That is, the new values of are . The indices are distinct and not necessarily in increasing order.
- : sum the elements of all contiguous subsequences of length from the sequence . An element that appears in multiple subsequences is added once for each subsequence it appears in.
Input
The first line contains two integers, and . The second line contains integers: the elements of array . The third line contains an integer , the number of queries, and the next 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
Hint
The first query is of type 2, and we must sum the elements of all contiguous subsequences of length from the sequence . Those subsequences are , , , and the sum of their elements is 52.
The second query is of type 1, and it circularly permutes the elements of array at indices , , . Array becomes .
The third query is of type 2, and we must sum the elements of all contiguous subsequences of length from the sequence . Those subsequences are , , , , and the sum of their elements is .