結果
問題 | No.2161 Black Market |
ユーザー | tnakao0123 |
提出日時 | 2022-12-15 15:23:11 |
言語 | C++14 (gcc 12.3.0 + boost 1.83.0) |
結果 |
AC
|
実行時間 | 566 ms / 7,000 ms |
コード長 | 3,168 bytes |
コンパイル時間 | 716 ms |
コンパイル使用メモリ | 64,732 KB |
実行使用メモリ | 11,008 KB |
最終ジャッジ日時 | 2024-04-26 16:43:31 |
合計ジャッジ時間 | 4,279 ms |
ジャッジサーバーID (参考情報) |
judge1 / judge2 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 3 ms
6,812 KB |
testcase_01 | AC | 3 ms
6,940 KB |
testcase_02 | AC | 3 ms
6,940 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,944 KB |
testcase_07 | AC | 2 ms
6,944 KB |
testcase_08 | AC | 2 ms
6,940 KB |
testcase_09 | AC | 3 ms
6,940 KB |
testcase_10 | AC | 3 ms
6,944 KB |
testcase_11 | AC | 2 ms
6,940 KB |
testcase_12 | AC | 2 ms
6,944 KB |
testcase_13 | AC | 3 ms
6,944 KB |
testcase_14 | AC | 2 ms
6,940 KB |
testcase_15 | AC | 3 ms
6,940 KB |
testcase_16 | AC | 3 ms
6,944 KB |
testcase_17 | AC | 2 ms
6,940 KB |
testcase_18 | AC | 2 ms
6,944 KB |
testcase_19 | AC | 2 ms
6,944 KB |
testcase_20 | AC | 64 ms
10,368 KB |
testcase_21 | AC | 67 ms
10,368 KB |
testcase_22 | AC | 116 ms
10,496 KB |
testcase_23 | AC | 143 ms
10,564 KB |
testcase_24 | AC | 566 ms
11,008 KB |
testcase_25 | AC | 125 ms
10,752 KB |
testcase_26 | AC | 458 ms
10,752 KB |
testcase_27 | AC | 48 ms
10,880 KB |
testcase_28 | AC | 48 ms
10,944 KB |
testcase_29 | AC | 15 ms
6,944 KB |
testcase_30 | AC | 15 ms
6,944 KB |
testcase_31 | AC | 288 ms
9,728 KB |
testcase_32 | AC | 47 ms
10,880 KB |
testcase_33 | AC | 37 ms
6,940 KB |
testcase_34 | AC | 11 ms
6,940 KB |
testcase_35 | AC | 12 ms
6,948 KB |
testcase_36 | AC | 26 ms
6,940 KB |
testcase_37 | AC | 17 ms
6,940 KB |
testcase_38 | AC | 169 ms
6,940 KB |
testcase_39 | AC | 9 ms
6,940 KB |
ソースコード
/* -*- coding: utf-8 -*- * * 2161.cc: No.2161 Black Market - yukicoder */ #include<cstdio> #include<vector> #include<algorithm> using namespace std; /* constant */ const int MAX_N = 34; const int MAX_M = MAX_N / 2; const int MBITS = 1 << MAX_M; /* typedef */ typedef long long ll; typedef vector<int> vi; typedef vector<vi> vvi; template <typename T> struct BIT { int n; vector<T> bits; BIT() {} BIT(int _n) { init(_n); } void init(int _n) { n = _n; bits.assign(n + 1, 0); } T sum(int x) { x = min(x, n); T s = 0; while (x > 0) { s += bits[x]; x -= (x & -x); } return s; } void add(int x, T v) { if (x <= 0) return; while (x <= n) { bits[x] += v; x += (x & -x); } } int lower_bound(T v) { int k = 1; while ((k << 1) <= n) k <<= 1; int x = 0; for (; k > 0; k >>= 1) if (x + k <= n && bits[x + k] < v) { x += k; v -= bits[x]; } return x + 1; } }; /* global variables */ int as[MAX_N], bs[MAX_N], bnums[MBITS]; ll ass0[MBITS], ass1[MBITS], bss0[MBITS], bss1[MBITS]; ll uas[MBITS]; BIT<int> bit; /* subroutines */ bool ltbss0(const int a, const int b) { return bss0[a] < bss0[b]; } bool ltbss1(const int a, const int b) { return bss1[a] < bss1[b]; } /* main */ int main() { bnums[0] = 0; for (int bits = 1, msb = 1; bits < MBITS; bits++) { if ((msb << 1) <= bits) msb <<= 1; bnums[bits] = bnums[bits ^ msb] + 1; } int n, k, l, p; scanf("%d%d%d%d", &n, &k, &l, &p); for (int i = 0; i < n; i++) scanf("%d%d", as + i, bs + i); int m0 = n / 2, m1 = n - m0; int mbits0 = 1 << m0, mbits1 = 1 << m1; vvi bvs0(m0 + 1), bvs1(m1 + 1); for (int bits = 0; bits < mbits0; bits++) bvs0[bnums[bits]].push_back(bits); for (int bits = 0; bits < mbits1; bits++) bvs1[bnums[bits]].push_back(bits); for (int bits = 1, msb = 1, msi = 0; bits < mbits0; bits++) { if ((msb << 1) <= bits) msb <<= 1, msi++; ass0[bits] = ass0[bits ^ msb] + as[msi]; bss0[bits] = bss0[bits ^ msb] + bs[msi]; } for (int bits = 1, msb = 1, msi = 0; bits < mbits1; bits++) { if ((msb << 1) <= bits) msb <<= 1, msi++; ass1[bits] = ass1[bits ^ msb] + as[m0 + msi]; bss1[bits] = bss1[bits ^ msb] + bs[m0 + msi]; uas[bits] = ass1[bits]; } for (int i = 0; i <= m0; i++) sort(bvs0[i].begin(), bvs0[i].end(), ltbss0); for (int i = 0; i <= m1; i++) sort(bvs1[i].begin(), bvs1[i].end(), ltbss1); sort(uas, uas + mbits1); int un = unique(uas, uas + mbits1) - uas; ll sum = 0; for (int i0 = 0; i0 <= m0; i0++) for (int i1 = 0; i1 <= m1 && i0 + i1 <= k; i1++) { vi &bv0 = bvs0[i0], &bv1 = bvs1[i1]; if (bv0.empty() || bv1.empty()) continue; int s0 = bv0.size(), s1 = bv1.size(); bit.init(un); for (int i = 0, j = s1 - 1; i < s0; i++) { ll bi = bss0[bv0[i]], ai = ass0[bv0[i]]; while (j >= 0 && bss1[bv1[j]] + bi >= p) { int ui = lower_bound(uas, uas + un, ass1[bv1[j]]) - uas; bit.add(ui + 1, 1); j--; } int ui = upper_bound(uas, uas + un, l - ai) - uas; sum += bit.sum(ui); } } printf("%lld\n", sum); return 0; }