-
Notifications
You must be signed in to change notification settings - Fork 0
/
Kuriyama Mirai's Stones.cpp
62 lines (57 loc) · 1.71 KB
/
Kuriyama Mirai's Stones.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
/*
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).
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.
*/
#include<bits/stdc++.h>
#define ll unsigned long long
using namespace std;
// using prefix Sum.
int main(){
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
int n, m;
cin >> n;
ll arr[n], sort_arr[n];
for(int i = 0; i < n; i++){
ll temp;
cin >> temp;
arr[i] = temp;
sort_arr[i] = temp;
}
sort(sort_arr, sort_arr + n);
cin >> m;
for(int i = 1; i < n; i++){
arr[i] = arr[i - 1] + arr[i];
sort_arr[i] = sort_arr[i - 1] + sort_arr[i];
}
while(m--){
int t, l, r;
ll sum;
cin >> t >> l >> r;
l--;
r--;
if(t == 1){
if(l - 1 >= 0)
sum = arr[r] - arr[l - 1];
else
sum = arr[r];
cout << sum << endl;
}
else if(t == 2){
if(l - 1 >= 0)
sum = sort_arr[r] - sort_arr[l - 1];
else
sum = sort_arr[r];
cout << sum << endl;
}
}
return 0;
}