結果

問題 No.1300 Sum of Inversions
ユーザー okok
提出日時 2020-11-27 22:08:10
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 631 ms / 2,000 ms
コード長 2,724 bytes
コンパイル時間 1,554 ms
コンパイル使用メモリ 103,248 KB
実行使用メモリ 24,200 KB
最終ジャッジ日時 2023-10-01 06:09:53
合計ジャッジ時間 18,686 ms
ジャッジサーバーID
(参考情報)
judge11 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 KB
testcase_01 AC 1 ms
4,380 KB
testcase_02 AC 1 ms
4,376 KB
testcase_03 AC 492 ms
23,028 KB
testcase_04 AC 480 ms
22,784 KB
testcase_05 AC 414 ms
14,152 KB
testcase_06 AC 606 ms
23,680 KB
testcase_07 AC 560 ms
23,272 KB
testcase_08 AC 592 ms
23,560 KB
testcase_09 AC 591 ms
23,556 KB
testcase_10 AC 339 ms
13,684 KB
testcase_11 AC 309 ms
13,672 KB
testcase_12 AC 478 ms
23,096 KB
testcase_13 AC 470 ms
22,844 KB
testcase_14 AC 631 ms
24,064 KB
testcase_15 AC 584 ms
23,740 KB
testcase_16 AC 495 ms
23,044 KB
testcase_17 AC 294 ms
13,588 KB
testcase_18 AC 333 ms
13,796 KB
testcase_19 AC 417 ms
22,500 KB
testcase_20 AC 468 ms
22,516 KB
testcase_21 AC 461 ms
22,624 KB
testcase_22 AC 414 ms
14,184 KB
testcase_23 AC 574 ms
23,272 KB
testcase_24 AC 383 ms
14,380 KB
testcase_25 AC 326 ms
13,916 KB
testcase_26 AC 320 ms
13,916 KB
testcase_27 AC 367 ms
14,228 KB
testcase_28 AC 605 ms
23,816 KB
testcase_29 AC 423 ms
22,500 KB
testcase_30 AC 589 ms
23,676 KB
testcase_31 AC 369 ms
14,188 KB
testcase_32 AC 397 ms
14,340 KB
testcase_33 AC 322 ms
24,096 KB
testcase_34 AC 332 ms
24,088 KB
testcase_35 AC 362 ms
24,196 KB
testcase_36 AC 366 ms
24,200 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<iostream>
#include<string>
#include<iomanip>
#include<cmath>
#include<vector>
#include<algorithm>
#include<utility>
#include <functional>
#include <limits>

using namespace std;

#define int long long
#define endl "\n"

constexpr long long INF = (long long)1e18;
constexpr long long MOD = 998244353; 

struct fast_io {
	fast_io(){
		std::cin.tie(nullptr);
		std::ios::sync_with_stdio(false);
	};
} fio;


template<typename T>
class segment_tree{
	int n;
	T fval;
	function<T(T,T)> operation;
	vector<T> dat;
	
	T _query(int a, int b, int k, int l, int r){
		if(r <= a || b <= l){
			return fval;
		}
		
		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 operation(vl, vr);
		}
	}
	
public:
	
	segment_tree(){
		
	}
	
	segment_tree(int n_, T val, function<T(T,T)> fun){
		init(n_, val, fun);
	}
	
	~segment_tree(){
		
	}
	
	void init(int n_, T val, function<T(T,T)> fun){
		fval = val;
		
		operation = fun;
		
		n = 1;
		
		while(n < n_) n *= 2;
		
		dat.resize(2 * n - 1);
	
		for(int i = 0; i < 2 * n - 1; i++) dat[i] = fval;
	}

	void update(int k, T a){
		k += n - 1;
		dat[k] += a;
		while(k > 0){
			k = (k - 1) / 2;
			dat[k] = operation(dat[k*2 + 1],dat[k*2 + 2]);
		}
	}
	
	T query(int a, int b){
		return _query(a, b, 0, 0, n);
	}
	
};
signed main(){
	cout<<fixed<<setprecision(10);
	
	int N;
    int ans = 0;
    vector<int> A, B, C;
    segment_tree<int> segl, segr;
    segment_tree<int> segl2, segr2;
    cin>>N;
	
    A.resize(N);
    B.resize(N);
    C.resize(N);
	segl.init(N+1,0, [](int first, int second){ return (first+second);});
	segr.init(N+1,0, [](int first, int second){ return (first+second);});
    
	segl2.init(N+1,0, [](int first, int second){ return (first+second);});
	segr2.init(N+1,0, [](int first, int second){ return (first+second);});
    for(int i = 0; i < N; i++){
        cin>>A[i];
    }
    
    B = A;
    C = A;
    
    sort(B.begin(), B.end());
    
    for(int i = 0; i < N; i++){
        C[i] = lower_bound(B.begin(), B.end(),A[i]) - B.begin();
        
        segr.update(C[i], 1);
        segr2.update(C[i], A[i]);
    }
    
    for(int i = 0; i < N; i++){
        segr.update(C[i], -1);
        segr2.update(C[i], -A[i]);
        
        ans += segl.query(C[i]+1,N)%MOD * (segr.query(0, C[i])%MOD) % MOD * A[i] % MOD;
        ans += segl.query(C[i]+1,N)%MOD * (segr2.query(0, C[i])%MOD) % MOD;
        ans += segl2.query(C[i]+1,N)%MOD * (segr.query(0, C[i])%MOD) % MOD;
        ans %= MOD;
        // cout<<i<<" "<<ans<<endl;
        
        segl.update(C[i], 1);
        segl2.update(C[i], A[i]);
    }
    
    cout<<ans<<endl;
    
	return 0;
}
0