Language: C++


#include <bits/stdc++.h>

#define null               ""
#define endl               '\n'
#define all(dat)           dat.begin(), dat.end()
#define loop(i, to)        for (num i = 0; i < to; ++i)
#define cont(i, to)        for (num i = 1; i <= to; ++i)
#define circ(i, fr, to)    for (num i = fr; i <= to; ++i)
#define foreach(i, dat)    for (__typeof(dat.begin()) i = dat.begin(); i != dat.end(); ++i)

typedef long long          num;

using namespace std;

const num nsz = 1e5, inf = 0x3f3f3f3f;
num n, m, ans = inf;

struct segment_tree {
    num tr[4 * nsz + 5], fi[4 * nsz + 5], del[4 * nsz + 5];
    
    void inline up(num u) {
        tr[u] = tr[u << 1] + tr[u << 1 | 1];
    }
    
    void inline down(num u, num l, num r) {
        if (!fi[u] && !del[u])  return;
        num ls = u << 1, rs = u << 1 | 1, mid = (l + r) >> 1;
        num fl = fi[u], fr = fi[u] + (mid - l + 1) * del[u], el = fl + (mid - l) * del[u], er = fr + (r - mid - 1) * del[u];
        tr[ls] += (fl + el) * (mid - l + 1) / 2;
        tr[rs] += (fr + er) * (r - mid) / 2;
        fi[ls] += fi[u];
        fi[rs] += fi[u] + (mid - l + 1) * del[u];
        del[ls] += del[u];
        del[rs] += del[u];
        fi[u] = del[u] = 0;
    }
    
    void upd(num el, num er, num f, num de, num u = 1, num l = 1, num r = m) {
        if (l >= el && r <= er) {
            tr[u] += (f + f + (r - l) * de) * (r - l + 1) / 2;
            fi[u] += f;
            del[u] += de;
            return;
        }
        down(u, l, r);
        num mid = (l + r) >> 1;
        if (el <= mid) {
            upd(el, er, f, de, u << 1, l, mid);
        }
        if (er >= mid + 1) {
            upd(el, er, f + (mid - l + 1) * de, de, u << 1 | 1, mid + 1, r);
        }
        up(u);
    }
    
    num qry(num el, num er, num u = 1, num l = 1, num r = m) {
        if (l >= el && r <= er)  return tr[u];
        down(u, l, r);
        num mid = (l + r) >> 1, res = 0;
        if (el <= mid) {
            res += qry(el, er, u << 1, l, mid);
        }
        if (er >= mid + 1) {
            res += qry(el, er, u << 1 | 1, mid + 1, r);
        }
        return res;
    }
};
segment_tree tr;

num inline read() {
    num f = 1, res = 0;
    char c = getchar();
    for (; !isdigit(c); f = c == '-' ? -1 : 1, c = getchar());
    for (; isdigit(c); res *= 10, res += c ^ 48, c = getchar());
    return f * res;
}

num inline calc(num l, num r) {
    return l > r ? r + m - l : r - l;
}

bool inline chk(num pos) {
    return pos >= 1 && pos <= m;
}

int main() {
    n = read();
    m = read();
    num pre = read(), cur;
    cont (i, n - 1) {
        cur = read();
        num mx = calc(pre, cur), pos = cur, l = 1, r = pos - mx + 1, f;
//        cout << "mx: " << mx << endl;
//        cout << 1 << ' ' << l << ' ' << r << endl;
        if (l <= r && chk(l) && chk(r)) {
            tr.upd(l, r, mx, 0);
        }
        l = pos + 1;
        r = min(m, pos + m - mx + 1);
//        cout << 2 << ' ' << l << ' ' << r << endl;
        if (l <= r && chk(l) && chk(r)) {
            tr.upd(l, r, mx, 0);
        }
        l = max(1, pos - mx + 2);
        r = pos;
        f = 1 + (pos - l);
//        cout << 3 << ' ' << l << ' ' << r << endl;
        if (l <= r && chk(l) && chk(r)) {
            tr.upd(l, r, f + (l - 1), -1);
        }
        l = pos + m - mx + 2;
        r = m;
        f = m;
//        cout << 4 << ' ' << l << ' ' << r << endl;
        if (l <= r && chk(l) && chk(r)) {
            tr.upd(l, r, f + (l - 1), -1);
        }
//        cont (i, m) {
//            cout << tr.qry(i, i) << ' ';
//        }
//        cout << endl;
        pre = cur;
    }
    cont (i, m) {
        ans = min(ans, tr.qry(i, i));
    }
    cout << ans << endl;
}

//4 6
//1 5 1 4


Compiler output:
a.cc: In function 'int main()':
a.cc:106:16: error: no matching function for call to 'max(int, num)'
  106 |         l = max(1, pos - mx + 2);
      |             ~~~^~~~~~~~~~~~~~~~~
In file included from /usr/include/c++/14/algorithm:60,
                 from /usr/include/x86_64-linux-gnu/c++/14/bits/stdc++.h:51,
                 from a.cc:2:
/usr/include/c++/14/bits/stl_algobase.h:257:5: note: candidate: 'template<class _Tp> constexpr const _Tp& std::max(const _Tp&, const _Tp&)'
  257 |     max(const _Tp& __a, const _Tp& __b)
      |     ^~~
/usr/include/c++/14/bits/stl_algobase.h:257:5: note:   template argument deduction/substitution failed:
a.cc:106:16: note:   deduced conflicting types for parameter 'const _Tp' ('int' and 'num' {aka 'long long int'})
  106 |         l = max(1, pos - mx + 2);
      |             ~~~^~~~~~~~~~~~~~~~~
/usr/include/c++/14/bits/stl_algobase.h:303:5: note: candidate: 'template<class _Tp, class _Compare> constexpr const _Tp& std::max(const _Tp&, const _Tp&, _Compare)'
  303 |     max(const _Tp& __a, const _Tp& __b, _Compare __comp)
      |     ^~~
/usr/include/c++/14/bits/stl_algobase.h:303:5: note:   candidate expects 3 arguments, 2 provided
In file included from /usr/include/c++/14/algorithm:61:
/usr/include/c++/14/bits/stl_algo.h:5706:5: note: candidate: 'template<class _Tp> constexpr _Tp std::max(initializer_list<_Tp>)'
 5706 |     max(initializer_list<_Tp> __l)
      |     ^~~
/usr/include/c++/14/bits/stl_algo.h:5706:5: note:   candidate expects 1 argument, 2 provided
/usr/include/c++/14/bits/stl_algo.h:5716:5: note: candidate: 'template<class _Tp, class _Compare> constexpr _Tp std::max(initializer_list<_Tp>, _Compare)'
 5716 |     max(initializer_list<_Tp> __l, _Compare __comp)
      |     ^~~
/usr/include/c++/14/bits/stl_algo.h:5716:5: note:   template argument deduction/substitution failed:
a.cc:106:16: note:   mismatched types 'std::initializer_list<_Tp>' and 'int'
  106 |         l = max(1, pos - mx + 2);
      |             ~~~^~~~~~~~~~~~~~~~~