fork download
  1. using namespace std;
  2. #include <bits/stdc++.h>
  3. #define mapii map<int, int>
  4. #define debug(a) cout << #a << ": " << a << endl
  5. #define fdto(i, r, l) for(int i = r; i >= l; --i)
  6. #define fto(i, l, r) for(int i = l; i <= r; ++i)
  7. #define forit(it, type, var) for(type::iterator it = var.begin(); it != var.end(); it++)
  8. #define ii pair<int, int>
  9. #define iii pair<int, ii>
  10. #define fi first
  11. #define se second
  12. #define mp make_pair
  13. #define pb push_back
  14. #define ll long long
  15. #define maxN 200005
  16.  
  17. int n, q, a[maxN];
  18. ll d[maxN];
  19.  
  20. int main () {
  21. #ifndef ONLINE_JUDGE
  22. freopen("input.txt", "r", stdin);
  23. //freopen("output.txt", "w", stdout);
  24. #endif // ONLINE_JUDGE
  25.  
  26. scanf("%d%d", &n, &q);
  27. fto(i, 1, n) scanf("%d", &a[i]);
  28.  
  29. fto(i, 1, n)
  30. if (i%2 == 1) d[i] = d[i-1]+a[i];
  31. else d[i] = d[i-1]-a[i];
  32.  
  33. fto(i, 1, q) {
  34. int l, r;
  35. scanf("%d%d", &l, &r);
  36. printf("%I64d\n", (l%2 == 1) ? d[r]-d[l-1] : d[l-1]-d[r]);
  37. }
  38.  
  39. return 0;
  40. }
  41.  
Success #stdin #stdout 0s 5804KB
stdin
5 4
1 2 3 4 5
1 2
1 3
2 3
1 5
stdout
                                                              -1
                                                               2
                                                              -1
                                                               3