forked from ngthanhtrung23/CompetitiveProgramming
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJ.cpp
More file actions
166 lines (148 loc) · 5.59 KB
/
J.cpp
File metadata and controls
166 lines (148 loc) · 5.59 KB
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
#include <bits/stdc++.h>
#define int long long
#define FOR(i,a,b) for(int i=(a),_b=(b); i<=_b; ++i)
#define FORD(i,a,b) for(int i=(a),_b=(b); i>=_b; --i)
#define REP(i,a) for(int i=0,_a=(a); i < _a; ++i)
#define DEBUG(X) { cout << #X << " = " << (X) << endl; }
#define PR(A,n) { cout << #A << " = "; FOR(_,1,n) cout << A[_] << ' '; cout << endl; }
#define PR0(A,n) { cout << #A << " = "; REP(_,n) cout << A[_] << ' '; cout << endl; }
#define sqr(x) ((x) * (x))
#define ll long long
#define __builtin_popcount __builtin_popcountll
#define SZ(x) ((int) (x).size())
using namespace std;
double safe_sqrt(double x) {
return sqrt(max((double)0.0,x));
}
int GI(ll& x) {
return scanf("%lld", &x);
}
#define EPS 1e-6
const double PI = acos(-1.0);
double DEG_to_RAD(double d) { return d * PI / 180.0; }
double RAD_to_DEG(double r) { return r * 180.0 / PI; }
inline int cmp(double a, double b) {
return (a < b - EPS) ? -1 : ((a > b + EPS) ? 1 : 0);
}
struct Point {
double x, y;
Point(double x = 0.0, double y = 0.0) : x(x), y(y) {}
Point operator + (const Point& a) const { return Point(x+a.x, y+a.y); }
Point operator - (const Point& a) const { return Point(x-a.x, y-a.y); }
Point operator * (double k) const { return Point(x*k, y*k); }
Point operator / (double k) const { return Point(x/k, y/k); }
double operator * (const Point& a) const { return x*a.x + y*a.y; } // dot product
double operator % (const Point& a) const { return x*a.y - y*a.x; } // cross product
int cmp(Point q) const { if (int t = ::cmp(x,q.x)) return t; return ::cmp(y,q.y); }
#define Comp(x) bool operator x (Point q) const { return cmp(q) x 0; }
Comp(>) Comp(<) Comp(==) Comp(>=) Comp(<=) Comp(!=)
#undef Comp
Point conj() { return Point(x, -y); }
double norm() { return x*x + y*y; }
// Note: There are 2 ways for implementing len():
// 1. sqrt(norm()) --> fast, but inaccurate (produce some values that are of order X^2)
// 2. hypot(x, y) --> slow, but much more accurate
double len() { return sqrt(norm()); }
Point rotate(double alpha) {
double cosa = cos(alpha), sina = sin(alpha);
return Point(x * cosa - y * sina, x * sina + y * cosa);
}
};
int ccw(Point a, Point b, Point c) {
return cmp((b-a)%(c-a),0);
}
int RE_TRAI = ccw(Point(0, 0), Point(0, 1), Point(-1, 1));
int RE_PHAI = ccw(Point(0, 0), Point(0, 1), Point(1, 1));
istream& operator >> (istream& cin, Point& p) {
cin >> p.x >> p.y;
return cin;
}
ostream& operator << (ostream& cout, Point& p) {
cout << p.x << ' ' << p.y;
return cout;
}
typedef vector< Point > Polygon;
#define Det(a,b,c) ((double)(b.x-a.x)*(double)(c.y-a.y)-(double)(b.y-a.y)*(c.x-a.x))
bool in_convex(vector<Point>& l, Point p){
int a = 1, b = l.size()-1, c;
if (Det(l[0], l[a], l[b]) > 0) swap(a,b);
// Allow on edge --> if (Det... > 0 || Det ... < 0)
if (Det(l[0], l[a], p) >= 0 || Det(l[0], l[b], p) <= 0) return false;
while(abs(a-b) > 1) {
c = (a+b)/2;
if (Det(l[0], l[c], p) > 0) b = c; else a = c;
}
// Alow on edge --> return Det... <= 0
return Det(l[a], l[b], p) < 0;
}
bool intersect_1pt(Point a, Point b,
Point c, Point d, Point &r) {
double D = (b - a) % (d - c);
if (cmp(D, 0) == 0) return false;
double t = ((c - a) % (d - c)) / D;
double s = -((a - c) % (b - a)) / D;
r = a + (b - a) * t;
return cmp(t, 0) >= 0 && cmp(t, 1) <= 0 && cmp(s, 0) >= 0 && cmp(s, 1) <= 0;
}
Polygon convex_intersect(Polygon P, Polygon Q) {
const int n = P.size(), m = Q.size();
int a = 0, b = 0, aa = 0, ba = 0;
enum { Pin, Qin, Unknown } in = Unknown;
Polygon R;
do {
int a1 = (a+n-1) % n, b1 = (b+m-1) % m;
double C = (P[a] - P[a1]) % (Q[b] - Q[b1]);
double A = (P[a1] - Q[b]) % (P[a] - Q[b]);
double B = (Q[b1] - P[a]) % (Q[b] - P[a]);
Point r;
if (intersect_1pt(P[a1], P[a], Q[b1], Q[b], r)) {
if (in == Unknown) aa = ba = 0;
R.push_back( r );
in = B > 0 ? Pin : A > 0 ? Qin : in;
}
if (C == 0 && B == 0 && A == 0) {
if (in == Pin) { b = (b + 1) % m; ++ba; }
else { a = (a + 1) % m; ++aa; }
} else if (C >= 0) {
if (A > 0) { if (in == Pin) R.push_back(P[a]); a = (a+1)%n; ++aa; }
else { if (in == Qin) R.push_back(Q[b]); b = (b+1)%m; ++ba; }
} else {
if (B > 0) { if (in == Qin) R.push_back(Q[b]); b = (b+1)%m; ++ba; }
else { if (in == Pin) R.push_back(P[a]); a = (a+1)%n; ++aa; }
}
} while ( (aa < n || ba < m) && aa < 2*n && ba < 2*m );
if (in == Unknown) {
if (in_convex(Q, P[0])) return P;
if (in_convex(P, Q[0])) return Q;
}
return R;
}
double signed_area(Polygon p) {
double area = 0;
for(int i = 0; i < p.size(); i++) {
int j = (i+1) % p.size();
area += p[i].x*p[j].y - p[j].x*p[i].y;
}
return area / 2.0;
}
double area(const Polygon &p) {
return fabs(signed_area(p));
}
#undef int
int main() {
#define int long long
ios :: sync_with_stdio(0); cin.tie(0);
cout << (fixed) << setprecision(4);
int ntest; cin >> ntest;
while (ntest--) {
int n1, n2;
cin >> n1 >> n2;
Polygon a1, a2;
a1.resize(n1);
a2.resize(n2);
REP(i,n1) cin >> a1[i];
REP(i,n2) cin >> a2[i];
auto p = convex_intersect(a1, a2);
cout << signed_area(p) << endl;
}
}