File size: 9,955 Bytes
d3f4f72 |
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 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 |
#include <algorithm>
#include <cstring>
#include <iostream>
#include <tuple>
#include <utility>
#include <vector>
using namespace std;
const int LIM = 300003;
using pii = pair<int, int>;
#define x first
#define y second
struct Compressor {
int N;
vector<pii> V;
Compressor() {}
Compressor(vector<pii> _V) {
V = _V;
comp();
}
void insert(pii v) {
V.push_back(v);
}
void comp() {
sort(V.begin(), V.end());
N = unique(V.begin(), V.end()) - V.begin();
V.resize(N);
}
int get(pii v) {
return lower_bound(V.begin(), V.end(), v) - V.begin();
}
};
const int TLIM = 2100000;
const int ZERO_VAL = 0;
const int ZERO_LAZY = 0;
struct MaxRangeSegTree {
static void update_value(int &a, int v, int r1, int r2) { a += v; }
static void update_lazy(int &a, int v, int r1, int r2) { a += v; }
static int join_values(int v1, int v2) { return max(v1, v2); }
int N, sz;
int tree[TLIM];
int lazy[TLIM];
MaxRangeSegTree() {}
MaxRangeSegTree(int _N) { init(_N); }
void init(int _N) {
N = _N;
for (sz = 1; sz < N; sz <<= 1)
;
clear();
}
void clear() {
for (int i = 0; i < (sz << 1); i++) {
tree[i] = ZERO_VAL;
lazy[i] = ZERO_LAZY;
}
}
void propagate(int i, int r1, int r2) {
int v = lazy[i];
lazy[i] = ZERO_LAZY;
update_value(tree[i], v, r1, r2);
if (i < sz) {
int m = (r1 + r2) >> 1, c1 = i << 1, c2 = c1 + 1;
update_lazy(lazy[c1], v, r1, m);
update_lazy(lazy[c2], v, m + 1, r2);
}
}
void comp(int i) {
int c1 = i << 1, c2 = c1 + 1;
tree[i] = join_values(tree[c1], tree[c2]);
}
int query(int a, int b, int i = 1, int r1 = 0, int r2 = -1) {
if (r2 < 0) {
a = max(a, 0);
b = min(b, sz - 1);
if (a > b) {
return ZERO_VAL;
}
r2 = sz - 1;
}
propagate(i, r1, r2);
if (a <= r1 && r2 <= b) {
return tree[i];
}
int m = (r1 + r2) >> 1, c = i << 1;
int res = ZERO_VAL;
if (a <= m) {
res = join_values(res, query(a, b, c, r1, m));
}
if (b > m) {
res = join_values(res, query(a, b, c + 1, m + 1, r2));
}
return res;
}
void update(int a, int b, int v, int i = 1, int r1 = 0, int r2 = -1) {
if (r2 < 0) {
a = max(a, 0);
b = min(b, sz - 1);
if (a > b) {
return;
}
r2 = sz - 1;
}
if (r2 < 0) {
r2 = sz - 1;
}
propagate(i, r1, r2);
if (a <= r1 && r2 <= b) {
update_lazy(lazy[i], v, r1, r2);
propagate(i, r1, r2);
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);
}
propagate(c, r1, m);
propagate(c + 1, m + 1, r2);
comp(i);
}
};
const pii NONE{-1, -1};
struct MaxElemSegTree {
pii join_values(pii v1, pii v2) { return max(v1, v2); }
int N, sz;
vector<pii> tree;
MaxElemSegTree() {}
MaxElemSegTree(int _N) { init(_N); }
void init(int _N) {
N = _N;
for (sz = 1; sz < N; sz <<= 1)
;
clear();
}
void clear() {
tree.clear();
tree.resize(sz << 1, NONE);
}
void comp(int i) {
int c1 = i << 1, c2 = c1 + 1;
tree[i] = join_values(tree[c1], tree[c2]);
}
pii query(int a, int b, int i = 1, int r1 = 0, int r2 = -1) {
if (r2 < 0) {
a = max(a, 0);
b = min(b, sz - 1);
if (a > b) {
return NONE;
}
r2 = sz - 1;
}
if (a <= r1 && r2 <= b) {
return tree[i];
}
int m = (r1 + r2) >> 1, c = i << 1;
pii res = NONE;
if (a <= m) {
res = join_values(res, query(a, b, c, r1, m));
}
if (b > m) {
res = join_values(res, query(a, b, c + 1, m + 1, r2));
}
return res;
}
void update_one(int i, pii p) {
i += sz;
tree[i] = p;
while (i > 1) {
i >>= 1;
comp(i);
}
}
};
struct NestedSegTree {
int N, sz;
bool has_init_keys;
vector<int> keys[TLIM];
MaxElemSegTree T[TLIM];
NestedSegTree() {}
NestedSegTree(int _N) { init(_N); }
void init(int _N) {
N = _N;
for (sz = 1; sz < N; sz <<= 1)
;
has_init_keys = false;
}
void init_keys() {
for (int i = sz; i < sz + N; i++) {
auto &k = keys[i];
sort(k.begin(), k.end());
k.resize(unique(k.begin(), k.end()) - k.begin());
T[i].init(k.size());
}
for (int i = sz - 1; i >= 1; i--) {
auto &k = keys[i], &c1 = keys[i << 1], &c2 = keys[(i << 1) + 1];
int a = 0, b = 0;
while (a < c1.size() || b < c2.size()) {
if (a < c1.size() && b < c2.size() && c1[a] == c2[b]) {
k.push_back(c1[a++]);
b++;
} else if (b == c2.size() || (a < c1.size() && c1[a] < c2[b])) {
k.push_back(c1[a++]);
} else {
k.push_back(c2[b++]);
}
}
T[i].init(k.size());
}
has_init_keys = true;
}
pii query_max_up_to_k(int a, int b, int k, int i = 1, int r1 = 0, int r2 = -1) {
if (r2 < 0) {
a = max(a, 0);
b = min(b, sz - 1);
if (a > b) {
return NONE;
}
r2 = sz - 1;
}
if (a <= r1 && r2 <= b) {
k = lower_bound(keys[i].begin(), keys[i].end(), k + 1) - keys[i].begin() - 1;
return T[i].query(0, k);
}
int m = (r1 + r2) >> 1, c = i << 1;
pii res = NONE;
if (a <= m) {
res = max(res, query_max_up_to_k(a, b, k, c, r1, m));
}
if (b > m) {
res = max(res, query_max_up_to_k(a, b, k, c + 1, m + 1, r2));
}
return res;
}
void update_one(int i, int k, pii p) {
i += sz;
if (!has_init_keys) {
keys[i].push_back(k);
return;
}
while (i >= 1) {
T[i].update_one(
lower_bound(keys[i].begin(), keys[i].end(), k) - keys[i].begin(), p
);
i >>= 1;
}
}
};
int N;
pair<pii, pii> A[LIM], B[LIM];
int Axx[LIM], Axy[LIM];
int lowerX[3 * LIM], lowerY[3 * LIM];
bool visit[LIM];
NestedSegTree STX, STY;
void update_A_rect(int i, bool ins) {
STX.update_one(A[i].x.x, Axy[i], ins ? make_pair(A[i].y.y, i) : NONE);
STX.update_one(A[i].y.x, Axy[i], ins ? make_pair(A[i].y.y, i) : NONE);
STY.update_one(A[i].x.y, Axx[i], ins ? make_pair(A[i].y.x, i) : NONE);
STY.update_one(A[i].y.y, Axx[i], ins ? make_pair(A[i].y.x, i) : NONE);
}
int get_A_rect_for_B_rect(int i) {
pii p = STX.query_max_up_to_k(B[i].x.x, B[i].y.x, B[i].y.y);
if (p.x >= B[i].x.y) {
return p.y;
}
p = STY.query_max_up_to_k(B[i].x.y, B[i].y.y, B[i].y.x);
if (p.x >= B[i].x.x) {
return p.y;
}
return -1;
}
bool rec(int i) {
visit[i] = true;
for (;;) {
int j = get_A_rect_for_B_rect(i);
if (j < 0) {
break;
}
if (visit[j]) {
return true;
}
if (rec(j)) {
return true;
}
}
update_A_rect(i, 0);
return false;
}
bool solve() {
Compressor cx, cy;
// Input, and compute flipped rectangles.
cin >> N;
for (int i = 0; i < N; i++) {
char c;
cin >> A[i].x.x >> A[i].x.y >> A[i].y.x >> A[i].y.y >> c;
B[i] = A[i];
switch (c) {
case 'U': B[i].x.y += A[i].y.y; break;
case 'D': B[i].x.y -= A[i].y.y; break;
case 'L': B[i].x.x -= A[i].y.x; break;
case 'R': B[i].x.x += A[i].y.x; break;
}
A[i].y.x += A[i].x.x;
A[i].y.y += A[i].x.y;
B[i].y.x += B[i].x.x;
B[i].y.y += B[i].x.y;
cx.insert({A[i].x.x, i});
cx.insert({A[i].y.x, i}),
cx.insert({B[i].x.x, i});
cx.insert({B[i].y.x, i});
cy.insert({A[i].x.y, i});
cy.insert({A[i].y.y, i});
cy.insert({B[i].x.y, i});
cy.insert({B[i].y.y, i});
}
// Compress coordinates.
cx.comp(), cy.comp();
auto V = cx.V;
for (int i = 0; i < V.size(); i++) {
lowerX[i] = (i == 0 || V[i - 1].x != V[i].x) ? i : lowerX[i - 1];
}
V = cy.V;
for (int i = 0; i < V.size(); i++) {
lowerY[i] = (i == 0 || V[i - 1].x != V[i].x) ? i : lowerY[i - 1];
}
for (int i = 0; i < N; i++) {
A[i].x.x = cx.get({A[i].x.x, i});
A[i].x.y = cy.get({A[i].x.y, i}),
A[i].y.x = cx.get({A[i].y.x, i});
A[i].y.y = cy.get({A[i].y.y, i});
B[i].x.x = cx.get({B[i].x.x, i});
B[i].x.y = cy.get({B[i].x.y, i}),
B[i].y.x = cx.get({B[i].y.x, i});
B[i].y.y = cy.get({B[i].y.y, i});
// Remember exact (unique) lower coordinates.
Axx[i] = A[i].x.x, Axy[i] = A[i].x.y;
// Round coordinates up to value boundaries (with exclusive upper
// coordinates).
A[i].x.x = lowerX[A[i].x.x];
B[i].x.x = lowerX[B[i].x.x];
A[i].x.y = lowerY[A[i].x.y];
B[i].x.y = lowerY[B[i].x.y];
A[i].y.x = lowerX[A[i].y.x] - 1;
B[i].y.x = lowerX[B[i].y.x] - 1;
A[i].y.y = lowerY[A[i].y.y] - 1;
B[i].y.y = lowerY[B[i].y.y] - 1;
}
// Check for any overlap between final tables via line sweep.
vector<tuple<int, int, int, int>> E;
for (int i = 0; i < N; i++) {
E.emplace_back(B[i].x.y, 1, B[i].x.x, B[i].y.x);
E.emplace_back(B[i].y.y + 1, -1, B[i].x.x, B[i].y.x);
}
sort(E.begin(), E.end());
MaxRangeSegTree ST(cx.V.size());
for (auto e : E) {
ST.update(get<2>(e), get<3>(e), get<1>(e));
if (ST.query(0, cx.V.size() - 1) > 1) {
return false;
}
}
// Initialize segment trees of initial tables, over x and y coordinates.
STX = NestedSegTree(cx.V.size());
STY = NestedSegTree(cy.V.size());
for (int j : {0, 1}) {
if (j) { // Initialize keys second time around.
STX.init_keys();
STY.init_keys();
}
for (int i = 0; i < N; i++) {
// Set up keys first time around, then perform actual updates.
update_A_rect(i, 1);
}
}
// Search for cycles via DFS.
memset(visit, 0, sizeof visit);
for (int i = 0; i < N; i++) {
if (!visit[i]) {
if (rec(i)) {
return false; // Cycle found.
}
}
}
return true;
}
int main() {
int T;
cin >> T;
for (int t = 1; t <= T; t++) {
cout << "Case #" << t << ": " << (solve() ? "YES" : "NO") << endl;
}
return 0;
}
|