Problem p03409 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;

int main() {
  int n;
  cin >> n;
  int a[n], b[n], c[n], d[n];
  int ans = 0, used_a[n], used_c[n];
  memset(used_a, 0, n * sizeof(used_a));
  memset(used_c, 0, n * sizeof(used_c));
  for (int i = 0; i < n; i++)
    cin >> a[i] >> b[i];
  for (int i = 0; i < n; i++)
    cin >> c[i] >> d[i];
  int tmp1 = 500, tmp2 = -1, cntx, cnty;
  // データをソート
  for (int i = 0; i < n; i++) {
    tmp1 = b[i];
    tmp2 = c[i];
    cntx = i;
    cnty = i;
    for (int j = i; j < n; j++) {
      // 赤のうちyが最大のもの
      if (tmp1 < b[j]) {
        tmp1 = b[j];
        cnty = j;
      }
      // 青のうちxが最小のもの
      if (tmp2 > c[j]) {
        tmp2 = c[j];
        cntx = j;
      }
      // cout<<"cntx:"<<cntx<<"cnty:"<<cnty<<endl;
    }
    b[cnty] = b[i];
    b[i] = tmp1;
    tmp1 = a[cnty];
    a[cnty] = a[i];
    a[i] = tmp1;
    c[cntx] = c[i];
    c[i] = tmp2;
    tmp2 = d[cntx];
    d[cntx] = d[i];
    d[i] = tmp2;
  }

  /*for(int i=0;i<n;i++){
          cout<<"a,b:"<<a[i]<<" "<<b[i]<<endl;
  }
  for(int i=0;i<n;i++){
          cout<<"c,d:"<<c[i]<<" "<<d[i]<<endl;
  }*/
  //	cout<<endl;

  for (int i = 0; i < n; i++) {
    for (int j = 0; j < n; j++) {
      if (used_a[j] == -1 || used_c[i] == -1) {
        // cout<<a[j]<<"or"<<c[i]<<"is used"<<endl;
        // cout<<a[j]<<"is"<<used_a[j]<<endl;;
        continue;
      }
      if (a[j] < c[i] && b[j] < d[i]) {
        // cout<<"a,b:"<<a[j]<<" "<<b[j]<<endl;
        // cout<<"c,d:"<<c[i]<<" "<<d[i]<<endl;
        ans++;
        used_a[j] = -1;
        used_c[i] = -1;
      }
    }
  }
  cout << ans << endl;
}


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

int main() {
  int n;
  cin >> n;
  int a[n], b[n], c[n], d[n];
  int ans = 0, used_a[n], used_c[n];
  // memset(used_a,0,n * sizeof(used_a));
  // memset(used_c,0,n * sizeof(used_c));

  for (int i = 0; i < n; i++)
    used_a[i] = 0;
  for (int j = 0; j < n; j++)
    used_c[j] = 0;

  for (int i = 0; i < n; i++)
    cin >> a[i] >> b[i];
  for (int i = 0; i < n; i++)
    cin >> c[i] >> d[i];
  int tmp1 = 500, tmp2 = -1, cntx, cnty;
  // データをソート
  for (int i = 0; i < n; i++) {
    tmp1 = b[i];
    tmp2 = c[i];
    cntx = i;
    cnty = i;
    for (int j = i; j < n; j++) {
      // 赤のうちyが最大のもの
      if (tmp1 < b[j]) {
        tmp1 = b[j];
        cnty = j;
      }
      // 青のうちxが最小のもの
      if (tmp2 > c[j]) {
        tmp2 = c[j];
        cntx = j;
      }
      // cout<<"cntx:"<<cntx<<"cnty:"<<cnty<<endl;
    }
    b[cnty] = b[i];
    b[i] = tmp1;
    tmp1 = a[cnty];
    a[cnty] = a[i];
    a[i] = tmp1;
    c[cntx] = c[i];
    c[i] = tmp2;
    tmp2 = d[cntx];
    d[cntx] = d[i];
    d[i] = tmp2;
  }

  /*for(int i=0;i<n;i++){
          cout<<"a,b:"<<a[i]<<" "<<b[i]<<endl;
  }
  for(int i=0;i<n;i++){
          cout<<"c,d:"<<c[i]<<" "<<d[i]<<endl;
  }*/
  //	cout<<endl;

  for (int i = 0; i < n; i++) {
    for (int j = 0; j < n; j++) {
      if (used_a[j] == -1 || used_c[i] == -1) {
        // cout<<a[j]<<"or"<<c[i]<<"is used"<<endl;
        // cout<<a[j]<<"is"<<used_a[j]<<endl;;
        continue;
      }
      if (a[j] < c[i] && b[j] < d[i]) {
        // cout<<"a,b:"<<a[j]<<" "<<b[j]<<endl;
        // cout<<"c,d:"<<c[i]<<" "<<d[i]<<endl;
        ans++;
        used_a[j] = -1;
        used_c[i] = -1;
      }
    }
  }
  cout << ans << endl;
}