結果

問題 No.2873 Kendall's Tau
ユーザー 👑 bo9chanbo9chan
提出日時 2024-09-06 22:30:44
言語 C++23
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 857 ms / 4,500 ms
コード長 54,188 bytes
コンパイル時間 3,133 ms
コンパイル使用メモリ 216,476 KB
実行使用メモリ 37,120 KB
最終ジャッジ日時 2024-09-06 22:31:00
合計ジャッジ時間 16,145 ms
ジャッジサーバーID
(参考情報)
judge3 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,816 KB
testcase_01 AC 2 ms
6,940 KB
testcase_02 AC 2 ms
6,944 KB
testcase_03 AC 2 ms
6,944 KB
testcase_04 AC 2 ms
6,940 KB
testcase_05 AC 2 ms
6,940 KB
testcase_06 AC 2 ms
6,940 KB
testcase_07 AC 769 ms
26,752 KB
testcase_08 AC 829 ms
35,428 KB
testcase_09 AC 775 ms
27,392 KB
testcase_10 AC 852 ms
37,120 KB
testcase_11 AC 731 ms
26,924 KB
testcase_12 AC 857 ms
36,748 KB
testcase_13 AC 188 ms
11,648 KB
testcase_14 AC 685 ms
33,152 KB
testcase_15 AC 106 ms
9,856 KB
testcase_16 AC 104 ms
8,576 KB
testcase_17 AC 510 ms
21,376 KB
testcase_18 AC 425 ms
21,376 KB
testcase_19 AC 483 ms
21,608 KB
testcase_20 AC 112 ms
9,856 KB
testcase_21 AC 443 ms
20,224 KB
testcase_22 AC 164 ms
12,544 KB
testcase_23 AC 422 ms
20,736 KB
testcase_24 AC 43 ms
6,940 KB
testcase_25 AC 95 ms
8,320 KB
testcase_26 AC 539 ms
21,376 KB
testcase_27 AC 320 ms
17,408 KB
testcase_28 AC 635 ms
26,368 KB
testcase_29 AC 718 ms
33,664 KB
testcase_30 AC 76 ms
7,680 KB
testcase_31 AC 148 ms
9,856 KB
testcase_32 AC 437 ms
19,456 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#ifdef __LOCAL
#include <mytemplate.hpp> // プリコンパイル済みヘッダ ~/local/include/mytemplate.hpp.gch
#else
#include <algorithm>
#include <array>
#include <bitset>
#include <cassert>
#include <climits>
#include <cmath>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <list>
#include <map>
#include <memory>
#include <numeric>
#include <optional>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>
#endif
using namespace std;
// #pragma GCC target "no-avx"  // gcc12.2のバグ回避用。boost使用時はコメントアウトの事
// #include <boost/multiprecision/cpp_int.hpp>
// using bint = boost::multiprecision::cpp_int;
using ll = long long;
using vl = vector<ll>;
using vvl = vector<vl>;
using vb = vector<bool>;
using vvb = vector<vb>;
using vd = vector<double>;
using vvd = vector<vd>;
using vc = vector<char>;
using vvc = vector<vc>;
using pll = pair<ll, ll>;
template <class T> using min_priority_queue = priority_queue<T, vector<T>, greater<T>>;
#define REP1(i, n) REP3(i, 0, n, 1)
#define REP2(i, s, n) REP3(i, s, n, 1)
#define REP3(i, s, n, d) for (ll i = (ll)(s); i < (ll)(n); i += (d))
#define REP_OVERLOAD(e1, e2, e3, e4, NAME, ...) NAME
#define rep(...) REP_OVERLOAD(__VA_ARGS__, REP3, REP2, REP1)(__VA_ARGS__)
#define DEP1(i, n) DEP3(i, n, -1, 1)
#define DEP2(i, n, s) DEP3(i, n, s, 1)
#define DEP3(i, n, s, d) for (ll i = (ll)(n); (ll)(s) < i; i -= (d))
#define DEP_OVERLOAD(e1, e2, e3, e4, NAME, ...) NAME
#define dep(...) DEP_OVERLOAD(__VA_ARGS__, DEP3, DEP2, DEP1)(__VA_ARGS__)
#define fore(e, a) for (auto&& e : (a))
#define len(a) (ll)(a).size()
#define all(a) (a).begin(), (a).end()
#define rall(a) (a).rbegin(), (a).rend()
#define pb push_back
#define eb emplace_back
#define fi first
#define se second
#define th third
#define fo fourth
/*********    C O N S T A N T    **********/
constexpr ll INF = LONG_LONG_MAX / 2 - 10000LL; // 4,611,686,018,427,377,903  ~= 4.6e+18, 19桁
const double PI = acos(-1);
/*********    1 2 8 b i t 整 数  **********/
using int128 = __int128_t;
istream& operator>>(istream& stream, int128& val);
ostream& operator<<(ostream& stream, const int128& val_);
/*********       P A I R       **********/
// 単項演算子(+ -)
template <class T, class U> pair<T, U> operator+(pair<T, U> a);
template <class T, class U> pair<T, U> operator-(pair<T, U> a);
// 複合代入演算子
template <class T, class U> pair<T, U>& operator+=(pair<T, U>& a, const pair<T, U> b);
template <class T, class U> pair<T, U>& operator-=(pair<T, U>& a, const pair<T, U> b);
template <class T, class U> pair<T, U>& operator*=(pair<T, U>& a, const pair<T, U> b);
template <class T, class U> pair<T, U>& operator/=(pair<T, U>& a, const pair<T, U> b);
template <class T, class U> pair<T, U>& operator%=(pair<T, U>& a, const pair<T, U> b);
template <class T, class U, class V> pair<T, U>& operator+=(pair<T, U>& a, const V b);
template <class T, class U, class V> pair<T, U>& operator-=(pair<T, U>& a, const V b);
template <class T, class U, class V> pair<T, U>& operator*=(pair<T, U>& a, const V b);
template <class T, class U, class V> pair<T, U>& operator/=(pair<T, U>& a, const V b);
template <class T, class U, class V> pair<T, U>& operator%=(pair<T, U>& a, const V b);
// 算術演算子
template <class T, class U, class V> pair<T, U> operator+(pair<T, U> a, const V& b);
template <class T, class U, class V> pair<T, U> operator-(pair<T, U> a, const V& b);
template <class T, class U, class V> pair<T, U> operator*(pair<T, U> a, const V& b);
template <class T, class U, class V> pair<T, U> operator/(pair<T, U> a, const V& b);
template <class T, class U, class V> pair<T, U> operator%(pair<T, U> a, const V& b);
// 入出力
template <class T, class U> istream& operator>>(istream& stream, pair<T, U>& a);
template <class T, class U> ostream& operator<<(ostream& stream, const pair<T, U>& a);
/*********       V E C T O R      **********/
// 単項演算子(+ -)
template <class T> vector<T> operator+(vector<T> a);
template <class T> vector<T> operator-(vector<T> a);
// 複合代入演算子
template <class T> vector<T>& operator+=(vector<T>& a, const vector<T> b);
template <class T> vector<T>& operator-=(vector<T>& a, const vector<T> b);
template <class T> vector<T>& operator*=(vector<T>& a, const vector<T> b);
template <class T> vector<T>& operator/=(vector<T>& a, const vector<T> b);
template <class T> vector<T>& operator%=(vector<T>& a, const vector<T> b);
template <class T, class U> vector<T>& operator+=(vector<T>& a, const U b);
template <class T, class U> vector<T>& operator-=(vector<T>& a, const U b);
template <class T, class U> vector<T>& operator*=(vector<T>& a, const U b);
template <class T, class U> vector<T>& operator/=(vector<T>& a, const U b);
template <class T, class U> vector<T>& operator%=(vector<T>& a, const U b);
// 算術演算子
template <class T, class U> vector<T> operator+(vector<T> a, const U& b);
template <class T, class U> vector<T> operator-(vector<T> a, const U& b);
template <class T, class U> vector<T> operator*(vector<T> a, const U& b);
template <class T, class U> vector<T> operator/(vector<T> a, const U& b);
template <class T, class U> vector<T> operator%(vector<T> a, const U& b);
// 入出力
template <class T> istream& operator>>(istream& stream, vector<T>& a);
template <class T> ostream& operator<<(ostream& stream, const vector<T>& v);
template <class T> ostream& operator<<(ostream& stream, const vector<vector<T>>& vv);
// 計算
template <class T> T Sum(const vector<T>& v);
template <class T> T Max(const vector<T>& v);
template <class T> T Min(const vector<T>& v);
template <class T> ll Argmax(const vector<T>& v);
template <class T> ll Argmin(const vector<T>& v);
template <class T, class U> bool Contains(const vector<T>& v, const U& a);
template <class T> void Unique(vector<T>& v);
// 生成
template <class T, class U> vector<T> make_vector(int n, U v);
template <class T, class... Args> auto make_vector(int n, Args... args) {
    auto val = make_vector<T>(args...);
    return make_vector<decltype(val)>(n, move(val));
}
/*********        S E T        **********/
template <class T> istream& operator>>(istream& stream, set<T>& st);
template <class T> ostream& operator<<(ostream& stream, const set<T>& st);
template <class T> T Max(const set<T>& st);
template <class T> T Min(const set<T>& st);
/*********   M U L T I S E T   **********/
template <class T> istream& operator>>(istream& stream, multiset<T>& st);
template <class T> ostream& operator<<(ostream& stream, const multiset<T>& st);
template <class T> T Max(const multiset<T>& st);
template <class T> T Min(const multiset<T>& st);
template <class T> bool EraseOne(multiset<T>& st, const T& x);
/*********       C H A R       **********/
bool IsUpper(const char& c);
bool IsLower(const char& c);
char ToUpper(const char& c);
char ToLower(const char& c);
/*********     S T R I N G     **********/
string ToUpper(const string& s);
string ToLower(const string& s);
bool IsPalindrome(const string& S);
/*********       T R I O       **********/
template <class T1, class T2, class T3> struct trio {
    T1 first;
    T2 second;
    T3 third;
    // コンストラクタ・代入演算子
    trio();
    trio(const T1& x);
    trio(const T1& x, const T2& y, const T3& z);
    trio(const trio& t);
    trio& operator=(const trio& t);
    // 比較演算子
    bool operator<(const trio& t) const;
    bool operator==(const trio& t) const;
    bool operator!=(const trio& t) const;
    bool operator>(const trio& t) const;
    bool operator<=(const trio& t) const;
    bool operator>=(const trio& t) const;
    // 単項演算子(+ -)
    trio operator+() const;
    trio operator-() const;
    // 入出力
    template <class U1, class U2, class U3> friend istream& operator>>(istream&, trio<U1, U2, U3>&);
    template <class U1, class U2, class U3> friend ostream& operator<<(ostream&, const trio<U1, U2, U3>&);
    // `tie(a, b, c) = tll{3, 5, 7}`をするのに必要(コンパイラが警告?を出すのでここで定義)
    operator tuple<T1&, T2&, T3&>() {
        return tie(first, second, third);
    }
};
// 複合代入演算子
template <class T1, class T2, class T3>
trio<T1, T2, T3>& operator+=(trio<T1, T2, T3>& a, const trio<T1, T2, T3> b);
template <class T1, class T2, class T3>
trio<T1, T2, T3>& operator-=(trio<T1, T2, T3>& a, const trio<T1, T2, T3> b);
template <class T1, class T2, class T3>
trio<T1, T2, T3>& operator*=(trio<T1, T2, T3>& a, const trio<T1, T2, T3> b);
template <class T1, class T2, class T3>
trio<T1, T2, T3>& operator/=(trio<T1, T2, T3>& a, const trio<T1, T2, T3> b);
template <class T1, class T2, class T3>
trio<T1, T2, T3>& operator%=(trio<T1, T2, T3>& a, const trio<T1, T2, T3> b);
template <class T1, class T2, class T3, class U> trio<T1, T2, T3>& operator+=(trio<T1, T2, T3>& a, const U b);
template <class T1, class T2, class T3, class U> trio<T1, T2, T3>& operator-=(trio<T1, T2, T3>& a, const U b);
template <class T1, class T2, class T3, class U> trio<T1, T2, T3>& operator*=(trio<T1, T2, T3>& a, const U b);
template <class T1, class T2, class T3, class U> trio<T1, T2, T3>& operator/=(trio<T1, T2, T3>& a, const U b);
template <class T1, class T2, class T3, class U> trio<T1, T2, T3>& operator%=(trio<T1, T2, T3>& a, const U b);
// 算術演算子
template <class T1, class T2, class T3, class U> trio<T1, T2, T3> operator+(trio<T1, T2, T3> a, const U& b);
template <class T1, class T2, class T3, class U> trio<T1, T2, T3> operator-(trio<T1, T2, T3> a, const U& b);
template <class T1, class T2, class T3, class U> trio<T1, T2, T3> operator*(trio<T1, T2, T3> a, const U& b);
template <class T1, class T2, class T3, class U> trio<T1, T2, T3> operator/(trio<T1, T2, T3> a, const U& b);
template <class T1, class T2, class T3, class U> trio<T1, T2, T3> operator%(trio<T1, T2, T3> a, const U& b);
using tll = trio<ll, ll, ll>;
/*********    Q U A R T E T    **********/
template <class T1, class T2, class T3, class T4> struct quartet {
    T1 first;
    T2 second;
    T3 third;
    T4 fourth;
    // コンストラクタ・代入演算子
    quartet();
    quartet(const T1& x);
    quartet(const T1& x, const T2& y, const T3& z, const T4& w);
    quartet(const quartet& t);
    quartet& operator=(const quartet& t);
    // 比較演算子
    bool operator<(const quartet& t) const;
    bool operator==(const quartet& t) const;
    bool operator!=(const quartet& other) const;
    bool operator>(const quartet& other) const;
    bool operator<=(const quartet& other) const;
    bool operator>=(const quartet& other) const;
    // 単項演算子(+ -)
    quartet operator+() const;
    quartet operator-() const;
    // 入出力
    template <class U1, class U2, class U3, class U4>
    friend istream& operator>>(istream&, quartet<U1, U2, U3, U4>&);
    template <class U1, class U2, class U3, class U4>
    friend ostream& operator<<(ostream&, const quartet<U1, U2, U3, U4>&);
    // `tie(a, b, c, d) = qll{3, 5, 7, 9}`をするのに必要(コンパイラが警告?を出すのでここで定義)
    operator tuple<T1&, T2&, T3&, T4&>() {
        return tie(first, second, third, fourth);
    }
};
// 複合代入演算子
template <class T1, class T2, class T3, class T4>
quartet<T1, T2, T3, T4>& operator+=(quartet<T1, T2, T3, T4>& a, const quartet<T1, T2, T3, T4> b);
template <class T1, class T2, class T3, class T4>
quartet<T1, T2, T3, T4>& operator-=(quartet<T1, T2, T3, T4>& a, const quartet<T1, T2, T3, T4> b);
template <class T1, class T2, class T3, class T4>
quartet<T1, T2, T3, T4>& operator*=(quartet<T1, T2, T3, T4>& a, const quartet<T1, T2, T3, T4> b);
template <class T1, class T2, class T3, class T4>
quartet<T1, T2, T3, T4>& operator/=(quartet<T1, T2, T3, T4>& a, const quartet<T1, T2, T3, T4> b);
template <class T1, class T2, class T3, class T4>
quartet<T1, T2, T3, T4>& operator%=(quartet<T1, T2, T3, T4>& a, const quartet<T1, T2, T3, T4> b);
template <class T1, class T2, class T3, class T4, class U>
quartet<T1, T2, T3, T4>& operator+=(quartet<T1, T2, T3, T4>& a, const U b);
template <class T1, class T2, class T3, class T4, class U>
quartet<T1, T2, T3, T4>& operator-=(quartet<T1, T2, T3, T4>& a, const U b);
template <class T1, class T2, class T3, class T4, class U>
quartet<T1, T2, T3, T4>& operator*=(quartet<T1, T2, T3, T4>& a, const U b);
template <class T1, class T2, class T3, class T4, class U>
quartet<T1, T2, T3, T4>& operator/=(quartet<T1, T2, T3, T4>& a, const U b);
template <class T1, class T2, class T3, class T4, class U>
quartet<T1, T2, T3, T4>& operator%=(quartet<T1, T2, T3, T4>& a, const U b);
// 算術演算子
template <class T1, class T2, class T3, class T4, class U>
quartet<T1, T2, T3, T4> operator+(quartet<T1, T2, T3, T4> a, const U& b);
template <class T1, class T2, class T3, class T4, class U>
quartet<T1, T2, T3, T4> operator-(quartet<T1, T2, T3, T4> a, const U& b);
template <class T1, class T2, class T3, class T4, class U>
quartet<T1, T2, T3, T4> operator*(quartet<T1, T2, T3, T4> a, const U& b);
template <class T1, class T2, class T3, class T4, class U>
quartet<T1, T2, T3, T4> operator/(quartet<T1, T2, T3, T4> a, const U& b);
template <class T1, class T2, class T3, class T4, class U>
quartet<T1, T2, T3, T4> operator%(quartet<T1, T2, T3, T4> a, const U& b);
using qll = quartet<ll, ll, ll, ll>;
/*********     R A N D O M     **********/
struct Random {
    mt19937_64 rnd;
    Random();
    Random(ll seed);
    ll randint(ll a, ll b);
    double randreal(double a, double b);
    char randchar(char a, char b);
    template <class T> void shuffle(vector<T>& vec);
};
/*********       T I M E R       **********/
struct Timer {
    int time_limit;
    int start_time;
    Timer(double time_limit_sec);
    bool is_time_up() const;
    double get_elapsed_ratio() const;
};
/*********       P R I N T       **********/
template <class T> void print(const T& e);
template <class H, class... T> void print(const H& h, const T&... t);
template <class... T> void End(const T&... t);
/*********       D E B U G       **********/
#ifdef __LOCAL
#define debug(...)                                                                                           \
    if (DEBUG) do {                                                                                          \
            cout << '[' << #__VA_ARGS__ << "] ";                                                             \
            debug_(__VA_ARGS__);                                                                             \
    } while (0)
#else
#define debug(...)
#endif
void dbg_(const long long& e);
template <class T> void dbg_(const T& e);
template <class T, class U> void dbg_(const pair<T, U>& p);
template <class T1, class T2, class T3> void dbg_(const trio<T1, T2, T3>& t);
template <class T1, class T2, class T3, class T4> void dbg_(const quartet<T1, T2, T3, T4>& t);
template <class T> void debug_(const T& e);
template <class T> void debug_(const vector<T>& v);
template <class T> void debug_(const vector<vector<T>>& vv);
template <class T, class U> void debug_(const map<T, U>& mp);
template <class T, class U> void debug_(const vector<map<T, U>>& vm);
template <class T> void debug_(const set<T>& st);
template <class T> void debug_(const multiset<T>& st);
template <class T> void debug_(const vector<set<T>>& vs);
template <class T> void debug_(const vector<multiset<T>>& vs);
template <class H, class... T> void debug_(const H& h, const T&... t);
/*********       O T H E R S       **********/
template <class T, class U> bool chmin(T& a, U b);
template <class T, class U> bool chmax(T& a, U b);
template <class T, class U> T Mod(const T& a, const U& m);
template <class T, class U> T Ceil(const T& x, const U& y);
template <class T, class U> T Floor(const T& x, const U& y);
template <class T> ll Sign(const T& x);
ll Isqrt(ll n);
ll Comb(ll n, ll r);
template <class T> T Pow(T x, ll n);
template <class T> T Aseries(T a, T d, ll n);
template <class T> T Gseries(T a, T r, ll n);
bool Bit(ll b, int i);
ll Popcount(ll b);
ll Mask(ll n);
ll Mask(ll n, ll m);
pll GetOverlap(ll l1, ll r1, ll l2, ll r2);
bool IsOverlap(ll l1, ll r1, ll l2, ll r2);
void PrintYesNo(bool b);
/*********       M  O  D       **********/
// #include <atcoder/modint>  // https://atcoder.github.io/ac-library/production/document_ja/
// using namespace atcoder;
// using mint = modint998244353;     // modint1000000007;
// istream& operator >>(istream& stream, mint& e) {ll n; stream >> n; e = n; return stream; }
// ostream& operator <<(ostream& stream, const mint& e) { stream << e.val(); return stream; }
// using vm = vector<mint>;
// using vvm = vector<vm>;
/***************************************/
constexpr bool DEBUG = true;

// 参考:   https://pages.soi.ch/soiminar/treap.pdf
// verify: https://yukicoder.me/problems/no/649
//         https://atcoder.jp/contests/abc245/tasks/abc245_e
template <class T> struct TreapMultiSet {
  private:
    struct Node {
        int priority;
        T key;
        ll size = 1;
        ll cnt = 1;
        unique_ptr<Node> left, right;

        Node(T key) : priority(rand()), key(key) {}
    };

    using upn = unique_ptr<Node>;
    upn root;

    // 分離。keyの値が含まれるとき、eq2rightがfalseならl, trueならrに行く。
    pair<upn, upn> split(upn current, const T& key, bool eq2right = false) {
        upn l, r;

        if (!current) return {move(l), move(r)};
        if (key < current->key || (key == current->key && eq2right)) {
            r = move(current);
            tie(l, r->left) = split(move(r->left), key, eq2right);
            r->size -= (!l ? 0 : l->size);
        } else {
            l = move(current);
            tie(l->right, r) = split(move(l->right), key, eq2right);
            l->size -= (!r ? 0 : r->size);
        }
        return {move(l), move(r)};
    }

    // 結合。必要条件: l->key <= r->key
    upn merge(upn l, upn r) {
        if (!l || !r) return l ? move(l) : move(r);
        if (l->priority > r->priority) {
            l->size += r->size;
            l->right = merge(move(l->right), move(r));
            return l;
        } else {
            r->size += l->size;
            r->left = merge(move(l), move(r->left));
            return r;
        }
    }

    // x未満の要素数, eq = trueならx以下
    ll index_(const T& x, bool eq) const {
        if (!root) return 0;
        Node* current = root.get();

        ll cnt = 0;
        while (current) {
            int left_size = !current->left ? 0 : current->left->size;
            if (current->key == x) {
                cnt += left_size;
                if (eq) cnt += current->cnt;
                break;
            } else if (current->key < x) {
                cnt += left_size + current->cnt;
                current = current->right.get();
            } else {
                current = current->left.get();
            }
        }
        return cnt;
    }

    // x未満の最大値, eq = trueならkey以下.
    optional<T> less_(const T& x, bool eq) const {
        if (!root) return nullopt;
        Node* current = root.get();

        T ret = T();
        bool exists = false;
        while (current) {
            if (eq && current->key == x) return x;
            if (current->key < x) {
                ret = current->key;
                exists = true;
                current = current->right.get();
            } else {
                current = current->left.get();
            }
        }
        if (exists)
            return ret;
        else
            return nullopt;
    }

    // x超過の最小値, eq = trueならkey以上.
    optional<T> greater_(const T& x, bool eq) const {
        if (!root) return nullopt;
        Node* current = root.get();

        T ret = T();
        bool exists = false;
        while (current) {
            if (eq && current->key == x) return x;
            if (current->key > x) {
                ret = current->key;
                exists = true;
                current = current->left.get();
            } else {
                current = current->right.get();
            }
        }
        if (exists)
            return ret;
        else
            return nullopt;
    }

  public:
    TreapMultiSet() {}

    TreapMultiSet(const vector<T>& vec) {
        fore(e, vec) insert(e);
    }

    // keyを追加
    void insert(const T& key) {
        unique_ptr<Node> l, r, eq;
        tie(l, r) = split(move(root), key);
        tie(l, eq) = split(move(l), key, true);

        if (eq) {
            eq->cnt++;
            eq->size++;
            root = merge(merge(move(l), move(eq)), move(r));
        } else {
            auto node = make_unique<Node>(key);
            root = merge(merge(move(l), move(node)), move(r));
        }
    }

    // keyを消去。消去前にkeyが存在したかを返す
    bool erase(const T& key) {
        unique_ptr<Node> l, r, eq;
        tie(l, r) = split(move(root), key);
        tie(l, eq) = split(move(l), key, true);

        bool key_exists = (bool)eq;
        if (eq) {
            if (eq->cnt == 1) { // keyが1個しかないときはNodeを削除
                eq = merge(move(eq->left), move(eq->right));
            } else {
                eq->cnt--;
                eq->size--;
            }
        }
        root = merge(merge(move(l), move(eq)), move(r));

        return key_exists;
    }

    // 値xが含まれるかどうか
    bool contains(const T& x) const {
        return count(x) > 0;
    }

    // 要素数
    ll size() const {
        if (!root) return 0;
        return root->size;
    }

    // xの要素数
    ll count(const T& x) const {
        if (!root) return 0;
        Node* current = root.get();

        while (current) {
            if (x == current->key)
                return current->cnt;
            else if (x < current->key)
                current = current->left.get();
            else
                current = current->right.get();
        }
        return 0;
    }

    // index番目の値(0-origin)
    T operator[](ll index) const {
        assert(root);
        assert(0 <= index && index < root->size);
        Node* current = root.get();

        while (current) {
            ll left_size = (!current->left ? 0 : current->left->size);

            if (index < left_size) {
                current = current->left.get();
            } else if (index >= left_size + current->cnt) {
                index -= left_size + current->cnt;
                current = current->right.get();
            } else {
                break;
            }
        }
        return current->key;
    }

    // x未満の要素数
    ll index(const T& x) const {
        return index_(x, false);
    }

    // x以下の要素数
    ll index_right(const T& x) const {
        return index_(x, true);
    }

    // l以上r未満の要素数
    ll count_range(const T& l, const T& r) const {
        assert(l <= r);
        return index_(r, false) - index_(l, false);
    }

    // x未満の最大値
    optional<T> lt(const T& x) const {
        return less_(x, false);
    }

    // x以下の最大値
    optional<T> le(const T& x) const {
        return less_(x, true);
    }

    // x超過の最小値(upper_bound)
    optional<T> gt(const T& x) const {
        return greater_(x, false);
    }

    // x以上の最小値(lower_bound)
    optional<T> ge(const T& x) const {
        return greater_(x, true);
    }

    // 空白区切りで出力
    friend ostream& operator<<(ostream& stream, const TreapMultiSet<T>& trp) {
        rep(i, trp.size()) {
            if (i > 0) stream << ' ';
            stream << trp[i];
        }
        return stream;
    }
};
// TreapMultiSet<ll> trp = vl{0, 0, 5};  // 0, 0, 5を挿入したインスタンスを得る
// trp.insert(3);                        // 挿入
// bool erased = trp.erase(3);           // 値を1つ削除
// bool exists = trp.contains(3);        // 存在判定
// ll cnt = trp.size();                  // 要素数
// ll cnt = trp.count(3);                // 3の要素数
// ll val = trp[i];                      // i番目の値, 0-indexed
// ll cnt = trp.index(5);                // 5未満の要素数
// ll cnt = trp.index_right(5);          // 5以下の要素数
// ll cnt = trp.count_range(5, 9);       // 5以上9未満の要素数
// optional<ll> opt = trp.lt(5);         // 5未満の最大値. 値取り出しは opt.value()
// optional<ll> opt = trp.le(5);         // 5以下の最大値. 値取り出しは opt.value()
// optional<ll> opt = trp.gt(5);         // 5超過の最小値. 値取り出しは opt.value()
// optional<ll> opt = trp.ge(5);         // 5以上の最小値. 値取り出しは opt.value()
// cout << trp;                          // 空白区切りで出力

int main() {
    cin.tie(nullptr);
    cout << fixed << setprecision(10);

    ll N;
    cin >> N;

    vector<pll> vec(N);
    cin >> vec;

    double P, Q, R, S;
    P = Q = 0;
    R = S = Comb(N, 2);
    map<ll, ll> mpx, mpy;
    fore(e, vec) {
        mpx[e.fi]++;
        mpy[e.se]++;
    }
    fore(e, mpx) R -= Comb(e.se, 2);
    fore(e, mpy) S -= Comb(e.se, 2);

    auto trp = TreapMultiSet<ll>();
    sort(all(vec));
    ll pre = -INF;
    stack<ll> stk;
    fore(e, vec) {
        auto [x, y] = e;
        if (pre != x) {
            while (stk.size()) {
                trp.insert(stk.top());
                stk.pop();
            }
            pre = x;
        }
        stk.push(y);
        P += trp.count_range(-INF, y);
        Q += trp.count_range(y + 1, INF);
    }
    debug(P, Q, R, S);
    double tau = (P - Q) / sqrt(R * S);
    print(tau);
}

/*********    1 2 8 b i t 整 数    *********/
// 128bit整数を入力. LONG_LONG_MAX超過/LONG_LONG_MIN未満もOK.
istream& operator>>(istream& stream, int128& val) {
    string s;
    stream >> s;
    assert(s.size() > 0);

    val = 0;
    bool is_minus = false;
    for (int i = 0; i < (int)s.size(); i++) {
        if (i == 0 && s[i] == '-') {
            assert(s.size() > 1);
            is_minus = true;
        } else {
            assert('0' <= s[i] && s[i] <= '9');
            val *= 10;
            val += s[i] - '0';
        }
    }
    if (is_minus) val *= -1;
    return stream;
}
// 128bit整数を出力.
ostream& operator<<(ostream& stream, const int128& val_) {
    auto val(val_);
    if (val == 0) return stream << 0;
    if (val < 0) {
        stream << '-';
        val *= -1;
    }
    string s;
    while (val > 0) {
        s += (val % 10) + '0';
        val /= 10;
    }
    reverse(all(s));
    return stream << s;
}
/*********       P A I R       **********/
/* 単項演算子(+ -) */
template <class T, class U> pair<T, U> operator+(pair<T, U> a) {
    return a;
}
template <class T, class U> pair<T, U> operator-(pair<T, U> a) {
    return a * (-1);
}
/* 複合代入演算子 (pair) */
template <class T, class U> pair<T, U>& operator+=(pair<T, U>& a, const pair<T, U> b) {
    a.fi += b.fi;
    a.se += b.se;
    return a;
}
template <class T, class U> pair<T, U>& operator-=(pair<T, U>& a, const pair<T, U> b) {
    a.fi -= b.fi;
    a.se -= b.se;
    return a;
}
template <class T, class U> pair<T, U>& operator*=(pair<T, U>& a, const pair<T, U> b) {
    a.fi *= b.fi;
    a.se *= b.se;
    return a;
}
template <class T, class U> pair<T, U>& operator/=(pair<T, U>& a, const pair<T, U> b) {
    a.fi /= b.fi;
    a.se /= b.se;
    return a;
}
template <class T, class U> pair<T, U>& operator%=(pair<T, U>& a, const pair<T, U> b) {
    a.fi %= b.fi;
    a.se %= b.se;
    return a;
}
template <class T, class U, class V> pair<T, U>& operator+=(pair<T, U>& a, const V b) {
    a.fi += b;
    a.se += b;
    return a;
}
template <class T, class U, class V> pair<T, U>& operator-=(pair<T, U>& a, const V b) {
    a.fi -= b;
    a.se -= b;
    return a;
}
template <class T, class U, class V> pair<T, U>& operator*=(pair<T, U>& a, const V b) {
    a.fi *= b;
    a.se *= b;
    return a;
}
template <class T, class U, class V> pair<T, U>& operator/=(pair<T, U>& a, const V b) {
    a.fi /= b;
    a.se /= b;
    return a;
}
template <class T, class U, class V> pair<T, U>& operator%=(pair<T, U>& a, const V b) {
    a.fi %= b;
    a.se %= b;
    return a;
}
/* 算術演算子 (pair) */
template <class T, class U, class V> pair<T, U> operator+(pair<T, U> a, const V& b) {
    return a += b;
}
template <class T, class U, class V> pair<T, U> operator-(pair<T, U> a, const V& b) {
    return a -= b;
}
template <class T, class U, class V> pair<T, U> operator*(pair<T, U> a, const V& b) {
    return a *= b;
}
template <class T, class U, class V> pair<T, U> operator/(pair<T, U> a, const V& b) {
    return a /= b;
}
template <class T, class U, class V> pair<T, U> operator%(pair<T, U> a, const V& b) {
    return a %= b;
}
// 入出力
template <class T, class U> istream& operator>>(istream& stream, pair<T, U>& a) {
    stream >> a.fi >> a.se;
    return stream;
}
template <class T, class U> ostream& operator<<(ostream& stream, const pair<T, U>& a) {
    stream << a.fi << " " << a.se;
    return stream;
}

/*********       V E C T O R      **********/
/* 単項演算子(+ -) */
template <class T> vector<T> operator+(vector<T> a) {
    return a;
}
template <class T> vector<T> operator-(vector<T> a) {
    return a * (-1);
}
/* 複合代入演算子 (vector) */
template <class T> vector<T>& operator+=(vector<T>& a, const vector<T> b) {
    assert(a.size() == b.size());
    rep(i, 0, a.size()) a[i] += b[i];
    return a;
}
template <class T> vector<T>& operator-=(vector<T>& a, const vector<T> b) {
    assert(a.size() == b.size());
    rep(i, 0, a.size()) a[i] -= b[i];
    return a;
}
template <class T> vector<T>& operator*=(vector<T>& a, const vector<T> b) {
    assert(a.size() == b.size());
    rep(i, 0, a.size()) a[i] *= b[i];
    return a;
}
template <class T> vector<T>& operator/=(vector<T>& a, const vector<T> b) {
    assert(a.size() == b.size());
    rep(i, 0, a.size()) a[i] /= b[i];
    return a;
}
template <class T> vector<T>& operator%=(vector<T>& a, const vector<T> b) {
    assert(a.size() == b.size());
    rep(i, 0, a.size()) a[i] %= b[i];
    return a;
}
template <class T, class U> vector<T>& operator+=(vector<T>& a, const U b) {
    fore(e, a) e += b;
    return a;
}
template <class T, class U> vector<T>& operator-=(vector<T>& a, const U b) {
    fore(e, a) e -= b;
    return a;
}
template <class T, class U> vector<T>& operator*=(vector<T>& a, const U b) {
    fore(e, a) e *= b;
    return a;
}
template <class T, class U> vector<T>& operator/=(vector<T>& a, const U b) {
    fore(e, a) e /= b;
    return a;
}
template <class T, class U> vector<T>& operator%=(vector<T>& a, const U b) {
    fore(e, a) e %= b;
    return a;
}
/* 算術演算子 (vector) */
template <class T, class U> vector<T> operator+(vector<T> a, const U& b) {
    return a += b;
}
template <class T, class U> vector<T> operator-(vector<T> a, const U& b) {
    return a -= b;
}
template <class T, class U> vector<T> operator*(vector<T> a, const U& b) {
    return a *= b;
}
template <class T, class U> vector<T> operator/(vector<T> a, const U& b) {
    return a /= b;
}
template <class T, class U> vector<T> operator%(vector<T> a, const U& b) {
    return a %= b;
}
/* 入出力 (vector) */
// ベクトルを入力
template <class T> istream& operator>>(istream& stream, vector<T>& a) {
    fore(e, a) stream >> e;
    return stream;
}
// ベクトルを空白区切りで出力
template <class T> ostream& operator<<(ostream& stream, const vector<T>& v) {
    if (v.size()) {
        stream << v[0];
        rep(i, 1, v.size()) cout << " " << v[i];
    }
    return stream;
}
// 2次元ベクトルを空白/改行区切りで出力
template <class T> ostream& operator<<(ostream& stream, const vector<vector<T>>& vv) {
    if (vv.size()) {
        stream << vv[0];
        rep(i, 1, vv.size()) cout << '\n' << vv[i];
    }
    return stream;
}
/* 計算 (vector) */
// 合計。v.size() == 0 のとき T() を返す
template <class T> T Sum(const vector<T>& v) {
    return reduce(all(v));
}
template <class T> T Max(const vector<T>& v) {
    assert(v.size());
    return *max_element(all(v));
}
template <class T> T Min(const vector<T>& v) {
    assert(v.size());
    return *min_element(all(v));
}
// 最大要素を指す最初の位置を返す(0-origin), O(N).
template <class T> ll Argmax(const vector<T>& v) {
    assert(v.size());
    return max_element(all(v)) - v.begin();
}
// 最小要素を指す最初の位置を返す(0-origin), O(N).
template <class T> ll Argmin(const vector<T>& v) {
    assert(v.size());
    return min_element(all(v)) - v.begin();
}
// 要素`a`がベクトル`v`に1つ以上含まれるかどうか
template <class T, class U> bool Contains(const vector<T>& v, const U& a) {
    return find(all(v), a) != v.end();
}
// ソートされたユニーク値のベクトルにinplaceで書き換える, O(NlogN).
template <class T> void Unique(vector<T>& v) {
    sort(all(v));
    v.erase(unique(all(v)), v.end());
}
/* 生成 (vector) */
template <class T, class U> vector<T> make_vector(int n, U v) {
    return vector<T>(n, v);
}

/*********        S E T        **********/
// 一要素を入力
template <class T> istream& operator>>(istream& stream, set<T>& st) {
    T e;
    stream >> e;
    st.insert(e);
    return stream;
}
// 空白区切りで出力
template <class T> ostream& operator<<(ostream& stream, const set<T>& st) {
    if (st.size()) {
        auto it = st.begin();
        stream << *it++;
        for (; it != st.end(); it++)
            cout << " " << *it;
    }
    return stream;
}
template <class T> T Max(const set<T>& st) {
    assert(st.size());
    return *prev(st.end());
}
template <class T> T Min(const set<T>& st) {
    assert(st.size());
    return *st.begin();
}

/*********   M U L T I S E T   **********/
// 一要素を入力
template <class T> istream& operator>>(istream& stream, multiset<T>& st) {
    T e;
    stream >> e;
    st.insert(e);
    return stream;
}
// 空白区切りで出力
template <class T> ostream& operator<<(ostream& stream, const multiset<T>& st) {
    if (st.size()) {
        auto it = st.begin();
        stream << *it++;
        for (; it != st.end(); it++)
            cout << " " << *it;
    }
    return stream;
}
template <class T> T Max(const multiset<T>& st) {
    assert(st.size());
    return *prev(st.end());
}
template <class T> T Min(const multiset<T>& st) {
    assert(st.size());
    return *st.begin();
}
// 要素xを1つ削除. 消せたらtrueを返す.
template <class T> bool EraseOne(multiset<T>& st, const T& x) {
    auto it = st.find(x);
    if (it != st.end()) {
        st.erase(it);
        return true;
    } else
        return false;
}

/*********       C H A R       **********/
// 大文字かどうか
bool IsUpper(const char& c) {
    return isupper(c) > 0;
}
// 小文字かどうか
bool IsLower(const char& c) {
    return islower(c) > 0;
}
// 大文字へ変換
char ToUpper(const char& c) {
    string s{c}, t;
    t.resize(s.size());
    std::transform(all(s), t.begin(), ::toupper);
    return t[0];
}
// 小文字へ変換
char ToLower(const char& c) {
    string s{c}, t;
    t.resize(s.size());
    std::transform(all(s), t.begin(), ::tolower);
    return t[0];
}

/*********     S T R I N G     **********/
// 大文字へ変換
string ToUpper(const string& s) {
    string t;
    t.resize(s.size());
    std::transform(all(s), t.begin(), ::toupper);
    return t;
}
// 小文字へ変換
string ToLower(const string& s) {
    string t;
    t.resize(s.size());
    std::transform(all(s), t.begin(), ::tolower);
    return t;
}
// 回文判定, O(N). 空文字の返り値はTrue.
bool IsPalindrome(const string& S) {
    ll N = len(S);
    for (ll i = 0; 2 * i < N; i++)
        if (S[i] != S[N - i - 1]) return false;
    return true;
}

/*********       T R I O       **********/
// コンストラクタ・代入演算子
template <class T1, class T2, class T3> trio<T1, T2, T3>::trio() {
    first = T1();
    second = T2();
    third = T3();
}
template <class T1, class T2, class T3> trio<T1, T2, T3>::trio(const T1& x) : first(x), second(x), third(x) {}
template <class T1, class T2, class T3>
trio<T1, T2, T3>::trio(const T1& x, const T2& y, const T3& z) : first(x), second(y), third(z) {}
template <class T1, class T2, class T3> trio<T1, T2, T3>::trio(const trio& t) {
    first = t.first;
    second = t.second;
    third = t.third;
}
template <class T1, class T2, class T3> trio<T1, T2, T3>& trio<T1, T2, T3>::operator=(const trio& t) {
    first = t.first;
    second = t.second;
    third = t.third;
    return *this;
}
// 比較演算子 (trio)
template <class T1, class T2, class T3> bool trio<T1, T2, T3>::operator<(const trio& t) const {
    return tie(first, second, third) < tie(t.first, t.second, t.third);
}
template <class T1, class T2, class T3> bool trio<T1, T2, T3>::operator==(const trio& t) const {
    return tie(first, second, third) == tie(t.first, t.second, t.third);
}
template <class T1, class T2, class T3> bool trio<T1, T2, T3>::operator!=(const trio& t) const {
    return !(*this == t);
}
template <class T1, class T2, class T3> bool trio<T1, T2, T3>::operator>(const trio& t) const {
    return t < *this;
}
template <class T1, class T2, class T3> bool trio<T1, T2, T3>::operator<=(const trio& t) const {
    return !(*this > t);
}
template <class T1, class T2, class T3> bool trio<T1, T2, T3>::operator>=(const trio& t) const {
    return !(*this < t);
}
// 単項演算子(+ -) (trio)
template <class T1, class T2, class T3> trio<T1, T2, T3> trio<T1, T2, T3>::operator+() const {
    return *this;
}
template <class T1, class T2, class T3> trio<T1, T2, T3> trio<T1, T2, T3>::operator-() const {
    return (*this) * (-1);
}
// 入出力 (trio)
template <class T1, class T2, class T3> istream& operator>>(istream& stream, trio<T1, T2, T3>& t) {
    return stream >> t.first >> t.second >> t.third;
}
template <class T1, class T2, class T3> ostream& operator<<(ostream& stream, const trio<T1, T2, T3>& t) {
    return stream << t.first << " " << t.second << " " << t.third;
}
/* 複合代入演算子 (trio) */
template <class T1, class T2, class T3>
trio<T1, T2, T3>& operator+=(trio<T1, T2, T3>& a, const trio<T1, T2, T3> b) {
    a.fi += b.fi;
    a.se += b.se;
    a.th += b.th;
    return a;
}
template <class T1, class T2, class T3>
trio<T1, T2, T3>& operator-=(trio<T1, T2, T3>& a, const trio<T1, T2, T3> b) {
    a.fi -= b.fi;
    a.se -= b.se;
    a.th -= b.th;
    return a;
}
template <class T1, class T2, class T3>
trio<T1, T2, T3>& operator*=(trio<T1, T2, T3>& a, const trio<T1, T2, T3> b) {
    a.fi *= b.fi;
    a.se *= b.se;
    a.th *= b.th;
    return a;
}
template <class T1, class T2, class T3>
trio<T1, T2, T3>& operator/=(trio<T1, T2, T3>& a, const trio<T1, T2, T3> b) {
    a.fi /= b.fi;
    a.se /= b.se;
    a.th /= b.th;
    return a;
}
template <class T1, class T2, class T3>
trio<T1, T2, T3>& operator%=(trio<T1, T2, T3>& a, const trio<T1, T2, T3> b) {
    a.fi %= b.fi;
    a.se %= b.se;
    a.th %= b.th;
    return a;
}
template <class T1, class T2, class T3, class U>
trio<T1, T2, T3>& operator+=(trio<T1, T2, T3>& a, const U b) {
    a.fi += b;
    a.se += b;
    a.th += b;
    return a;
}
template <class T1, class T2, class T3, class U>
trio<T1, T2, T3>& operator-=(trio<T1, T2, T3>& a, const U b) {
    a.fi -= b;
    a.se -= b;
    a.th -= b;
    return a;
}
template <class T1, class T2, class T3, class U>
trio<T1, T2, T3>& operator*=(trio<T1, T2, T3>& a, const U b) {
    a.fi *= b;
    a.se *= b;
    a.th *= b;
    return a;
}
template <class T1, class T2, class T3, class U>
trio<T1, T2, T3>& operator/=(trio<T1, T2, T3>& a, const U b) {
    a.fi /= b;
    a.se /= b;
    a.th /= b;
    return a;
}
template <class T1, class T2, class T3, class U>
trio<T1, T2, T3>& operator%=(trio<T1, T2, T3>& a, const U b) {
    a.fi %= b;
    a.se %= b;
    a.th %= b;
    return a;
}
/* 算術演算子 (trio) */
template <class T1, class T2, class T3, class U> trio<T1, T2, T3> operator+(trio<T1, T2, T3> a, const U& b) {
    return a += b;
}
template <class T1, class T2, class T3, class U> trio<T1, T2, T3> operator-(trio<T1, T2, T3> a, const U& b) {
    return a -= b;
}
template <class T1, class T2, class T3, class U> trio<T1, T2, T3> operator*(trio<T1, T2, T3> a, const U& b) {
    return a *= b;
}
template <class T1, class T2, class T3, class U> trio<T1, T2, T3> operator/(trio<T1, T2, T3> a, const U& b) {
    return a /= b;
}
template <class T1, class T2, class T3, class U> trio<T1, T2, T3> operator%(trio<T1, T2, T3> a, const U& b) {
    return a %= b;
}

/*********    Q U A R T E T    **********/
// コンストラクタ・代入演算子
template <class T1, class T2, class T3, class T4> quartet<T1, T2, T3, T4>::quartet() {
    first = T1();
    second = T2();
    third = T3();
    fourth = T4();
}
template <class T1, class T2, class T3, class T4>
quartet<T1, T2, T3, T4>::quartet(const T1& x) : first(x), second(x), third(x), fourth(x) {}
template <class T1, class T2, class T3, class T4>
quartet<T1, T2, T3, T4>::quartet(const T1& x, const T2& y, const T3& z, const T4& w)
    : first(x), second(y), third(z), fourth(w) {}
template <class T1, class T2, class T3, class T4> quartet<T1, T2, T3, T4>::quartet(const quartet& t) {
    first = t.first;
    second = t.second;
    third = t.third;
    fourth = t.fourth;
}
template <class T1, class T2, class T3, class T4>
quartet<T1, T2, T3, T4>& quartet<T1, T2, T3, T4>::operator=(const quartet& t) {
    first = t.first;
    second = t.second;
    third = t.third;
    fourth = t.fourth;
    return *this;
}
// 比較演算子 (quartet)
template <class T1, class T2, class T3, class T4>
bool quartet<T1, T2, T3, T4>::operator<(const quartet& t) const {
    return tie(first, second, third, fourth) < tie(t.first, t.second, t.third, t.fourth);
}
template <class T1, class T2, class T3, class T4>
bool quartet<T1, T2, T3, T4>::operator==(const quartet& t) const {
    return tie(first, second, third, fourth) == tie(t.first, t.second, t.third, t.fourth);
}
template <class T1, class T2, class T3, class T4>
bool quartet<T1, T2, T3, T4>::operator!=(const quartet& t) const {
    return !(*this == t);
}
template <class T1, class T2, class T3, class T4>
bool quartet<T1, T2, T3, T4>::operator>(const quartet& t) const {
    return t < *this;
}
template <class T1, class T2, class T3, class T4>
bool quartet<T1, T2, T3, T4>::operator<=(const quartet& t) const {
    return !(*this > t);
}
template <class T1, class T2, class T3, class T4>
bool quartet<T1, T2, T3, T4>::operator>=(const quartet& t) const {
    return !(*this < t);
}
// 単項演算子(+ -) (quartet)
template <class T1, class T2, class T3, class T4>
quartet<T1, T2, T3, T4> quartet<T1, T2, T3, T4>::operator+() const {
    return *this;
}
template <class T1, class T2, class T3, class T4>
quartet<T1, T2, T3, T4> quartet<T1, T2, T3, T4>::operator-() const {
    return (*this) * (-1);
}
// 入出力 (quartet)
template <class T1, class T2, class T3, class T4>
istream& operator>>(istream& stream, quartet<T1, T2, T3, T4>& t) {
    return stream >> t.first >> t.second >> t.third >> t.fourth;
}
template <class T1, class T2, class T3, class T4>
ostream& operator<<(ostream& stream, const quartet<T1, T2, T3, T4>& t) {
    return stream << t.first << " " << t.second << " " << t.third << " " << t.fourth;
}
/* 複合代入演算子 (quartet) */
template <class T1, class T2, class T3, class T4>
quartet<T1, T2, T3, T4>& operator+=(quartet<T1, T2, T3, T4>& a, const quartet<T1, T2, T3, T4> b) {
    a.fi += b.fi;
    a.se += b.se;
    a.th += b.th;
    a.fo += b.fo;
    return a;
}
template <class T1, class T2, class T3, class T4>
quartet<T1, T2, T3, T4>& operator-=(quartet<T1, T2, T3, T4>& a, const quartet<T1, T2, T3, T4> b) {
    a.fi -= b.fi;
    a.se -= b.se;
    a.th -= b.th;
    a.fo -= b.fo;
    return a;
}
template <class T1, class T2, class T3, class T4>
quartet<T1, T2, T3, T4>& operator*=(quartet<T1, T2, T3, T4>& a, const quartet<T1, T2, T3, T4> b) {
    a.fi *= b.fi;
    a.se *= b.se;
    a.th *= b.th;
    a.fo *= b.fo;
    return a;
}
template <class T1, class T2, class T3, class T4>
quartet<T1, T2, T3, T4>& operator/=(quartet<T1, T2, T3, T4>& a, const quartet<T1, T2, T3, T4> b) {
    a.fi /= b.fi;
    a.se /= b.se;
    a.th /= b.th;
    a.fo /= b.fo;
    return a;
}
template <class T1, class T2, class T3, class T4>
quartet<T1, T2, T3, T4>& operator%=(quartet<T1, T2, T3, T4>& a, const quartet<T1, T2, T3, T4> b) {
    a.fi %= b.fi;
    a.se %= b.se;
    a.th %= b.th;
    a.fo %= b.fo;
    return a;
}
template <class T1, class T2, class T3, class T4, class U>
quartet<T1, T2, T3, T4>& operator+=(quartet<T1, T2, T3, T4>& a, const U b) {
    a.fi += b;
    a.se += b;
    a.th += b;
    a.fo += b;
    return a;
}
template <class T1, class T2, class T3, class T4, class U>
quartet<T1, T2, T3, T4>& operator-=(quartet<T1, T2, T3, T4>& a, const U b) {
    a.fi -= b;
    a.se -= b;
    a.th -= b;
    a.fo -= b;
    return a;
}
template <class T1, class T2, class T3, class T4, class U>
quartet<T1, T2, T3, T4>& operator*=(quartet<T1, T2, T3, T4>& a, const U b) {
    a.fi *= b;
    a.se *= b;
    a.th *= b;
    a.fo *= b;
    return a;
}
template <class T1, class T2, class T3, class T4, class U>
quartet<T1, T2, T3, T4>& operator/=(quartet<T1, T2, T3, T4>& a, const U b) {
    a.fi /= b;
    a.se /= b;
    a.th /= b;
    a.fo /= b;
    return a;
}
template <class T1, class T2, class T3, class T4, class U>
quartet<T1, T2, T3, T4>& operator%=(quartet<T1, T2, T3, T4>& a, const U b) {
    a.fi %= b;
    a.se %= b;
    a.th %= b;
    a.fo %= b;
    return a;
}
/* 算術演算子 (quartet) */
template <class T1, class T2, class T3, class T4, class U>
quartet<T1, T2, T3, T4> operator+(quartet<T1, T2, T3, T4> a, const U& b) {
    return a += b;
}
template <class T1, class T2, class T3, class T4, class U>
quartet<T1, T2, T3, T4> operator-(quartet<T1, T2, T3, T4> a, const U& b) {
    return a -= b;
}
template <class T1, class T2, class T3, class T4, class U>
quartet<T1, T2, T3, T4> operator*(quartet<T1, T2, T3, T4> a, const U& b) {
    return a *= b;
}
template <class T1, class T2, class T3, class T4, class U>
quartet<T1, T2, T3, T4> operator/(quartet<T1, T2, T3, T4> a, const U& b) {
    return a /= b;
}
template <class T1, class T2, class T3, class T4, class U>
quartet<T1, T2, T3, T4> operator%(quartet<T1, T2, T3, T4> a, const U& b) {
    return a %= b;
}

/*********     R A N D O M     **********/
// 引数なし: seedはランダム
Random::Random() {
    random_device seed_gen;
    rnd.seed(seed_gen());
}
// 引数: seed
Random::Random(ll seed) {
    rnd.seed(seed);
}
// [a, b]からランダムに整数を返す
ll Random::randint(ll a, ll b) {
    uniform_int_distribution<ll> dist(a, b);
    return dist(rnd);
}
// [a, b]からランダムに浮動小数点数を返す
double Random::randreal(double a, double b) {
    uniform_real_distribution<double> dist(a, b);
    return dist(rnd);
}
// [a, b]からランダムに文字を返す
char Random::randchar(char a, char b) {
    uniform_int_distribution<ll> dist(a, b);
    return dist(rnd);
}
// `vec`をinplaceで並び替える
template <class T> void Random::shuffle(vector<T>& vec) {
    std::shuffle(all(vec), rnd);
}

/*********       T I M E R       **********/
// 引数: タイムリミット(秒)
Timer::Timer(double time_limit_sec) {
    assert(time_limit_sec > 0);
    this->time_limit = time_limit_sec * CLOCKS_PER_SEC;
    this->start_time = clock();
}
// タイムリミットに到達したかどうか
bool Timer::is_time_up() const {
    return clock() - this->start_time >= this->time_limit;
}
// 経過時間の割合を[0.0, 1.0]で返す
double Timer::get_elapsed_ratio() const {
    double ratio = 1.0 * (clock() - this->start_time) / (this->time_limit - this->start_time);
    return min(1.0, ratio);
}

/*********       P R I N T       **********/
template <class T> void print(const T& e) {
    cout << e << '\n';
}
template <class H, class... T> void print(const H& h, const T&... t) {
    cout << h << ' ';
    print(t...);
}
template <class... T> void End(const T&... t) {
    print(t...);
    exit(0);
}

/*********       D E B U G       *********/
void dbg_(const long long& e) {
    if (e == INF)
        cout << "INF";
    else if (e == -INF)
        cout << "-INF";
    else
        cout << e;
}
template <class T> void dbg_(const T& e) {
    cout << e;
}
template <class T, class U> void dbg_(const pair<T, U>& p) {
    cout << '(';
    dbg_(p.first);
    cout << ' ';
    dbg_(p.second);
    cout << ')';
}
template <class T1, class T2, class T3> void dbg_(const trio<T1, T2, T3>& t) {
    cout << '(';
    dbg_(t.first);
    cout << ' ';
    dbg_(t.second);
    cout << ' ';
    dbg_(t.third);
    cout << ')';
}
template <class T1, class T2, class T3, class T4> void dbg_(const quartet<T1, T2, T3, T4>& t) {
    cout << '(';
    dbg_(t.first);
    cout << ' ';
    dbg_(t.second);
    cout << ' ';
    dbg_(t.third);
    cout << ' ';
    dbg_(t.fourth);
    cout << ')';
}
template <class T> void debug_(const T& e) {
    dbg_(e);
    cout << '\n';
}
template <class T> void debug_(const vector<T>& v) {
    if (v.size()) {
        auto it = v.begin();
        dbg_(*it++);
        for (; it != v.end(); ++it) {
            cout << ' ';
            dbg_(*it);
        }
    }
    cout << '\n';
}
template <class T> void debug_(const vector<vector<T>>& vv) {
    cout << '\n';
    ll cnt = 0;
    for (auto&& v : vv) {
        cout << cnt++ << ": ";
        debug_(v);
    }
}
template <class T, class U> void debug_(const map<T, U>& mp) {
    if (mp.size()) {
        auto it = mp.begin();
        dbg_(*it++);
        for (; it != mp.end(); ++it) {
            cout << ' ';
            dbg_(*it);
        }
    }
    cout << '\n';
}
template <class T, class U> void debug_(const vector<map<T, U>>& vm) {
    cout << '\n';
    ll cnt = 0;
    for (auto&& mp : vm) {
        cout << cnt++ << ": ";
        debug_(mp);
    }
}
template <class T> void debug_(const set<T>& st) {
    if (st.size()) {
        auto it = st.begin();
        dbg_(*it++);
        for (; it != st.end(); ++it) {
            cout << ' ';
            dbg_(*it);
        }
    }
    cout << '\n';
}
template <class T> void debug_(const multiset<T>& st) {
    if (st.size()) {
        auto it = st.begin();
        dbg_(*it++);
        for (; it != st.end(); ++it) {
            cout << ' ';
            dbg_(*it);
        }
    }
    cout << '\n';
}
template <class T> void debug_(const vector<set<T>>& vs) {
    cout << '\n';
    ll cnt = 0;
    for (auto&& st : vs) {
        cout << cnt++ << ": ";
        debug_(st);
    }
}
template <class T> void debug_(const vector<multiset<T>>& vs) {
    cout << '\n';
    ll cnt = 0;
    for (auto&& st : vs) {
        cout << cnt++ << ": ";
        debug_(st);
    }
}
template <class H, class... T> void debug_(const H& h, const T&... t) {
    dbg_(h);
    cout << ", ";
    debug_(t...);
}

/*********       O T H E R S       **********/
// a > b なら a に b を代入.
template <class T, class U> bool chmin(T& a, U b) {
    if (a > b) {
        a = b;
        return true;
    }
    return false;
}
// a < b なら a に b を代入.
template <class T, class U> bool chmax(T& a, U b) {
    if (a < b) {
        a = b;
        return true;
    }
    return false;
}
// a % mを返す. aは負もOK.
template <class T, class U> T Mod(const T& a, const U& m) {
    assert(m > 0);
    return (a % m + m) % m;
}
// 天井関数. xは負もOK
template <class T, class U> T Ceil(const T& x, const U& y) {
    assert(y > 0);
    return x < 0 ? x / y : (x + y - 1) / y;
}
// 床関数. xは負もOK
template <class T, class U> T Floor(const T& x, const U& y) {
    assert(y > 0);
    return -(Ceil(-x, y));
}
// 符号関数. 返り値: -1, 0, +1のいずれか
template <class T> ll Sign(const T& x) {
    return (x > T(0)) - (x < T(0));
}
// x * x <= nを満たす最大の整数`x`を返す
ll Isqrt(ll n) {
    assert(n >= 0);
    ll x = round(sqrt(n));
    while (x * x > n)
        --x;
    return x;
}
// nCr. n=60, r=30までOK.
ll Comb(ll n, ll r) {
    if (r < 0 || n < r) return 0;
    r = min(r, n - r);
    ll ret = 1;
    rep(i, r) {
        ret *= n - i;
        ret /= i + 1;
    }
    return ret;
}
// べき乗. mintもOK.
template <class T> T Pow(T x, ll n) {
    assert(n >= 0);
    T ret = 1;
    while (1) {
        if (n % 2) ret *= x;
        n /= 2;
        if (!n) return ret;
        x = x * x;
    }
}
// 等差級数. mintもOK.
template <class T> T Aseries(T a, T d, ll n) {
    assert(n >= 0);
    return a * n + n * (n - 1) / 2 * d;
}
// 等比級数. mintもOK.
template <class T> T Gseries(T a, T r, ll n) {
    assert(n >= 0);
    if (r == 1)
        return a * n;
    else
        return a * (1 - Pow(r, n)) / (1 - r);
}
// `b`の`i`番目(0 origin)のビットが1かどうか
bool Bit(ll b, int i) {
    assert(0 <= i && i < 64);
    return (b >> i) & 1;
}
// `b`の立っているビット数
ll Popcount(ll b) {
    return __builtin_popcountll(b);
}
// [0, n)番目のビットが立った64bit整数を返す.
ll Mask(ll n) {
    assert(0 <= n && n < 63);
    return (1LL << n) - 1LL;
}
// [m, n)番目のビットが立った64bit整数を返す.
ll Mask(ll n, ll m) {
    assert(n >= m);
    return Mask(n) ^ Mask(m);
}
// [l1, r1)と[l2, r2)の積区間を返す. 例: [3, 6) x [5, 7) = [5, 6)
pll GetOverlap(ll l1, ll r1, ll l2, ll r2) {
    return {max(l1, l2), min(r1, r2)};
}
// [l1, r1)と[l2, r2)で重なりがあるかを返す. 引数が片方でも空区間ならfalse;
bool IsOverlap(ll l1, ll r1, ll l2, ll r2) {
    return max(l1, l2) < min(r1, r2);
}
// b=trueなら"Yes\n", falseなら"No\n"を出力
void PrintYesNo(bool b) {
    b ? print("Yes") : print("No");
}
0