結果

問題 No.121 傾向と対策:門松列(その2)
ユーザー 沙耶花
提出日時 2021-10-26 22:17:55
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 1,004 ms / 5,000 ms
コード長 1,098 bytes
コンパイル時間 4,825 ms
コンパイル使用メモリ 257,056 KB
最終ジャッジ日時 2025-01-25 07:25:46
ジャッジサーバーID
(参考情報)
judge5 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 9
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.cpp: In function ‘int main()’:
main.cpp:17:22: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
   17 |         rep(i,n)scanf("%d",&a[i]);
      |                 ~~~~~^~~~~~~~~~~~

ソースコード

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