Problem p02796 (C++)

Buggy submission (status: Time Limit Exceeded):
/**
 * Libraries
 */
#include <algorithm>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <unordered_map>
#include <vector>

#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

using namespace std;

/**
 * macros
 */
#define rep(i, n) for (int i = 0, i##_len = (n); i < i##_len; ++i)
#define repx(i, s, e, d) for (int i = s, i##_end = (e); i < i##_end; i += d)
#define repxr(i, s, e, d) for (int i = s, i##_end = (e); i > i##_end; i += d)
#define rept(n)                                                                \
  for (int ___i___ = 0, i##_len = (n); ___i___ < i##_len; ++___i___)
#define repi(itr, ds) for (auto itr = ds.begin(); itr != ds.end(); itr++)
#define SZ(x) ((int)(x).size())
#define ZERO(a) memset(a, 0, sizeof(a))
#define BIT(n) (1LL << (n))
#define UNIQUE(v) v.erase(unique(v.begin(), v.end()), v.end());
#define CEIL(x, y) (((x) + (y)-1) / (y))

/**
 * helpers
 */
typedef long long int lli;
typedef pair<int, int> ii;
#define int long long int
// struct S{
//     bool operator<(const S& x) const {
//         return ;
//     }
// };
struct Robot {
  int x, l;
  int deleted;
  inline int right() { return x + l; }
  inline int left() { return x - l; }
  bool operator<(const Robot &r) const { return this->x - this->l < r.x - r.l; }
};

signed main() {
  int N;
  cin >> N;
  Robot robots[100000];
  rep(i, N) {
    int x, l;
    cin >> x >> l;
    robots[i].x = x;
    robots[i].l = l;
    robots[i].deleted = 0;
  }
  sort(robots, robots + N);

  ii rL[100000];
  rep(i, N) { rL[i] = ii(i, robots[i].left()); }
  sort(rL, rL + N,
       [](const ii &x, const ii &y) { return x.second < y.second; });

  rep(i, N) {
    if (robots[i].deleted == 1)
      continue;

    int rl = robots[i].left(), rr = robots[i].right();
    auto li = lower_bound(rL, rL + N, ii(-1, rl), [](const ii &x, const ii &y) {
      return x.second < y.second;
    });
    auto ri = lower_bound(rL, rL + N, ii(-1, rr), [](const ii &x, const ii &y) {
      return x.second < y.second;
    });

    bool deleteMe = false;
    for (auto it = li; it != ri; ++it) {
      int j = it->first;
      if (i == j)
        continue;
      if (robots[j].deleted == 1)
        continue;

      if (robots[j].right() < rr) {
        deleteMe = true;
      }
    }

    if (deleteMe) {
      robots[i].deleted = 1;
      continue;
    }
    for (auto it = li; it != ri; ++it) {
      int j = it->first;
      if (i == j)
        continue;
      if (robots[j].deleted == 1)
        continue;

      if (rr <= robots[j].right()) {
        robots[j].deleted = 1;
      }
    }
  }

  int ans = 0;
  rep(i, N) {
    if (robots[i].deleted == 0)
      ans++;
  }
  cout << ans << endl;
  return 0;
}


Fixed submission:
/**
 * Libraries
 */
#include <algorithm>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <unordered_map>
#include <vector>

#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

using namespace std;

/**
 * macros
 */
#define rep(i, n) for (int i = 0, i##_len = (n); i < i##_len; ++i)
#define repx(i, s, e, d) for (int i = s, i##_end = (e); i < i##_end; i += d)
#define repxr(i, s, e, d) for (int i = s, i##_end = (e); i > i##_end; i += d)
#define rept(n)                                                                \
  for (int ___i___ = 0, i##_len = (n); ___i___ < i##_len; ++___i___)
#define repi(itr, ds) for (auto itr = ds.begin(); itr != ds.end(); itr++)
#define SZ(x) ((int)(x).size())
#define ZERO(a) memset(a, 0, sizeof(a))
#define BIT(n) (1LL << (n))
#define UNIQUE(v) v.erase(unique(v.begin(), v.end()), v.end());
#define CEIL(x, y) (((x) + (y)-1) / (y))

/**
 * helpers
 */
typedef long long int lli;
typedef pair<int, int> ii;
#define int long long int
// struct S{
//     bool operator<(const S& x) const {
//         return ;
//     }
// };
struct Robot {
  int x, l;
  int deleted;
  inline int right() { return x + l; }
  inline int left() { return x - l; }
  bool operator<(const Robot &r) const { return this->x - this->l < r.x - r.l; }
};

signed main() {
  int N;
  cin >> N;
  Robot robots[100000];
  rep(i, N) {
    int x, l;
    cin >> x >> l;
    robots[i].x = x;
    robots[i].l = l;
    robots[i].deleted = 0;
  }
  sort(robots, robots + N);

  ii rL[100000];
  rep(i, N) { rL[i] = ii(i, robots[i].left()); }
  sort(rL, rL + N,
       [](const ii &x, const ii &y) { return x.second < y.second; });

  rep(i, N) {
    if (robots[i].deleted == 1)
      continue;

    int rl = robots[i].left(), rr = robots[i].right();
    auto li = lower_bound(rL, rL + N, ii(-1, rl), [](const ii &x, const ii &y) {
      return x.second < y.second;
    });
    auto ri = lower_bound(rL, rL + N, ii(-1, rr), [](const ii &x, const ii &y) {
      return x.second < y.second;
    });

    bool deleteMe = false;
    for (auto it = li; it != ri; ++it) {
      int j = it->first;
      if (i == j)
        continue;
      if (robots[j].deleted == 1)
        continue;

      if (robots[j].right() < rr) {
        deleteMe = true;
        break;
      }
    }

    if (deleteMe) {
      robots[i].deleted = 1;
      continue;
    }
    for (auto it = li; it != ri; ++it) {
      int j = it->first;
      if (i == j)
        continue;
      if (robots[j].deleted == 1)
        continue;

      if (rr <= robots[j].right()) {
        robots[j].deleted = 1;
      }
    }
  }

  int ans = 0;
  rep(i, N) {
    if (robots[i].deleted == 0)
      ans++;
  }
  cout << ans << endl;
  return 0;
}