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

#include <cstdio>
#include <cstring>
#include <iostream>
using namespace std;

const int MAX_N = 30001;

int L[MAX_N], R[MAX_N], sum[MAX_N];
int n, m, w, h, s;

int bin_search_line(double x, double y) {
  int l = 0, r = m, line = 0;
  while (l <= r) {
    int mid = (l + r) >> 1;
    if ((y - L[mid]) * w > x * (R[mid] - L[mid])) {
      l = mid + 1;
    } else
      r = mid - 1, line = mid;
  }
  return line;
}

int bin_search_level(int end) {
  int l = 0, r = end, level = -1;
  while (l <= r) {
    int mid = (l + r) >> 1;
    if ((L[end] - L[mid] + R[end] - R[mid]) * w > 2 * (w * h - s)) {
      l = mid + 1;
    } else
      r = mid - 1, level = mid;
  }
  return level;
}

void solve() {
  L[0] = R[0] = 0.;
  fill(sum, sum + m + 1, 0);
  for (int i = 1; i <= m; ++i) {
    scanf("%d%d", &L[i], &R[i]);
  }
  double x, y;
  for (int i = 0; i < n; ++i) {
    scanf("%lf%lf", &x, &y);
    ++sum[bin_search_line(x, y)];
  }
  for (int i = 1; i <= m; ++i) {
    sum[i] += sum[i - 1];
  }
  int res = n;
  for (int i = 0; i <= m; i++) {
    int index = bin_search_level(i);
    if (-1 == index)
      continue;
    res = min(res, n - (sum[i] - sum[index]));
  }
  printf("%d\\n", res);
}

int main() {
  while (5 == scanf("%d%d%d%d%d", &n, &m, &w, &h, &s)) {
    if (0 == n)
      break;
    solve();
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <math.h>
#include <queue>
#include <stack>
#include <stdio.h>
#include <string.h>
using namespace std;
const double eps = 1e-8;
const double inf = 1e99;
double sqr(double x) { return x * x; }
int sgn(double x) { return (x > eps) - (x < -eps); }
struct Point {
  double x, y;
  Point() {}
  Point(double _x, double _y) : x(_x), y(_y) {}
  bool operator==(const Point &p) const {
    return (sgn(p.x - x) == 0 && sgn(p.y - y) == 0);
  }
  bool operator<(const Point &p) const {
    return y + eps < p.y || (y < p.y + eps && x + eps < p.x);
  }
  double operator*(const Point &p) const { return x * p.y - y * p.x; }
  double operator/(const Point &p) const { // 轤ケ遘ッ
    return x * p.x + y * p.y;
  }
  Point operator-(const Point &p) const { return Point(x - p.x, y - p.y); }
  Point operator+(const Point &p) const { return Point(x + p.x, y + p.y); }
  Point operator*(const double &k) const { return Point(x * k, y * k); }
  Point operator/(const double &k) const { return Point(x / k, y / k); }
  void input() { scanf("%lf%lf", &x, &y); }
  void output() { printf("(%.3f %.3f)\\n", x, y); }
};
struct Line {
  Point a, b;
  Line() {}
  Line(Point _a, Point _b) : a(_a), b(_b) {}
  Line(double _x1, double _y1, double _x2, double _y2) {
    a = Point(_x1, _y1), b = Point(_x2, _y2);
  }
  double operator*(const Point &p) const { return (b - a) * (p - a); }
  void output() { printf("(%.3f %.3f)-(%.3f %.3f)\\n", a.x, a.y, b.x, b.y); }
  int Direction(const Point &p) const { // 蟾ヲ1?悟承-1?檎コソ荳?
    return sgn((*this) * p);
  }
};
double lp[32000], rp[32000];
Point pt[32000];
int sz[32000];
double area[32000], sp[32000];
int st[32000];
Line ls[32000];
int main() {
  int n, m;
  double w, h, s;
  while (scanf("%d%d%lf%lf%lf", &n, &m, &w, &h, &s)) {
    if (n == 0 && m == 0 && sgn(w) == 0 && sgn(h) == 0 && sgn(s) == 0)
      break;
    for (int i = 0; i < n; i++) {
      scanf("%lf%lf", &lp[i], &rp[i]);
    }
    sort(lp, lp + n), sort(rp, rp + n);
    for (int i = 0; i < m; i++)
      pt[i].input();
    if (sgn(s - w * h) < 0)
      while (1)
        ;
    memset(sz, 0, sizeof(sz));
    int cnt = 0;
    //	printf("&&&&&%f %f\\n",lp[n-1],rp[n-1]);
    if (sgn(h - lp[n - 1]) || sgn(h - rp[n - 1]))
      ls[cnt++] = Line(Point(0, h), Point(w, h));
    for (int i = n - 1; i >= 0; i--) {
      ls[cnt++] = Line(Point(0, lp[i]), Point(w, rp[i]));
    }
    ls[cnt++] = Line(Point(0, 0), Point(w, 0));
    //	printf("~~~%d\\n",cnt);
    for (int i = 0; i < m; i++) {
      int l = 0, r = cnt - 1, md, res = -1;
      while (l <= r) {
        md = (l + r) >> 1;
        if (ls[md].Direction(pt[i]) == -1) {
          l = md + 1;
          res = md;
        } else
          r = md - 1;
      }
      sz[res]++;
    }
    double pl = h, pr = h;
    for (int i = 0; i <= n; i++) {
      if (i == n) {
        area[i] = (fabs(pl) + fabs(pr)) * w * 0.5;
      } else {
        area[i] =
            (fabs(lp[n - i - 1] - pl) + fabs(rp[n - i - 1] - pr)) * w * 0.5;
        pl = lp[n - i - 1], pr = rp[n - i - 1];
      }
    }
    // for(int i=0;i<=n;i++) printf("%d\\n",sz[i]);
    sp[n] = area[n];
    st[n] = sz[n];
    sz[n + 1] = 0;
    sp[n + 1] = 0;
    for (int i = n - 1; i >= 0; i--)
      sp[i] = area[i] + sp[i + 1], st[i] = sz[i] + st[i + 1];
    int mi = m, rb = 0, has = 0;

    while (rb <= n && sgn(sp[rb + 1] - s) >= 0)
      rb++;
    if (sgn(sp[rb] - s) >= 0)
      mi = min(mi, st[rb]);
    for (int i = 0; i <= n; i++) {
      s -= area[i];
      has += sz[i];
      while (rb <= n && sgn(sp[rb + 1] - s) >= 0)
        rb++;
      if (sgn(sp[rb] - s) >= 0)
        mi = min(has + st[rb], mi);
    }
    printf("-------------------------------%d\\n", mi);
  }
  return 0;
}