結果

問題 No.1300 Sum of Inversions
ユーザー furafura
提出日時 2020-11-27 23:32:52
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 130 ms / 2,000 ms
コード長 3,211 bytes
コンパイル時間 2,341 ms
コンパイル使用メモリ 211,260 KB
実行使用メモリ 9,508 KB
最終ジャッジ日時 2023-10-10 21:47:37
合計ジャッジ時間 7,328 ms
ジャッジサーバーID
(参考情報)
judge12 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,352 KB
testcase_01 AC 2 ms
4,352 KB
testcase_02 AC 1 ms
4,348 KB
testcase_03 AC 101 ms
7,776 KB
testcase_04 AC 97 ms
8,056 KB
testcase_05 AC 78 ms
7,264 KB
testcase_06 AC 113 ms
8,428 KB
testcase_07 AC 111 ms
8,320 KB
testcase_08 AC 123 ms
8,864 KB
testcase_09 AC 121 ms
8,524 KB
testcase_10 AC 63 ms
6,144 KB
testcase_11 AC 64 ms
6,316 KB
testcase_12 AC 102 ms
7,732 KB
testcase_13 AC 97 ms
7,576 KB
testcase_14 AC 130 ms
9,156 KB
testcase_15 AC 122 ms
8,588 KB
testcase_16 AC 102 ms
8,068 KB
testcase_17 AC 62 ms
6,260 KB
testcase_18 AC 73 ms
6,844 KB
testcase_19 AC 86 ms
7,284 KB
testcase_20 AC 91 ms
7,272 KB
testcase_21 AC 88 ms
7,200 KB
testcase_22 AC 78 ms
7,128 KB
testcase_23 AC 114 ms
8,368 KB
testcase_24 AC 81 ms
6,952 KB
testcase_25 AC 68 ms
6,492 KB
testcase_26 AC 68 ms
6,608 KB
testcase_27 AC 76 ms
6,676 KB
testcase_28 AC 126 ms
8,848 KB
testcase_29 AC 86 ms
7,272 KB
testcase_30 AC 120 ms
8,628 KB
testcase_31 AC 77 ms
7,260 KB
testcase_32 AC 82 ms
7,312 KB
testcase_33 AC 79 ms
9,388 KB
testcase_34 AC 93 ms
9,440 KB
testcase_35 AC 83 ms
9,396 KB
testcase_36 AC 89 ms
9,508 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>

#define rep(i,n) for(int i=0;i<(n);i++)

using namespace std;

template<class G>
class Fenwick_tree{
	vector<G> a;
public:
	Fenwick_tree(){}
	Fenwick_tree(int n){ build(n); }
	Fenwick_tree(const vector<G>& a){ build(a); }
	void build(int n){
		a.assign(n,G{});
	}
	void build(const vector<G>& a){
		this->a=a;
		for(int i=1;i<a.size();i++) if(i+(i&-i)-1<a.size()) (this->a)[i+(i&-i)-1]+=(this->a)[i-1];
	}
	void add(int i,const G& val){
		for(i++;i<=a.size();i+=i&-i) a[i-1]+=val;
	}
	G sum(int l,int r)const{
		if(l==0){
			G res{};
			for(;r>0;r-=r&-r) res+=a[r-1];
			return res;
		}
		return sum(0,r)-sum(0,l);
	}
	int lower_bound(G val)const{
		if(val<=G{}) return 0;
		int x=0,k;
		for(k=1;k<=a.size();k<<=1);
		for(k>>=1;k>0;k>>=1) if(x+k<=a.size() && a[x+k-1]<val) val-=a[x+k-1], x+=k;
		return x;
	}
	int upper_bound(G val)const{
		if(val<G{}) return 0;
		int x=0,k;
		for(k=1;k<=a.size();k<<=1);
		for(k>>=1;k>0;k>>=1) if(x+k<=a.size() && a[x+k-1]<=val) val-=a[x+k-1], x+=k;
		return x;
	}
};

class mint{
	static const int MOD=998244353;
	int x;
public:
	mint():x(0){}
	mint(long long y){ x=y%MOD; if(x<0) x+=MOD; }

	mint& operator+=(const mint& m){ x+=m.x; if(x>=MOD) x-=MOD; return *this; }
	mint& operator-=(const mint& m){ x-=m.x; if(x<   0) x+=MOD; return *this; }
	mint& operator*=(const mint& m){ x=1LL*x*m.x%MOD; return *this; }
	mint& operator/=(const mint& m){ return *this*=inverse(m); }
	mint operator+(const mint& m)const{ return mint(*this)+=m; }
	mint operator-(const mint& m)const{ return mint(*this)-=m; }
	mint operator*(const mint& m)const{ return mint(*this)*=m; }
	mint operator/(const mint& m)const{ return mint(*this)/=m; }
	mint operator-()const{ return -x; }

	friend mint inverse(const mint& m){
		int a=m.x,b=MOD,u=1,v=0;
		while(b>0){ int t=a/b; a-=t*b; swap(a,b); u-=t*v; swap(u,v); }
		return u;
	}

	friend istream& operator>>(istream& is,mint& m){ long long t; is>>t; m=t; return is; }
	friend ostream& operator<<(ostream& os,const mint& m){ return os<<m.x; }
	int to_int()const{ return x; }
};

mint operator+(long long x,const mint& m){ return mint(x)+m; }
mint operator-(long long x,const mint& m){ return mint(x)-m; }
mint operator*(long long x,const mint& m){ return mint(x)*m; }
mint operator/(long long x,const mint& m){ return mint(x)/m; }

int main(){
	int n; scanf("%d",&n);
	vector<int> a(n);
	rep(i,n) scanf("%d",&a[i]);

	vector<int> p(n);
	iota(p.begin(),p.end(),0);
	sort(p.begin(),p.end(),[&](int i,int j){
		if(a[i]!=a[j]) return a[i]<a[j];
		return j>i;
	});

	// A_i + A_j + A_k の A_j からの寄与
	mint res1=0;
	Fenwick_tree<mint> F_big(n),F_small(n);
	for(int i:p) F_big.add(i,1);
	for(int i:p){
		F_big.add(i,-1);
		res1+=a[i]*F_big.sum(0,i)*F_small.sum(i+1,n);
		F_small.add(i,1);
	}

	// A_i + A_j + A_k の A_i からの寄与
	mint res2=0;
	Fenwick_tree<mint> F1(n),F2(n);
	for(int i:p){
		F1.add(i,1);
		F2.add(i,F1.sum(i+1,n));
		res2+=a[i]*F2.sum(i+1,n);
	}

	// A_i + A_j + A_k の A_k からの寄与
	mint res3=0;
	Fenwick_tree<mint> F3(n),F4(n);
	reverse(p.begin(),p.end());
	for(int i:p){
		F3.add(i,1);
		F4.add(i,F3.sum(0,i));
		res3+=a[i]*F4.sum(0,i);
	}

	cout<<res1+res2+res3<<'\n';

	return 0;
}
0