結果

問題 No.2873 Kendall's Tau
ユーザー nana
提出日時 2024-09-20 00:40:09
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 356 ms / 4,500 ms
コード長 6,163 bytes
コンパイル時間 3,813 ms
コンパイル使用メモリ 199,436 KB
最終ジャッジ日時 2025-02-24 09:39:14
ジャッジサーバーID
(参考情報)
judge4 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 30
権限があれば一括ダウンロードができます

ソースコード

diff #
プレゼンテーションモードにする

#include<iostream>
#include<sstream>
#include<algorithm>
#include<deque>
#include<list>
#include<map>
#include<memory>
#include<queue>
#include<set>
#include<stack>
#include<utility>
#include<string.h>
#include<string>
#include<math.h>
#include<float.h>
#include<stdio.h>
#include<vector>
#include<iomanip>
#include<bitset>
#include<random>
#include<complex>
#include <cassert>
#include <utility>
#include <atcoder/all>
using namespace std;
using namespace atcoder;
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
template<typename T> inline bool chmin(T &a, T b) { return ((a>b) ? (a = b, true) : (false));}
#define rep(i,s,n) for(long long i=s;i<(long long)(n);i++)
#define rrep(i,s,n) for(long long i=n-1;i>=s;i--)
const long long inf = 1LL<<60;
typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
//pairsecondsort(p.begin(),p.end(),cmp)
#define cmp [](pair<ll,ll> a, pair<ll,ll> b){return a.second<b.second;}
//cmpArg := , atan2l(y, x); atan2l(y, x) = [-pi, pi]
#define cmpArg [](const auto &p1, const auto &p2) { return atan2l(p1.second, p1.first) < atan2l(p2.second, p2.first); }
typedef pair<long long, long long> P;
typedef pair<ll, pair<ll,ll> > PP;
#define rll ll,vector<ll>,greater<ll>
#define rP P,vector<P>,greater<P>
const long double pi = 3.14159265358979;
typedef unsigned long long ull;
#define vll vector<ll>
#define vvll vector<vector<ll>>
#define vmint vector<mint>
#define vvmint vector<vector<mint>>
#define vvch vector<vector<char>>
#define vch vector<char>
#define vstring vector<string>
#define rPP PP,vector<PP>,greater<PP>
#define vP vector<P>
#define vvP vector<vector<P>>
#define vPP vector<PP>
#define all(x) x.begin(), x.end()
//UNIQUE(x) x
#define UNIQUE(x) sort(all(x)), x.erase(unique(all(x)), x.end())
int pc(ll x) { return __builtin_popcount(x); } //1 pop count
//lower_bound(調)`ll index = lower_bound(all(inv), -a[i], greater<ll>()) - inv.begin();`,
    lis
//a > inf - b a > inf / b
//(siz+1)/2
//using mint = atcoder::modint, mainmint::set_mod(M)modmint
//map auto it = mp.lower_bound(key)lower_bound使 while(it != mp.end())
//!!
using mint = modint998244353;
class BIT {
public:
//
ll n;
//
vector<ll> a;
//
BIT(ll n):n(n),a(n+1,0){}
//a[i]x
void add(ll i,ll x){
i++;
if(i==0) return;
for(ll k=i;k<=n;k+=(k & -k)){
a[k]+=x;
}
}
//a[i]+a[i+1]+…+a[j]
ll sum(ll i,ll j){
return sum_sub(j)-sum_sub(i-1);
}
//a[0]+a[1]+…+a[i]
ll sum_sub(ll i){
i++;
ll s=0;
if(i==0) return s;
for(ll k=i;k>0;k-=(k & -k)){
s+=a[k];
}
return s;
}
//a[0]+a[1]+…+a[i]>=xi(ka[k]>=0)
ll lower_bound(ll x){
if(x<=0){
//x0→0
return 0;
}else{
ll i=0;ll r=1;
//
//n(BIT)
while(r<n) r=r<<1;
//調
for(int len=r;len>0;len=len>>1) {
//
if(i+len<n && a[i+len]<x){
x-=a[i+len];
i+=len;
}
}
return i;
}
}
};
// Coodinate Compression
// https://youtu.be/fR3W5IcBGLQ?t=8550
template<typename T=ll>
struct CC {
bool initialized;
vector<T> xs;
CC(): initialized(false) {}
void add(T x) { xs.push_back(x);} //
void init() {
sort(xs.begin(), xs.end());
xs.erase(unique(xs.begin(),xs.end()),xs.end());
initialized = true;
}
int operator()(T x) { // ->
if (!initialized) init();
return upper_bound(xs.begin(), xs.end(), x) - xs.begin() - 1;
}
T operator[](int i) { // ->
if (!initialized) init();
return xs[i];
}
int size() {
if (!initialized) init();
return xs.size();
}
};
int main()
{
ll n; cin >> n;
CC cc;
vector<int> x(n) , y(n);
rep(i,0,n) cin >> x[i] >> y[i];
rep(i,0,n) cc.add(x[i]), cc.add(y[i]);
rep(i,0,n) x[i] = cc(x[i]), y[i] = cc(y[i]);
ll P=0,Q=0,R,S;
R=S=n*(n-1)/2;
//R,S
auto calcRS = [&](vector<int> array) -> ll {
vector<ll> cnt(cc.size()+10,0);
rep(i,0,array.size()) cnt[array[i]]++;
ll res = 0;
rep(i,0,cc.size()+10) {
res -= cnt[i] * (cnt[i] - 1) / 2;
}
return res;
};
R += calcRS(x);
S += calcRS(y);
//P,Q
vector<pair<int,int>> xy(n);
rep(i,0,n) xy[i] = {x[i], y[i]};
sort(xy.begin(), xy.end(), [](pair<int,int> a, pair<int,int> b) {
if(a.first != b.first) return a.first < b.first;
return a.second > b.second;
});
BIT bit(cc.size()+10);
rep(i,0,n) {
P += bit.sum(0,xy[i].second-1);
bit.add(xy[i].second, 1);
}
//Q
sort(all(xy));
BIT bit2(cc.size()+10);
rep(i,0,n) {
Q += bit2.sum(xy[i].second+1, cc.size()+9);
bit2.add(xy[i].second, 1);
}
double ans = double(P - Q) / sqrt(R) / sqrt(S);
cout << setprecision(20) << ans << endl;
}
הההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההה
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
0