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

#include <bits/stdc++.h>

#define _overload(_1, _2, _3, name, ...) name
#define _rep(i, n) _range(i, 0, n)
#define _range(i, a, b) for (int i = int(a); i < int(b); ++i)
#define rep(...) _overload(__VA_ARGS__, _range, _rep, )(__VA_ARGS__)

#define _rrep(i, n) _rrange(i, n, 0)
#define _rrange(i, a, b) for (int i = int(a) - 1; i >= int(b); --i)
#define rrep(...) _overload(__VA_ARGS__, _rrange, _rrep, )(__VA_ARGS__)

#define _all(arg) begin(arg), end(arg)
#define uniq(arg) sort(_all(arg)), (arg).erase(unique(_all(arg)), end(arg))
#define getidx(ary, key) lower_bound(_all(ary), key) - begin(ary)
#define clr(a, b) memset((a), (b), sizeof(a))
#define bit(n) (1LL << (n))
#define popcount(n) (__builtin_popcountll(n))

template <class T> bool chmax(T &a, const T &b) {
  return (a < b) ? (a = b, 1) : 0;
}
template <class T> bool chmin(T &a, const T &b) {
  return (b < a) ? (a = b, 1) : 0;
}

using namespace std;

using ll = long long;
using R = long double;
struct P {
  ll x, y, z;
  P operator-(const P &arg) const {
    P ret = {x, y, z};
    ret.x -= arg.x, ret.y -= arg.y, ret.z -= arg.z;
    return ret;
  }
};
using L = struct {
  P a, b;
};
inline ll dot(P a, P b) { return a.x * b.x + a.y * b.y + a.z * b.z; }
inline ll norm(ll a) { return a * a; }
inline ll norm(P a, P b) {
  return norm(b.x - a.x) + norm(b.y - a.y) + norm(b.z - a.z);
}

bool dlp(L l, P p, ll r) {
  const ll dr = r * r;
  const ll da = norm(l.a, p), db = norm(l.b, p);
  const ll dl = norm(l.a, l.b);
  const ll cdot = norm(dot(l.b - l.a, p - l.a));

  vector<int> res;
  res.push_back(da < dr);
  res.push_back(db < dr);
  if (dot(l.b - l.a, p - l.a) >= 0 && dot(l.a - l.b, p - l.b) >= 0)
    res.push_back(da * dl - cdot < dr * dl);
  sort(_all(res));
  return res.front() ^ res.back();
}

int main(void) {
  int n, m, r;
  while (cin >> n >> m >> r, n) {
    P sp[2010], tp[16], e;
    ll sr[2010], tb[16];

    rep(i, n) cin >> sp[i].x >> sp[i].y >> sp[i].z >> sr[i];
    rep(i, m) cin >> tp[i].x >> tp[i].y >> tp[i].z >> tb[i];
    cin >> e.x >> e.y >> e.z;

    R ans = 0.0;

    int used[2010];
    rep(i, n) {
      used[i] = 0;
      rep(j, m) if (dlp({e, tp[j]}, sp[i], sr[i])) used[i] |= bit(j);
    }

    rep(mask, 1, 1 << m) {
      vector<int> idx;
      int num = 0;
      rep(i, n) if (mask & used[i]) num++;

      // cout << mask << " " << num << endl;
      if (num <= r) {
        R cur = 0.0;
        rep(i, m) if (mask & bit(i)) cur += 1.0 * tb[i] / norm(e, tp[i]);
        chmax(ans, cur);
      }
    }
    cout.precision(20);
    cout << fixed << ans << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>

#define _overload(_1, _2, _3, name, ...) name
#define _rep(i, n) _range(i, 0, n)
#define _range(i, a, b) for (int i = int(a); i < int(b); ++i)
#define rep(...) _overload(__VA_ARGS__, _range, _rep, )(__VA_ARGS__)

#define _rrep(i, n) _rrange(i, n, 0)
#define _rrange(i, a, b) for (int i = int(a) - 1; i >= int(b); --i)
#define rrep(...) _overload(__VA_ARGS__, _rrange, _rrep, )(__VA_ARGS__)

#define _all(arg) begin(arg), end(arg)
#define uniq(arg) sort(_all(arg)), (arg).erase(unique(_all(arg)), end(arg))
#define getidx(ary, key) lower_bound(_all(ary), key) - begin(ary)
#define clr(a, b) memset((a), (b), sizeof(a))
#define bit(n) (1LL << (n))
#define popcount(n) (__builtin_popcountll(n))

template <class T> bool chmax(T &a, const T &b) {
  return (a < b) ? (a = b, 1) : 0;
}
template <class T> bool chmin(T &a, const T &b) {
  return (b < a) ? (a = b, 1) : 0;
}

using namespace std;

using ll = long long;
using R = long double;
struct P {
  ll x, y, z;
  P operator-(const P &arg) const {
    P ret = {x, y, z};
    ret.x -= arg.x, ret.y -= arg.y, ret.z -= arg.z;
    return ret;
  }
};
using L = struct {
  P a, b;
};
inline ll dot(P a, P b) { return a.x * b.x + a.y * b.y + a.z * b.z; }
inline ll norm(ll a) { return a * a; }
inline ll norm(P a, P b) {
  return norm(b.x - a.x) + norm(b.y - a.y) + norm(b.z - a.z);
}

bool dlp(L l, P p, ll r) {
  const ll dr = r * r;
  const ll da = norm(l.a, p), db = norm(l.b, p);
  const ll dl = norm(l.a, l.b);
  const ll cdot = norm(dot(l.b - l.a, p - l.a));

  vector<int> res;
  res.push_back(da < dr);
  res.push_back(db < dr);
  if (dot(l.b - l.a, p - l.a) >= 0 && dot(l.a - l.b, p - l.b) >= 0)
    res.push_back(da * dl - cdot < dr * dl);
  sort(_all(res));
  return res.front() ^ res.back();
}

int main(void) {
  int n, m, r;
  while (cin >> n >> m >> r, n) {
    P sp[2010], tp[16], e;
    ll sr[2010], tb[16];

    rep(i, n) cin >> sp[i].x >> sp[i].y >> sp[i].z >> sr[i];
    rep(i, m) cin >> tp[i].x >> tp[i].y >> tp[i].z >> tb[i];
    cin >> e.x >> e.y >> e.z;

    R ans = 0.0;
    rep(mask, 1, 1 << m) {
      vector<int> idx;
      rep(i, m) if (mask & bit(i)) idx.push_back(i);
      int num = 0;

      rep(i, n) {
        bool need = false;
        for (auto &j : idx)
          if (dlp({e, tp[j]}, sp[i], sr[i]))
            need = true;
        if (need)
          num++;
      }

      // cout << mask << " " << num << endl;
      if (num <= r) {
        R cur = 0.0;
        for (auto &i : idx)
          cur += 1.0 * tb[i] / norm(e, tp[i]);
        chmax(ans, cur);
      }
    }
    cout.precision(20);
    cout << fixed << ans << endl;
  }
  return 0;
}