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

#include <bits/stdc++.h>
#define pb push_back
#define db double
using namespace std;
db sqr(db x) { return x * x; }
struct B {
  db x, y, z, r;
};
struct L {
  db x, y, z, b;
  db val;
};
struct P {
  db x, y, z;
  P(db x, db y, db z) : x(x), y(y), z(z) {}
  P(B &bl) : x(bl.x), y(bl.y), z(bl.z) {}
  P(L &ls) : x(ls.x), y(ls.y), z(ls.z) {}
  P() {}
  db dis(P &p) { return sqr(p.x - x) + sqr(p.y - y) + sqr(p.z - z); }
};
struct V {
  db x, y, z;
  V() {}
  V(P &p1, P &p2) : x(p2.x - p1.x), y(p2.y - p1.y), z(p2.z - p1.z) {}
  V(V &a, V &b)
      : x(a.y * b.z - a.z * b.y), y(a.x * b.z - a.z * b.x),
        z(a.x * b.y - a.y * b.x) {}
  db dot(V &v) { return x * v.x + y * v.y + z * v.z; }
  db mod() { return x * x + y * y + z * z; }
};
db dis(P &p, P &p1, P &p2) {
  V v1(p1, p), v2(p1, p2), v3(p2, p), v4(p2, p1), v5(v2, v1);
  if (v1.dot(v2) < 0)
    return p.dis(p1);
  if (v3.dot(v4) < 0)
    return p.dis(p2);
  return (db)v5.mod() / (db)v2.mod();
}
const int MAX_N = 2e3 + 50, MAX_M = 1000;
bool vis[MAX_N];
B bl[MAX_N];
L ls[MAX_M];
vector<int> block[MAX_M];
int N, M, R;
P obj;
bool is_block(int i, int j) {
  P a(ls[i]), b(bl[j]);
  if (b.dis(a) < sqr(bl[j].r) && b.dis(obj) < sqr(bl[j].r))
    return false;
  return dis(b, a, obj) < (db)sqr(bl[j].r);
}
bool ok(int x) {
  int cnt = 0;
  memset(vis, 0, sizeof(vis));
  for (int i = 0; i < M; i++) {
    if (!(x & 1 << i))
      continue;
    for (int j = 0; j < block[i].size(); j++)
      if (!vis[block[i][j]])
        vis[block[i][j]] = true, cnt++;
    if (cnt > R)
      return false;
  }
  return true;
}
db calc(int x) {
  db ans = 0;
  for (int i = 0; i < M; i++) {
    if (!(x & 1 << i))
      continue;
    ans += ls[i].val;
  }
  return ans;
}
void solve() {
  double ans = 0;
  for (int i = 0; i < (1 << M); i++) {
    if (ok(i))
      ans = max(ans, calc(i));
  }
  printf("%.9f\\n", ans);
}
int main() {
  // freopen("in", "r", stdin);
  while (~scanf("%d%d%d", &N, &M, &R)) {
    if (N || M || R) {
      for (int i = 0; i < N; i++)
        scanf("%lf%lf%lf%lf", &bl[i].x, &bl[i].y, &bl[i].z, &bl[i].r);
      for (int i = 0; i < M; i++) {
        scanf("%lf%lf%lf%lf", &ls[i].x, &ls[i].y, &ls[i].z, &ls[i].b);
        block[i].clear();
      }
      scanf("%lf%lf%lf", &obj.x, &obj.y, &obj.z);
      for (int i = 0; i < N; i++) {
        P p(ls[i]);
        ls[i].val = (db)ls[i].b / (db)obj.dis(p);
      }
      for (int i = 0; i < M; i++) {
        P p1(ls[i]);
        for (int j = 0; j < N; j++)
          if (is_block(i, j))
            block[i].pb(j);
      }
      solve();
    }
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define pb push_back
#define ll long long
#define db double
using namespace std;
ll sqr(ll x) { return x * x; }
struct B {
  ll x, y, z, r;
};
struct L {
  ll x, y, z, b;
};
struct P {
  ll x, y, z;
  P(ll x, ll y, ll z) : x(x), y(y), z(z) {}
  P(B &bl) : x(bl.x), y(bl.y), z(bl.z) {}
  P(L &ls) : x(ls.x), y(ls.y), z(ls.z) {}
  P() {}
  ll dis(P &p) { return sqr(p.x - x) + sqr(p.y - y) + sqr(p.z - z); }
};
struct V {
  ll x, y, z;
  V() {}
  V(P &p1, P &p2) : x(p2.x - p1.x), y(p2.y - p1.y), z(p2.z - p1.z) {}
  V(V &a, V &b)
      : x(a.y * b.z - a.z * b.y), y(a.x * b.z - a.z * b.x),
        z(a.x * b.y - a.y * b.x) {}
  ll dot(V &v) { return x * v.x + y * v.y + z * v.z; }
  ll mod() { return x * x + y * y + z * z; }
};
db dis(P &p, P &p1, P &p2) {
  V v1(p1, p), v2(p1, p2), v3(p2, p), v4(p2, p1), v(p1, p2);
  if (v1.dot(v2) < 0)
    return p.dis(p1);
  if (v3.dot(v4) < 0)
    return p.dis(p2);
  V a(p1, p2), b(p1, p), c(a, b);
  return (db)c.mod() / (db)a.mod();
}
B bl[(ll)2e3 + 10];
L ls[15];
vector<ll> block[15];
ll N, M, R;
P obj;
bool is_block(ll i, ll j) {
  P a(ls[i]), b(bl[j]);
  if (b.dis(a) < sqr(bl[j].r) && b.dis(obj) < sqr(bl[j].r))
    return false;
  return dis(b, a, obj) < (db)sqr(bl[j].r);
}
set<ll> check;
bool ok(ll x) {
  check.clear();
  for (ll i = 0; i < M; i++) {
    if (!(x & 1 << i))
      continue;
    for (ll j = 0; j < block[i].size(); j++) {
      check.insert(block[i][j]);
    }
  }
  return check.size() <= R;
}
db calc(ll x) {
  db ans = 0;
  for (ll i = 0; i < M; i++) {
    if (!(x & 1 << i))
      continue;
    P p(ls[i]);
    ans += (db)ls[i].b / (db)obj.dis(p);
  }
  return ans;
}
void solve() {
  double ans = 0;
  for (ll i = 0; i < (1 << M); i++) {
    if (ok(i))
      ans = max(ans, calc(i));
  }
  printf("%f\\n", ans);
}
int main() {
  // freopen("in", "r", stdin);
  while (~scanf("%lld%lld%lld", &N, &M, &R)) {
    if (N || M || R) {
      for (ll i = 0; i < N; i++)
        scanf("%lld%lld%lld%lld", &bl[i].x, &bl[i].y, &bl[i].z, &bl[i].r);
      for (ll i = 0; i < M; i++) {
        scanf("%lld%lld%lld%lld", &ls[i].x, &ls[i].y, &ls[i].z, &ls[i].b);
        block[i].clear();
      }
      scanf("%lld%lld%lld", &obj.x, &obj.y, &obj.z);
      for (ll i = 0; i < M; i++) {
        P p1(ls[i]);
        for (ll j = 0; j < N; j++) {
          if (is_block(i, j))
            block[i].pb(j);
        }
      }
      solve();
    }
  }
  return 0;
}