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

#include <algorithm>
#include <cmath>
#include <iostream>
#include <numeric>
#include <string>
#include <vector>

using namespace std;

int par[123];

int find(int x) { return (par[x] == x) ? x : par[x] = find(par[x]); }

void unite(int a, int b) { par[find(a)] = find(b); }

int ex[123][1234], ey[123][1234];
int evx[123][1234], evy[123][1234];

double sqr(double x) { return x * x; }

int main() {
  for (int N, T, R; cin >> N >> T >> R, N | T | R;) {
    char nicknames[123][12];
    vector<int> t[123], x[123], y[123], vx[123], vy[123];
    vector<double> ev;
    for (int i = 0; i < N; i++) {
      cin >> nicknames[i];
      for (int j = 0;; j++) {
        int ti, xi, yi;
        cin >> ti >> xi >> yi;
        ev.push_back(ti);
        t[i].push_back(ti);
        int cx, cy;
        if (j) {
          vx[i].push_back(xi);
          vy[i].push_back(yi);
          for (int k = t[i][j - 1] + 1; k <= ti; k++) {
            ey[i][k] = ey[i][k - 1] + yi;
            ex[i][k] = ex[i][k - 1] + xi;
            evy[i][k - 1] = yi;
            evx[i][k - 1] = xi;
          }
          cy = ey[i][ti];
          cx = ex[i][ti];
        } else {
          cx = xi;
          cy = yi;
          ex[i][0] = cx;
          ey[i][0] = cy;
        }
        x[i].push_back(cx);
        y[i].push_back(cy);
        if (ti == T)
          break;
      }
    }
    for (int i = 0; i < N; i++) {
      for (int j = 0; j < vx[i].size(); j++) {
        int y1 = y[i][j] - t[i][j] * vy[i][j];
        int x1 = x[i][j] - t[i][j] * vx[i][j];
        for (int k = 0; k < i; k++) {
          for (int l = 0; l < vx[k].size(); l++) {
            int y2 = y[k][l] - t[k][l] * vy[k][l];
            int x2 = x[k][l] - t[k][l] * vx[k][l];
            long long X = x1 - x2;
            long long Y = y1 - y2;
            long long VX = vx[i][j] - vx[k][l];
            long long VY = vy[i][j] - vy[k][l];
            long long a = VX * VX + VY * VY;
            long long b = X * VX + Y * VY;
            long long c = X * X + Y * Y - R * R;
            long long inr = b * b - a * c;
            if (inr >= 0 && a) {
              ev.push_back((-b - sqrt(inr)) / a);
              ev.push_back((-b + sqrt(inr)) / a);
            }
          }
        }
      }
    }
    sort(begin(ev), end(ev));
    bool has[123] = {true};
    for (int k = 0; k < ev.size() - 1; k++) {
      double e = (ev[k] + ev[k + 1]) / 2;
      //      cerr<<e<<endl;
      if (e < 0 || T < e)
        continue;
      iota(begin(par), end(par), 0);
      int g = e;
      double f = e - g;
      for (int i = 0; i < N; i++) {
        for (int j = 0; j < i; j++) {
          if (sqr(ey[i][g] + evy[i][g] * f - (ey[j][g] + evy[j][g] * f)) +
                  sqr(ex[i][g] + evx[i][g] * f - (ex[j][g] + evx[j][g] * f)) <=
              R * R) {
            unite(i, j);
          }
        }
      }
      for (int i = 0; i < N; i++) {
        has[find(i)] |= has[i];
      }
      for (int i = 0; i < N; i++) {
        has[i] |= has[find(i)];
      }
    }
    vector<string> ans;
    for (int i = 0; i < N; i++) {
      if (has[i]) {
        ans.push_back(nicknames[i]);
      }
    }
    sort(begin(ans), end(ans));
    for (auto e : ans) {
      cout << e << endl;
    }
  }
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <iostream>
#include <numeric>
#include <string>
#include <vector>

using namespace std;

int par[123];

int find(int x) { return (par[x] == x) ? x : par[x] = find(par[x]); }

void unite(int a, int b) { par[find(a)] = find(b); }

int ex[123][1234], ey[123][1234];
int evx[123][1234], evy[123][1234];

int main() {
  for (int N, T, R; cin >> N >> T >> R, N | T | R;) {
    char nicknames[123][12];
    vector<int> t[123], x[123], y[123], vx[123], vy[123];
    vector<double> ev;
    for (int i = 0; i < N; i++) {
      cin >> nicknames[i];
      for (int j = 0;; j++) {
        int ti, xi, yi;
        cin >> ti >> xi >> yi;
        ev.push_back(ti);
        t[i].push_back(ti);
        int cx, cy;
        if (j) {
          vx[i].push_back(xi);
          vy[i].push_back(yi);
          for (int k = t[i][j - 1] + 1; k <= ti; k++) {
            ey[i][k] = ey[i][k - 1] + yi;
            ex[i][k] = ex[i][k - 1] + xi;
            evy[i][k - 1] = yi;
            evx[i][k - 1] = xi;
          }
          cx = ey[i][ti];
          cy = ex[i][ti];
        } else {
          cx = xi;
          cy = yi;
          ex[i][0] = cx;
          ey[i][0] = cy;
        }
        x[i].push_back(cx);
        y[i].push_back(cy);
        if (ti == T)
          break;
      }
    }
    for (int i = 0; i < N; i++) {
      for (int j = 0; j < vx[i].size(); j++) {
        int y1 = y[i][j] - t[i][j] * vy[i][j];
        int x1 = x[i][j] - t[i][j] * vx[i][j];
        for (int k = 0; k < i; k++) {
          for (int l = 0; l < vx[k].size(); l++) {
            int y2 = y[k][l] - t[k][l] * vy[k][l];
            int x2 = x[k][l] - t[k][l] * vx[k][l];
            long long X = x1 - x2;
            long long Y = y1 - y2;
            long long VX = vx[i][j] - vx[k][l];
            long long VY = vy[i][j] - vy[k][l];
            long long a = VX * VX + VY * VY;
            long long b = X * VX + Y * VY;
            long long c = X * X + Y * Y - R * R;
            long long inr = b * b - a * c;
            if (inr >= 0 && a) {
              ev.push_back((-b - sqrt(inr)) / a);
              ev.push_back((-b + sqrt(inr)) / a);
            }
          }
        }
      }
    }
    sort(begin(ev), end(ev));
    bool has[123] = {true};
    for (int k = 0; k < ev.size() - 1; k++) {
      double e = (ev[k] + ev[k + 1]) / 2;
      if (e < 0 || T < e)
        continue;
      iota(begin(par), end(par), 0);
      int g = e;
      double f = e - g;
      for (int i = 0; i < N; i++) {
        for (int j = 0; j < i; j++) {
          if (hypot(ey[i][g] + evy[i][g] * f - (ey[j][g] + evy[j][g] * f),
                    ex[i][g] + evx[i][g] * f - (ex[j][g] + evx[j][g] * f)) <=
              R) {
            unite(i, j);
          }
        }
      }
      for (int i = 0; i < N; i++) {
        has[find(i)] |= has[i];
      }
      for (int i = 0; i < N; i++) {
        has[i] |= has[find(i)];
      }
    }
    vector<string> ans;
    for (int i = 0; i < N; i++) {
      if (has[i]) {
        ans.push_back(nicknames[i]);
      }
    }
    sort(begin(ans), end(ans));
    for (auto e : ans) {
      cout << e << endl;
    }
  }
}