Algorithm
Code Examples
#1 Code Example with C++ Programming
Code -
C++ Programming
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int const N = 1e5 + 1;
long long scum[N], uscum[N];
vector<int> arr;
int main() {
int n;
cin >> n;
arr.resize(n);
for(int i = 0; i < n; i++) cin >> arr[i];
uscum[0] = 0;
for(int i = 1; i <= n; i++)
uscum[i] = uscum[i-1] + arr[i-1];
sort(arr.begin(), arr.end());
scum[0] = 0;
for(int i = 1; i <= n; i++)
scum[i] = scum[i-1] + arr[i-1];
int q;
cin >> q;
int t, a, b;
while(q--) {
cin >> t >> a >> b;
if(t == 1)
cout << uscum[b] - uscum[a-1] << endl;
else
cout << scum[b] - scum[a-1] << endl;
}
}
Copy The Code &
Try With Live Editor
Input
6
6 4 2 7 2 7
3
2 3 6
1 3 4
1 1 6
6 4 2 7 2 7
3
2 3 6
1 3 4
1 1 6
Output
24
9
28
9
28
Demonstration
Codeforces Solution-Kuriyama Mirai's Stones-Solution in C, C++, Java, Python