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

来源:互联网 发布:图标设计图案软件 编辑:程序博客网 时间:2024/05/16 17:45
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 type, l 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
66 4 2 7 2 732 3 61 3 41 1 6
output
24928
input
45 5 2 3101 2 42 1 41 1 12 1 42 1 21 1 11 3 31 1 31 4 41 2 2
output
10155155521235

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;}


 

0 0
原创粉丝点击