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

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<ll, ll> P;

ll N, Q, x, y, z;
P s, t;
char str[20];

P a[300005];
ll b[300005];
ll C;

P find(ll x) {
  if (a[x].first == -1)
    return P(x, 0);
  P res = find(a[x].first);
  res.second += a[x].second;
  return a[x] = res;
}

void init() {
  for (int i = 0; i < 300005; i++) {
    a[i] = P(-1, 0);
    b[i] = i;
  }
  C = 100001;
}

int main() {
  init();
  cin >> N >> Q;
  for (int i = 0; i < Q; i++) {
    scanf("%s", str);

    if (str[0] == 'C') {
      scanf("%lld %lld", &x, &y);
      x = b[x];
      y = b[y];
      s = find(x);
      t = find(y);
      if (s.first != t.first)
        printf("WARNING\\n");
      else {
        printf("%lld\\n", s.second - t.second);
      }
    } else {
      scanf("%lld %lld %lld", &x, &y, &z);
      a[C] = a[b[x]];
      if (a[C].first != -1)
        a[C].second -= z;
      a[b[x]] = P(C, z);
      b[x] = C++;

      a[C] = a[b[y]];
      if (a[C].first != -1)
        a[C].second -= z;
      a[b[y]] = P(C, z);
      b[y] = C++;

      x = b[x];
      y = b[y];
      s = find(x);
      t = find(y);
      if (s.first != t.first) {
        a[s.first].first = t.first;
        a[s.first].second = z + t.second - s.second;
      }
    }
  }
  return 0;
}

Error: -11

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef complex<double> P;

#define MAX 5000
#define MAX_N 105
int n, x[MAX], y[MAX];
int L[MAX], R[MAX];

void f(int x, int y) {
  L[y] = min(L[y], x);
  R[y] = max(R[y], x);
}

void init() {
  for (int i = 0; i < MAX; i++) {
    L[i] = 5000;
    R[i] = -1;
  }
}

int main() {
  while (1) {
    cin >> n;
    if (n == 0)
      break;
    init();

    vector<P> V;
    for (int i = 0; i < n; i++) {
      cin >> x[i] >> y[i];
      V.push_back(P(x[i], y[i]));
      x[i] += 2001, y[i] += 2001;
    }
    double sum = 0;
    for (int i = 0; i < n; i++) {
      sum +=
          arg((V[(i + 2) % n] - V[(i + 1) % n]) * conj(V[(i + 1) % n] - V[i]));
    }

    if (sum < 0) {
      reverse(x, x + n);
      reverse(y, y + n);
    }

    for (int i = 0; i < n; i++) {
      int sx = x[i], sy = y[i];
      int tx = x[(i + 1) % n], ty = y[(i + 1) % n];
      if (sx == tx) {
        if (sy < ty)
          for (int i = sy; i < ty; i++)
            f(sx - 1, i);
        else
          for (int i = ty; i < sy; i++)
            f(sx, i);
        continue;
      }
      if (sy == ty) {
        if (sx < tx)
          f(sx, sy), f(tx - 1, sy);
        else
          f(sx - 1, sy - 1), f(tx, sy - 1);
        continue;
      }
      int vx = tx - sx, vy = ty - sy;

      long long A = abs(vx), B = abs(vy);

      if (vx > 0 && vy > 0) {
        f(sx, sy);
        for (int y = sy; y < ty; y++) {
          if (A % B == 0) {
            f(sx + A / B - 1, y);
            if (y + 1 < ty)
              f(sx + A / B - 1 + 1, y + 1);
          } else {
            f(sx + A / B, y);
            if (y + 1 < ty)
              f(sx + A / B, y + 1);
          }
          A += abs(vx);
        }
      }

      if (vx < 0 && vy > 0) {
        f(sx - 1, sy);
        for (int y = sy; y < ty; y++) {
          if (A % B == 0) {
            f(sx - (A / B), y);
            if (y + 1 < ty)
              f(sx - (A / B) - 1, y + 1);
          } else {
            f(sx - (A / B) - 1, y);
            if (y + 1 < ty)
              f(sx - (A / B) - 1, y + 1);
          }
          A += abs(vx);
        }
      }

      if (vx < 0 && vy < 0) {

        f(sx - 1, sy - 1);
        for (int y = sy - 1; y >= ty; y--) {
          if (A % B == 0) {
            f(sx - (A / B), y);
            if (y - 1 >= ty)
              f(sx - (A / B) - 1, y - 1);
          } else {
            f(sx - (A / B) - 1, y);
            if (y - 1 >= ty)
              f(sx - (A / B) - 1, y - 1);
          }
          A += abs(vx);
        }
      }

      if (vx > 0 && vy < 0) {
        f(sx, sy - 1);
        for (int y = sy - 1; y >= ty; y--) {
          if (A % B == 0) {
            f(sx + A / B - 1, y);
            if (y - 1 >= ty)
              f(sx + A / B - 1 + 1, y - 1);
          } else {
            f(sx + A / B, y);
            if (y - 1 >= ty)
              f(sx + A / B, y - 1);
          }
          A += abs(vx);
        }
      }
    }
    int ans = 0;
    for (int i = 0; i < MAX; i++) {
      if (L[i] <= R[i]) {

        ans += (R[i] - L[i] + 1);
      }
    }
    cout << ans << endl;
  }
  return 0;
}