fork download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. int main() {
  5. // your code goes here
  6. int n;
  7. cin>>n;
  8.  
  9. vector<int> values(n);
  10. for(int i = 0; i < n; i++){
  11. cin>>values[i];
  12. }
  13.  
  14. vector<long long> psum(n);
  15. psum[0] = values[0];
  16.  
  17. for(int i = 1; i <= n-1; i++){
  18. psum[i] = psum[i-1] + values[i];
  19. }
  20.  
  21. // Read queries
  22. int q;
  23. cin>> q;
  24.  
  25. while(q--){
  26. int l,r;
  27. cin>>l>>r;
  28. long long ans = 0;
  29. if(l == 0){
  30. ans = psum[r];
  31. }
  32. else{
  33. ans = psum[r] - psum[l-1];
  34. }
  35. cout<<ans<<endl;
  36. }
  37. return 0;
  38. }
Success #stdin #stdout 0.01s 5288KB
stdin
5
10 20 30 40 50
4
0 2
1 4
2 2
3 4
stdout
60
140
30
90