#K38507. Bell Ring Operations

    ID: 26213 Type: Default 1000ms 256MiB

Bell Ring Operations

Bell Ring Operations

You are given an array representing the frequencies of chimes produced by a bell. You need to perform various operations on this array. The operations are provided as queries and can be one of the following types:

  • update i v: Update the i-th frequency to value v.
  • swap a b c d: Swap the subarray from index a to b with the subarray from index c to d. Note that indices are 1-indexed.
  • reverse a b: Reverse the subarray from index a to b.
  • sum a b: Calculate and output the sum of frequencies from index a to b.

All operations should be processed in the order they are given. Only the results of the "sum" operations are to be printed as output.

The operations that modify the array (update, swap, reverse) affect subsequent queries.

Formally, if you denote the frequencies by \(f_1, f_2, \dots, f_n\), then for a query of type "sum a b", compute:

\[ S = f_a + f_{a+1} + \cdots + f_b \]

inputFormat

The input is read from standard input (stdin) and has the following structure:

  1. A line with two integers n and q separated by space, where n is the number of frequencies and q is the number of queries.
  2. A line with n space-separated integers representing the initial frequencies.
  3. q lines, each containing a query. The queries are in one of the following formats:
    • update i v
    • swap a b c d
    • reverse a b
    • sum a b

outputFormat

For each query of type "sum", output the computed sum on a separate line to standard output (stdout).

## sample
5 7
1 2 3 4 5
sum 1 5
update 3 10
sum 2 4
swap 1 2 4 5
sum 1 5
reverse 1 3
sum 1 5
15

16 22 22

</p>