noya2_Library

This documentation is automatically generated by online-judge-tools/verification-helper

View the Project on GitHub noya2ruler/noya2_Library

:heavy_check_mark: geometry/partition_by_circle.hpp

Depends on

Verified with

Code

#pragma once

#include"../template/template.hpp"
#include"base_ld.hpp"
#include"../data_structure/dsu.hpp"

namespace noya2{

using vec = complex<ld>;

struct circle {
    vec c;
    ld r;
};

// c1 != c2
vector<ld> cross_point_x(const circle &c1, const circle &c2){
    assert(sgn(abs(c1.r - c2.r)) != 0 || sgn(abs(c1.c - c2.c)) != 0);
    ld d = abs(c1.c - c2.c);
    // 円が離れすぎている
    if (sgn(d - c1.r - c2.r) > 0) return {};
    // 円が近すぎる
    if (sgn(abs(c1.r - c2.r) - d) > 0) return {};
    // 外接している
    if (sgn(d - c1.r - c2.r) == 0){
        return {(c1.r*c2.c.real() + c2.r*c1.c.real()) / (c1.r + c2.r)};
    }
    // 内接している
    if (sgn(abs(c1.r - c2.r) - d) == 0){
        return {(c1.r * c2.c.real() - c2.r * c1.c.real()) / (c1.r - c2.r)};
    }
    // 2 点を共有する
    ld e = (d * d + c1.r * c1.r - c2.r * c2.r) / (2 * d);
    vec p = c1.c + (c2.c - c1.c) * e / d;
    vec v((c1.c - c2.c).imag(),(c2.c - c1.c).real());
    v *= sqrtl(max(c1.r * c1.r - e * e, ld(0))) / abs(v);
    return {(p+v).real(),(p-v).real()};
}

vector<ld> cross_point_y(const circle &c, const ld &x){
    int cond = sgn(abs(c.c.real() - x) - c.r); 
    if (cond > 0) return {};
    if (cond == 0) return {c.c.imag()};
    ld ydiff = sqrtl(max(c.r*c.r - (x - c.c.real()) * (x - c.c.real()),ld(0)));
    return {c.c.imag()+ydiff,c.c.imag()-ydiff};
}

struct Partition_by_Circles {
    vector<circle> a;
    // 同じ円は与えられない
    void add_circle(circle c){
        // x 座標で区切る場所が重ならないように 1 rad 回転しておく
        c.c = rot(c.c,1);
        a.emplace_back(c);
    }
    ld coordinate_width(){
        ld res = 0.0;
        for (auto &c : a){
            chmax(res,abs(c.c)+c.r);
        }
        return res;
    }
    vector<ld> x_coordinates(){
        vector<ld> xs;
        for (auto &c : a){
            xs.emplace_back(c.c.real()-c.r);
            xs.emplace_back(c.c.real()+c.r);
        }
        int siza = a.size();
        for (int i = 0; i < siza-1; i++) for (int j = i+1; j < siza; j++){
            for (auto x : cross_point_x(a[i],a[j])){
                bool yet = true;
                for (auto testx : xs){
                    if (sgn(abs(x - testx)) == 0){
                        yet = false;
                        break;
                    }
                }
                if (yet) xs.emplace_back(x);
            }
        }
        sort(all(xs));
        return xs;
    }
    using ylr = tuple<ld,int,int>;
    vector<ylr> y_coordinates(ld x){
        vector<ylr> ys;
        auto add = [&](ld y, int l, int r){
            bool yet = true;
            for (auto &[testy, testl, testr] : ys){
                if (sgn(abs(y - testy)) == 0){
                    yet = false;
                    testl += l;
                    testr += r;
                    break;
                }
            }
            if (yet) ys.push_back({y,l,r});
        };
        for (auto &c : a){
            auto vecy = cross_point_y(c,x);
            if (vecy.empty()) continue;
            if ((int)(vecy.size()) == 2){
                for (auto y : vecy) add(y,1,1);
            }
            else {
                if (c.c.real() < x){
                    add(vecy[0],2,0);
                }
                else {
                    add(vecy[0],0,2);
                }
            }
        }
        sort(all(ys),[&](ylr lhs, ylr rhs){
            return get<0>(lhs) < get<0>(rhs);
        });
        return ys;
    }
    dsu d;
    vector<ld> xs;
    vector<int> lower_idx;
    map<int,int> mp;
    vector<vector<int>> build_graph(ld max_width = 0){
        // 大きな円で全体を囲ってしまう
        if (sgn(max_width) == 0) max_width = coordinate_width()*2;
        a.push_back({vec(0,0),max_width});
        // x 座標の列挙
        xs = x_coordinates();
        // 各 x について y 座標の列挙
        vector<vector<ylr>> yss; yss.reserve(xs.size());
        for (auto &x : xs) yss.push_back(y_coordinates(x));
        // x で切って領域を舐める
        int idx = 0;
        vector<pair<int,int>> es, merge;
        vector<int> cur;
        for (int itr = 0; itr < (int)(xs.size())-1; itr++){
            lower_idx.emplace_back(idx);
            vector<int> lid, rid;
            for (int l = 0; l < (int)(yss[itr].size()); l++){
                for (int t = 0; t < get<2>(yss[itr][l]); t++){
                    lid.emplace_back(l);
                }
            }
            for (int r = 0; r < (int)(yss[itr+1].size()); r++){
                for (int t = 0; t < get<1>(yss[itr+1][r]); t++){
                    rid.emplace_back(r);
                }
            }
            assert(lid.size() == rid.size());
            int pre = 0;
            vector<int> nxt;
            for (int i = 0; i < (int)(lid.size())-1; i++){
                for (int t = 0; t < rid[i+1]-rid[i]; t++){
                    nxt.emplace_back(idx);
                }
                if (i != 0){
                    es.emplace_back(idx-1,idx);
                }
                if (lid[i+1] - lid[i] != 0){
                    merge.emplace_back(cur[pre++],idx);
                }
                if (lid[i+1] - lid[i] == 2){
                    merge.emplace_back(cur[pre++],idx);
                }
                idx++;
            }
            swap(cur,nxt);
        }
        // グラフを構築
        d = dsu(idx);
        for (auto &[u, v] : merge) d.merge(u,v);
        set<int> st;
        for (int i = 0; i < idx; i++) st.insert(d.leader(i));
        int vid = 0;
        for (auto v : st) mp[v] = vid++;
        vector<vector<int>> graph(vid);
        for (auto [u, v] : es){
            u = mp[d.leader(u)];
            v = mp[d.leader(v)];
            graph[u].emplace_back(v);
            graph[v].emplace_back(u);
        }
        return graph;
    }
    int get_area_idx(vec p){
        p = rot(p,1);
        int xid = int(lower_bound(all(xs),p.real()) - xs.begin()) - 1;
        vector<ld> ys;
        for (auto &[y, l, r] : y_coordinates(p.real())) ys.emplace_back(y);
        int yid = int(lower_bound(all(ys),p.imag()) - ys.begin()) - 1;
        return mp[d.leader(lower_idx[xid] + yid)];
    }
};

} //namespace noya2
#line 2 "geometry/partition_by_circle.hpp"

#line 2 "template/template.hpp"
using namespace std;

#include<bits/stdc++.h>
#line 1 "template/inout_old.hpp"
namespace noya2 {

template <typename T, typename U>
ostream &operator<<(ostream &os, const pair<T, U> &p){
    os << p.first << " " << p.second;
    return os;
}
template <typename T, typename U>
istream &operator>>(istream &is, pair<T, U> &p){
    is >> p.first >> p.second;
    return is;
}

template <typename T>
ostream &operator<<(ostream &os, const vector<T> &v){
    int s = (int)v.size();
    for (int i = 0; i < s; i++) os << (i ? " " : "") << v[i];
    return os;
}
template <typename T>
istream &operator>>(istream &is, vector<T> &v){
    for (auto &x : v) is >> x;
    return is;
}

void in() {}
template <typename T, class... U>
void in(T &t, U &...u){
    cin >> t;
    in(u...);
}

void out() { cout << "\n"; }
template <typename T, class... U, char sep = ' '>
void out(const T &t, const U &...u){
    cout << t;
    if (sizeof...(u)) cout << sep;
    out(u...);
}

template<typename T>
void out(const vector<vector<T>> &vv){
    int s = (int)vv.size();
    for (int i = 0; i < s; i++) out(vv[i]);
}

struct IoSetup {
    IoSetup(){
        cin.tie(nullptr);
        ios::sync_with_stdio(false);
        cout << fixed << setprecision(15);
        cerr << fixed << setprecision(7);
    }
} iosetup_noya2;

} // namespace noya2
#line 1 "template/const.hpp"
namespace noya2{

const int iinf = 1'000'000'007;
const long long linf = 2'000'000'000'000'000'000LL;
const long long mod998 =  998244353;
const long long mod107 = 1000000007;
const long double pi = 3.14159265358979323;
const vector<int> dx = {0,1,0,-1,1,1,-1,-1};
const vector<int> dy = {1,0,-1,0,1,-1,-1,1};
const string ALP = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
const string alp = "abcdefghijklmnopqrstuvwxyz";
const string NUM = "0123456789";

void yes(){ cout << "Yes\n"; }
void no(){ cout << "No\n"; }
void YES(){ cout << "YES\n"; }
void NO(){ cout << "NO\n"; }
void yn(bool t){ t ? yes() : no(); }
void YN(bool t){ t ? YES() : NO(); }

} // namespace noya2
#line 2 "template/utils.hpp"

#line 6 "template/utils.hpp"

namespace noya2{

unsigned long long inner_binary_gcd(unsigned long long a, unsigned long long b){
    if (a == 0 || b == 0) return a + b;
    int n = __builtin_ctzll(a); a >>= n;
    int m = __builtin_ctzll(b); b >>= m;
    while (a != b) {
        int mm = __builtin_ctzll(a - b);
        bool f = a > b;
        unsigned long long c = f ? a : b;
        b = f ? b : a;
        a = (c - b) >> mm;
    }
    return a << std::min(n, m);
}

template<typename T> T gcd_fast(T a, T b){ return static_cast<T>(inner_binary_gcd(std::abs(a),std::abs(b))); }

long long sqrt_fast(long long n) {
    if (n <= 0) return 0;
    long long x = sqrt(n);
    while ((x + 1) * (x + 1) <= n) x++;
    while (x * x > n) x--;
    return x;
}

template<typename T> T floor_div(const T n, const T d) {
    assert(d != 0);
    return n / d - static_cast<T>((n ^ d) < 0 && n % d != 0);
}

template<typename T> T ceil_div(const T n, const T d) {
    assert(d != 0);
    return n / d + static_cast<T>((n ^ d) >= 0 && n % d != 0);
}

template<typename T> void uniq(std::vector<T> &v){
    std::sort(v.begin(),v.end());
    v.erase(unique(v.begin(),v.end()),v.end());
}

template <typename T, typename U> inline bool chmin(T &x, U y) { return (y < x) ? (x = y, true) : false; }

template <typename T, typename U> inline bool chmax(T &x, U y) { return (x < y) ? (x = y, true) : false; }

template<typename T> inline bool range(T l, T x, T r){ return l <= x && x < r; }

} // namespace noya2
#line 8 "template/template.hpp"

#define rep(i,n) for (int i = 0; i < (int)(n); i++)
#define repp(i,m,n) for (int i = (m); i < (int)(n); i++)
#define reb(i,n) for (int i = (int)(n-1); i >= 0; i--)
#define all(v) (v).begin(),(v).end()

using ll = long long;
using ld = long double;
using uint = unsigned int;
using ull = unsigned long long;
using pii = pair<int,int>;
using pll = pair<ll,ll>;
using pil = pair<int,ll>;
using pli = pair<ll,int>;

namespace noya2{

/* ~ (. _________ . /) */

}

using namespace noya2;


#line 2 "geometry/base_ld.hpp"

#line 4 "geometry/base_ld.hpp"

namespace noya2 {

using vec = complex<ld>;

const ld PI = acos(-1);

int sgn(ld a, const ld eps = 1e-7) {
    return (a < -eps) ? -1 : (a > eps) ? 1 : 0;
}

bool same_vec(vec a, vec b) {
    a -= b;
    return sgn(a.real()) == 0 && sgn(a.imag()) == 0;
}

ld dot(const vec &a, const vec &b) {
    return (conj(a) * b).real();
}

ld cross(const vec &a, const vec &b) {
    return (conj(a) * b).imag();
}

int isp(const vec &a, const vec &b, const vec &c) {
    int cross_sgn = sgn(cross(b - a, c - a));
    if (cross_sgn == 0) {
        if (sgn(dot(b - a, c - a)) < 0) return -2;
        if (sgn(dot(a - b, c - b)) < 0) return 2;
    }
    return cross_sgn;
}

vec rot90(const vec &a) {
    return {-a.imag(), a.real()};
}

vec rot(const vec &a, ld rad) {
    return a * vec(cosl(rad), sinl(rad));
}


}  // namespace lib
#line 2 "data_structure/dsu.hpp"

#line 6 "data_structure/dsu.hpp"

namespace noya2{

struct dsu {
  public:
    dsu() : _n(0) {}
    dsu(int n) : _n(n), parent_or_size(n, -1) {}

    int merge(int a, int b) {
        assert(0 <= a && a < _n);
        assert(0 <= b && b < _n);
        int x = leader(a), y = leader(b);
        if (x == y) return x;
        if (-parent_or_size[x] < -parent_or_size[y]) std::swap(x, y);
        parent_or_size[x] += parent_or_size[y];
        parent_or_size[y] = x;
        return x;
    }

    bool same(int a, int b) {
        assert(0 <= a && a < _n);
        assert(0 <= b && b < _n);
        return leader(a) == leader(b);
    }

    int leader(int a) {
        assert(0 <= a && a < _n);
        if (parent_or_size[a] < 0) return a;
        return parent_or_size[a] = leader(parent_or_size[a]);
    }

    int size(int a) {
        assert(0 <= a && a < _n);
        return -parent_or_size[leader(a)];
    }

    std::vector<std::vector<int>> groups() {
        std::vector<int> leader_buf(_n), group_size(_n);
        for (int i = 0; i < _n; i++) {
            leader_buf[i] = leader(i);
            group_size[leader_buf[i]]++;
        }
        std::vector<std::vector<int>> result(_n);
        for (int i = 0; i < _n; i++) {
            result[i].reserve(group_size[i]);
        }
        for (int i = 0; i < _n; i++) {
            result[leader_buf[i]].push_back(i);
        }
        result.erase(
            std::remove_if(result.begin(), result.end(),
                           [&](const std::vector<int>& v) { return v.empty(); }),
            result.end());
        return result;
    }

  private:
    int _n;
    // root node: -1 * component size
    // otherwise: parent
    std::vector<int> parent_or_size;
};

} // namespace noya2
#line 6 "geometry/partition_by_circle.hpp"

namespace noya2{

using vec = complex<ld>;

struct circle {
    vec c;
    ld r;
};

// c1 != c2
vector<ld> cross_point_x(const circle &c1, const circle &c2){
    assert(sgn(abs(c1.r - c2.r)) != 0 || sgn(abs(c1.c - c2.c)) != 0);
    ld d = abs(c1.c - c2.c);
    // 円が離れすぎている
    if (sgn(d - c1.r - c2.r) > 0) return {};
    // 円が近すぎる
    if (sgn(abs(c1.r - c2.r) - d) > 0) return {};
    // 外接している
    if (sgn(d - c1.r - c2.r) == 0){
        return {(c1.r*c2.c.real() + c2.r*c1.c.real()) / (c1.r + c2.r)};
    }
    // 内接している
    if (sgn(abs(c1.r - c2.r) - d) == 0){
        return {(c1.r * c2.c.real() - c2.r * c1.c.real()) / (c1.r - c2.r)};
    }
    // 2 点を共有する
    ld e = (d * d + c1.r * c1.r - c2.r * c2.r) / (2 * d);
    vec p = c1.c + (c2.c - c1.c) * e / d;
    vec v((c1.c - c2.c).imag(),(c2.c - c1.c).real());
    v *= sqrtl(max(c1.r * c1.r - e * e, ld(0))) / abs(v);
    return {(p+v).real(),(p-v).real()};
}

vector<ld> cross_point_y(const circle &c, const ld &x){
    int cond = sgn(abs(c.c.real() - x) - c.r); 
    if (cond > 0) return {};
    if (cond == 0) return {c.c.imag()};
    ld ydiff = sqrtl(max(c.r*c.r - (x - c.c.real()) * (x - c.c.real()),ld(0)));
    return {c.c.imag()+ydiff,c.c.imag()-ydiff};
}

struct Partition_by_Circles {
    vector<circle> a;
    // 同じ円は与えられない
    void add_circle(circle c){
        // x 座標で区切る場所が重ならないように 1 rad 回転しておく
        c.c = rot(c.c,1);
        a.emplace_back(c);
    }
    ld coordinate_width(){
        ld res = 0.0;
        for (auto &c : a){
            chmax(res,abs(c.c)+c.r);
        }
        return res;
    }
    vector<ld> x_coordinates(){
        vector<ld> xs;
        for (auto &c : a){
            xs.emplace_back(c.c.real()-c.r);
            xs.emplace_back(c.c.real()+c.r);
        }
        int siza = a.size();
        for (int i = 0; i < siza-1; i++) for (int j = i+1; j < siza; j++){
            for (auto x : cross_point_x(a[i],a[j])){
                bool yet = true;
                for (auto testx : xs){
                    if (sgn(abs(x - testx)) == 0){
                        yet = false;
                        break;
                    }
                }
                if (yet) xs.emplace_back(x);
            }
        }
        sort(all(xs));
        return xs;
    }
    using ylr = tuple<ld,int,int>;
    vector<ylr> y_coordinates(ld x){
        vector<ylr> ys;
        auto add = [&](ld y, int l, int r){
            bool yet = true;
            for (auto &[testy, testl, testr] : ys){
                if (sgn(abs(y - testy)) == 0){
                    yet = false;
                    testl += l;
                    testr += r;
                    break;
                }
            }
            if (yet) ys.push_back({y,l,r});
        };
        for (auto &c : a){
            auto vecy = cross_point_y(c,x);
            if (vecy.empty()) continue;
            if ((int)(vecy.size()) == 2){
                for (auto y : vecy) add(y,1,1);
            }
            else {
                if (c.c.real() < x){
                    add(vecy[0],2,0);
                }
                else {
                    add(vecy[0],0,2);
                }
            }
        }
        sort(all(ys),[&](ylr lhs, ylr rhs){
            return get<0>(lhs) < get<0>(rhs);
        });
        return ys;
    }
    dsu d;
    vector<ld> xs;
    vector<int> lower_idx;
    map<int,int> mp;
    vector<vector<int>> build_graph(ld max_width = 0){
        // 大きな円で全体を囲ってしまう
        if (sgn(max_width) == 0) max_width = coordinate_width()*2;
        a.push_back({vec(0,0),max_width});
        // x 座標の列挙
        xs = x_coordinates();
        // 各 x について y 座標の列挙
        vector<vector<ylr>> yss; yss.reserve(xs.size());
        for (auto &x : xs) yss.push_back(y_coordinates(x));
        // x で切って領域を舐める
        int idx = 0;
        vector<pair<int,int>> es, merge;
        vector<int> cur;
        for (int itr = 0; itr < (int)(xs.size())-1; itr++){
            lower_idx.emplace_back(idx);
            vector<int> lid, rid;
            for (int l = 0; l < (int)(yss[itr].size()); l++){
                for (int t = 0; t < get<2>(yss[itr][l]); t++){
                    lid.emplace_back(l);
                }
            }
            for (int r = 0; r < (int)(yss[itr+1].size()); r++){
                for (int t = 0; t < get<1>(yss[itr+1][r]); t++){
                    rid.emplace_back(r);
                }
            }
            assert(lid.size() == rid.size());
            int pre = 0;
            vector<int> nxt;
            for (int i = 0; i < (int)(lid.size())-1; i++){
                for (int t = 0; t < rid[i+1]-rid[i]; t++){
                    nxt.emplace_back(idx);
                }
                if (i != 0){
                    es.emplace_back(idx-1,idx);
                }
                if (lid[i+1] - lid[i] != 0){
                    merge.emplace_back(cur[pre++],idx);
                }
                if (lid[i+1] - lid[i] == 2){
                    merge.emplace_back(cur[pre++],idx);
                }
                idx++;
            }
            swap(cur,nxt);
        }
        // グラフを構築
        d = dsu(idx);
        for (auto &[u, v] : merge) d.merge(u,v);
        set<int> st;
        for (int i = 0; i < idx; i++) st.insert(d.leader(i));
        int vid = 0;
        for (auto v : st) mp[v] = vid++;
        vector<vector<int>> graph(vid);
        for (auto [u, v] : es){
            u = mp[d.leader(u)];
            v = mp[d.leader(v)];
            graph[u].emplace_back(v);
            graph[v].emplace_back(u);
        }
        return graph;
    }
    int get_area_idx(vec p){
        p = rot(p,1);
        int xid = int(lower_bound(all(xs),p.real()) - xs.begin()) - 1;
        vector<ld> ys;
        for (auto &[y, l, r] : y_coordinates(p.real())) ys.emplace_back(y);
        int yid = int(lower_bound(all(ys),p.imag()) - ys.begin()) - 1;
        return mp[d.leader(lower_idx[xid] + yid)];
    }
};

} //namespace noya2
Back to top page