Problem p03685 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <iostream>
#include <stack>
#define mem(a, b) memset(a, b, sizeof(a))
#define For(i, a, b) for (int i = a, i##E = b; i <= i##E; ++i)
#define rFor(i, a, b) for (int i = a, i##E = b; i >= i##E; --i)
using namespace std;
typedef long long LL;
const int N = 200010;
template <typename T> inline T chkmax(T A, T B) { return A > B ? A : B; }
template <typename T> inline T chkmin(T A, T B) { return A < B ? A : B; }
template <typename T> inline void read(T &x) {
  x = 0;
  int _f(0);
  char ch = getchar();
  while (!isdigit(ch))
    _f |= (ch == '-'), ch = getchar();
  while (isdigit(ch))
    x = x * 10 + ch - '0', ch = getchar();
  x = _f ? -x : x;
}
inline void file() {
#ifndef ONLINE_JUDGE
  freopen("in.txt", "r", stdin);
  freopen("out.txt", "w", stdout);
#endif
}
int n, cnt, R, C;
stack<int> st;
struct node {
  int x, id, flag;
  node() {}
  node(int A, int B, int C) : x(A), id(B), flag(C) {}
} a[N];
inline bool operator<(node A, node B) { return A.x < B.x; }
inline bool OnBoundary(int x, int y) {
  return x == 0 || x == R || y == 0 || y == C;
}
inline int find(int x, int y) {
  if (x == 0)
    return y;
  if (x == R)
    return C + R + C - y;
  if (y == 0)
    return R + R + C + C - x;
  if (y == C)
    return C + x;
}
int main() {
  int x1, y1, x2, y2;
  file();
  read(R), read(C), read(n);
  For(i, 1, n) {
    read(x1), read(y1), read(x2), read(y2);
    if (OnBoundary(x1, y1) && OnBoundary(x2, y2)) {
      int l = find(x1, y1), r = find(x2, y2);
      if (l > r)
        swap(l, r);
      a[++cnt] = node(l, i, 0);
      a[++cnt] = node(r, i, 1);
    }
  }
  sort(a + 1, a + cnt + 1);
  For(i, 1, cnt) {
    if (a[i].flag) {
      if (st.top() != a[i].id)
        return puts("NO"), 0;
      else
        st.pop();
    } else
      st.push(a[i].id);
  }
  puts("YES");
  return 0;
}


Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <iostream>
#include <stack>
#define mem(a, b) memset(a, b, sizeof(a))
#define For(i, a, b) for (int i = a, i##E = b; i <= i##E; ++i)
#define rFor(i, a, b) for (int i = a, i##E = b; i >= i##E; --i)
using namespace std;
typedef long long LL;
const int N = 200010;
template <typename T> inline T chkmax(T A, T B) { return A > B ? A : B; }
template <typename T> inline T chkmin(T A, T B) { return A < B ? A : B; }
template <typename T> inline void read(T &x) {
  x = 0;
  int _f(0);
  char ch = getchar();
  while (!isdigit(ch))
    _f |= (ch == '-'), ch = getchar();
  while (isdigit(ch))
    x = x * 10 + ch - '0', ch = getchar();
  x = _f ? -x : x;
}
inline void file() {
#ifndef ONLINE_JUDGE
  freopen("in.txt", "r", stdin);
  freopen("out.txt", "w", stdout);
#endif
}
int n, cnt, R, C;
stack<int> st;
struct node {
  int x, id, flag;
  node() {}
  node(int A, int B, int C) : x(A), id(B), flag(C) {}
} a[N];
inline bool operator<(node A, node B) { return A.x < B.x; }
inline bool OnBoundary(int x, int y) {
  return x == 0 || x == R || y == 0 || y == C;
}
inline int find(int x, int y) {
  if (x == 0)
    return y;
  if (x == R)
    return C + R + C - y;
  if (y == 0)
    return R + R + C + C - x;
  if (y == C)
    return C + x;
}
int main() {
  int x1, y1, x2, y2;
  read(R), read(C), read(n);
  For(i, 1, n) {
    read(x1), read(y1), read(x2), read(y2);
    if (OnBoundary(x1, y1) && OnBoundary(x2, y2)) {
      int l = find(x1, y1), r = find(x2, y2);
      if (l > r)
        swap(l, r);
      a[++cnt] = node(l, i, 0);
      a[++cnt] = node(r, i, 1);
    }
  }
  sort(a + 1, a + cnt + 1);
  For(i, 1, cnt) {
    if (a[i].flag) {
      if (st.top() != a[i].id)
        return puts("NO"), 0;
      else
        st.pop();
    } else
      st.push(a[i].id);
  }
  puts("YES");
  return 0;
}