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