結果

問題 No.694 square1001 and Permutation 3
ユーザー face4face4
提出日時 2019-02-16 16:43:56
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 428 ms / 3,000 ms
コード長 1,280 bytes
コンパイル時間 1,239 ms
コンパイル使用メモリ 78,736 KB
実行使用メモリ 11,300 KB
最終ジャッジ日時 2023-10-23 20:10:39
合計ジャッジ時間 5,074 ms
ジャッジサーバーID
(参考情報)
judge12 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,348 KB
testcase_01 AC 2 ms
4,348 KB
testcase_02 AC 2 ms
4,348 KB
testcase_03 AC 2 ms
4,348 KB
testcase_04 AC 2 ms
4,348 KB
testcase_05 AC 2 ms
4,348 KB
testcase_06 AC 3 ms
4,348 KB
testcase_07 AC 158 ms
9,188 KB
testcase_08 AC 391 ms
9,716 KB
testcase_09 AC 428 ms
11,300 KB
testcase_10 AC 129 ms
9,188 KB
testcase_11 AC 426 ms
11,300 KB
testcase_12 AC 423 ms
11,300 KB
testcase_13 AC 2 ms
4,372 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;
typedef long long ll;

struct BIT{
private:
    vector<int> bit;
    int n;
public:
    BIT(int arg){
        n = arg + 5;
        bit.resize(n, 0);
    }

    void add(int i, int x){
        while(i <= n){
            bit[i] += x;
            i += i & -i;
        }
    }

    ll sum(int i){
        ll ret = 0;
        while(i > 0){
            ret += bit[i];
            i -= i & -i;
        }
        return ret;
    }
};

int main(){
    int n;
    cin >> n;

    vector<int> v(n);
    for(int i = 0; i < n; i++)  scanf("%d", v.begin()+i);

    vector<int> cp = v;
    sort(cp.begin(), cp.end());
    vector<int> cp2 = cp;
    cp.erase(unique(cp.begin(), cp.end()), cp.end());

    auto pos = [&](int val) -> int{
        return lower_bound(cp.begin(), cp.end(), val)-cp.begin();
    };

    BIT bit(cp.size());
    
    ll ans = 0;
    for(int i = 0; i < n; i++){
        ans += i-bit.sum(pos(v[i])+1);
        bit.add(pos(v[i])+1, 1);
    }

    printf("%lld\n", ans);
    for(int i = 0; i < n-1; i++){
        ans += -(lower_bound(cp2.begin(),cp2.end(),v[i])-cp2.begin()) 
               +(cp2.end()-upper_bound(cp2.begin(),cp2.end(),v[i]));
        printf("%lld\n", ans);
    }

    return 0;
}
0