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

#include <bits/stdc++.h>
using namespace std;

int main() {

  int n, m;
  cin >> n >> m;
  vector<tuple<int, int, int>> abc(n);
  for (int i = 0; i < n; ++i) {
    int a, b, c;
    cin >> a >> b >> c;
    abc[i] = make_tuple(a, b, c);
  }

  vector<tuple<int, int, int>> def(m);
  for (int i = 0; i < m; ++i) {
    int d, e, f;
    cin >> d >> e >> f;
    def[i] = make_tuple(d, e, f);
  }

  map<int, int> mx, my;
  mx[0] = 0;
  my[0] = 0;
  for (int i = 0; i < n; ++i) {
    int a, b, c;
    tie(a, b, c) = abc[i];
    mx[a] = 0;
    mx[b] = 0;
    my[c] = 0;
  }
  for (int i = 0; i < m; ++i) {
    int d, e, f;
    tie(d, e, f) = def[i];
    mx[d] = 0;
    my[e] = 0;
    my[f] = 0;
  }

  vector<int> realx(mx.size());
  int count = 0;
  // fprintf(stderr,"X:\\n");
  for (auto p : mx) {
    // fprintf(stderr,"%d %d\\n",count,p.first);
    mx[p.first] = count;
    realx[count] = p.first;
    ++count;
  }

  vector<int> realy(my.size());
  count = 0;
  // fprintf(stderr,"Y:\\n");
  for (auto p : my) {
    // fprintf(stderr,"%d %d\\n",count,p.first);
    my[p.first] = count;
    realy[count] = p.first;
    ++count;
  }

  vector<vector<int>> mm(mx.size(), vector<int>(my.size()));
  const int WALL_L = 0x01;
  const int WALL_U = 0x02;
  const int WALL_R = 0x04;
  const int WALL_D = 0x08;
  const int FOUND = 0x10;

  // place vertical walls
  for (int i = 0; i < n; ++i) {
    int a, b, c;
    tie(a, b, c) = abc[i];
    a = mx[a];
    b = mx[b];
    c = my[c];
    for (int j = a; j < b; ++j) {
      if (c > 0) {
        mm[j][c - 1] |= WALL_R;
      }
      mm[j][c] |= WALL_L;
    }
  }

  // place horizontal walls
  for (int i = 0; i < m; ++i) {
    int d, e, f;
    tie(d, e, f) = def[i];
    d = mx[d];
    e = my[e];
    f = my[f];
    for (int j = e; j < f; ++j) {
      if (d > 0) {
        mm[d - 1][j] |= WALL_D;
      }
      mm[d][j] |= WALL_U;
    }
  }

  if ((mx[0] == 0) || (mx[0] == mx.size() - 1) || (my[0] == 0) ||
      (my[0] == my.size() - 1)) {
    cout << "INF" << endl;
    return 0;
  }

  int x = mx[0], y = my[0];
  int64_t sum_area = 0;
  int maxx = mx.size();
  int maxy = my.size();
  queue<pair<int, int>> q;

  q.push(make_pair(x, y));
  mm[x][y] |= FOUND;
  while (q.empty() == false) {
    pair<int, int> p = q.front();
    q.pop();
    x = p.first;
    y = p.second;
    // fprintf(stderr,"%d,%d (%d,%d) - %d,%d (%d,%d)
    // (%d)\\n",x,y,realx[x],realy[y],x+1,y+1,realx[x+1],realy[y+1],(realx[x+1]-realx[x])*(realy[y+1]-realy[y])
    // );
    // sum_area+=((int64_t)realx[x+1]-realx[x])*((int64_t)realy[y+1]-realy[y]);

    // if ( (x>=maxx-1) || (y>=maxy-1) )
    //{
    //	cout << "INF" << endl;
    // }

    if ((mm[x][y] & WALL_U) == 0) {
      if (x <= 0) {
        // can go outside...
        cout << "INF" << endl;
        return 0;
      }
      if ((mm[x - 1][y] & FOUND) == 0) {
        // fprintf(stderr,"\\tcan go up\\n");
        q.push(make_pair(x - 1, y));
        mm[x - 1][y] |= FOUND;
      }
    }
    if ((mm[x][y] & WALL_D) == 0) {
      if (x >= maxx - 1) {
        // can go outside...
        cout << "INF" << endl;
        return 0;
      }
      if ((mm[x + 1][y] & FOUND) == 0) {
        // fprintf(stderr,"\\tcan go down\\n");
        q.push(make_pair(x + 1, y));
        mm[x + 1][y] |= FOUND;
      }
    }
    if ((mm[x][y] & WALL_L) == 0) {
      if (y <= 0) {
        // can go outside...
        cout << "INF" << endl;
        return 0;
      }
      if ((mm[x][y - 1] & FOUND) == 0) {
        // fprintf(stderr,"\\tcan go left\\n");
        q.push(make_pair(x, y - 1));
        mm[x][y - 1] |= FOUND;
      }
    }
    if ((mm[x][y] & WALL_R) == 0) {
      if (y >= maxy - 1) {
        // can go outside...
        cout << "INF" << endl;
        return 0;
      }
      if ((mm[x][y + 1] & FOUND) == 0) {
        // fprintf(stderr,"\\tcan go right\\n");
        q.push(make_pair(x, y + 1));
        mm[x][y + 1] |= FOUND;
      }
    }
    sum_area +=
        ((int64_t)realx[x + 1] - realx[x]) * ((int64_t)realy[y + 1] - realy[y]);
  }
  cout << sum_area << endl;
}


Error: WA

Fixed submission:

#include <bits/stdc++.h>
using namespace std;

int main() {

  int n, m;
  cin >> n >> m;
  vector<tuple<int, int, int>> abc(n);
  for (int i = 0; i < n; ++i) {
    int a, b, c;
    cin >> a >> b >> c;
    abc[i] = make_tuple(a, b, c);
  }

  vector<tuple<int, int, int>> def(n);
  for (int i = 0; i < m; ++i) {
    int d, e, f;
    cin >> d >> e >> f;
    def[i] = make_tuple(d, e, f);
  }

  map<int, int> mx, my;
  mx[0] = 0;
  my[0] = 0;
  for (int i = 0; i < n; ++i) {
    int a, b, c;
    tie(a, b, c) = abc[i];
    mx[a] = 0;
    mx[b] = 0;
    my[c] = 0;
  }
  for (int i = 0; i < m; ++i) {
    int d, e, f;
    tie(d, e, f) = def[i];
    mx[d] = 0;
    my[e] = 0;
    my[f] = 0;
  }

  vector<int> realx(mx.size());
  int count = 0;
  // fprintf(stderr,"X:\\n");
  for (auto p : mx) {
    // fprintf(stderr,"%d %d\\n",count,p.first);
    mx[p.first] = count;
    realx[count] = p.first;
    ++count;
  }

  vector<int> realy(my.size());
  count = 0;
  // fprintf(stderr,"Y:\\n");
  for (auto p : my) {
    // fprintf(stderr,"%d %d\\n",count,p.first);
    my[p.first] = count;
    realy[count] = p.first;
    ++count;
  }

  vector<vector<int>> mm(mx.size(), vector<int>(my.size()));
  const int WALL_L = 0x01;
  const int WALL_U = 0x02;
  const int WALL_R = 0x04;
  const int WALL_D = 0x08;
  const int FOUND = 0x10;

  // place vertical walls
  for (int i = 0; i < n; ++i) {
    int a, b, c;
    tie(a, b, c) = abc[i];
    a = mx[a];
    b = mx[b];
    c = my[c];
    for (int j = a; j < b; ++j) {
      if (c > 0) {
        mm[j][c - 1] |= WALL_R;
      }
      mm[j][c] |= WALL_L;
    }
  }

  // place horizontal walls
  for (int i = 0; i < m; ++i) {
    int d, e, f;
    tie(d, e, f) = def[i];
    d = mx[d];
    e = my[e];
    f = my[f];
    for (int j = e; j < f; ++j) {
      if (d > 0) {
        mm[d - 1][j] |= WALL_D;
      }
      mm[d][j] |= WALL_U;
    }
  }

  return 0;

  if ((mx[0] == 0) || (mx[0] == mx.size() - 1) || (my[0] == 0) ||
      (my[0] == my.size() - 1)) {
    cout << "INF" << endl;
    return 0;
  }

  int x = mx[0], y = my[0];
  int64_t sum_area = 0;
  int maxx = mx.size();
  int maxy = my.size();
  queue<pair<int, int>> q;

  q.push(make_pair(x, y));
  mm[x][y] |= FOUND;
  while (q.empty() == false) {
    pair<int, int> p = q.front();
    q.pop();
    x = p.first;
    y = p.second;
    // fprintf(stderr,"%d,%d (%d,%d) - %d,%d (%d,%d)
    // (%d)\\n",x,y,realx[x],realy[y],x+1,y+1,realx[x+1],realy[y+1],(realx[x+1]-realx[x])*(realy[y+1]-realy[y])
    // );
    // sum_area+=((int64_t)realx[x+1]-realx[x])*((int64_t)realy[y+1]-realy[y]);

    // if ( (x>=maxx-1) || (y>=maxy-1) )
    //{
    //	cout << "INF" << endl;
    // }

    if ((mm[x][y] & WALL_U) == 0) {
      if (x <= 0) {
        // can go outside...
        cout << "INF" << endl;
        return 0;
      }
      if ((mm[x - 1][y] & FOUND) == 0) {
        // fprintf(stderr,"\\tcan go up\\n");
        q.push(make_pair(x - 1, y));
        mm[x - 1][y] |= FOUND;
      }
    }
    if ((mm[x][y] & WALL_D) == 0) {
      if (x >= maxx - 1) {
        // can go outside...
        cout << "INF" << endl;
        return 0;
      }
      if ((mm[x + 1][y] & FOUND) == 0) {
        // fprintf(stderr,"\\tcan go down\\n");
        q.push(make_pair(x + 1, y));
        mm[x + 1][y] |= FOUND;
      }
    }
    if ((mm[x][y] & WALL_L) == 0) {
      if (y <= 0) {
        // can go outside...
        cout << "INF" << endl;
        return 0;
      }
      if ((mm[x][y - 1] & FOUND) == 0) {
        // fprintf(stderr,"\\tcan go left\\n");
        q.push(make_pair(x, y - 1));
        mm[x][y - 1] |= FOUND;
      }
    }
    if ((mm[x][y] & WALL_R) == 0) {
      if (y >= maxy - 1) {
        // can go outside...
        cout << "INF" << endl;
        return 0;
      }
      if ((mm[x][y + 1] & FOUND) == 0) {
        // fprintf(stderr,"\\tcan go right\\n");
        q.push(make_pair(x, y + 1));
        mm[x][y + 1] |= FOUND;
      }
    }
    sum_area +=
        ((int64_t)realx[x + 1] - realx[x]) * ((int64_t)realy[y + 1] - realy[y]);
  }
  cout << sum_area << endl;
}