Problem p00525 (C++)

Buggy submission (status: Runtime Error):
#pragma comment(linker, "/STACK:36777216")
// #pragma GCC optimize ("O2")
#define LOCAL
// #include "testlib.h"

#include <algorithm>
#include <cstdio>
#include <iostream>
#include <set>
#include <vector>
// #include <tr1/unordered_set>
// #include <tr1/unordered_map>
// #include <array>

using namespace std;

#define REP(i, n) for (int i = 0; i < n; ++i)
#define FOR(i, a, b) for (int i = a; i < b; ++i)
#define DWN(i, b, a) for (int i = b - 1; i >= a; --i)
#define REP_1(i, n) for (int i = 1; i <= n; ++i)
#define FOR_1(i, a, b) for (int i = a; i <= b; ++i)
#define DWN_1(i, b, a) for (int i = b; i >= a; --i)
#define REP_C(i, n) for (int n____ = n, i = 0; i < n____; ++i)
#define FOR_C(i, a, b) for (int b____ = b, i = a; i < b____; ++i)
#define DWN_C(i, b, a) for (int a____ = a, i = b - 1; i >= a____; --i)
#define REP_N(i, n) for (i = 0; i < n; ++i)
#define FOR_N(i, a, b) for (i = a; i < b; ++i)
#define DWN_N(i, b, a) for (i = b - 1; i >= a; --i)
#define REP_1_C(i, n) for (int n____ = n, i = 1; i <= n____; ++i)
#define FOR_1_C(i, a, b) for (int b____ = b, i = a; i <= b____; ++i)
#define DWN_1_C(i, b, a) for (int a____ = a, i = b; i >= a____; --i)
#define REP_1_N(i, n) for (i = 1; i <= n; ++i)
#define FOR_1_N(i, a, b) for (i = a; i <= b; ++i)
#define DWN_1_N(i, b, a) for (i = b; i >= a; --i)
#define REP_C_N(i, n) for (int n____ = (i = 0, n); i < n____; ++i)
#define FOR_C_N(i, a, b) for (int b____ = (i = 0, b); i < b____; ++i)
#define DWN_C_N(i, b, a) for (int a____ = (i = b - 1, a); i >= a____; --i)
#define REP_1_C_N(i, n) for (int n____ = (i = 1, n); i <= n____; ++i)
#define FOR_1_C_N(i, a, b) for (int b____ = (i = a, b); i <= b____; ++i)
#define DWN_1_C_N(i, b, a) for (int a____ = (i = b, a); i >= a____; --i)

#define ECH(it, A)                                                             \
  for (__typeof((A).begin()) it = (A).begin(); it != (A).end(); ++it)
#define rECH(it, A)                                                            \
  for (__typeof((A).rbegin()) it = (A).rbegin(); it != (A).rend(); ++it)
#define REP_S(i, str) for (char *i = str; *i; ++i)
#define REP_L(i, hd, suc) for (int i = hd; i; i = suc[i])
#define REP_G(i, u) REP_L(i, hd[u], suc)
#define REP_SS(x, s) for (int x = s; x; x = (x - 1) & s)
#define DO(n) for (int ____n = n; ____n-- > 0;)
#define REP_2(i, j, n, m) REP(i, n) REP(j, m)
#define REP_2_1(i, j, n, m) REP_1(i, n) REP_1(j, m)
#define REP_3(i, j, k, n, m, l) REP(i, n) REP(j, m) REP(k, l)
#define REP_3_1(i, j, k, n, m, l) REP_1(i, n) REP_1(j, m) REP_1(k, l)
#define REP_4(i, j, k, ii, n, m, l, nn)                                        \
  REP(i, n) REP(j, m) REP(k, l) REP(ii, nn)
#define REP_4_1(i, j, k, ii, n, m, l, nn)                                      \
  REP_1(i, n) REP_1(j, m) REP_1(k, l) REP_1(ii, nn)

#define ALL(A) A.begin(), A.end()
#define LLA(A) A.rbegin(), A.rend()
#define CPY(A, B) memcpy(A, B, sizeof(A))
#define INS(A, P, B) A.insert(A.begin() + P, B)
#define ERS(A, P) A.erase(A.begin() + P)
#define LBD(A, x) (lower_bound(ALL(A), x) - A.begin())
#define UBD(A, x) (upper_bound(ALL(A), x) - A.begin())
#define CTN(T, x) (T.find(x) != T.end())
#define SZ(A) int((A).size())
#define PB push_back
#define MP(A, B) make_pair(A, B)
#define PTT pair<T, T>
#define Ts *this
#define rTs return Ts
#define fi first
#define se second
#define re real()
#define im imag()

#define Rush for (int ____T = RD(); ____T--;)
#define Display(A, n, m)                                                       \
  {                                                                            \
    REP(i, n) {                                                                \
      REP(j, m - 1) cout << A[i][j] << " ";                                    \
      cout << A[i][m - 1] << endl;                                             \
    }                                                                          \
  }
#define Display_1(A, n, m)                                                     \
  {                                                                            \
    REP_1(i, n) {                                                              \
      REP_1(j, m - 1) cout << A[i][j] << " ";                                  \
      cout << A[i][m] << endl;                                                 \
    }                                                                          \
  }

typedef long long LL;
// typedef long double DB;
typedef double DB;
typedef unsigned uint;
typedef unsigned long long uLL;

typedef vector<int> VI;

template <class T> inline T &RD(T &);
template <class T> inline void OT(const T &);
// inline int RD(){int x; return RD(x);}
inline LL RD() {
  LL x;
  return RD(x);
}
inline DB &RF(DB &);
inline DB RF() {
  DB x;
  return RF(x);
}
inline char *RS(char *s);
inline char &RC(char &c);
inline char RC();
inline char &RC(char &c) {
  scanf(" %c", &c);
  return c;
}
inline char RC() {
  char c;
  return RC(c);
}
// inline char& RC(char &c){c = getchar(); return c;}
// inline char RC(){return getchar();}

template <class T> inline T &RDD(T &);
inline LL RDD() {
  LL x;
  return RDD(x);
}

template <class T0, class T1> inline T0 &RD(T0 &x0, T1 &x1) {
  RD(x0), RD(x1);
  return x0;
}
template <class T0, class T1, class T2> inline T0 &RD(T0 &x0, T1 &x1, T2 &x2) {
  RD(x0), RD(x1), RD(x2);
  return x0;
}
template <class T0, class T1, class T2, class T3>
inline T0 &RD(T0 &x0, T1 &x1, T2 &x2, T3 &x3) {
  RD(x0), RD(x1), RD(x2), RD(x3);
  return x0;
}
template <class T0, class T1, class T2, class T3, class T4>
inline T0 &RD(T0 &x0, T1 &x1, T2 &x2, T3 &x3, T4 &x4) {
  RD(x0), RD(x1), RD(x2), RD(x3), RD(x4);
  return x0;
}
template <class T0, class T1, class T2, class T3, class T4, class T5>
inline T0 &RD(T0 &x0, T1 &x1, T2 &x2, T3 &x3, T4 &x4, T5 &x5) {
  RD(x0), RD(x1), RD(x2), RD(x3), RD(x4), RD(x5);
  return x0;
}
template <class T0, class T1, class T2, class T3, class T4, class T5, class T6>
inline T0 &RD(T0 &x0, T1 &x1, T2 &x2, T3 &x3, T4 &x4, T5 &x5, T6 &x6) {
  RD(x0), RD(x1), RD(x2), RD(x3), RD(x4), RD(x5), RD(x6);
  return x0;
}
template <class T0, class T1> inline void OT(const T0 &x0, const T1 &x1) {
  OT(x0), OT(x1);
}
template <class T0, class T1, class T2>
inline void OT(const T0 &x0, const T1 &x1, const T2 &x2) {
  OT(x0), OT(x1), OT(x2);
}
template <class T0, class T1, class T2, class T3>
inline void OT(const T0 &x0, const T1 &x1, const T2 &x2, const T3 &x3) {
  OT(x0), OT(x1), OT(x2), OT(x3);
}
template <class T0, class T1, class T2, class T3, class T4>
inline void OT(const T0 &x0, const T1 &x1, const T2 &x2, const T3 &x3,
               const T4 &x4) {
  OT(x0), OT(x1), OT(x2), OT(x3), OT(x4);
}
template <class T0, class T1, class T2, class T3, class T4, class T5>
inline void OT(const T0 &x0, const T1 &x1, const T2 &x2, const T3 &x3,
               const T4 &x4, const T5 &x5) {
  OT(x0), OT(x1), OT(x2), OT(x3), OT(x4), OT(x5);
}
template <class T0, class T1, class T2, class T3, class T4, class T5, class T6>
inline void OT(const T0 &x0, const T1 &x1, const T2 &x2, const T3 &x3,
               const T4 &x4, const T5 &x5, const T6 &x6) {
  OT(x0), OT(x1), OT(x2), OT(x3), OT(x4), OT(x5), OT(x6);
}
inline char &RC(char &a, char &b) {
  RC(a), RC(b);
  return a;
}
inline char &RC(char &a, char &b, char &c) {
  RC(a), RC(b), RC(c);
  return a;
}
inline char &RC(char &a, char &b, char &c, char &d) {
  RC(a), RC(b), RC(c), RC(d);
  return a;
}
inline char &RC(char &a, char &b, char &c, char &d, char &e) {
  RC(a), RC(b), RC(c), RC(d), RC(e);
  return a;
}
inline char &RC(char &a, char &b, char &c, char &d, char &e, char &f) {
  RC(a), RC(b), RC(c), RC(d), RC(e), RC(f);
  return a;
}
inline char &RC(char &a, char &b, char &c, char &d, char &e, char &f, char &g) {
  RC(a), RC(b), RC(c), RC(d), RC(e), RC(f), RC(g);
  return a;
}
inline DB &RF(DB &a, DB &b) {
  RF(a), RF(b);
  return a;
}
inline DB &RF(DB &a, DB &b, DB &c) {
  RF(a), RF(b), RF(c);
  return a;
}
inline DB &RF(DB &a, DB &b, DB &c, DB &d) {
  RF(a), RF(b), RF(c), RF(d);
  return a;
}
inline DB &RF(DB &a, DB &b, DB &c, DB &d, DB &e) {
  RF(a), RF(b), RF(c), RF(d), RF(e);
  return a;
}
inline DB &RF(DB &a, DB &b, DB &c, DB &d, DB &e, DB &f) {
  RF(a), RF(b), RF(c), RF(d), RF(e), RF(f);
  return a;
}
inline DB &RF(DB &a, DB &b, DB &c, DB &d, DB &e, DB &f, DB &g) {
  RF(a), RF(b), RF(c), RF(d), RF(e), RF(f), RF(g);
  return a;
}
inline void RS(char *s1, char *s2) { RS(s1), RS(s2); }
inline void RS(char *s1, char *s2, char *s3) { RS(s1), RS(s2), RS(s3); }
template <class T0, class T1> inline T0 &RDD(T0 &a, T1 &b) {
  RDD(a), RDD(b);
  return a;
}
template <class T0, class T1, class T2> inline T1 &RDD(T0 &a, T1 &b, T2 &c) {
  RDD(a), RDD(b), RDD(c);
  return a;
}

template <class T> inline void RST(T &A) { memset(A, 0, sizeof(A)); }
template <class T> inline void FLC(T &A, int x) { memset(A, x, sizeof(A)); }
template <class T> inline void CLR(T &A) { A.clear(); }

template <class T0, class T1> inline void RST(T0 &A0, T1 &A1) {
  RST(A0), RST(A1);
}
template <class T0, class T1, class T2>
inline void RST(T0 &A0, T1 &A1, T2 &A2) {
  RST(A0), RST(A1), RST(A2);
}
template <class T0, class T1, class T2, class T3>
inline void RST(T0 &A0, T1 &A1, T2 &A2, T3 &A3) {
  RST(A0), RST(A1), RST(A2), RST(A3);
}
template <class T0, class T1, class T2, class T3, class T4>
inline void RST(T0 &A0, T1 &A1, T2 &A2, T3 &A3, T4 &A4) {
  RST(A0), RST(A1), RST(A2), RST(A3), RST(A4);
}
template <class T0, class T1, class T2, class T3, class T4, class T5>
inline void RST(T0 &A0, T1 &A1, T2 &A2, T3 &A3, T4 &A4, T5 &A5) {
  RST(A0), RST(A1), RST(A2), RST(A3), RST(A4), RST(A5);
}
template <class T0, class T1, class T2, class T3, class T4, class T5, class T6>
inline void RST(T0 &A0, T1 &A1, T2 &A2, T3 &A3, T4 &A4, T5 &A5, T6 &A6) {
  RST(A0), RST(A1), RST(A2), RST(A3), RST(A4), RST(A5), RST(A6);
}
template <class T0, class T1> inline void FLC(T0 &A0, T1 &A1, int x) {
  FLC(A0, x), FLC(A1, x);
}
template <class T0, class T1, class T2>
inline void FLC(T0 &A0, T1 &A1, T2 &A2, int x) {
  FLC(A0, x), FLC(A1, x), FLC(A2, x);
}
template <class T0, class T1, class T2, class T3>
inline void FLC(T0 &A0, T1 &A1, T2 &A2, T3 &A3, int x) {
  FLC(A0, x), FLC(A1, x), FLC(A2, x), FLC(A3, x);
}
template <class T0, class T1, class T2, class T3, class T4>
inline void FLC(T0 &A0, T1 &A1, T2 &A2, T3 &A3, T4 &A4, int x) {
  FLC(A0, x), FLC(A1, x), FLC(A2, x), FLC(A3, x), FLC(A4, x);
}
template <class T0, class T1, class T2, class T3, class T4, class T5>
inline void FLC(T0 &A0, T1 &A1, T2 &A2, T3 &A3, T4 &A4, T5 &A5, int x) {
  FLC(A0, x), FLC(A1, x), FLC(A2, x), FLC(A3, x), FLC(A4, x), FLC(A5, x);
}
template <class T0, class T1, class T2, class T3, class T4, class T5, class T6>
inline void FLC(T0 &A0, T1 &A1, T2 &A2, T3 &A3, T4 &A4, T5 &A5, T6 &A6, int x) {
  FLC(A0, x), FLC(A1, x), FLC(A2, x), FLC(A3, x), FLC(A4, x), FLC(A5, x),
      FLC(A6, x);
}

template <class T0, class T1> inline void CLR(T0 &A0, T1 &A1) {
  CLR(A0), CLR(A1);
}
template <class T0, class T1, class T2>
inline void CLR(T0 &A0, T1 &A1, T2 &A2) {
  CLR(A0), CLR(A1), CLR(A2);
}
template <class T0, class T1, class T2, class T3>
inline void CLR(T0 &A0, T1 &A1, T2 &A2, T3 &A3) {
  CLR(A0), CLR(A1), CLR(A2), CLR(A3);
}
template <class T0, class T1, class T2, class T3, class T4>
inline void CLR(T0 &A0, T1 &A1, T2 &A2, T3 &A3, T4 &A4) {
  CLR(A0), CLR(A1), CLR(A2), CLR(A3), CLR(A4);
}
template <class T0, class T1, class T2, class T3, class T4, class T5>
inline void CLR(T0 &A0, T1 &A1, T2 &A2, T3 &A3, T4 &A4, T5 &A5) {
  CLR(A0), CLR(A1), CLR(A2), CLR(A3), CLR(A4), CLR(A5);
}
template <class T0, class T1, class T2, class T3, class T4, class T5, class T6>
inline void CLR(T0 &A0, T1 &A1, T2 &A2, T3 &A3, T4 &A4, T5 &A5, T6 &A6) {
  CLR(A0), CLR(A1), CLR(A2), CLR(A3), CLR(A4), CLR(A5), CLR(A6);
}
template <class T> inline void CLR(T &A, int n) { REP(i, n) CLR(A[i]); }

template <class T> inline bool EPT(T &a) { return a.empty(); }
template <class T> inline T &SRT(T &A) {
  sort(ALL(A));
  return A;
}
template <class T, class C> inline T &SRT(T &A, C B) {
  sort(ALL(A), B);
  return A;
}
template <class T> inline T &RVS(T &A) {
  reverse(ALL(A));
  return A;
}
template <class T> inline T &UNQQ(T &A) {
  A.resize(unique(ALL(A)) - A.begin());
  return A;
}
template <class T> inline T &UNQ(T &A) {
  SRT(A);
  return UNQQ(A);
}

//}

/** Constant List .. **/ //{

const int MOD = int(1e9) + 7;
const int INF = 0x3f3f3f3f;
const LL INFF = 0x3f3f3f3f3f3f3f3fLL;
const DB EPS = 1e-9;
const DB OO = 1e20;

const int dx[] = {-1, 0, 1, 0};
const int dy[] = {0, 1, 0, -1};

//}

/** Add On .. **/ //{
// <<= '0. Nichi Joo ., //{

template <class T> inline bool checkMin(T &a, const T b) {
  return b < a ? a = b, 1 : 0;
}
template <class T> inline bool checkMax(T &a, const T b) {
  return a < b ? a = b, 1 : 0;
}
template <class T, class C> inline bool checkUpd(T &a, const T b, C c) {
  return c(b, a) ? a = b, 1 : 0;
}
template <class T> inline T min(T a, T b, T c) { return min(min(a, b), c); }
template <class T> inline T max(T a, T b, T c) { return max(max(a, b), c); }
template <class T> inline T min(T a, T b, T c, T d) {
  return min(min(a, b), min(c, d));
}
template <class T> inline T max(T a, T b, T c, T d) {
  return max(max(a, b), max(c, d));
}
template <class T> inline T min(T a, T b, T c, T d, T e) {
  return min(min(min(a, b), min(c, d)), e);
}
template <class T> inline T max(T a, T b, T c, T d, T e) {
  return max(max(max(a, b), max(c, d)), e);
}
template <class T> inline T sqr(T a) { return a * a; }
template <class T> inline T cub(T a) { return a * a * a; }
template <class T> inline T ceil(T x, T y) { return (x - 1) / y + 1; }
template <class T> T abs(T x) { return x > 0 ? x : -x; }
inline int sgn(DB x) { return x < -EPS ? -1 : x > EPS; }
inline int sgn(DB x, DB y) { return sgn(x - y); }
//}

//}

/** I/O Accelerator Interface .. **/ //{
#define g (c = getchar())
#define d isdigit(g)
#define p x = x * 10 + c - '0'
#define n x = x * 10 + '0' - c
#define pp l /= 10, p
#define nn l /= 10, n
template <class T> inline T &RD(T &x) {
  char c;
  while (!d)
    ;
  x = c - '0';
  while (d)
    p;
  return x;
}
template <class T> inline T &RDD(T &x) {
  char c;
  while (g, c != '-' && !isdigit(c))
    ;
  if (c == '-') {
    x = '0' - g;
    while (d)
      n;
  } else {
    x = c - '0';
    while (d)
      p;
  }
  return x;
}
inline DB &RF(DB &x) {
  // scanf("%lf", &x);
  char c;
  while (g, c != '-' && c != '.' && !isdigit(c))
    ;
  if (c == '-')
    if (g == '.') {
      x = 0;
      DB l = 1;
      while (d)
        nn;
      x *= l;
    } else {
      x = '0' - c;
      while (d)
        n;
      if (c == '.') {
        DB l = 1;
        while (d)
          nn;
        x *= l;
      }
    }
  else if (c == '.') {
    x = 0;
    DB l = 1;
    while (d)
      pp;
    x *= l;
  } else {
    x = c - '0';
    while (d)
      p;
    if (c == '.') {
      DB l = 1;
      while (d)
        pp;
      x *= l;
    }
  }
  return x;
}
#undef nn
#undef pp
#undef n
#undef p
#undef d
#undef g
inline char *RS(char *s) {
  // gets(s);
  scanf("%s", s);
  return s;
}

LL last_ans;
int Case;
template <class T> inline void OT(const T &x) {
  // printf("Case #%d: ", ++Case);
  // printf("%lld\n", x);
  // printf("%.9f\n", x);
  // printf("%d\n", x);
  cout << x << endl;
  // last_ans = x;
}

//}/*
//..................................................................................................................................
//*/

const int MAXN = 100020;

vector<int> xs;

namespace ST {
const int NN = 4 * MAXN;
bool T[NN];
int a, b;

#define lx (x << 1)
#define rx (lx | 1)
#define ml (l + r >> 1)
#define mr (ml + 1)
#define lc lx, l, ml
#define rc rx, mr, r
#define rt 1, 0, xs.size()

void cov(int x) { T[x] = 1; }

void rls(int x) {
  if (T[x]) {
    T[lx] = T[rx] = 1;
    T[x] = 0;
  }
}

bool Query(int x, int l, int r, int p) {
  if (l == r) {
    bool z = T[x];
    T[x] = 0;
    return z;
  } else {
    rls(x);
    return p < mr ? Query(lc, p) : Query(rc, p);
  }
}
bool Query(int p) { return Query(rt, p); }

void Insert(int x, int l, int r) {
  if (b < l || r < a)
    return;
  if (a <= l && r <= b) {
    cov(x);
  } else {
    rls(x);
    Insert(lc);
    Insert(rc);
  }
}
void Insert(int _a, int _b) {
  a = _a, b = _b;
  // cout << " Insert: " << a << " " << b << " " << xs.size() << endl;
  Insert(rt);
}

void Clear(int x, int l, int r, int p) {
  if (l == r) {
    T[x] = 0;
  } else {
    rls(x);
    if (p < mr)
      Clear(lc, p);
    else
      Clear(rc, p);
  }
}
} // namespace ST

struct seg_tree {
  static const int DEPTH = 19;
  static const int SIZE = 1 << DEPTH;

  int bit[1 << (DEPTH + 1)];
  int renew[1 << (DEPTH + 1)];

  seg_tree() {}

  void init() {
    for (int i = 0; i < 2 * SIZE; i++)
      bit[i] = renew[i] = 0;
  }

  void add(int p, int v) {
    p += SIZE;
    while (p) {
      bit[p] += v;
      p >>= 1;
    }
  }

  int query(int l, int r) {
    l += SIZE;
    r += SIZE;
    int ret = 0;

    while (l < r) {
      if (l & 1)
        ret += bit[l++];
      if (r & 1)
        ret += bit[--r];
      l >>= 1;
      r >>= 1;
    }

    return ret;
  }

  void set_renew(int l, int r) {
    l += SIZE;
    r += SIZE;

    while (l < r) {
      if (l & 1)
        renew[l++] = 1;
      if (r & 1)
        renew[--r] = 1;
      l >>= 1;
      r >>= 1;
    }
  }

  bool is_renew(int p) {
    p += SIZE;
    while (p) {
      if (renew[p])
        return true;
      p >>= 1;
    }
    return false;
  }

  void unset_renew(int p) {
    p += SIZE;
    for (int i = DEPTH - 1; i > 0; i--) {
      if (renew[p >> i]) {
        renew[p >> i] = 0;
        renew[(p >> i) * 2] = renew[(p >> i) * 2 + 1] = 1;
      }
    }

    renew[p] = 0;
  }
};

struct action {
  int pos, act, left, right;

  action(int p, int a, int l, int r) {
    pos = p;
    act = a;
    left = l;
    right = r;
  }
};

inline bool operator<(const action &a, const action &b) {
  return a.pos < b.pos || (a.pos == b.pos && a.act < b.act);
}

int W, H, N;
int x1[MAXN], y1[MAXN], x2[MAXN], y2[MAXN];
vector<int> uf;
seg_tree seg;
int target[MAXN * 2];

int root(int p) {
  // cout << p << " " << uf[p] << endl;
  return (uf[p] == p) ? p : (uf[p] = root(uf[p]));
}

bool join(int p, int q) {
  p = root(p);
  q = root(q);
  if (p == q)
    return false;
  uf[q] = p;
  return true;
}

void adjust(int p) {
  // if(seg.is_renew(p)){

  // cout << " " << p << endl;

  // cout << "   " << p << " "<< ST::Query(p) << " " << seg.is_renew(p) << endl;

  // if (ST::Query(p)){
  if (seg.is_renew(p)) {

    // cout << "?!" << endl;

    int t = uf.size();
    uf.push_back(t);
    seg.unset_renew(p);
    target[p] = t;
  }
}

template <class T> inline T low_bit(T x) { return x & -x; }

namespace BIT {
const int N = MAXN * 2;
int C[N], n;
void Add(int x, int d) {
  for (; x <= n; x += low_bit(x))
    C[x] += d;
}
int Sum(int x) {
  int res = 0;
  for (; x; x ^= low_bit(x))
    res += C[x];
  return res;
}
int Sum(int l, int r) { return Sum(r) - Sum(l - 1); }

} // namespace BIT

int main() {

#ifndef ONLINE_JUDGE
  // freopen("in.txt", "r", stdin);
  // freopen("out.txt", "w", stdout);
#endif

  scanf("%d%d%d", &W, &H, &N);
  // N = 0;
  for (int i = 0; i < N; i++) {
    scanf("%d%d%d%d", x1 + i, y1 + i, x2 + i, y2 + i);
    if (x1[i] > x2[i])
      swap(x1[i], x2[i]);
    if (y1[i] > y2[i])
      swap(y1[i], y2[i]);
  }
  // FLC(for(int i=0;i<2*N;i++) target[i] = -1;

  x1[N] = 0;
  y1[N] = 0;
  x2[N] = W;
  y2[N] = 0;
  x1[N + 1] = 0;
  y1[N + 1] = 0;
  x2[N + 1] = 0;
  y2[N + 1] = H;
  x1[N + 2] = W;
  y1[N + 2] = 0;
  x2[N + 2] = W;
  y2[N + 2] = H;
  x1[N + 3] = 0;
  y1[N + 3] = H;
  x2[N + 3] = W;
  y2[N + 3] = H;
  N += 4;

  for (int i = 0; i < N; i++) {
    xs.push_back(x1[i]);
    xs.push_back(x2[i]);
  }
  xs.push_back(-1);
  UNQ(xs);

  BIT::n = xs.size();

  for (int i = 0; i < N; i++) {
    x1[i] = lower_bound(xs.begin(), xs.end(), x1[i]) - xs.begin();
    x2[i] = lower_bound(xs.begin(), xs.end(), x2[i]) - xs.begin();
  }

  set<int> S;
  S.insert(0);
  target[0] = 0;
  uf.push_back(0);

  vector<action> A;

  for (int i = 0; i < N; i++) {
    if (x1[i] == x2[i]) {
      A.push_back(action(y1[i], 0, x1[i], -1));
      A.push_back(action(y2[i], 2, x1[i], -1));
    } else {
      A.push_back(action(y1[i], 1, x1[i], x2[i]));
    }
  }

  sort(A.begin(), A.end());

  long long ret = 0;
  for (int i = 0; i < A.size(); i++) {
    action V = A[i];

    if (V.act == 0) {
      int lf = *--S.lower_bound(V.left);

      adjust(lf);
      adjust(V.left);
      // cout << V.left << " " << lf << endl;
      // cout << target[V.left] << " " << target[lf] << endl;
      // target[V.left] = target[lf];
      // cout << "Joint: " <<  target[V.left] << " " << target[lf] << endl;
      join(target[V.left], target[lf]);

      S.insert(V.left);
      BIT::Add(V.left, 1);
    } else if (V.act == 1) {
      int ll = *S.lower_bound(V.left);
      int rr = *--S.upper_bound(V.right);
      // cout << ll << " "<< rr << endl;
      if (rr <= ll)
        continue;
      ret += BIT::Sum(ll, rr) - 1;
      seg.set_renew(ll, rr);
      ST::Insert(ll, rr - 1);

    } else if (V.act == 2) {
      int lf = *--S.lower_bound(V.left);
      adjust(lf);
      adjust(V.left);

      int rett = ret;
      if (join(target[lf], target[V.left]))
        --ret;

      S.erase(V.left);
      BIT::Add(V.left, -1);
    }
    // cout << V.act << " " << ret << endl;
  }

  printf("%lld\n", ret);

  return 0;
}

Fixed submission:
#pragma comment(linker, "/STACK:36777216")
// #pragma GCC optimize ("O2")
#define LOCAL
// #include "testlib.h"

#include <algorithm>
#include <cstdio>
#include <iostream>
#include <set>
#include <vector>
// #include <tr1/unordered_set>
// #include <tr1/unordered_map>
// #include <array>

using namespace std;

#define REP(i, n) for (int i = 0; i < n; ++i)
#define FOR(i, a, b) for (int i = a; i < b; ++i)
#define DWN(i, b, a) for (int i = b - 1; i >= a; --i)
#define REP_1(i, n) for (int i = 1; i <= n; ++i)
#define FOR_1(i, a, b) for (int i = a; i <= b; ++i)
#define DWN_1(i, b, a) for (int i = b; i >= a; --i)
#define REP_C(i, n) for (int n____ = n, i = 0; i < n____; ++i)
#define FOR_C(i, a, b) for (int b____ = b, i = a; i < b____; ++i)
#define DWN_C(i, b, a) for (int a____ = a, i = b - 1; i >= a____; --i)
#define REP_N(i, n) for (i = 0; i < n; ++i)
#define FOR_N(i, a, b) for (i = a; i < b; ++i)
#define DWN_N(i, b, a) for (i = b - 1; i >= a; --i)
#define REP_1_C(i, n) for (int n____ = n, i = 1; i <= n____; ++i)
#define FOR_1_C(i, a, b) for (int b____ = b, i = a; i <= b____; ++i)
#define DWN_1_C(i, b, a) for (int a____ = a, i = b; i >= a____; --i)
#define REP_1_N(i, n) for (i = 1; i <= n; ++i)
#define FOR_1_N(i, a, b) for (i = a; i <= b; ++i)
#define DWN_1_N(i, b, a) for (i = b; i >= a; --i)
#define REP_C_N(i, n) for (int n____ = (i = 0, n); i < n____; ++i)
#define FOR_C_N(i, a, b) for (int b____ = (i = 0, b); i < b____; ++i)
#define DWN_C_N(i, b, a) for (int a____ = (i = b - 1, a); i >= a____; --i)
#define REP_1_C_N(i, n) for (int n____ = (i = 1, n); i <= n____; ++i)
#define FOR_1_C_N(i, a, b) for (int b____ = (i = a, b); i <= b____; ++i)
#define DWN_1_C_N(i, b, a) for (int a____ = (i = b, a); i >= a____; --i)

#define ECH(it, A)                                                             \
  for (__typeof((A).begin()) it = (A).begin(); it != (A).end(); ++it)
#define rECH(it, A)                                                            \
  for (__typeof((A).rbegin()) it = (A).rbegin(); it != (A).rend(); ++it)
#define REP_S(i, str) for (char *i = str; *i; ++i)
#define REP_L(i, hd, suc) for (int i = hd; i; i = suc[i])
#define REP_G(i, u) REP_L(i, hd[u], suc)
#define REP_SS(x, s) for (int x = s; x; x = (x - 1) & s)
#define DO(n) for (int ____n = n; ____n-- > 0;)
#define REP_2(i, j, n, m) REP(i, n) REP(j, m)
#define REP_2_1(i, j, n, m) REP_1(i, n) REP_1(j, m)
#define REP_3(i, j, k, n, m, l) REP(i, n) REP(j, m) REP(k, l)
#define REP_3_1(i, j, k, n, m, l) REP_1(i, n) REP_1(j, m) REP_1(k, l)
#define REP_4(i, j, k, ii, n, m, l, nn)                                        \
  REP(i, n) REP(j, m) REP(k, l) REP(ii, nn)
#define REP_4_1(i, j, k, ii, n, m, l, nn)                                      \
  REP_1(i, n) REP_1(j, m) REP_1(k, l) REP_1(ii, nn)

#define ALL(A) A.begin(), A.end()
#define LLA(A) A.rbegin(), A.rend()
#define CPY(A, B) memcpy(A, B, sizeof(A))
#define INS(A, P, B) A.insert(A.begin() + P, B)
#define ERS(A, P) A.erase(A.begin() + P)
#define LBD(A, x) (lower_bound(ALL(A), x) - A.begin())
#define UBD(A, x) (upper_bound(ALL(A), x) - A.begin())
#define CTN(T, x) (T.find(x) != T.end())
#define SZ(A) int((A).size())
#define PB push_back
#define MP(A, B) make_pair(A, B)
#define PTT pair<T, T>
#define Ts *this
#define rTs return Ts
#define fi first
#define se second
#define re real()
#define im imag()

#define Rush for (int ____T = RD(); ____T--;)
#define Display(A, n, m)                                                       \
  {                                                                            \
    REP(i, n) {                                                                \
      REP(j, m - 1) cout << A[i][j] << " ";                                    \
      cout << A[i][m - 1] << endl;                                             \
    }                                                                          \
  }
#define Display_1(A, n, m)                                                     \
  {                                                                            \
    REP_1(i, n) {                                                              \
      REP_1(j, m - 1) cout << A[i][j] << " ";                                  \
      cout << A[i][m] << endl;                                                 \
    }                                                                          \
  }

typedef long long LL;
// typedef long double DB;
typedef double DB;
typedef unsigned uint;
typedef unsigned long long uLL;

typedef vector<int> VI;

template <class T> inline T &RD(T &);
template <class T> inline void OT(const T &);
// inline int RD(){int x; return RD(x);}
inline LL RD() {
  LL x;
  return RD(x);
}
inline DB &RF(DB &);
inline DB RF() {
  DB x;
  return RF(x);
}
inline char *RS(char *s);
inline char &RC(char &c);
inline char RC();
inline char &RC(char &c) {
  scanf(" %c", &c);
  return c;
}
inline char RC() {
  char c;
  return RC(c);
}
// inline char& RC(char &c){c = getchar(); return c;}
// inline char RC(){return getchar();}

template <class T> inline T &RDD(T &);
inline LL RDD() {
  LL x;
  return RDD(x);
}

template <class T0, class T1> inline T0 &RD(T0 &x0, T1 &x1) {
  RD(x0), RD(x1);
  return x0;
}
template <class T0, class T1, class T2> inline T0 &RD(T0 &x0, T1 &x1, T2 &x2) {
  RD(x0), RD(x1), RD(x2);
  return x0;
}
template <class T0, class T1, class T2, class T3>
inline T0 &RD(T0 &x0, T1 &x1, T2 &x2, T3 &x3) {
  RD(x0), RD(x1), RD(x2), RD(x3);
  return x0;
}
template <class T0, class T1, class T2, class T3, class T4>
inline T0 &RD(T0 &x0, T1 &x1, T2 &x2, T3 &x3, T4 &x4) {
  RD(x0), RD(x1), RD(x2), RD(x3), RD(x4);
  return x0;
}
template <class T0, class T1, class T2, class T3, class T4, class T5>
inline T0 &RD(T0 &x0, T1 &x1, T2 &x2, T3 &x3, T4 &x4, T5 &x5) {
  RD(x0), RD(x1), RD(x2), RD(x3), RD(x4), RD(x5);
  return x0;
}
template <class T0, class T1, class T2, class T3, class T4, class T5, class T6>
inline T0 &RD(T0 &x0, T1 &x1, T2 &x2, T3 &x3, T4 &x4, T5 &x5, T6 &x6) {
  RD(x0), RD(x1), RD(x2), RD(x3), RD(x4), RD(x5), RD(x6);
  return x0;
}
template <class T0, class T1> inline void OT(const T0 &x0, const T1 &x1) {
  OT(x0), OT(x1);
}
template <class T0, class T1, class T2>
inline void OT(const T0 &x0, const T1 &x1, const T2 &x2) {
  OT(x0), OT(x1), OT(x2);
}
template <class T0, class T1, class T2, class T3>
inline void OT(const T0 &x0, const T1 &x1, const T2 &x2, const T3 &x3) {
  OT(x0), OT(x1), OT(x2), OT(x3);
}
template <class T0, class T1, class T2, class T3, class T4>
inline void OT(const T0 &x0, const T1 &x1, const T2 &x2, const T3 &x3,
               const T4 &x4) {
  OT(x0), OT(x1), OT(x2), OT(x3), OT(x4);
}
template <class T0, class T1, class T2, class T3, class T4, class T5>
inline void OT(const T0 &x0, const T1 &x1, const T2 &x2, const T3 &x3,
               const T4 &x4, const T5 &x5) {
  OT(x0), OT(x1), OT(x2), OT(x3), OT(x4), OT(x5);
}
template <class T0, class T1, class T2, class T3, class T4, class T5, class T6>
inline void OT(const T0 &x0, const T1 &x1, const T2 &x2, const T3 &x3,
               const T4 &x4, const T5 &x5, const T6 &x6) {
  OT(x0), OT(x1), OT(x2), OT(x3), OT(x4), OT(x5), OT(x6);
}
inline char &RC(char &a, char &b) {
  RC(a), RC(b);
  return a;
}
inline char &RC(char &a, char &b, char &c) {
  RC(a), RC(b), RC(c);
  return a;
}
inline char &RC(char &a, char &b, char &c, char &d) {
  RC(a), RC(b), RC(c), RC(d);
  return a;
}
inline char &RC(char &a, char &b, char &c, char &d, char &e) {
  RC(a), RC(b), RC(c), RC(d), RC(e);
  return a;
}
inline char &RC(char &a, char &b, char &c, char &d, char &e, char &f) {
  RC(a), RC(b), RC(c), RC(d), RC(e), RC(f);
  return a;
}
inline char &RC(char &a, char &b, char &c, char &d, char &e, char &f, char &g) {
  RC(a), RC(b), RC(c), RC(d), RC(e), RC(f), RC(g);
  return a;
}
inline DB &RF(DB &a, DB &b) {
  RF(a), RF(b);
  return a;
}
inline DB &RF(DB &a, DB &b, DB &c) {
  RF(a), RF(b), RF(c);
  return a;
}
inline DB &RF(DB &a, DB &b, DB &c, DB &d) {
  RF(a), RF(b), RF(c), RF(d);
  return a;
}
inline DB &RF(DB &a, DB &b, DB &c, DB &d, DB &e) {
  RF(a), RF(b), RF(c), RF(d), RF(e);
  return a;
}
inline DB &RF(DB &a, DB &b, DB &c, DB &d, DB &e, DB &f) {
  RF(a), RF(b), RF(c), RF(d), RF(e), RF(f);
  return a;
}
inline DB &RF(DB &a, DB &b, DB &c, DB &d, DB &e, DB &f, DB &g) {
  RF(a), RF(b), RF(c), RF(d), RF(e), RF(f), RF(g);
  return a;
}
inline void RS(char *s1, char *s2) { RS(s1), RS(s2); }
inline void RS(char *s1, char *s2, char *s3) { RS(s1), RS(s2), RS(s3); }
template <class T0, class T1> inline T0 &RDD(T0 &a, T1 &b) {
  RDD(a), RDD(b);
  return a;
}
template <class T0, class T1, class T2> inline T1 &RDD(T0 &a, T1 &b, T2 &c) {
  RDD(a), RDD(b), RDD(c);
  return a;
}

template <class T> inline void RST(T &A) { memset(A, 0, sizeof(A)); }
template <class T> inline void FLC(T &A, int x) { memset(A, x, sizeof(A)); }
template <class T> inline void CLR(T &A) { A.clear(); }

template <class T0, class T1> inline void RST(T0 &A0, T1 &A1) {
  RST(A0), RST(A1);
}
template <class T0, class T1, class T2>
inline void RST(T0 &A0, T1 &A1, T2 &A2) {
  RST(A0), RST(A1), RST(A2);
}
template <class T0, class T1, class T2, class T3>
inline void RST(T0 &A0, T1 &A1, T2 &A2, T3 &A3) {
  RST(A0), RST(A1), RST(A2), RST(A3);
}
template <class T0, class T1, class T2, class T3, class T4>
inline void RST(T0 &A0, T1 &A1, T2 &A2, T3 &A3, T4 &A4) {
  RST(A0), RST(A1), RST(A2), RST(A3), RST(A4);
}
template <class T0, class T1, class T2, class T3, class T4, class T5>
inline void RST(T0 &A0, T1 &A1, T2 &A2, T3 &A3, T4 &A4, T5 &A5) {
  RST(A0), RST(A1), RST(A2), RST(A3), RST(A4), RST(A5);
}
template <class T0, class T1, class T2, class T3, class T4, class T5, class T6>
inline void RST(T0 &A0, T1 &A1, T2 &A2, T3 &A3, T4 &A4, T5 &A5, T6 &A6) {
  RST(A0), RST(A1), RST(A2), RST(A3), RST(A4), RST(A5), RST(A6);
}
template <class T0, class T1> inline void FLC(T0 &A0, T1 &A1, int x) {
  FLC(A0, x), FLC(A1, x);
}
template <class T0, class T1, class T2>
inline void FLC(T0 &A0, T1 &A1, T2 &A2, int x) {
  FLC(A0, x), FLC(A1, x), FLC(A2, x);
}
template <class T0, class T1, class T2, class T3>
inline void FLC(T0 &A0, T1 &A1, T2 &A2, T3 &A3, int x) {
  FLC(A0, x), FLC(A1, x), FLC(A2, x), FLC(A3, x);
}
template <class T0, class T1, class T2, class T3, class T4>
inline void FLC(T0 &A0, T1 &A1, T2 &A2, T3 &A3, T4 &A4, int x) {
  FLC(A0, x), FLC(A1, x), FLC(A2, x), FLC(A3, x), FLC(A4, x);
}
template <class T0, class T1, class T2, class T3, class T4, class T5>
inline void FLC(T0 &A0, T1 &A1, T2 &A2, T3 &A3, T4 &A4, T5 &A5, int x) {
  FLC(A0, x), FLC(A1, x), FLC(A2, x), FLC(A3, x), FLC(A4, x), FLC(A5, x);
}
template <class T0, class T1, class T2, class T3, class T4, class T5, class T6>
inline void FLC(T0 &A0, T1 &A1, T2 &A2, T3 &A3, T4 &A4, T5 &A5, T6 &A6, int x) {
  FLC(A0, x), FLC(A1, x), FLC(A2, x), FLC(A3, x), FLC(A4, x), FLC(A5, x),
      FLC(A6, x);
}

template <class T0, class T1> inline void CLR(T0 &A0, T1 &A1) {
  CLR(A0), CLR(A1);
}
template <class T0, class T1, class T2>
inline void CLR(T0 &A0, T1 &A1, T2 &A2) {
  CLR(A0), CLR(A1), CLR(A2);
}
template <class T0, class T1, class T2, class T3>
inline void CLR(T0 &A0, T1 &A1, T2 &A2, T3 &A3) {
  CLR(A0), CLR(A1), CLR(A2), CLR(A3);
}
template <class T0, class T1, class T2, class T3, class T4>
inline void CLR(T0 &A0, T1 &A1, T2 &A2, T3 &A3, T4 &A4) {
  CLR(A0), CLR(A1), CLR(A2), CLR(A3), CLR(A4);
}
template <class T0, class T1, class T2, class T3, class T4, class T5>
inline void CLR(T0 &A0, T1 &A1, T2 &A2, T3 &A3, T4 &A4, T5 &A5) {
  CLR(A0), CLR(A1), CLR(A2), CLR(A3), CLR(A4), CLR(A5);
}
template <class T0, class T1, class T2, class T3, class T4, class T5, class T6>
inline void CLR(T0 &A0, T1 &A1, T2 &A2, T3 &A3, T4 &A4, T5 &A5, T6 &A6) {
  CLR(A0), CLR(A1), CLR(A2), CLR(A3), CLR(A4), CLR(A5), CLR(A6);
}
template <class T> inline void CLR(T &A, int n) { REP(i, n) CLR(A[i]); }

template <class T> inline bool EPT(T &a) { return a.empty(); }
template <class T> inline T &SRT(T &A) {
  sort(ALL(A));
  return A;
}
template <class T, class C> inline T &SRT(T &A, C B) {
  sort(ALL(A), B);
  return A;
}
template <class T> inline T &RVS(T &A) {
  reverse(ALL(A));
  return A;
}
template <class T> inline T &UNQQ(T &A) {
  A.resize(unique(ALL(A)) - A.begin());
  return A;
}
template <class T> inline T &UNQ(T &A) {
  SRT(A);
  return UNQQ(A);
}

//}

/** Constant List .. **/ //{

const int MOD = int(1e9) + 7;
const int INF = 0x3f3f3f3f;
const LL INFF = 0x3f3f3f3f3f3f3f3fLL;
const DB EPS = 1e-9;
const DB OO = 1e20;

const int dx[] = {-1, 0, 1, 0};
const int dy[] = {0, 1, 0, -1};

//}

/** Add On .. **/ //{
// <<= '0. Nichi Joo ., //{

template <class T> inline bool checkMin(T &a, const T b) {
  return b < a ? a = b, 1 : 0;
}
template <class T> inline bool checkMax(T &a, const T b) {
  return a < b ? a = b, 1 : 0;
}
template <class T, class C> inline bool checkUpd(T &a, const T b, C c) {
  return c(b, a) ? a = b, 1 : 0;
}
template <class T> inline T min(T a, T b, T c) { return min(min(a, b), c); }
template <class T> inline T max(T a, T b, T c) { return max(max(a, b), c); }
template <class T> inline T min(T a, T b, T c, T d) {
  return min(min(a, b), min(c, d));
}
template <class T> inline T max(T a, T b, T c, T d) {
  return max(max(a, b), max(c, d));
}
template <class T> inline T min(T a, T b, T c, T d, T e) {
  return min(min(min(a, b), min(c, d)), e);
}
template <class T> inline T max(T a, T b, T c, T d, T e) {
  return max(max(max(a, b), max(c, d)), e);
}
template <class T> inline T sqr(T a) { return a * a; }
template <class T> inline T cub(T a) { return a * a * a; }
template <class T> inline T ceil(T x, T y) { return (x - 1) / y + 1; }
template <class T> T abs(T x) { return x > 0 ? x : -x; }
inline int sgn(DB x) { return x < -EPS ? -1 : x > EPS; }
inline int sgn(DB x, DB y) { return sgn(x - y); }
//}

//}

/** I/O Accelerator Interface .. **/ //{
#define g (c = getchar())
#define d isdigit(g)
#define p x = x * 10 + c - '0'
#define n x = x * 10 + '0' - c
#define pp l /= 10, p
#define nn l /= 10, n
template <class T> inline T &RD(T &x) {
  char c;
  while (!d)
    ;
  x = c - '0';
  while (d)
    p;
  return x;
}
template <class T> inline T &RDD(T &x) {
  char c;
  while (g, c != '-' && !isdigit(c))
    ;
  if (c == '-') {
    x = '0' - g;
    while (d)
      n;
  } else {
    x = c - '0';
    while (d)
      p;
  }
  return x;
}
inline DB &RF(DB &x) {
  // scanf("%lf", &x);
  char c;
  while (g, c != '-' && c != '.' && !isdigit(c))
    ;
  if (c == '-')
    if (g == '.') {
      x = 0;
      DB l = 1;
      while (d)
        nn;
      x *= l;
    } else {
      x = '0' - c;
      while (d)
        n;
      if (c == '.') {
        DB l = 1;
        while (d)
          nn;
        x *= l;
      }
    }
  else if (c == '.') {
    x = 0;
    DB l = 1;
    while (d)
      pp;
    x *= l;
  } else {
    x = c - '0';
    while (d)
      p;
    if (c == '.') {
      DB l = 1;
      while (d)
        pp;
      x *= l;
    }
  }
  return x;
}
#undef nn
#undef pp
#undef n
#undef p
#undef d
#undef g
inline char *RS(char *s) {
  // gets(s);
  scanf("%s", s);
  return s;
}

LL last_ans;
int Case;
template <class T> inline void OT(const T &x) {
  // printf("Case #%d: ", ++Case);
  // printf("%lld\n", x);
  // printf("%.9f\n", x);
  // printf("%d\n", x);
  cout << x << endl;
  // last_ans = x;
}

//}/*
//..................................................................................................................................
//*/

const int MAXN = 100020;

vector<int> xs;

namespace ST {
const int NN = 8 * MAXN;
bool T[NN];
int a, b;

#define lx (x << 1)
#define rx (lx | 1)
#define ml (l + r >> 1)
#define mr (ml + 1)
#define lc lx, l, ml
#define rc rx, mr, r
#define rt 1, 0, xs.size()

void cov(int x) { T[x] = 1; }

void rls(int x) {
  if (T[x]) {
    T[lx] = T[rx] = 1;
    T[x] = 0;
  }
}

bool Query(int x, int l, int r, int p) {
  if (l == r) {
    bool z = T[x];
    T[x] = 0;
    return z;
  } else {
    rls(x);
    return p < mr ? Query(lc, p) : Query(rc, p);
  }
}
bool Query(int p) { return Query(rt, p); }

void Insert(int x, int l, int r) {
  if (b < l || r < a)
    return;
  if (a <= l && r <= b) {
    cov(x);
  } else {
    rls(x);
    Insert(lc);
    Insert(rc);
  }
}
void Insert(int _a, int _b) {
  a = _a, b = _b;
  // cout << " Insert: " << a << " " << b << " " << xs.size() << endl;
  Insert(rt);
}

void Clear(int x, int l, int r, int p) {
  if (l == r) {
    T[x] = 0;
  } else {
    rls(x);
    if (p < mr)
      Clear(lc, p);
    else
      Clear(rc, p);
  }
}
} // namespace ST

struct seg_tree {
  static const int DEPTH = 19;
  static const int SIZE = 1 << DEPTH;

  int bit[1 << (DEPTH + 1)];
  int renew[1 << (DEPTH + 1)];

  seg_tree() {}

  void init() {
    for (int i = 0; i < 2 * SIZE; i++)
      bit[i] = renew[i] = 0;
  }

  void add(int p, int v) {
    p += SIZE;
    while (p) {
      bit[p] += v;
      p >>= 1;
    }
  }

  int query(int l, int r) {
    l += SIZE;
    r += SIZE;
    int ret = 0;

    while (l < r) {
      if (l & 1)
        ret += bit[l++];
      if (r & 1)
        ret += bit[--r];
      l >>= 1;
      r >>= 1;
    }

    return ret;
  }

  void set_renew(int l, int r) {
    l += SIZE;
    r += SIZE;

    while (l < r) {
      if (l & 1)
        renew[l++] = 1;
      if (r & 1)
        renew[--r] = 1;
      l >>= 1;
      r >>= 1;
    }
  }

  bool is_renew(int p) {
    p += SIZE;
    while (p) {
      if (renew[p])
        return true;
      p >>= 1;
    }
    return false;
  }

  void unset_renew(int p) {
    p += SIZE;
    for (int i = DEPTH - 1; i > 0; i--) {
      if (renew[p >> i]) {
        renew[p >> i] = 0;
        renew[(p >> i) * 2] = renew[(p >> i) * 2 + 1] = 1;
      }
    }

    renew[p] = 0;
  }
};

struct action {
  int pos, act, left, right;

  action(int p, int a, int l, int r) {
    pos = p;
    act = a;
    left = l;
    right = r;
  }
};

inline bool operator<(const action &a, const action &b) {
  return a.pos < b.pos || (a.pos == b.pos && a.act < b.act);
}

int W, H, N;
int x1[MAXN], y1[MAXN], x2[MAXN], y2[MAXN];
vector<int> uf;
seg_tree seg;
int target[MAXN * 2];

int root(int p) {
  // cout << p << " " << uf[p] << endl;
  return (uf[p] == p) ? p : (uf[p] = root(uf[p]));
}

bool join(int p, int q) {
  p = root(p);
  q = root(q);
  if (p == q)
    return false;
  uf[q] = p;
  return true;
}

void adjust(int p) {
  // if(seg.is_renew(p)){

  // cout << " " << p << endl;

  // cout << "   " << p << " "<< ST::Query(p) << " " << seg.is_renew(p) << endl;

  // if (ST::Query(p)){
  if (seg.is_renew(p)) {

    // cout << "?!" << endl;

    int t = uf.size();
    uf.push_back(t);
    seg.unset_renew(p);
    target[p] = t;
  }
}

template <class T> inline T low_bit(T x) { return x & -x; }

namespace BIT {
const int N = MAXN * 2;
int C[N], n;
void Add(int x, int d) {
  for (; x <= n; x += low_bit(x))
    C[x] += d;
}
int Sum(int x) {
  int res = 0;
  for (; x; x ^= low_bit(x))
    res += C[x];
  return res;
}
int Sum(int l, int r) { return Sum(r) - Sum(l - 1); }

} // namespace BIT

int main() {

#ifndef ONLINE_JUDGE
  // freopen("in.txt", "r", stdin);
  // freopen("out.txt", "w", stdout);
#endif

  scanf("%d%d%d", &W, &H, &N);
  // N = 0;
  for (int i = 0; i < N; i++) {
    scanf("%d%d%d%d", x1 + i, y1 + i, x2 + i, y2 + i);
    if (x1[i] > x2[i])
      swap(x1[i], x2[i]);
    if (y1[i] > y2[i])
      swap(y1[i], y2[i]);
  }
  // FLC(for(int i=0;i<2*N;i++) target[i] = -1;

  x1[N] = 0;
  y1[N] = 0;
  x2[N] = W;
  y2[N] = 0;
  x1[N + 1] = 0;
  y1[N + 1] = 0;
  x2[N + 1] = 0;
  y2[N + 1] = H;
  x1[N + 2] = W;
  y1[N + 2] = 0;
  x2[N + 2] = W;
  y2[N + 2] = H;
  x1[N + 3] = 0;
  y1[N + 3] = H;
  x2[N + 3] = W;
  y2[N + 3] = H;
  N += 4;

  for (int i = 0; i < N; i++) {
    xs.push_back(x1[i]);
    xs.push_back(x2[i]);
  }
  xs.push_back(-1);
  UNQ(xs);

  BIT::n = xs.size();

  for (int i = 0; i < N; i++) {
    x1[i] = lower_bound(xs.begin(), xs.end(), x1[i]) - xs.begin();
    x2[i] = lower_bound(xs.begin(), xs.end(), x2[i]) - xs.begin();
  }

  set<int> S;
  S.insert(0);
  target[0] = 0;
  uf.push_back(0);

  vector<action> A;

  for (int i = 0; i < N; i++) {
    if (x1[i] == x2[i]) {
      A.push_back(action(y1[i], 0, x1[i], -1));
      A.push_back(action(y2[i], 2, x1[i], -1));
    } else {
      A.push_back(action(y1[i], 1, x1[i], x2[i]));
    }
  }

  sort(A.begin(), A.end());

  long long ret = 0;
  for (int i = 0; i < A.size(); i++) {
    action V = A[i];

    if (V.act == 0) {
      int lf = *--S.lower_bound(V.left);

      adjust(lf);
      adjust(V.left);
      // cout << V.left << " " << lf << endl;
      // cout << target[V.left] << " " << target[lf] << endl;
      // target[V.left] = target[lf];
      // cout << "Joint: " <<  target[V.left] << " " << target[lf] << endl;
      join(target[V.left], target[lf]);

      S.insert(V.left);
      BIT::Add(V.left, 1);
    } else if (V.act == 1) {
      int ll = *S.lower_bound(V.left);
      int rr = *--S.upper_bound(V.right);
      // cout << ll << " "<< rr << endl;
      if (rr <= ll)
        continue;
      ret += BIT::Sum(ll, rr) - 1;
      seg.set_renew(ll, rr);
      ST::Insert(ll, rr - 1);

    } else if (V.act == 2) {
      int lf = *--S.lower_bound(V.left);
      adjust(lf);
      adjust(V.left);

      int rett = ret;
      if (join(target[lf], target[V.left]))
        --ret;

      S.erase(V.left);
      BIT::Add(V.left, -1);
    }
    // cout << V.act << " " << ret << endl;
  }

  printf("%lld\n", ret);

  return 0;
}