Bubble Sort Machine

시간 제한2초메모리 제한2048 MB

요약
수열에 왼쪽부터 훑는 버블 정렬 패스를 반복로 적용하면서, 각 시점마다 구간 합을 답한다.
난이도

어려움10점 중 9점

유형
구현, 이분 탐색, 배열, 세그먼트 트리
정답자
아직 제출이 없습니다

문제

JOI-kun, an algorithm researcher, has developed a machine called the Bubble Sort Machine.

The Bubble Sort Machine operates on an integer sequence a=(a_1,a_2,…,a_N)a = (a\_1, a\_2, \dots , a\_N) of length NN. To activate the Bubble Sort Machine, the initial values A_iA\_i are provided as input for each a_ia\_i (1≤i≤N1 ≤ i ≤ N). Each time Button 1 on the Bubble Sort Machine is pressed, the machine modifies the sequence aa in the following way:

  • For each i=1,2,…,N−1i = 1, 2, \dots , N − 1 in order, if a_i>a_i+1a\_i > a\_{i+1}, then the values of a_ia\_i and a_i+1a\_{i+1} are swapped.

To make the Bubble Sort Machine even more appealing, JOI-kun decided to add the following feature:

  • When Button 2 is pressed and integers ll and rr satisfying 1≤l≤r≤N1 ≤ l ≤ r ≤ N are given as input, the machine outputs the value of a_l+a_l+1+⋯+a_ra\_l + a\_{l+1} + \cdots + a\_r.

Given the initial values of the integer sequence and the sequence of operations on the Bubble Sort Machine, write a program that computes the outputs produced by Button 2.

입력

Read the following data from the standard input.

NN

A_1A\_1 A_2A\_2 ⋯\cdots A_NA\_N

QQ

(Query 11)

(Query 22)

⋮\vdots

(Query QQ)

Here, QQ is the number of operations performed on the Bubble Sort Machine. Each (Query jj) (1≤j≤Q1 ≤ j ≤ Q) consists space separated integers. Let T_jT\_j denote the first integer of (Query jj). The content of this line is one of the following.

  • If T_j=1T\_j = 1, this line contains no additional integers. This means that the jj-th operation on the Bubble Sort Machine is pressing Button 1.
  • If T_j=2T\_j = 2, this line contains two more integers, L_jL\_j and R_jR\_j, in that order. This means that the jj-th operation on the Bubble Sort Machine is pressing Button 2 with the integers L_jL\_j and R_jR\_j as input.

출력

For each operation where Button 2 is pressed, that is, for each jj (1≤j≤Q1 ≤ j ≤ Q) such that T_j=2T\_j = 2, output the integer produced by the Bubble Sort Machine on a separate line in the order of the queries.

제한

  • 2≤N≤500,0002 ≤ N ≤ 500\\, 000.
  • 1≤A_i≤1091 ≤ A\_i ≤ 10^9 (1≤i≤N1 ≤ i ≤ N).
  • 1≤Q≤500,0001 ≤ Q ≤ 500\\, 000.
  • T_jT\_j is either 11 or 22 (1≤j≤Q1 ≤ j ≤ Q).
  • If T_j=2T\_j = 2, 1≤L_j≤R_j≤N1 ≤ L\_j ≤ R\_j ≤ N (1≤j≤Q1 ≤ j ≤ Q).
  • Given values are all integers.

예제2

  1. 예제 1

    입력
    4
    5 3 5 2
    6
    2 1 3
    1
    2 1 1
    2 2 4
    1
    2 1 2
    
    예상 출력
    13
    3
    12
    5
    
  2. 예제 2

    입력
    5
    1 1 2 1 2
    5
    2 2 3
    1
    2 2 4
    1
    2 2 4
    
    예상 출력
    3
    4
    4