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

#include <algorithm>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <functional>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <string>
#include <utility>
#include <vector>

using namespace std;

/* constant */

const int MAX_N = 100000;

const double INF = 1e70;

/* typedef */

template <typename T> struct Pt {
  T x, y;

  Pt() {}
  Pt(T _x, T _y) : x(_x), y(_y) {}
  Pt(const Pt &pt) : x(pt.x), y(pt.y) {}

  bool operator==(const Pt pt) const { return x == pt.x && y == pt.y; }
  Pt<T> operator+(const Pt pt) const { return Pt<T>(x + pt.x, y + pt.y); }
  Pt<T> operator-() const { return Pt<T>(-x, -y); }
  Pt<T> operator-(const Pt pt) const { return Pt<T>(x - pt.x, y - pt.y); }
  Pt<T> operator*(T t) const { return Pt<T>(x * t, y * t); }
  Pt<T> operator/(T t) const { return Pt<T>(x / t, y / t); }
  T dot(Pt v) const { return x * v.x + y * v.y; }
  T cross(Pt v) const { return x * v.y - y * v.x; }
  Pt<T> mid(const Pt pt) { return Pt<T>((x + pt.x) / 2, (y + pt.y) / 2); }
  T d2() { return x * x + y * y; }
  double d() { return sqrt(d2()); }

  Pt<T> rot(double th) {
    double c = cos(th), s = sin(th);
    return Pt<T>(c * x - s * y, s * x + c * y);
  }

  bool operator<(const Pt &pt) const {
    return x < pt.x || (x == pt.x && y < pt.y);
  }

  void print(string format) {
    printf(("(" + format + ", " + format + ")\\n").c_str(), x, y);
  }
  void print() { print("%.6lf"); }
};

typedef Pt<double> pt;

struct Robot {
  pt p;
  int drc;
  Robot() {}
  Robot(pt _p, int _drc) : p(_p), drc(_drc) {}
  bool operator<(const Robot &r0) const { return p.x < r0.p.x; }
};

typedef set<Robot> srbt;

/* global variables */

int n;
pt v;
double r;
Robot rbts[MAX_N];

/* subroutines */

bool lessy(const Robot &r0, const Robot &r1) { return r0.p.y < r1.p.y; }

/* main */

int main() {
  for (;;) {
    cin >> n;
    if (n == 0)
      break;

    cin >> v.x >> v.y >> r;

    double th = -atan2(v.y, v.x);
    pt v0 = v.rot(th);
    // v0.print(); printf("th=%.6lf\\n", th);

    double rr = r * 2;
    double dvx = v0.x * 2;

    for (int i = 0; i < n; i++) {
      pt p;
      cin >> p.x >> p.y >> rbts[i].drc;
      rbts[i].p = p.rot(th);
    }
    sort(rbts, rbts + n, lessy);

    double min_t = INF;
    srbt srl, srr;

    for (int i0 = 0, i1 = 0; i1 < n; i1++) {
      Robot &r1 = rbts[i1];

      while (i0 < i1 && r1.p.y - rbts[i0].p.y > rr) {
        if (rbts[i0].drc > 0)
          srl.erase(rbts[i0]);
        else
          srr.erase(rbts[i0]);
        i0++;
      }

      if (r1.drc < 0) {
        if (!srl.empty()) {
          srbt::iterator rit = srl.lower_bound(r1);
          if (rit != srl.begin()) {
            rit--;
            pt dp = r1.p - rit->p;
            double t = (dp.x - sqrt(rr * rr - dp.y * dp.y)) / dvx;
            if (min_t > t)
              min_t = t;
          }
        }

        srr.insert(r1);
      } else {
        if (!srr.empty()) {
          srbt::iterator rit = srr.lower_bound(r1);
          if (rit != srr.end()) {
            pt dp = rit->p - r1.p;
            double t = (dp.x - sqrt(rr * rr - dp.y * dp.y)) / dvx;
            if (min_t > t)
              min_t = t;
          }
        }

        srl.insert(r1);
      }
    }

    if (min_t >= INF)
      cout << "SAFE" << endl;
    else
      printf("%.6lf\\n", min_t);
  }

  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <functional>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <string>
#include <utility>
#include <vector>

using namespace std;

/* constant */

const int MAX_N = 100000;

const double INF = 1e20;

/* typedef */

template <typename T> struct Pt {
  T x, y;

  Pt() {}
  Pt(T _x, T _y) : x(_x), y(_y) {}
  Pt(const Pt &pt) : x(pt.x), y(pt.y) {}

  bool operator==(const Pt pt) const { return x == pt.x && y == pt.y; }
  Pt operator+(const Pt pt) const { return Pt(x + pt.x, y + pt.y); }
  Pt operator-() const { return Pt(-x, -y); }
  Pt operator-(const Pt pt) const { return Pt(x - pt.x, y - pt.y); }
  Pt operator*(T t) const { return Pt(x * t, y * t); }
  Pt operator/(T t) const { return Pt(x / t, y / t); }
  T dot(Pt v) const { return x * v.x + y * v.y; }
  T cross(Pt v) const { return x * v.y - y * v.x; }
  Pt mid(const Pt pt) { return Pt((x + pt.x) / 2, (y + pt.y) / 2); }
  T d2() { return x * x + y * y; }
  double d() { return sqrt(d2()); }

  Pt rot(double th) {
    double c = cos(th), s = sin(th);
    return Pt(c * x - s * y, s * x + c * y);
  }

  // bool operator<(const Pt& pt) const {
  // return x < pt.x || (x == pt.x && y < pt.y);
  // }
  bool operator<(const Pt &pt) const { // ordered in y
    return y < pt.y || (y == pt.y && x < pt.x);
  }

  void print(string format) {
    printf(("(" + format + ", " + format + ")\\n").c_str(), x, y);
  }
  void print() { print("%.6lf"); }
};

typedef Pt<double> pt;

/* global variables */

int n, us[MAX_N];
double r;
pt v, cpts[MAX_N];

/* subroutines */

/* main */

int main() {
  for (;;) {
    cin >> n;
    if (n == 0)
      break;

    cin >> v.x >> v.y >> r;
    double r2 = r * 2;

    for (int i = 0; i < n; i++)
      cin >> cpts[i].x >> cpts[i].y >> us[i];

    double th = -atan2(v.y, v.x);
    pt v0 = v.rot(th);
    // v0.print();

    for (int i = 0; i < n; i++)
      cpts[i] = cpts[i].rot(th);
    sort(cpts, cpts + n);

    bool crash = false;
    double min_t = INF;

    for (int i0 = 0, i1 = 1; i1 < n; i1++) {
      pt &c1 = cpts[i1];
      for (int j = i0; j < i1; j++) {
        pt &cj = cpts[j];
        pt dc = c1 - cj;
        if (dc.y > r2) {
          i0 = j + 1;
          continue;
        }

        if (us[j] != us[i1] && dc.x * us[j] > 0.0) {
          double t = (dc.x * us[j] - sqrt(r2 * r2 - dc.y * dc.y)) / (2 * v0.x);
          if (min_t > t)
            min_t = t;
        }
      }
    }

    if (min_t >= INF)
      cout << "SAFE" << endl;
    else
      printf("%.6lf\\n", min_t);
  }

  return 0;
}