結果
問題 | No.694 square1001 and Permutation 3 |
ユーザー |
![]() |
提出日時 | 2018-06-08 22:57:15 |
言語 | C++14 (gcc 13.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 2,127 ms / 3,000 ms |
コード長 | 1,142 bytes |
コンパイル時間 | 2,005 ms |
コンパイル使用メモリ | 187,852 KB |
実行使用メモリ | 50,192 KB |
最終ジャッジ日時 | 2024-06-30 10:56:16 |
合計ジャッジ時間 | 13,487 ms |
ジャッジサーバーID (参考情報) |
judge1 / judge5 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 1 |
other | AC * 13 |
ソースコード
#include <bits/stdc++.h>using namespace std;// one-based numberingstruct Fenwick {vector<int> bit; int N;Fenwick(int n) {N = n;bit.resize(n+1, 0);}void add(int a, int w) {for (int x = a; x <= N; x += x & -x) bit[x] += w;}int sum(int a) {int ret = 0;for (int x = a; x > 0; x -= x & -x) ret += bit[x];return ret;}};int main(){int n; cin >> n;vector<int> v(n); vector< pair<long long, int> > p(n);vector<long long> a(n);map<long long, int> ma;for (int i = 0; i < n; i++) {cin >> p[i].first; p[i].second = i;a[i] = p[i].first;if (ma.find(p[i].first) != ma.end()) ma[p[i].first]++;else ma.insert(make_pair(p[i].first, 1));}sort(p.begin(), p.end());for (int i = 0; i < n; i++) {v[p[i].second] = i+1;}long long tmp = 0;Fenwick f = Fenwick(n);for (int i = 0; i < n; i++) {tmp += (long long) (i - f.sum(v[i]));f.add(v[i], 1);}cout << tmp << endl;for (int i = 0; i < n-1; i++) {tmp += (n-v[i]) - (v[i]-1) - (ma[a[i]]-1);ma[a[i]] -= 2;cout << tmp << endl;}return 0;}