結果

問題 No.694 square1001 and Permutation 3
ユーザー 夜
提出日時 2020-11-19 12:24:45
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 993 ms / 3,000 ms
コード長 1,245 bytes
コンパイル時間 901 ms
コンパイル使用メモリ 101,168 KB
実行使用メモリ 13,360 KB
最終ジャッジ日時 2023-09-30 16:15:36
合計ジャッジ時間 10,161 ms
ジャッジサーバーID
(参考情報)
judge14 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 3 ms
6,876 KB
testcase_01 AC 4 ms
6,852 KB
testcase_02 AC 3 ms
6,676 KB
testcase_03 AC 4 ms
6,708 KB
testcase_04 AC 5 ms
6,900 KB
testcase_05 AC 5 ms
6,672 KB
testcase_06 AC 4 ms
6,944 KB
testcase_07 AC 817 ms
13,360 KB
testcase_08 AC 918 ms
13,116 KB
testcase_09 AC 972 ms
12,880 KB
testcase_10 AC 792 ms
13,052 KB
testcase_11 AC 993 ms
13,320 KB
testcase_12 AC 993 ms
13,088 KB
testcase_13 AC 4 ms
6,980 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <string>
#include <algorithm>
#include <iostream>
#include <string>
#include <algorithm>
#include <vector>
#include <iomanip>
#include <cmath>
#include <stdio.h>
#include <queue>
#include <deque>
#include <cstdio>
#include <set>
#include <map>
#include <bitset>
#include <stack>
#include <cctype>
using namespace std;
long long n;
vector<long long> BIT(500050);
vector<pair<int, int>> vec;
int a[500050];
void add(long long i, long long x) {
	while (i <= n) {
		BIT[i] += x;
		i += i & -i;
	}
}
long long csum(long long i) {
	long long count = 0;
	while (i > 0) {
		count += BIT[i];
		i -= i & -i;
	}
	return count;
}
long long sum(long long l, long long r) {
	return csum(r) - csum(l - 1);
}
int main() {
	cin >> n;
	for (int i = 0; i < n; i++) {
		cin >> a[i];
		vec.emplace_back(make_pair(a[i], i));
	}
	sort(vec.begin(), vec.end());
	int now = 1;
	for (int i = 0; i < n; i++) {
		if (i != 0 && vec[i - 1].first != vec[i].first) {
			now++;
		}
		a[vec[i].second] = now;
	}
	long long ans = 0;
	for (int i = 0; i < n; i++) {
		ans += sum(a[i] + 1, now);
		add(a[i], 1);
	}
	cout << ans << endl;
	for (int i = 0; i < n - 1; i++) {
		ans -= sum(1, a[i] - 1);
		ans += sum(a[i] + 1, now);
		cout << ans << endl;
	}
}
0