結果

問題 No.1282 Display Elements
ユーザー hedwig100hedwig100
提出日時 2020-11-06 22:11:54
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 157 ms / 2,000 ms
コード長 2,576 bytes
コンパイル時間 2,529 ms
コンパイル使用メモリ 177,036 KB
実行使用メモリ 14,628 KB
最終ジャッジ日時 2023-09-29 18:53:01
合計ジャッジ時間 3,569 ms
ジャッジサーバーID
(参考情報)
judge15 / judge12
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 1 ms
4,376 KB
testcase_03 AC 1 ms
4,376 KB
testcase_04 AC 1 ms
4,376 KB
testcase_05 AC 2 ms
4,376 KB
testcase_06 AC 2 ms
4,380 KB
testcase_07 AC 1 ms
4,376 KB
testcase_08 AC 1 ms
4,376 KB
testcase_09 AC 1 ms
4,380 KB
testcase_10 AC 2 ms
4,376 KB
testcase_11 AC 2 ms
4,376 KB
testcase_12 AC 1 ms
4,376 KB
testcase_13 AC 2 ms
4,376 KB
testcase_14 AC 2 ms
4,380 KB
testcase_15 AC 144 ms
13,876 KB
testcase_16 AC 49 ms
7,528 KB
testcase_17 AC 114 ms
12,024 KB
testcase_18 AC 65 ms
8,536 KB
testcase_19 AC 29 ms
4,796 KB
testcase_20 AC 28 ms
4,640 KB
testcase_21 AC 157 ms
14,612 KB
testcase_22 AC 40 ms
4,668 KB
testcase_23 AC 157 ms
14,628 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
#ifdef LOCAL_
void debug_out() {cerr << endl;}
template<typename Head,typename... Tail> void debug_out(Head H,Tail... T){cerr << ' ' << H; debug_out(T...);}
#define debug(...) cerr << 'L' << __LINE__ << " [" << #__VA_ARGS__ << "]:",debug_out(__VA_ARGS__)
#define dump(x) cerr << 'L' << __LINE__ << " " << #x << " = " << (x) << endl;
#else
#define debug(...) (void(0))
#define dump(x) (void(0))
#endif
#define rep(i,n) for (int i = 0; i < (int)(n); i ++)
#define irep(i,n) for (int i = (int)(n) - 1;i >= 0;--i)
using ll = long long;
using PL = pair<ll,ll>;
using P = pair<int,int>;
constexpr int INF = 1000000000;
constexpr long long HINF = 1000000000000000;
constexpr long long MOD = 1000000007;// = 998244353;
constexpr double EPS = 1e-4;
constexpr double PI = 3.14159265358979;

template<class T> 
struct BinaryIndexedTree {
    int N,power = 1;
    vector<T> bit;

    BinaryIndexedTree(int N = 0): N(N){
        bit.assign(N + 1,0);
        while (power <= N) power <<= 1; //power > N
    }
    void build(const vector<T> &A) {
        for (int i = 1;i <= N; ++i) add(i,A[i - 1]);
    }
    // add x to a[i]
    void add(int i,T x) {
        for (int idx = i; idx <= N; idx += (idx & -idx)) {
            bit[idx] += x;
        }
    }
    // return a[1] + a[2] + a[3] + .. + a[k]
    T sum(int k) {
        T ret = 0;
        for (int idx = k; idx > 0; idx -= (idx & -idx)) {
            ret += bit[idx];
        }
        return ret;
    }
    // return a[l] + a[l + 1] + a[l + 2] + .. + a[r - 1]
    T sum(int l,int r) {
        return sum(r - 1) - sum(l - 1);
    }
    // return min index s.t. a[1] + a[2] + a[3] + .. + a[x] >= w
    int lower_bound(T w) {
        if (w <= 0) return 0;
        int x = 0;
        for (int r = power; r > 0; r >>= 1) {
            if (x + r <= N && bit[x + r] < w) {
                w -= bit[x + r];
                x += r;
            }
        } 
        return x + 1;
    }
};

int main() {
    cin.tie(nullptr);
    ios::sync_with_stdio(false);

    int N; cin >> N;
    vector<int> A(N),B(N);
    rep(i,N) cin >> A[i];
    rep(i,N) cin >> B[i];
    sort(A.begin(),A.end());
    vector<int> C(2*N);
    rep(i,N) C[i] = A[i];
    rep(i,N) C[i+N] = B[i];
    sort(C.begin(),C.end());
    C.erase(unique(C.begin(),C.end()),C.end());
    map<int,int> mp;
    rep(i,C.size()) mp[C[i]] = i;
    int m = C.size();
    BinaryIndexedTree<int> bit(m);
    ll ans = 0;
    rep(i,N) {
        bit.add(mp[B[i]]+1,1);
        ans += bit.sum(mp[A[i]]);
    }
    cout << ans << '\n';
    return 0;
}
0