結果

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

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,376 KB
testcase_01 AC 2 ms
4,380 KB
testcase_02 AC 2 ms
4,380 KB
testcase_03 AC 2 ms
4,376 KB
testcase_04 AC 1 ms
4,380 KB
testcase_05 AC 2 ms
4,380 KB
testcase_06 AC 2 ms
4,380 KB
testcase_07 AC 162 ms
7,268 KB
testcase_08 AC 254 ms
7,624 KB
testcase_09 AC 290 ms
9,244 KB
testcase_10 AC 151 ms
7,336 KB
testcase_11 AC 299 ms
9,228 KB
testcase_12 AC 295 ms
9,208 KB
testcase_13 AC 2 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