結果

問題 No.2161 Black Market
ユーザー 👑 hos.lyrichos.lyric
提出日時 2022-12-12 00:13:59
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 290 ms / 7,000 ms
コード長 3,955 bytes
コンパイル時間 1,340 ms
コンパイル使用メモリ 121,440 KB
実行使用メモリ 13,224 KB
最終ジャッジ日時 2024-04-23 18:31:04
合計ジャッジ時間 3,518 ms
ジャッジサーバーID
(参考情報)
judge3 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,248 KB
testcase_01 AC 2 ms
5,376 KB
testcase_02 AC 1 ms
5,376 KB
testcase_03 AC 1 ms
5,376 KB
testcase_04 AC 2 ms
5,376 KB
testcase_05 AC 1 ms
5,376 KB
testcase_06 AC 1 ms
5,376 KB
testcase_07 AC 2 ms
5,376 KB
testcase_08 AC 2 ms
5,376 KB
testcase_09 AC 1 ms
5,376 KB
testcase_10 AC 2 ms
5,376 KB
testcase_11 AC 2 ms
5,376 KB
testcase_12 AC 2 ms
5,376 KB
testcase_13 AC 2 ms
5,376 KB
testcase_14 AC 2 ms
5,376 KB
testcase_15 AC 2 ms
5,376 KB
testcase_16 AC 2 ms
5,376 KB
testcase_17 AC 2 ms
5,376 KB
testcase_18 AC 2 ms
5,376 KB
testcase_19 AC 2 ms
5,376 KB
testcase_20 AC 33 ms
13,216 KB
testcase_21 AC 34 ms
13,224 KB
testcase_22 AC 41 ms
13,220 KB
testcase_23 AC 56 ms
13,092 KB
testcase_24 AC 290 ms
13,220 KB
testcase_25 AC 80 ms
13,220 KB
testcase_26 AC 250 ms
13,220 KB
testcase_27 AC 38 ms
13,092 KB
testcase_28 AC 36 ms
13,220 KB
testcase_29 AC 11 ms
5,888 KB
testcase_30 AC 10 ms
5,376 KB
testcase_31 AC 166 ms
10,972 KB
testcase_32 AC 37 ms
13,224 KB
testcase_33 AC 25 ms
5,376 KB
testcase_34 AC 8 ms
5,376 KB
testcase_35 AC 10 ms
6,144 KB
testcase_36 AC 7 ms
5,376 KB
testcase_37 AC 4 ms
5,376 KB
testcase_38 AC 21 ms
7,168 KB
testcase_39 AC 3 ms
5,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <cassert>
#include <cmath>
#include <cstdint>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <bitset>
#include <complex>
#include <deque>
#include <functional>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>

using namespace std;

using Int = long long;

template <class T1, class T2> ostream &operator<<(ostream &os, const pair<T1, T2> &a) { return os << "(" << a.first << ", " << a.second << ")"; };
template <class T> ostream &operator<<(ostream &os, const vector<T> &as) { const int sz = as.size(); os << "["; for (int i = 0; i < sz; ++i) { if (i >= 256) { os << ", ..."; break; } if (i > 0) { os << ", "; } os << as[i]; } return os << "]"; }
template <class T> void pv(T a, T b) { for (T i = a; i != b; ++i) cerr << *i << " "; cerr << endl; }
template <class T> bool chmin(T &t, const T &f) { if (t > f) { t = f; return true; } return false; }
template <class T> bool chmax(T &t, const T &f) { if (t < f) { t = f; return true; } return false; }


template <class T> void bAdd(vector<T> &bit, int pos, const T &val) {
  const int bitN = bit.size();
  for (int x = pos; x < bitN; x |= x + 1) bit[x] += val;
}
template <class T> T bSum(const vector<T> &bit, int pos) {
  T ret = 0;
  for (int x = pos - 1; x >= 0; x = (x & (x + 1)) - 1) ret += bit[x];
  return ret;
}
template <class T> T bSum(const vector<T> &bit, int pos0, int pos1) {
  return bSum(bit, pos1) - bSum(bit, pos0);
}


int N, K;
Int L, P;
vector<Int> A, B;

int main() {
  for (; ~scanf("%d%d%lld%lld", &N, &K, &L, &P); ) {
    A.resize(N);
    B.resize(N);
    for (int i = 0; i < N; ++i) {
      scanf("%lld%lld", &A[i], &B[i]);
    }
    
    const int half = N / 2;
    vector<Int> ASum0(1 << half, 0);
    vector<Int> BSum0(1 << half, 0);
    for (int i = 0; i < half; ++i) {
      for (int h = 0; h < 1 << i; ++h) {
        ASum0[h | 1 << i] = ASum0[h] + A[i];
        BSum0[h | 1 << i] = BSum0[h] + B[i];
      }
    }
    vector<Int> ASum1(1 << (N - half), 0);
    vector<Int> BSum1(1 << (N - half), 0);
    for (int i = 0; i < N - half; ++i) {
      for (int h = 0; h < 1 << i; ++h) {
        ASum1[h | 1 << i] = ASum1[h] + A[half + i];
        BSum1[h | 1 << i] = BSum1[h] + B[half + i];
      }
    }
    
    vector<vector<pair<Int, Int>>> pss(half + 1);
    vector<vector<pair<Int, Int>>> qss(N - half + 1);
    for (int h = 0; h < 1 << half; ++h) {
      pss[__builtin_popcount(h)].emplace_back(ASum0[h], BSum0[h]);
    }
    for (int h = 0; h < 1 << (N - half); ++h) {
      qss[__builtin_popcount(h)].emplace_back(ASum1[h], BSum1[h]);
    }
// cerr<<"pss = "<<pss<<endl;
// cerr<<"qss = "<<qss<<endl;
    for (int k = 0; k <= half; ++k) {
      sort(pss[k].begin(), pss[k].end());
    }
    vector<vector<Int>> yss(N - half + 1);
    for (int k = 0; k <= N - half; ++k) {
      sort(qss[k].begin(), qss[k].end());
      yss[k].resize(qss[k].size());
      for (int l = 0; l < (int)qss[k].size(); ++l) {
        yss[k][l] = qss[k][l].second;
      }
      sort(yss[k].begin(), yss[k].end());
      yss[k].erase(unique(yss[k].begin(), yss[k].end()), yss[k].end());
    }
    
    Int ans = 0;
    for (int k0 = 0; k0 <= half; ++k0) for (int k1 = 0; k1 <= N - half; ++k1) if (k0 + k1 <= K) {
      auto indexOf = [&](Int y) -> int {
        return lower_bound(yss[k1].begin(), yss[k1].end(), y) - yss[k1].begin();
      };
      vector<int> bit(yss[k1].size(), 0);
      int m = 0;
      for (int l = (int)pss[k0].size(); --l >= 0; ) {
        const Int a = pss[k0][l].first;
        const Int b = pss[k0][l].second;
        for (; m < (int)qss[k1].size() && a + qss[k1][m].first <= L; ++m) {
          bAdd(bit, indexOf(qss[k1][m].second), +1);
        }
        ans += (m - bSum(bit, indexOf(P - b)));
      }
    }
    printf("%lld\n", ans);
  }
  return 0;
}
0