// Cake-Cutting Committee // Solution by Jacob Plachta #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include using namespace std; #define LL long long #define LD long double #define PR pair #define Fox(i,n) for (i=0; i=0; i--) #define FoxR1(i,n) for (i=n; i>0; i--) #define FoxRI(i,a,b) for (i=b; i>=a; i--) #define Foxen(i,s) for (auto i:s) #define Min(a,b) a=min(a,b) #define Max(a,b) a=max(a,b) #define Sz(s) int((s).size()) #define All(s) (s).begin(),(s).end() #define Fill(s,v) memset(s,v,sizeof(s)) #define pb push_back #define mp make_pair #define x first #define y second template T Abs(T x) { return(x < 0 ? -x : x); } template T Sqr(T x) { return(x * x); } string plural(string s) { return(Sz(s) && s[Sz(s) - 1] == 'x' ? s + "en" : s + "s"); } const int INF = (int)1e9; const LD EPS = 1e-12; const LD PI = acos(-1.0); #define GETCHAR getchar_unlocked bool Read(int& x) { char c, r = 0, n = 0; x = 0; for (;;) { c = GETCHAR(); if ((c < 0) && (!r)) return(0); if ((c == '-') && (!r)) n = 1; else if ((c >= '0') && (c <= '9')) x = x * 10 + c - '0', r = 1; else if (r) break; } if (n) x = -x; return(1); } #define LIM 800 #define TVAL int #define TLAZY int #define TLIM 4100 TVAL ZERO_VAL = 0; TLAZY ZERO_LAZY = 0; struct SegTree { void UpdateValForUpdateOrLazy(TVAL& a, TLAZY v) { a += v; } void UpdateLazyForUpdateOrLazy(TLAZY& a, TLAZY v) { a += v; } TVAL CombVals(TVAL v1, TVAL v2) { return(max(v1, v2)); } int N, sz; TVAL tree[TLIM]; TLAZY lazy[TLIM]; SegTree() {} SegTree(int _N) { Init(_N); } void Init(int _N) { N = _N; for (sz = 1; sz < N; sz <<= 1); Clear(); } void Clear() { int i; Fox(i, sz << 1) tree[i] = ZERO_VAL; Fox(i, sz << 1) lazy[i] = ZERO_LAZY; } void Prop(int i) { TLAZY v = lazy[i]; lazy[i] = ZERO_LAZY; UpdateValForUpdateOrLazy(tree[i], v); if (i < sz) { int c1 = i << 1, c2 = c1 + 1; UpdateLazyForUpdateOrLazy(lazy[c1], v); UpdateLazyForUpdateOrLazy(lazy[c2], v); } } void Comp(int i) { int c1 = i << 1, c2 = c1 + 1; tree[i] = CombVals(tree[c1], tree[c2]); } TVAL Query( int a, int b, int i = 1, int r1 = 0, int r2 = -1 ) { if (r2 < 0) { Max(a, 0); Min(b, sz - 1); if (a > b) return ZERO_VAL; r2 = sz - 1; } Prop(i); if (a <= r1 && r2 <= b) return(tree[i]); int m = (r1 + r2) >> 1, c = i << 1; TVAL ret = ZERO_VAL; if (a <= m) ret = CombVals(ret, Query(a, b, c, r1, m)); if (b > m) ret = CombVals(ret, Query(a, b, c + 1, m + 1, r2)); return(ret); } void Update( int a, int b, TLAZY v, int i = 1, int r1 = 0, int r2 = -1 ) { if (r2 < 0) { Max(a, 0); Min(b, sz - 1); if (a > b) return; r2 = sz - 1; } Prop(i); if (a <= r1 && r2 <= b) { UpdateLazyForUpdateOrLazy(lazy[i], v); Prop(i); return; } int m = (r1 + r2) >> 1, c = i << 1; if (a <= m) Update(a, b, v, c, r1, m); if (b > m) Update(a, b, v, c + 1, m + 1, r2); Prop(c), Prop(c + 1), Comp(i); } }; struct Event { int x, y1, y2, c; bool s; }; const bool operator<(const Event& a, const Event& b) { return(mp(a.x, !a.s) < mp(b.x, !b.s)); } int S, N; int C[LIM], P[LIM][4]; bool IsBetween(int a, int b, int p, bool ex) { if (b < a) b += INF; if (p < a) p += INF; return ex ? a < p && p < b : a <= p && p <= b; } int GetPosAfter(int a, int p) { if (p < a) p += INF; return(p - a); } int SolveForLine(vector h) { int i, j, s; // compare all pieces against dividing line, and assemble line sweep events int base = 0; vector CY; vector E; Fox(i, N) { // full intersection? if ( (IsBetween(P[i][0], P[i][2], h[0], 0) || IsBetween(P[i][3], P[i][1], h[0], 0)) && (IsBetween(P[i][0], P[i][2], h[1], 0) || IsBetween(P[i][3], P[i][1], h[1], 0)) ) { base += C[i]; continue; } // look for orientation of line segments such that at least one spans crosses from the 1st to the 2nd half Fox(s, 2) { int p[4]; memcpy(p, P[i], sizeof(p)); if (s) reverse(p, p + 4); // check which points are on their required halves bool bx[2], by[2]; Fox(j, 2) { bx[j] = IsBetween(h[0], h[1], p[j * 2], 0); by[j] = IsBetween(h[1], h[0], p[j * 2 + 1], 0); } // neither line segment is entirely valid? if ((!bx[0] || !by[0]) && (!bx[1] || !by[1])) continue; assert(bx[0] + by[0] + bx[1] + by[1] >= 3); // other one must be at least half-valid // map points to positions on their halves int x[2], y[2]; Fox(j, 2) { x[j] = bx[j] ? GetPosAfter(h[0], p[j * 2]) : 2 * INF * (j ? 1 : -1); y[j] = by[j] ? -GetPosAfter(h[1], p[j * 2 + 1]) : 2 * INF * (j ? 1 : -1); } assert(x[0] < x[1] && y[0] < y[1]); E.pb({ x[0], y[0], y[1], C[i], 1 }); E.pb({ x[1], y[0], y[1], C[i], 0 }); CY.pb(y[0]), CY.pb(y[1]); break; } } // compress Y-coordinates sort(All(CY)); int K = unique(All(CY)) - CY.begin(); CY.resize(K); // line sweep SegTree ST(K); sort(All(E)); Foxen(e, E) { e.y1 = lower_bound(All(CY), e.y1) - CY.begin(); e.y2 = lower_bound(All(CY), e.y2) - CY.begin(); // left edge of a rectangle? if (e.s) { ST.Update(e.y1, K - 1, e.c); continue; } // right edge of a rectangle ST.Update(e.y2, e.y2, ST.Query(e.y2, K - 1) - ST.Query(e.y2, e.y2)); ST.Update(e.y2 + 1, K - 1, -e.c); } return(base + ST.Query(0, K - 1)); } int ProcessCase() { int i, j; // input Read(S), Read(N); Fox(i, N) { Read(C[i]); Fox(j, 4) { int x, y; Read(x), Read(y); if (!x) P[i][j] = y; else if (y == S) P[i][j] = S + x; else if (x == S) P[i][j] = 3 * S - y; else P[i][j] = 4 * S - x; } // normalize lines if (IsBetween(P[i][1], P[i][0], P[i][2], 1)) swap(P[i][0], P[i][1]); if (IsBetween(P[i][2], P[i][3], P[i][0], 1)) swap(P[i][2], P[i][3]); } // consider all possible dividing lines int ans = 0; Fox(i, N) { Fox(j, 2) Max(ans, SolveForLine({ P[i][j * 2], P[i][j * 2 + 1] })); } return(ans); } int main() { int T, t; Read(T); Fox1(t, T) printf("Case #%d: %d\n", t, ProcessCase()); return(0); }