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

#include <algorithm>
#include <cstdio>
#include <iostream>
#include <map>
#include <set>
#include <string>
#include <vector>

using namespace std;

#define reps(i, f, n) for (int i = f; i < int(n); i++)
#define rep(i, n) reps(i, 0, n)

#define f first
#define s second

typedef pair<int, int> pii;

#ifdef __BORLANDC__
typedef __int64 ll;
#else
typedef long long ll;
#endif

int n, m;
vector<pii> line;
vector<int> cut;

bool load = false;
bool input() {
  if (load)
    return false;

  cin >> n >> m;
  line = vector<pii>(n);
  cut = vector<int>(m);

  rep(i, n) cin >> line[i].f >> line[i].s;
  rep(i, m) cin >> cut[i];

  return load = true;
}

const int T = 111111;
const int N = 1111111111;
int A[T], B[T];
int sumA[T], sumB[T];

ll solve() {

  cut.push_back(0);
  cut.push_back(N - 2);
  sort(cut.begin(), cut.end());

  rep(i, T) A[i] = B[i] = sumA[i] = sumB[i] = 0;
  rep(i, cut.size()) {
    int val = cut[i];
    if (i != 0)
      val -= cut[i - 1];
    if (i % 2 == 0)
      A[i] = val;
    else
      B[i] = val;
  }

  sumA[0] = A[0];
  reps(i, 1, cut.size()) {
    sumA[i] = sumA[i - 1] + A[i];
    sumB[i] = sumB[i - 1] + B[i];
  }
  ll sum = 0;
  /*
  rep(i,cut.size())printf("%3d ",A[i]);puts("");
  rep(i,cut.size())printf("%3d ",B[i]);puts("");
  rep(i,cut.size())printf("%3d ",sumA[i]);puts("");
  rep(i,cut.size())printf("%3d ",sumB[i]);puts("");
  */
  rep(i, line.size()) {
    int st = line[i].f;
    int en = line[i].s;

    int stnum = upper_bound(cut.begin(), cut.end(), st) - cut.begin();
    int ennum = upper_bound(cut.begin(), cut.end(), en) - cut.begin();

    bool sta = stnum % 2 == 0;
    bool ena = ennum % 2 == 0;

    int maxi = max(stnum, ennum);
    int mini = min(stnum, ennum);

    ll val = 0;

    if (stnum == ennum) {
      if (st > en)
        val += st - en;
      else
        val += en - st;
    } else {
      if (sta)
        val += sumA[maxi - 1] - sumA[mini];
      else
        val += sumB[maxi - 1] - sumB[mini];
      if (st > en) {
        val += st - cut[stnum - 1];
        if (sta == ena)
          val += cut[ennum] - en;
      } else {
        val += cut[stnum] - st;
        if (sta == ena)
          val += en - cut[ennum - 1];
      }
    }

    // printf("st=%d  en=%d  stnum=%d  ennum=%d  sta=%d  ena=%d  maxi=%d
    // mini=%d\\n",st,en,stnum,ennum,sta,ena,maxi,mini); printf("i=%d
    // val=%d\\n",i,val);
    sum += val;
  }

  return sum;
}

void c_solve() { cout << solve() << endl; }

int main() {
  while (input())
    c_solve();
}

/*
1 8
200 100
5
15
25
35
45
55
65
75

*/

Error: 0

Fixed submission:

#include <algorithm>
#include <cstdio>
#include <iostream>
#include <map>
#include <set>
#include <string>
#include <vector>

using namespace std;

#define reps(i, f, n) for (int i = f; i < int(n); i++)
#define rep(i, n) reps(i, 0, n)

#define f first
#define s second

typedef pair<int, int> pii;

int n, m;
vector<pii> line;
vector<int> cut;

bool load = false;
bool input() {
  if (load)
    return false;

  cin >> n >> m;
  line = vector<pii>(n);
  cut = vector<int>(m);

  rep(i, n) cin >> line[i].f >> line[i].s;
  rep(i, m) cin >> cut[i];

  return load = true;
}

const int T = 111111;
int A[T], B[T];
int sumA[T], sumB[T];

int solve() {

  cut.push_back(T - 2);
  sort(cut.begin(), cut.end());

  rep(i, T) A[i] = B[i] = sumA[i] = sumB[i] = 0;

  int bef = 0;
  rep(i, cut.size()) {
    if (i % 2 == 0) {
      reps(j, bef, cut[i]) A[j] = 1;
    } else {
      reps(j, bef, cut[i]) B[j] = 1;
    }
    bef = cut[i];
  }

  reps(i, 1, T) {
    sumA[i] = sumA[i - 1] + A[i];
    sumB[i] = sumB[i - 1] + B[i];
  }

  /*
  rep(i,25)printf("%2d ",A[i]);puts("");
  rep(i,25)printf("%2d ",B[i]);puts("");
  rep(i,25)printf("%2d ",sumA[i]);puts("");
  rep(i,25)printf("%2d ",sumB[i]);puts("");
  */
  int ret = 0;

  rep(i, line.size()) {
    int st = line[i].f;
    int en = line[i].s;
    int mini = min(st, en);
    int maxi = max(st, en);

    if (A[st] == 1) {
      ret += sumA[maxi - 1] - sumA[mini - 1];
    } else {
      ret += sumB[maxi - 1] - sumB[mini - 1];
    }
  }

  return ret;
}

void c_solve() { cout << solve() << endl; }

int main() {
  while (input())
    c_solve();
}