Language: C++

#include <bits/stdc++.h>
#define err(args...) {}
#ifdef DEBUG
#include "_debug.cpp"
#endif
using namespace std;
using ll = long long;
using ld = long double;
template <typename T> using lim = numeric_limits<T>;
template <typename T> istream& operator>>(istream& is, vector<T>& a) { for(T& x : a) { is >> x; } return is; }
const int N = 200'000;
template <typename T> struct monoid {
    const T identity;
    const function<T(T, T)> combine;
};
template <typename T> struct segtree {
    using Type = T;
    using DeltaType = T;
    const int size;
    vector<T> tree;
    const monoid<T> m;
    segtree(int size, const monoid<T>& m) : size(size), tree(2 * (size + 1), m.identity), m(m) {}
    void update(int i, T x) {
        for(tree[i += size] = x; i >>= 1; )
            tree[i] = m.combine(tree[i << 1], tree[(i << 1) | 1]);
    }
    T query(int a, int b, bool inclusive) {
        assert(a <= b);
        T ans_l = m.identity, ans_r = m.identity;
        for(a += size, b += size + inclusive; a < b; a >>= 1, b >>= 1) {
            if(a & 1) ans_l = m.combine(ans_l, tree[a++]);
            if(b & 1) ans_r = m.combine(tree[--b], ans_r);
        }
        return m.combine(ans_l, ans_r);
    }
    T query(int i) { return query(i, i, true); }
    friend ostream& operator<<(ostream& os, segtree& t) {
        os << "[";
        for(int i = 0; i <= t.size; i++) {
            if(i != 0) os << ", ";
            os << t.query(i);
        }
        return os << "]";
    }
};
int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    int n, q, a, b;
    cin >> n >> q >> a >> b;
    vector<ll> x(q), s(q);
    cin >> x;
    adjacent_difference(x.begin(), x.end(), s.begin());
    for(ll& d : s) {
        d = abs(d);
    }
    partial_sum(s.begin(), s.end(), s.begin());
    auto cost0 = [&](int i, int j) {
        if(j + 1 == q) {
            return min(abs(a - x[i]), abs(b - x[i]));
        } else {
            return min(abs(a - x[i]) + abs(b - x[j + 1]), abs(b - x[i]) + abs(a - x[j + 1]));
        } 
    };
    segtree<ll> gt_st(N + 1, {N * q + 1, [](int x, int y) { return min(x, y); }});
    segtree<ll> lt_st(N + 1, {N * q + 1, [](int x, int y) { return max(x, y); }});
    vector<ll> opt(q + 1, lim<ll>::max());
    opt[q] = 0;
    for(int i = q - 1; i >= 0; i--) {
        if(i == 0) {
            for(int j = i; j < q; j++) {
                opt[i] = min(opt[i], s[j] - s[i] + cost0(i, j) + opt[j + 1]);    
            }    
        } else {
            opt[i] = min(
                gt_st.query(x[i - 1], N, true) - s[i] - x[i - 1],
                lt_st.query(0, x[i - 1], true) - s[i] + x[i - 1]
            );
            opt[i] = min(opt[i], s[q - 1] - s[i]);
            gt_st.update(x[i], min(gt_st.query(x[i]), s[i - 1] + x[i] + opt[i]));
            lt_st.update(x[i], min(lt_st.query(x[i]), s[i - 1] - x[i] + opt[i]));
        }
    }
    cout << opt[0] << endl;
    return 0;
}#include <bits/stdc++.h>
#define err(args...) {}
#ifdef DEBUG
#include "_debug.cpp"
#endif
using namespace std;
using ll = long long;
using ld = long double;
template <typename T> using lim = numeric_limits<T>;
template <typename T> istream& operator>>(istream& is, vector<T>& a) { for(T& x : a) { is >> x; } return is; }
const int N = 200'000;
template <typename T> struct monoid {
    const T identity;
    const function<T(T, T)> combine;
};
template <typename T> struct segtree {
    using Type = T;
    using DeltaType = T;
    const int size;
    vector<T> tree;
    const monoid<T> m;
    segtree(int size, const monoid<T>& m) : size(size), tree(2 * (size + 1), m.identity), m(m) {}
    void update(int i, T x) {
        for(tree[i += size] = x; i >>= 1; )
            tree[i] = m.combine(tree[i << 1], tree[(i << 1) | 1]);
    }
    T query(int a, int b, bool inclusive) {
        assert(a <= b);
        T ans_l = m.identity, ans_r = m.identity;
        for(a += size, b += size + inclusive; a < b; a >>= 1, b >>= 1) {
            if(a & 1) ans_l = m.combine(ans_l, tree[a++]);
            if(b & 1) ans_r = m.combine(tree[--b], ans_r);
        }
        return m.combine(ans_l, ans_r);
    }
    T query(int i) { return query(i, i, true); }
    friend ostream& operator<<(ostream& os, segtree& t) {
        os << "[";
        for(int i = 0; i <= t.size; i++) {
            if(i != 0) os << ", ";
            os << t.query(i);
        }
        return os << "]";
    }
};
int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    int n, q, a, b;
    cin >> n >> q >> a >> b;
    vector<ll> x(q), s(q);
    cin >> x;
    adjacent_difference(x.begin(), x.end(), s.begin());
    for(ll& d : s) {
        d = abs(d);
    }
    partial_sum(s.begin(), s.end(), s.begin());
    auto cost0 = [&](int i, int j) {
        if(j + 1 == q) {
            return min(abs(a - x[i]), abs(b - x[i]));
        } else {
            return min(abs(a - x[i]) + abs(b - x[j + 1]), abs(b - x[i]) + abs(a - x[j + 1]));
        } 
    };
    segtree<ll> gt_st(N + 1, {N * q + 1, [](int x, int y) { return min(x, y); }});
    segtree<ll> lt_st(N + 1, {N * q + 1, [](int x, int y) { return max(x, y); }});
    vector<ll> opt(q + 1, lim<ll>::max());
    opt[q] = 0;
    for(int i = q - 1; i >= 0; i--) {
        if(i == 0) {
            for(int j = i; j < q; j++) {
                opt[i] = min(opt[i], s[j] - s[i] + cost0(i, j) + opt[j + 1]);    
            }    
        } else {
            opt[i] = min(
                gt_st.query(x[i - 1], N, true) - s[i] - x[i - 1],
                lt_st.query(0, x[i - 1], true) - s[i] + x[i - 1]
            );
            opt[i] = min(opt[i], s[q - 1] - s[i]);
            gt_st.update(x[i], min(gt_st.query(x[i]), s[i - 1] + x[i] + opt[i]));
            lt_st.update(x[i], min(lt_st.query(x[i]), s[i - 1] - x[i] + opt[i]));
        }
    }
    cout << opt[0] << endl;
    return 0;
}

Compiler output:
a.cc:86:2: error: stray '#' in program
   86 | }#include <bits/stdc++.h>
      |  ^
a.cc:86:3: error: 'include' does not name a type
   86 | }#include <bits/stdc++.h>
      |   ^~~~~~~
a.cc:95:32: error: redefinition of 'template<class T> std::istream& operator>>(std::istream&, std::vector<_Tp>&)'
   95 | template <typename T> istream& operator>>(istream& is, vector<T>& a) { for(T& x : a) { is >> x; } return is; }
      |                                ^~~~~~~~
a.cc:10:32: note: 'template<class T> std::istream& operator>>(std::istream&, std::vector<_Tp>&)' previously declared here
   10 | template <typename T> istream& operator>>(istream& is, vector<T>& a) { for(T& x : a) { is >> x; } return is; }
      |                                ^~~~~~~~
a.cc:96:11: error: redefinition of 'const int N'
   96 | const int N = 200'000;
      |           ^
a.cc:11:11: note: 'const int N' previously defined here
   11 | const int N = 200'000;
      |           ^
a.cc:97:30: error: redefinition of 'struct monoid<T>'
   97 | template <typename T> struct monoid {
      |                              ^~~~~~
a.cc:12:30: note: previous definition of 'struct monoid<T>'
   12 | template <typename T> struct monoid {
      |                              ^~~~~~
a.cc:101:30: error: redefinition of 'struct segtree<T>'
  101 | template <typename T> struct segtree {
      |                              ^~~~~~~
a.cc:16:30: note: previous definition of 'struct segtree<T>'
   16 | template <typename T> struct segtree {
      |                              ^~~~~~~
a.cc:131:5: error: redefinition of 'int main()'
  131 | int main() {
      |     ^~~~
a.cc:46:5: note: 'int main()' previously defined here
   46 | int main() {
      |     ^~~~