codeforces 600D Area of Two Circles' Intersection

分相离,内含,想交三种情况讨论一下。

主要是精度和数据范围的问题,首先数据用long double,能用整型判断就不要用浮点型。

题目中所给的坐标,半径是整型的,出现卡浮点判断的情况还是比较少的。

最后算三角型面积的时候不要用海伦公式,有四个连乘很容易爆数据范围损失精度,即使拆开两两乘也要考虑符号

(取对数也是比较好的办法)。(不知道sqrt和cos,sin的精度如何比较

#include<bits/stdc++.h>
using namespace std;

typedef long double ld;
typedef long long ll;

ld x[2],y[2],r[2];

inline ld sqr(ld x){ return x*x; }
inline ll sqrl(ll x) { return x*x; }
inline ld fcos(ld a, ld b, ld c)
{
    return acosl((a*a+b*b-c*c)/(2*a*b));
}

inline ld cut(ld ang, ld r)
{
    ld s1 = ang*r*r;
    ld s2 = sinl(ang)*cosl(ang)*r*r;
    return s1 - s2;
}

const ld pi = acosl(-1);

double solve()
{
    if(r[0] > r[1]){
        swap(r[0],r[1]);
        swap(x[0],x[1]);
        swap(y[0],y[1]);
    }
    ll dx = x[0]-x[1], dy = y[0]-y[1];
    ll ds = sqrl(dx)+sqrl(dy);
    if(ds >= sqrl(r[0]+r[1])) return 0;
    ld d = sqrtl(ds);
    if(d+r[0] <= r[1]) return sqr(r[0])*pi;

    ld ang[2];
    ang[0] = fcos(d,r[0],r[1]);
    ang[1] = fcos(d,r[1],r[0]);
    /*
    WA 28
    ld area = ang[1]*sqr(r[1]) + ang[0]*sqr(r[0]);
    ld s = (d+r[0]+r[1])/2;
    area -= sqrtl(s*(s-d)*(s-r[0])*(s-r[1]))*2; // O(n^4) 拆分会有符号问题 对数也许可行
    return area;
    */
    return cut(ang[0],r[0]) + cut(ang[1],r[1]);
}

//#define LOCAL
int main()
{
#ifdef LOCAL
    freopen("in.txt","r",stdin);
#endif
    cin>>x[0]>>y[0]>>r[0]>>x[1]>>y[1]>>r[1];
    printf("%.10f\n", solve());
    return 0;
}

codeforces 600D Area of Two Circles' Intersection

时间: 2024-08-05 11:16:23

codeforces 600D Area of Two Circles' Intersection的相关文章

codeforce--600D - Area of Two Circles&#39; Intersection

题意:求相交圆的面积.借鉴大神代码,精度超高. 1 #include <fstream> 2 #include <iostream> 3 #include <string> 4 #include <complex> 5 #include <math.h> 6 #include <set> 7 #include <vector> 8 #include <map> 9 #include <queue>

Codeforces Round #513 D - Social Circles(贪心)

题目大意: 有n(n≤105)个人排成一圈,第i个人要求自己左边空出li个座位,右边空出ri(li,ri≤109)个座位.问最少需要安排多少个座位.思路: 一开始先假设每个人都占了li+ri+1个位置.考虑怎样安排相邻人的顺序,并合并相邻人的li,ri使得答案最优.将所有li,ri分别排序,将对应的li,ri合并一定是最优的(想一想这是为什么). #include <iostream> #include <iomanip> #include <algorithm> #i

codeforces 几道题目

BZOJ挂了....明天就要出发去GDKOI了....不能弃疗. 于是在cf水了几道题, 写写详(jian)细(dan)题解, 攒攒RP, 希望GDKOI能好好发挥.......  620E. New Year Tree 题目大意: N个结点的树, 结点1为根, 要支持2种操作(M个操作): 1.将以v为根的子树所有节点的颜色为c 2.询问以v为根的子树中不同颜色个数 N,M<=4*10^5, 1<=c<=60 题解: 处理出dfs序, 线段树维护. 1,2操作都对应线段树的一段区间(子

2017-5-22-Train:Educational Codeforces Round 2

B. Queries about less or equal elements(二分) You are given two arrays of integers a and b. For each element of the second array b**j you should find the number of elements in array athat are less than or equal to the value b**j. Input The first line c

Trilateration三边测量定位算法

转载自Jiaxing / 2014年2月22日 基本原理 Trilateration(三边测量)是一种常用的定位算法: 已知三点位置 (x1, y1), (x2, y2), (x3, y3) 已知未知点 (x0, y0) 到三点距离 d1, d2, d3 以 d1, d2, d3 为半径作三个圆,根据毕达哥拉斯定理,得出交点即未知点的位置计算公式: ( x1 - x0 )2 + ( y1 - y0 )2 = d12 ( x2 - x0 )2 + ( y2 - y0 )2 = d22 ( x3 -

HDOJ 1724 Ellipse 自适应Simpson

自适应Simpson Ellipse Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 1268    Accepted Submission(s): 493 Problem Description Math is important!! Many students failed in 2+2's mathematical test, s

HDU 1724 Ellipse 【自适应Simpson积分】

Ellipse Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 1977    Accepted Submission(s): 832 Problem Description Math is important!! Many students failed in 2+2's mathematical test, so let's AC t

HDU 1724 Ellipse [辛普森积分]

Ellipse Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 1868    Accepted Submission(s): 792 Problem Description Math is important!! Many students failed in 2+2’s mathematical test, so let's AC t

A Statistical View of Deep Learning (IV): Recurrent Nets and Dynamical Systems

A Statistical View of Deep Learning (IV): Recurrent Nets and Dynamical Systems Recurrent neural networks (RNNs) are now established as one of the key tools in the machine learning toolbox for handling large-scale sequence data. The ability to specify