Problem p01705 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cmath>
#include <iostream>
#include <vector>
using namespace std;
#pragma warning(disable : 4996)
int n, c[120000];
long double x[60000], r[60000], a1[60000], a2[60000];
bool solve(long double p) {
  vector<long double> v;
  v.push_back(-1e9);
  for (int i = 0; i < n; i++) {
    if (r[i] < p) {
      a1[i] = -1e9;
      a2[i] = -1e9;
      continue;
    }
    long double e = sqrtl(r[i] * r[i] - p * p);
    a1[i] = x[i] - e;
    v.push_back(a1[i]);
    a2[i] = x[i] + e;
    v.push_back(a2[i]);
  }
  sort(v.begin(), v.end());
  for (int i = 0; i <= v.size() + 200; i++)
    c[i] = 0;
  for (int i = 0; i < n; i++) {
    if (a1[i] < -1e8)
      continue;
    int pos1 = lower_bound(v.begin(), v.end(), a1[i]) - v.begin();
    int pos2 = lower_bound(v.begin(), v.end(), a2[i]) - v.begin();
    c[pos1]++;
    c[pos2]--;
  }
  for (int i = 1; i <= v.size() + 1; i++)
    c[i] += c[i - 1];
  long double maxn = 0, ret = -1e9;
  for (int i = 1; i < v.size(); i++) {
    if (c[i - 1] == 0 && c[i] >= 1) {
      ret = v[i];
    }
    if (c[i - 1] >= 1 && c[i] == 0) {
      maxn = max(maxn, v[i] - ret);
    }
  }
  if (maxn >= p * 2.0l)
    return true;
  return false;
}
int main() {
  while (true) {
    cin >> n;
    if (n == 0)
      break;
    for (int i = 0; i < n; i++)
      scanf("%Lf%Lf", &x[i], &r[i]);
    for (int i = 0; i < n; i++) {
      a1[i] = 0;
      a2[i] = 0;
    }
    long double L = 0.0l, R = 1.2e5, M;
    for (int i = 0; i < 60; i++) {
      M = (L + R) / 2;
      if (solve(M) == true)
        L = M;
      else
        R = M;
    }
    printf("%.16Lf\\n", M * 2.0l);
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <iostream>
#include <vector>
using namespace std;
#pragma warning(disable : 4996)
int n, c[120000];
long double x[60000], r[60000], a1[60000], a2[60000];
bool solve(long double p) {
  vector<long double> v;
  v.push_back(-1e9);
  for (int i = 0; i < n; i++) {
    if (r[i] < p)
      continue;
    long double e = sqrtl(r[i] * r[i] - p * p);
    a1[i] = x[i] - e;
    v.push_back(a1[i]);
    a2[i] = x[i] + e;
    v.push_back(a2[i]);
  }
  sort(v.begin(), v.end());
  for (int i = 0; i <= v.size() + 200; i++)
    c[i] = 0;
  for (int i = 0; i < n; i++) {
    int pos1 = lower_bound(v.begin(), v.end(), a1[i]) - v.begin();
    int pos2 = lower_bound(v.begin(), v.end(), a2[i]) - v.begin();
    c[pos1]++;
    c[pos2]--;
  }
  for (int i = 1; i <= v.size() + 1; i++)
    c[i] += c[i - 1];
  long double maxn = 0, ret = -1e9;
  for (int i = 1; i < v.size(); i++) {
    if (c[i - 1] == 0 && c[i] >= 1) {
      ret = v[i];
    }
    if (c[i - 1] >= 1 && c[i] == 0) {
      maxn = max(maxn, v[i] - ret);
    }
  }
  if (maxn >= p * 2.0l)
    return true;
  return false;
}
int main() {
  while (true) {
    cin >> n;
    if (n == 0)
      break;
    for (int i = 0; i < n; i++)
      scanf("%Lf%Lf", &x[i], &r[i]);
    long double L = 0.0l, R = 1.2e5, M;
    for (int i = 0; i < 60; i++) {
      M = (L + R) / 2;
      if (solve(M) == true)
        L = M;
      else
        R = M;
    }
    printf("%.16Lf\\n", M * 2.0l);
  }
  return 0;
}