結果
問題 | No.694 square1001 and Permutation 3 |
ユーザー |
![]() |
提出日時 | 2018-06-08 23:26:42 |
言語 | C++11 (gcc 13.3.0) |
結果 |
AC
|
実行時間 | 909 ms / 3,000 ms |
コード長 | 2,329 bytes |
コンパイル時間 | 1,475 ms |
コンパイル使用メモリ | 168,832 KB |
実行使用メモリ | 18,816 KB |
最終ジャッジ日時 | 2024-06-30 11:06:47 |
合計ジャッジ時間 | 8,756 ms |
ジャッジサーバーID (参考情報) |
judge2 / judge3 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 1 |
other | AC * 13 |
ソースコード
#include <bits/stdc++.h>#define rep(i, a, n) for(int i = a; i < n; i++)#define repr(i, a, b) for(int i = a; i >= b; i--)#define int long long#define all(a) a.begin(), a.end()#define chmax(x, y) x = max(x, y)#define chmin(x, y) x = min(x, y)using namespace std;typedef pair<int, int> P;const int mod = 1000000007;const int INF = 1e12;struct BIT{int N;vector<int> dat;BIT() {}BIT(int n) {N = n;dat.resize(N + 1);}// update k th element (0-index)void add(int k, int x){k++;while(k <= N){dat[k] += x;k += k&-k;}}// sum [0, k) (0-index)int sum(int k){int s = 0;while(k > 0){s += dat[k];k -= k&-k;}return s;}// sum [a, b) (0-index)int query(int a, int b){return sum(b) - sum(a);}};int n;int calc(vector<int> &a){int res = 0;BIT bit(n);for(int j = 0; j < n; j++){int tmp = j - bit.sum(a[j]);res += tmp;cout << tmp << endl;bit.add(a[j], 1);}return res;}struct CumulativeSum{vector<int> data;CumulativeSum(int sz) : data(sz, 0) {};void add(int k, int x){data[k] += x;}void build(){for(int i = 1; i < data.size(); i++){data[i] += data[i - 1];}}//区間[0,k]の和int query(int k){if(k < 0) return 0;return (data[min(k, (int)data.size() - 1)]);}//区間[l.r)の和int query(int l, int r){return query(r - 1) - query(l - 1);}};signed main(){ios::sync_with_stdio(false);cin.tie(0);cin >> n;vector<int> a(n);rep(i, 0, n) cin >> a[i];vector<int> b = a;sort(all(b));b.erase(unique(all(b)), b.end());rep(i, 0, n){a[i] = lower_bound(all(b), a[i]) - b.begin();}CumulativeSum s(n);rep(i, 0, n) s.add(a[i], 1);s.build();int res = 0;BIT bit(n);for(int j = 0; j < n; j++){int tmp = j - bit.sum(a[j] + 1);res += tmp;bit.add(a[j], 1);}cout << res << endl;for(int j = 0; j < n - 1; j++){int tmp = n - s.query(a[j]) - s.query(a[j] - 1);res += tmp;cout << res << endl;}}