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.

Examples
input

Copy
6
6 4 2 7 2 7
3
2 3 6
1 3 4
1 1 6
output

Copy
24
9
28
input

Copy
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

Copy
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.


刚开始没有注意下面的Note,用int类型的数组WA了。后来改用long long型。我之前也没去算过OJ可以接受多大的数组,就这道AC的代码来看4W的long long是没有问题的。

#include <iostream>
#include <vector>
#include <algorithm>
#include <string>
#include <set>
#include <queue>
#include <map>
#include <sstream>
#include <cstdio>
#include <cstring>
#include <numeric>
#include <cmath>
#include<unordered_set>
#define ll long long
using namespace std;
int dir[][] = { {,},{-,},{,},{,-} }; int main()
{
int n;
cin >> n;
vector<ll> v(n),a(n);
for (int i = ; i < n; i++)
{
int t;
cin >> t;
v[i] = t;
a[i] = t;
}
sort(a.begin(), a.end());
for (int i = ; i < n; i++)
{
v[i] += v[i - ];
a[i] += a[i - ];
} int m;
cin >> m;
while (m--)
{
int q, l, r;
cin >> q >> l >> r;
if (q == )
{
if (l == )
cout << v[r - ] << endl;
else
cout << v[r - ] - v[l - ] << endl;
}
else
{
if (l == )
cout << a[r - ] << endl;
else
cout << a[r - ] - a[l - ] << endl;
}
}
//system("pause");
return ;
}

433B.Kuriyama Mirai's Stones的更多相关文章

  1. 动态规划,而已! CodeForces 433B - Kuriyama Mirai&#39;s Stones

    Kuriyama Mirai has killed many monsters and got many (namely n) stones. She numbers the stones from  ...

  2. Codeforces Round #248 (Div. 2) B. Kuriyama Mirai's Stones

    题目简单描述就是求数组中[l,r]区间的和 #include <iostream> #include <vector> #include <string> #inc ...

  3. CF433B Kuriyama Mirai's Stones 题解

    Content 有一个长度为 \(n\) 的数组 \(a_1,a_2,a_3,...,a_n\).有 \(m\) 次询问,询问有以下两种: \(1~l~r\),求 \(\sum\limits_{i=l ...

  4. codeforces433B

    Kuriyama Mirai's Stones CodeForces - 433B 有n颗宝石,每个宝石都有自己的价值. 然后m次询问.问区间[i,j]的宝石的总值,或者问排序后的区间[i,j]的总值 ...

  5. Codeforces Round #248 (Div. 2) (ABCD解决问题的方法)

    比赛链接:http://codeforces.com/contest/433 A. Kitahara Haruki's Gift time limit per test:1 second memory ...

  6. Anna-senpai帖子翻译与Mirai源代码使用

    Anna-senpai这个人太好玩了,整件事就像没有黄段子的无聊世界那样. 无聊翻译了一下,顺便实验了效果. --------------------------------------------- ...

  7. HDU 5973 Game of Taking Stones 威佐夫博弈+大数

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5973 Game of Taking Stones Time Limit: 2000/1000 MS ...

  8. HDU 4573 Throw the Stones(动态三维凸包)(2013 ACM-ICPC长沙赛区全国邀请赛)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4573 Problem Description Remember our childhood? A fe ...

  9. codechef Jewels and Stones 题解

    Soma is a fashionable girl. She absolutely loves shiny stones that she can put on as jewellery acces ...

随机推荐

  1. Git学习笔记-上传一个新的项目到GitHub上

    前提: 已有Github账号,已在Github上建立了仓库,已在Github上配置了SSH,已上传过一些项目到Github上 目标: 目前有一个新的项目,需要上传到github上 我的做法记录: 1. ...

  2. python 深浅copy总结

    总结: ''' 总结:假设l1为原数据,l2为deepcopy后的数据: 1.浅copy,只能改变第一层的内存地址(不可变数据类型除外). 2.深copy,能够改变第一层和第二层的内存地址(不可变数据 ...

  3. url 加参数

    url = 访问地址 + ? key1=value1 & key2 = value2 304(未修改)自从上次请求后,请求的网页未修改过.服务器返回此响应时,不会返回网页内容. 加参数,骗过服 ...

  4. 装饰器_python

    一.装饰器中提及的知识点 装饰器主要作用:在原函数基础上添加新功能 1.作用域:LEGB 2.高阶函数 3.闭包(在一个内部函数中,对在外部作用域(但不是在全局作用域)的变量进行引用,那么内部函数就认 ...

  5. 10.3lambda表达式笔记

    与参数不同被捕获的变量的值是在lambda创建时拷贝,而不是调用时拷贝 void fcn() { int v1 = 42; //局部变量 auto f = [v1] { return v1; }; a ...

  6. 题解【洛谷P2003】平板

    题面 由于本题中\(n\)很小,\(\Theta(n^2)\)的暴力也可以通过. 具体可参照洛谷题解区 #include <bits/stdc++.h> #define itn int # ...

  7. 移动端安卓IOS对接H5项目遇到的坑

    最近做一个H5项目 退出的时候需要调用原生的弹框退出, 之前以为很简单,直接ajax调用后端退出接口即可.后来发现数据拿不到,因为状态用的是cookie存储, 后来想到ajax 传参数请求,后来还是拿 ...

  8. K3修改字段名

    在K3的BOS中,自定义字段之后我们往往会修改字段名,便于记忆和理解,但是修改字段名之后,只是数据库中的字段名被修改了,BOS中的字段标识并没有被修改,可以通过以下语句将标识和字段名改成一致. sel ...

  9. 【转载】Hive vs HBase

    转自:http://www.chinastor.com/a/hbase/0G3Z532014.html Hive是什么? Apache Hive是一个构建于Hadoop(分布式系统基础架构)顶层的数据 ...

  10. 使用正则提取字符串中URL等信息

    一.说明 背景:最近在做同步京东商品信息时遇到一个问题,同步后的商品详情无法在富文本中修改,强制修改会导致图片无法正常显示,研究发现详情中的图片是在css的作为背景图指定的. 解决:经过多次尝试,最后 ...