Problem p01434 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <climits>
#include <cstdlib>
#include <iostream>
#include <vector>
using namespace std;

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);

  int n, m;
  cin >> n >> m;

  vector<int> s(n), t(n);
  for (int i = 0; i < n; ++i)
    cin >> s[i] >> t[i];

  vector<int> p(m + 1);
  for (int i = 0; i < m; ++i)
    cin >> p[i];

  p[m] = INT_MAX;
  sort(p.begin(), p.end());

  long long ans = 0;
  vector<int> cnt(m + 1, 0);
  for (int i = 0; i < n; ++i) {
    const int l = upper_bound(p.begin(), p.end(), s[i]) - p.begin();
    const int h = upper_bound(p.begin(), p.end(), t[i]) - p.begin();
    if (l < h) {
      ans += p[l] - s[i];
      ++cnt[l + 1];
      if ((h - l) & 1) {
        --cnt[h];
      } else {
        --cnt[h - 1];
        ans += t[i] - p[h - 1];
      }
    } else if (l > h) {
      ans += s[i] - p[l - 1];
      --cnt[l - 1];
      if ((h - l) & 1) {
        ++cnt[h];
      } else {
        ++cnt[h + 1];
        ans += p[h] - t[i];
      }
    } else
      ans += abs(s[i] - t[i]);
  }

  for (int i = 2; i < m - 1; ++i)
    cnt[i] += cnt[i - 2];

  for (int i = 0; i < m - 1; ++i)
    ans += (long long)cnt[i] * (p[i + 1] - p[i]);

  cout << ans << endl;

  return EXIT_SUCCESS;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cassert>
#include <cctype>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <functional>
#include <iostream>
#include <iterator>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>
using namespace std;

#define dump(a) (cerr << (#a) << " = " << (a) << endl)
#define FOR(it, c)                                                             \\
  for (__typeof((c).begin()) it = (c).begin(); it != (c).end(); ++it)
#define RFOR(it, c)                                                            \\
  for (__typeof((c).rbegin()) it = (c).rbegin(); it != (c).rend(); ++it)
#define exist(c, v) (find((c).begin(), (c).end(), (v)) != (c).end())

template <class T> inline void chmax(T &a, const T &b) {
  if (b > a)
    a = b;
}
template <class T> inline void chmin(T &a, const T &b) {
  if (b < a)
    a = b;
}

template <typename T, typename U>
ostream &operator<<(ostream &os, const pair<T, U> &p) {
  os << '(' << p.first << ", " << p.second << ')';
  return os;
}

template <typename T> ostream &operator<<(ostream &os, const vector<T> &v) {
  copy(v.begin(), v.end(), ostream_iterator<T>(os, " "));
  return os;
}

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);

  int n, m;
  cin >> n >> m;

  vector<int> s(n), t(n);
  for (int i = 0; i < n; ++i)
    cin >> s[i] >> t[i];

  vector<int> p(m);
  for (int i = 0; i < m; ++i)
    cin >> p[i];

  p[m] = INT_MAX;
  sort(p.begin(), p.end());

  long long ans = 0;
  vector<int> cnt(m + 1, 0);
  for (int i = 0; i < n; ++i) {
    const int l = upper_bound(p.begin(), p.end(), s[i]) - p.begin();
    const int h = upper_bound(p.begin(), p.end(), t[i]) - p.begin();
    if (l < h) {
      ans += p[l] - s[i];
      ++cnt[l + 1];
      if ((h - l) & 1) {
        --cnt[h];
      } else {
        --cnt[h - 1];
        ans += t[i] - p[h - 1];
      }
    } else if (l > h) {
      ans += s[i] - p[l - 1];
      --cnt[l - 1];
      if ((h - l) & 1) {
        ++cnt[h];
      } else {
        ++cnt[h + 1];
        ans += p[h] - t[i];
      }
    } else
      ans += abs(s[i] - t[i]);
  }

  for (int i = 2; i < m - 1; ++i)
    cnt[i] += cnt[i - 2];

  for (int i = 0; i < m - 1; ++i)
    ans += (long long)cnt[i] * (p[i + 1] - p[i]);

  cout << ans << endl;

  return EXIT_SUCCESS;
}