Problem p03677 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>

#define PI acos(-1)
#define mp make_pair
#define fi first
#define se second
#define pb push_back
#define endl '\n'
#define io ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
#define file freopen("team.in", "r", stdin), freopen("team.out", "w", stdout);
#define fileIn freopen("in.txt", "r", stdin);
#define all(v) ((v).begin()), ((v).end())

using namespace std;

typedef pair<int, int> pii;

typedef long long ll;
typedef unsigned long long ull;
typedef pair<ll, int> pli;

const int N = 1e5 + 10;

int a[N];
ll sum[N], d[N];
int n, m;

void add(int L, int R, int c, int k) {

  if (L > R) {
    return;
  }
  // add

  sum[L] += c;
  sum[R + 1] -= c + (R - L) * 1LL * k;

  d[L] += k;
  d[R] -= k;
}

void calc() {
  for (int i = 1; i <= m; i++) {
    sum[i] += sum[i - 1];
    d[i] += d[i - 1];
  }
  ll cur = 0;
  for (int i = 1; i <= m; i++) {
    sum[i] += cur;
    cur += d[i];
  }
}

void print(ll *a) {

  for (int i = 1; i <= m; i++) {
    cout << a[i] << " ";
  }
  cout << endl << endl;
}

int main() {
#ifndef ONLINE_JUDGE
  fileIn;
#endif
  io;
  cin >> n >> m;

  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }

  // L, R, c, k

  for (int i = 0; i < n - 1; i++) {
    int from = a[i];
    int to = a[i + 1];
    int dist = (to - from + m) % m;
    if (from < to) {
      add(from, from, dist, 0);
      add(from + 1, to, dist, -1);
      add(1, from - 1, dist, 0);
      add(to + 1, m, dist, 0);
    }
    if (to < from) {
      add(from, from, dist, 0);
      add(from + 1, m, dist, -1);
      add(1, to, to, -1);
      add(to + 1, from - 1, dist, 0);
    }
  }
  calc();

  ll best = sum[1];
  for (int i = 1; i <= m; i++) {
    if (sum[i] < best) {
      best = sum[i];
    }
  }
  cout << best << endl;
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>

#define PI acos(-1)
#define mp make_pair
#define fi first
#define se second
#define pb push_back
#define endl '\n'
#define io ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
#define file freopen("team.in", "r", stdin), freopen("team.out", "w", stdout);
#define fileIn freopen("in.txt", "r", stdin);
#define all(v) ((v).begin()), ((v).end())

using namespace std;

typedef pair<int, int> pii;

typedef long long ll;
typedef unsigned long long ull;
typedef pair<ll, int> pli;

const int N = 1e5 + 10;

int a[N];
ll sum[N], d[N];
int n, m;

void add(int L, int R, int c, int k) {

  if (L > R) {
    return;
  }
  // add

  sum[L] += c;
  sum[R + 1] -= c + (R - L) * 1LL * k;

  d[L] += k;
  d[R] -= k;
}

void calc() {
  for (int i = 1; i <= m; i++) {
    sum[i] += sum[i - 1];
    d[i] += d[i - 1];
  }
  ll cur = 0;
  for (int i = 1; i <= m; i++) {
    sum[i] += cur;
    cur += d[i];
  }
}

void print(ll *a) {

  for (int i = 1; i <= m; i++) {
    cout << a[i] << " ";
  }
  cout << endl << endl;
}

int main() {
#ifndef ONLINE_JUDGE
  // fileIn;
#endif
  io;
  cin >> n >> m;

  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }

  // L, R, c, k

  for (int i = 0; i < n - 1; i++) {
    int from = a[i];
    int to = a[i + 1];
    int dist = (to - from + m) % m;
    if (from < to) {
      add(from, from, dist, 0);
      add(from + 1, to, dist, -1);
      add(1, from - 1, dist, 0);
      add(to + 1, m, dist, 0);
    }
    if (to < from) {
      add(from, from, dist, 0);
      add(from + 1, m, dist, -1);
      add(1, to, to, -1);
      add(to + 1, from - 1, dist, 0);
    }
  }
  calc();

  ll best = sum[1];
  for (int i = 1; i <= m; i++) {
    if (sum[i] < best) {
      best = sum[i];
    }
  }
  cout << best << endl;
  return 0;
}