結果

問題 No.694 square1001 and Permutation 3
ユーザー e869120e869120
提出日時 2018-06-08 22:31:04
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 310 ms / 3,000 ms
コード長 982 bytes
コンパイル時間 707 ms
コンパイル使用メモリ 77,416 KB
実行使用メモリ 9,424 KB
最終ジャッジ日時 2023-09-12 23:57:29
合計ジャッジ時間 4,676 ms
ジャッジサーバーID
(参考情報)
judge12 / judge13
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,376 KB
testcase_01 AC 2 ms
4,380 KB
testcase_02 AC 1 ms
4,376 KB
testcase_03 AC 2 ms
4,380 KB
testcase_04 AC 2 ms
4,380 KB
testcase_05 AC 2 ms
4,380 KB
testcase_06 AC 2 ms
4,376 KB
testcase_07 AC 168 ms
7,308 KB
testcase_08 AC 262 ms
7,780 KB
testcase_09 AC 308 ms
9,424 KB
testcase_10 AC 152 ms
7,200 KB
testcase_11 AC 307 ms
9,212 KB
testcase_12 AC 310 ms
9,192 KB
testcase_13 AC 1 ms
4,380 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
#pragma warning (disable: 4996)

int bit[500009]; vector<int>vec;

void add(int pos, int x) {
	pos++;
	while (pos <= 500007) { bit[pos] += x; pos += (pos&-pos); }
}
int sum(int pos) {
	pos++; int s = 0;
	while (pos >= 1) { s += bit[pos]; pos -= (pos&-pos); }
	return s;
}
int Sum(int L, int R) {
	return sum(R) - sum(L - 1);
}

int n, a[500009];

int main() {
	cin >> n;
	for (int i = 1; i <= n; i++) {
		scanf("%d", &a[i]); vec.push_back(a[i]);
	}
	sort(vec.begin(), vec.end());
	vec.erase(unique(vec.begin(), vec.end()), vec.end());
	for (int i = 1; i <= n; i++) {
		a[i] = lower_bound(vec.begin(), vec.end(), a[i]) - vec.begin();
	}

	long long V = 0;
	for (int i = 1; i <= n; i++) {
		V += Sum(a[i] + 1, vec.size());
		add(a[i], 1);
	}
	for (int i = 1; i <= n; i++) {
		printf("%lld\n", V);
		add(a[i], -1);
		V -= Sum(0, a[i] - 1);
		V += Sum(a[i] + 1, vec.size());
		add(a[i], 1);
	}
	return 0;
}
0