PHƯỚC HƯNG OJ · THỬ THÁCH THUẬT TOÁN

#SRD0000056. Đảo ngược và tổng đoạn (Reversals and Sums)

HỌC THEO LỘ TRÌNH

Bài tập thuộc lộ trình

1 lộ trình

Đảo ngược và tổng đoạn (Reversals and Sums)

Reversals and Sums

Source: VNOJ

Version: Phuoc Hung OJ Extended

Problem Statement

Given a sequence a1,…,ana_1,\ldots,a_n of nonnegative integers, process mm queries on inclusive, one-indexed ranges:

  • 1 l r: reverse the order of elements within [l,r][l,r].
  • 2 l r: print the current sum of elements within [l,r][l,r].

Every reversal affects all subsequent operations; sequence positions are dynamic.

Input

The first line contains n,mn,m; the next line has the nn values. Each subsequent operation is t l r, with t∈{1,2}t\in\{1,2\} and 1≤l≤r≤n1\le l\le r\le n.

Output

Print a sum for each type-2 query.

Subtasks

  • Subtask 1 (10 points): 1≤n≤801\le n\le80; 1≤q≤1001\le q\le100. One-based, inclusive [l,r][l,r] intervals.
  • Subtask 2 (20 points): 1≤n≤30001\le n\le3000; 1≤q≤30001\le q\le3000. One-based, inclusive [l,r][l,r] intervals.
  • Subtask 3 (30 points): 1≤n≤250001\le n\le25000; 1≤q≤250001\le q\le25000. One-based, inclusive [l,r][l,r] intervals.
  • Subtask 4 (40 points): 1≤n≤2000001\le n\le200000; 1≤q≤1000001\le q\le100000. One-based, inclusive [l,r][l,r] intervals.

Examples

Input

8 3
2 1 3 4 5 3 4 4
2 2 4
1 3 6
2 2 4

Output

8
9

Explanation

Initially the array is (2,1,3,4,5,3,4,4)(2,1,3,4,5,3,4,4), so the sum on [2,4][2,4] is 1+3+4=81+3+4=8. Reversing [3,6][3,6] gives (2,1,3,5,4,3,4,4)(2,1,3,5,4,3,4,4), and the next sum on [2,4][2,4] is 1+3+5=91+3+5=9.