結果

問題 No.1300 Sum of Inversions
ユーザー okok
提出日時 2020-11-27 22:06:17
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 2,659 bytes
コンパイル時間 1,292 ms
コンパイル使用メモリ 103,312 KB
実行使用メモリ 24,332 KB
最終ジャッジ日時 2023-10-01 06:03:19
合計ジャッジ時間 18,530 ms
ジャッジサーバーID
(参考情報)
judge14 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,380 KB
testcase_01 AC 1 ms
4,380 KB
testcase_02 AC 2 ms
4,380 KB
testcase_03 WA -
testcase_04 WA -
testcase_05 WA -
testcase_06 WA -
testcase_07 WA -
testcase_08 WA -
testcase_09 WA -
testcase_10 WA -
testcase_11 WA -
testcase_12 WA -
testcase_13 WA -
testcase_14 WA -
testcase_15 WA -
testcase_16 WA -
testcase_17 WA -
testcase_18 WA -
testcase_19 WA -
testcase_20 WA -
testcase_21 WA -
testcase_22 WA -
testcase_23 WA -
testcase_24 WA -
testcase_25 WA -
testcase_26 WA -
testcase_27 WA -
testcase_28 WA -
testcase_29 WA -
testcase_30 WA -
testcase_31 WA -
testcase_32 WA -
testcase_33 AC 316 ms
24,032 KB
testcase_34 AC 331 ms
24,328 KB
testcase_35 WA -
testcase_36 WA -
権限があれば一括ダウンロードができます

ソースコード

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) * segr.query(0, C[i]) * A[i];
        ans += segl.query(C[i]+1,N) * segr2.query(0, C[i]);
        ans += segl2.query(C[i]+1,N) * segr.query(0, C[i]);
        
        // cout<<i<<" "<<ans<<endl;
        
        segl.update(C[i], 1);
        segl2.update(C[i], A[i]);
    }
    
    cout<<ans<<endl;
    
	return 0;
}
0