結果

問題 No.1300 Sum of Inversions
ユーザー tko919
提出日時 2021-08-27 20:58:00
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 215 ms / 2,000 ms
コード長 3,312 bytes
コンパイル時間 2,369 ms
コンパイル使用メモリ 205,836 KB
最終ジャッジ日時 2025-01-24 02:19:43
ジャッジサーバーID
(参考情報)
judge5 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 34
権限があれば一括ダウンロードができます

ソースコード

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

#define _USE_MATH_DEFINES
#include <bits/stdc++.h>
using namespace std;
//template
#define rep(i,a,b) for(int i=(int)(a);i<(int)(b);i++)
#define ALL(v) (v).begin(),(v).end()
using ll=long long int;
const int inf = 0x3fffffff; const ll INF = 0x1fffffffffffffff; const double eps=1e-12;
template<typename 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){if(a>b){a=b;return 1;}return 0;}
template<int mod=998244353>struct fp {
int v; static int get_mod(){return mod;}
int inv() const{
int tmp,a=v,b=mod,x=1,y=0;
while(b)tmp=a/b,a-=tmp*b,swap(a,b),x-=tmp*y,swap(x,y);
if(x<0){x+=mod;} return x;
}
fp(ll x=0){init(x%mod+mod);}
fp& init(int x){v=(x<mod?x:x-mod); return *this;}
fp operator-()const{return fp()-*this;}
fp pow(ll t){fp res=1,b=*this; while(t){if(t&1)res*=b;b*=b;t>>=1;} return res;}
fp& operator+=(const fp& x){return init(v+x.v);}
fp& operator-=(const fp& x){return init(v+mod-x.v);}
fp& operator*=(const fp& x){v=ll(v)*x.v%mod; return *this;}
fp& operator/=(const fp& x){v=ll(v)*x.inv()%mod; return *this;}
fp operator+(const fp& x)const{return fp(*this)+=x;}
fp operator-(const fp& x)const{return fp(*this)-=x;}
fp operator*(const fp& x)const{return fp(*this)*=x;}
fp operator/(const fp& x)const{return fp(*this)/=x;}
bool operator==(const fp& x)const{return v==x.v;}
bool operator!=(const fp& x)const{return v!=x.v;}
}; using Fp=fp<>;
template<typename T>struct factorial {
vector<T> Fact,Finv,Inv;
factorial(int maxx){
Fact.resize(maxx); Finv.resize(maxx); Inv.resize(maxx);
Fact[0]=Fact[1]=Finv[0]=Finv[1]=Inv[1]=1;
rep(i,2,maxx){Fact[i]=Fact[i-1]*i;} Finv[maxx-1]=Fact[maxx-1].inv();
for(int i=maxx-1;i>=2;i--){Finv[i-1]=Finv[i]*i; Inv[i]=Finv[i]*Fact[i-1];}
}
T fact(int n,bool inv=0){if(n<0)return 0; return (inv?Finv[n]:Fact[n]);}
T inv(int n){if(n<0)return 0; return Inv[n];}
T nPr(int n,int r,bool inv=0){if(n<0||n<r||r<0)return 0; return fact(n,inv)*fact(n-r,inv^1);}
T nCr(int n,int r,bool inv=0){if(n<0||n<r||r<0)return 0; return fact(n,inv)*fact(r,inv^1)*fact(n-r,inv^1);}
T nHr(int n,int r,bool inv=0){return nCr(n+r-1,r,inv);}
};
template<typename T>struct BIT{
int n; T all=0; vector<T> val;
BIT(int _n):n(_n),val(_n+10){}
void clear(){val.assign(n+10,0); all=T();}
void add(int i,T x){
for(i++;i<=n;i+=(i&-i))val[i]+=x;
all+=x;
}
T sum(int i){
T res=0;
for(i++;i;i-=(i&-i))res+=val[i];
return res;
}
};
int main(){
int n;
cin>>n;
vector<int> a(n),vs;
rep(i,0,n){
cin>>a[i];
vs.push_back(a[i]);
}
sort(ALL(vs));
vs.erase(unique(ALL(vs)),vs.end());
rep(i,0,n)a[i]=lower_bound(ALL(vs),a[i])-vs.begin();
BIT<Fp> L(201010),R(201010),Lcnt(201010),Rcnt(201010);
rep(i,0,n){
R.add(a[i],vs[a[i]]);
Rcnt.add(a[i],1);
}
Fp res;
rep(i,0,n-1){
R.add(a[i],-vs[a[i]]);
Rcnt.add(a[i],-1);
if(i){
L.add(a[i-1],vs[a[i-1]]);
Lcnt.add(a[i-1],1);
res+=(L.all-L.sum(a[i]))*Rcnt.sum(a[i]-1);
res+=R.sum(a[i]-1)*(Lcnt.all-Lcnt.sum(a[i]));
res+=(Lcnt.all-Lcnt.sum(a[i]))*Rcnt.sum(a[i]-1)*vs[a[i]];
}
}
cout<<res.v<<'\n';
return 0;
}
הההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההה
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
0