Files
Codeforces-solutions/2015 HIAST Collegiate Programming Contest/J. Polygons Intersection.cpp
2026-04-03 14:51:26 -03:00

200 lines
3.8 KiB
C++

/* Problem URL: https://codeforces.com/gym/100952/problem/J */
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
template <class T, class comp = less<>>
using ordered_set = tree<T, null_type , comp , rb_tree_tag , tree_order_statistics_node_update>;
#define V vector
#define rmin(a, b) a = min(a, b)
#define rmax(a, b) a = max(a, b)
#define rep(i, lim) for (int i = 0; i < (lim); i++)
#define nrep(i, s, lim) for (int i = s; i < (lim); i++)
#define repv(i, v) for (auto &i : (v))
#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; }
#define sortv(v) sort(v.begin(), v.end())
#define all(v) (v).begin(), (v).end()
using vi = vector<int>;
using vvi = vector<vi>;
using vvvi = vector<vvi>;
using vvvvi = vector<vvvi>;
using ll = long long;
using vl = vector<ll>;
using vvl = vector<vl>;
using vvvl = vector<vvl>;
using vvvvl = vector<vvvl>;
template<class v>
auto operator<<(ostream &os, const vector<v> &vec)->ostream& {
os << vec[0];
for (size_t i = 1; i < vec.size(); i++) {
os << ' ' << vec[i];
}
os << '\n';
return os;
}
template<class v>
auto operator>>(istream &is, vector<v> &vec)->istream& {
for (auto &i : vec) {
is >> i;
}
return is;
}
template<class v>
auto operator<<(ostream &os, const vector<vector<v>> &vec)->ostream& {
for (auto &i : vec) {
os << i[0];
for (size_t j = 1; j < i.size(); j++) {
os << ' ' << i[j];
}
os << '\n';
}
return os;
}
template<class v>
auto operator>>(istream &is, vector<vector<v>> &vec)->istream& {
for (auto &i : vec) {
for (auto &j : i) {
is >> j;
}
}
return is;
}
const int oo = INT32_MAX >> 1;
const ll OO = INT64_MAX >> 1;
using ld = long double;
struct pt {
ld x, y;
pt() = default;
pt(ld x, ld y): x(x), y(y) {}
friend istream &operator >> (istream &is, pt &a) {
is >> a.x >> a.y;
return is;
}
ld operator ^ (pt b) {
return x * b.y - y * b.x;
}
pt operator - (pt b) {
return pt(x - b.x, y - b.y);
}
};
#define eq(x, y) (abs(x - y) < EPS)
ll sarea2(pt a, pt b, pt c)
{
return ((b - a) ^ (c - b));
}
int ccw(pt a, pt b, pt c)
{
ld r = sarea2(a, b, c);
return (r > 0) - (r < 0);
}
struct line {
pt a;
pt b;
line() = default;
line(pt a, pt b): a(a), b(b) {}
};
bool lineinter(line a, line b) {
return ccw(a.a, a.b, b.a) != ccw(a.a, a.b, b.b) && ccw(b.a, b.b, a.a) != ccw(b.a, b.b, a.b);
}
pt linept(line a, line b) {
ld l1[] = {a.b.y - a.a.y, a.a.x - a.b.x, a.a ^ a.b};
ld l2[] = {b.b.y - b.a.y, b.a.x - b.b.x, b.a ^ b.b};
ld det = l1[0] * l2[1] - l1[1] * l2[0];
return pt((l2[1] * l1[2] - l1[1] * l2[2]) / det, (l2[0] * l1[2] - l1[0] * l2[2]) / det);
}
void pre()
{
}
#define TEST 1
void solve()
{
int n, m;
cin >> n >> m;
V<pt> a(n);
V<pt> b(m);
cin >> a >> b;
V<pt> act[2];
int now = 0;
rep(i, n) {
pt p1 = a[i];
pt p2 = a[(i + 1) % n];
auto inter = [&](line a) {
rep(j, m) {
pt p3 = b[j];
pt p4 = b[(j + 1) % m];
if (!lineinter(a, line(p3, p4))) {
continue;
}
act[0].emplace_back(linept(a, line(p3, p4)));
act[1].emplace_back(linept(a, line(p3, p4)));
return true;
}
return false;
};
if (inter(line(p1, p2))) {
now ^= 1;
continue;
}
act[now].emplace_back(p1);
}
}
int main()
{
ios::sync_with_stdio(false);
cin.tie(nullptr);
pre();
int t;
(TEST && cin >> t) || (t = 1);
while (t--) {
solve();
}
}