Problem p03685 (C++)

Buggy submission (status: Runtime Error):


#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;

#define REP(i, n) for (int i = 1; i <= int(n); i++)
#define MP make_pair
#define PB push_back
#define SZ(x) (int((x).size()))
#define ALL(x) (x).begin(), (x).end()
#define X first
#define Y second
template <typename T> inline bool chkmin(T &a, const T &b) {
  return a > b ? a = b, 1 : 0;
}
template <typename T> inline bool chkmax(T &a, const T &b) {
  return a < b ? a = b, 1 : 0;
}

typedef long long LL;
typedef pair<int, int> pii;
typedef long double LD;
const int INF = 0x3f3f3f3f;

template <class T> inline bool RD(T &ret) {
  char c;
  int sgn;
  if (c = getchar(), c == EOF)
    return 0;
  while (c != '-' && (c < '0' || c > '9'))
    c = getchar();
  sgn = (c == '-') ? -1 : 1, ret = (c == '-') ? 0 : (c - '0');
  while (c = getchar(), c >= '0' && c <= '9')
    ret = ret * 10 + (c - '0');
  ret *= sgn;
  return 1;
}
template <class T> inline void PT(T x) {
  if (x < 0)
    putchar('-'), x = -x;
  if (x > 9)
    PT(x / 10);
  putchar(x % 10 + '0');
}

const int N = 1e5 + 100;

pii a[N];
int cnt;
int R, C, n;
void insert(int x, int y, int id) {
  if (x == 0) {
    a[++cnt] = pii(y, id);
  } else if (x == R) {
    a[++cnt] = pii(2 * C + R - y, id);
  } else if (y == C) {
    a[++cnt] = pii(C + x, id);
  } else if (y == 0) {
    a[++cnt] = pii(2 * C + 2 * R - x, id);
  }
}

vector<int> vec;
int main() {
  cin >> R >> C >> n;
  for (int i = 1; i <= n; i++) {
    int x1, y1, x2, y2;
    scanf("%d %d %d %d", &x1, &y1, &x2, &y2);
    if (pii(x1, y1) > pii(x2, y2))
      swap(x1, x2), swap(y1, y2);
    if ((x1 == 0 || x1 == R || y1 == 0 || y1 == C) &&
        (x2 == 0 || x2 == R || y2 == 0 || y2 == C)) {
      insert(x1, y1, i);
      insert(x2, y2, i);
    }
  }
  sort(a + 1, a + cnt + 1);
  for (int i = 1; i <= cnt; i++) {
    if (vec.empty() || vec[SZ(vec) - 1] != a[i].Y)
      vec.push_back(a[i].Y);
    else
      vec.pop_back();
  }
  puts(SZ(vec) ? "NO" : "YES");
}


Fixed submission:


#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;

#define REP(i, n) for (int i = 1; i <= int(n); i++)
#define MP make_pair
#define PB push_back
#define SZ(x) (int((x).size()))
#define ALL(x) (x).begin(), (x).end()
#define X first
#define Y second
template <typename T> inline bool chkmin(T &a, const T &b) {
  return a > b ? a = b, 1 : 0;
}
template <typename T> inline bool chkmax(T &a, const T &b) {
  return a < b ? a = b, 1 : 0;
}

typedef long long LL;
typedef pair<int, int> pii;
typedef long double LD;
const int INF = 0x3f3f3f3f;

template <class T> inline bool RD(T &ret) {
  char c;
  int sgn;
  if (c = getchar(), c == EOF)
    return 0;
  while (c != '-' && (c < '0' || c > '9'))
    c = getchar();
  sgn = (c == '-') ? -1 : 1, ret = (c == '-') ? 0 : (c - '0');
  while (c = getchar(), c >= '0' && c <= '9')
    ret = ret * 10 + (c - '0');
  ret *= sgn;
  return 1;
}
template <class T> inline void PT(T x) {
  if (x < 0)
    putchar('-'), x = -x;
  if (x > 9)
    PT(x / 10);
  putchar(x % 10 + '0');
}

const int N = 1e6 + 100;

pii a[N];
int cnt;
int R, C, n;
void insert(int x, int y, int id) {
  if (x == 0) {
    a[++cnt] = pii(y, id);
  } else if (x == R) {
    a[++cnt] = pii(2 * C + R - y, id);
  } else if (y == C) {
    a[++cnt] = pii(C + x, id);
  } else if (y == 0) {
    a[++cnt] = pii(2 * C + 2 * R - x, id);
  }
}

vector<int> vec;
int main() {
  cin >> R >> C >> n;
  for (int i = 1; i <= n; i++) {
    int x1, y1, x2, y2;
    scanf("%d %d %d %d", &x1, &y1, &x2, &y2);
    if (pii(x1, y1) > pii(x2, y2))
      swap(x1, x2), swap(y1, y2);
    if ((x1 == 0 || x1 == R || y1 == 0 || y1 == C) &&
        (x2 == 0 || x2 == R || y2 == 0 || y2 == C)) {
      insert(x1, y1, i);
      insert(x2, y2, i);
    }
  }
  sort(a + 1, a + cnt + 1);
  for (int i = 1; i <= cnt; i++) {
    if (vec.empty() || vec[SZ(vec) - 1] != a[i].Y)
      vec.push_back(a[i].Y);
    else
      vec.pop_back();
  }
  puts(SZ(vec) ? "NO" : "YES");
}