fork download
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. int main() {
  5. // your code goes here
  6. int n , q;
  7. cin>>n;
  8. cin>>q;
  9. int arr[n];
  10. for(int i = 0 ; i< n ; i++){
  11. cin>> arr[i];
  12. }
  13. int p[n];
  14. p[0] = arr[0];
  15. for(int i = 1; i<n ; i++){
  16. p[i] = p[i-1] + arr[i];
  17. }
  18. while(q--){
  19. int l , r;
  20. cin>>l>>r;
  21. int sum = 0;
  22. if(l==0){
  23. sum = p[r];
  24. }
  25. else {
  26. sum = p[r] + p[l-1];
  27. }
  28. cout<<sum<<endl;
  29. }
  30. return 0;
  31. }
Success #stdin #stdout 0s 5320KB
stdin
8 3
5 8 7 6 5 2 1 1
0 2
0 3
3 5
stdout
20
26
53