Under construction

Still being built. Things may change

Sequence Rotation and Queries

Interview

Time limit1sMemory limit1024 MB

Summary
Given an array with right and left rotation queries, answer range sum queries over positions a to b after each rotation.
Level

Medium4 of 10

Topics
Array, Prefix sum, Implementation
Solved
No attempts yet

Problem

An integer sequence A1,A2,…,ANA_1, A_2, \dots, A_N of length NN is given. Write a program that handles the following queries.

  • 11 kk: Rotate the sequence to the right by kk. The value of A1A_1 becomes AN−k+1A_{N-k+1}, A2A_2 becomes AN−k+2A_{N-k+2}, …\dots, AkA_k becomes ANA_N, Ak+1A_{k+1} becomes A1A_1, Ak+2A_{k+2} becomes A2A_2, …\dots, and ANA_N becomes AN−kA_{N-k}. All values change at the same time.
  • 22 kk: Rotate the sequence to the left by kk. The value of A1A_1 becomes Ak+1A_{k+1}, A2A_2 becomes Ak+2A_{k+2}, …\dots, AN−kA_{N-k} becomes ANA_N, AN−k+1A_{N-k+1} becomes A1A_1, AN−k+2A_{N-k+2} becomes A2A_2, …\dots, and ANA_N becomes AkA_k. All values change at the same time.
  • 33 aa bb: Print the sum of the aa-th through bb-th numbers, that is, ∑i=abAi\sum_{i=a}^b A_i.

Input

The first line contains NN and QQ, separated by a space. The second line contains NN integers A1,A2,…,ANA_1, A_2, \dots, A_N, separated by spaces. The next QQ lines each contain one query.

Output

For each type 3 query, print the result on its own line, in the order the queries are given.

Constraints

  • 2≤N≤200 0002 \le N \le 200\,000
  • 1≤Q≤200 0001 \le Q \le 200\,000
  • 1≤Ai≤1091 \le A_i \le 10^9
  • 1≤k≤N1 \le k \le N
  • 1≤a≤b≤N1 \le a \le b \le N
  • Every number in the input is an integer.
  • At least one type 3 query is given.

Examples1

  1. Example 1

    Input
    7 5
    3 2 1 5 10 6 2
    3 1 3
    2 1
    3 1 3
    1 3
    3 2 6
    
    Expected output
    6
    8
    13