|
#include <algorithm> |
|
#include <chrono> |
|
#include <iostream> |
|
#include <random> |
|
#include <unordered_map> |
|
#include <vector> |
|
using namespace std; |
|
|
|
using LL = long long; |
|
|
|
struct Point { |
|
LL x, y; |
|
|
|
Point() : x(0), y(0) {} |
|
Point(LL _x, LL _y) : x(_x), y(_y) {} |
|
|
|
bool operator==(const Point &p) const { return x == p.x && y == p.y; } |
|
bool operator!=(const Point &p) const { return x != p.x || y != p.y; } |
|
bool operator<(const Point &p) const { return x != p.x ? x < p.x : y < p.y; } |
|
}; |
|
|
|
struct Line { |
|
Point left, right; |
|
|
|
Line() : left() {} |
|
Line(const Point &p1, const Point &p2) : left(p1), right(p2) { |
|
if (left.x > right.x) { |
|
swap(left, right); |
|
} |
|
} |
|
|
|
bool operator==(const Line &l) const { |
|
return left == l.left && right == l.right; |
|
} |
|
bool is_horizontal() const { return left.y == right.y; } |
|
bool is_vertical() const { return left.x == right.x; } |
|
}; |
|
|
|
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); |
|
|
|
struct Treap { |
|
Line *key; |
|
Treap *left, *right; |
|
int weight; |
|
|
|
Treap() : key(nullptr), left(nullptr), right(nullptr), weight(rng()) {} |
|
Treap(Line *l) : key(l), left(nullptr), right(nullptr), weight(rng()) {} |
|
}; |
|
|
|
static Treap *rotate_left(Treap *x) { |
|
|
|
Treap *y = x->left; |
|
x->left = y->right; |
|
y->right = x; |
|
return y; |
|
} |
|
|
|
static Treap *rotate_right(Treap *x) { |
|
|
|
Treap *y = x->right; |
|
x->right = y->left; |
|
y->left = x; |
|
return y; |
|
} |
|
|
|
template <typename TCompare> |
|
static Treap *persistent_insert(Treap *h, Line *key, const TCompare &cmp) { |
|
if (!h) { |
|
return new Treap(key); |
|
} |
|
Treap *x = new Treap(*h); |
|
if (cmp(key, x->key)) { |
|
x->left = persistent_insert(x->left, key, cmp); |
|
if (x->left->weight < x->weight) { |
|
return rotate_left(x); |
|
} |
|
} else if (cmp(x->key, key)) { |
|
x->right = persistent_insert(x->right, key, cmp); |
|
if (x->right->weight < x->weight) { |
|
return rotate_right(x); |
|
} |
|
} else { |
|
|
|
} |
|
|
|
return x; |
|
} |
|
|
|
static Treap *persistent_remove(Treap *h) { |
|
if (h->left != nullptr && h->right != nullptr) { |
|
Treap *y; |
|
if (h->left->weight < h->right->weight) { |
|
h->left = new Treap(*h->left); |
|
y = rotate_left(h); |
|
y->right = persistent_remove(h); |
|
} else { |
|
h->right = new Treap(*h->right); |
|
y = rotate_right(h); |
|
y->left = persistent_remove(h); |
|
} |
|
return y; |
|
} |
|
Treap *next = h->left; |
|
if (h->right != nullptr) { |
|
next = h->right; |
|
} |
|
|
|
return next; |
|
} |
|
|
|
template <typename TCompare> |
|
static Treap *persistent_remove(Treap *h, Line *key, const TCompare &cmp) { |
|
|
|
Treap *x = new Treap(*h); |
|
if (cmp(key, x->key)) { |
|
x->left = persistent_remove(x->left, key, cmp); |
|
} else if (cmp(x->key, key)) { |
|
x->right = persistent_remove(x->right, key, cmp); |
|
} else { |
|
return persistent_remove(x); |
|
} |
|
return x; |
|
} |
|
|
|
using TreapPair = pair<Treap *, Treap *>; |
|
|
|
template <typename TCompare> |
|
TreapPair floor_and_ceil(Treap *h, const Point &p, const TCompare &cmp) { |
|
Treap *x = h; |
|
TreapPair result(nullptr, nullptr); |
|
while (x) { |
|
if (cmp(p, x->key)) { |
|
result.second = x; |
|
x = x->left; |
|
} else { |
|
result.first = x; |
|
x = x->right; |
|
} |
|
} |
|
return result; |
|
} |
|
|
|
template <class It> |
|
pair<vector<Line *>, vector<Line *>> get_lower_and_upper_hull(It lo, It hi) { |
|
auto cross = [&](const Point &a, const Point &b, const Point &o) { |
|
return (a.x - o.x)*(b.y - o.y) - (a.y - o.y)*(b.x - o.x); |
|
}; |
|
int len = distance(lo, hi), k = 0; |
|
|
|
vector<Point> hull(2 * len); |
|
sort(lo, hi); |
|
for (It it = lo; it != hi; ++it) { |
|
while (k >= 2 && cross(hull[k - 1], *it, hull[k - 2]) >= 0) { |
|
k--; |
|
} |
|
hull[k++] = *it; |
|
} |
|
int t = k; |
|
for (It it = hi - 2; it != lo - 1; --it) { |
|
while (k > t && cross(hull[k - 1], *it, hull[k - 2]) >= 0) { |
|
k--; |
|
} |
|
hull[k++] = *it; |
|
} |
|
hull.resize(k); |
|
vector<Line *> lower, upper; |
|
for (int i = 1; i < t; i++) { |
|
upper.push_back(new Line(hull[i - 1], hull[i])); |
|
} |
|
for (int i = t; i < k; i++) { |
|
lower.push_back(new Line(hull[i - 1], hull[i])); |
|
} |
|
return make_pair(lower, upper); |
|
} |
|
|
|
enum class EventType : int { |
|
Open = 1, |
|
Close = 2, |
|
}; |
|
|
|
struct Event { |
|
Line *line; |
|
EventType type; |
|
|
|
Event() : line(nullptr) {} |
|
Event(Line *_line, const EventType _type) : line(_line), type(_type) {} |
|
}; |
|
|
|
struct EnclosedPolygons { |
|
vector<Line *> edges; |
|
unordered_map<Line *, int> poly_id; |
|
unordered_map<Line *, bool> is_upper; |
|
unordered_map<int, int> parent; |
|
|
|
vector<LL> xvals; |
|
vector<Event> events; |
|
Treap *tlatest; |
|
vector<Treap *> tversions; |
|
|
|
static double eval_line(const Line *l, LL x) { |
|
|
|
double m = (double)(l->right.y - l->left.y) / (l->right.x - l->left.x); |
|
double b = l->left.y - m * l->left.x; |
|
return m * x + b; |
|
} |
|
|
|
static bool cmp_point(const Point &p, const Line *l) { |
|
if (p == l->left || p == l->right) { |
|
return false; |
|
} |
|
return p.y < eval_line(l, p.x); |
|
} |
|
|
|
EnclosedPolygons(vector<vector<Point>> &_polygons) : tlatest(nullptr) { |
|
for (size_t i = 0; i < _polygons.size(); ++i) { |
|
auto &points = _polygons[i]; |
|
for (const Point &p : points) { |
|
xvals.push_back(p.x); |
|
} |
|
auto hulls = get_lower_and_upper_hull(points.begin(), points.end()); |
|
auto add_edge = [&](Line *l) { |
|
edges.push_back(l); |
|
events.push_back(Event(l, EventType::Open)); |
|
events.push_back(Event(l, EventType::Close)); |
|
poly_id[l] = i; |
|
}; |
|
for (Line *l : hulls.first) { |
|
add_edge(l); |
|
} |
|
for (Line *l : hulls.second) { |
|
add_edge(l); |
|
is_upper[l] = true; |
|
} |
|
} |
|
sort(xvals.begin(), xvals.end()); |
|
xvals.resize(distance(xvals.begin(), unique(xvals.begin(), xvals.end()))); |
|
auto cmp_events = [&](const Event &a, const Event &b) { |
|
LL ax = a.type == EventType::Open ? a.line->left.x : a.line->right.x; |
|
LL ay = a.type == EventType::Open ? a.line->left.y : a.line->right.y; |
|
LL bx = b.type == EventType::Open ? b.line->left.x : b.line->right.x; |
|
LL by = b.type == EventType::Open ? b.line->left.y : b.line->right.y; |
|
if (ax != bx) { |
|
return ax < bx; |
|
} |
|
if (ay != by) { |
|
return ay < by; |
|
} |
|
|
|
|
|
if (a.type != b.type) { |
|
return a.type == EventType::Close; |
|
} |
|
|
|
|
|
|
|
|
|
return ay < max(b.line->left.y, b.line->right.y); |
|
}; |
|
stable_sort(events.begin(), events.end(), cmp_events); |
|
parent[0] = -1; |
|
auto xit = xvals.begin(); |
|
auto eit = events.begin(); |
|
while (xit != xvals.end() && eit != events.end()) { |
|
LL curr_x = *xit; |
|
tversions.push_back(tlatest); |
|
|
|
auto cmp = [curr_x](const Line *l1, const Line *l2) { |
|
|
|
if (l1->left == l2->left) { |
|
int x = min(l1->right.x, l2->right.x); |
|
return eval_line(l1, x) < eval_line(l2, x); |
|
} |
|
if (l1->right == l2->right) { |
|
int x = max(l1->left.x, l2->left.x); |
|
return eval_line(l1, x) < eval_line(l2, x); |
|
} |
|
if (l1->right == l2->left) { |
|
return l1->left.y < l2->right.y; |
|
} |
|
if (l1->left == l2->right) { |
|
|
|
return l1->right.y < l2->left.y; |
|
} |
|
return eval_line(l1, curr_x) < eval_line(l2, curr_x); |
|
}; |
|
|
|
while (eit != events.end()) { |
|
if (eit->line->is_vertical()) { |
|
++eit; |
|
continue; |
|
} |
|
if (curr_x != (eit->type == EventType::Open ? eit->line->left.x |
|
: eit->line->right.x)) { |
|
break; |
|
} |
|
int id = poly_id[eit->line]; |
|
if (parent.find(id) == parent.end()) { |
|
LL curr_y = eit->type == EventType::Open ? eit->line->left.y |
|
: eit->line->right.y; |
|
auto res = floor_and_ceil(tlatest, Point(curr_x, curr_y), cmp_point); |
|
Line *line_below = res.first ? res.first->key : nullptr; |
|
|
|
int below_id = poly_id[line_below]; |
|
if (is_upper[line_below]) { |
|
parent[id] = parent[below_id]; |
|
} else { |
|
|
|
if (id != below_id) { |
|
parent[id] = below_id; |
|
} |
|
} |
|
} |
|
if (eit->type == EventType::Open) { |
|
tlatest = persistent_insert(tlatest, eit->line, cmp); |
|
} else { |
|
tlatest = persistent_remove(tlatest, eit->line, cmp); |
|
} |
|
++eit; |
|
} |
|
++xit; |
|
} |
|
} |
|
|
|
int get_containing_polygon(const Point &p) { |
|
|
|
size_t left = 1, right = xvals.size() - 1; |
|
while (left + 1 < right) { |
|
int mid = (left + right) / 2; |
|
if (xvals[mid] <= p.x) { |
|
left = mid; |
|
} else { |
|
right = mid; |
|
} |
|
} |
|
if (left != right && xvals[left] <= p.x) { |
|
left = right; |
|
} |
|
|
|
auto res = floor_and_ceil(tversions[left], p, cmp_point); |
|
Line *line_below = res.first ? res.first->key : nullptr; |
|
|
|
int below_id = poly_id[line_below]; |
|
return is_upper[line_below] ? parent[below_id] : below_id; |
|
} |
|
|
|
~EnclosedPolygons() { |
|
for (Line *l : edges) { |
|
delete l; |
|
} |
|
} |
|
}; |
|
|
|
const size_t SEED = chrono::steady_clock::now().time_since_epoch().count(); |
|
|
|
size_t get_hash(size_t v) { |
|
size_t x = v + SEED; |
|
|
|
x += 0x9e3779b97f4a7c15; |
|
x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9; |
|
x = (x ^ (x >> 27)) * 0x94d049bb133111eb; |
|
return x ^ (x >> 31); |
|
} |
|
|
|
vector<size_t> subtree_hash, overall_hash; |
|
|
|
void dfs1(int u, const vector<vector<int>> &adj) { |
|
size_t tot = 0; |
|
for (int v : adj[u]) { |
|
dfs1(v, adj); |
|
tot += subtree_hash[v]; |
|
} |
|
subtree_hash[u] = get_hash(tot); |
|
} |
|
|
|
void dfs2(int u, const vector<vector<int>> &adj, size_t par_hash) { |
|
overall_hash[u] = get_hash(subtree_hash[u] + get_hash(par_hash)); |
|
for (int v : adj[u]) { |
|
dfs2(v, adj, overall_hash[u]); |
|
} |
|
} |
|
|
|
int solve() { |
|
|
|
const LL LO = -5, HI = (int)1e9 + 5; |
|
vector<vector<Point>> polygons{{{LO, LO}, {HI, LO}, {HI, HI}, {LO, HI}}}; |
|
int N, Q; |
|
|
|
cin >> N; |
|
for (int i = 0, M; i < N; i++) { |
|
cin >> M; |
|
vector<Point> points; |
|
for (int j = 0, x, y; j < M; j++) { |
|
cin >> x >> y; |
|
points.push_back(Point(x, y)); |
|
} |
|
polygons.push_back(points); |
|
} |
|
EnclosedPolygons EP(polygons); |
|
|
|
N++; |
|
vector<vector<int>> adj(N); |
|
for (int i = 1; i < N; i++) { |
|
adj[EP.parent[i]].push_back(i); |
|
} |
|
subtree_hash.resize(N); |
|
overall_hash.resize(N); |
|
dfs1(0, adj); |
|
dfs2(0, adj, 0); |
|
|
|
cin >> Q; |
|
int ans = 0, E = 0; |
|
for (int i = 0, a, b, c, d, e; i < Q; i++) { |
|
cin >> a >> b >> c >> d >> e; |
|
a ^= E; |
|
b ^= E; |
|
c ^= E; |
|
d ^= E; |
|
int u = EP.get_containing_polygon(Point(a, b)); |
|
int v = EP.get_containing_polygon(Point(c, d)); |
|
if (overall_hash[u] == overall_hash[v]) { |
|
ans++; |
|
E ^= e; |
|
} |
|
} |
|
return ans; |
|
} |
|
|
|
int main() { |
|
ios_base::sync_with_stdio(false); |
|
cin.tie(nullptr); |
|
int T; |
|
cin >> T; |
|
for (int t = 1; t <= T; t++) { |
|
cout << "Case #" << t << ": " << solve() << endl; |
|
} |
|
return 0; |
|
} |