fork download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. typedef long long ll;
  4.  
  5. const int MAXN = 1e6 + 5;
  6.  
  7. int n, q;
  8. vector<ll> a(MAXN, 0);
  9. vector<ll> Prefix(MAXN, 0);
  10. vector<ll> Square(MAXN, 0);
  11.  
  12. void fastip() {
  13. ios_base::sync_with_stdio(0);
  14. cin.tie(0); cout.tie(0);
  15. }
  16.  
  17. int main() {
  18. fastip();
  19. cin >> n >> q;
  20. for (int i = 1; i <= n; i++) {
  21. cin >> a[i];
  22. Prefix[i] = Prefix[i-1] + a[i];
  23. Square[i] = Square[i-1] + 1ll * a[i] * a[i];
  24. }
  25.  
  26. while (q--) {
  27. int u, v;
  28. cin >> u >> v;
  29. ll S = Prefix[v] - Prefix[u-1];
  30. ll Sq = Square[v] - Square[u-1];
  31. ll ans = (S * S - Sq) / 2;
  32. cout << ans << '\n';
  33. }
  34. return 0;
  35. }
Success #stdin #stdout 0.01s 26448KB
stdin
Standard input is empty
stdout
Standard output is empty