思路都是最难的,具体的代码很简答:
#include <iostream>
#include <algorithm>
#define db double
using namespace std;
const int maxn = 5000 + 10;
const db eps = 1e-11;
int n, w;
struct node {
db h, l, r;
bool operator < (const node &a) const {
return h > a.h;
}
}A[maxn];
bool ok() {
db l = 0, r = w, ll, rr;
for (int i = 1; i < n; i ++) {
if (A[0].r == A[i].l) {
ll = A[0].r;
} else {
ll = (A[0].r * A[i].h - A[i].l * A[0].h) / (A[i].h - A[0].h);
}
if (A[0].l == A[i].r) {
rr = A[0].l;
} else {
rr = (A[0].l * A[i].h - A[i].r * A[0].h) / (A[i].h - A[0].h);
}
l = max(l, ll);
r = min(r, rr);
}
return r >= l;
}
int main(){
freopen("a.in", "r", stdin);
freopen("a.out", "w", stdout);
int T; scanf("%d", &T);
while (T --) {
scanf("%d %d", &w, &n);
for (int i = 0; i < n; i ++)
scanf("%lf %lf %lf", &A[i].h, &A[i].l, &A[i].r);
sort(A, A + n);
if (ok()) printf("YES\n");
else printf("NO\n");
}
return 0;
}