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

#define _USE_MATH_DEFINES
#include <algorithm>
#include <bitset>
#include <cmath>
#include <deque>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;

int bs(double x, double y, long l[], long r[], long w, int m) {

  int lower = 0;
  int upper = m + 1;

  while (true) {
    int t = (lower + upper) / 2;

    if (y < x * (r[t] - l[t]) / (w + 0.0) + l[t]) {
      if (y > x * (r[t - 1] - l[t - 1]) / (w + 0.0) + l[t - 1]) {
        return t - 1;
      } else {
        upper = t;
      }
    } else {
      lower = t;
    }
  }
}

int main() {

  int n, m;
  long w, h, S;
  while (cin >> n >> m >> w >> h >> S && (n || m || w || h || S)) {
    S *= 2;
    long l[m + 1];
    long r[m + 1];
    l[0] = 0;
    r[0] = 0;
    for (int i = 1; i <= m; i++) {
      cin >> l[i] >> r[i];
    }
    double x[n];
    double y[n];
    for (int i = 0; i < n; i++) {
      cin >> x[i] >> y[i];
    }

    long fromd_area[m + 1];
    long fromu_area[m + 1];
    fromd_area[0] = 0;
    fromu_area[0] = 0;
    for (int i = 1; i <= m; i++) {
      fromd_area[i] =
          fromd_area[i - 1] + (l[i] - l[i - 1] + r[i] - r[i - 1]) * w;
      fromu_area[i] = fromu_area[i - 1] +
                      (l[m + 1 - i] - l[m - i] + r[m + 1 - i] - r[m - i]) * w;
    }

    int nuts[m];
    fill(nuts, nuts + m, 0);
    for (int i = 0; i < n; i++) {
      nuts[bs(x[i], y[i], l, r, w, m)] += 1;
    }

    int fromd_nuts[m + 1];
    int fromu_nuts[m + 1];
    fromd_nuts[0] = 0;
    fromu_nuts[0] = 0;
    for (int i = 1; i <= m; i++) {
      fromd_nuts[i] = fromd_nuts[i - 1] + nuts[i - 1];
      fromu_nuts[i] = fromu_nuts[i - 1] + nuts[m - i];
    }

    int minnuts = n + 1;
    for (int i = 0; i < m; i++) {
      int j = lower_bound(fromu_area, fromu_area + m + 1, S - fromd_area[i]) -
              fromu_area;
      if (minnuts > fromd_nuts[i] + fromu_nuts[j]) {
        minnuts = fromd_nuts[i] + fromu_nuts[j];
      }
    }

    cout << minnuts << endl;
  }

  return 0;
}

Error: TLE

Fixed submission:

#define _USE_MATH_DEFINES
#include <algorithm>
#include <bitset>
#include <cmath>
#include <deque>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;

int main() {

  int n, m, w, h, S;
  while (cin >> n >> m >> w >> h >> S && (n || m || w || h || S)) {
    S *= 2;
    int l[m + 1];
    int r[m + 1];
    l[0] = 0;
    r[0] = 0;
    for (int i = 1; i <= m; i++) {
      cin >> l[i] >> r[i];
    }
    double x[n];
    double y[n];
    for (int i = 0; i < n; i++) {
      cin >> x[i] >> y[i];
    }

    int fromd_area[m + 1];
    int fromu_area[m + 1];
    fromd_area[0] = 0;
    fromu_area[0] = 0;
    for (int i = 1; i <= m; i++) {
      fromd_area[i] =
          fromd_area[i - 1] + (l[i] - l[i - 1] + r[i] - r[i - 1]) * w;
      fromu_area[i] = fromu_area[i - 1] +
                      (l[m + 1 - i] - l[m - i] + r[m + 1 - i] - r[m - i]) * w;
    }

    int nuts[m];
    for (int i = 0; i < n; i++) {
      int j;
      for (j = 0; j <= lower_bound(l, l + n, y[i]) - l; j++) {
        if (y[i] <= x[i] * (r[j] - l[j]) / (w + 0.0) + l[j])
          break;
      }
      nuts[j - 1] += 1;
    }

    deque<int> from_down_nuts;
    deque<int> from_up_nuts;
    from_up_nuts.push_back(0);
    from_down_nuts.push_back(0);
    for (int i = 1; i <= m; i++) {
      from_down_nuts.push_back(nuts[i - 1] + from_down_nuts[i - 1]);
      from_up_nuts.push_back(nuts[m - i] + from_up_nuts[i - 1]);
    }

    int minnuts = n + 1;
    for (int i = 0; i < m; i++) {
      if (nuts[i] == 0)
        continue;
      int j = lower_bound(fromu_area, fromu_area + m, S - fromd_area[i]) -
              fromu_area;
      if (minnuts > from_down_nuts[i] + from_up_nuts[j]) {
        minnuts = from_down_nuts[i] + from_up_nuts[j];
      }
      if (from_down_nuts[i] > minnuts)
        break;
    }

    cout << minnuts << endl;
  }

  return 0;
}