Codeforces Round #248 (Div. 2) —— B

B. Kuriyama Mirai's Stones
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

Kuriyama Mirai has killed many monsters and got many (namely n) stones. She numbers the stones from 1 to n. The cost of the i-th stone is vi. Kuriyama Mirai wants to know something about these stones so she will ask you two kinds of questions:

  1. She will tell you two numbers, l and r (1 ≤ l ≤ r ≤ n), and you should tell her .
  2. Let ui be the cost of the i-th cheapest stone (the cost that will be on the i-th place if we arrange all the stone costs in non-decreasing order). This time she will tell you two numbers, l and r (1 ≤ l ≤ r ≤ n), and you should tell her .

For every question you should give the correct answer, or Kuriyama Mirai will say "fuyukai desu" and then become unhappy.

Input

The first line contains an integer n (1 ≤ n ≤ 105). The second line contains n integers: v1, v2, ..., vn (1 ≤ vi ≤ 109) — costs of the stones.

The third line contains an integer m (1 ≤ m ≤ 105) — the number of Kuriyama Mirai's questions. Then follow m lines, each line contains three integers typel and r (1 ≤ l ≤ r ≤ n; 1 ≤ type ≤ 2), describing a question. If type equal to 1, then you should output the answer for the first question, else you should output the answer for the second one.

Output

Print m lines. Each line must contain an integer — the answer to Kuriyama Mirai's question. Print the answers to the questions in the order of input.

Sample test(s)
input
6
6 4 2 7 2 7
3
2 3 6
1 3 4
1 1 6
output
24
9
28
input
4
5 5 2 3
10
1 2 4
2 1 4
1 1 1
2 1 4
2 1 2
1 1 1
1 3 3
1 1 3
1 4 4
1 2 2
output
10
15
5
15
5
5
2
12
3
5

Note

Please note that the answers to the questions may overflow 32-bit integer type.

 

题意:求序列 l~r 的和。还要求排序后序列 l~r 的和。

 

一开始我无脑地求,结果超时。后来想想可以用树状数组,但也有点麻烦,所以可以用数组储存和,然后用 r 的和减 l 的和即可,有点类似树状数组的思想。

#include<bits/stdc++.h>
using namespace std;
int main()
{
    __int64 n, m, i, v[100010], u[100010];

    while(~scanf("%I64d", &n))
    {
        __int64 sum = 0;
        for(i=1; i<=n; i++)
        {
            scanf("%I64d", v+i);
            u[i] = v[i];
            sum += v[i];
            v[i] = sum;
        }
        sum = 0;
        sort(u+1,u+n+1);
        for(i=1; i<=n; i++)
        {
            sum += u[i];
            u[i] = sum;
        }
        __int64 t, l, r;
        scanf("%I64d", &m);
        while(m--)
        {
            scanf("%I64d%I64d%I64d", &t,&l,&r);
            l--;
            if(t == 1) printf("%I64d\n", v[r]-v[l]);
            else printf("%I64d\n", u[r]-u[l]);
        }
    }
    return 0;
}


 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值