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. int n, q;
  15. int h[MAXN];
  16. int h_comp[MAXN];
  17. vi v;
  18. int main() {
  19. Tran_Hoan_Thien;
  20. cin >> n >> q;
  21. for (int i = 1; i <= n; ++i) {
  22. cin >> h[i];
  23. v.push_back(h[i]);
  24. }
  25. sort(v.begin(), v.end());
  26. v.erase(unique(v.begin(), v.end()), v.end());
  27. for (int i = 1; i <= n; ++i) {
  28. h_comp[i] = lower_bound(v.begin(), v.end(), h[i]) - v.begin() + 1;
  29. }
  30. while (q--) {
  31. int x, y;
  32. cin >> x >> y;
  33. int l = lower_bound(v.begin(), v.end(), x) - v.begin() + 1;
  34. int r = upper_bound(v.begin(), v.end(), y) - v.begin();
  35.  
  36. if (l > r) {
  37. cout << 0 << '\n';
  38. continue;
  39. }
  40. int prev = -1, res = 0;
  41. for (int i = 1; i <= n; ++i) {
  42. if (h_comp[i] >= l && h_comp[i] <= r) {
  43. if (prev != -1) {
  44. res += abs(h[i] - h[prev]);
  45. }
  46. prev = i;
  47. }
  48. }
  49. cout << res << '\n';
  50. }
  51.  
  52. return 0;
  53. }
  54.  
Success #stdin #stdout 0s 5320KB
stdin
Standard input is empty
stdout
Standard output is empty