| | #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; |
| | |
| | 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}); |
| | } |
| | |
| | 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}); |
| | |
| | Axx[i] = A[i].x.x, Axy[i] = A[i].x.y; |
| | |
| | |
| | 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; |
| | } |
| | |
| | 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; |
| | } |
| | } |
| | |
| | STX = NestedSegTree(cx.V.size()); |
| | STY = NestedSegTree(cy.V.size()); |
| | for (int j : {0, 1}) { |
| | if (j) { |
| | STX.init_keys(); |
| | STY.init_keys(); |
| | } |
| | for (int i = 0; i < N; i++) { |
| | |
| | update_A_rect(i, 1); |
| | } |
| | } |
| | |
| | memset(visit, 0, sizeof visit); |
| | for (int i = 0; i < N; i++) { |
| | if (!visit[i]) { |
| | if (rec(i)) { |
| | return false; |
| | } |
| | } |
| | } |
| | return true; |
| | } |
| |
|
| | int main() { |
| | int T; |
| | cin >> T; |
| | for (int t = 1; t <= T; t++) { |
| | cout << "Case #" << t << ": " << (solve() ? "YES" : "NO") << endl; |
| | } |
| | return 0; |
| | } |
| |
|