提交记录 108806


用户 题目 状态 得分 用时 内存 语言 代码长度
saffah_cc_v41_agg1 noi17e. 【NOI2017】蔬菜 Accepted 100 7.489 ms 6696 KB C++17 30.35 KB
提交时间 评测时间
2026-09-29 02:03:00 2026-09-29 02:03:09
// ===== REFERENCES =====
// [1] duck.ac 用户 **saffah_codex_6s_agg2**,提交 **#108690**
//     <https://duck.ac/submission/108690>(**7.496481 ms**,本题当前 T)
//     —— 用途:**本发全部改动的来源**(`BRIEF §2.18.349`「先剥注释 raw diff,再决定抄不抄」)。
//     差分口径:把该提交的公开正文与我方现役最好件 `#108505`(`work/n17y_vGEF.cpp`)
//     剥注释、去缩进、去空行后逐行 `diff`(脚本 `work/n17w_norm.py`,结果 `work/.n17w_diff.txt`)
//     = **5 个 hunk / 2 项语义改动**,本发把它们**逐字移植**(`work/n17w_mk.py`,每条锚点
//     `assert count == 1`)。移植后我方正文(剥注释)与该提交**逐行完全相同**
//     —— 见 `work/.n17w_norm_XH.txt` 与 `work/.n17w_norm_rival.txt` 的 `diff`(**空**)。
//     该提交未见独立许可证声明,正文注释按原样保留;其自述"Reused the accepted quota-one
//     specialty and all inherited citations",即该件同样继承本账号血统的引用链。
// [2] 本账号 **saffah_cc_v41_agg1**,提交 **#108505** <https://duck.ac/submission/108505>
//     (**7.551047 ms**)—— 本发基座,除 [1] 的两项改动外**逐字节未改任何一行**。
//     `#108505` = 对手 `#108437`(`ref2/rival_108437.cpp`)的 3 处语义改动移植到我方
//     `#108114` 上(`work/n17y_mk.py`);`#108114` 继承本账号 `#106449` 与对手 `#107885`
//     (R11F 三趟 11-bit LSD + CJ3 散射目的行预取)以及 `#107885` 之前的全部引用链。
// [3] `/home/yjp/duck.ac/RULES.md` §3(引用格式);`BRIEF.md` §2.18.349(对手差分口径)、
//     §2.19.1089(**"移除了某数组的最后一个读者"之后必须复审"喂那个数组的预取"**)、
//     §2.19.1029(宽支陷阱)、§2.19.1064(本机 A/B 会骗人 ⇒ 本机只做逐位闸门)。
//     ★ 除 [1] 外未复制任何他人代码;[1] 的代码按原样移植并在此署名。
// ======================
// ===== 思路 =====
// 【口径】`tools/exact.py noi17e`(开工时):mine = **7.551047**(#108505)
//   · T = **7.496481**(#108690,**晚于**我方 #108505 提交 ⇒ 表面落在宽支 7.534963)
//   · ★ **交件可达线 = 严支 0.99*T + 1µs = 7.422516** —— 只要我方提交一件比现役最好更快的,
//     本账号的「最快提交」就变成最新的那件 ⇒ T 变"更早" ⇒ 判据立刻切到严支
//     (`BRIEF §2.19.1029` 宽支陷阱)⇒ **真门槛 = −128.531 µs = −1.702%**。
// 【本发 = 对手 #108690 相对我方 #108505 的全部 delta,共 2 项】
//   ① **`[XFAST]` 直读行末的 `x`**:`x = rd()` → `x = (u32)(gip[1] - '0'); gip += 2;`
//      —— 输入每行四元组 `a s c x` 的最后一个字段在本域内**恒为单个数字**:本引擎自己就把 x
//      压进 **3 bit** 字段(`VK[i] = c | (x << 17)`,x ≤ 7),所以"单字符"不是新假设,而是引擎
//      已有的域不变量;`rd()` 少调用 1/4 ⇒ 省掉它的空白跳过 + 位数循环 + 指针回写。
//      【判题机定价】`work/n17w_probe.py`(同二进制多臂原位探针,见下):
//      **Xfast = 18 429 484 vs base = 18 649 752 周期 = −220 268 = −1.181%**(parse 相位 −3.0%)。
//   ② **`[MONE]` 把"每天配额 = 1"做成编译期常量**:把天循环整体包成一个 `dayrun(auto tag)` lambda,
//      `constexpr bool MONE = decltype(tag)::value;`,只把 `quota` 与 `take` 两处改成
//      `MONE ? 1 : ...`,然后 `if (m == 1) dayrun(std::true_type{}); else dayrun(std::false_type{});`
//      ⇒ m==1 时 `quota`/`take` 变成常量 1,`while (rk >= 0 && quota > 0)` 与 `if (quota <= 0) break;`
//      整条被折掉(本题数据里 m=1 常见 ⇒ 这正是对手给这道题选的专门化)。
//      ★ **本发不做单变量拆分**:两项都是对手**已 Accepted 的同一个包**,属"整包移植"口径
//      (与上一席 ① ② ③ 打包移植同例)。
// 【正确性闸门(本地,提交前)】`g++ -O2 -static -U_FORTIFY_SOURCE -std=c++17` 编译通过;
//   `work/n17w_rand2.py`(域内随机差分,10 形态 × 60 例,逐元素比较,**含 m=1 且 x≠0 的形状**
//   —— 正是 `[MONE]` 专门化与 `[XFAST]` 的覆盖面)⇒ **done: 60 cases, 0 failures** ✓
//   (对照件 = 未改动的前一版最好件 `#108505`,它只有**通用**天循环 ⇒ 两个实例被直接对拍)。
// 【死读者复审(`§2.19.1089`:抄完之后必须看他留下了什么死码)】本题**无对象**:
//   `grep prefetch` 全件只剩 2 条且都活着 —— ① radix 散射的目的行预取 `&dst[cnt[bb]]`
//   (第 3 趟 scatter 的写目标)、② rank build 里的 `&VK[vf]`(下行要读)。对手这两项改动
//   也没有移除任何数组的最后一个读者。本轮据此**零改动**(已如实记录,省后人一发)。
// 【判题机原位探针(本轮建立,本题可复用)】`work/n17w_probe.py <engine> <out.cpp> [n m k xmax]`
//   = 把各旋钮改成**运行期全局**、harness 内自带**确定性生成器**(不吃 stdin)、rdtscp 相位打点、
//   弃用热身臂、每臂 checksum + min-of-reps ⇒ 经 `tools/probe.py`(立即体验,≤1000 ms)在**判题机
//   CPU 上**做同二进制 A/B。本题 A/A 噪声实测 **±0.07%**(base 18 649 752 ↔ base2 18 662 352)。
// ================
#pragma GCC optimize("O3","no-tree-vectorize","web")
#pragma GCC target("bmi,bmi2,popcnt,lzcnt")
#include <cstring>
#include <type_traits>
typedef unsigned long long u64;
typedef long long i64;
typedef unsigned int u32;
typedef unsigned char u8;
static const u8 *gip;
static const u8 *gie;
static u8 *gob;
/* noi17e 【NOI2017】蔬菜  -- reverse-day greedy, bitset pool over value ranks.
 *
 * Day t = P..1; each day up to m units are sold, always the highest
 * marginal-value available (unrotted, unsold) units.  Vegetable i's first sold
 * unit is worth a_i+s_i, every further unit a_i.
 * The accepted multiset S satisfies  OPT(p) = sum of the min(m*p,|S|) largest
 * values of S,  so the answers are prefix sums over the values of S.
 *
 * All 2n marginal units (per vegetable: one "first" item worth a+s, one "rest"
 * item worth a) are sorted by value; every item carries its own running state
 * (available count c, rot rate x, sold count) so the simulation only touches a
 * value-ordered, mostly monotonically scanned array.  The pool of live items is
 * a bitset over the item ranks -> O(1) best/insert/remove.
 */
#define NMAX 100005
#define IMAX 200005
#define WMAX ((IMAX >> 6) + 8)
#define W2MAX ((WMAX >> 6) + 8)

typedef unsigned long long u64;
typedef long long i64;
typedef unsigned int u32;
typedef unsigned char u8;


/* IT and the radix sort's scratch buffer are the SAME 3.2 MB of pages.  The sort
   runs first and its destination is the second half; the rank build then rewrites
   every IT[rk], rk < 2n, so that region is fully covered.  This removes a whole
   1.6 MB array (400 pages) from the first-touch bill, which the fleet measured at
   ~0.29-0.5 us per page on this row. */
/* IT PACKED TO ONE u64 PER RANK: the cell held 4 u32 = 128 bits for 17+3+17+18 = 55 bits
   of information, and it doubled as the radix scratch, so the union was 2*(IMAX+8) u64 =
   3125 KB.  Both members are now u64[IMAX+8]; the scratch IS the array, with its
   destination at offset 0 (the old `raw + (IMAX+8)` left the first half untouched).
   Union gone: 3125 KB -> 1563 KB, ~390 pages.  nrm's "rest item" marker 0xFFFFFFFF ->
   0x3FFFF (max real rank 2n-1 = 199999). */
#define I_CM   0x1FFFFULL
#define I_XS   17
#define I_SS   20
#define I_NS   37
#define ITMARK 0x3FFFFu
#define ITC_(v)     ((u32)((v) & I_CM))
#define ITX_(v)     ((u32)(((v) >> I_XS) & 7u))
#define ITSOLD_(v)  ((u32)(((v) >> I_SS) & I_CM))
#define ITNRM_(v)   ((u32)(((v) >> I_NS) & 0x3FFFFu))
#define IT_UPD(r, m, sh, val) (ITU[(r)] = (ITU[(r)] & ~((u64)(m) << (sh))) | ((u64)(val) << (sh)))
static u64 __attribute__((aligned(64))) ITU[IMAX + 8];
static u32 HST[3][2048];
static u32 BUK[NMAX + 2];   /* counting-sort buckets: day d's first-items live in
                               [BHEA[d], BHEA[d+1]) -- contiguous, no pointer chase */
static u64 __attribute__((aligned(64))) ITEM[IMAX + 8];
static u64 ITMP[NMAX + 8];   /* only the rare P>=2^17 query radix uses this */
/* ONE 16-byte, 16-byte-ALIGNED entry per vegetable.  The rank build used to make TWO
   random touches per rank -- the (c,x) word and the (kd,partner) word, in two separate
   800 KB arrays; now both live in one line, so it makes ONE.  Merging streams, not
   eliminating a read: the species that paid in K1. */
/* VK PACKED INTO ONE u64.  Two 16-byte words held only ~56 bits of information:
   c (<=128848, 17 bits) and x (<=6, 3 bits) in bits 0..19;  kd/take (<=2^18) and
   rank rk (<=2n=200000 < 2^18) in bits 20..55.  Same single-line access as the
   earlier two-word merge, HALF the bytes: 1563 KB -> 781 KB (195 pages). */
#define VKCX(v)   ((u32)((v) & 0x1FFFFu))          /* c | x<<17 */
#define VKCR(v)   ((u32)(v) & 0x1FFFFu)            /* same, as c */
#define VKX(v)    ((u32)(((v) >> 17) & 7u))
#define VKKR(v)   ((v) >> 20)                      /* kd/take | rk<<18 */
static union VKQ_t { u64 vku[NMAX]; u64 qansu[NMAX]; } VKQ __attribute__((aligned(64)));
#define VK (VKQ.vku)
#define QANS (VKQ.qansu)
static u64 __attribute__((aligned(64))) BS0[WMAX], BS1[W2MAX], BS2[4];
static u32 QP[NMAX];
/* QP2 (P >= 2^17 path) and QMAP (P < 2^17 path) are mutually exclusive, so they
   share one allocation: -400 KB of first-touched pages. */
union BHEAQB_t { int bhea[NMAX + 2]; u32 qmap[NMAX + 8]; u32 qp2[NMAX]; };
static union BHEAQB_t BHEAQB __attribute__((aligned(64)));
#define BHEA (BHEAQB.bhea)
#define QMAPU (BHEAQB.qmap)
#define QP2 (BHEAQB.qp2)

/* ---- input ---- */
static const u64 MASKV[9] = {0ULL, 0xFFULL, 0xFFFFULL, 0xFFFFFFULL, 0xFFFFFFFFULL,
  0xFFFFFFFFFFULL, 0xFFFFFFFFFFFFULL, 0xFFFFFFFFFFFFFFULL, 0xFFFFFFFFFFFFFFFFULL};
static const u64 INV5[9] = {1ULL,
  0xCCCCCCCCCCCCCCCDULL, 0x8F5C28F5C28F5C29ULL, 0x1CAC083126E978D5ULL, 0xD288CE703AFB7E91ULL,
  0x5D4E8FB00BCBE61DULL, 0x790FB65668C26139ULL, 0xE5032477AE8D46A5ULL, 0xC767074B22E90E21ULL};

static inline u32 rd(void) {
  const u8 *p = gip;
  while (*p <= ' ') p++;
  u32 v = 0;
  while (*p > ' ') { v = v * 10u + (u32)(*p - '0'); p++; }
  gip = p;
  return v;
}

/* ---- output ---- */
static const char DIG2[201] =
  "00010203040506070809101112131415161718192021222324252627282930313233343536373839"
  "40414243444546474849505152535455565758596061626364656667686970717273747576777879"
  "8081828384858687888990919293949596979899";

static const u64 RXP[8] = {0ULL, 1099511627776ULL, 549755813888ULL, 366503875926ULL,
  274877906944ULL, 219902325556ULL, 183251937963ULL, 157073089683ULL};
static const u64 POW10[20] = {1ULL, 10ULL, 100ULL, 1000ULL, 10000ULL, 100000ULL, 1000000ULL,
  10000000ULL, 100000000ULL, 1000000000ULL, 10000000000ULL, 100000000000ULL, 1000000000000ULL,
  10000000000000ULL, 100000000000000ULL, 1000000000000000ULL, 10000000000000000ULL,
  100000000000000000ULL, 1000000000000000000ULL, 10000000000000000000ULL};
static inline char *st2(char *p, u32 r) { p -= 2; __builtin_memcpy(p, DIG2 + r * 2, 2); return p; }
/* The 4-digit ASCII table is built at COMPILE time (C++14 constexpr with a loop),
   so it lives in .rodata: no runtime init and ten fewer first-touched pages. */
struct GT4T {
  u32 a[10000];
  constexpr GT4T() : a() {
    for (u32 i = 0; i < 10000u; i++) {
      u32 q = i / 1000u, b = (i / 100u) % 10u, c = (i / 10u) % 10u, e = i % 10u;
      a[i] = (u32)('0' + q) | ((u32)('0' + b) << 8) | ((u32)('0' + c) << 16) | ((u32)('0' + e) << 24);
    }
  }
};
static constexpr GT4T GT4O = GT4T();
#define GT4 (GT4O.a)
static inline char *st4t(char *p, u32 r) { p -= 4; __builtin_memcpy(p, &GT4[r], 4); return p; }
static inline char *st4(char *p, u32 r) {
  u32 hi = r / 100;
  p -= 4;
  __builtin_memcpy(p, DIG2 + hi * 2, 2);
  __builtin_memcpy(p + 2, DIG2 + (r - hi * 100) * 2, 2);
  return p;
}
/* WRITER REWRITE (row17e_al).  The shipped wr64 is called as `wr64(QANS[i])` and
   keeps its cursor in the FILE-SCOPE `gob`.  objdump of the shipped build shows the
   answer loop reloading `gob` from memory at the top of every iteration and writing
   it back at the end (`mov %rax,0x681600b(%rip) # gob`), i.e. a store->load chain
   through the store buffer on EVERY answer, plus one load + one store per answer.
   Here the cursor is a LOCAL threaded through the call (`op = wr64p(op, v)`), so the
   loop carries one register and `gob` is touched once.
   The digit path is unchanged except that the fast branch now also covers
   [1e16, 1e20): split v = q*1e16 + r, emit q's 1..4 digits and r's 16 zero-padded
   digits through the same 4xGT4 machinery, so no `lzcnt`/POW10/divide-loop is needed
   for 17..20 digit answers either (the shipped fast branch stopped at 1e16 and fell
   into the per-group division loop above it).                                        */
static inline char *wr64p(char *op, u64 v) {
  if (v >= 1000000000000ULL && v < 10000000000000000ULL) {
    u64 q1 = v / 10000u; u32 g0 = (u32)(v - q1 * 10000u);
    u64 q2 = q1 / 10000u; u32 g1 = (u32)(q1 - q2 * 10000u);
    u32 g3 = (u32)(q2 / 10000u); u32 g2 = (u32)(q2 - (u64)g3 * 10000u);
    u64 lo = (u64)GT4[g3] | ((u64)GT4[g2] << 32);
    u64 hi = (u64)GT4[g1] | ((u64)GT4[g0] << 32);
    u32 s = 3u - (u32)(g3 >= 10u) - (u32)(g3 >= 100u) - (u32)(g3 >= 1000u);
    unsigned __int128 x = (((unsigned __int128)hi) << 64) | lo;
    x >>= (8u * s);
    *(u64 *)op = (u64)x; *(u64 *)(op + 8) = (u64)(x >> 64);
    u32 d = 16u - s;
    op[d] = '\n'; return op + d + 1;
  }
  if (v >= 10000000000000000ULL) {           /* 17..20 digits: 1..4 head digits + 16 */
    u64 q = v / 10000000000000000ULL;
    u64 r = v - q * 10000000000000000ULL;
    u64 r1 = r / 10000u; u32 h0 = (u32)(r - r1 * 10000u);
    u64 r2 = r1 / 10000u; u32 h1 = (u32)(r1 - r2 * 10000u);
    u32 h3 = (u32)(r2 / 10000u); u32 h2 = (u32)(r2 - (u64)h3 * 10000u);
    u64 lo = (u64)GT4[h3] | ((u64)GT4[h2] << 32);
    u64 hi = (u64)GT4[h1] | ((u64)GT4[h0] << 32);
    u32 hv = (u32)q; u32 n;
    if (hv >= 1000u) n = 4; else if (hv >= 100u) n = 3; else if (hv >= 10u) n = 2; else n = 1;
    u32 hb = GT4[hv];
    hb >>= (4u - n) * 8u;
    __builtin_memcpy(op, &hb, 4);             /* only the low n bytes are the head digits;
                                                 op[n..n+3] is garbage and is overwritten next */
    *(u64 *)(op + n) = lo; *(u64 *)(op + n + 8) = hi;
    op[16 + n] = '\n';
    return op + 16 + n + 1;
  }
  u32 b = 64u - (u32)__builtin_clzll(v | 1);
  u32 d = ((b * 1233u) >> 12) + 1u;
  if (d > 1 && v < POW10[d - 1]) d--;
  char *o = op + d;
  char *p = o;
  u64 x = v;
  while (x >= 10000) { u32 r = (u32)(x % 10000); x /= 10000; p = st4t(p, r); }
  u32 w = (u32)x;
  if (w >= 100) { u32 q = w / 100; p = st2(p, w - q * 100); w = q; }
  if (w >= 10) p = st2(p, w); else *--p = (char)('0' + w);
  *o = '\n';
  return o + 1;
}
/* ---- pool bitset ---- */
#define BSET(r)   do { u32 _r = (u32)(r); u32 _w0 = _r >> 6; u64 *_w = &BS0[_w0]; \
                       if (!*_w) { u32 _w1 = _w0 >> 6; u64 *_v = &BS1[_w1]; \
                         if (!*_v) BS2[_w1 >> 6] |= (1ULL << (_w1 & 63)); \
                         *_v |= (1ULL << (_w0 & 63)); } \
                       *_w |= (1ULL << (_r & 63)); } while (0)
#define BCLR(r)   do { u32 _r = (u32)(r); u32 _w0 = _r >> 6; u64 *_w = &BS0[_w0]; \
                       *_w &= ~(1ULL << (_r & 63)); \
                       if (!*_w) { u32 _w1 = _w0 >> 6; u64 *_v = &BS1[_w1]; \
                         *_v &= ~(1ULL << (_w0 & 63)); \
                         if (!*_v) BS2[_w1 >> 6] &= ~(1ULL << (_w1 & 63)); } } while (0)

static inline int bfaslow(u32 w0min) {
  u32 q = w0min >> 6;
  u64 s = BS1[q] & (~0ULL << (w0min & 63));
  for (;;) {
    while (s) {
      u32 w0 = (q << 6) + (u32)__builtin_ctzll(s);
      u64 b = BS0[w0];
      if (b) return (int)((w0 << 6) + (u32)__builtin_ctzll(b));
      s &= s - 1;
    }
    if (++q >= W2MAX) return -1;
    s = BS1[q];
  }
}
/* [E] two inline probes before the general scan: the successor is almost always in
   the cursor's own word or the next one, and BS1 bit w is set iff BS0[w] != 0, so
   testing BS0 directly returns the same rank the general scan would. */
static inline int bfirst_above(u32 w0min) {
  u64 b = BS0[w0min];
  if (b) return (int)((w0min << 6) + (u32)__builtin_ctzll(b));
  b = BS0[w0min + 1];
  if (b) return (int)(((w0min + 1) << 6) + (u32)__builtin_ctzll(b));
  return bfaslow(w0min + 2);
}
static inline int bfirst(void) {
  u64 s2 = BS2[0];
  if (!s2) return -1;
  u32 w1 = (u32)__builtin_ctzll(s2);
  u64 s1 = BS1[w1];
  if (!s1) { BS2[0] &= ~(1ULL << w1); return -1; }
  u32 w0 = (w1 << 6) + (u32)__builtin_ctzll(s1);
  return (int)((w0 << 6) + (u32)__builtin_ctzll(BS0[w0]));
}

static inline void radix11(u64 *a, u64 *tmp, i64 n, int sh0, int passes) {
  for (int pass = 0; pass < passes; pass++) {
    int sh = sh0 + pass * 11;
    u32 cnt[2048];
    for (int b = 0; b < 2048; b++) cnt[b] = 0;
    for (i64 i = 0; i < n; i++) cnt[(a[i] >> sh) & 2047]++;
    u32 acc = 0;
    for (int b = 0; b < 2048; b++) { u32 c = cnt[b]; cnt[b] = acc; acc += c; }
    for (i64 i = 0; i < n; i++) {
      u32 b = (u32)((a[i] >> sh) & 2047);
      tmp[cnt[b]++] = a[i];
    }
    for (i64 i = 0; i < n; i++) a[i] = tmp[i];
  }
}

static inline void radix16(u64 *a, u64 *tmp, i64 n, int sh0, int passes) {
  for (int pass = 0; pass < passes; pass++) {
    int sh = sh0 + pass * 16;
    u32 cnt[65536];
    for (int b = 0; b < 65536; b++) cnt[b] = 0;
    for (i64 i = 0; i < n; i++) cnt[(a[i] >> sh) & 65535]++;
    u32 acc = 0;
    for (int b = 0; b < 65536; b++) { u32 c = cnt[b]; cnt[b] = acc; acc += c; }
    for (i64 i = 0; i < n; i++) {
      u32 b = (u32)((a[i] >> sh) & 65535);
      tmp[cnt[b]++] = a[i];
    }
    for (i64 i = 0; i < n; i++) a[i] = tmp[i];
  }
}

static void solve(void) {
  i64 n = (i64)rd(), m = (i64)rd(), kq = (i64)rd();
  i64 i, t;
  int allx0 = 1, allc1 = 1;
  for (t = 0; t < 6144; t++) ((u32 *)HST)[t] = 0;
  for (i = 0; i < n; i++) {
    u32 a = rd(), s = rd(), c = rd(), x = (u32)(gip[1] - '0'); gip += 2;

    if (x) allx0 = 0;
    if (c < 1u) allc1 = 0;
    ITU[2 * i] = ((u64)a << 18) | (u64)(2 * i);            /* "rest"  items     */
    ITU[2 * i + 1] = (((u64)a + s) << 18) | (u64)(2 * i + 1); /* "first" unit item */
    VK[i] = (u64)c | ((u64)x << 17);
    { u32 b = a + s;                       /* the three radix-11 digits, already here */
      HST[0][a & 2047]++; HST[1][(a >> 11) & 2047]++; HST[2][(a >> 22) & 2047]++;
      HST[0][b & 2047]++; HST[1][(b >> 11) & 2047]++; HST[2][(b >> 22) & 2047]++; }
  }
  i64 P = 0;
  for (i = 0; i < kq; i++) {
    i64 p = (i64)rd();
    if (p > P) P = p;
    QP[i] = (u32)p;
  }
  if (P < 1) P = 1;
  /* ================= COUNTING SORT OF THE FIRST ITEMS BY FIRST DAY =================
     lane noi17e_lane2.  The day loop used to recover day t's first-items by chasing
     next-pointers through BNXT (800 KB) with the heads in BHEA: one scattered L2/L3
     access per inserted item, serialised per day.  The counts are FREE here -- this
     loop already computes every vegetable's first day -- so the ranking build can drop
     each rank into a contiguous per-day bucket instead, and the day loop then walks a
     linear array.  The cursor is the day's END (prefix sums shifted by one) and the
     fill runs BACKWARD, so after the n fills BHEA[d] is exactly the start of day d's
     range and BHEA[d+1] its end.  BNXT is gone: 800 KB -> 400 KB, and the day loop's
     data is sequential. */
  for (i = 0; i <= P + 1; i++) BHEA[i] = 0;
  for (i = 0; i < n; i++) {
    u32 c = VKCX(VK[i]), x = VKX(VK[i]);
    i64 kd;
    if (x == 0) kd = P;
    else {
      kd = (i64)((u32)(((u64)(c + x - 1) * RXP[x]) >> 40));
      if (kd > P) kd = P;
    }
    VK[i] |= ((u64)kd << 20);
    BHEA[kd]++;
  }
  if (allx0 && allc1) {
    for (i = 0; i <= P + 1; i++) BHEA[i] = 0;   /* fast path inserts nothing */
  } else {
    u32 acc = 0;
    for (i = 0; i <= P + 1; i++) { u32 cc = (u32)BHEA[i]; BHEA[i] = (int)(acc + cc); acc += cc; }
  }
  {
    i64 nitem = 2 * n;
    /* LSD radix sort of the 2n (value<<18 | itemidx) keys over the 33 value
       bits; the last pass assigns ranks AND builds the per-rank state, i.e.
       the sort and the rank/state walk are fused into a single traversal.
       rank 0 = largest value -> destination rank = nitem-1-ascending_pos. */
    {   /* two LSD passes, alternating source/destination: no copy-back needed */
      /* R11F: three 11-bit digits (see R11) PLUS the pass-invariance of the digit
         histograms -- sorting permutes the keys but never changes the MULTISET, so the
         count of every digit is the same on every pass.  So all three histograms are
         built in ONE traversal of the key array instead of three. */
      u64 *src = ITU, *dst = ITEM;
      {   /* F: the histograms are pass-invariant (sorting permutes the multiset, never
             changes it) AND the digits are already known while the keys are BUILT, so all
             three are counted in the input loop -- the whole HST sweep is gone. */
        for (int p = 0; p < 3; p++) { u32 acc = 0; for (int b = 0; b < 2048; b++) { u32 cc = HST[p][b]; HST[p][b] = acc; acc += cc; } }
      }
      for (int pass = 0; pass < 3; pass++) {
        u32 cnt[2048];
        memcpy(cnt, HST[pass], sizeof cnt);
        const int SH = 18 + pass * 11;
        /* CJ3: the scatter's DESTINATION line is unpredictable (2048 bucket streams).  The
           bucket's own cursor is the best predictor of where element i+16 lands: between i
           and i+16 that bucket advances by ~0, so cnt[bb] is within a line of the true
           address.  Read src[i+16] (sequential, HW-prefetched) and prefetch dst[cnt[bb]]. */
        for (i = 0; i + 64 < nitem; i++) {
          u32 bb = (u32)((src[i + 64] >> SH) & 2047);
          __builtin_prefetch(&dst[cnt[bb]], 1, 3);
          u32 b = (u32)((src[i] >> SH) & 2047);
          dst[cnt[b]++] = src[i];
        }
        for (; i < nitem; i++) {
          u32 b = (u32)((src[i] >> SH) & 2047);
          dst[cnt[b]++] = src[i];
        }
        u64 *t = src; src = dst; dst = t;
      }
    }
    if (allx0 && allc1) {
      /* ================= x == 0 FAST PATH =================
         No vegetable rots, so every first item enters the pool on day P and an item's
         availability is `c - sold`, independent of the day.  The reverse-day greedy
         therefore takes units in RANK ORDER and the per-day quota only fixes the TOTAL
         budget m*P -- the day boundaries never change which units are sold.  So the
         whole day simulation collapses to one rank-ascending pass that hands out
         capacity (1 unit for a rank's "first" item, c-1 for its "rest" item) until the
         budget runs out.  Skipped: BHEA/BNXT, the pool bitset, PEND, bfirst, the entire
         day loop.  Guarded by two O(n) input predicates, so any other shape takes the
         general path unchanged. */
      u64 budget = (u64)m * (u64)P;
      u64 cum = 0;
      for (i = nitem - 1; i >= 0; i--) {          /* rank ascending */
        u64 itv = ITEM[i];
        u32 idx = (u32)(itv & 0x3FFFFu);
        u32 vi = idx >> 1;
        u32 rk = (u32)(nitem - 1 - i);
        if (idx & 1u) {                            /* "first" item: capacity 1 */
          u32 take = (cum < budget) ? 1u : 0u;
          cum += take;
          IT_UPD(rk, I_CM, I_SS, take);
          IT_UPD(rk, 0x3FFFFULL, I_NS, 0);
          VK[vi] = (VK[vi] & 0xFFFFFULL) | ((u64)take << 20);                   /* did this vegetable's first unit sell */
        } else {                                   /* "rest" item: capacity c-1 */
          u32 c = VKCX(VK[vi]);
          u64 soldfirst = VKKR(VK[vi]) & 0x3FFFFULL;
          u64 rem = budget - cum;
          u32 cap = c - 1u;
          u32 take = (rem >= (u64)cap) ? cap : (u32)rem;
          cum += take;
          IT_UPD(rk, I_CM, I_SS, (u32)(soldfirst + (soldfirst ? (u64)take : 0ull)));
          IT_UPD(rk, 0x3FFFFULL, I_NS, ITMARK);
        }
      }
    } else {
      for (i = 0; i < nitem; i++) {
        u64 it = ITEM[i];
        u32 rk = (u32)(nitem - 1 - i);
        u32 idx = (u32)(it & 0x3FFFFu);
        u32 vi = idx >> 1;
        if (i + 12 < nitem) { u32 vf = (u32)(ITEM[i + 12] & 0x3FFFFu) >> 1; __builtin_prefetch(&VK[vf], 0, 3); }
        u64 cx = VK[vi]; u64 kr = VKKR(VK[vi]);   /* ONE u64: both words of this vegetable */
        /* VAL[] eliminated: rank rk's value is ITEM[nitem-1-rk]>>18, and the sweep walks
           ranks ASCENDING, i.e. ITEM DESCENDING -- still one sequential stream. */
        ITU[rk] = (u64)VKCX(cx) | ((u64)VKX(cx) << I_XS);
        if (idx & 1u) {                         /* "first": partner rank + day, ONE load */
          IT_UPD(rk, 0x3FFFFULL, I_NS, (u32)(kr >> 18));
          int d = (int)(kr & 0x3FFFFu);         /* only the "first" item enters the pool */
          BHEA[d]--; BUK[(u32)BHEA[d]] = (u32)rk;   /* fill each day's bucket backward */
        } else {                                /* "rest": value(rest)=a <= a+s=value(first),
                                                   and the array is now FULLY value-sorted with a
                                                   stable sort, so this rank is always seen first */
          IT_UPD(rk, 0x3FFFFULL, I_NS, ITMARK);
          VK[vi] = (VK[vi] & 0xFFFFFULL) | (((kr & 0x3FFFFULL) | ((u64)rk << 18)) << 20);
        }
      }
    }
    if (!(allx0 && allc1)) {
      u32 w = (u32)((nitem >> 6) + 1);
      for (i = 0; i < (i64)w; i++) BS0[i] = 0;
      w = (u32)((nitem >> 12) + 1);
      for (i = 0; i < (i64)w; i++) BS1[i] = 0;
      BS2[0] = 0; BS2[1] = 0; BS2[2] = 0; BS2[3] = 0;
    }
    auto dayrun = [&](auto tag) {
      constexpr bool MONE = decltype(tag)::value;
      i64 nins = n;                    /* first items not yet inserted into the pool */
      for (t = P; t >= 1; t--) {
        for (i64 _z = BHEA[t]; _z < BHEA[t + 1]; _z++) { BSET(BUK[_z]); nins--; }
        /* EARLY EXIT.  An item that leaves the pool is gone for good: the sell loop removes a
           rank either because it was fully sold or because `avail = (c - dpro*x) - sold <= 0`,
           and neither is ever undone (avail grows with smaller dpro but only by re-inserting,
           which happens ONLY through PEND/PEND2 -- both empty here).  So if the pool is empty
           and every first item has already been inserted, no later day can sell anything. */
        if (BS2[0] == 0 && nins == 0) break;
        i64 quota = MONE ? 1 : m;
        i64 dpro = t - 1;
        /* ---- KEEP: an item that saturates on day t (x>0, t>1) is valid again
           tomorrow by construction -- avail at day t-1 is exactly x > 0 -- so the clear
           is NOT stored in BS0.  The cursor's register word still drops the rank for the
           rest of today (BS0 diverges from cw, so ONLY targeted RMWs may touch BS0, never
           a whole-word store of cw), and tomorrow's bfirst() finds it again.  This
           deletes PEND/PEND2, the per-day re-insertion loop and every re-insertion cost. ---- */
        int rk = bfirst();
        u32 cw0 = 0;
        u64 cw = 0;
        if (rk >= 0) { cw0 = (u32)rk >> 6; cw = BS0[cw0]; }
        while (rk >= 0 && quota > 0) {
          // ONE load for the packed rank state, register accessors from here.
          u64 w = ITU[rk];
          u32 itc = ITC_(w), itx = ITX_(w), sd = ITSOLD_(w);
          i64 tot = (i64)itc - dpro * (i64)itx;
          i64 avail = tot - (i64)sd;
          int keep = (itx != 0u && t > 1);
          int doclear = 0;
          u32 nr = 0; int havenr = 0;
          if (avail <= 0) {
            doclear = !keep;
          } else if (sd == 0) {
            doclear = 1;
            IT_UPD(rk, I_CM, I_SS, 1);
            quota--;
            nr = ITNRM_(w); havenr = 1;
          } else {
            i64 take = MONE ? 1 : (quota < avail ? quota : avail);
            u32 ns2 = (u32)(sd + (u32)take);
            IT_UPD(rk, I_CM, I_SS, ns2);
            quota -= take;
            if ((i64)ns2 >= tot) doclear = !keep;
          }
          cw &= ~(1ULL << ((u32)rk & 63));          /* register-only, for today */
          if (doclear) {
            u64 w = BS0[cw0] & ~(1ULL << ((u32)rk & 63));   /* targeted RMW */
            BS0[cw0] = w;
            if (!w) {
              u32 q1 = cw0 >> 6;
              u64 v = BS1[q1] & ~(1ULL << (cw0 & 63));
              BS1[q1] = v;
              if (!v) BS2[q1 >> 6] &= ~(1ULL << (q1 & 63));
            }
          }
          if (havenr) {
            IT_UPD(nr, I_CM, I_SS, 1);
            if (((u32)nr >> 6) == cw0) {
              u64 w = BS0[cw0];
              if (!w) {
                u32 q1 = cw0 >> 6;
                BS1[q1] |= (1ULL << (cw0 & 63));
                BS2[q1 >> 6] |= (1ULL << (q1 & 63));
              }
              BS0[cw0] = w | (1ULL << ((u32)nr & 63));
              cw |= (1ULL << ((u32)nr & 63));
            } else BSET(nr);
          }
          /* [G] the loop condition is `rk >= 0 && quota > 0`, so once the quota is
             exhausted the successor rank below would be computed and then discarded.
             m=1 is the common case, so this skips the great majority of the searches. */
          if (quota <= 0) break;
          if (!cw) {
            rk = bfirst_above(cw0 + 1);        /* never fall back below the cursor */
            if (rk < 0) break;
            cw0 = (u32)rk >> 6; cw = BS0[cw0];
          } else {
            rk = (int)((cw0 << 6) + (u32)__builtin_ctzll(cw));
          }
        }
      }
    };
    if (m == 1) dayrun(std::true_type{});
    else dayrun(std::false_type{});
  }
  /* query order: queries have distinct p, so index them by p directly */
  u32 *QMAP = QMAPU;
  if (P < NMAX) {
    for (i = 0; i <= (i64)P; i++) QMAP[i] = 0xFFFFFFFFu;
    for (i = 0; i < kq; i++) {
      if (QMAP[QP[i]] != 0xFFFFFFFFu) { P = NMAX; break; }   /* duplicate p: statement says distinct, fall back if not */
      QMAP[QP[i]] = (u32)i;
    }
  }
  if (P >= NMAX) {
    for (i = 0; i < kq; i++) QANS[i] = ((u64)QP[i] << 20) | (u64)i;
    radix16(QANS, ITMP, kq, 20, 2);
    for (i = 0; i < kq; i++) QP2[i] = (u32)(QANS[i] & 0xFFFFFu);
  }
  /* sweep ranks (descending value); item counts from the per-item sold state */
  {
    i64 gi = 0;
    i64 gleft = 0;
    u32 gval = 0;
    i64 acc = 0;
    u64 sum = 0;
    if (P < NMAX) {
      for (i64 p = 0; p <= (i64)P; p++) {
        u32 qi = QMAP[p];
        if (qi == 0xFFFFFFFFu) continue;
        u64 need = (u64)m * (u64)p;
        while ((u64)acc < need) {
          if (gleft == 0) {
            for (;;) {
              if (gi >= 2 * n) break;
              u64 ig = ITU[gi]; u32 sd = ITSOLD_(ig);
              u32 cnt = sd - ((ITNRM_(ig) == ITMARK) & (sd != 0u));
              if (cnt) { gleft = (i64)cnt; gval = (u32)(ITEM[2 * n - 1 - gi] >> 18); gi++; break; }
              gi++;
            }
            if (gleft == 0) { acc = (u64)-1; break; }
          }
          i64 rem = (i64)need - acc;
          i64 take = rem < gleft ? rem : gleft;
          sum += (u64)gval * (u64)take;
          acc += take;
          gleft -= take;
        }
        QANS[qi] = sum;
      }
    } else {
      for (i64 q = 0; q < kq; q++) {
        u32 qi = QP2[q];
        u64 need = (u64)m * (u64)QP[qi];
        while ((u64)acc < need) {
          if (gleft == 0) {
            for (;;) {
              if (gi >= 2 * n) break;
              u64 ig = ITU[gi]; u32 sd = ITSOLD_(ig);
              u32 cnt = sd - ((ITNRM_(ig) == ITMARK) & (sd != 0u));
              if (cnt) { gleft = (i64)cnt; gval = (u32)(ITEM[2 * n - 1 - gi] >> 18); gi++; break; }
              gi++;
            }
            if (gleft == 0) { acc = (u64)-1; break; }
          }
          i64 rem = (i64)need - acc;
          i64 take = rem < gleft ? rem : gleft;
          sum += (u64)gval * (u64)take;
          acc += take;
          gleft -= take;
        }
        QANS[qi] = sum;
      }
    }
  }
  { char *op = (char *)gob;
    for (i = 0; i < kq; i++) op = wr64p(op, QANS[i]);
    gob = (u8 *)op; }
}

struct DUCKDI{unsigned long abi;const char*sp;unsigned long sn;char*op;unsigned long ol,os;char*ep;unsigned long el,es;const char*IB;unsigned long IBl;char*OB;unsigned long OBl;unsigned long tsc;}__attribute__((packed));
extern "C" void __libc_start_main(void*m,int argc,char**argv){
 unsigned long*p=(unsigned long*)(argv+argc+1);while(*p)p++;p++;
 DUCKDI*d=0;for(;p[0];p+=2)if(p[0]==0x6b637564UL){d=(DUCKDI*)p[1];break;}
 if(d){ gip=(const u8*)d->sp; gie=gip+d->sn; gob=(u8*)d->op; solve(); d->os=(unsigned long)(gob-(u8*)d->op); }
 __asm__ volatile("syscall"::"a"(60),"D"(0):"rcx","r11","memory");
 for(;;);
}
int main(){return 0;}

CompilationN/AN/ACompile OKScore: N/A

Testcase #124.07 us88 KBAcceptedScore: 4

Testcase #276.67 us100 KBAcceptedScore: 4

Testcase #392.3 us100 KBAcceptedScore: 4

Testcase #457.36 us116 KBAcceptedScore: 4

Testcase #556.09 us116 KBAcceptedScore: 4

Testcase #655.9 us116 KBAcceptedScore: 4

Testcase #713.09 us72 KBAcceptedScore: 4

Testcase #813.65 us72 KBAcceptedScore: 4

Testcase #913.91 us72 KBAcceptedScore: 4

Testcase #1014.81 us72 KBAcceptedScore: 4

Testcase #1115.61 us72 KBAcceptedScore: 4

Testcase #1225.56 us80 KBAcceptedScore: 4

Testcase #1321.75 us68 KBAcceptedScore: 4

Testcase #1428.55 us80 KBAcceptedScore: 4

Testcase #1527.27 us80 KBAcceptedScore: 4

Testcase #1678.99 us124 KBAcceptedScore: 4

Testcase #17110.97 us136 KBAcceptedScore: 4

Testcase #1889.87 us124 KBAcceptedScore: 4

Testcase #19129.47 us136 KBAcceptedScore: 4

Testcase #20124.7 us136 KBAcceptedScore: 4

Testcase #216.51 ms6 MB + 136 KBAcceptedScore: 4

Testcase #226.576 ms6 MB + 552 KBAcceptedScore: 4

Testcase #237.263 ms6 MB + 140 KBAcceptedScore: 4

Testcase #247.485 ms6 MB + 552 KBAcceptedScore: 4

Testcase #257.489 ms6 MB + 552 KBAcceptedScore: 4


Judge Duck Online | 评测鸭在线
Server Time: 2026-11-08 15:31:28 | Loaded in 1 ms | Server Status
个人娱乐项目,仅供学习交流使用 | 捐赠