結果

問題 No.559 swapAB列
ユーザー masaktmasakt
提出日時 2017-08-25 22:40:40
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 2 ms / 2,000 ms
コード長 2,055 bytes
コンパイル時間 1,668 ms
コンパイル使用メモリ 168,792 KB
実行使用メモリ 6,944 KB
最終ジャッジ日時 2024-04-23 15:37:45
合計ジャッジ時間 2,203 ms
ジャッジサーバーID
(参考情報)
judge2 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,816 KB
testcase_01 AC 2 ms
6,940 KB
testcase_02 AC 2 ms
6,944 KB
testcase_03 AC 2 ms
6,940 KB
testcase_04 AC 2 ms
6,940 KB
testcase_05 AC 2 ms
6,940 KB
testcase_06 AC 2 ms
6,940 KB
testcase_07 AC 2 ms
6,944 KB
testcase_08 AC 2 ms
6,940 KB
testcase_09 AC 2 ms
6,944 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
namespace ac {
    using namespace std;
}
#define rep(i,n) for(int (i)=0;(i)<(int)(n);++(i))
#define all(r) r.begin(),r.end()
using namespace std;
using namespace ac;
typedef int64_t ll;
typedef vector<ll> vll;
namespace ac {
    // FenwickTree繝サBit
    struct BinaryIndexedTree {
        int n;
        vector<int64_t> b; // [1, m_n]
        BinaryIndexedTree() {}

        void init(int a_n) {
            n = a_n;
            b.resize(n + 1, 0);
        }

        int64_t sum(int i) {
            int64_t s = 0;
            while (0 < i) {
                s += b[i];
                i -= i & -i;
            }
            return s;
        }
        void add(int i, int64_t x) {
            while (i <= n) {
                b[i] += x;
                i += i & -i;
            }
        }

    };
    struct Inversion {
        int n;
        ac::BinaryIndexedTree bit;

        Inversion(int a_n) {
            init(a_n);
        }
        void init(int a_n) {
            n = a_n;
            bit.init(n);
        }
        vector<int64_t> getGreaterThan(vector<int64_t> &a_v) {
            int length = a_v.size();
            vector<int64_t> ord(length);
            for (int i = length - 1; i >= 0; i--) {
                ord[i] = bit.sum(a_v[i]);
                bit.add(a_v[i] + 1, 1);
            }
            return ord;
        }
    };
}
struct Solution {
    void solve(std::istream& in, std::ostream& out) {
        string S;
        in >> S;
        Inversion i(S.size());
        vll a(S.size());
        rep(i, S.size()) {
            a[i] = S[i] - 'A';
        }
        vll res = i.getGreaterThan(a);
        ll ans = accumulate(all(res), 0);
        out << ans << '\n';
    }
};
void solve(std::istream& in, std::ostream& out) {
    out << std::setprecision(12);
    Solution solution;
    solution.solve(in, out);
}
#include <fstream>
#include <iostream>
int main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    istream& in = cin;
    ostream& out = cout;
    solve(in, out);
    return 0;
}
0