結果
| 問題 |
No.694 square1001 and Permutation 3
|
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2019-04-01 21:51:22 |
| 言語 | C++14 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
AC
|
| 実行時間 | 1,319 ms / 3,000 ms |
| コード長 | 1,373 bytes |
| コンパイル時間 | 1,750 ms |
| コンパイル使用メモリ | 182,608 KB |
| 実行使用メモリ | 44,296 KB |
| 最終ジャッジ日時 | 2024-11-26 22:31:35 |
| 合計ジャッジ時間 | 9,567 ms |
|
ジャッジサーバーID (参考情報) |
judge1 / judge4 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 1 |
| other | AC * 13 |
ソースコード
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
//引数unzipには圧縮前のvectorを入れる
int compress(vector<ll>& unzip, map<ll, int>& zip) {
sort(unzip.begin(), unzip.end());
unzip.erase(unique(unzip.begin(), unzip.end()), unzip.end());
for (int i = 0; i < unzip.size(); i++) {
zip[unzip[i]] = i;
}
return unzip.size();
}
//1-indexであることに注意
template <typename T>
struct BIT {
int n;
vector<T> data;
BIT(int n) : n(n), data(n + 1, 0) {}
T sum(int i) {
T s = 0;
while (i > 0) {
s += data[i];
i -= i & -i;
}
return s;
}
void add(int i, T x) {
while (i <= n) {
data[i] += x;
i += i & -i;
}
}
};
int main() {
cin.tie(0);
ios::sync_with_stdio(false);
int n;
cin >> n;
vector<ll> a(n);
for (int i = 0; i < n; i++) cin >> a[i];
vector<ll> unzip(a);
map<ll, int> zip;
compress(unzip, zip);
ll ans = 0;
BIT<int> b(unzip.size());
for (int i = 0; i < n; i++) {
ans += i - b.sum(zip[a[i]] + 1);
b.add((zip[a[i]] + 1), 1);
}
cout << ans << "\n";
for (int i = 0; i < n - 1; i++) {
ans -= b.sum(zip[a[i]]);
ans += n - b.sum(zip[a[i]] + 1);
cout << ans << "\n";
}
return 0;
}