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

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <iostream>
#include <map>
#include <queue>
#include <stack>
#include <string>
#include <vector>

using namespace std;

#define REP(i, k, n) for (int i = k; i < n; i++)
#define rep(i, n) for (int i = 0; i < n; i++)

#define all all(v) v.begin(), v.end()
#define INF i << 30
#define mp make_pair
#define pb push_back

#define fi first
#define se second

#define each(it, c) for (auto it = (c).begin(); it != (c).end(); it++)

typedef vector<int> vi;
typedef vector<vi> vvi;
typedef pair<int, int> pii;
typedef vector<pair<int, int>> vp;

typedef long long ll;

#define EPS (1e-10)

class Point {
public:
  double x, y;

  Point(double x = 0, double y = 0) : x(x), y(y) {}

  Point operator+(Point P) { return Point(x + P.x, y + P.y); }
  Point operator-(Point P) { return Point(x - P.x, y - P.y); }
  Point operator*(double a) { return Point(a * x, a * y); }
  Point operator/(double a) { return Point(x / a, y / a); }

  double abs_() { return sqrt(norm()); }
  double norm() { return x * x + y * y; }

  bool operator<(const Point &p) const { return x != p.x ? x < p.x : y < p.y; }

  bool operator==(const Point &p) const {
    return fabs(x - p.x) < EPS && fabs(y - p.y) < EPS;
  }
};

double dot(Point a, Point b) { return a.x * b.x + a.y * b.y; }

double cross(Point a, Point b) { return a.x * b.y - a.y * b.x; }

int ccw(Point p0, Point p1, Point p2) {
  Point a = p1 - p0;
  Point b = p2 - p0;
  if (cross(a, b) > EPS)
    return 1;
  if (cross(a, b) < -EPS)
    return -1;
  if (dot(a, b) < -EPS)
    return 2;
  if (a.norm() < b.norm())
    return -2;

  return 0;
}

typedef vector<Point> Polygon;

Polygon andrewScan(Polygon s) {
  Polygon u;
  if (s.size() < 3)
    return s;
  sort(s.begin(), s.end());

  u.pb(s[0]);
  u.pb(s[1]);

  for (int i = 2; i < s.size(); i++) {
    for (int n = u.size(); n >= 2 && ccw(u[n - 2], u[n - 1], s[i]) != -1; n--) {
      u.pop_back();
    }
    u.pb(s[i]);
  }

  return u;
}

int main() {
  while (1) {
    int r;
    int n;
    Polygon pv;
    cin >> r >> n;
    if (r == 0)
      break;

    map<int, pii> mv;

    rep(i, n) {
      int xr, xl, h;
      cin >> xl >> xr >> h;
      mv[xl].se = max(h, mv[xl].se);
      for (int nx = xl + 1; nx < xr; nx++) {
        mv[nx].fi = max(h, mv[nx].fi);
        mv[nx].se = max(h, mv[nx].se);
      }
      mv[xr].fi = max(h, mv[xr].fi);
      /*
      pv.pb(Point(xl,0));
      pv.pb(Point(xr,0));
      pv.pb(Point(xl,h));
      pv.pb(Point(xr,h));
      if(xl < 0 && 0 < xr){
              pv.pb(Point(0,h));
      }
      */
    }
    mv[-r].fi = mv[-r].se;
    mv[r].se = mv[r].fi;

    // cout<<"======="<<endl;
    for (int i = -20; i <= 20; i++) {

      // cout << min(mv[i].fi,mv[i].se) << endl;
      pv.pb(Point(i, min(mv[i].fi, mv[i].se)));
    }
    // cout<<"======="<<endl;

    /*
                    for(int i=-20;i<21;i++){
                            pv.pb(Point(i,min(mv[i],mv[i+1])));
                    }

                    cout<<"-----------"<<endl;
                    rep(i,pv.size()){
                            if(pv[i].y!=23)cout<<pv[i].x<<" "<<pv[i].y<<endl;
                    }
                    cout<<"--------"<<endl;
    */

    for (double t = 0; t <= 20.1; t += 0.00005) {
      Point c = Point(0, -r + t);
      rep(i, pv.size()) {
        if ((c - pv[i]).abs_() <= r) {
          // cout<<(c-pv[i]).abs_()<<endl;
          printf("%.4lf\\n", t);
          goto END;
        }
      }
    }
  END:;
    /*
    // Polygon pt;
    // pt = andrewScan(pv);
    // rep(i,pv.size()){
    // 	cout << "pv :" << pv[i].x << " "<< pv[i].y << endl;
    // }

    rep(i,pt.size()){
            cout << "pt :" << pt[i].x << " "<< pt[i].y << endl;
    }
*/
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <iostream>
#include <map>
#include <queue>
#include <stack>
#include <string>
#include <vector>

using namespace std;

#define REP(i, k, n) for (int i = k; i < n; i++)
#define rep(i, n) for (int i = 0; i < n; i++)

#define all all(v) v.begin(), v.end()
#define INF i << 30
#define mp make_pair
#define pb push_back

#define fi first
#define se second

#define each(it, c) for (auto it = (c).begin(); it != (c).end(); it++)

typedef vector<int> vi;
typedef vector<vi> vvi;
typedef pair<int, int> pii;
typedef vector<pair<int, int>> vp;

typedef long long ll;

#define EPS (1e-10)

class Point {
public:
  double x, y;

  Point(double x = 0, double y = 0) : x(x), y(y) {}

  Point operator+(Point P) { return Point(x + P.x, y + P.y); }
  Point operator-(Point P) { return Point(x - P.x, y - P.y); }
  Point operator*(double a) { return Point(a * x, a * y); }
  Point operator/(double a) { return Point(x / a, y / a); }

  double abs_() { return sqrt(norm()); }
  double norm() { return x * x + y * y; }

  bool operator<(const Point &p) const { return x != p.x ? x < p.x : y < p.y; }

  bool operator==(const Point &p) const {
    return fabs(x - p.x) < EPS && fabs(y - p.y) < EPS;
  }
};

double dot(Point a, Point b) { return a.x * b.x + a.y * b.y; }

double cross(Point a, Point b) { return a.x * b.y - a.y * b.x; }

int ccw(Point p0, Point p1, Point p2) {
  Point a = p1 - p0;
  Point b = p2 - p0;
  if (cross(a, b) > EPS)
    return 1;
  if (cross(a, b) < -EPS)
    return -1;
  if (dot(a, b) < -EPS)
    return 2;
  if (a.norm() < b.norm())
    return -2;

  return 0;
}

typedef vector<Point> Polygon;

Polygon andrewScan(Polygon s) {
  Polygon u;
  if (s.size() < 3)
    return s;
  sort(s.begin(), s.end());

  u.pb(s[0]);
  u.pb(s[1]);

  for (int i = 2; i < s.size(); i++) {
    for (int n = u.size(); n >= 2 && ccw(u[n - 2], u[n - 1], s[i]) != -1; n--) {
      u.pop_back();
    }
    u.pb(s[i]);
  }

  return u;
}

int main() {
  while (1) {
    int r;
    int n;
    Polygon pv;
    cin >> r >> n;
    if (r == 0)
      break;

    map<int, pii> mv;

    rep(i, n) {
      int xr, xl, h;
      cin >> xl >> xr >> h;
      mv[xl].se = max(h, mv[xl].se);
      for (int nx = xl + 1; nx < xr; nx++) {
        mv[nx].fi = max(h, mv[nx].fi);
        mv[nx].se = max(h, mv[nx].se);
      }
      mv[xr].fi = max(h, mv[xr].fi);
      /*
      pv.pb(Point(xl,0));
      pv.pb(Point(xr,0));
      pv.pb(Point(xl,h));
      pv.pb(Point(xr,h));
      if(xl < 0 && 0 < xr){
              pv.pb(Point(0,h));
      }
      */
    }
    mv[-r - 1].fi = mv[-r - 1].se;
    mv[r + 1].se = mv[r + 1].fi;

    // cout<<"======="<<endl;
    for (int i = -20; i <= 20; i++) {

      // cout << min(mv[i].fi,mv[i].se) << endl;
      pv.pb(Point(i, min(mv[i].fi, mv[i].se)));
    }
    // cout<<"======="<<endl;

    /*
                    for(int i=-20;i<21;i++){
                            pv.pb(Point(i,min(mv[i],mv[i+1])));
                    }

                    cout<<"-----------"<<endl;
                    rep(i,pv.size()){
                            if(pv[i].y!=23)cout<<pv[i].x<<" "<<pv[i].y<<endl;
                    }
                    cout<<"--------"<<endl;
    */

    for (double t = 0; t <= 20; t += 0.00001) {
      Point c = Point(0, -r + t);
      rep(i, pv.size()) {
        if ((c - pv[i]).abs_() <= r) {
          // cout<<(c-pv[i]).abs_()<<endl;
          printf("%lf\\n", t);
          goto END;
        }
      }
    }
  END:;
    /*
    // Polygon pt;
    // pt = andrewScan(pv);
    // rep(i,pv.size()){
    // 	cout << "pv :" << pv[i].x << " "<< pv[i].y << endl;
    // }

    rep(i,pt.size()){
            cout << "pt :" << pt[i].x << " "<< pt[i].y << endl;
    }
*/
  }
  return 0;
}