fork download
  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5. int n, q, x;
  6. int64_t ps[int(1e5+3)], px[int(1e5+3)];
  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 >> x; // a[i]
  16. ps[i] = ps[i-1] + x;
  17. px[i] = px[i-1] + ps[i-1]*x;
  18. }
  19. while(q--){
  20. cin >> l >> r;
  21. cout << (px[r]-px[l-1]) - (ps[r]-ps[l-1])*ps[l-1] << '\n';
  22. }
  23.  
  24. return 0;
  25. }
Success #stdin #stdout 0.01s 5284KB
stdin
4 3
1 2 3 4
1 2
1 3
2 3
stdout
2
11
6