fork download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. long long a[(int)1e5 + 50];
  5. int n;
  6.  
  7. void buildPrefixSum() { // O(n)
  8. for(int i = 1; i < n; i++) {
  9. a[i] += a[i - 1];
  10. }
  11.  
  12. }
  13.  
  14. int getSum(int i, int j) { // O(1)
  15. int sum = a[j];
  16. if(i > 0) sum -= a[i - 1];
  17. return sum;
  18. }
  19.  
  20. int main()
  21. {
  22. ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
  23. int t,a,b;
  24. cin >> n;
  25. for(int i = 0; i < n; i++)
  26. {
  27. cin >> a[i];
  28. }
  29. buildPrefixSum();
  30. cin >> t;
  31. while(t--)
  32. {
  33. cin >> a >> b;
  34. cout << getSum(a,b) << "\n";
  35. }
  36. }
  37.  
  38.  
  39.  
  40.  
Compilation error #stdin compilation error #stdout 0s 4312KB
stdin
Standard input is empty
compilation info
prog.cpp: In function ‘int main()’:
prog.cpp:27:13: error: invalid types ‘int[int]’ for array subscript
   cin >> a[i];
             ^
stdout
Standard output is empty