#include <algorithm>
#include <array>
#include <assert.h>
#include <bitset>
#include <chrono>
#include <cmath>
#include <complex>
#include <cstring>
#include <functional>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <istream>

#include <map>
#include <math.h>
#include <numeric>
#include <ostream>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>

namespace asl
{
    
    template <typename numeric_type>
    class point
    {
    public:
        numeric_type x, y;

        point(numeric_type x = 0, numeric_type y = 0) : x(x), y(y) {}

        numeric_type real() const
        {
            return x;
        }

        numeric_type imag() const
        {
            return y;
        }

    };

    template <typename numeric_type>
    std::istream &operator>>(std::istream &is, point<numeric_type> &p)
    {
        return is >> p.x >> p.y;
    }

    template <const int eps_exp = 9>
    int sign(double v)
    {
        const double eps = std::pow(10, -eps_exp);

        if (v < -eps)
            return -1;
        else if (v > eps)
            return +1;
        else
            return 0;
    }

#include <stdint.h>

    template <typename T>
    std::istream &operator>>(std::istream &is, std::vector<T> &vec)
    {
        for (auto &value : vec)
            is >> value;
        return is;
    }

} 

#include <experimental/optional>

#define _NEXT(i) (((i) + 1) == n ? 0 : ((i) + 1))

namespace asl
{
    template <typename numeric_type>
    using polygon = std::vector<point<numeric_type>>;

#define x(_t) (xa + (_t)*a)
#define y(_t) (ya + (_t)*b)

    double radian(double xa, double ya, double xb, double yb)
    {
        return atan2(xa * yb - xb * ya, xa * xb + ya * yb);
    }

    double part(double xa, double ya, double xb, double yb, double r)
    {
        double l = sqrt((xa - xb) * (xa - xb) + (ya - yb) * (ya - yb));
        double a = (xb - xa) / l, b = (yb - ya) / l, c = a * xa + b * ya;
        double d = 4.0 * (c * c - xa * xa - ya * ya + r * r);
        if (sign(d) <= 0)
            return radian(xa, ya, xb, yb) * r * r * 0.5;
        else
        {
            d = sqrt(d) * 0.5;
            double s = -c - d, t = -c + d;
            if (s < 0.0)
                s = 0.0;
            else if (s > l)
                s = l;
            if (t < 0.0)
                t = 0.0;
            else if (t > l)
                t = l;
            return (x(s) * y(t) - x(t) * y(s) + (radian(xa, ya, x(s), y(s)) + radian(x(t), y(t), xb, yb)) * r * r) * 0.5;
        }
    }

    double intersection_circle_polygon(const polygon<double> &P, double r)
    {
        double s = 0.0;
        int n = P.size();
        for (int i = 0; i < n; i++)
            s += part(P[i].real(), P[i].imag(),
                      P[_NEXT(i)].real(), P[_NEXT(i)].imag(), r);
        return fabs(s);
    }

} 

#include <random>

#define endl '\n'

using namespace asl;
using namespace std;

int main()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.precision(12);
    cout << fixed;

    int n;
    double r;
    cin >> n >> r;

    polygon<double> p(n);

    cin >> p;

    cout << intersection_circle_polygon(p, r) << endl;

    return 0;
}





Measured memory_bytes: 3548