Problem p03677 (C++)

Buggy submission (status: Runtime Error):
/* ***********************************************
Author        :yang12138
Created Time  :2017年07月07日 星期五 18时09分44秒
File Name     :C.cpp
************************************************ */
#include <algorithm>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stdio.h>
#include <string>
#include <vector>
using namespace std;
#define mem(a, b) memset(a, b, sizeof(a))
typedef long long ll;
typedef pair<int, int> pii;
#define lson (root << 1)
#define rson (root << 1 | 1)

const int N = 1e5 + 10;

struct Node {
  int pos, sub, id;
  Node() {}
  Node(int pos, int sub, int id) {
    this->pos = pos;
    this->sub = sub;
    this->id = id;
  }
  bool operator<(const Node &tmp) const {
    if (pos == tmp.pos)
      return id < tmp.id;
    return pos < tmp.pos;
  }
} p[N << 1];

int a[N];
int main() {
  // freopen("in.txt","r",stdin);
  // freopen("out.txt","w",stdout);

  int n, m;
  scanf("%d%d", &n, &m);
  for (int i = 1; i <= n; i++)
    scanf("%d", a + i);

  int cnt = 0;
  ll ans = 0;
  for (int i = 1; i < n; i++) {
    if (a[i] < a[i + 1]) {
      ans += a[i + 1] - a[i];
      if (a[i] + 2 < a[i + 1] + 1) {
        p[cnt++] = Node(a[i] + 2, 0, 2);
        p[cnt++] = Node(a[i + 1] + 1, -(a[i + 1] - a[i] - 1), 1);
      }
    } else {
      ans += a[i + 1] - a[i] + m;
      if (a[i] + 2 < m + 1) {
        p[cnt++] = Node(a[i] + 2, 0, 2);
        p[cnt++] = Node(m + 1, -(m - a[i] - 1), 1);
      }
      p[cnt++] = Node(1, m + 1 - a[i] - 2, 2);
      p[cnt++] = Node(a[i + 1] + 1, -(a[i + 1] - a[i] + m - 1), 1);
    }
  }

  sort(p, p + cnt);
  int tot = 0, pre = 1;
  ll tmp = ans;
  for (int i = 0, j; i < cnt; i = j) {

    j = i;
    while (j < cnt && p[j].pos == p[i].pos) {
      if (p[j].id == 1) {
        tmp -= p[j].sub;
        tot--;
      } else {
        tmp -= p[j].sub;
        tot++;
      }
      j++;
    }
    if (j < cnt) {
      int cur = p[j].pos;
      tmp -= (cur - pre) * tot;
      pre = cur;
    }

    ans = min(ans, tmp);
  }

  printf("%lld\n", ans);

  return 0;
}


Fixed submission:
/* ***********************************************
Author        :yang12138
Created Time  :2017年07月07日 星期五 18时09分44秒
File Name     :C.cpp
************************************************ */
#include <algorithm>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stdio.h>
#include <string>
#include <vector>
using namespace std;
#define mem(a, b) memset(a, b, sizeof(a))
typedef long long ll;
typedef pair<int, int> pii;
#define lson (root << 1)
#define rson (root << 1 | 1)

const int N = 1e5 + 10;

struct Node {
  int pos, sub, id;
  Node() {}
  Node(int pos, int sub, int id) {
    this->pos = pos;
    this->sub = sub;
    this->id = id;
  }
  bool operator<(const Node &tmp) const {
    if (pos == tmp.pos)
      return id < tmp.id;
    return pos < tmp.pos;
  }
} p[N << 3];

int a[N];
int main() {
  // freopen("in.txt","r",stdin);
  // freopen("out.txt","w",stdout);

  int n, m;
  scanf("%d%d", &n, &m);
  for (int i = 1; i <= n; i++)
    scanf("%d", a + i);

  int cnt = 0;
  ll ans = 0;
  for (int i = 1; i < n; i++) {
    if (a[i] < a[i + 1]) {
      ans += a[i + 1] - a[i];
      if (a[i] + 2 < a[i + 1] + 1) {
        p[cnt++] = Node(a[i] + 2, 0, 2);
        p[cnt++] = Node(a[i + 1] + 1, -(a[i + 1] - a[i] - 1), 1);
      }
    } else {
      ans += a[i + 1] - a[i] + m;
      if (a[i] + 2 < m + 1) {
        p[cnt++] = Node(a[i] + 2, 0, 2);
        p[cnt++] = Node(m + 1, -(m - a[i] - 1), 1);
      }
      p[cnt++] = Node(1, m + 1 - a[i] - 2, 2);
      p[cnt++] = Node(a[i + 1] + 1, -(a[i + 1] - a[i] + m - 1), 1);
    }
  }

  sort(p, p + cnt);
  int tot = 0, pre = 1;
  ll tmp = ans;
  for (int i = 0, j; i < cnt; i = j) {

    j = i;
    while (j < cnt && p[j].pos == p[i].pos) {
      if (p[j].id == 1) {
        tmp -= p[j].sub;
        tot--;
      } else {
        tmp -= p[j].sub;
        tot++;
      }
      j++;
    }
    if (j < cnt) {
      int cur = p[j].pos;
      tmp -= (cur - pre) * tot;
      pre = cur;
    }

    ans = min(ans, tmp);
  }

  printf("%lld\n", ans);

  return 0;
}