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

#include <bits/stdc++.h>
#define FOR(i, k, n) for (int i = (k); i < int(n); ++i)
#define REP(i, n) FOR(i, 0, n)
#define ALL(x) begin(x), end(x)

using namespace std;
using ld = long double;
constexpr ld eps = 1e-8;

using P = array<ld, 3>;
P operator+(P l, P r) { return {l[0] + r[0], l[1] + r[1], l[2] + r[2]}; }
P operator-(P l, P r) { return {l[0] - r[0], l[1] - r[1], l[2] - r[2]}; }
P operator*(ld l, P r) { return {l * r[0], l * r[1], l * r[2]}; }
ld dot(P l, P r) { return inner_product(ALL(l), begin(r), 0.0); }
ld norm(P p) { return dot(p, p); }
ld abs(P p) { return sqrt(norm(p)); }

struct L {
  P a, b;
};
struct S {
  P p;
  ld r;
};

P proj(L l, P p) {
  ld t = dot(p - l.a, l.a - l.b) / norm(l.a - l.b);
  return l.a + t * (l.a - l.b);
}

ld dist_sp(L l, P p) {
  P f = proj(l, p);
  if (abs(f - l.a) + abs(f - l.b) - abs(l.a - l.b) < eps)
    return abs(f - p);
  return min(abs(p - l.a), abs(p - l.b));
}

bool isis_seg_sph(L l, S s) {
  if (abs(l.a - s.p) <= s.r && abs(l.b - s.p) <= s.r)
    return false;
  return dist_sp(l, s.p) <= s.r;
}

int main() {
  while (1) {
    int n, m, R;
    cin >> n >> m >> R;
    if (!n)
      break;
    vector<S> vs;
    REP(i, n) {
      ld x, y, z, r;
      cin >> x >> y >> z >> r;
      vs.push_back((S){{x, y, z}, r});
    }
    vector<tuple<P, ld>> vl;
    REP(i, m) {
      ld x, y, z, b;
      cin >> x >> y >> z >> b;
      vl.emplace_back((P){x, y, z}, b);
    }
    ld ex, ey, ez;
    cin >> ex >> ey >> ez;
    P e = {ex, ey, ez};
    vector<bitset<2000>> v(m);
    vector<ld> vb(m);
    REP(i, m) {
      P p;
      ld b;
      tie(p, b) = vl[i];
      L seg = {e, p};
      REP(j, n) {
        if (isis_seg_sph(seg, vs[j]))
          v[i].set(j);
      }
      vb[i] = b / norm(e - p);
    }
    ld res = 0;
    REP(i, 1 << m) {
      bitset<2000> s;
      ld br = 0;
      REP(j, m) {
        if ((i >> j) & 1) {
          s |= v[j];
          br += vb[j];
        }
      }
      if ((int)s.count() <= R)
        res = max(res, br);
    }
    cout << fixed << setprecision(10) << res << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define FOR(i, k, n) for (int i = (k); i < int(n); ++i)
#define REP(i, n) FOR(i, 0, n)
#define ALL(x) begin(x), end(x)

using namespace std;
using ld = long double;
constexpr ld eps = 1e-8;

using P = array<ld, 3>;
P operator+(P l, P r) { return {l[0] + r[0], l[1] + r[1], l[2] + r[2]}; }
P operator-(P l, P r) { return {l[0] - r[0], l[1] - r[1], l[2] - r[2]}; }
P operator*(ld l, P r) { return {l * r[0], l * r[1], l * r[2]}; }
ld dot(P l, P r) { return inner_product(ALL(l), begin(r), 0.0); }
ld norm(P p) { return dot(p, p); }
ld abs(P p) { return sqrt(norm(p)); }

struct L {
  P a, b;
};
struct S {
  P p;
  ld r;
};

P proj(L l, P p) {
  ld t = dot(p - l.a, l.a - l.b) / norm(l.a - l.b);
  return l.a + t * (l.a - l.b);
}

ld dist_sp(L l, P p) {
  P f = proj(l, p);
  if (abs(f - l.a) + abs(f - l.b) - abs(l.a - l.b) < eps)
    return abs(f - p);
  return min(abs(p - l.a), abs(p - l.b));
}

bool isis_seg_sph(L l, S s) {
  if (abs(l.a - s.p) <= s.r && abs(l.b - s.p) <= s.r)
    return false;
  return dist_sp(l, s.p) <= s.r;
}

int main() {
  while (1) {
    int n, m, R;
    cin >> n >> m >> R;
    if (!n)
      break;
    vector<S> vs;
    REP(i, n) {
      ld x, y, z, r;
      cin >> x >> y >> z >> r;
      vs.push_back((S){{x, y, z}, r});
    }
    vector<tuple<P, ld>> vl;
    REP(i, m) {
      ld x, y, z, b;
      cin >> x >> y >> z >> b;
      vl.emplace_back((P){x, y, z}, b);
    }
    ld ex, ey, ez;
    cin >> ex >> ey >> ez;
    P e = {ex, ey, ez};
    vector<set<int>> v(m);
    vector<ld> vb(m);
    REP(i, m) {
      P p;
      ld b;
      tie(p, b) = vl[i];
      L seg = {e, p};
      REP(j, n) {
        if (isis_seg_sph(seg, vs[j]))
          v[i].insert(j);
      }
      vb[i] = b / norm(e - p);
    }
    ld res = 0;
    REP(i, 1 << m) {
      vector<int> s;
      s.reserve(1000);
      ld br = 0;
      bool ok = true;
      REP(j, m) {
        if ((i >> j) & 1) {
          if ((int)v[j].size() > R) {
            ok = false;
            break;
          }
          for (int k : v[j]) {
            s.push_back(k);
          }
          br += vb[j];
        }
      }
      if (!ok)
        continue;
      sort(ALL(s));
      int sz = unique(ALL(s)) - begin(s);
      if (sz <= R)
        res = max(res, br);
    }
    cout << fixed << setprecision(10) << res << endl;
  }
  return 0;
}