fork download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. int main()
  4. {
  5. int m;
  6.  
  7. int n;
  8. int type;
  9. int l, r;
  10. int costs[100001];
  11. int costssorted[100001];
  12. int sumarr[100001], sumsorted[100001];
  13.  
  14. cin >> n;
  15. for (int i = 0; i < n; i++) {
  16. cin >> costs[i]; costssorted[i] = costs[i];
  17. }
  18. cin >> m;
  19. sort(costssorted, costssorted + n);
  20. sumarr[0] = 0; sumsorted[0] = 0;
  21. for (int t = 1; t <= n; t++)
  22. {
  23. sumarr[t] = costs[t - 1] + sumarr[t - 1];
  24. sumsorted[t] = costssorted[t - 1] + sumsorted[t - 1];
  25. }
  26. for (int i = 0; i < m; i ++) {
  27. cin >> type; cin >> l; cin >> r;
  28. if (type == 1) { cout << sumarr[r] - sumarr[l - 1] << endl; }
  29. else if (type == 2) { cout<< sumsorted[r] - sumsorted[l - 1]<<endl; }
  30.  
  31. }
  32.  
  33. }
Success #stdin #stdout 0.01s 5440KB
stdin
Standard input is empty
stdout
Standard output is empty