Problem p03677 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
typedef long long lld;
#define PB push_back
#define FF first
#define SS second

const int INF = 2000000000;
const int N = 100000;
int arr[N];

class BIT {
#define lowbit(x) ((x) & (-(x)))
private:
  lld arr[N];
  int size;

public:
  // 1-base (l, r]
  void init(int n) {
    size = n;
    // arr=new lld[n];
    fill(arr, arr + n, 0);
  }
  void add(int l, int r, lld v) {
    add(r, v);
    add(l, -v);
  }
  void add(int r, lld v) {
    while (r) {
      arr[r] += v;
      r -= lowbit(r);
    }
  }
  lld query(int x) {
    lld sm = 0;
    while (x < size) {
      sm += arr[x];
      x += lowbit(x);
    }
    return sm;
  }
#undef lowbit
};

int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  int n, m;
  cin >> n >> m;
  for (int i = 0; i < n; i++)
    cin >> arr[i];
  lld sumL = 0, sumR = 0;
  for (int i = 0; i < n - 1; i++)
    sumL += arr[i];
  for (int i = 1; i < n; i++)
    sumR += arr[i] + m * (arr[i - 1] > arr[i]);
  BIT cnt, sm;
  cnt.init(m + 5);
  sm.init(m + 5);
  for (int i = 1; i < n; i++) {
    if (arr[i - 1] > arr[i]) {
      cnt.add(arr[i - 1], m, 1);
      cnt.add(0, arr[i], 1);
      sm.add(arr[i - 1], m, arr[i - 1]);
      sm.add(0, arr[i], arr[i - 1] - m);
    } else {
      cnt.add(arr[i - 1], arr[i], 1);
      sm.add(arr[i - 1], arr[i], arr[i - 1]);
    }
  }
  lld val = sumR - sumL;
  for (int i = 1; i <= m; i++) {
    lld curV = sumR - sumL - cnt.query(i) * (i - 1) + sm.query(i);
    if (curV < val)
      val = curV;
  }
  cout << val << '\n';
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long lld;
#define PB push_back
#define FF first
#define SS second

const int INF = 2000000000;
const int N = 100000 + 10;
int arr[N];

class BIT {
#define lowbit(x) ((x) & (-(x)))
private:
  lld arr[N];
  int size;

public:
  // 1-base (l, r]
  void init(int n) {
    size = n;
    // arr=new lld[n];
    fill(arr, arr + n, 0);
  }
  void add(int l, int r, lld v) {
    add(r, v);
    add(l, -v);
  }
  void add(int r, lld v) {
    while (r) {
      arr[r] += v;
      r -= lowbit(r);
    }
  }
  lld query(int x) {
    lld sm = 0;
    while (x < size) {
      sm += arr[x];
      x += lowbit(x);
    }
    return sm;
  }
#undef lowbit
};

int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  int n, m;
  cin >> n >> m;
  for (int i = 0; i < n; i++)
    cin >> arr[i];
  lld sumL = 0, sumR = 0;
  for (int i = 0; i < n - 1; i++)
    sumL += arr[i];
  for (int i = 1; i < n; i++)
    sumR += arr[i] + m * (arr[i - 1] > arr[i]);
  BIT cnt, sm;
  cnt.init(m + 5);
  sm.init(m + 5);
  for (int i = 1; i < n; i++) {
    if (arr[i - 1] > arr[i]) {
      cnt.add(arr[i - 1], m, 1);
      cnt.add(0, arr[i], 1);
      sm.add(arr[i - 1], m, arr[i - 1]);
      sm.add(0, arr[i], arr[i - 1] - m);
    } else {
      cnt.add(arr[i - 1], arr[i], 1);
      sm.add(arr[i - 1], arr[i], arr[i - 1]);
    }
  }
  lld val = sumR - sumL;
  for (int i = 1; i <= m; i++) {
    lld curV = sumR - sumL - cnt.query(i) * (i - 1) + sm.query(i);
    if (curV < val)
      val = curV;
  }
  cout << val << '\n';
  return 0;
}