#include #pragma GCC optimize("Ofast") #pragma GCC optimize("unroll-loops") #pragma GCC target("sse,sse2,sse3,ssse3,sse4,fma,abm,mmx,avx,avx2") #define rep(i, n) for (int i = 0; i < (int)(n); i++) #define rrep(i, n) for (int i = (int)(n) - 1; i >= 0; i--) #define all(x) (x).begin(), (x).end() #define sz(x) int(x.size()) #define yn(joken) cout<<((joken) ? "Yes" : "No")<<"\n" #define YN(joken) cout<<((joken) ? "YES" : "NO")<<"\n" using namespace std; using ll = long long; using vi = vector; using vl = vector; using vs = vector; using vc = vector; using vd = vector; using vld = vector; using vvi = vector>; using vvl = vector>; using vvs = vector>; using vvc = vector>; using vvd = vector>; using vvld = vector>; using vvvi = vector>>; using vvvl = vector>>; using vvvvi = vector>>>; using vvvvl = vector>>>; using pii = pair; using pll = pair; const int INF = 1e9; const ll LINF = 2e18; template bool chmax(T& a, const T& b) { if (a < b) { a = b; return 1; } return 0; } template bool chmin(T& a, const T& b) { if (b < a) { a = b; return 1; } return 0; } bool ispow2(int i) { return i && (i & -i) == i; } bool ispow2(ll i) { return i && (i & -i) == i; } template vector make_vec(size_t a) { return vector(a); } template auto make_vec(size_t a, Ts... ts) { return vector(ts...))>(a, make_vec(ts...)); } template istream& operator>>(istream& is, vector& v) { for (int i = 0; i < int(v.size()); i++) { is >> v[i]; } return is; } template ostream& operator<<(ostream& os, const vector& v) { for (int i = 0; i < int(v.size()); i++) { os << v[i]; if (i < int(v.size()) - 1) os << ' '; } return os; } static uint32_t RandXor(){ static uint32_t x=123456789; static uint32_t y=362436069; static uint32_t z=521288629; static uint32_t w=88675123; uint32_t t; t=x^(x<<11); x=y; y=z; z=w; return w=(w^(w>>19))^(t^(t>>8)); } static double Rand01(){ return (RandXor()+0.5)*(1.0/UINT_MAX); } // BinaryIndexedTree(sz): 長さszの0で初期化された配列で構築する. // BinaryIndexedTree(vs): 配列vsで構築する. // add(k, x): 要素kに値xを加える. // fold(r): [0,r)の総和を求める // fold(l,r): [l,r)の総和を求める // lower_bound(x): [0,k]の総和がx以上になる最小のkを返す // upper_bound(x): [0,k]の総和がxより大になる最小のkを返す template struct BinaryIndexedTree{ private: vector data; public: BinaryIndexedTree() = default; explicit BinaryIndexedTree(size_t sz) : data(sz + 1, 0) {} explicit BinaryIndexedTree(const vector &vs) : data(vs.size() + 1, 0){ for (size_t i = 0; i < vs.size(); i++) data[i + 1] = vs[i]; for (size_t i = 1; i < data.size(); i++){ size_t j = i + (i & -i); if (j < data.size()) data[j] += data[i]; } } void add(int k, const T &x){ for (++k; k < (int)data.size(); k += k & -k) data[k] += x; } T fold(int r) const{ T ret = T(); for (; r > 0; r -= r & -r) ret += data[r]; return ret; } T fold(int l, int r) const{ return fold(r) - fold(l); } int lower_bound(T x) const{ int i = 0; for (int k = 1 << (__lg(data.size() - 1) + 1); k > 0; k >>= 1){ if (i + k < data.size() && data[i + k] < x){ x -= data[i + k]; i += k; } } return i; } int upper_bound(T x) const{ int i = 0; for (int k = 1 << (__lg(data.size() - 1) + 1); k > 0; k >>= 1){ if (i + k < data.size() && data[i + k] <= x){ x -= data[i + k]; i += k; } } return i; } }; void solve(){ ll N; cin>>N; vi A(N); cin>>A; vi V=A; V.emplace_back(-INF); V.emplace_back(INF); sort(all(V)); V.erase(unique(all(V)),V.end()); map mp; rep(i,sz(V)) mp[V[i]]=i; rep(i,N) A[i]=mp[A[i]]; map cnt; rep(i,N) cnt[A[i]]++; ll ans=N*(N-1)*(N-2)/6; for(auto [k,v]:cnt){ if(v>=2) ans-=v*(v-1)/2*(N-v); if(v>=3) ans-=v*(v-1)*(v-2)/6; } vl X(N),Y(N),XX(N),YY(N); // 左のless, 右のmore, 左のmore, 右のless BinaryIndexedTree BIT(sz(V)); rep(i,N){ X[i]=BIT.fold(0,A[i]); BIT.add(A[i],1); } BinaryIndexedTree BIT2(sz(V)); rrep(i,N){ Y[i]=BIT2.fold(A[i]+1,sz(V)); BIT2.add(A[i],1); } BinaryIndexedTree BIT3(sz(V)); rep(i,N){ XX[i]=BIT3.fold(A[i]+1,sz(V)); BIT3.add(A[i],1); } BinaryIndexedTree BIT4(sz(V)); rrep(i,N){ YY[i]=BIT4.fold(0,A[i]); BIT4.add(A[i],1); } for(int i=1;i