prithivMLmods/Coder-Stat huggingface dataset:
Problem p03682 (C++)

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

typedef pair<int, int> pint;

// Union Find
int par[100010];

void init(int n) {
  for (int i = 0; i < n; i++) {
    par[i] = i;
  }
}

int find(int x) {
  if (x == par[x])
    return x;
  return find(par[x]);
}

void unite(int x, int y) {
  int parx = find(x);
  int pary = find(y);
  if (parx == pary)
    return;
  par[parx] = pary;
}

bool same(int x, int y) {
  int parx = find(x);
  int pary = find(y);
  return parx == pary;
}

// Edge
vector<pair<int, pint>> E;

int main() {
  int N;
  cin >> N;
  vector<pair<int, int>> X;
  vector<pair<int, int>> Y;
  for (int i = 0; i < N; i++) {
    int x, y;
    cin >> x >> y;
    X.push_back(make_pair(x, i));
    Y.push_back(make_pair(y, i));
  }
  sort(X.begin(), X.end());
  sort(Y.begin(), Y.end());
  for (int i = 1; i < X.size(); i++) {
    int d = X[i].first - X[i - 1].first;
    pint p(X[i].second, X[i - 1].second);
    E.push_back(make_pair(d, p));
  }
  for (int i = 1; i < Y.size(); i++) {
    int d = Y[i].first - Y[i - 1].first;
    pint p(Y[i].second, Y[i - 1].second);
    E.push_back(make_pair(d, p));
  }
  sort(E.begin(), E.end());
  int ans = 0;
  init(N);
  for (int i = 0; i < E.size(); i++) {
    if (!same(E[i].second.first, E[i].second.second)) {
      unite(E[i].second.first, E[i].second.second);
      ans += E[i].first;
    }
  }
  cout << ans << endl;
  return 0;
}

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

typedef pair<int, int> pint;

// Union Find
int par[100010];

void init(int n) {
  for (int i = 0; i < n; i++) {
    par[i] = i;
  }
}

int find(int x) {
  if (x == par[x])
    return x;
  return par[x] = find(par[x]);
}

void unite(int x, int y) {
  int parx = find(x);
  int pary = find(y);
  if (parx == pary)
    return;
  par[parx] = pary;
}

bool same(int x, int y) {
  int parx = find(x);
  int pary = find(y);
  return parx == pary;
}

// Edge
vector<pair<int, pint>> E;

int main() {
  int N;
  cin >> N;
  vector<pair<int, int>> X;
  vector<pair<int, int>> Y;
  for (int i = 0; i < N; i++) {
    int x, y;
    cin >> x >> y;
    X.push_back(make_pair(x, i));
    Y.push_back(make_pair(y, i));
  }
  sort(X.begin(), X.end());
  sort(Y.begin(), Y.end());
  for (int i = 1; i < X.size(); i++) {
    int d = X[i].first - X[i - 1].first;
    pint p(X[i].second, X[i - 1].second);
    E.push_back(make_pair(d, p));
  }
  for (int i = 1; i < Y.size(); i++) {
    int d = Y[i].first - Y[i - 1].first;
    pint p(Y[i].second, Y[i - 1].second);
    E.push_back(make_pair(d, p));
  }
  sort(E.begin(), E.end());
  int ans = 0;
  init(N);
  for (int i = 0; i < E.size(); i++) {
    if (!same(E[i].second.first, E[i].second.second)) {
      unite(E[i].second.first, E[i].second.second);
      ans += E[i].first;
    }
  }
  cout << ans << endl;
  return 0;
}
Problem p03682 (C++)

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

struct UnionFind {
  vector<int> par;
  UnionFind(int N) : par(N) {
    for (int i = 0; i < N; i++)
      par[i] = i;
  }
  int root(int x) {
    if (par[x] == x)
      return x;
    return par[x] = root(par[x]);
  }
  void unite(int x, int y) {
    int rx = root(x);
    int ry = root(y);
    if (rx == ry)
      return;
    par[rx] = ry;
  }
  bool same(int x, int y) {
    int rx = root(x);
    int ry = root(y);
    return rx == ry;
  }
};

int main() {
  int N;
  scanf("%d", &N);
  vector<pair<int, int>> x, y;
  for (int X, Y, i = 0; i < N; i++) {
    scanf("%d %d", &X, &Y);
    x.push_back(make_pair(X, i));
    y.push_back(make_pair(Y, i));
  }
  sort(x.begin(), x.end());
  sort(y.begin(), y.end());
  vector<tuple<int, int, int>> G;
  for (int i = 1; i < N; i++) {
    G.push_back(
        make_tuple(x[i].first - x[i - 1].first, x[i].second, x[i - 1].second));
    G.push_back(
        make_tuple(y[i].first - y[i - 1].first, y[i].second, y[i - 1].second));
  }
  sort(G.begin(), G.end());
  long long ans = 0;
  UnionFind tree(N);
  for (int dist, a, b, i = 0; i < 2 * N - 1; i++) {
    dist = get<0>(G[i]);
    a = get<1>(G[i]);
    b = get<2>(G[i]);
    if (tree.same(a, b))
      continue;
    ans += dist;
    tree.unite(a, b);
  }
  printf("%lld", ans);
}

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

struct UnionFind {
  vector<int> par;
  UnionFind(int N) : par(N) {
    for (int i = 0; i < N; i++)
      par[i] = i;
  }
  int root(int x) {
    if (par[x] == x)
      return x;
    return par[x] = root(par[x]);
  }
  void unite(int x, int y) {
    int rx = root(x);
    int ry = root(y);
    if (rx == ry)
      return;
    par[rx] = ry;
  }
  bool same(int x, int y) {
    int rx = root(x);
    int ry = root(y);
    return rx == ry;
  }
};

int main() {
  int N;
  scanf("%d", &N);
  vector<pair<int, int>> x, y;
  for (int X, Y, i = 0; i < N; i++) {
    scanf("%d %d", &X, &Y);
    x.push_back(make_pair(X, i));
    y.push_back(make_pair(Y, i));
  }
  sort(x.begin(), x.end());
  sort(y.begin(), y.end());
  vector<tuple<int, int, int>> G;
  for (int i = 1; i < N; i++) {
    G.push_back(
        make_tuple(x[i].first - x[i - 1].first, x[i].second, x[i - 1].second));
    G.push_back(
        make_tuple(y[i].first - y[i - 1].first, y[i].second, y[i - 1].second));
  }
  sort(G.begin(), G.end());
  long long ans = 0;
  UnionFind tree(N);
  for (int dist, a, b, i = 0; i < 2 * N - 2; i++) {
    dist = get<0>(G[i]);
    a = get<1>(G[i]);
    b = get<2>(G[i]);
    if (tree.same(a, b))
      continue;
    ans += dist;
    tree.unite(a, b);
  }
  printf("%lld", ans);
}
Problem p03682 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#include <utility>
using namespace std;
vector<int> par;
int root(int v) {
  if (v == par[v])
    return v;
  return par[v] = root(par[v]);
}
bool unit(int a, int b) {
  a = root(a);
  b = root(b);
  if (a == b)
    return false;
  par[b] = a;
  return true;
}
int main() {
  int N;
  cin >> N;
  vector<tuple<int, int, int>> X(N);
  vector<tuple<int, int, int>> Y(N);
  for (int i = 0; i < N; i++) {
    int x, y;
    cin >> x >> y;
    X[i] = make_tuple(x, y, i);
    Y[i] = make_tuple(y, x, i);
  }
  sort(X.begin(), X.end());
  sort(Y.begin(), Y.end());
  vector<tuple<int, int, int>> roa(2 * N - 2);
  for (int i = 0; i < N - 1; i++) {
    roa[i] = make_tuple(get<0>(X[i + 1]) - get<0>(X[i]), get<2>(X[i]),
                        get<2>(X[i + 1]));
  }
  for (int i = 0; i < N - 1; i++) {
    int j = i + N - 1;
    roa[j] = make_tuple(get<0>(Y[i + 1]) - get<0>(Y[i]), get<2>(Y[i]),
                        get<2>(Y[i + 1]));
  }
  long long ans = 0;
  sort(roa.begin(), roa.end());
  for (int i = 0; i < N * 2 - 2; i++) {
    bool kj = unit(get<1>(roa[i]), get<2>(roa[i]));
    if (kj)
      ans += (long long)get<0>(roa[i]);
  }
  cout << ans << endl;
}

Fixed submission:
#include <bits/stdc++.h>
#include <utility>
using namespace std;
vector<int> par;
int root(int v) {
  if (v == par[v])
    return v;
  return par[v] = root(par[v]);
}
bool unit(int a, int b) {
  a = root(a);
  b = root(b);
  if (a == b)
    return false;
  par[b] = a;
  return true;
}
int main() {
  int N;
  cin >> N;
  par.resize(N);
  for (int i = 0; i < N; i++)
    par[i] = i;
  vector<tuple<int, int, int>> X(N);
  vector<tuple<int, int, int>> Y(N);
  for (int i = 0; i < N; i++) {
    int x, y;
    cin >> x >> y;
    X[i] = make_tuple(x, y, i);
    Y[i] = make_tuple(y, x, i);
  }
  sort(X.begin(), X.end());
  sort(Y.begin(), Y.end());
  vector<tuple<int, int, int>> roa(2 * N - 2);
  for (int i = 0; i < N - 1; i++) {
    roa[i] = make_tuple(get<0>(X[i + 1]) - get<0>(X[i]), get<2>(X[i]),
                        get<2>(X[i + 1]));
  }
  for (int i = 0; i < N - 1; i++) {
    int j = i + N - 1;
    roa[j] = make_tuple(get<0>(Y[i + 1]) - get<0>(Y[i]), get<2>(Y[i]),
                        get<2>(Y[i + 1]));
  }
  long long ans = 0;
  sort(roa.begin(), roa.end());
  for (int i = 0; i < N * 2 - 2; i++) {
    bool kj = unit(get<1>(roa[i]), get<2>(roa[i]));
    if (kj)
      ans += (long long)get<0>(roa[i]);
  }
  cout << ans << endl;
}
Problem p03682 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
#define ll long long
typedef struct edge {
  ll from, to;
  ll cost;
};
typedef struct points {
  ll x, y;
  ll id;
};
bool xcmp(points a, points b) { return a.x < b.x; }
bool ycmp(points a, points b) { return a.y < b.y; }
bool cmp(edge a, edge b) { return a.cost < b.cost; }
int find(int x, int par[]) {
  if (x == par[x]) {
    return x;
  } else
    return find(par[x], par);
}
int main() {
  int n;
  cin >> n;
  points v[n + 1];
  for (int i = 1; i <= n; i++) {
    cin >> v[i].x >> v[i].y;
    v[i].id = i;
  }

  vector<edge> e;
  sort(v + 1, v + n + 1, xcmp);
  for (int i = 1; i < n; i++) {
    edge xx;
    xx.from = v[i].id;
    xx.to = v[i + 1].id;
    xx.cost = v[i + 1].x - v[i].x;
    e.push_back(xx);
  }
  sort(v + 1, v + n + 1, ycmp);
  for (int i = 1; i < n; i++) {
    edge xx;
    xx.from = v[i].id;
    xx.to = v[i + 1].id;
    xx.cost = v[i + 1].y - v[i].y;
    e.push_back(xx);
  }
  sort(e.begin(), e.end(), cmp);
  int par[n + 1];
  for (int i = 1; i <= n; i++) {
    par[i] = i;
  }
  ll ans = 0;
  for (int i = 0; i < e.size(); i++) {
    int x = e[i].from;
    int y = e[i].to;
    int root1 = find(x, par);
    int root2 = find(y, par);
    if (root1 != root2) {
      ans += e[i].cost;
      par[root2] = root1;
    }
  }
  cout << ans << endl;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define ll long long
typedef struct edge {
  ll from, to;
  ll cost;
};
typedef struct points {
  ll x, y;
  ll id;
};
bool xcmp(points a, points b) { return a.x < b.x; }
bool ycmp(points a, points b) { return a.y < b.y; }
bool cmp(edge a, edge b) { return a.cost < b.cost; }
int find(int x, int par[]) {
  while (x != par[x]) {
    par[x] = par[par[x]];
    x = par[x];
  }
  return x;
}
int main() {
  int n;
  cin >> n;
  points v[n + 1];
  for (int i = 1; i <= n; i++) {
    cin >> v[i].x >> v[i].y;
    v[i].id = i;
  }

  vector<edge> e;
  sort(v + 1, v + n + 1, xcmp);
  for (int i = 1; i < n; i++) {
    edge xx;
    xx.from = v[i].id;
    xx.to = v[i + 1].id;
    xx.cost = v[i + 1].x - v[i].x;
    e.push_back(xx);
  }
  sort(v + 1, v + n + 1, ycmp);
  for (int i = 1; i < n; i++) {
    edge xx;
    xx.from = v[i].id;
    xx.to = v[i + 1].id;
    xx.cost = v[i + 1].y - v[i].y;
    e.push_back(xx);
  }
  sort(e.begin(), e.end(), cmp);
  int par[n + 1];
  for (int i = 1; i <= n; i++) {
    par[i] = i;
  }
  ll ans = 0;
  for (int i = 0; i < e.size(); i++) {
    int x = e[i].from;
    int y = e[i].to;
    int root1 = find(x, par);
    int root2 = find(y, par);
    if (root1 != root2) {
      ans += e[i].cost;
      par[root2] = root1;
    }
  }
  cout << ans << endl;
}
Problem p03682 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cassert>
#include <chrono>
#include <cmath>
#include <deque>
#include <iomanip>
#include <iostream>
#include <limits>
#include <map>
#include <math.h>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string>
#include <thread>
#include <utility>
#include <vector>
using namespace std;
typedef unsigned long long int ull;
typedef long long int ll;
typedef pair<ll, ll> pll;
typedef pair<int, int> pi;
const ll E = 1e18 + 7;
#define F first
#define S second
#define MK make_pair

vector<ll> uft;

ll search(ll w) {
  if (uft[w] == w) {
    return w;
  }
  return search(uft[w]);
}

int main() {
  ll n;
  cin >> n;
  uft.resize(n);
  for (int i = 0; i < n; i++) {
    uft[i] = i;
  }
  vector<pll> x, y;
  for (int i = 0; i < n; i++) {
    ll k, l;
    cin >> k >> l;
    x.push_back({k, i});
    y.push_back({l, i});
  }
  sort(x.begin(), x.end());
  sort(y.begin(), y.end());
  vector<vector<ll>> road;
  for (int i = 1; i < n; i++) {
    road.push_back({x[i].F - x[i - 1].F, x[i].S, x[i - 1].S});
    road.push_back({y[i].F - y[i - 1].F, y[i].S, y[i - 1].S});
  }
  sort(road.begin(), road.end());
  ll sum = 0;
  ll count = 1;
  for (int i = 0; i < road.size() && count < n; i++) {
    if (search(road[i][1]) == search(road[i][2])) {
      continue;
    }
    sum += road[i][0];
    uft[search(road[i][2])] = search(road[i][1]);
    count++;
  }
  cout << sum << endl;

  return 0;
}

Fixed submission:
#include <algorithm>
#include <cassert>
#include <chrono>
#include <cmath>
#include <deque>
#include <iomanip>
#include <iostream>
#include <limits>
#include <map>
#include <math.h>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string>
#include <thread>
#include <utility>
#include <vector>
using namespace std;
typedef unsigned long long int ull;
typedef long long int ll;
typedef pair<ll, ll> pll;
typedef pair<int, int> pi;
const ll E = 1e18 + 7;
#define F first
#define S second
#define MK make_pair

vector<ll> uft;

ll search(ll w) {
  if (uft[w] == w) {
    return w;
  }
  ll a = search(uft[w]);
  uft[w] = a;
  return a;
}

int main() {
  ll n;
  cin >> n;
  uft.resize(n);
  for (int i = 0; i < n; i++) {
    uft[i] = i;
  }
  vector<pll> x, y;
  for (int i = 0; i < n; i++) {
    ll k, l;
    cin >> k >> l;
    x.push_back({k, i});
    y.push_back({l, i});
  }
  sort(x.begin(), x.end());
  sort(y.begin(), y.end());
  vector<vector<ll>> road;
  for (int i = 1; i < n; i++) {
    road.push_back({x[i].F - x[i - 1].F, x[i].S, x[i - 1].S});
    road.push_back({y[i].F - y[i - 1].F, y[i].S, y[i - 1].S});
  }
  sort(road.begin(), road.end());
  ll sum = 0;
  ll count = 1;
  for (int i = 0; i < road.size() && count < n; i++) {
    if (search(road[i][1]) == search(road[i][2])) {
      continue;
    }
    sum += road[i][0];
    uft[search(road[i][2])] = search(road[i][1]);
    count++;
  }
  cout << sum << endl;

  return 0;
}
Problem p03682 (C++)

Buggy submission (status: Time Limit Exceeded):
// He always smiles :)
#include <bits/stdc++.h>
#define maxn 200005
#define mod 1000000007
#define pii pair<int, int>
#define fi first
#define se second
#define pb push_back
#define task "main"
#define ll long long
#define reset(x, a) memset(x, a, sizeof(x))
#define endl "\n"

using namespace std;
struct data {
  int x, y, pos;
};
data a[maxn], E[maxn];
int n, m;
ll ans;
int root[maxn];
bool cmp(data a, data b) { return a.x < b.x; }
bool cmp1(data a, data b) { return a.y < b.y; }
bool cmp2(data a, data b) { return a.pos < b.pos; }
int get_root(int u) { return (root[u] ? get_root(root[u]) : u); }
void kruskal() {
  sort(E + 1, E + m + 1, cmp2);
  for (int i = 1; i <= m; i++) {
    int p = get_root(E[i].x);
    int q = get_root(E[i].y);
    if (p != q) {
      root[p] = q;
      ans += E[i].pos;
    }
  }
  cout << ans;
}
int main() {
  ios_base::sync_with_stdio(0);
  //    freopen(task".inp", "r", stdin);
  //    freopen(task".out", "w", stdout);
  cin >> n;
  for (int i = 1; i <= n; i++)
    cin >> a[i].x >> a[i].y, a[i].pos = i;
  sort(a + 1, a + n + 1, cmp);
  for (int i = 1; i < n; i++) {
    E[++m].pos = a[i + 1].x - a[i].x;
    E[m].x = a[i].pos, E[m].y = a[i + 1].pos;
  }
  sort(a + 1, a + n + 1, cmp1);
  for (int i = 1; i < n; i++) {
    E[++m].pos = a[i + 1].y - a[i].y;
    E[m].x = a[i].pos, E[m].y = a[i + 1].pos;
  }
  kruskal();
  return 0;
}


Fixed submission:
// He always smiles :)
#include <bits/stdc++.h>
#define maxn 200005
#define mod 1000000007
#define pii pair<int, int>
#define fi first
#define se second
#define pb push_back
#define task "main"
#define ll long long
#define reset(x, a) memset(x, a, sizeof(x))
#define endl "\n"

using namespace std;
struct data {
  int x, y, pos;
};
data a[maxn], E[maxn];
int n, m;
ll ans;
int root[maxn];
bool cmp(data a, data b) { return a.x < b.x; }
bool cmp1(data a, data b) { return a.y < b.y; }
bool cmp2(data a, data b) { return a.pos < b.pos; }
int get_root(int u) { return (root[u] ? root[u] = get_root(root[u]) : u); }
void kruskal() {
  sort(E + 1, E + m + 1, cmp2);
  for (int i = 1; i <= m; i++) {
    int p = get_root(E[i].x);
    int q = get_root(E[i].y);
    if (p != q) {
      root[p] = q;
      ans += E[i].pos;
    }
  }
  cout << ans;
}
int main() {
  ios_base::sync_with_stdio(0);
  //    freopen(task".inp", "r", stdin);
  //    freopen(task".out", "w", stdout);
  cin >> n;
  for (int i = 1; i <= n; i++)
    cin >> a[i].x >> a[i].y, a[i].pos = i;
  sort(a + 1, a + n + 1, cmp);
  for (int i = 1; i < n; i++) {
    E[++m].pos = a[i + 1].x - a[i].x;
    E[m].x = a[i].pos, E[m].y = a[i + 1].pos;
  }
  sort(a + 1, a + n + 1, cmp1);
  for (int i = 1; i < n; i++) {
    E[++m].pos = a[i + 1].y - a[i].y;
    E[m].x = a[i].pos, E[m].y = a[i + 1].pos;
  }
  kruskal();
  return 0;
}
Problem p03682 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
#define ll long long
typedef struct edge {
  ll from, to;
  ll cost;
};
typedef struct points {
  ll x, y;
  ll id;
};
bool xcmp(points a, points b) { return a.x < b.x; }
bool ycmp(points a, points b) { return a.y < b.y; }
bool cmp(edge a, edge b) { return a.cost < b.cost; }
int find(int x, int par[]) {
  while (par[x] != x) {
    x = par[x];
  }
  return x;
}
int main() {
  int n;
  cin >> n;
  points v[n + 1];
  for (int i = 1; i <= n; i++) {
    cin >> v[i].x >> v[i].y;
    v[i].id = i;
  }

  vector<edge> e;
  sort(v + 1, v + n + 1, xcmp);
  for (int i = 1; i < n; i++) {
    edge xx;
    xx.from = v[i].id;
    xx.to = v[i + 1].id;
    xx.cost = v[i + 1].x - v[i].x;
    e.push_back(xx);
  }
  sort(v + 1, v + n + 1, ycmp);
  for (int i = 1; i < n; i++) {
    edge xx;
    xx.from = v[i].id;
    xx.to = v[i + 1].id;
    xx.cost = v[i + 1].y - v[i].y;
    e.push_back(xx);
  }
  sort(e.begin(), e.end(), cmp);
  int par[n + 1];
  for (int i = 1; i <= n; i++) {
    par[i] = i;
  }
  ll ans = 0;
  for (int i = 0; i < e.size(); i++) {
    int x = e[i].from;
    int y = e[i].to;
    int root1 = find(x, par);
    int root2 = find(y, par);
    if (root1 != root2) {
      ans += e[i].cost;
      par[root2] = root1;
    }
  }
  cout << ans << endl;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define ll long long
typedef struct edge {
  ll from, to;
  ll cost;
};
typedef struct points {
  ll x, y;
  ll id;
};
bool xcmp(points a, points b) { return a.x < b.x; }
bool ycmp(points a, points b) { return a.y < b.y; }
bool cmp(edge a, edge b) { return a.cost < b.cost; }
int find(int x, int par[]) {
  if (x == par[x])
    return x;
  return par[x] = find(par[x], par);
}
int main() {
  int n;
  cin >> n;
  points v[n + 1];
  for (int i = 1; i <= n; i++) {
    cin >> v[i].x >> v[i].y;
    v[i].id = i;
  }

  vector<edge> e;
  sort(v + 1, v + n + 1, xcmp);
  for (int i = 1; i < n; i++) {
    edge xx;
    xx.from = v[i].id;
    xx.to = v[i + 1].id;
    xx.cost = v[i + 1].x - v[i].x;
    e.push_back(xx);
  }
  sort(v + 1, v + n + 1, ycmp);
  for (int i = 1; i < n; i++) {
    edge xx;
    xx.from = v[i].id;
    xx.to = v[i + 1].id;
    xx.cost = v[i + 1].y - v[i].y;
    e.push_back(xx);
  }
  sort(e.begin(), e.end(), cmp);
  int par[n + 1];
  for (int i = 1; i <= n; i++) {
    par[i] = i;
  }
  ll ans = 0;
  for (int i = 0; i < e.size(); i++) {
    int x = e[i].from;
    int y = e[i].to;
    int root1 = find(x, par);
    int root2 = find(y, par);
    if (root1 != root2) {
      ans += e[i].cost;
      par[root2] = root1;
    }
  }
  cout << ans << endl;
}
Problem p03683 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <ctime>
#include <fstream>
#include <iostream>
#include <locale>
#include <queue>
#include <set>
#include <stdio.h>
#include <string.h>
#include <string>
#include <vector>
using namespace std;
const long long N = 1000000007;
long long d;
long long fx(long long a) {
  long long c = 1, e = 1;
  while (c <= a) {
    e = (e * c) % N;
    c++;
    if (c == a)
      d = e;
  }
  // cout<<e<<endl;
  return e;
}
int main() {
  long long n, m;
  cin >> n >> m;
  if (n < m) {
    long long x = n;
    n = m;
    m = x;
  }
  if (n > m + 1) {
    cout << 0;
    return 1;
  }
  long long b = fx(n);
  if (n == m) {
    cout << b % N * b % N * 2 % N;
  } else {
    // cout<<b<<d<<endl;
    cout << b % N * d % N;
  }
  return 0;
}


Fixed submission:
#include <algorithm>
#include <cmath>
#include <ctime>
#include <fstream>
#include <iostream>
#include <locale>
#include <queue>
#include <set>
#include <stdio.h>
#include <string.h>
#include <string>
#include <vector>
using namespace std;
const long long N = 1000000007;
long long d;
long long fx(long long a) {
  long long c = 1, e = 1;
  while (c <= a) {
    e = (e * c) % N;
    c++;
    if (c == a)
      d = e;
  }
  // cout<<e<<endl;
  return e;
}
int main() {
  long long n, m;
  cin >> n >> m;
  if (n < m) {
    long long x = n;
    n = m;
    m = x;
  }
  if (n > m + 1) {
    cout << 0;
    return 0;
  }
  long long b = fx(n);
  if (n == m) {
    cout << b % N * b % N * 2 % N;
  } else {
    // cout<<b<<d<<endl;
    cout << b % N * d % N;
  }
  return 0;
}
Problem p03683 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
int f[100000 + 9];
int const mod = 1e9 + 7;
int main() {
  f[0] = 1;
  for (int i = 1; i < 1e5 + 3; i++) {
    f[i] = (1LL * f[i - 1] * i) % mod;
  }
  int n, m;
  cin >> n >> m;
  if (abs(n - m) > 1)
    return puts("0");
  int ans = (1LL * f[n] * f[m]) % mod;
  if (abs(n - m) == 0) {
    ans = (ans * 2) % mod;
  }
  cout << ans;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
int f[100000 + 9];
int const mod = 1e9 + 7;
int main() {
  f[0] = 1;
  for (int i = 1; i < 1e5 + 3; i++) {
    f[i] = (1LL * f[i - 1] * i) % mod;
  }
  int n, m;
  cin >> n >> m;
  if (abs(n - m) > 1) {
    cout << 0;
    return 0;
  }
  int ans = (1LL * f[n] * f[m]) % mod;
  if (abs(n - m) == 0) {
    ans = (ans * 2) % mod;
  }
  cout << ans;
  return 0;
}
Problem p03683 (C++)

Buggy submission (status: Runtime Error):
#include <cmath>
#include <iostream>
using namespace std;

long long fac(int num) {
  long long ans = 1;
  for (long long i = 2; i <= num; i++) {
    ans = (ans * i) % 1000000007LL;
  }
  return ans;
}

int main() {
  int x, y;
  cin >> x >> y;
  if (abs(x - y) > 1) {
    cout << 0;
  } else {
    cout << (2 * fac(x) * fac(y)) % 1000000007LL / (abs(x - y) * 2);
  }
}

Fixed submission:
#include <cmath>
#include <iostream>
using namespace std;

long long fac(int num) {
  long long ans = 1;
  for (long long i = 2; i <= num; i++) {
    ans = (ans * i) % 1000000007LL;
  }
  return ans;
}

int main() {
  int x, y;
  cin >> x >> y;
  if (abs(x - y) > 1) {
    cout << 0;
  } else {
    cout << (2 * fac(x) * fac(y) / (1 + abs(x - y)) % 1000000007LL);
  }
}
Problem p03683 (C++)

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

using namespace std;

#define mod 1000000007

int main() {
  int n, m;
  cin >> n >> m;
  long long int f[100011];
  f[1] = 1;
  f[0] = 1;
  for (int i = 2; i <= 1000011; i++) {
    f[i] = (i * f[i - 1]) % mod;
  }
  if (n >= m + 2 || m >= n + 2) {
    cout << "0\n";
  } else if (n == m) {
    cout << ((f[n] * f[m]) % mod * 2) % mod << "\n";
  } else {
    cout << (f[n] * f[m]) % mod << "\n";
  }
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>

using namespace std;

#define mod 1000000007

int main() {
  int n, m;
  cin >> n >> m;
  long long int f[100011];
  f[1] = 1;
  f[0] = 1;
  for (int i = 2; i <= 100001; i++) {
    f[i] = (i * f[i - 1]) % mod;
  }
  if (n >= m + 2 || m >= n + 2) {
    cout << "0\n";
  } else if (n == m) {
    cout << ((f[n] * f[m]) % mod * 2) % mod << "\n";
  } else {
    cout << (f[n] * f[m]) % mod << "\n";
  }
  return 0;
}
Problem p03684 (C++)

Buggy submission (status: Runtime Error):
// Hi =)) I'm T.A
#include <bits/stdc++.h>
#define nmax 100002
#define f first
#define s second
#define mp make_pair
#define pb push_back
#define pa pair<int, int>
#define ll long long
#define ld long double
#define reset(a, x) memset(a, x, sizeof(a))
#define task "built"

using namespace std;
const int MOD = 1e9 + 7;

int n, cnt(0), root[nmax];
ll res(0);

struct on {
  int u, v, w;
} b[nmax];

struct in {
  int x, y, vt;
} a[nmax];

bool sx1(in a, in b) { return a.x < b.x; }

bool sx2(in a, in b) { return a.y < b.y; }

bool sx3(on a, on b) { return a.w < b.w; }

int getroot(int u) { return root[u] ? (root[u] = getroot(root[u])) : u; }

int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0), cout.tie(0);
  // freopen(task".inp","r",stdin);
  // freopen(task".out","w",stdout);
  cin >> n;
  for (int i = 1; i <= n; i++)
    cin >> a[i].x >> a[i].y, a[i].vt = i;
  sort(a + 1, a + 1 + n, sx1);
  for (int i = 1; i < n; i++)
    b[++cnt] = {a[i].vt, a[i + 1].vt, a[i + 1].x - a[i].x};
  sort(a + 1, a + 1 + n, sx2);
  for (int i = 1; i < n; i++)
    b[++cnt] = {a[i].vt, a[i + 1].vt, a[i + 1].y - a[i].y};
  sort(b + 1, b + 2 * n, sx3);
  for (int i = 1; i < 2 * n; i++) {
    int p = getroot(b[i].u);
    int q = getroot(b[i].v);
    if (p != q) {
      root[p] = q;
      res += 1ll * b[i].w;
    }
  }
  cout << res;

  return 0;
}


Fixed submission:
// Hi =)) I'm T.A
#include <bits/stdc++.h>
#define nmax 200002
#define f first
#define s second
#define mp make_pair
#define pb push_back
#define pa pair<int, int>
#define ll long long
#define ld long double
#define reset(a, x) memset(a, x, sizeof(a))
#define task "built"

using namespace std;
const int MOD = 1e9 + 7;

int n, cnt(0), root[nmax];
ll res(0);

struct on {
  int u, v, w;
} b[nmax];

struct in {
  int x, y, vt;
} a[nmax];

bool sx1(in a, in b) { return a.x < b.x; }

bool sx2(in a, in b) { return a.y < b.y; }

bool sx3(on a, on b) { return a.w < b.w; }

int getroot(int u) { return root[u] ? (root[u] = getroot(root[u])) : u; }

int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0), cout.tie(0);
  // freopen(task".inp","r",stdin);
  // freopen(task".out","w",stdout);
  cin >> n;
  for (int i = 1; i <= n; i++)
    cin >> a[i].x >> a[i].y, a[i].vt = i;
  sort(a + 1, a + 1 + n, sx1);
  for (int i = 1; i < n; i++)
    b[++cnt] = {a[i].vt, a[i + 1].vt, a[i + 1].x - a[i].x};
  sort(a + 1, a + 1 + n, sx2);
  for (int i = 1; i < n; i++)
    b[++cnt] = {a[i].vt, a[i + 1].vt, a[i + 1].y - a[i].y};
  sort(b + 1, b + 2 * n, sx3);
  for (int i = 1; i < 2 * n; i++) {
    int p = getroot(b[i].u);
    int q = getroot(b[i].v);
    if (p != q) {
      root[p] = q;
      res += 1ll * b[i].w;
    }
  }
  cout << res;

  return 0;
}
Problem p03684 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cstdio>
#include <iostream>
#include <vector>
#define N 10005
#define F first
#define S second
using namespace std;

struct st {
  int s, e, d;
};
int n, ans, b[N];
pair<int, int> x[N], y[N];
vector<st> a;

bool cmp(st p, st q) { return p.d < q.d; }

int f(int p) {
  if (b[p] == p)
    return p;
  return b[p] = f(b[p]);
}

int main() {
  int i, t1, t2;
  cin >> n;
  for (i = 1; i <= n; i++)
    scanf("%d %d", &x[i].F, &y[i].F), x[i].S = y[i].S = i;
  for (i = 1; i <= n; i++)
    b[i] = i;
  sort(x + 1, x + n + 1);
  sort(y + 1, y + n + 1);
  for (i = 2; i <= n; i++) {
    a.push_back({x[i - 1].S, x[i].S, x[i].F - x[i - 1].F});
    a.push_back({y[i - 1].S, y[i].S, y[i].F - y[i - 1].F});
  }
  sort(a.begin(), a.end(), cmp);
  for (i = 0; i < a.size(); i++) {
    t1 = f(a[i].e), t2 = f(a[i].s);
    if (t1 != t2)
      b[t1] = t2, ans += a[i].d;
  }
  cout << ans << endl;
  return 0;
}

Fixed submission:
#include <algorithm>
#include <cstdio>
#include <iostream>
#include <vector>
#define N 100005
#define F first
#define S second
using namespace std;

struct st {
  int s, e, d;
};
int n, ans, b[N];
pair<int, int> x[N], y[N];
vector<st> a;

bool cmp(st p, st q) { return p.d < q.d; }

int f(int p) {
  if (b[p] == p)
    return p;
  return b[p] = f(b[p]);
}

int main() {
  int i, t1, t2;
  cin >> n;
  for (i = 1; i <= n; i++)
    scanf("%d %d", &x[i].F, &y[i].F), x[i].S = y[i].S = i;
  for (i = 1; i <= n; i++)
    b[i] = i;
  sort(x + 1, x + n + 1);
  sort(y + 1, y + n + 1);
  for (i = 2; i <= n; i++) {
    a.push_back({x[i - 1].S, x[i].S, x[i].F - x[i - 1].F});
    a.push_back({y[i - 1].S, y[i].S, y[i].F - y[i - 1].F});
  }
  sort(a.begin(), a.end(), cmp);
  for (i = 0; i < a.size(); i++) {
    t1 = f(a[i].e), t2 = f(a[i].s);
    if (t1 != t2)
      b[t1] = t2, ans += a[i].d;
  }
  cout << ans << endl;
  return 0;
}
Problem p03684 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <chrono>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;

int n;
const int maxn = 1e5 + 5;

int x[maxn], y[maxn];

pair<int, pair<int, int>> p[maxn];
int tot;
typedef pair<int, int> pii;

pii z[maxn];
int fa[maxn];
int Find(int x) { return x == fa[x] ? x : fa[x] = Find(fa[x]); }

int main() {
  cin >> n;
  for (int i = 1; i <= n; i++) {
    scanf("%d%d", &x[i], &y[i]);
  }
  for (int i = 1; i <= n; i++) {
    z[i] = make_pair(x[i], i);
  }
  tot = 0;
  sort(z + 1, z + 1 + n);
  for (int i = 1; i < n; i++) {
    int u = z[i].second;
    int v = z[i + 1].second;
    p[++tot] = make_pair(z[i + 1].first - z[i].first, make_pair(u, v));
  }
  for (int i = 1; i <= n; i++) {
    z[i] = make_pair(y[i], i);
  }
  sort(z + 1, z + 1 + n);
  for (int i = 1; i < n; i++) {
    int u = z[i].second;
    int v = z[i + 1].second;
    p[++tot] = make_pair(z[i + 1].first - z[i].first, make_pair(u, v));
  }
  sort(p + 1, p + 1 + tot);
  for (int i = 1; i <= n; i++) {
    fa[i] = i;
  }
  long long ans = 0;
  for (int i = 1; i <= tot; i++) {
    int u = p[i].second.first;
    int v = p[i].second.second;
    int fx = Find(u);
    int fy = Find(v);
    if (fx != fy) {
      fa[fx] = fy;
      ans += p[i].first;
    }
  }
  cout << ans << endl;
  return 0;
}


Fixed submission:
#include <algorithm>
#include <chrono>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;

int n;
const int maxn = 2e5 + 5;

int x[maxn], y[maxn];

pair<int, pair<int, int>> p[maxn];
int tot;
typedef pair<int, int> pii;

pii z[maxn];
int fa[maxn];
int Find(int x) { return x == fa[x] ? x : fa[x] = Find(fa[x]); }

int main() {
  cin >> n;
  for (int i = 1; i <= n; i++) {
    scanf("%d%d", &x[i], &y[i]);
  }
  for (int i = 1; i <= n; i++) {
    z[i] = make_pair(x[i], i);
  }
  tot = 0;
  sort(z + 1, z + 1 + n);
  for (int i = 1; i < n; i++) {
    int u = z[i].second;
    int v = z[i + 1].second;
    p[++tot] = make_pair(z[i + 1].first - z[i].first, make_pair(u, v));
  }
  for (int i = 1; i <= n; i++) {
    z[i] = make_pair(y[i], i);
  }
  sort(z + 1, z + 1 + n);
  for (int i = 1; i < n; i++) {
    int u = z[i].second;
    int v = z[i + 1].second;
    p[++tot] = make_pair(z[i + 1].first - z[i].first, make_pair(u, v));
  }
  sort(p + 1, p + 1 + tot);
  for (int i = 1; i <= n; i++) {
    fa[i] = i;
  }
  long long ans = 0;
  for (int i = 1; i <= tot; i++) {
    int u = p[i].second.first;
    int v = p[i].second.second;
    int fx = Find(u);
    int fy = Find(v);
    if (fx != fy) {
      fa[fx] = fy;
      ans += p[i].first;
    }
  }
  cout << ans << endl;
  return 0;
}
Problem p03684 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <bitset>
#include <cmath>
#include <complex>
#include <ctime>
#include <deque>
#include <iostream>
#include <list>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <string>
#include <vector>
using namespace std;

const int N = 100005;
int n, side;
int fa[N];
long long ans;
pair<int, int> X[N], Y[N];
priority_queue<pair<int, pair<int, int>>> pq;

inline int Find(int x) {
  if (fa[x] == x)
    return x;
  else
    return Find(fa[x]);
}

int main() {
  scanf("%d", &n);
  for (int i = 0; i < n; i++) {
    int x, y;
    scanf("%d%d", &x, &y);
    X[i] = make_pair(x, i);
    Y[i] = make_pair(y, i);
  }
  for (int i = 0; i < n; i++)
    fa[i] = i;
  sort(X, X + n);
  sort(Y, Y + n);
  for (int i = 0; i < n - 1; i++)
    pq.push(make_pair(X[i].first - X[i + 1].first,
                      make_pair(X[i].second, X[i + 1].second)));
  for (int i = 0; i < n - 1; i++)
    pq.push(make_pair(Y[i].first - Y[i + 1].first,
                      make_pair(Y[i].second, Y[i + 1].second)));
  while (side < n - 1) {
    pair<int, pair<int, int>> p = pq.top();
    pq.pop();
    int x = p.second.first;
    int y = p.second.second;
    int dis = -p.first;
    x = Find(x);
    y = Find(y);
    if (x != y) {
      fa[x] = y;
      side++;
      ans += dis;
    }
  }
  printf("%I64d\n", ans);
  return 0;
}

Fixed submission:
#include <algorithm>
#include <bitset>
#include <cmath>
#include <complex>
#include <ctime>
#include <deque>
#include <iostream>
#include <list>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <string>
#include <vector>
using namespace std;

const int N = 100005;
int n, side;
int fa[N];
long long ans;
pair<int, int> X[N], Y[N];
priority_queue<pair<int, pair<int, int>>> pq;

inline int Find(int x) {
  if (fa[x] == x)
    return x;
  else
    return fa[x] = Find(fa[x]);
}

int main() {
  scanf("%d", &n);
  for (int i = 0; i < n; i++) {
    int x, y;
    scanf("%d%d", &x, &y);
    X[i] = make_pair(x, i);
    Y[i] = make_pair(y, i);
  }
  for (int i = 0; i < n; i++)
    fa[i] = i;
  sort(X, X + n);
  sort(Y, Y + n);
  for (int i = 0; i < n - 1; i++)
    pq.push(make_pair(X[i].first - X[i + 1].first,
                      make_pair(X[i].second, X[i + 1].second)));
  for (int i = 0; i < n - 1; i++)
    pq.push(make_pair(Y[i].first - Y[i + 1].first,
                      make_pair(Y[i].second, Y[i + 1].second)));
  while (side < n - 1) {
    pair<int, pair<int, int>> p = pq.top();
    pq.pop();
    int x = p.second.first;
    int y = p.second.second;
    int dis = -p.first;
    x = Find(x);
    y = Find(y);
    if (x != y) {
      fa[x] = y;
      side++;
      ans += dis;
    }
  }
  printf("%I64d\n", ans);
  return 0;
}
Problem p03685 (C++)

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

#define MAX_N 100010

int r, c, n;
stack<int> s;
struct point {
  int id, coor;
} p[4][2 * MAX_N];

int boundary(int x, int y) {
  if (x == 0)
    return 0;
  else if (y == c)
    return 1;
  else if (x == r)
    return 2;
  else if (y == 0)
    return 3;
  else
    return -1;
}

bool cmp_inc(point a, point b) { return a.coor < b.coor; }

bool cmp_dec(point a, point b) { return a.coor > b.coor; }

int main(void) {
  scanf("%d%d%d", &r, &c, &n);
  int cnt[4] = {0};
  for (int i = 1; i <= n; i++) {
    int x[2], y[2];
    scanf("%d%d%d%d", &x[0], &y[0], &x[1], &y[1]);
    bool is_boundary = true;
    for (int j = 0; j < 2; j++) {
      if (x[0] % r && y[0] % c)
        is_boundary = false;
    }
    if (is_boundary) {
      for (int j = 0; j < 2; j++) {
        int t = boundary(x[j], y[j]);
        p[t][cnt[t]].id = i;
        p[t][cnt[t]].coor = t % 2 ? x[j] : y[j];
        cnt[t]++;
      }
    }
  }
  for (int i = 0; i < 4; i++) {
    if (i < 2)
      sort(p[i], p[i] + cnt[i], cmp_inc);
    else
      sort(p[i], p[i] + cnt[i], cmp_dec);
    for (int j = 0; j < cnt[i]; j++) {
      if (s.size() && s.top() == p[i][j].id)
        s.pop();
      else
        s.push(p[i][j].id);
    }
  }
  if (s.empty())
    printf("YES\n");
  else
    printf("NO\n");
  return 0;
}


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

#define MAX_N 100010

int r, c, n;
stack<int> s;
struct point {
  int id, coor;
} p[4][2 * MAX_N];

int boundary(int x, int y) {
  if (x == 0)
    return 0;
  else if (y == c)
    return 1;
  else if (x == r)
    return 2;
  else if (y == 0)
    return 3;
  else
    return -1;
}

bool cmp_inc(point a, point b) { return a.coor < b.coor; }

bool cmp_dec(point a, point b) { return a.coor > b.coor; }

int main(void) {
  scanf("%d%d%d", &r, &c, &n);
  int cnt[4] = {0};
  for (int i = 1; i <= n; i++) {
    int x[2], y[2];
    scanf("%d%d%d%d", &x[0], &y[0], &x[1], &y[1]);
    bool is_boundary = true;
    for (int j = 0; j < 2; j++) {
      if (x[j] % r && y[j] % c)
        is_boundary = false;
    }
    if (is_boundary) {
      for (int j = 0; j < 2; j++) {
        int t = boundary(x[j], y[j]);
        p[t][cnt[t]].id = i;
        p[t][cnt[t]].coor = t % 2 ? x[j] : y[j];
        cnt[t]++;
      }
    }
  }
  for (int i = 0; i < 4; i++) {
    if (i < 2)
      sort(p[i], p[i] + cnt[i], cmp_inc);
    else
      sort(p[i], p[i] + cnt[i], cmp_dec);
    for (int j = 0; j < cnt[i]; j++) {
      if (s.size() && s.top() == p[i][j].id)
        s.pop();
      else
        s.push(p[i][j].id);
    }
  }
  if (s.empty())
    printf("YES\n");
  else
    printf("NO\n");
  return 0;
}
Problem p03685 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <iostream>
#include <queue>

using namespace std;

pair<int, pair<int, int>> E[100005];

int n, m;

int getPoz(int x, int y) // pozitia de pe border
{
  if (x == 0)
    return m - y;
  if (y == 0)
    return m + x;
  if (x == n)
    return n + m + y;
  return n + 2 * m + (n - x);
}

deque<int> Q;

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);

  int num;
  cin >> n >> m >> num;

  int nr = 0;
  for (int i = 1; i <= num; i++) {
    int x, y, xx, yy;
    cin >> x >> y >> xx >> yy;

    if ((x != 0 && x != n && y != 0 && y != m) ||
        (xx != 0 && xx != n && yy != 0 && yy != m))
      continue;

    int ind1 = getPoz(x, y);
    int ind2 = getPoz(xx, yy);
    E[++nr] = {min(ind1, ind2), {1, i}};
    E[++nr] = {max(ind1, ind2), {-1, i}};
  }

  sort(E + 1, E + nr + 1);

  bool ok = 1;
  for (int i = 1; i <= nr; i++) {
    int tip = E[i].second.first;
    int el = E[i].second.second;

    if (tip == 1)
      Q.push_back(el);
    else {
      if (Q.back() != el) {
        ok = 0;
        break;
      }
      Q.pop_back();
    }
  }

  if (ok)
    cout << "YES\n";
  else
    cout << "NO\n";
  return 0;
}


Fixed submission:
#include <algorithm>
#include <iostream>
#include <queue>

using namespace std;

pair<int, pair<int, int>> E[200005];

int n, m;

int getPoz(int x, int y) // pozitia de pe border
{
  if (x == 0)
    return m - y;
  if (y == 0)
    return m + x;
  if (x == n)
    return n + m + y;
  return n + 2 * m + (n - x);
}

deque<int> Q;

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);

  int num;
  cin >> n >> m >> num;

  int nr = 0;
  for (int i = 1; i <= num; i++) {
    int x, y, xx, yy;
    cin >> x >> y >> xx >> yy;

    if ((x != 0 && x != n && y != 0 && y != m) ||
        (xx != 0 && xx != n && yy != 0 && yy != m))
      continue;

    int ind1 = getPoz(x, y);
    int ind2 = getPoz(xx, yy);
    E[++nr] = {min(ind1, ind2), {1, i}};
    E[++nr] = {max(ind1, ind2), {-1, i}};
  }

  sort(E + 1, E + nr + 1);

  bool ok = 1;
  for (int i = 1; i <= nr; i++) {
    int tip = E[i].second.first;
    int el = E[i].second.second;

    if (tip == 1)
      Q.push_back(el);
    else {
      if (Q.back() != el) {
        ok = 0;
        break;
      }
      Q.pop_back();
    }
  }

  if (ok)
    cout << "YES\n";
  else
    cout << "NO\n";
  return 0;
}
Problem p03685 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
using namespace std;

const int MAXN = 100010;
int R, C, n, pn;
int s[MAXN], top;
struct Point {
  int loc, id, t;
  bool operator<(const Point &rhs) const { return loc < rhs.loc; }
} P[MAXN];

inline int read() {
  int x = 0, f = 1;
  char ch = getchar();
  for (; !isdigit(ch); ch = getchar())
    if (ch == '-')
      f = -1;
  for (; isdigit(ch); ch = getchar())
    x = (x << 1) + (x << 3) + (ch ^ 48);
  return x * f;
}

inline int turn(int x, int y) {
  if (x == 0)
    return y;
  if (y == C)
    return C + x;
  if (x == R)
    return C + R + (C - y);
  return 2 * C + R + (R - x);
}

int main() {
  int i;
  R = read(), C = read(), n = read();
  for (i = 1; i <= n; i++) {
    int x1 = read(), y1 = read(), x2 = read(), y2 = read();
    if (x1 != 0 && x1 != R && y1 != 0 && y1 != C)
      continue;
    if (x2 != 0 && x2 != R && y2 != 0 && y2 != C)
      continue;
    int lef = turn(x1, y1), rig = turn(x2, y2);
    if (lef > rig)
      swap(lef, rig);
    P[++pn].loc = lef, P[pn].id = i, P[pn].t = 1;
    P[++pn].loc = rig, P[pn].id = i, P[pn].t = -1;
  }
  sort(P + 1, P + pn + 1);
  // for(i = 1; i <= ln; i++) printf("%d %d\n", L[i].l, L[i].r);
  for (i = 1; i <= pn; i++) {
    if (P[i].t == -1) {
      if (P[i].id != P[s[top]].id)
        break;
      top--;
      continue;
    } else
      s[++top] = i;
  }
  if (i <= pn)
    printf("NO\n");
  else
    printf("YES\n");
  return 0;
}


Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
using namespace std;

const int MAXN = 100010;
int R, C, n, pn;
int s[MAXN], top;
struct Point {
  int loc, id, t;
  bool operator<(const Point &rhs) const { return loc < rhs.loc; }
} P[MAXN << 1];

inline int read() {
  int x = 0, f = 1;
  char ch = getchar();
  for (; !isdigit(ch); ch = getchar())
    if (ch == '-')
      f = -1;
  for (; isdigit(ch); ch = getchar())
    x = (x << 1) + (x << 3) + (ch ^ 48);
  return x * f;
}

inline int turn(int x, int y) {
  if (x == 0)
    return y;
  if (y == C)
    return C + x;
  if (x == R)
    return C + R + (C - y);
  return 2 * C + R + (R - x);
}

int main() {
  int i;
  R = read(), C = read(), n = read();
  for (i = 1; i <= n; i++) {
    int x1 = read(), y1 = read(), x2 = read(), y2 = read();
    if (x1 != 0 && x1 != R && y1 != 0 && y1 != C)
      continue;
    if (x2 != 0 && x2 != R && y2 != 0 && y2 != C)
      continue;
    int lef = turn(x1, y1), rig = turn(x2, y2);
    if (lef > rig)
      swap(lef, rig);
    P[++pn].loc = lef, P[pn].id = i, P[pn].t = 1;
    P[++pn].loc = rig, P[pn].id = i, P[pn].t = -1;
  }
  sort(P + 1, P + pn + 1);
  // for(i = 1; i <= ln; i++) printf("%d %d\n", L[i].l, L[i].r);
  for (i = 1; i <= pn; i++) {
    if (P[i].t == -1) {
      if (P[i].id != P[s[top]].id)
        break;
      top--;
      continue;
    } else
      s[++top] = i;
  }
  if (i <= pn)
    printf("NO\n");
  else
    printf("YES\n");
  return 0;
}
Problem p03685 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cstdio>
#include <stack>
#include <utility>
#include <vector>
using namespace std;
int main() {
  int r, c, n, x, y, x1, y1;
  stack<int> s;
  vector<pair<int, int>> v;
  scanf("%d %d %d", &r, &c, &n);
  for (int i = 0; i < n; i++) {
    scanf("%d %d %d %d", &x, &y, &x1, &y1);
    if ((x == 0 || y == 0 || x == r || y == c) &&
        (x1 == 0 || y1 == 0 || x1 == r || y1 == c)) {
      if (x == 0) {
        v.push_back(make_pair(y, i));
      } else if (y == c) {
        v.push_back(make_pair(c + x, i));
      } else if (x == r) {
        v.push_back(make_pair(2 * c + r - y, i));
      } else {
        v.push_back(make_pair(2 * c + 2 * r - x, i));
      }
      if (x1 == 0) {
        v.push_back(make_pair(y1, i));
      } else if (y1 == c) {
        v.push_back(make_pair(c + x1, i));
      } else if (x1 == r) {
        v.push_back(make_pair(2 * c + r - y1, i));
      } else {
        v.push_back(make_pair(2 * c + 2 * r - x1, i));
      }
    }
  }
  if (v.size()) {
    sort(v.begin(), v.end());
    s.push(v[0].second);
    for (int i = 1; i < v.size(); i++) {
      if (v[i].second == s.top()) {
        s.pop();
      } else {
        s.push(v[i].second);
      }
    }
  }
  if (s.size()) {
    printf("NO\n");
  } else {
    printf("YES\n");
  }
}

Fixed submission:
#include <algorithm>
#include <cstdio>
#include <stack>
#include <utility>
#include <vector>
using namespace std;
int main() {
  int r, c, n, x, y, x1, y1;
  stack<int> s;
  vector<pair<int, int>> v;
  scanf("%d %d %d", &r, &c, &n);
  for (int i = 0; i < n; i++) {
    scanf("%d %d %d %d", &x, &y, &x1, &y1);
    if ((x == 0 || y == 0 || x == r || y == c) &&
        (x1 == 0 || y1 == 0 || x1 == r || y1 == c)) {
      if (x == 0) {
        v.push_back(make_pair(y, i));
      } else if (y == c) {
        v.push_back(make_pair(c + x, i));
      } else if (x == r) {
        v.push_back(make_pair(2 * c + r - y, i));
      } else {
        v.push_back(make_pair(2 * c + 2 * r - x, i));
      }
      if (x1 == 0) {
        v.push_back(make_pair(y1, i));
      } else if (y1 == c) {
        v.push_back(make_pair(c + x1, i));
      } else if (x1 == r) {
        v.push_back(make_pair(2 * c + r - y1, i));
      } else {
        v.push_back(make_pair(2 * c + 2 * r - x1, i));
      }
    }
  }
  if (v.size()) {
    sort(v.begin(), v.end());
    s.push(v[0].second);
    for (int i = 1; i < v.size(); i++) {
      if (!s.empty() && v[i].second == s.top()) {
        s.pop();
      } else {
        s.push(v[i].second);
      }
    }
  }
  if (s.size()) {
    printf("NO\n");
  } else {
    printf("YES\n");
  }
}
Problem p03685 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <iostream>
#include <stack>
#include <vector>
using namespace std;
typedef pair<int, int> P;

int encode(int x, int y, int r, int c) {
  if (y == 0)
    return x;
  if (x == r)
    return r + y;
  if (y == c)
    return r + c + (r - x);
  if (x == 0)
    return 2 * r + 2 * c - y;
  return -1;
}

int main(int argc, char **argv) {
  int r, c, n;
  cin >> r >> c >> n;
  vector<P> pts;
  for (int i = 0; i < n; i++) {
    int x1, y1, x2, y2;
    cin >> x1 >> y1 >> x2 >> y2;
    int n1 = encode(x1, y1, r, c), n2 = encode(x2, y2, r, c);
    if (n1 == -1 || n2 == -1)
      continue;
    pts.push_back(make_pair(min(n1, n2), max(n1, n2)));
  }
  sort(pts.begin(), pts.end());
  stack<int> s;
  for (int i = 0; i < n; i++) {
    int n1 = pts[i].first, n2 = pts[i].second;
    while (!s.empty() && s.top() < n1)
      s.pop();
    if (!s.empty() && s.top() < n2) {
      cout << "NO\n";
      return 0;
    } else
      s.push(n2);
  }
  cout << "YES\n";
}


Fixed submission:
#include <algorithm>
#include <iostream>
#include <stack>
#include <vector>
using namespace std;
typedef pair<int, int> P;

int encode(int x, int y, int r, int c) {
  if (y == 0)
    return x;
  if (x == r)
    return r + y;
  if (y == c)
    return r + c + (r - x);
  if (x == 0)
    return 2 * r + 2 * c - y;
  return -1;
}

int main(int argc, char **argv) {
  int r, c, n;
  cin >> r >> c >> n;
  vector<P> pts;
  for (int i = 0; i < n; i++) {
    int x1, y1, x2, y2;
    cin >> x1 >> y1 >> x2 >> y2;
    int n1 = encode(x1, y1, r, c), n2 = encode(x2, y2, r, c);
    if (n1 == -1 || n2 == -1)
      continue;
    pts.push_back(make_pair(min(n1, n2), max(n1, n2)));
  }
  sort(pts.begin(), pts.end());
  stack<int> s;
  for (P p : pts) {
    int n1 = p.first, n2 = p.second;
    while (!s.empty() && s.top() < n1)
      s.pop();
    if (!s.empty() && s.top() < n2) {
      cout << "NO\n";
      return 0;
    } else
      s.push(n2);
  }
  cout << "YES\n";
}