Two times, the first time to get the minimum value, the second to the right two to see if you can go to the right
Note that part of the land must be removed.
#include <cstdio> #include <algorithm>using namespace std;typedef long long ll;struct edge{int x, y, W, H;} A[10010];bool CMP (Edge A, Edge B) {return a.x < b.x;} int N;LL cal (int x) {ll S = 0; for (int i = 1; I <= n; i++) {if (a[i].x < x) {S + = 1ll * (min (x, a[i].x + a[i].w)-a[i].x) * min (A [I].h, A[I].Y]; }} return S;} int main () {int T, R; scanf ("%d", &t); while (t--) {int L, r, Mid; scanf ("%d", &r); scanf ("%d", &n); ll Sum = 0; Max_r = R; for (int i = 1; I <= n; i++) {scanf ("%d%d%d%d", &a[i].x, &a[i].y, &A[I].W, &a[i].h); Sum + = 1ll * min (a[i].h, a[i].y) * min (A[I].W, r-a[i].x); } sort (A + 1, a + n + 1, CMP); L = 0; R = r; ll S, _s; ll f_s = 1e13; int ans = R; while (L <= r) {mid = L + R >> 1; S = Cal (mid); _s = S-(Sum- S); printf ("%d%lld\n", Mid, _s); if (_s >= 0) {if (_s <= f_s) {//printf ("%d%d", L, R); R = mid-1; f_s = _s; ans = min (ans, mid); printf ("%d\n", ans); } else L = mid + 1; } else L = mid + 1; } R = r; L = ans; while (L <= r) {mid = L + R >> 1; S = Cal (mid); _s = S-(sum-s); if (_s < f_s) L = mid + 1; if (_s = = f_s) {ans = max (ans, mid); L = mid + 1; } else R = mid-1; } printf ("%d\n", ans); } return 0;}
15 Beijing regional Race--a--hihocoder 1249 Xiongnu ' s land