結果

問題 No.2161 Black Market
ユーザー t98slidert98slider
提出日時 2022-12-12 03:41:10
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 1,132 ms / 7,000 ms
コード長 4,867 bytes
コンパイル時間 2,088 ms
コンパイル使用メモリ 186,940 KB
実行使用メモリ 108,028 KB
最終ジャッジ日時 2023-08-06 01:01:18
合計ジャッジ時間 10,238 ms
ジャッジサーバーID
(参考情報)
judge12 / judge14
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,376 KB
testcase_01 AC 1 ms
4,380 KB
testcase_02 AC 2 ms
4,376 KB
testcase_03 AC 2 ms
4,384 KB
testcase_04 AC 1 ms
4,380 KB
testcase_05 AC 1 ms
4,376 KB
testcase_06 AC 2 ms
4,380 KB
testcase_07 AC 2 ms
4,376 KB
testcase_08 AC 2 ms
4,380 KB
testcase_09 AC 2 ms
4,376 KB
testcase_10 AC 2 ms
4,380 KB
testcase_11 AC 3 ms
4,380 KB
testcase_12 AC 2 ms
4,376 KB
testcase_13 AC 2 ms
4,380 KB
testcase_14 AC 2 ms
4,376 KB
testcase_15 AC 3 ms
4,376 KB
testcase_16 AC 3 ms
4,376 KB
testcase_17 AC 2 ms
4,376 KB
testcase_18 AC 2 ms
4,376 KB
testcase_19 AC 2 ms
4,376 KB
testcase_20 AC 72 ms
19,340 KB
testcase_21 AC 71 ms
19,452 KB
testcase_22 AC 127 ms
25,604 KB
testcase_23 AC 117 ms
22,504 KB
testcase_24 AC 1,089 ms
89,688 KB
testcase_25 AC 908 ms
89,552 KB
testcase_26 AC 1,132 ms
108,028 KB
testcase_27 AC 417 ms
89,568 KB
testcase_28 AC 441 ms
104,252 KB
testcase_29 AC 93 ms
23,404 KB
testcase_30 AC 25 ms
7,780 KB
testcase_31 AC 1,000 ms
88,536 KB
testcase_32 AC 392 ms
89,608 KB
testcase_33 AC 58 ms
12,596 KB
testcase_34 AC 78 ms
22,772 KB
testcase_35 AC 68 ms
23,232 KB
testcase_36 AC 58 ms
12,768 KB
testcase_37 AC 25 ms
7,684 KB
testcase_38 AC 378 ms
44,352 KB
testcase_39 AC 25 ms
7,812 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
using ll = long long;

//2次元セグ木(自動座圧)
template <class S, S (*op)(S, S), S (*e)(), class C = long long int> struct segtree2D_lite {
    int H;
    std::vector<int> logsize, W;
    std::vector<C> Yc;
    std::vector<std::vector<C>> pos;
    std::vector<std::pair<C, C>> preorder;
    std::vector<std::vector<S>> d;
    segtree2D_lite()  {}
    void insert(C y, C x){
        preorder.emplace_back(y, x);
    }
    void build(){
        for(int i = 0; i < int(preorder.size()) ; i++){
            Yc.push_back(preorder[i].first);
        }
        std::sort(Yc.begin(), Yc.end());
        Yc.erase(std::unique(Yc.begin(), Yc.end()), Yc.end());
        H = 1 << ceil_pow2(Yc.size());
        pos.resize(2*H), d.resize(2*H);
        logsize.resize(2*H), W.resize(2*H);
        for(int i = 0; i < int(preorder.size()) ; i++){
            int y = std::lower_bound(Yc.begin(), Yc.end(), preorder[i].first) - Yc.begin();
            y += H;
            while(y){
                pos[y].push_back(preorder[i].second);
                y >>= 1;
            }
        }
        for(int y = 0; y < int(pos.size()) ; y++){
            std::sort(pos[y].begin(), pos[y].end());
            pos[y].erase(std::unique(pos[y].begin(), pos[y].end()), pos[y].end());
            logsize[y] = ceil_pow2(pos[y].size());
            W[y] = 1 << logsize[y] ;
            d[y].resize(2 * W[y] , e());
        }
    }
    void set(C py, C px, S v){
        int y = std::lower_bound(Yc.begin(), Yc.end(), py) - Yc.begin();
        y += H;
        while(y){
            int x = std::lower_bound(pos[y].begin(), pos[y].end(), px) - pos[y].begin();
            x += W[y];
            d[y][x]++;
            x >>= 1;
            while(x){
                updateX(y, x);
                x >>= 1;
            }
            y >>= 1;
        }
    }
    S get(C py, C px){
        int y = std::lower_bound(Yc.begin(), Yc.end(), py) - Yc.begin();
        y += H;
        int x = std::lower_bound(pos[y].begin(), pos[y].end(), px) - pos[y].begin();
        return d[y][x + W[y]];
    }
    S prod(C lpy, C lpx , C rpy , C rpx){
        int ly = std::lower_bound(Yc.begin(), Yc.end(), lpy) - Yc.begin();
        int ry = std::lower_bound(Yc.begin(), Yc.end(), rpy) - Yc.begin();
        S sml = e(), smr = e();
        ly += H, ry += H ;
        while (ly < ry) {
            if (ly & 1) sml = op(sml, yline_prod(lpx, rpx , ly++));
            if (ry & 1) smr = op(yline_prod(lpx, rpx , --ry), smr);
            ly >>= 1, ry >>= 1;
        }
        return op(sml, smr);
    }
    S yline_prod(C lpx, C rpx , int y){
        int lx = std::lower_bound(pos[y].begin(), pos[y].end(), lpx) - pos[y].begin();
        int rx = std::lower_bound(pos[y].begin(), pos[y].end(), rpx) - pos[y].begin();
        S sml = e(), smr = e();
        lx += W[y], rx += W[y] ;
        while (lx < rx) {
            if (lx & 1) sml = op(sml, d[y][lx++]);
            if (rx & 1) smr = op(d[y][--rx], smr);
            lx >>= 1, rx >>= 1;
        }
        return op(sml, smr);
    }
    private:
    int ceil_pow2(int n) {
        int x = 0;
        while ((1U << x) < (unsigned int)(n)) x++;
        return x;
    }
    void updateX(int yi, int xi) { d[yi][xi] = op(d[yi][2 * xi], d[yi][2 * xi + 1]); }
};

ll op(ll lhs, ll rhs){
    return lhs + rhs;
}
ll e(){return 0;}

int main(){
    ios::sync_with_stdio(false);
    cin.tie(0);
    ll N, K, L, P;
    cin >> N >> K >> L >> P;
    int n1 = N / 2, n2 = N - n1;
    vector<ll> a(N), b(N);
    for(int i = 0; i < N; i++)cin >> a[i] >> b[i];
    vector<vector<pair<ll,ll>>> c(n1 + 1), d(n2 + 1);
    segtree2D_lite<ll, op, e> seg;
    for(int i = 0; i < (1 << n1); i++){
        ll s1 = 0, s2 = 0;
        for(int j = 0; j < n1; j++){
            if(i >> j & 1){
                s1 += a[j];
                s2 += b[j];
            }
        }
        c[__builtin_popcount(i)].emplace_back(s1, s2);
    }

    for(int i = 0; i < (1 << n2); i++){
        ll s1 = 0, s2 = 0;
        for(int j = 0; j < n2; j++){
            if(i >> j & 1){
                s1 += a[j + n1];
                s2 += b[j + n1];
            }
        }
        d[__builtin_popcount(i)].emplace_back(s1, s2);
        seg.insert(s1, s2);
    }
    seg.build();

    int cnt = 0;
    ll ans = 0;
    for(int ci = min(n1, int(K)), di = 0; ci >= 0; ci--){
        while(di <= n2 && ci + di <= K){
            for(auto &&p:d[di]){
                seg.set(p.first, p.second, 1);
                cnt++;
            }
            di++;
        }
        //cerr << cnt << " " << seg.d[1][1] << '\n';
        //assert(cnt == seg.d[1][1]);
        for(auto &&p:c[ci]){
            ll ly = 0, ry = L + 1 - p.first;
            if(ry <= ly)continue;
            ans += seg.prod(ly, P - p.second, ry, 1ll << 62);
        }
    }
    cout << ans << '\n';
}
0