結果

問題 No.121 傾向と対策:門松列(その2)
ユーザー 👑 kmjpkmjp
提出日時 2015-01-08 23:52:40
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 2,392 ms / 5,000 ms
コード長 1,568 bytes
コンパイル時間 1,388 ms
コンパイル使用メモリ 153,404 KB
実行使用メモリ 70,108 KB
最終ジャッジ日時 2023-09-11 04:00:48
合計ジャッジ時間 6,915 ms
ジャッジサーバーID
(参考情報)
judge15 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 37 ms
15,812 KB
testcase_01 AC 60 ms
16,980 KB
testcase_02 AC 7 ms
13,904 KB
testcase_03 AC 336 ms
18,348 KB
testcase_04 AC 2,392 ms
70,108 KB
testcase_05 AC 320 ms
18,404 KB
testcase_06 AC 342 ms
17,336 KB
testcase_07 AC 374 ms
17,268 KB
testcase_08 AC 354 ms
18,348 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
typedef signed long long ll;

#undef _P
#define _P(...) (void)printf(__VA_ARGS__)
#define FOR(x,to) for(x=0;x<to;x++)
#define ITR(x,c) for(__typeof(c.begin()) x=c.begin();x!=c.end();x++)
#define ALL(a) (a.begin()),(a.end())
#define ZERO(a) memset(a,0,sizeof(a))
#define MINUS(a) memset(a,0xff,sizeof(a))
//-------------------------------------------------------

int N;
map<int,int> M;
int A[1010000];
int L[1010000],R[1010000];
ll ret,dif;


template<class V, int ME> class BIT {
public:
	V bit[1<<ME];
	int lower_bound(V val) {
		V tv=0; int i,ent=0;
		for(i=ME-1;i>=0;i--) if(tv+bit[ent+(1<<i)-1]<val) tv+=bit[ent+(1<<i)-1],ent+=(1<<i);
		return ent;
	}
	V total(int e) {V s=0;e++;while(e) s+=bit[e-1],e-=e&-e; return s;}
	void update(int e, V val) {e++; while(e<=1<<ME) bit[e-1]+=val,e+=e&-e;}
};
BIT<int,20> bt,bt2;

void solve() {
	int i,j,k,l,r,x,y; string s;
	
	cin>>N;
	FOR(i,N) cin>>A[i], M[A[i]]=0;
	
	x=0;
	ITR(it,M) it->second=++x;
	FOR(i,N) A[i]=M[A[i]], R[A[i]]++;
	
	FOR(i,N) bt2.update(A[i],1);
	FOR(i,N) {
		bt2.update(A[i],-1);
		ret += 1LL*bt.total(A[i]-1)*bt2.total(A[i]-1);
		ret += 1LL*(i-bt.total(A[i]))*((N-i-1)-bt2.total(A[i]));
		bt.update(A[i],1);
	}
	FOR(i,N) {
		dif -= 1LL*L[A[i]]*R[A[i]];
		ret -= dif;
		L[A[i]]++;
		R[A[i]]--;
		dif += 1LL*L[A[i]]*R[A[i]];
	}
	
	
	cout<<ret-dif<<endl;
}


int main(int argc,char** argv){
	string s;int i;
	if(argc==1) ios::sync_with_stdio(false);
	FOR(i,argc-1) s+=argv[i+1],s+='\n';
	FOR(i,s.size()) ungetc(s[s.size()-1-i],stdin);
	solve(); return 0;
}
0