fork download
  1. #include <bits/stdc++.h>
  2. #define MOD 1000000007
  3. #define Tran_Hoan_Thien ios::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL)
  4. #define fi first
  5. #define se second
  6. #define pb push_back
  7. #define vi vector<int>
  8. #define ll long long
  9. #define vll vector<ll>
  10. #define db double
  11. #define pii pair<int,int>
  12. using namespace std;
  13. const int MAXN = 2e5 + 5;
  14. const int MAXH = 405; // vì h[i] ≤ 400
  15.  
  16. int n, q;
  17. int h[MAXN];
  18. int diff[MAXN];
  19. int sumDiff[MAXH];
  20. vector<int> pos[MAXH];
  21. int main() {
  22. Tran_Hoan_Thien;
  23. cin >> n >> q;
  24. for (int i = 1; i <= n; ++i) {
  25. cin >> h[i];
  26. pos[h[i]].push_back(i);
  27. }
  28. for (int i = 1; i < n; ++i) {
  29. diff[i] = abs(h[i] - h[i + 1]);
  30. }
  31. while (q--) {
  32. int L, R;
  33. cin >> L >> R;
  34. int res = 0;
  35. for (int i = 1; i < n; ++i) {
  36. if (h[i] >= L && h[i] <= R && h[i + 1] >= L && h[i + 1] <= R) {
  37. res += abs(h[i] - h[i + 1]);
  38. }
  39. }
  40. cout << res << '\n';
  41. }
  42. return 0;
  43. }
  44.  
Success #stdin #stdout 0.01s 5288KB
stdin
Standard input is empty
stdout
Standard output is empty