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

#include <algorithm>
#include <cmath>
#include <complex>
#include <cstdio>
#include <iostream>
using namespace std;
struct P {
  double x, y, z;
  P(double x, double y, double z) : x(x), y(y), z(z) {}
  P() { x = y = z = 0; }
};

bool operator<(const P &a, const P &b) { return a.x < b.x; }

P p[100000];
P q[100];

double dist[100];
int n, Q;

void calc(int idx) {
  dist[idx] = 1e15;
  /*for(int i = 0 ; i < n ; i++){
          double dx = (p[i].x-q[idx].x)*(p[i].x-q[idx].x);
          double dy = (p[i].y-q[idx].y)*(p[i].y-q[idx].y);
          double dz = (p[i].z-q[idx].z)*(p[i].z-q[idx].z);
          dist[idx] = min( dist[idx] , sqrt(dx+dy+dz) );
  }*/
  int s = lower_bound(p, p + n, q[idx]) - p;
  for (int i = s; i < n && abs(p[i].x - q[idx].x) < dist[idx]; i++) {
    double dx = (p[i].x - q[idx].x) * (p[i].x - q[idx].x);
    double dy = (p[i].y - q[idx].y) * (p[i].y - q[idx].y);
    double dz = (p[i].z - q[idx].z) * (p[i].z - q[idx].z);
    dist[idx] = min(dist[idx], sqrt(dx + dy + dz));
  }
  for (int i = s - 1; i >= 0 && abs(p[i].x - q[idx].x) < dist[idx]; i--) {
    double dx = (p[i].x - q[idx].x) * (p[i].x - q[idx].x);
    double dy = (p[i].y - q[idx].y) * (p[i].y - q[idx].y);
    double dz = (p[i].z - q[idx].z) * (p[i].z - q[idx].z);
    dist[idx] = min(dist[idx], sqrt(dx + dy + dz));
  }
}

int main() {
  while (cin >> n >> Q && n) {
    for (int i = 0; i < n; i++)
      scanf("%lf%lf%lf", &p[i].x, &p[i].y, &p[i].z);
    sort(p, p + n);
    while (Q--) {
      int m;
      cin >> m;
      for (int i = 0; i < m; i++) {
        scanf("%lf%lf%lf", &q[i].x, &q[i].y, &q[i].z);
        calc(i);
      }
      double res = 0;
      for (int i = 1; i < m; i++) {
        double dx = (q[i].x - q[i - 1].x) * (q[i].x - q[i - 1].x);
        double dy = (q[i].y - q[i - 1].y) * (q[i].y - q[i - 1].y);
        double dz = (q[i].z - q[i - 1].z) * (q[i].z - q[i - 1].z);

        res += min(dist[i] + dist[i - 1], sqrt(dx + dy + dz));
      }
      printf("%.10lf\\n", res);
    }
  }
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <complex>
#include <cstdio>
#include <iostream>
using namespace std;
struct P {
  double x, y, z;
  P(double x, double y, double z) : x(x), y(y), z(z) {}
  P() { x = y = z = 0; }
};

bool operator<(const P &a, const P &b) { return a.x < b.x; }

P p[100000];
P q[100];

double dist[100];
int n, Q;

void calc(int idx) {
  dist[idx] = 1e15;
  for (int i = 0; i < n; i++) {
    double dx = (p[i].x - q[idx].x) * (p[i].x - q[idx].x);
    double dy = (p[i].y - q[idx].y) * (p[i].y - q[idx].y);
    double dz = (p[i].z - q[idx].z) * (p[i].z - q[idx].z);
    dist[idx] = min(dist[idx], sqrt(dx + dy + dz));
  }
  int s = lower_bound(p, p + n, q[idx]) - p;
  /*for(int i = s ; i < n && abs(p[i].x-q[idx].x) < dist[idx] ; i++){
          double dx = (p[i].x-q[idx].x)*(p[i].x-q[idx].x);
          double dy = (p[i].y-q[idx].y)*(p[i].y-q[idx].y);
          double dz = (p[i].z-q[idx].z)*(p[i].z-q[idx].z);
          dist[idx] = min( dist[idx] , sqrt(dx+dy+dz) );
  }
  for(int i = s-1 ; i >= 0 && abs(p[i].x-q[idx].x) < dist[idx] ; i--){
          double dx = (p[i].x-q[idx].x)*(p[i].x-q[idx].x);
          double dy = (p[i].y-q[idx].y)*(p[i].y-q[idx].y);
          double dz = (p[i].z-q[idx].z)*(p[i].z-q[idx].z);
          dist[idx] = min( dist[idx] , sqrt(dx+dy+dz) );
  }*/
}

int main() {
  while (cin >> n >> Q && n) {
    for (int i = 0; i < n; i++)
      scanf("%lf%lf%lf", &p[i].x, &p[i].y, &p[i].z);
    sort(p, p + n);
    while (Q--) {
      int m;
      cin >> m;
      for (int i = 0; i < m; i++) {
        scanf("%lf%lf%lf", &q[i].x, &q[i].y, &q[i].z);
        calc(i);
      }
      double res = 0;
      for (int i = 1; i < m; i++) {
        double dx = (q[i].x - q[i - 1].x) * (q[i].x - q[i - 1].x);
        double dy = (q[i].y - q[i - 1].y) * (q[i].y - q[i - 1].y);
        double dz = (q[i].z - q[i - 1].z) * (q[i].z - q[i - 1].z);

        res += min(dist[i] + dist[i - 1], sqrt(dx + dy + dz));
      }
      printf("%.10lf\\n", res);
    }
  }
}