您的位置:首页 > 其它

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

2014-08-13 15:06 2121 查看
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:

She will tell you two numbers, l and r (1 ≤ l ≤ r ≤ n),
and you should tell her

.

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
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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: