fork download
  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5. int n, q, a[int(1e5+3)];
  6. int64_t ps[int(1e5+3)], ans;
  7. int l, r;
  8.  
  9. int main()
  10. {
  11. cin.tie(0) -> sync_with_stdio(0);
  12.  
  13. cin >> n >> q;
  14. for(int i=1; i<=n; ++i){
  15. cin >> a[i];
  16. ps[i] = ps[i-1] + a[i];
  17. }
  18. while(q--){
  19. cin >> l >> r;
  20. ans = 0;
  21. for(int i=l; i<=r; ++i)
  22. ans += a[i]*(ps[i-1]-ps[l-1]);
  23. cout << ans << '\n';
  24. }
  25.  
  26. return 0;
  27. }
Success #stdin #stdout 0s 5304KB
stdin
4 3
1 2 3 4
1 2
1 3
2 3
stdout
2
11
6