Problem p00892 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <cstdio>
#include <utility>
#include <vector>
using namespace std;

double width(const pair<int, int> P[], double x, int length) {
  vector<double> w(0);
  for (int i = 0; i < length; i++) {
    pair<int, int> p, q;
    p = P[i];
    i != length - 1 ? q = P[i + 1] : q = P[0];
    if (p.first == x) {
      w.push_back(p.second);
    } else if ((p.first < x && x < q.first) || (p.first > x && x > q.first)) {
      int x0, y0, x1, y1;
      if (p.first < x) {
        x0 = p.first;
        y0 = p.second;
        x1 = q.first;
        y1 = q.second;
      } else {
        x0 = q.first;
        y0 = q.second;
        x1 = p.first;
        y1 = p.second;
      }
      w.push_back(y0 + 1.0 * (y1 - y0) * (x - x0) / (x1 - x0));
    }
  }
  sort(w.begin(), w.end());
  return w.front() - w.back();
}

// P1??¨P2????????????????????????????????????x????????????sort?????????x???????????????????¬???¢??°?????????????????????
double volume(const pair<int, int> P1[], const pair<int, int> P2[],
              pair<int, int> X[], int m, int n) {
  /*
  for(int i = 0; i < m+n; i++){
      if(i < m){
          printf("P1[%d]:%d %d\\n",i,P1[i].first,P1[i].second);
      }else{
          printf("P2[%d]:%d %d\\n",i-m,P2[i-m].first,P2[i-m].second);
      }
  }
   */
  sort(X, X + m + n);
  pair<int, int> p1mm(P1[0].first, P1[0].first), p2mm(P2[0].first, P2[0].first);
  for (int i = 0; i < m; i++) {
    if (p1mm.first > P1[i].first) {
      p1mm.first = P1[i].first;
    }
    if (p1mm.second < P1[i].first) {
      p1mm.second = P1[i].first;
    }
  }
  for (int i = 0; i < n; i++) {
    if (p2mm.first > P2[i].first) {
      p2mm.first = P2[i].first;
    }
    if (p2mm.second < P2[i].first) {
      p2mm.second = P2[i].first;
    }
  }
  /*
  printf("p1mm: %d %d\\np2mm: %d
  %d\\n",p1mm.first,p1mm.second,p2mm.first,p2mm.second); for(int i = 0; i < m+n;
  i++){ if(i < m){ printf("P1[%d]:%d %d\\n",i,P1[i].first,P1[i].second); }else{
          printf("P2[%d]:%d %d\\n",i-m,P2[i-m].first,P2[i-m].second);
      }
  }*/
  int xmin = max(p1mm.first, p2mm.first), xmax = min(p1mm.second, p2mm.second);
  // printf("minmax: %d %d\\n",xmin,xmax);
  double sum = 0;
  for (int i = 0; i < m + n - 1; i++) {
    pair<int, int> a = X[i], b = X[i + 1];
    if (xmin <= a.first && xmax >= a.first && xmin <= b.first &&
        xmax >= b.first) {
      if (a.first == b.first) {
        continue;
      }
      double mid = (a.first + b.first) / 2.0;
      double va = width(P1, a.first, m) * width(P2, a.first, n);
      double vb = width(P1, b.first, m) * width(P2, b.first, n);
      double vmid = width(P1, mid, m) * width(P2, mid, n);
      double area = (b.first - a.first) / 6.0 * (va + 4 * vmid + vb);
      // printf("a,b %d %d\\nmid %f\\nva %f\\nvb %f\\nvmid %f\\narea
      // %f\\n",a.first,b.first,mid,va,vb,vmid,area);
      sum += area;
    }
  }
  return sum;
}

int main() {
  while (true) {
    int m, n;
    scanf("%d %d", &m, &n);
    if (m == 0 && n == 0) {
      break;
    }
    pair<int, int> *P1 = new pair<int, int>[m], *P2 = new pair<int, int>[n],
                   *X = new pair<int, int>[m + n];
    for (int i = 0; i < m + n; i++) {
      int x, y;
      scanf("%d %d", &x, &y);
      if (i < m) {
        P1[i] = make_pair(x, y);
      } else {
        P2[i - m] = make_pair(x, y);
      }
      X[i] = make_pair(x, y);
    }
    double vol = volume(P1, P2, X, m, n);
    printf("%f\\n", vol);
    delete[] P1;
    delete[] P2;
    delete[] X;
  }
}

Error: WA

Fixed submission:

#include <algorithm>
#include <cstdio>
#include <utility>
#include <vector>
using namespace std;

double width(const pair<int, int> P[], double x, int length) {
  vector<double> w(0);
  for (int i = 0; i < length; i++) {
    pair<int, int> p, q;
    p = P[i];
    i != length - 1 ? q = P[i + 1] : q = P[0];
    if (p.first == x) {
      w.push_back(p.second);
    } else if ((p.first < x && x < q.first) || (p.first > x && x > q.first)) {
      int x0, y0, x1, y1;
      if (p.first < x) {
        x0 = p.first;
        y0 = p.second;
        x1 = q.first;
        y1 = q.second;
      } else {
        x0 = q.first;
        y0 = q.second;
        x1 = p.first;
        y1 = p.second;
      }
      w.push_back(y0 + 1.0 * (y1 - y0) * (x - x0) / (x1 - x0));
    }
  }
  sort(w.begin(), w.end());
  return w.front() - w.back();
}

// P1??¨P2????????????????????????????????????x????????????sort?????????x???????????????????¬???¢??°?????????????????????
double volume(const pair<int, int> P1[], const pair<int, int> P2[],
              pair<int, int> X[], int m, int n) {
  /*
  for(int i = 0; i < m+n; i++){
      if(i < m){
          printf("P1[%d]:%d %d\\n",i,P1[i].first,P1[i].second);
      }else{
          printf("P2[%d]:%d %d\\n",i-m,P2[i-m].first,P2[i-m].second);
      }
  }
   */
  sort(X, X + m + n);
  pair<int, int> p1mm = P1[0], p2mm = P2[0];
  for (int i = 0; i < m; i++) {
    if (p1mm.first > P1[i].first) {
      p1mm.first = P1[i].first;
    }
    if (p1mm.second < P1[i].first) {
      p1mm.second = P1[i].first;
    }
  }
  for (int i = 0; i < n; i++) {
    if (p2mm.first > P2[i].first) {
      p2mm.first = P2[i].first;
    }
    if (p2mm.second < P2[i].first) {
      p2mm.second = P2[i].first;
    }
  }
  /*
  printf("p1mm: %d %d\\np2mm: %d
  %d\\n",p1mm.first,p1mm.second,p2mm.first,p2mm.second); for(int i = 0; i < m+n;
  i++){ if(i < m){ printf("P1[%d]:%d %d\\n",i,P1[i].first,P1[i].second); }else{
          printf("P2[%d]:%d %d\\n",i-m,P2[i-m].first,P2[i-m].second);
      }
  }*/
  int xmin = max(p1mm.first, p2mm.first), xmax = min(p1mm.second, p2mm.second);
  // printf("minmax: %d %d\\n",xmin,xmax);
  double sum = 0;
  for (int i = 0; i < m + n - 1; i++) {
    pair<int, int> a = X[i], b = X[i + 1];
    if (xmin <= a.first && xmax >= a.first && xmin <= b.first &&
        xmax >= b.first) {
      if (a.first == b.first) {
        continue;
      }
      double mid = (a.first + b.first) / 2.0;
      double va = width(P1, a.first, m) * width(P2, a.first, n);
      double vb = width(P1, b.first, m) * width(P2, b.first, n);
      double vmid = width(P1, mid, m) * width(P2, mid, n);
      double area = (b.first - a.first) / 6.0 * (va + 4 * vmid + vb);
      // printf("a,b %d %d\\nmid %f\\nva %f\\nvb %f\\nvmid %f\\narea
      // %f\\n",a.first,b.first,mid,va,vb,vmid,area);
      sum += area;
    }
  }
  return sum;
}

int main() {
  while (true) {
    int m, n;
    scanf("%d %d", &m, &n);
    if (m == 0 && n == 0) {
      break;
    }
    pair<int, int> *P1 = new pair<int, int>[m], *P2 = new pair<int, int>[n],
                   *X = new pair<int, int>[m + n];
    for (int i = 0; i < m + n; i++) {
      int x, y;
      scanf("%d %d", &x, &y);
      if (i < m) {
        P1[i] = make_pair(x, y);
      } else {
        P2[i - m] = make_pair(x, y);
      }
      X[i] = make_pair(x, y);
    }
    double vol = volume(P1, P2, X, m, n);
    printf("%f\\n", vol);
    delete[] P1;
    delete[] P2;
    delete[] X;
  }
}