結果

問題 No.2161 Black Market
ユーザー 👑 chro_96chro_96
提出日時 2022-12-12 01:27:05
言語 C
(gcc 12.3.0)
結果
AC  
実行時間 231 ms / 7,000 ms
コード長 3,045 bytes
コンパイル時間 286 ms
コンパイル使用メモリ 34,412 KB
実行使用メモリ 141,820 KB
最終ジャッジ日時 2024-04-23 19:33:03
合計ジャッジ時間 6,591 ms
ジャッジサーバーID
(参考情報)
judge1 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 93 ms
138,596 KB
testcase_01 AC 92 ms
138,600 KB
testcase_02 AC 94 ms
138,596 KB
testcase_03 AC 97 ms
138,604 KB
testcase_04 AC 94 ms
138,596 KB
testcase_05 AC 92 ms
138,600 KB
testcase_06 AC 92 ms
138,608 KB
testcase_07 AC 93 ms
138,600 KB
testcase_08 AC 93 ms
138,604 KB
testcase_09 AC 93 ms
138,600 KB
testcase_10 AC 93 ms
138,480 KB
testcase_11 AC 93 ms
138,604 KB
testcase_12 AC 93 ms
138,604 KB
testcase_13 AC 93 ms
138,468 KB
testcase_14 AC 94 ms
138,600 KB
testcase_15 AC 93 ms
138,732 KB
testcase_16 AC 93 ms
138,604 KB
testcase_17 AC 93 ms
138,596 KB
testcase_18 AC 93 ms
138,596 KB
testcase_19 AC 93 ms
138,472 KB
testcase_20 AC 188 ms
141,692 KB
testcase_21 AC 189 ms
141,688 KB
testcase_22 AC 188 ms
141,816 KB
testcase_23 AC 204 ms
141,820 KB
testcase_24 AC 231 ms
141,692 KB
testcase_25 AC 211 ms
141,692 KB
testcase_26 AC 226 ms
141,564 KB
testcase_27 AC 207 ms
141,688 KB
testcase_28 AC 207 ms
141,568 KB
testcase_29 AC 118 ms
139,372 KB
testcase_30 AC 97 ms
138,724 KB
testcase_31 AC 195 ms
141,424 KB
testcase_32 AC 207 ms
141,696 KB
testcase_33 AC 109 ms
138,752 KB
testcase_34 AC 109 ms
139,132 KB
testcase_35 AC 116 ms
139,388 KB
testcase_36 AC 104 ms
139,132 KB
testcase_37 AC 96 ms
138,736 KB
testcase_38 AC 132 ms
140,008 KB
testcase_39 AC 96 ms
138,732 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <stdio.h>
#include <stdlib.h>

int cmp_ll (const void *ap, const void *bp) {
  long long a = *(long long *)ap;
  long long b = *(long long *)bp;
  
  if (a < b) {
    return -1;
  }
  
  if (a > b) {
    return 1;
  }
  
  return 0;
}

void set_segt (int *t, int idx, int val, int size) {
  idx += size-1;
  t[idx] = val;
  
  while (idx > 0) {
    idx = (idx-1)/2;
    t[idx] = t[2*idx+1]+t[2*idx+2];
  }
  
  return;
}

int sum_segt_rec (int *t, int a, int b, int k, int l, int r) {
  int ans = 0;
  
  if (r <= a || b <= l) {
    return 0;
  }
  
  if (a <= l && r <= b) {
    return t[k];
  }
  
  ans = sum_segt_rec(t, a, b, 2*k+1, l, (l+r)/2);
  ans += sum_segt_rec(t, a, b, 2*k+2, (l+r)/2, r);
  
  return ans;
}

int sum_segt (int *t, int a, int b, int size) {
  return sum_segt_rec(t, a, b, 0, 0, size);
}

int main () {
  int n = 0;
  int k = 0;
  long long l = 0LL;
  long long p = 0LL;
  long long a[34] = {};
  long long b[34] = {};
  
  int res = 0;
  
  long long ans = 0LL;
  long long sumabc1[1000000][3] = {};
  long long sumabc2[1000000][3] = {};
  
  long long sumb_sort[1000000][2] = {};
  int sumb_map[1000000] = {};
  
  int size = 1;
  int segt[18][1000000] = {};
  
  int n1 = 0;
  int n2 = 0;
  int idx = 0;
  
  res = scanf("%d", &n);
  res = scanf("%d", &k);
  res = scanf("%lld", &l);
  res = scanf("%lld", &p);
  for (int i = 0; i < n; i++) {
    res = scanf("%lld", a+i);
    res = scanf("%lld", b+i);
  }
  
  if (n <= 1) {
    if (a[0] <= l && b[0] >= p) {
      ans = 1LL;
    } else {
      ans = 0LL;
    }
    printf("%lld\n", ans);
    return 0;
  }
  
  n2 = n/2;
  n1 = n-n2;
  for (int i = 0; i < (1<<n1); i++) {
    for (int j = 0; j < n1; j++) {
      if ((i&(1<<j)) > 0) {
        sumabc1[i][0] += a[j];
        sumabc1[i][1] += b[j];
        sumabc1[i][2] += 1LL;
      }
    }
  }
  for (int i = 0; i < (1<<n2); i++) {
    for (int j = 0; j < n2; j++) {
      if ((i&(1<<j)) > 0) {
        sumabc2[i][0] += a[n1+j];
        sumabc2[i][1] += b[n1+j];
        sumabc2[i][2] += 1LL;
      }
    }
  }
  
  qsort(sumabc1, (1<<n1), sizeof(long long)*3, cmp_ll);
  qsort(sumabc2, (1<<n2), sizeof(long long)*3, cmp_ll);
  
  for (int i = 0; i < (1<<n2); i++) {
    sumb_sort[i][0] = sumabc2[i][1];
    sumb_sort[i][1] = (long long)i;
  }
  
  qsort(sumb_sort, (1<<n2), sizeof(long long)*2, cmp_ll);
  for (int i = 0; i < (1<<n2); i++) {
    sumb_map[(int)(sumb_sort[i][1])] = i;
  }
  
  size = (1<<n2);
  for (int i = (1<<n1)-1; i >= 0; i--) {
    int r[2] = { -1, (1<<n2) };
    while (idx < (1<<n2) && sumabc2[idx][0]+sumabc1[i][0] <= l) {
      set_segt(segt[(int)(sumabc2[idx][2])], sumb_map[idx], 1, size);
      idx++;
    }
    while (r[1]-r[0] > 1) {
      int nxt = (r[0]+r[1])/2;
      if (sumb_sort[nxt][0]+sumabc1[i][1] < p) {
        r[0] = nxt;
      } else {
        r[1] = nxt;
      }
    }
    for (int j = 0; (j <= k-((int)(sumabc1[i][2])) && j <= n2); j++) {
      ans += (long long) sum_segt(segt[j], r[1], (1<<n2), size);
    }
  }
  
  printf("%lld\n", ans);
  return 0;
}
0