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

#include <bits/stdc++.h>

using namespace std;
int m, n;
const double EPS = 1e-11;

double width(vector<pair<int, int>> polygon, double x) {
  vector<double> w;
  for (int i = 0; i < polygon.size(); i++) {
    pair<int, int> p = polygon[i];
    pair<int, int> q = polygon[(i + 1) % polygon.size()];
    if (abs(p.first - x) < EPS) {
      w.push_back(1.0 * p.second);
    } else if ((p.first < x && x < q.first) || (p.first > x && x > q.first)) {
      int x0 = p.first;
      int y0 = p.second;
      int x1 = q.first;
      int y1 = q.second;
      w.push_back(y0 + 1.0 * (y1 - y0) * (x - x0) / (x1 - x0));
    }
  }
  assert(w.size() > 0);
  sort(w.begin(), w.end());
  return w[w.size() - 1] - w[0];
}

double volume(vector<pair<int, int>> Pxy, vector<pair<int, int>> Pxz,
              vector<int> X) {
  double total = 0.0;
  int xmin = max((*min_element(Pxy.begin(), Pxy.end())).first,
                 (*min_element(Pxz.begin(), Pxz.end())).first);
  int xmax = min((*max_element(Pxy.begin(), Pxy.end())).first,
                 (*max_element(Pxz.begin(), Pxz.end())).first);
  sort(X.begin(), X.end());
  for (int i = 0; i < X.size() - 1; i++) {
    int a = X[i];
    int b = X[i + 1];
    if (!(xmin <= a && a <= xmax && xmin <= b && b <= xmax)) {
      continue;
    }
    double m = (a + b) / 2.0;
    double va = width(Pxy, a) * width(Pxz, a);
    double vb = width(Pxy, b) * width(Pxz, b);
    double vm = width(Pxy, m) * width(Pxz, m);
    double area = ((b - a) / 6.0) * (va + 4.0 * vm + vb);
    total += area;
  }
  return total;
}
int main() {
  while (cin >> m >> n && m != 0) {
    vector<pair<int, int>> Pxy, Pxz;
    pair<int, int> tem;
    vector<int> X;
    for (int i = 0; i < m; i++) {
      cin >> tem.first >> tem.second;
      Pxy.push_back(tem);
      X.push_back(tem.first);
    }
    for (int j = 0; j < n; j++) {
      cin >> tem.first >> tem.second;
      Pxz.push_back(tem);
      X.push_back(tem.first);
    }
    cout << fixed << volume(Pxy, Pxz, X) << endl;
  }
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>

using namespace std;
int m, n;
const double EPS = 1e-11;
vector<pair<int, int>> Pxy, Pxz;
pair<int, int> tem;
vector<int> X;

double width(vector<pair<int, int>> polygon, double x) {
  vector<double> w;
  for (int i = 0; i < polygon.size(); i++) {
    pair<int, int> p = polygon[i];
    pair<int, int> q = polygon[(i + 1) % polygon.size()];
    if (abs(p.first - x) < EPS) {
      w.push_back(1.0 * p.second);
    } else if ((p.first < x && x < q.first) || (p.first > 0 && x > q.first)) {
      int x0 = p.first;
      int y0 = p.second;
      int x1 = q.first;
      int y1 = q.second;
      w.push_back(y0 + 1.0 * (y1 - y0) * (x - x0) / (x1 - x0));
    }
  }
  assert(w.size() > 0);
  sort(w.begin(), w.end());
  return w[w.size() - 1] - w[0];
}

int volume(vector<pair<int, int>> Pxy, vector<pair<int, int>> Pxz,
           vector<int> X) {
  double total = 0.0;
  int xmin = max((*min_element(Pxy.begin(), Pxy.end())).first,
                 (*min_element(Pxz.begin(), Pxz.end())).first);
  int xmax = min((*max_element(Pxy.begin(), Pxy.end())).first,
                 (*max_element(Pxz.begin(), Pxz.end())).first);
  sort(X.begin(), X.end());
  for (int i = 0; i < X.size() - 1; i++) {
    int a = X[i];
    int b = X[i + 1];
    if (!(xmin <= a && a <= xmax && xmin <= b && b <= xmax)) {
      continue;
    }
    double m = (a + b) / 2.0;
    double va = width(Pxy, a) * width(Pxz, a);
    double vb = width(Pxy, b) * width(Pxz, b);
    double vm = width(Pxy, m) * width(Pxz, m);
    double area = ((b - a) / 6.0) * (va + 4.0 * vm + vb);
    total += area;
  }
  return total;
}
int main() {
  while (cin >> m >> n && n != 0) {
    for (int i = 1; i <= m; i++) {
      cin >> tem.first >> tem.second;
      Pxy.push_back(tem);
      X.push_back(tem.first);
    }
    for (int j = 1; j <= n; j++) {
      cin >> tem.first >> tem.second;
      Pxz.push_back(tem);
      X.push_back(tem.first);
    }
    cout << fixed << volume(Pxy, Pxz, X) << endl;
  }
}