結果
問題 | No.1300 Sum of Inversions |
ユーザー |
![]() |
提出日時 | 2024-02-29 20:47:00 |
言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 766 ms / 2,000 ms |
コード長 | 3,358 bytes |
コンパイル時間 | 2,370 ms |
コンパイル使用メモリ | 208,928 KB |
最終ジャッジ日時 | 2025-02-19 22:04:10 |
ジャッジサーバーID (参考情報) |
judge1 / judge4 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 3 |
other | AC * 34 |
ソースコード
#include <bits/stdc++.h> using namespace std; using ll = long long; const ll modc = 998244353; class mint { ll x; public: mint(ll x=0) : x((x%modc+modc)%modc) {} mint operator-() const { return mint(-x);} mint& operator+=(const mint& a) { if ((x += a.x) >= modc) x -= modc; return *this; } mint& operator-=(const mint& a) { if ((x += modc-a.x) >= modc) x -= modc; return *this; } mint& operator*=(const mint& a) { (x *= a.x) %= modc; return *this; } mint operator+(const mint& a) const { mint res(*this); return res+=a; } mint operator-(const mint& a) const { mint res(*this); return res-=a; } mint operator*(const mint& a) const { mint res(*this); return res*=a; } mint pow(ll t) const { if (!t) return 1; mint a = pow(t>>1); a *= a; if (t&1) a *= *this; return a; } mint inv() const {return pow(modc-2);} mint& operator/=(const mint& a){ return (*this) *= a.inv();} mint operator/(const mint& a) const { mint res(*this); return res/=a; } bool operator == (const mint& a) const{ return x == a.x;} bool operator != (const mint& a) const{ return x != a.x;} friend ostream& operator<<(ostream& os, const mint& m){ os << m.x; return os; } friend istream& operator>>(istream& ip, mint &m) { ll t; ip >> t; m = mint(t); return ip; } ll val(){ return x;} }; //RSQ (0-indexed) template<class T> struct BIT { private: vector<T> bit; int N; T _sum(int r){ r++; T res = 0; while(r > 0) res += bit[r-1], r -= r & -r; return res; } public: BIT (int n){ N = n; bit.resize(N);} BIT (vector<T> &a) : BIT(a.size()) { for (int i=0; i<N; i++) add(i, a[i]);} void add(int loc, T val){ loc++; while(loc <= N) bit[loc-1] += val, loc += loc & -loc; } void change(int loc, T val){add(loc, -get(loc)+val);} T sum(int l, int r) {return _sum(r) - _sum(l-1);} T get(int l) {return sum(l, l);} void clear() {for (int i=0; i<N; i++) bit[i] = 0;} }; template <typename T> map<T, T> compress(vector<T> &A){ map<T, T> comp; int N = A.size(), i=0; for (int i=0; i<N; i++) comp[A[i]]; for (auto &e : comp){ e.second = i; i++; } return comp; } int main(){ cin.tie(nullptr); ios_base::sync_with_stdio(false); ll N; cin >> N; vector<ll> A(N); for (int i=0; i<N; i++) cin >> A[i]; map<ll, ll> mp = compress(A); BIT<mint> bitlc(N), bitrc(N), bitls(N), bitrs(N); mint ans = 0, lc, rc, ls, rs; for (int i=0; i<N; i++){ bitrc.add(mp[A[i]], 1); bitrs.add(mp[A[i]], A[i]); } for (int i=0; i<N; i++){ bitrc.add(mp[A[i]], -1); bitrs.add(mp[A[i]], -A[i]); lc = bitlc.sum(mp[A[i]]+1, N-1); rc = bitrc.sum(0, mp[A[i]]-1); ls = bitls.sum(mp[A[i]]+1, N-1); rs = bitrs.sum(0, mp[A[i]]-1); ans += lc*rc*A[i]; ans += ls*rc; ans += lc*rs; bitlc.add(mp[A[i]], 1); bitls.add(mp[A[i]], A[i]); } cout << ans << endl; return 0; }