結果

問題 No.694 square1001 and Permutation 3
ユーザー ama_nukoama_nuko
提出日時 2018-07-11 13:25:43
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 1,616 ms / 3,000 ms
コード長 2,332 bytes
コンパイル時間 1,493 ms
コンパイル使用メモリ 109,920 KB
実行使用メモリ 23,196 KB
最終ジャッジ日時 2023-10-19 01:53:39
合計ジャッジ時間 12,639 ms
ジャッジサーバーID
(参考情報)
judge13 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
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 3 ms
4,348 KB
testcase_04 AC 3 ms
4,348 KB
testcase_05 AC 3 ms
4,348 KB
testcase_06 AC 3 ms
4,348 KB
testcase_07 AC 1,164 ms
23,196 KB
testcase_08 AC 1,558 ms
23,196 KB
testcase_09 AC 1,616 ms
23,196 KB
testcase_10 AC 1,103 ms
23,196 KB
testcase_11 AC 1,597 ms
23,196 KB
testcase_12 AC 1,600 ms
23,196 KB
testcase_13 AC 2 ms
4,352 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <cmath>
#include <iostream>
#include <vector>
#include <queue>
#include <deque>
#include <map>
#include <set>
#include <stack>
#include <tuple>
#include <bitset>
#include <algorithm>
#include <functional>
#include <utility>
#include <iomanip>

#define int long long int
#define rep(i, n) for(int i = 0; i < (n); ++i)

using namespace std;

typedef pair<int, int> P;

const int INF = 1e15;
const int MOD = 1e9+7;

template <typename T>
class segment_tree{
    private:
        static int calc_size(int n){
            int m = 1;
            while(m < n){
                m *= 2;
            }
            return m;
        }

        T query(int s, int t, int i, int l, int r) const {
            if(r <= s || t <= l){
                return init;
            }

            if(s <= l && r <= t){
                return node[i];
            }

            int m = l + (r - l) / 2;
            T vl = query(s, t, i * 2 + 1, l, m);
            T vr = query(s, t, i * 2 + 2, m, r);
            return f(vl, vr);
        }

    public:
        int n;
        vector<T> node;
        T init;
        function<T(T, T)> f;

        segment_tree(int n, T init, function<T(T, T)> f)
            : n(calc_size(n)), node(calc_size(n) * 2, init), init(init), f(f){}

        void update(int i, const T& x){
            i += (n - 1);
            node[i] += x;
            while(i > 0){
                i = (i - 1) / 2;
                node[i] = f(node[2*i+1], node[2*i+2]);
            }
        }

        //[s, t)
        T query(int s, int t) const {
            return query(s, t, 0, 0, n);
        }
};

signed main(){
    int n;
    cin >> n;
    vector<P> a;
    rep(i, n){
        int aa;
        cin >> aa;
        a.emplace_back(aa, i);
    }
    sort(a.begin(), a.end());
    vector<int> b(n);
    rep(i, n){
        if(i > 0 && a[i-1].first == a[i].first){
            b[a[i].second] = b[a[i-1].second];
            continue;
        }
        b[a[i].second] = i+1;
    }

    segment_tree<int> st(n+1, 0, [](int a, int b){return a+b;});
    int cnt = 0;
    for(int i = n-1; i >= 0; i--){
        cnt += st.query(0, b[i]);
        st.update(b[i], 1);
    }
    cout << cnt << endl;

    rep(i, n-1){
        cnt += st.query(b[i]+1, n+1);
        cnt -= st.query(0, b[i]);
        cout << cnt << endl;
    }

    return 0;
}
0