結果

問題 No.404 部分門松列
ユーザー antaanta
提出日時 2016-07-21 01:37:12
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
RE  
実行時間 -
コード長 2,689 bytes
コンパイル時間 1,977 ms
コンパイル使用メモリ 173,616 KB
実行使用メモリ 5,376 KB
最終ジャッジ日時 2024-04-24 01:33:53
合計ジャッジ時間 8,468 ms
ジャッジサーバーID
(参考情報)
judge4 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

#include "bits/stdc++.h"
using namespace std;
#define rep(i,n) for(int (i)=0;(i)<(int)(n);++(i))
#define rer(i,l,u) for(int (i)=(int)(l);(i)<=(int)(u);++(i))
#define reu(i,l,u) for(int (i)=(int)(l);(i)<(int)(u);++(i))
static const int INF = 0x3f3f3f3f; static const long long INFL = 0x3f3f3f3f3f3f3f3fLL;
typedef vector<int> vi; typedef pair<int, int> pii; typedef vector<pair<int, int> > vpii; typedef long long ll;
template<typename T, typename U> static void amin(T &x, U y) { if(y < x) x = y; }
template<typename T, typename U> static void amax(T &x, U y) { if(x < y) x = y; }

#ifdef NDEBUG
#error assert is disabled!
#endif

template<typename T>
T readNatural(T lo, T up) {
	assert(0 <= lo && lo <= up);
	T x = 0;
	while(1) {
		int d = getchar();
		if(!('0' <= d && d <= '9')) {
			ungetc(d, stdin);
			break;
		}
		d -= '0';
		assert(d <= up && x <= (up - d) / 10);
		x = x * 10 + d;
	}
	assert(lo <= x && x <= up);
	return x;
}

void readSpace() { int c = getchar(); assert(c == ' '); }
static bool read_eof = false;
void readEOL() {
	int c = getchar();
	if(c == EOF) {
		assert(!read_eof);
		read_eof = true;
	} else {
		assert(c == '\n');
	}
}
void readEOF() {
	assert(!read_eof);
	int c = getchar();
	assert(c == EOF);
	read_eof = true;
}


struct FenwickTree {
	typedef int T;
	vector<T> v;
	void init(int n) { v.assign(n, 0); }
	void add(int i, T x) {
		for(; i < (int)v.size(); i |= i + 1) v[i] += x;
	}
	T sum(int i) const {	//[0, i)
		T r = 0;
		for(-- i; i >= 0; i = (i & (i + 1)) - 1) r += v[i];
		return r;
	}
	T sum(int left, int right) const {	//[left, right)
		return sum(right) - sum(left);
	}
};

int main() {
	int N = readNatural(1, 100000);
	readEOL();
	vector<int> A(N);
	for(int i = 0; i < N; ++ i) {
		if(i != 0) readSpace();
		A[i] = readNatural(1, (int)1e9);
	}
	readEOL();
	readEOF();
	vector<int> values = A;
	sort(values.begin(), values.end());
	values.erase(unique(values.begin(), values.end()), values.end());
	for(vector<int>::iterator it = A.begin(); it != A.end(); ++ it)
		*it = lower_bound(values.begin(), values.end(), *it) - values.begin();
	int X = values.size();
	vi cnt(X, 0), sum(X + 1, 0);
	rep(i, N)
		++ cnt[A[i]];
	rep(i, X)
		sum[i + 1] = sum[i] + cnt[i];
	FenwickTree ft;
	ft.init(X);
	vi curcnt(X, 0);
	vector<ll> cursum(X, 0);
	vi firstpos(X, -1);
	ll ans = 0;
	rep(i, N) {
		int a = A[i];
		int xL = ft.sum(a);
		int yL = i - xL - curcnt[a];
		ans += (ll)xL * (sum[a] - xL);
		ans += (ll)yL * (sum[X] - sum[a + 1] - yL);

		ans -= (ll)curcnt[a] * (i - 1) - cursum[a];

		ft.add(a, 1);
		++ curcnt[a];
		cursum[a] += i;
	}
	rep(a, X) {
		int n = cnt[a];
		ans += (ll)n * (n - 1) * (n - 2) / 6;
	}
	printf("%lld\n", ans);
	return 0;
}
0