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

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <array>
#include <climits>
#include <cmath>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stdio.h>
#include <string>
#include <vector>

using namespace std;
using P = pair<int, int>;

long long MOD = 1000000007;
long long dp[100005][4];

bool compare_by_b(pair<int, int> a, pair<int, int> b) {
  if (a.second != b.second) {
    return a.second < b.second;
  } else {
    return a.first < b.first;
  }
}

int main() {
  int N, M;
  cin >> N >> M;

  vector<P> ab(M);
  for (int i = 0; i < M; ++i) {
    cin >> ab[i].first >> ab[i].second;
  }
  sort(ab.begin(), ab.end(), compare_by_b);

  vector<int> place;
  for (int i = 0; i < M; ++i) {
    if (!(ab[i].first <= place.back() && place.back() < ab[i].second)) {
      place.push_back(ab[i].second - 1);
    }
  }

  cout << place.size() << endl;
}


Fixed submission:
#include <algorithm>
#include <array>
#include <climits>
#include <cmath>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stdio.h>
#include <string>
#include <vector>

using namespace std;
using P = pair<int, int>;

long long MOD = 1000000007;
long long dp[100005][4];

bool compare_by_b(pair<int, int> a, pair<int, int> b) {
  if (a.second != b.second) {
    return a.second < b.second;
  } else {
    return a.first < b.first;
  }
}

int main() {
  int N, M;
  cin >> N >> M;

  vector<P> ab(M);
  for (int i = 0; i < M; ++i) {
    cin >> ab[i].first >> ab[i].second;
  }
  sort(ab.begin(), ab.end(), compare_by_b);

  vector<int> place;
  for (int i = 0; i < M; ++i) {
    if (place.empty() ||
        !(ab[i].first <= place.back() && place.back() < ab[i].second)) {
      place.push_back(ab[i].second - 1);
    }
  }

  cout << place.size() << endl;
}
Problem p03295 (C++)

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

using namespace std;

bool second_compare(pair<int, int> x, pair<int, int> y) {
  return x.second < y.second;
}

int main() {
  int n, m;
  cin >> n >> m;
  vector<pair<int, int>> a(m);
  for (int i = 0; i < m; i++)
    cin >> a[i].first >> a[i].second;

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

  int ans = 0;
  int pos = 0;
  while (pos < m) {
    pos += distance(a.begin() + pos,
                    min_element(a.begin() + pos, a.end(), second_compare));
    ans++;
    int right_end = a[pos].second;
    while (a[pos].first < right_end) {
      pos++;
    }
  }

  cout << ans << endl;
  return 0;
}

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

using namespace std;

bool second_compare(pair<int, int> x, pair<int, int> y) {
  return x.second < y.second;
}

int main() {
  int n, m;
  cin >> n >> m;
  vector<pair<int, int>> a(m);
  for (int i = 0; i < m; i++)
    cin >> a[i].first >> a[i].second;

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

  int ans = 0;
  int pos = 0;
  while (pos < m) {
    pos += distance(a.begin() + pos,
                    min_element(a.begin() + pos, a.end(), second_compare));
    ans++;
    int right_end = a[pos].second;
    while (pos < m && a[pos].first < right_end) {
      pos++;
    }
  }

  cout << ans << endl;
  return 0;
}
Problem p03295 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <queue>
#include <stdio.h>
#include <string.h>
using namespace std;
#define N 100010
#define inf 1000000007
typedef struct {
  int u, v, next;
} Node;
Node e[N * 3];
int head[N], vis[N], d[N];
int tot, l, r;
void add(int x, int y, int z) {
  e[tot].u = y;
  e[tot].v = z;
  e[tot].next = head[x];
  head[x] = tot++;
}
void Spfa() {
  int i, j, k, t;
  queue<int> q;
  memset(vis, 0, sizeof(vis));
  for (i = l; i <= r; i++)
    d[i] = -inf;
  d[l] = 0;
  vis[l] = 1;
  q.push(l);
  while (!q.empty()) {
    t = q.front();
    q.pop();
    vis[t] = 0;
    for (i = head[t]; i != -1; i = e[i].next) {
      j = e[i].u;
      k = e[i].v;
      if (d[j] < d[t] + k) {
        d[j] = d[t] + k;
        if (vis[j] == 0) {
          vis[j] = 1;
          q.push(j);
        }
      }
    }
  }
}
int main() {
  int i, j, k, x, y, z, n, m;
  scanf("%d%d", &n, &m);
  memset(head, -1, sizeof(head));
  tot = 0;
  l = inf;
  r = -inf;
  for (i = 0; i < m; i++) {
    scanf("%d%d", &x, &y);
    x = x * 2 + 1;
    y = y * 2 - 1;
    add(x, y + 1, 1);
    l = min(l, x);
    r = max(r, y + 1);
  }
  for (i = l; i < r; i++) {
    add(i, i + 1, 0);
    add(i + 1, i, -1);
  }
  Spfa();
  printf("%d\n", d[r]);
  return 0;
}


Fixed submission:
#include <algorithm>
#include <queue>
#include <stdio.h>
#include <string.h>
using namespace std;
#define N 200010
#define inf 1000000007
typedef struct {
  int u, v, next;
} Node;
Node e[N * 3];
int head[N], vis[N], d[N];
int tot, l, r;
void add(int x, int y, int z) {
  e[tot].u = y;
  e[tot].v = z;
  e[tot].next = head[x];
  head[x] = tot++;
}
void Spfa() {
  int i, j, k, t;
  queue<int> q;
  memset(vis, 0, sizeof(vis));
  for (i = l; i <= r; i++)
    d[i] = -inf;
  d[l] = 0;
  vis[l] = 1;
  q.push(l);
  while (!q.empty()) {
    t = q.front();
    q.pop();
    vis[t] = 0;
    for (i = head[t]; i != -1; i = e[i].next) {
      j = e[i].u;
      k = e[i].v;
      if (d[j] < d[t] + k) {
        d[j] = d[t] + k;
        if (vis[j] == 0) {
          vis[j] = 1;
          q.push(j);
        }
      }
    }
  }
}
int main() {
  int i, j, k, x, y, z, n, m;
  scanf("%d%d", &n, &m);
  memset(head, -1, sizeof(head));
  tot = 0;
  l = inf;
  r = -inf;
  for (i = 0; i < m; i++) {
    scanf("%d%d", &x, &y);
    x = x * 2 + 1;
    y = y * 2 - 1;
    add(x, y + 1, 1);
    l = min(l, x);
    r = max(r, y + 1);
  }
  for (i = l; i < r; i++) {
    add(i, i + 1, 0);
    add(i + 1, i, -1);
  }
  Spfa();
  printf("%d\n", d[r]);
  return 0;
}
Problem p03295 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cassert>
#include <climits>
#include <cmath>
#include <deque>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <tuple>
#include <type_traits>
#include <utility>
#include <vector>
using namespace std;
typedef long long ll;
typedef pair<ll, int> P;
#define rep(i, N) for (ll(i) = 0; (i) < (N); (i)++)
#define chmax(x, y) x = max(x, y)
#define chmin(x, y) x = min(x, y)
const int mod = 1000000007;
const int INF = 1001001001;

int main() {
  int n, m;
  cin >> n >> m;
  vector<P> v(n);
  rep(i, m) {
    int a, b;
    cin >> a >> b;
    v[i] = P(b, a);
  }
  sort(v.begin(), v.end());
  int pre = -1;
  int ans = 0;
  rep(i, m) {
    if (pre <= v[i].second) {
      ans++;
      pre = v[i].first;
    }
  }
  cout << ans << endl;
}


Fixed submission:
#include <algorithm>
#include <cassert>
#include <climits>
#include <cmath>
#include <deque>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <tuple>
#include <type_traits>
#include <utility>
#include <vector>
using namespace std;
typedef long long ll;
typedef pair<ll, int> P;
#define rep(i, N) for (ll(i) = 0; (i) < (N); (i)++)
#define chmax(x, y) x = max(x, y)
#define chmin(x, y) x = min(x, y)
const int mod = 1000000007;
const int INF = 1001001001;

int main() {
  int n, m;
  cin >> n >> m;
  vector<P> v(m);
  rep(i, m) {
    int a, b;
    cin >> a >> b;
    v[i] = P(b, a);
  }
  sort(v.begin(), v.end());
  int pre = -1;
  int ans = 0;
  rep(i, m) {
    if (pre <= v[i].second) {
      ans++;
      pre = v[i].first;
    }
  }
  cout << ans << endl;
}
Problem p03295 (C++)

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

#define MAXN 300010
#define MAXM 100010

struct Edge {
  int next, to, w;
} edge[MAXM];
int head[MAXN], cnt;

void Add(int u, int v, int w) {
  edge[++cnt].w = w;
  edge[cnt].next = head[u];
  head[u] = cnt;
  edge[cnt].to = v;
}
// 上面是链式前向星存图
int N, M;
int dis[MAXN];
bool vis[MAXN];

void SPFA(int S) { // SPFA模版
  queue<int> Q;
  Q.push(S);
  for (int i = 0; i <= N + 1; i++)
    dis[i] = 1e9;
  dis[S] = 0;
  vis[S] = 1;
  while (!Q.empty()) {
    int k = Q.front();
    Q.pop();
    vis[k] = 0;
    for (int i = head[k]; i != -1; i = edge[i].next) {
      int to = edge[i].to;
      if (dis[to] > dis[k] + edge[i].w) {
        dis[to] = dis[k] + edge[i].w;
        if (!vis[to]) {
          Q.push(to);
          vis[to] = 1;
        }
      }
    }
  }
}

int main() {
  int x, y, t, S;
  cin >> N >> M;
  S = N + 1; // 源点为N+1
  memset(head, -1, sizeof(head));
  for (int i = 0; i <= N; i++)
    Add(S, i, 0); // Sum(i) - Sum(N+1) <= 0
  for (int i = 1; i <= M; i++) {
    cin >> x >> y;
    Add(y, x, -1); // Sum(x-1) - Sum(y) <= -t
  }
  for (int i = 1; i <= N; i++) {
    Add(i - 1, i, 1); // Sum(i) - Sum(i-1) <= 1
    Add(i, i - 1, 0); // Sum(i-1) - Sum(i) <= 0
  }
  SPFA(S);
  int _min = 1e9;
  for (int i = 0; i <= N; i++)
    _min = min(_min, dis[i]);
  cout << dis[N] - _min << endl;
  return 0;
}

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

#define MAXN 400010
#define MAXM 400005

struct Edge {
  int next, to, w;
} edge[MAXM];
int head[MAXN], cnt;

void Add(int u, int v, int w) {
  edge[++cnt].w = w;
  edge[cnt].next = head[u];
  head[u] = cnt;
  edge[cnt].to = v;
}
// 上面是链式前向星存图
int N, M;
int dis[MAXN];
bool vis[MAXN];

void SPFA(int S) { // SPFA模版
  queue<int> Q;
  Q.push(S);
  for (int i = 0; i <= N + 1; i++)
    dis[i] = 1e9;
  dis[S] = 0;
  vis[S] = 1;
  while (!Q.empty()) {
    int k = Q.front();
    Q.pop();
    vis[k] = 0;
    for (int i = head[k]; i != -1; i = edge[i].next) {
      int to = edge[i].to;
      if (dis[to] > dis[k] + edge[i].w) {
        dis[to] = dis[k] + edge[i].w;
        if (!vis[to]) {
          Q.push(to);
          vis[to] = 1;
        }
      }
    }
  }
}

int main() {
  int x, y, t, S;
  cin >> N >> M;
  S = N + 1; // 源点为N+1
  memset(head, -1, sizeof(head));
  for (int i = 0; i <= N; i++)
    Add(S, i, 0); // Sum(i) - Sum(N+1) <= 0
  for (int i = 1; i <= M; i++) {
    cin >> x >> y;
    Add(y, x, -1); // Sum(x-1) - Sum(y) <= -t
  }
  for (int i = 1; i <= N; i++) {
    Add(i - 1, i, 1); // Sum(i) - Sum(i-1) <= 1
    Add(i, i - 1, 0); // Sum(i-1) - Sum(i) <= 0
  }
  SPFA(S);
  int _min = 1e9;
  for (int i = 0; i <= N; i++)
    _min = min(_min, dis[i]);
  cout << dis[N] - _min << endl;
  return 0;
}
Problem p03295 (C++)

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

using namespace std;

using ll = long long;
using P = pair<int, int>;

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int n, m;
  cin >> n >> m;
  vector<P> v;
  for (int i = 0; i < m; i++) {
    int a, b;
    cin >> a >> b;
    v.emplace_back(a, b);
  }

  auto comp = [](const P &x, const P &y) {
    if (x.second != y.second) {
      return x.second < y.second;
    }
  };

  sort(v.begin(), v.end(), comp);

  int cur = 1, ans = 0;
  for (auto &p : v) {
    if (p.first >= cur) {
      ans++;
      cur = p.second;
    }
  }
  cout << ans << endl;
  return 0;
}

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

using namespace std;

using ll = long long;
using P = pair<int, int>;

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int n, m;
  cin >> n >> m;
  vector<P> v;
  for (int i = 0; i < m; i++) {
    int a, b;
    cin >> a >> b;
    v.emplace_back(a, b);
  }

  auto comp = [](const P &x, const P &y) { return x.second < y.second; };

  sort(v.begin(), v.end(), comp);

  int cur = 1, ans = 0;
  for (auto &p : v) {
    if (p.first >= cur) {
      ans++;
      cur = p.second;
    }
  }
  cout << ans << endl;
  return 0;
}
Problem p03295 (C++)

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

typedef long long int ll;
ll MOD = 1000000007;
ll INFL = 1ll << 60;
ll INF = 1 << 28;

// ====================================================================

int main() {
  int n, m;
  cin >> n >> m;
  vector<pair<pair<int, int>, int>> v1(m); // 左端の位置でソート
  vector<pair<pair<int, int>, int>> v2(m); // 右端の位置でソート
  for (int i = 0; i < m; i++) {
    v1[i].second = v2[i].second = i;
    cin >> v1[i].first.first >> v1[i].first.second;
    v2[i].first.first = v1[i].first.second; // 逆にして突っ込む
    v2[i].first.second = v1[i].first.first;
  }
  sort(v1.begin(), v1.end());
  sort(v2.begin(), v2.end());

  vector<bool> ok(m); // i番目の要求が満たされたか
  int ans = 0, iti = 0;
  for (int i = 0; i < m; i++) {
    if (ok[v2[i].second] == false) {
      ans++;
      while (v1[iti].first.first < v2[i].first.first) {
        ok[v1[iti].second] = true;
        iti++;
      }
    }
  }

  cout << ans << endl;
}


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

typedef long long int ll;
ll MOD = 1000000007;
ll INFL = 1ll << 60;
ll INF = 1 << 28;

// ====================================================================

int main() {
  int n, m;
  cin >> n >> m;
  vector<pair<pair<int, int>, int>> v1(m); // 左端の位置でソート
  vector<pair<pair<int, int>, int>> v2(m); // 右端の位置でソート
  for (int i = 0; i < m; i++) {
    v1[i].second = v2[i].second = i;
    cin >> v1[i].first.first >> v1[i].first.second;
    v2[i].first.first = v1[i].first.second; // 逆にして突っ込む
    v2[i].first.second = v1[i].first.first;
  }
  sort(v1.begin(), v1.end());
  sort(v2.begin(), v2.end());

  vector<bool> ok(m); // i番目の要求が満たされたか
  int ans = 0, iti = 0;
  for (int i = 0; i < m; i++) {
    if (ok[v2[i].second] == false) {
      ans++;
      while (iti < m && v1[iti].first.first < v2[i].first.first) {
        ok[v1[iti].second] = true;
        iti++;
      }
    }
  }

  cout << ans << endl;
}
Problem p03295 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cstdio>
#include <iostream>
using namespace std;
struct Node {
  int l, r;
} pd[10010];
// int fd[10010][10010];
bool cmp(Node x, Node y) {
  if (x.r == x.r)
    return x.l < y.l;
  else
    return x.r < x.r;
}
int main() {
  int n, m, ans = 0;
  cin >> n >> m;
  for (int i = 0; i < m; i++) {
    cin >> pd[i].l >> pd[i].r;
  }
  sort(pd, pd + m, cmp);
  int b = 0x3f3f3f, k = 0;
  for (int i = 0; i < m; i++) {
    if (pd[i].r <= b) {
      b = pd[i].r;
      k = i;
    } else if (pd[i].l >= b) {
      k = i;
      b = pd[i].r;
      ans++;
    }
  }
  cout << ans + 1 << endl;
}


Fixed submission:
#include <algorithm>
#include <cstdio>
#include <iostream>
using namespace std;
struct Node {
  int l, r;
} pd[100010];
// int fd[10010][10010];
bool cmp(Node x, Node y) {
  if (x.r == x.r)
    return x.l < y.l;
  else
    return x.r < x.r;
}
int main() {
  int n, m, ans = 0;
  cin >> n >> m;
  for (int i = 0; i < m; i++) {
    cin >> pd[i].l >> pd[i].r;
  }
  sort(pd, pd + m, cmp);
  int b = 0x3f3f3f, k = 0;
  for (int i = 0; i < m; i++) {
    if (pd[i].r <= b) {
      b = pd[i].r;
      k = i;
    } else if (pd[i].l >= b) {
      k = i;
      b = pd[i].r;
      ans++;
    }
  }
  cout << ans + 1 << endl;
}
Problem p03295 (C++)

Buggy submission (status: Runtime Error):
#ifdef LOCAL
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <vector>
#else
#include <bits/stdc++.h>
#endif
using namespace std;

typedef long long ll;
#define rep(i, n) for (int i = 0; i < n; i++)
#define _sort(arg) sort(begin(arg), end(arg))
#define MOD 1000000007
#define pb push_back
#define DEBUG(x) cout << #x << ": " << x << endl;

/*
__attribute__((constructor))
void initial()
{
        cin.tie(NULL);
        ios::sync_with_stdio(false);
}
*/

class Block {

public:
  ll a, b;
  Block(ll a, ll b) {
    this->a = a;
    this->b = b;
  };
};

bool compare(Block &a, Block &b) { return a.b <= b.b; }

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

  ll n, m;

  cin >> n >> m;

  // ll a[m], b[m];
  /*
  map<ll, std::vector<ll> v > mapping;

  rep(i, m)
  {
          ll a, b;
          cin >> a >> b;
          mapping[a].pb(b);
  }
  */
  std::vector<Block> v;
  rep(i, m) {
    ll a, b;
    cin >> a >> b;
    Block tmp(a, b);
    v.pb(tmp);
  }

  sort(v.begin(), v.end(), compare);

  /*
  for(int i = 0; i < m; i++)
  {
          cout << v[i].a << "; " << v[i].b << endl;
  }
  */

  ll ans = 0;
  ll cur = 0;

  for (int i = 0; i < m; i++) {
    if ((v[i].a - cur < 0) && (v[i].b - cur >= 0)) {
      continue;
    }

    cur = v[i].b;
    ans++;
  }

  cout << ans << endl;

  return 0;
}


Fixed submission:
#ifdef LOCAL
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <vector>
#else
#include <bits/stdc++.h>
#endif
using namespace std;

typedef long long ll;
#define rep(i, n) for (int i = 0; i < n; i++)
#define _sort(arg) sort(begin(arg), end(arg))
#define MOD 1000000007
#define pb push_back
#define DEBUG(x) cout << #x << ": " << x << endl;

/*
__attribute__((constructor))
void initial()
{
        cin.tie(NULL);
        ios::sync_with_stdio(false);
}
*/

class Block {

public:
  ll a, b;
  Block(ll a, ll b) {
    this->a = a;
    this->b = b;
  };
};

bool compare(Block &a, Block &b) { return a.b < b.b; }

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

  ll n, m;

  cin >> n >> m;

  // ll a[m], b[m];
  /*
  map<ll, std::vector<ll> v > mapping;

  rep(i, m)
  {
          ll a, b;
          cin >> a >> b;
          mapping[a].pb(b);
  }
  */
  std::vector<Block> v;
  rep(i, m) {
    ll a, b;
    cin >> a >> b;
    Block tmp(a, b);
    v.pb(tmp);
  }

  sort(v.begin(), v.end(), compare);

  /*
  for(int i = 0; i < m; i++)
  {
          cout << v[i].a << "; " << v[i].b << endl;
  }
  */

  ll ans = 0;
  ll cur = 0;

  for (int i = 0; i < m; i++) {
    if ((v[i].a - cur < 0) && (v[i].b - cur >= 0)) {
      continue;
    }

    cur = v[i].b;
    ans++;
  }

  cout << ans << endl;

  return 0;
}
Problem p03295 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <bits/stdc++.h>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <limits>
#include <queue>
#include <string>
#include <vector>
using namespace std;

#define rep(i, N) for (int i = 0; i < N; i++)
#define pb push_back

typedef long long ll;
typedef pair<ll, ll> P;

bool pairCompare(const P &firstElof, const P &secondElof) {
  return firstElof.second < secondElof.second;
}

int main() {
  ll N, M;
  cin >> N >> M;
  vector<P> v(M);
  for (int i = 0; i < M; ++i) {
    ll a, b;
    cin >> a >> b;
    v[i] = P(a, b);
  }
  sort(v.begin(), v.end(), pairCompare);
  ll cut = -1;
  ll ans = 0;
  for (int i = 0; i < N; ++i) {
    if (v[i].first < cut) {
      continue;
    }
    cut = v[i].second;
    ans++;
  }
  cout << ans << endl;
  return 0;
}

Fixed submission:
#include <algorithm>
#include <bits/stdc++.h>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <limits>
#include <queue>
#include <string>
#include <vector>
using namespace std;

#define rep(i, N) for (int i = 0; i < N; i++)
#define pb push_back

typedef long long ll;
typedef pair<ll, ll> P;

bool pairCompare(const P &firstElof, const P &secondElof) {
  return firstElof.second < secondElof.second;
}

int main() {
  ll N, M;
  cin >> N >> M;
  vector<P> v(M);
  for (int i = 0; i < M; ++i) {
    ll a, b;
    cin >> a >> b;
    v[i] = P(a, b);
  }
  sort(v.begin(), v.end(), pairCompare);
  ll cut = -1;
  ll ans = 0;
  for (int i = 0; i < M; ++i) {
    if (v[i].first < cut) {
      continue;
    }
    cut = v[i].second;
    ans++;
  }
  cout << ans << endl;
  return 0;
}
Problem p03295 (C++)

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

const int N = 2e5 + 100;
const int mod = 1e9 + 7;
typedef long long ll;

typedef pair<int, int> pa;

int n, m;

struct line

{
  int l, r;
  bool operator<(const line &x) { return r < x.r; }
} p[N];
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cin >> n >> m;
  for (int i = 1; i <= m; i++) {
    cin >> p[i].l >> p[i].r;
  }
  sort(p + 1, p + 1 + m);

  int ans = 0;
  int pos = 1;
  while (pos <= m) {
    ans++;
    int len = p[pos].r;
    int po = pos + 1;
    while (len > p[po].l) {
      po++;
    }
    pos = po;
  }
  cout << ans << endl;
  return 0;
}


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

const int N = 2e5 + 100;
const int mod = 1e9 + 7;
typedef long long ll;

typedef pair<int, int> pa;

int n, m;

struct line

{
  int l, r;
  bool operator<(const line &x) { return r < x.r; }
} p[N];
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cin >> n >> m;
  for (int i = 1; i <= m; i++) {
    cin >> p[i].l >> p[i].r;
  }
  sort(p + 1, p + 1 + m);

  int ans = 0;
  int pos = 1;
  while (pos <= m) {
    ans++;
    int len = p[pos].r;
    int po = pos + 1;
    while (len > p[po].l && po <= m) {
      po++;
    }
    pos = po;
  }
  cout << ans << endl;
  return 0;
}
Problem p03295 (C++)

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

using ll = long long;

int main() {
  int n, m;
  cin >> n >> m;
  vector<vector<int>> l(n), r(n);
  vector<bool> ok(n);
  for (int i = 0; i < m; ++i) {
    int a, b;
    cin >> a >> b;
    l[a - 1].push_back(i);
    r[b - 1].push_back(i);
  }

  int ans = 0;
  vector<int> now;
  for (int i = 0; i < n; ++i) {
    for (auto idx : r[i]) {
      if (ok[idx])
        continue;
      ok[idx] = true;
      ans += now.size() > 0;
      for (auto j : now)
        ok[j] = true;
      now.clear();
    }
    now.insert(end(now), begin(l[i]), end(l[i]));
  }

  cout << ans << endl;
}


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

using ll = long long;

int main() {
  int n, m;
  cin >> n >> m;
  vector<vector<int>> l(n), r(n);
  vector<bool> ok(m);
  for (int i = 0; i < m; ++i) {
    int a, b;
    cin >> a >> b;
    l[a - 1].push_back(i);
    r[b - 1].push_back(i);
  }

  int ans = 0;
  vector<int> now;
  for (int i = 0; i < n; ++i) {
    for (auto idx : r[i]) {
      if (ok[idx])
        continue;
      ok[idx] = true;
      ans += now.size() > 0;
      for (auto j : now)
        ok[j] = true;
      now.clear();
    }
    now.insert(end(now), begin(l[i]), end(l[i]));
  }

  cout << ans << endl;
}
Problem p03295 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <iostream>
using namespace std;
pair<int, int> a[10005];
int main() {
  int n, m;
  cin >> n >> m;
  for (int i = 0; i < m; i++) {
    cin >> a[i].second >> a[i].first;
  }
  int ans = 0;
  sort(a, a + m);
  int l = a[0].first;
  for (int i = 1; i < m; i++) {
    if (a[i].second >= l) {
      ans++;
      l = a[i].first;
    }
  }
  cout << ans + 1 << endl;
  return 0;
}


Fixed submission:
#include <algorithm>
#include <iostream>
using namespace std;
pair<int, int> a[100005];
int main() {
  int n, m;
  cin >> n >> m;
  for (int i = 0; i < m; i++) {
    cin >> a[i].second >> a[i].first;
  }
  int ans = 0;
  sort(a, a + m);
  int l = a[0].first;
  for (int i = 1; i < m; i++) {
    if (a[i].second >= l) {
      ans++;
      l = a[i].first;
    }
  }
  cout << ans + 1 << endl;
  return 0;
}
Problem p03295 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <iostream>
#include <map>
#include <set>
#include <unordered_map>
#include <unordered_set>
#include <vector>
using namespace std;

typedef long long ll;

template <typename T> void out(T x) {
  cout << x << endl;
  exit(0);
}
#define watch(x) cout << (#x) << " is " << (x) << endl

const int maxn = 1e5 + 5;

int n, m;

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  cin >> n >> m;
  vector<pair<int, int>> v(n);

  for (int i = 0; i < m; i++) {
    cin >> v[i].second >> v[i].first;
  }

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

  int ans = 0;
  int last = -1;
  for (int i = 0; i < m;) {
    int j = i + 1;
    while (j < m && v[j].first == v[i].first)
      j++;
    if (v[j - 1].second >= last) {
      ans++;
      last = v[i].first;
    }

    i = j;
  }

  cout << ans << endl;
  return 0;
}


Fixed submission:
#include <algorithm>
#include <iostream>
#include <map>
#include <set>
#include <unordered_map>
#include <unordered_set>
#include <vector>
using namespace std;

typedef long long ll;

template <typename T> void out(T x) {
  cout << x << endl;
  exit(0);
}
#define watch(x) cout << (#x) << " is " << (x) << endl

const int maxn = 1e5 + 5;

int n, m;

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  cin >> n >> m;
  vector<pair<int, int>> v(m);

  for (int i = 0; i < m; i++) {
    cin >> v[i].second >> v[i].first;
  }

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

  int ans = 0;
  int last = -1;
  for (int i = 0; i < m;) {
    int j = i + 1;
    while (j < m && v[j].first == v[i].first)
      j++;
    if (v[j - 1].second >= last) {
      ans++;
      last = v[i].first;
    }

    i = j;
  }

  cout << ans << endl;
  return 0;
}
Problem p03295 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#define REP(i, n) for (int i = 0; i < n; ++i)
#define rep(i, n) for (int i = 1; i <= n; ++i)
#define boost ios::sync_with_stdio(0)
#define ms0(s) memset((s), 0, sizeof(s))
#define INF 0x3f3f3f3f
#define ABS(x) ((x) < 0 ? (-(x)) : (x))
#define forvec(it, v)                                                          \
  for (vector<int>::iterator it = v.begin(); it != v.end(); ++it)

#ifdef LOCAL
#define DEBUG 1
#else
#define DEBUG 0
#endif
#define $ if (DEBUG)
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
template <typename T> inline void read(T &x) {
  int w = 1;
  T data = 0;
  char ch = getchar();
  while (!isdigit(ch) && ch != '-')
    ch = getchar();
  if (ch == '-')
    w = -1, ch = getchar();
  while (isdigit(ch))
    data = 10 * data + ch - '0', ch = getchar();
  x = data * w;
}

const int maxn = 100005;
int a[maxn], n, m;
pii seg[maxn];
bool cmp(pii p1, pii p2) {
  return p1.second < p2.second ||
         (p1.second == p2.second && p1.first > p2.first);
}
int main() {
  freopen("in.txt", "r", stdin);
  read(n), read(m);
  REP(i, m) read(seg[i].first), read(seg[i].second);
  sort(seg, seg + m, cmp);
  int pt = seg[0].second, cnt = 1;
  for (int i = 0; i < m; ++i) {
    if (seg[i].first < pt)
      continue;
    pt = seg[i].second;
    cnt++;
  }
  printf("%d\n", cnt);
}


Fixed submission:
#include <bits/stdc++.h>
#define REP(i, n) for (int i = 0; i < n; ++i)
#define rep(i, n) for (int i = 1; i <= n; ++i)
#define boost ios::sync_with_stdio(0)
#define ms0(s) memset((s), 0, sizeof(s))
#define INF 0x3f3f3f3f
#define ABS(x) ((x) < 0 ? (-(x)) : (x))
#define forvec(it, v)                                                          \
  for (vector<int>::iterator it = v.begin(); it != v.end(); ++it)

#ifdef LOCAL
#define DEBUG 1
#else
#define DEBUG 0
#endif
#define $ if (DEBUG)
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
template <typename T> inline void read(T &x) {
  int w = 1;
  T data = 0;
  char ch = getchar();
  while (!isdigit(ch) && ch != '-')
    ch = getchar();
  if (ch == '-')
    w = -1, ch = getchar();
  while (isdigit(ch))
    data = 10 * data + ch - '0', ch = getchar();
  x = data * w;
}

const int maxn = 100005;
int a[maxn], n, m;
pii seg[maxn];
bool cmp(pii p1, pii p2) {
  return p1.second < p2.second ||
         (p1.second == p2.second && p1.first > p2.first);
}
int main() {
  read(n), read(m);
  REP(i, m) read(seg[i].first), read(seg[i].second);
  sort(seg, seg + m, cmp);
  int pt = seg[0].second, cnt = 1;
  for (int i = 0; i < m; ++i) {
    if (seg[i].first < pt)
      continue;
    pt = seg[i].second;
    cnt++;
  }
  printf("%d\n", cnt);
}
Problem p03295 (C++)

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

const int NMAX = 10010;

typedef pair<int, int> ii;

int n, m, res = 0, last = 0;
ii a[NMAX];

bool cmp(ii x, ii y) { return x.second < y.second; }

int main() {
  cin >> n >> m;
  for (int i = 1; i <= m; ++i) {
    cin >> a[i].first >> a[i].second;
  }
  sort(a + 1, a + 1 + m, cmp);
  for (int i = 1; i <= m; ++i)
    if (a[i].first > last) {
      res++;
      last = a[i].second - 1;
    }
  cout << res;
  return 0;
}

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

const int NMAX = 100010;

typedef pair<int, int> ii;

int n, m, res = 0, last = 0;
ii a[NMAX];

bool cmp(ii x, ii y) { return x.second < y.second; }

int main() {
  cin >> n >> m;
  for (int i = 1; i <= m; ++i) {
    cin >> a[i].first >> a[i].second;
  }
  sort(a + 1, a + 1 + m, cmp);
  for (int i = 1; i <= m; ++i)
    if (a[i].first > last) {
      res++;
      last = a[i].second - 1;
    }
  cout << res;
  return 0;
}
Problem p03295 (C++)

Buggy submission (status: Runtime Error):
// -----------------------------------
// author   : MatsuTaku
// country  : Japan
// created  : 06/17/20 20:48:52
// -----------------------------------

#include <bits/stdc++.h>

using namespace std;
using ll = long long;

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

  int n, m;
  cin >> n >> m;
  struct Data {
    int i;
    int a, b;
  };
  vector<Data> abr(m), abl(m);
  for (int i = 0; i < m; i++) {
    abr[i].i = i;
    cin >> abr[i].a >> abr[i].b;
  }
  copy(abr.begin(), abr.end(), abl.begin());
  sort(abr.begin(), abr.end(), [&](auto l, auto r) { return l.b < r.b; });
  sort(abl.begin(), abl.end(), [&](auto l, auto r) { return l.a < r.a; });
  int ans = 0;
  vector<int> vis(n, false);
  int li = 0;
  for (int i = 0; i < m; i++) {
    if (vis[abr[i].i])
      continue;
    ans++;
    vis[abr[i].i] = true;
    while (li < m and abl[li].a < abr[i].b) {
      vis[abl[li++].i] = true;
    }
  }
  cout << ans << endl;

  return 0;
}


Fixed submission:
// -----------------------------------
// author   : MatsuTaku
// country  : Japan
// created  : 06/17/20 20:48:52
// -----------------------------------

#include <bits/stdc++.h>

using namespace std;
using ll = long long;

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

  int n, m;
  cin >> n >> m;
  struct Data {
    int i;
    int a, b;
  };
  vector<Data> abr(m), abl(m);
  for (int i = 0; i < m; i++) {
    abr[i].i = i;
    cin >> abr[i].a >> abr[i].b;
  }
  copy(abr.begin(), abr.end(), abl.begin());
  sort(abr.begin(), abr.end(), [&](auto l, auto r) { return l.b < r.b; });
  sort(abl.begin(), abl.end(), [&](auto l, auto r) { return l.a < r.a; });
  int ans = 0;
  vector<int> vis(m, false);
  int li = 0;
  for (int i = 0; i < m; i++) {
    if (vis[abr[i].i])
      continue;
    ans++;
    vis[abr[i].i] = true;
    while (li < m and abl[li].a < abr[i].b) {
      vis[abl[li++].i] = true;
    }
  }
  cout << ans << endl;

  return 0;
}
Problem p03295 (C++)

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

#define clr(i, j) memset(i, j, sizeof(i))
#define pb push_back
#define PI acos(-1)
#define MOD ((int)1e9 + 7)
typedef long long ll;

//-*-*-*-*-*-*-*-*-

const int N = 2e5 + 100;
ll visited[100005];
// ll a[200000];

#define lop(i, n) for (int i = 0; i < n; ++i)
// ll n,m,mod;

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

const int MAX = 2e5 + 10;
void finis() { cout << -1; }
vector<ll> v[10000];

ll h, w, n, m;

int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cin >> n >> m;
  ll lol = 0;
  ll ans = 0;
  ll sum = 0;
  for (int i = 1; i <= m; i++) {
    ll x, y;
    cin >> x >> y;
    ll xx = max(x, y);
    ll yy = min(x, y);
    v[xx].push_back(yy);
  }
  ll mx = -11;
  for (int i = n; i >= 1; i--) {
    if (i == mx) {
      ans++;
      mx = -111;
    }
    for (ll j : v[i]) {
      mx = max(j, mx);
    }
  }
  cout << ans;
}


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

#define clr(i, j) memset(i, j, sizeof(i))
#define pb push_back
#define PI acos(-1)
#define MOD ((int)1e9 + 7)
typedef long long ll;

//-*-*-*-*-*-*-*-*-

const int N = 2e5 + 100;
ll visited[100005];
// ll a[200000];

#define lop(i, n) for (int i = 0; i < n; ++i)
// ll n,m,mod;

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

const int MAX = 2e5 + 10;
void finis() { cout << -1; }
vector<ll> v[1000000];

ll h, w, n, m;

int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cin >> n >> m;
  ll lol = 0;
  ll ans = 0;
  ll sum = 0;
  for (int i = 1; i <= m; i++) {
    ll x, y;
    cin >> x >> y;
    ll xx = max(x, y);
    ll yy = min(x, y);
    v[xx].push_back(yy);
  }
  ll mx = -11;
  for (int i = n; i >= 1; i--) {
    if (i == mx) {
      ans++;
      mx = -111;
    }
    for (ll j : v[i]) {
      mx = max(j, mx);
    }
  }
  cout << ans;
}
Problem p03295 (C++)

Buggy submission (status: Runtime Error):
#include "bits/stdc++.h"
using namespace std;
#define rep(i, n) for (ll i = 0; i < n; i++)
#define REP(i, a, n) for (ll i = a; i < n; i++)
#define ALL(s) s.begin(), s.end()
#define P pair<ll, ll>
#define vl vector<ll>
#define vl vector<ll>
#define vvl vector<vector<ll>>
#define vvl vector<vector<ll>>
#define print(n) cout << n << endl
const int M = 100010;
const int MOD = 1000000007;
const int inf = 1000000007;
const long long INF = 1000000000000000007;
using ll = long long;
int dx[4] = {-1, 0, 1, 0}, dy[4] = {0, -1, 0, 1};

int main() {
  int n, m, x[100010] = {}, ans = 0;
  P p[1000];
  cin >> n >> m;
  rep(i, m) cin >> p[i].second >> p[i].first;
  sort(p, p + m);
  ans++;
  int t;
  t = p[0].first;
  rep(i, m) if (t <= p[i].second) t = p[i].first, ans++;
  print(ans);
}

Fixed submission:
#include "bits/stdc++.h"
using namespace std;
#define rep(i, n) for (ll i = 0; i < n; i++)
#define REP(i, a, n) for (ll i = a; i < n; i++)
#define ALL(s) s.begin(), s.end()
#define P pair<ll, ll>
#define vl vector<ll>
#define vl vector<ll>
#define vvl vector<vector<ll>>
#define vvl vector<vector<ll>>
#define print(n) cout << n << endl
const int M = 100010;
const int MOD = 1000000007;
const int inf = 1000000007;
const long long INF = 1000000000000000007;
using ll = long long;
int dx[4] = {-1, 0, 1, 0}, dy[4] = {0, -1, 0, 1};

int main() {
  int n, m, x[100010] = {}, ans = 0;
  P p[100010];
  cin >> n >> m;
  rep(i, m) cin >> p[i].second >> p[i].first;
  sort(p, p + m);
  ans++;
  int t;
  t = p[0].first;
  rep(i, m) if (t <= p[i].second) t = p[i].first, ans++;
  print(ans);
}
Problem p03295 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < n; i++)
#define REP(i, x, n) for (int i = x; i < n; i++)
using namespace std;
using vi = vector<int>;
using vvi = vector<vi>;
using ll = long long;
using P = pair<int, int>;

int main() {
  int n, m;
  cin >> n >> m;
  vector<P> vec(m);
  rep(i, m) {
    int a, b;
    cin >> a >> b;
    vec[i].first = b;
    vec[i].second = a;
  }
  sort(vec.begin(), vec.end());

  int cur = -1e9;
  int ans = 0;
  rep(i, n) {
    if (vec[i].second >= cur) {
      cur = vec[i].first;
      ans++;
    }
  }

  cout << ans << endl;
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < n; i++)
#define REP(i, x, n) for (int i = x; i < n; i++)
using namespace std;
using vi = vector<int>;
using vvi = vector<vi>;
using ll = long long;
using P = pair<int, int>;

int main() {
  int n, m;
  cin >> n >> m;
  vector<P> vec(m);
  rep(i, m) {
    int a, b;
    cin >> a >> b;
    vec[i].first = b;
    vec[i].second = a;
  }
  sort(vec.begin(), vec.end());

  int cur = -1e9;
  int ans = 0;
  rep(i, m) {
    if (vec[i].second >= cur) {
      cur = vec[i].first;
      ans++;
    }
  }

  cout << ans << endl;
  return 0;
}
Problem p03295 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <iostream>
#include <string>
using namespace std;
pair<int, int> pairs[10001];
int main() {
  int n, m;
  cin >> n >> m;
  int i, j, k, l;
  for (i = 0; i < m; i++) {
    // second is a[i]
    cin >> pairs[i].second >> pairs[i].first;
  }
  int ans = 0, tmp = 0;
  sort(pairs, pairs + m);

  for (i = 0; i < m; i++) {
    if (pairs[i].second >= tmp) {
      tmp = pairs[i].first;
      ans++;
    }
  }
  cout << ans << endl;
}


Fixed submission:
#include <algorithm>
#include <cmath>
#include <iostream>
#include <string>
using namespace std;
pair<int, int> pairs[100001];
int main() {
  int n, m;
  cin >> n >> m;
  int i, j, k, l;
  for (i = 0; i < m; i++) {
    // second is a[i]
    cin >> pairs[i].second >> pairs[i].first;
  }
  int ans = 0, tmp = 0;
  sort(pairs, pairs + m);

  for (i = 0; i < m; i++) {
    if (pairs[i].second >= tmp) {
      tmp = pairs[i].first;
      ans++;
    }
  }
  cout << ans << endl;
}
Problem p03295 (C++)

Buggy submission (status: Runtime Error):

#include <algorithm>
#include <bitset>
#include <cstdio>
#include <functional>
#include <iostream>
#include <list>
#include <math.h>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <vector>
// #include "bits/stdc++.h"

using namespace std;
// debug

#define rep(i, N) for (int i = 0; i < N; i++)
#define rep2(i, N, M) for (int i = N; i < M; i++)
#define pb push_back

typedef long long ll;
typedef pair<ll, ll> pll;
typedef pair<int, int> pii;
typedef vector<ll> vll;
typedef vector<vll> vvll;
typedef vector<string> vs;
typedef priority_queue<pll, vector<pll>, greater<pll>> greater_pq;

#define all(a) (a).begin(), (a).end()
#define rall(a) (a).rbegin(), (a).rend()
#define vec(a) vector<a>

void f(string &str) {
  str.insert(str.begin(), str.back());
  str.pop_back();
}

int main() {
  int N, M;
  cin >> N >> M;
  vector<int> a(M), b(M), color(M, 0);
  vector<pii> v;
  vector<pii> w;

  rep(i, M) {
    cin >> a[i] >> b[i];
    --a[i];
    --b[i];
    v.push_back(make_pair(i, a[i]));
    w.push_back(make_pair(i, b[i]));
  }

  sort(all(v), [](pii x, pii y) { return x.second < y.second; });
  sort(all(w), [](pii x, pii y) { return x.second < y.second; });
  ll res = 0;
  ll state = 0;
  ll j = 0;
  rep(i, M) {
    if (color[w[i].first] == 1) {
      continue;
    }
    res++;
    for (j = state; v[j].second < w[i].second; j++) {

      color[v[state].first] = 1;
      state++;
    }
  }

  cout << res << endl;

  return 0;
}


Fixed submission:

#include <algorithm>
#include <bitset>
#include <cstdio>
#include <functional>
#include <iostream>
#include <list>
#include <math.h>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <vector>
// #include "bits/stdc++.h"

using namespace std;
// debug

#define rep(i, N) for (int i = 0; i < N; i++)
#define rep2(i, N, M) for (int i = N; i < M; i++)
#define pb push_back

typedef long long ll;
typedef pair<ll, ll> pll;
typedef pair<int, int> pii;
typedef vector<ll> vll;
typedef vector<vll> vvll;
typedef vector<string> vs;
typedef priority_queue<pll, vector<pll>, greater<pll>> greater_pq;

#define all(a) (a).begin(), (a).end()
#define rall(a) (a).rbegin(), (a).rend()
#define vec(a) vector<a>

void f(string &str) {
  str.insert(str.begin(), str.back());
  str.pop_back();
}

int main() {
  int N, M;
  cin >> N >> M;
  vector<int> a(M), b(M), color(M, 0);
  vector<pii> v;
  vector<pii> w;

  rep(i, M) {
    cin >> a[i] >> b[i];
    --a[i];
    --b[i];
    v.push_back(make_pair(i, a[i]));
    w.push_back(make_pair(i, b[i]));
  }

  sort(all(v), [](pii x, pii y) { return x.second < y.second; });
  sort(all(w), [](pii x, pii y) { return x.second < y.second; });
  ll res = 0;
  ll state = 0;
  ll j = 0;
  rep(i, M) {
    if (color[w[i].first] == 1) {
      continue;
    }
    res++;
    for (j = state; j < M; j++) {
      if (v[j].second < w[i].second) {
        color[v[state].first] = 1;
        state++;
      }
    }
  }

  cout << res << endl;

  return 0;
}
Problem p03295 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <deque>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <limits>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdio.h>
#include <stdlib.h>
#include <string>
#include <vector>
using namespace std;
#define FOR(I, A, B) for (int I = (A); I < (B); ++I)
#define CLR(mat) memset(mat, 0, sizeof(mat))
typedef long long ll;
typedef pair<int, int> P;
typedef pair<P, int> PP;
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int n, m;
  cin >> n >> m;
  vector<PP> v(2 * m);
  int gr = 1;
  int group[n + 1];
  CLR(group);
  FOR(i, 0, m) {
    int a, b;
    cin >> a >> b;
    v[i] = PP(P(a, 1), i);
    v[i + m] = PP(P(b, -1), i);
  }
  sort(v.begin(), v.end());
  int ans = 0;
  bool up = false;
  FOR(i, 0, 2 * m) {
    if (v[i].first.second == 1) {
      group[v[i].second] = gr;
      if (!up) {
        up = true;
        ans++;
      }
    } else {
      if (group[v[i].second] == gr) {
        up = false;
        gr++;
      }
    }
  }
  cout << ans << endl;
}

Fixed submission:
#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <deque>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <limits>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdio.h>
#include <stdlib.h>
#include <string>
#include <vector>
using namespace std;
#define FOR(I, A, B) for (int I = (A); I < (B); ++I)
#define CLR(mat) memset(mat, 0, sizeof(mat))
typedef long long ll;
typedef pair<int, int> P;
typedef pair<P, int> PP;
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int n, m;
  cin >> n >> m;
  vector<PP> v(2 * m);
  int gr = 1;
  int group[m + 1];
  CLR(group);
  FOR(i, 0, m) {
    int a, b;
    cin >> a >> b;
    v[i] = PP(P(a, 1), i);
    v[i + m] = PP(P(b, -1), i);
  }
  sort(v.begin(), v.end());
  int ans = 0;
  bool up = false;
  FOR(i, 0, 2 * m) {
    if (v[i].first.second == 1) {
      group[v[i].second] = gr;
      if (!up) {
        up = true;
        ans++;
      }
    } else {
      if (group[v[i].second] == gr) {
        up = false;
        gr++;
      }
    }
  }
  cout << ans << endl;
}
Problem p03295 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define sync                                                                   \
  ios::sync_with_stdio(false);                                                 \
  cin.tie(NULL);                                                               \
  cerr.tie(NULL);

int main() {
  int n, m;
  cin >> n >> m;
  pair<int, int> a[m];
  for (int i = 0; i < m; i++) {
    cin >> a[i].first >> a[i].second;
  }
  sort(a, a + n);
  int left = INT_MAX, right = INT_MAX, count = 0;
  for (int i = 0; i < m; i++) {
    if (a[i].first >= left && a[i].first < right) {
      left = a[i].first;
      right = min(right, a[i].second);
    } else {
      count++;
      left = a[i].first, right = a[i].second;
    }
  }
  cout << count << endl;
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define sync                                                                   \
  ios::sync_with_stdio(false);                                                 \
  cin.tie(NULL);                                                               \
  cerr.tie(NULL);

int main() {
  int n, m;
  cin >> n >> m;
  pair<int, int> a[m];
  for (int i = 0; i < m; i++) {
    cin >> a[i].first >> a[i].second;
  }
  sort(a, a + m);
  int left = INT_MAX, right = INT_MAX, count = 0;
  for (int i = 0; i < m; i++) {
    if (a[i].first >= left && a[i].first < right) {
      left = a[i].first;
      right = min(right, a[i].second);
    } else {
      count++;
      left = a[i].first, right = a[i].second;
    }
  }
  cout << count << endl;
  return 0;
}
Problem p03295 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define REP(i, n) for (int i = 0; i < n; i++)
#define REPR(i, n) for (int i = n; i >= 0; i--)
#define FOR(i, m, n) for (int i = m; i < n; i++)
#define FORR(i, m, n) for (int i = m; i >= n; i--)
#define SORT(v, n) sort(v, v + n);
#define VSORT(v) sort(v.begin(), v.end());
#define ll long long
#define pb(a) push_back(a)
#define INF 1000000000
using namespace std;
typedef pair<int, int> P;
typedef pair<ll, ll> LP;
typedef pair<int, P> PP;
typedef pair<ll, LP> LPP;

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

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int n, m;
  cin >> n >> m;
  vector<P> itv(n);
  REP(i, m) {
    cin >> itv[i].second >> itv[i].first; // 終了時間，開始時間
  }
  int ans = 0, t = 0;
  VSORT(itv)
  REP(i, m) {
    if (t <= itv[i].second) {
      ans++;
      t = itv[i].first;
    }
  }
  cout << ans << endl;
}

Fixed submission:
#include <bits/stdc++.h>
#define REP(i, n) for (int i = 0; i < n; i++)
#define REPR(i, n) for (int i = n; i >= 0; i--)
#define FOR(i, m, n) for (int i = m; i < n; i++)
#define FORR(i, m, n) for (int i = m; i >= n; i--)
#define SORT(v, n) sort(v, v + n);
#define VSORT(v) sort(v.begin(), v.end());
#define ll long long
#define pb(a) push_back(a)
#define INF 1000000000
using namespace std;
typedef pair<int, int> P;
typedef pair<ll, ll> LP;
typedef pair<int, P> PP;
typedef pair<ll, LP> LPP;

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

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int n, m;
  cin >> n >> m;
  vector<P> itv(m);
  REP(i, m) {
    cin >> itv[i].second >> itv[i].first; // 終了時間，開始時間
  }
  int ans = 0, t = 0;
  VSORT(itv)
  REP(i, m) {
    if (t <= itv[i].second) {
      ans++;
      t = itv[i].first;
    }
  }
  cout << ans << endl;
}
Problem p03295 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <vector>
using namespace std;
typedef long long int ll;
typedef vector<int> vi;
typedef pair<int, int> P;
struct pos {
  int row;
  int column;
};
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define Sort(a) sort(a.begin(), a.end())
const int INF = 1 << 30;
const ll MOD = 1000000007;
int main(void) {
  int N, M;
  vector<P> dp;
  cin >> N >> M;
  rep(i, M) {
    int left, right;
    cin >> left >> right;
    dp.push_back(P(right, left));
  }

  Sort(dp);

  int count = 0;
  int before = 0;
  rep(i, N) {
    P p = dp[i];
    if (count == 0 || before <= p.second) {
      count++;
      before = p.first;
    }
  }

  cout << count << endl;

  return 0;
}

Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <vector>
using namespace std;
typedef long long int ll;
typedef vector<int> vi;
typedef pair<int, int> P;
struct pos {
  int row;
  int column;
};
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define Sort(a) sort(a.begin(), a.end())
const int INF = 1 << 30;
const ll MOD = 1000000007;
int main(void) {
  int N, M;
  vector<P> dp;
  cin >> N >> M;
  rep(i, M) {
    int left, right;
    cin >> left >> right;
    dp.push_back(P(right, left));
  }

  Sort(dp);

  int count = 0;
  int before = 0;
  rep(i, M) {
    P p = dp[i];
    if (count == 0 || before <= p.second) {
      count++;
      before = p.first;
    }
  }

  cout << count << endl;

  return 0;
}