Problem p03677 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <bitset>
#include <cassert>
#include <cctype>
#include <cerrno>
#include <cfloat>
#include <ciso646>
#include <climits>
#include <clocale>
#include <cmath>
#include <complex>
#include <csetjmp>
#include <csignal>
#include <cstdarg>
#include <cstddef>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <cwchar>
#include <cwctype>
#include <deque>
#include <exception>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <limits>
#include <list>
#include <locale>
#include <map>
#include <new>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdexcept>
#include <string>
#include <utility>
#include <vector>
using namespace std;

#define ll long long
#define INF 1e9
#define rep(i, n) for (int(i) = 0; (i) < n; i++)
#define REP(i, n) for (int(i) = 1; (i) <= n; i++)
#define mk(a, b) make_pair(a, b)
#define fi first
#define se second
#define pii pair<int, int>
#define pll pair<ll, ll>

int readint() {
  char c;
  while (c = getchar(), (c < '0' || c > '9') && c != '-')
    ;
  bool flag = (c == '-');
  if (flag)
    c = getchar();
  int x = 0;
  while (c >= '0' && c <= '9') {
    x = x * 10 + c - 48;
    c = getchar();
  }
  return flag ? -x : x;
}

const int maxn = 100005;
int n, m;
ll ans = (ll)INF * (ll)INF;
int ar[maxn];
pll coef[maxn * 4], cons[maxn * 4]; // first tree.value second tree.tag

void Push(int node, pll tree[]) {
  ll add = tree[node].se;
  if (add) {
    tree[node * 2].se += add;
    tree[node * 2 + 1].se += add;
    tree[node].fi += add;
    tree[node].se = 0;
  }
}

void Pull(int node, pll tree[]) {
  tree[node].fi = tree[node * 2].fi + tree[node * 2 + 1].fi;
}

void update(int l, int r, int vl, int vr, int node, pll tree[], int val) {
  if (l > vr || r < vl)
    return;
  if (l >= vl && r <= vr) {
    tree[node].se += val;
    Push(node, tree);
    return;
  }
  Push(node, tree);
  int mid = (l + r) >> 1;
  update(l, mid, vl, vr, node * 2, tree, val);
  update(mid + 1, r, vl, vr, node * 2 + 1, tree, val);
  Pull(node, tree);
}

ll query(int l, int r, int vl, int vr, int node, pll tree[]) {
  if (l > vr || r < vl)
    return 0;
  if (l >= vl && r <= vr)
    return tree[node].fi + tree[node].se;
  int mid = (l + r) >> 1;
  Push(node, tree);
  return query(l, mid, vl, vr, node * 2, tree) +
         query(mid + 1, r, vl, vr, node * 2 + 1, tree);
}

void add_ans(int a, int b) {
  if (a < b) {
    update(1, m, 1, a, 1, coef, b - a);
    update(1, m, a + 1, b, 1, cons, -1);
    update(1, m, a + 1, b, 1, coef, 1 + b);
    update(1, m, b + 1, m, 1, coef, b - a);
  } else {
    update(1, m, a + 1, m, 1, coef, 1 + m + b);
    update(1, m, a + 1, m, 1, cons, -1);
    update(1, m, 1, b, 1, coef, 1 + b);
    update(1, m, 1, b, 1, cons, -1);
    update(1, m, b + 1, a, 1, coef, m - a + b);
  }
}

int main() {
  scanf("%d%d", &n, &m);
  rep(i, n) scanf("%d", &ar[i]);
  rep(i, n - 1) add_ans(ar[i], ar[i + 1]);
  REP(i, m) {
    ll co = query(1, m, i, i, 1, coef);
    ll cs = query(1, m, i, i, 1, cons);
    ll cur = co + cs * i;
    //		cout<<co<<" "<<cs<<" "<<cur<<endl;
    ans = min(cur, ans);
  }
  printf("%lld", ans);
  return 0;
}

/*
4 6
1 5 1 4
10 10
10 9 8 7 6 5 4 3 2 1
*/

Fixed submission:
#include <algorithm>
#include <bitset>
#include <cassert>
#include <cctype>
#include <cerrno>
#include <cfloat>
#include <ciso646>
#include <climits>
#include <clocale>
#include <cmath>
#include <complex>
#include <csetjmp>
#include <csignal>
#include <cstdarg>
#include <cstddef>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <cwchar>
#include <cwctype>
#include <deque>
#include <exception>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <limits>
#include <list>
#include <locale>
#include <map>
#include <new>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdexcept>
#include <string>
#include <utility>
#include <vector>
using namespace std;

#define ll long long
#define INF 1e9
#define rep(i, n) for (int(i) = 0; (i) < n; i++)
#define REP(i, n) for (int(i) = 1; (i) <= n; i++)
#define mk(a, b) make_pair(a, b)
#define fi first
#define se second
#define pii pair<int, int>
#define pll pair<ll, ll>

int readint() {
  char c;
  while (c = getchar(), (c < '0' || c > '9') && c != '-')
    ;
  bool flag = (c == '-');
  if (flag)
    c = getchar();
  int x = 0;
  while (c >= '0' && c <= '9') {
    x = x * 10 + c - 48;
    c = getchar();
  }
  return flag ? -x : x;
}

const int maxn = 100005;
int n, m;
ll ans = (ll)INF * (ll)INF;
int ar[maxn];
pll coef[maxn * 8], cons[maxn * 8]; // first tree.value second tree.tag

void Push(int node, pll tree[]) {
  ll add = tree[node].se;
  if (add) {
    tree[node * 2].se += add;
    tree[node * 2 + 1].se += add;
    tree[node].fi += add;
    tree[node].se = 0;
  }
}

void Pull(int node, pll tree[]) {
  tree[node].fi = tree[node * 2].fi + tree[node * 2 + 1].fi;
}

void update(int l, int r, int vl, int vr, int node, pll tree[], int val) {
  if (l > vr || r < vl)
    return;
  if (l >= vl && r <= vr) {
    tree[node].se += val;
    Push(node, tree);
    return;
  }
  Push(node, tree);
  int mid = (l + r) >> 1;
  update(l, mid, vl, vr, node * 2, tree, val);
  update(mid + 1, r, vl, vr, node * 2 + 1, tree, val);
  Pull(node, tree);
}

ll query(int l, int r, int vl, int vr, int node, pll tree[]) {
  if (l > vr || r < vl)
    return 0;
  if (l >= vl && r <= vr)
    return tree[node].fi + tree[node].se;
  int mid = (l + r) >> 1;
  Push(node, tree);
  return query(l, mid, vl, vr, node * 2, tree) +
         query(mid + 1, r, vl, vr, node * 2 + 1, tree);
}

void add_ans(int a, int b) {
  if (a < b) {
    update(1, m, 1, a, 1, coef, b - a);
    update(1, m, a + 1, b, 1, cons, -1);
    update(1, m, a + 1, b, 1, coef, 1 + b);
    update(1, m, b + 1, m, 1, coef, b - a);
  } else {
    update(1, m, a + 1, m, 1, coef, 1 + m + b);
    update(1, m, a + 1, m, 1, cons, -1);
    update(1, m, 1, b, 1, coef, 1 + b);
    update(1, m, 1, b, 1, cons, -1);
    update(1, m, b + 1, a, 1, coef, m - a + b);
  }
}

int main() {
  scanf("%d%d", &n, &m);
  rep(i, n) scanf("%d", &ar[i]);
  rep(i, n - 1) add_ans(ar[i], ar[i + 1]);
  REP(i, m) {
    ll co = query(1, m, i, i, 1, coef);
    ll cs = query(1, m, i, i, 1, cons);
    ll cur = co + cs * i;
    //		cout<<co<<" "<<cs<<" "<<cur<<endl;
    ans = min(cur, ans);
  }
  printf("%lld", ans);
  return 0;
}

/*
4 6
1 5 1 4
10 10
10 9 8 7 6 5 4 3 2 1
*/