AlexNeagu11

Untitled

Feb 23rd, 2024
51
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.72 KB | None | 0 0
  1. #include <fstream>
  2.  
  3. using namespace std;
  4.  
  5. ifstream cin("qtsume.in");
  6. ofstream cout("qtsume.out");
  7.  
  8. const int nax = 100005;
  9. long long a[nax], pre[nax], pre2[nax];
  10.  
  11. int main() {
  12. ios_base::sync_with_stdio(false);
  13. cin.tie(0);
  14.  
  15. int n;
  16. cin >> n;
  17. for (int i = 1; i <= n; ++i) {
  18. cin >> a[i];
  19. }
  20.  
  21. for (int i = 1; i <= n; ++i) {
  22. pre[i] = pre[i - 1] + a[i];
  23. }
  24.  
  25. for (int i = 1; i <= n; ++i) {
  26. pre2[i] = pre2[i - 1] + pre[i];
  27. }
  28.  
  29. int q;
  30. cin >> q;
  31. while (q--) {
  32. int x, y;
  33. cin >> x >> y;
  34. int len = y - x + 1;
  35. cout << len * pre[y] - pre2[y - 1] + ((x - 2 >= 0) ? pre2[x - 2] : 0) << '\n';
  36. }
  37.  
  38. return 0;
  39. }
Advertisement
Add Comment
Please, Sign In to add comment