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