結果

問題 No.1282 Display Elements
ユーザー lorent_kyoprolorent_kyopro
提出日時 2020-11-08 13:31:33
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 193 ms / 2,000 ms
コード長 2,610 bytes
コンパイル時間 2,243 ms
コンパイル使用メモリ 211,396 KB
実行使用メモリ 13,864 KB
最終ジャッジ日時 2023-09-29 21:11:23
合計ジャッジ時間 4,511 ms
ジャッジサーバーID
(参考情報)
judge13 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 KB
testcase_01 AC 2 ms
4,376 KB
testcase_02 AC 1 ms
4,380 KB
testcase_03 AC 1 ms
4,376 KB
testcase_04 AC 2 ms
4,380 KB
testcase_05 AC 2 ms
4,380 KB
testcase_06 AC 2 ms
4,380 KB
testcase_07 AC 1 ms
4,380 KB
testcase_08 AC 1 ms
4,380 KB
testcase_09 AC 1 ms
4,376 KB
testcase_10 AC 2 ms
4,376 KB
testcase_11 AC 2 ms
4,376 KB
testcase_12 AC 1 ms
4,380 KB
testcase_13 AC 2 ms
4,380 KB
testcase_14 AC 2 ms
4,376 KB
testcase_15 AC 173 ms
13,148 KB
testcase_16 AC 56 ms
6,944 KB
testcase_17 AC 133 ms
11,356 KB
testcase_18 AC 75 ms
8,556 KB
testcase_19 AC 27 ms
4,380 KB
testcase_20 AC 26 ms
4,376 KB
testcase_21 AC 193 ms
13,864 KB
testcase_22 AC 46 ms
4,376 KB
testcase_23 AC 192 ms
13,812 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

// #include <atcoder/all>
// using namespace atcoder;
// using mint = modint1000000007;
// using mint = modint998244353;
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (int)n; ++i)
#define rrep(i, n) for (int i = (int)n-1; i >= 0; --i)
using namespace std;
using ll = long long;
template<typename T>
inline bool chmax(T& a, const T& b) {
    if (a < b){
        a = b;
        return true;
    }
    return false;
}
template<typename T>
inline bool chmin(T& a, const T& b) {
    if (b < a) {
        a = b;
        return true;
    }
    return false;
}
/**
 * @brief 多次元 vector の作成
 * @author えびちゃん
 */
namespace detail {
    template<typename T, int N>
    auto make_vec(vector<int>& sizes, T const& x) {
        if constexpr (N == 1) {
            return vector(sizes[0], x);
        } else {
            int size = sizes[N-1];
            sizes.pop_back();
            return vector(size, make_vec<T, N-1>(sizes, x));
        }
    }
}
template<typename T, int N>
auto make_vec(int const(&sizes)[N], T const& x = T()) {
    vector<int> s(N);
    for (int i = 0; i < N; ++i) s[i] = sizes[N-i-1];
    return detail::make_vec<T, N>(s, x);
}
__attribute__((constructor))
void fast_io() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
}

template <typename T>
struct fenwick_tree {
    int n, power; // min power s.t. n <= power && power = 2^k;
    vector<T> data;
    fenwick_tree(int n=0) : n(n), data(n+1) {
        power = 1;
        while (power < n) power <<= 1;
    }
    void add(int i, T x=1) {
        for (i++; i <= n; i += i&-i) data[i] += x;
    }
    // [0, i)
    T sum(int i) {
        T res = 0;
        for (; i; i -= i&-i) res += data[i];
        return res;
    }
    // [l, r)
    T sum(int l, int r) { return sum(r) - sum(l);} 
    // min x s.t. v0 + v1 + v2 + ... + vx >= w
    int lower_bound(T w) {
        if (w <= 0) return 0;
        int x = 0;
        for (int i = power; i; i >>= 1) {
            if (i+x <= n && data[i+x] < w) {
                w -= data[i+x];
                x += i;
            }
        }
        return x;
    }
};

int main() {
    int n;
    cin >> n;
    vector<int> a(n), b(n);
    rep(i, n) cin >> a[i];
    rep(i, n) cin >> b[i];

    map<int, int> mp;
    rep(i, n) mp[a[i]] = 0;
    rep(i, n) mp[b[i]] = 0;
    int k = 0;
    for (auto& p : mp) p.second = k++;
    rep(i, n) a[i] = mp[a[i]];
    rep(i, n) b[i] = mp[b[i]];

    sort(a.begin(), a.end());

    fenwick_tree<int> fw(k);
    ll ans = 0;
    rep(i, n) {
        fw.add(b[i], 1);
        ans += fw.sum(a[i]);
    }
    cout << ans << '\n';
}
0