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
output
input
output
Note
Please note that the answers to the questions may overflow 32-bit integer type.
题意:求序列 l~r 的和。还要求排序后序列 l~r 的和。
一开始我无脑地求,结果超时。后来想想可以用树状数组,但也有点麻烦,所以可以用数组储存和,然后用 r 的和减 l 的和即可,有点类似树状数组的思想。
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; }
相关文章推荐
- Codeforces Round #248 (Div. 2) C - Ryouko's Memory Note
- Codeforces Round #248 (Div. 2) B称号 【数据结构:树状数组】
- Codeforces Round #248 (Div. 2) C. Ryouko's Memory Note
- Codeforces Round #248 (Div. 2)
- Codeforces Round #248 (Div. 2) C. Ryouko's Memory Note
- Codeforces Round #172 (Div. 2) B. Nearest Fraction
- Codeforces Round #403 div2 B. The Meeting Place Cannot Be Changed
- Codeforces Round #368 (Div 2) A,B,C,D,E
- codeforces Technocup 2017 - Elimination Round 2/Codeforces Round #380 (Div. 2, Rated, Based on Technocup 2017 - Elimination Round 2) 题解
- Codeforces Round #172 (Div. 2)
- Codeforces Round #415 (Div. 2) A B C 暴力 sort 规律
- Codeforces Round #461 (Div. 2) D. Robot Vacuum Cleaner(贪心)
- Codeforces Round #324 (Div. 2) 584A. Olesya and Rodion
- Codeforces Round #430 (Div. 2) D. Vitya and Strange Lesson [trie]
- Codeforces Round #459 (Div. 2)
- Codeforces Round #305 (Div. 2)--A. Mike and Fax
- Codeforces Round #354 (Div. 2) C (二分???)
- 【Codeforces Round #476 (Div. 2) [Thanks, Telegram!] B】Battleship
- Codeforces Round #324 (Div. 2)——C. Marina and Vasya
- Codeforces Round #342 (Div. 2) [Codeforces625]