結果
| 問題 |
No.1300 Sum of Inversions
|
| コンテスト | |
| ユーザー |
milanis48663220
|
| 提出日時 | 2020-11-27 21:51:48 |
| 言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
AC
|
| 実行時間 | 863 ms / 2,000 ms |
| コード長 | 4,821 bytes |
| コンパイル時間 | 1,580 ms |
| コンパイル使用メモリ | 108,808 KB |
| 最終ジャッジ日時 | 2025-01-16 06:59:32 |
|
ジャッジサーバーID (参考情報) |
judge2 / judge5 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 3 |
| other | AC * 34 |
ソースコード
#include <iostream>
#include <algorithm>
#include <iomanip>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <cassert>
#define debug_value(x) cerr << "line" << __LINE__ << ":<" << __func__ << ">:" << #x << "=" << x << endl;
#define debug(x) cerr << "line" << __LINE__ << ":<" << __func__ << ">:" << x << endl;
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }
using namespace std;
typedef long long ll;
const ll MOD = 998244353;
class ModInt{
public:
ll v;
ModInt(ll _v = 0){
if(_v >= MOD) _v %= MOD;
v = _v;
}
ModInt operator+(ll n){
return ModInt((v+n)%MOD);
}
ModInt operator-(ll n){
return ModInt((v-n+MOD)%MOD);
}
ModInt operator*(ll n){
if(n >= MOD) n %= MOD;
return ModInt((v*n)%MOD);
}
ModInt operator/(ll n){
return ModInt((ModInt(n).inv()*v).v%MOD);
}
void operator+=(ll n){
v = (v+n)%MOD;
}
void operator-=(ll n){
v = (v-n+MOD)%MOD;
}
void operator*=(ll n){
v = (v*n)%MOD;
}
ModInt operator+(ModInt n){
return ModInt((v+n.v)%MOD);
}
ModInt operator-(ModInt n){
return ModInt((v-n.v+MOD)%MOD);
}
ModInt operator*(ModInt n){
return ModInt((v*n.v)%MOD);
}
ModInt operator/(ModInt n){
return ModInt((n.inv()*v).v%MOD);
}
void operator+=(ModInt n){
v = (v+n.v)%MOD;
}
void operator-=(ModInt n){
v = (v-n.v+MOD)%MOD;
}
void operator*=(ModInt n){
v = (v*n.v)%MOD;
}
void operator=(ModInt n){
v = n.v;
}
void operator=(ll n){
v = n;
}
ModInt inv(){
if(v == 1) return ModInt(1);
else return ModInt(MOD-ModInt(MOD%v).inv().v*(MOD/v)%MOD);
}
};
ostream& operator<<(ostream& os, const ModInt& m){
os << m.v;
return os;
}
istream & operator >> (istream &in, ModInt &m){
in >> m.v;
return in;
}
int n;
ll a[200000];
ModInt cntl[200000], cntr[200000], suml[200000], sumr[200000];
map<ll, int> compress;
void make_compress(){
set<ll> st;
for(int i = 0; i < n; i++) st.insert(a[i]);
int cnt = 0;
for(ll x : st){
compress[x] = cnt;
cnt++;
}
}
template <typename T>
struct segtree{
int n;
T UNIT;
vector<T> dat;
T (*calc)(T, T);
segtree(int n_, T unit, T (*_calc)(T, T)){
UNIT = unit;
calc = _calc;
n = 1;
while(n < n_) n *= 2;
dat = vector<T>(2*n);
for(int i = 0; i < 2*n; i++) dat[i] = UNIT;
}
void insert(int k, T a){
dat[k+n-1] = a;
}
void update_all(){
for(int i = n-2; i >= 0; i--){
dat[i] = calc(dat[i*2+1], dat[i*2+2]);
}
}
//k番目の値(0-indexed)をaに変更
void update(int k, T a){
k += n-1;
dat[k] = a;
while(k > 0){
k = (k-1)/2;
dat[k] = calc(dat[k*2+1], dat[k*2+2]);
}
}
//[a, b)
//区間[a, b]へのクエリに対してはquery(a, b+1)と呼ぶ
T query(int a, int b, int k=0, int l=0, int r=-1){
if(r < 0) r = n;
if(r <= a || b <= l) return UNIT;
if(a <= l && r <= b) return dat[k];
else{
T vl = query(a, b, k*2+1, l, (l+r)/2);
T vr = query(a, b, k*2+2, (l+r)/2, r);
return calc(vl, vr);
}
}
};
struct node{
ModInt cnt, sum;
};
node add_node(node a, node b){
return (node){a.cnt+b.cnt, a.sum+b.sum};
}
int main(){
ios::sync_with_stdio(false);
cin.tie(0);
cout << setprecision(10) << fixed;
cin >> n;
for(int i = 0; i < n; i++){
cin >> a[i];
}
make_compress();
int m = compress.size();
node unit = (node){0, 0};
segtree<node> sgtl(m, unit, add_node);
segtree<node> sgtr(m, unit, add_node);
for(int i = 0; i < n; i++){
int idx = compress[a[i]];
auto nd = sgtl.query(idx+1, m);
cntl[i] = nd.cnt;
suml[i] = nd.sum;
auto tmp = sgtl.query(idx, idx+1);
tmp.cnt+=1;
tmp.sum+=a[i];
sgtl.update(idx, tmp);
}
for(int i = n-1; i >= 0; i--){
int idx = compress[a[i]];
auto nd = sgtr.query(0, idx);
cntr[i] = nd.cnt;
sumr[i] = nd.sum;
auto tmp = sgtr.query(idx, idx+1);
tmp.cnt+=1;
tmp.sum+=a[i];
sgtr.update(idx, tmp);
}
ModInt ans = 0;
for(int i = 1; i < n-1; i++){
ans += cntl[i]*sumr[i];
ans += cntr[i]*suml[i];
ans += cntl[i]*cntr[i]*a[i];
// cout << suml[i] << ' ' << sumr[i] << endl;
}
cout << ans << endl;
}
milanis48663220