結果
| 問題 | No.694 square1001 and Permutation 3 | 
| コンテスト | |
| ユーザー |  startcpp | 
| 提出日時 | 2018-06-20 11:11:25 | 
| 言語 | C++11(廃止可能性あり) (gcc 13.3.0) | 
| 結果 | 
                                AC
                                 
                             | 
| 実行時間 | 1,158 ms / 3,000 ms | 
| コード長 | 1,409 bytes | 
| コンパイル時間 | 641 ms | 
| コンパイル使用メモリ | 66,020 KB | 
| 実行使用メモリ | 23,040 KB | 
| 最終ジャッジ日時 | 2024-06-30 17:23:49 | 
| 合計ジャッジ時間 | 9,357 ms | 
| ジャッジサーバーID (参考情報) | judge3 / judge4 | 
(要ログイン)
| ファイルパターン | 結果 | 
|---|---|
| sample | AC * 1 | 
| other | AC * 13 | 
ソースコード
#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;
}
            
            
            
        