結果

問題 No.121 傾向と対策:門松列(その2)
ユーザー 沙耶花沙耶花
提出日時 2021-10-26 22:17:55
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 1,094 ms / 5,000 ms
コード長 1,098 bytes
コンパイル時間 5,111 ms
コンパイル使用メモリ 261,032 KB
実行使用メモリ 81,428 KB
最終ジャッジ日時 2024-04-15 22:03:58
合計ジャッジ時間 9,631 ms
ジャッジサーバーID
(参考情報)
judge2 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 23 ms
6,360 KB
testcase_01 AC 35 ms
8,144 KB
testcase_02 AC 4 ms
5,376 KB
testcase_03 AC 251 ms
37,364 KB
testcase_04 AC 1,094 ms
81,428 KB
testcase_05 AC 247 ms
37,364 KB
testcase_06 AC 217 ms
34,784 KB
testcase_07 AC 273 ms
34,908 KB
testcase_08 AC 300 ms
37,220 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <stdio.h>
#include <bits/stdc++.h>
#include <atcoder/all>
using namespace atcoder;
using mint = modint1000000007;
using namespace std;
#define rep(i,n) for (int i = 0; i < (n); ++i)
#define Inf 1000000000



int main(){
	
	int n;
	cin>>n;
	vector<int> a(n);
	rep(i,n)scanf("%d",&a[i]);
	
	auto t = a;
	sort(t.begin(),t.end());
	t.erase(unique(t.begin(),t.end()),t.end());
	rep(i,n){
		a[i] = distance(t.begin(),lower_bound(t.begin(),t.end(),a[i]));
	}
	
	vector<long long> Rm(n),RM(n);
	{
		fenwick_tree<long long> F(t.size());
		for(int i=n-1;i>=0;i--){
			Rm[i] = F.sum(0,a[i]);
			RM[i] = F.sum(a[i]+1,t.size());
			F.add(a[i],1LL);
		}
	
	}
	long long ans = 0LL;
	{
		fenwick_tree<long long> F(t.size());
		rep(i,n){
			ans += Rm[i] * F.sum(0,a[i]);
			ans += RM[i] * F.sum(a[i]+1,t.size());
			F.add(a[i],1LL);
		}
	}
	
	vector<vector<long long>> dp(t.size());
	rep(i,n){
		dp[a[i]].push_back(i);
	}
	
	rep(i,t.size()){
		long long cur = 0LL;
		for(int j=1;j<dp[i].size();j++){
			cur += (dp[i][j] - dp[i][j-1] - 1) * j;
			ans -= cur;
		}
	}
	
	cout<<ans<<endl;
	
		
	
	
	return 0;
}
0