結果

問題 No.694 square1001 and Permutation 3
ユーザー treeonetreeone
提出日時 2018-06-08 23:26:42
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 921 ms / 3,000 ms
コード長 2,329 bytes
コンパイル時間 1,785 ms
コンパイル使用メモリ 153,900 KB
実行使用メモリ 18,748 KB
最終ジャッジ日時 2023-09-13 00:19:43
合計ジャッジ時間 8,731 ms
ジャッジサーバーID
(参考情報)
judge12 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 2 ms
4,376 KB
testcase_02 AC 2 ms
4,376 KB
testcase_03 AC 2 ms
4,376 KB
testcase_04 AC 2 ms
4,376 KB
testcase_05 AC 3 ms
4,380 KB
testcase_06 AC 2 ms
4,380 KB
testcase_07 AC 727 ms
18,552 KB
testcase_08 AC 844 ms
18,604 KB
testcase_09 AC 908 ms
18,748 KB
testcase_10 AC 713 ms
18,540 KB
testcase_11 AC 906 ms
18,684 KB
testcase_12 AC 921 ms
18,624 KB
testcase_13 AC 2 ms
4,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#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;
    }
}
0