File size: 7,498 Bytes
f96d8dd |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 |
// Cake-Cutting Committee
// Solution by Jacob Plachta
#include <algorithm>
#include <functional>
#include <numeric>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <complex>
#include <cstdlib>
#include <ctime>
#include <cstring>
#include <cassert>
#include <string>
#include <vector>
#include <list>
#include <map>
#include <set>
#include <unordered_map>
#include <unordered_set>
#include <deque>
#include <queue>
#include <stack>
#include <bitset>
#include <sstream>
using namespace std;
#define LL long long
#define LD long double
#define PR pair<int,int>
#define Fox(i,n) for (i=0; i<n; i++)
#define Fox1(i,n) for (i=1; i<=n; i++)
#define FoxI(i,a,b) for (i=a; i<=b; i++)
#define FoxR(i,n) for (i=(n)-1; 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<typename T> T Abs(T x) { return(x < 0 ? -x : x); }
template<typename T> 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<int> h)
{
int i, j, s;
// compare all pieces against dividing line, and assemble line sweep events
int base = 0;
vector<int> CY;
vector<Event> 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);
} |