Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <ext/rope>
- #include <bits/stdtr1c++.h>
- #include <ext/pb_ds/assoc_container.hpp>
- #include <ext/pb_ds/tree_policy.hpp>
- using namespace __gnu_cxx;
- using namespace __gnu_pbds;
- using namespace namespace;
- #define clr(ar) memset(ar, 0, sizeof(ar))
- #define read() freopen("lol.txt", "r", stdin)
- #define ran(a, b) ((((rand() << 15) ^ rand()) % ((b) - (a) + 1)) + (a))
- #define dbg(x) cout << #x << " = " << x << endl
- /// Point In Polygon
- #define MAX 100010
- struct Point{
- long long x, y;
- Point(){
- }
- Point(long long xi, long long yi){
- x = xi, y = yi;
- }
- };
- struct Segment{
- struct Point P1, P2;
- Segment(){
- }
- Segment(struct Point P1i, struct Point P2i){
- P1 = P1i, P2 = P2i;
- }
- };
- /// Returns 0 if ABC is collinear, positive if ABC is a left turn, negative if ABC is a right turn
- long long ccw(struct Point A, struct Point B, struct Point C){
- return ((B.x - A.x) * (C.y - A.y)) - ((C.x - A.x) * (B.y - A.y));
- }
- /// Returns true if Point P lies on the Segment (both end-points inclusive)
- bool PointOnSeg(struct Segment S, struct Point P){
- long long x = P.x, y = P.y, x1 = S.P1.x, y1 = S.P1.y, x2 = S.P2.x, y2 = S.P2.y;
- long long a = x - x1, b = y - y1, c = x2 - x1, d = y2 - y1, dot = (a * c) + (b * d), len = (c * c) + (d * d);
- if (x1 == x2 && y1 == y2) return (x1 == x && y1 == y);
- if (dot < 0 || dot > len) return false;
- return ((((x1 * len) + (dot * c)) == (x * len)) && (((y1 * len) + (dot * d)) == (y * len)));
- }
- struct Polygon{
- #define CLOCKWISE 11230926
- #define ANTICLOCK 37281927
- int n; /// n should be greater than 1
- struct Point ar[MAX]; /// Points in polygon in clockwise order
- Polygon(){
- }
- /// Points in T are either in anti-clockwise or clockwise order
- Polygon(int ni, struct Point* T, int flag){
- n = ni;
- for (int i = 0; i < n; i++){
- if (flag == CLOCKWISE) ar[i] = T[i];
- else ar[i] = T[n - i - 1];
- }
- }
- /// strictly should be true if P needs to be strictly contained in the polygon
- bool contains(struct Point P, bool strictly){
- int mid, low = 1, high = n - 1, idx = 1;
- while (low <= high){
- mid = (low + high) >> 1;
- if (ccw(ar[0], P, ar[mid]) > 0) low = mid + 1;
- else idx = mid, high = mid - 1;
- }
- if (!strictly && PointOnSeg(Segment(ar[0], ar[n - 1]), P)) return true;
- if (!strictly && PointOnSeg(Segment(ar[idx], ar[idx - 1]), P)) return true;
- if (idx == 1 || ccw(ar[0], P, ar[n - 1]) == 0) return false;
- return (ccw(ar[idx], P, ar[idx - 1]) < 0);
- }
- };
- /// Point Rotations
- struct Point{
- double x, y;
- Point(){
- }
- Point(double xi, double yi){
- x = xi, y = yi;
- }
- };
- double dis(struct Point A, struct Point B){
- double x = A.x - B.x;
- double y = A.y - B.y;
- return sqrt((x * x) + (y * y));
- }
- /// Rotate P anti-clockwise around the centre point by theta (theta in degrees)
- struct Point rotate(struct Point centre, struct Point P, double theta){
- P.x -= centre.x, P.y -= centre.y;
- theta = (theta * 2.0 * acos(0.0)) / 180.0;
- double s = sin(theta), c = cos(theta);
- double x = (P.x * c) - (P.y * s);
- double y = (P.x * s) + (P.y * c);
- return Point(x + centre.x, y + centre.y);
- }
- /// Clockwise rotation from vector A to vector B
- double thetaX(struct Point A, struct Point B){
- double dot = (B.x * A.x) + (B.y * A.y);
- double det = (B.x * A.y) - (B.y * A.x);
- double theta = (atan2(det, dot) * 180.0) / (2.0 * acos(0.0));
- if (theta < 0) theta += 360.0;
- return theta;
- }
- /// Simple Polygon From Points
- struct Point{
- int idx, x, y, d;
- double theta;
- };
- int n;
- struct Point ar[2010];
- double centre_x, centre_y;
- int compare(const void* a, const void* b){
- struct Point P1 = *(struct Point*)a;
- struct Point P2 = *(struct Point*)b;
- if (fabs(P1.theta - P2.theta) <= 1e-9){
- double d1 = ((centre_x - P1.x) * (centre_x - P1.x)) + ((centre_y - P1.y) + (centre_y - P1.y));
- double d2 = ((centre_x - P2.x) * (centre_x - P2.x)) + ((centre_y - P2.y) + (centre_y - P2.y));
- if (fabs(d1 - d2) <= 1e-9) return 0;
- else if (d1 < d2) return -1;
- else return +1;
- }
- else if (P1.theta < P2.theta) return +1;
- else return -1;
- }
- int main(){
- int t, line, i, j;
- scanf("%d", &t);
- for (line = 1; line <= t; line++){
- scanf("%d", &n);
- centre_x = 0.0, centre_y = 0.0;
- for (i = 0; i < n; i++){
- scanf("%d %d", &ar[i].x, &ar[i].y);
- ar[i].idx = i;
- centre_x += ar[i].x;
- centre_y += ar[i].y;
- }
- centre_x /= (1.0 * n), centre_y /= (1.0 * n);
- for (i = 0; i < n; i++) ar[i].theta = atan2((double)ar[i].y - centre_y, (double)ar[i].x - centre_x);
- qsort(ar, n, sizeof(struct Point), compare);
- for (i = 0; i < n; i++){
- if (i != 0) putchar(32);
- printf("%d", ar[i].idx);
- }
- puts("");
- }
- return 0;
- }
- /// Convex Hull
- using namespace std;
- struct Point {
- long long x, y; /// x*x or y*y should fit long long because of cross() function
- Point(){}
- Point(long long a, long long b){
- x = a, y = b;
- }
- inline bool operator < (const Point &p) const {
- return ((x < p.x) || (x == p.x && y < p.y));
- }
- };
- long long cross(const Point &O, const Point &A, const Point &B){
- return ((A.x - O.x) * (B.y - O.y)) - ((A.y - O.y) * (B.x - O.x));
- }
- bool isConvex(vector <Point> P){ /// Polygon P convex or not, P is given in clock-wise of anti-clockwise order
- int n = P.size();
- ///sort(P.begin(), P.end());
- if (n <= 2) return false; /// Line or point is not convex
- n++, P.push_back(P[0]); /// Last point = first point
- bool flag = (cross(P[0], P[1], P[2]) > 0);
- for (int i = 1; (i + 1) < n; i++){
- bool cmp = (cross(P[i], P[i + 1], (i + 2) == n ? P[1] : P[i + 2]) > 0);
- if (cmp ^ flag) return false;
- }
- return true;
- }
- /// Convex hull using the monotone chain algorithm
- vector <Point> convex_hull (vector<Point> P){
- int i, t, k = 0, n = P.size();
- vector<Point> H(n << 1);
- sort(P.begin(), P.end()); /// Can be converted to O(n) using radix sort
- for (i = 0; i < n; i++) {
- while (k >= 2 && cross(H[k - 2], H[k - 1], P[i]) < 0) k--;
- H[k++] = P[i];
- }
- for (i = n - 2, t = k + 1; i >= 0; i--) {
- while (k >= t && cross(H[k - 2], H[k - 1], P[i]) < 0) k--;
- H[k++] = P[i];
- }
- H.resize(k - 1); /// The last point is the same as the first in this implementation
- return H;
- }
- /// Circle Circle Intersection Area
- #define sqr(x) ((x) * (x))
- struct Point{
- double x, y;
- Point() {
- }
- Point(double a, double b){
- x = a, y = b;
- }
- };
- struct Circle{
- Point centre;
- double radius;
- Circle(){
- }
- Circle(double a, double b, double c){
- radius = c;
- centre = Point(a, b);
- }
- Circle(struct Point c, double r){
- centre = c;
- radius = r;
- }
- };
- const double pi = 2.0 * acos(0.0);
- double dis(Point A, Point B){
- double x = (A.x - B.x);
- double y = (A.y - B.y);
- double res = sqrt((x * x) + (y * y));
- return res;
- }
- double Area(Circle C){
- return (pi * sqr(C.radius));
- }
- double intersection(Circle A, Circle B){
- double x = A.radius + B.radius;
- double y = dis(A.centre, B.centre);
- if ((fabs(x - y) <= 1e-8) || (y > x)) return 0.0;
- double c = y;
- double x0 = A.centre.x, y0 = A.centre.y, r0 = A.radius;
- double x1 = B.centre.x, y1 = B.centre.y, r1 = B.radius;
- x = (sqr(r1) + sqr(c) - sqr(r0)) / (2.0 * r1 * c);
- double CBD = acos(x) * 2.0;
- y = (sqr(r0) + sqr(c) - sqr(r1)) / (2.0 * r0 * c);
- double CAD = acos(y) * 2.0;
- double res = (0.5 * CBD * sqr(r1)) - (0.5 * sqr(r1) * sin(CBD)) + (0.5 * CAD * sqr(r0)) - (0.5 * sqr(r0) * sin(CAD));
- return res;
- }
- bool Inside(Circle A, Circle B){
- double x = dis(A.centre, B.centre) + B.radius;
- if ((fabs(x - A.radius) <= 1e-8) || (x < A.radius)) return true;
- return false;
- }
- int main(){
- double res;
- int T = 0, t, i, a, b, c;
- scanf("%d", &t);
- while (t--){
- scanf("%d %d %d", &a, &b, &c);
- Circle grass = Circle(a, b, c);
- scanf("%d %d %d", &a, &b, &c);
- Circle wolf = Circle(a, b, c);
- if (Inside(wolf, grass)) res = 0.0;
- else if (Inside(grass, wolf)) res = Area(grass) - Area(wolf);
- else{
- res = Area(grass) - intersection(grass, wolf);
- }
- printf("Case #%d: %0.12f\n", ++T, res + 1e-15);
- }
- return 0;
- }
Add Comment
Please, Sign In to add comment