Problem p01434 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cassert>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>

#define rep(i, n) for (int i = 0; i < (int)n; i++)
#define all(c) (c).begin(), (c).end()
#define mp make_pair
#define pb push_back
#define each(i, c)                                                             \
  for (__typeof((c).begin()) i = (c).begin(); i != (c).end(); i++)
#define dbg(x) cerr << __LINE__ << ": " << #x << " = " << (x) << endl

using namespace std;

typedef long long ll;
typedef vector<int> vi;
typedef pair<int, int> pi;
const int inf = (int)1e9 + 10;
const double INF = 1e12, EPS = 1e-9;

const int MX = 100000;
int n, m, p[MX];
int s[MX], t[MX];

int main() {
  vi v;
  scanf("%d%d", &n, &m);
  rep(i, n) scanf("%d%d", s + i, t + i), v.pb(s[i]), v.pb(t[i]);
  rep(i, m) scanf("%d", p + i), v.pb(p[i]);
  v.pb(p[m++] = -inf);
  v.pb(p[m++] = inf);
  sort(p, p + m);
  sort(all(v));
  v.erase(unique(all(v)), v.end());

  vi d[2];
  d[0].resize(v.size());
  d[1].resize(v.size());

  rep(i, n) {
    int a = lower_bound(all(v), s[i]) - v.begin();
    int b = lower_bound(all(v), t[i]) - v.begin();

    int p1 = (upper_bound(p, p + m, s[i]) - 1 - p) % 2;

    if (a < b)
      d[p1][a]++, d[p1][b]--;
    else
      d[p1][b]++, d[p1][a]--;
  }
  ll ans = 0, parity = 1, s0 = 0, s1 = 0;
  rep(i, v.size()) {
    if (i) {
      if (parity % 2 == 0)
        ans += s0 * (v[i] - v[i - 1]);
      else
        ans += s1 * (v[i] - v[i - 1]);
    }
    s0 += d[0][i];
    s1 += d[1][i];
    if (binary_search(p, p + m, v[i]))
      parity ^= 1;
  }
  cout << ans << endl;
  return 0;
}

Fixed submission:
#include <algorithm>
#include <cassert>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>

#define rep(i, n) for (int i = 0; i < (int)n; i++)
#define all(c) (c).begin(), (c).end()
#define mp make_pair
#define pb push_back
#define each(i, c)                                                             \
  for (__typeof((c).begin()) i = (c).begin(); i != (c).end(); i++)
#define dbg(x) cerr << __LINE__ << ": " << #x << " = " << (x) << endl

using namespace std;

typedef long long ll;
typedef vector<int> vi;
typedef pair<int, int> pi;
const int inf = (int)1e9 + 10;
const double INF = 1e12, EPS = 1e-9;

const int MX = 100010;
int n, m, p[MX];
int s[MX], t[MX];

int main() {
  vi v;
  scanf("%d%d", &n, &m);
  rep(i, n) scanf("%d%d", s + i, t + i), v.pb(s[i]), v.pb(t[i]);
  rep(i, m) scanf("%d", p + i), v.pb(p[i]);
  v.pb(p[m++] = -inf);
  v.pb(p[m++] = inf);
  sort(p, p + m);
  sort(all(v));
  v.erase(unique(all(v)), v.end());

  vi d[2];
  d[0].resize(v.size());
  d[1].resize(v.size());

  rep(i, n) {
    int a = lower_bound(all(v), s[i]) - v.begin();
    int b = lower_bound(all(v), t[i]) - v.begin();

    int p1 = (upper_bound(p, p + m, s[i]) - 1 - p) % 2;

    if (a < b)
      d[p1][a]++, d[p1][b]--;
    else
      d[p1][b]++, d[p1][a]--;
  }
  ll ans = 0, parity = 1, s0 = 0, s1 = 0;
  rep(i, v.size()) {
    if (i) {
      if (parity % 2 == 0)
        ans += s0 * (v[i] - v[i - 1]);
      else
        ans += s1 * (v[i] - v[i - 1]);
    }
    s0 += d[0][i];
    s1 += d[1][i];
    if (binary_search(p, p + m, v[i]))
      parity ^= 1;
  }
  cout << ans << endl;
  return 0;
}