Codeforces Round #248 (Div. 2) —— B
来源:互联网 发布:图标设计图案软件 编辑:程序博客网 时间:2024/05/16 17:45
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.
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.
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.
66 4 2 7 2 732 3 61 3 41 1 6
24928
45 5 2 3101 2 42 1 41 1 12 1 42 1 21 1 11 3 31 1 31 4 41 2 2
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;}
- Codeforces Round #248 (Div. 2) —— B
- Codeforces Round #154 (Div. 2)——B
- Codeforces Round #156 (Div. 2)——B
- Codeforces Round #151 (Div. 2)——B
- Codeforces Round #152 (Div. 2)——B
- Codeforces Round #149 (Div. 2)——B
- Codeforces Round #146 (Div. 2)——B
- Codeforces Round #144 (Div. 2)——B
- Codeforces Round #142 (Div. 2)——B
- Codeforces Round #157 (Div. 2)——B
- Codeforces Round #134 (Div. 2)——B
- Codeforces Round #141 (Div. 2)——B
- Codeforces Round #140 (Div. 2)——B
- Codeforces Round #137 (Div. 2)——B
- Codeforces Round #136 (Div. 2)——B
- Codeforces Round #135 (Div. 2)——B
- Codeforces Round #124 (Div. 2)——B
- Codeforces Round #123 (Div. 2)——B
- GitHub上最火的40个Android开源项目(二)
- 解决ubuntu14.04下Qt 5.3.1 下的QtCreator fcitx,ibus不能输入中文
- Android目录结构
- windows mysql提示:1045 access denied for user 'root'@'localhost' using password ye
- 通过用户id输出头像文件流
- Codeforces Round #248 (Div. 2) —— B
- POJ Who Gets the Most Candies? 线段树 第 5天
- IOS中 NSDictionary(字典)排序
- 在Eclipse中离线安装ADT(Android Development Tools)
- GitHub上最火的74个Android开源项目(三)
- ftp批量下载 与单个下载
- c# Bitmap byte[] Stream 文件相互转换
- StringUtils全览
- android学习----主要数据存储方