結果

問題 No.694 square1001 and Permutation 3
ユーザー startcppstartcpp
提出日時 2018-06-20 11:11:25
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 1,165 ms / 3,000 ms
コード長 1,409 bytes
コンパイル時間 553 ms
コンパイル使用メモリ 67,900 KB
実行使用メモリ 23,032 KB
最終ジャッジ日時 2023-09-13 07:19:41
合計ジャッジ時間 9,351 ms
ジャッジサーバーID
(参考情報)
judge15 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 3 ms
11,540 KB
testcase_01 AC 3 ms
11,428 KB
testcase_02 AC 4 ms
11,472 KB
testcase_03 AC 4 ms
11,436 KB
testcase_04 AC 4 ms
11,520 KB
testcase_05 AC 4 ms
11,432 KB
testcase_06 AC 4 ms
11,528 KB
testcase_07 AC 871 ms
22,864 KB
testcase_08 AC 1,117 ms
22,904 KB
testcase_09 AC 1,138 ms
22,860 KB
testcase_10 AC 780 ms
23,028 KB
testcase_11 AC 1,165 ms
23,032 KB
testcase_12 AC 1,142 ms
22,816 KB
testcase_13 AC 4 ms
11,428 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <algorithm>
#include <vector>
#define int long long
using namespace std;
typedef pair<int, int> P;

struct BIT {
	int a[500001];
	BIT() { for (int i = 0; i <= 500000; i++) a[i] = 0; }
	void add(int pos, int w) { for (int i = pos; i <= 500000; i += i & -i) a[i] += w; }
	int sum(int pos) { int ret = 0; for (int i = pos; i > 0; i -= i & -i) ret += a[i]; return ret; }
};

int n;
int a[500000];
P valPos[500000];
BIT existFlag;
int sorted_a[500000];

signed main() {
	int i, j;
	
	cin >> n;
	for (i = 0; i < n; i++) { cin >> a[i]; valPos[i] = P(a[i], i); existFlag.add(i + 1, 1); }
	sort(valPos, valPos + n);
	
	int prevVal = -1;
	int ans = 0;
	for (i = 0; i < n; i++) {
		if (prevVal != valPos[i].first) {
			for (j = i; j < n; j++) {
				if (valPos[i].first != valPos[j].first) break;
				existFlag.add(valPos[j].second + 1, -1);
			}
			prevVal = valPos[i].first;
		}
		
		int hanten_number = existFlag.sum(valPos[i].second);
		ans += hanten_number;
	}
	
	cout << ans << endl;
	
	//差分計算
	for (i = 0; i < n; i++) sorted_a[i] = a[i]; sort(sorted_a, sorted_a + n);
	
	for (i = 0; i < n - 1; i++) {
		int minus = lower_bound(sorted_a, sorted_a + n, a[i]) - sorted_a;		//a[i]未満の要素の個数
		int plus = n - (upper_bound(sorted_a, sorted_a + n, a[i]) - sorted_a);	//a[i]を超える要素の個数
		ans -= minus;
		ans += plus;
		cout << ans << endl;
	}
	return 0;
}
0