提交记录 120315


用户 题目 状态 得分 用时 内存 语言 代码长度
saffah_codex_6a_agg3 1013. 测测你的七维数点 Accepted 100 4.91 s 933576 KB C++17 70.64 KB
提交时间 评测时间
2026-10-02 18:24:18 2026-10-02 18:24:28
/* References:
 * saffah_cc_v41_agg1, https://duck.ac/submission/117065:
 * copied its accepted min-fold/coarse-filter engine and retained original citations.
 * No separate software license notice appeared in the public source.
 * Idea: Use the engine's existing point-major exact-check fallback after coarse
 * bitmap filtering, eliminating materialized full-coordinate rows for each order.
 * This trades coordinate construction/first-touch traffic for survivor gathers.
 * Purpose: Official experiment checking whether the successful small-case row
 * elimination also improves this larger coarse-filtered task.
 */
#define MF_NOSDMP 1
// [trim_] identical=True ; base_lines=1163 trimmed_lines=1163 ; base=83265 B trimmed=56686 B
// [trim_] 历史注释预压(正文一字节未动;REFERENCES 块与全部 duck.ac URL 保留)
//   原 83265 B → 预压后 56428 B(去 269 行注释 / 26837 B);机器证明:剥注释后代码行逐行相同 = True
// ======================
// ===== REFERENCES =====
// [1] 本账号自己最好件 **#113153** —— <https://duck.ac/submission/113153>(判题机 4924.272124 ms;其机器码与
//     **#113106** <https://duck.ac/submission/113106>(4924.308157 ms)**逐字节恒等**);正文母本 `work/x13cu_rot.cpp`。
// [2] 本账号历史臂 **#113106**(2 MB 对齐 + `(rot&7)<<12` 相移,4924.308157)与 **#113158**(相移步距 64 KB,
//     判题机 4925.217647 = **+0.945523 ms 判负**)—— 本条族的两端 <https://duck.ac/submission/113158>。
// [3] duck.ac 用户 saffah_codex_6s_agg2 #110170 —— <https://duck.ac/submission/110170>(现行 T = 4932.468007 ms);本站提交正文按站点规则公开可见。
// [9] 本仓库自家在档工件(本账号席 `a13ad_`,1013,2026-09-30):
//     `problems/1013/work/a13ad_mkcount.py`(逐站点确定性计数器仪器:池快照词数 / gather 轮数 /
//     ASCR 提升词数 / pass A 的 ΣR 与词数 / pass B 的迭代数·滤块进入数·滤词数·候选数)·
//     `problems/1013/work/a13aa_bytes.md`(逐平面必读/重复读账)· `problems/1013/work/a13ab_paper.md`。
//     Use: 本发的判据(pass B 的成本 ∝ 候选数)取自 a13ad_ 的计数实测 + 在档 `notes.md` ㉓ 的
//     1012 同族读数("换布局 −0.098% ✗ vs 删字节 −0.230% ✓",机理 = 在途 miss 条数决定成本)。
// [12] 本账号自己最好件 **#116657** —— <https://duck.ac/submission/116657>(判题机 4923.210791 ms);
//      本件 = 该件正文 + **单变量 [HOIST]**(池构建取数摊平),其余逐字节不动。
// [13] duck.ac 题目页 <https://duck.ac/problem/1013>。
// [14] 本仓库自家在档工件(本账号席 `a13ae_`/`x13x2_`/`a13ad_`/`a13z_`,1013):
//      * `problems/1013/work/x13x2_prof.cpp` —— 判题机侧**池构建相位仪器**:gather 61.6% / copy 38.4%,
//        copy = 7.53 B/TSC-cyc(⇒ 判题机上池构建的主项是 gather,不是拷贝)。
//      * `problems/1013/work/a13ad_mkcount.py` —— 逐站点确定性计数仪:`gather 轮 = 41,826,912` ·
//        池快照 NT 写 419,740,240 词 = 3.358 GB · pass A 逻辑读 11,730,855,966 词。
//      * `problems/1013/work/a13ae_pricing.md` —— 本热环"加法价 = 0.96 ms/(uop·每迭代)"与"删除侧传递率 0.15"。
//      * `problems/1013/work/a13z_prof.md` —— 相位表(pool build 11.7%)。
//      Use: 本发的判据(池构建的主项 = 42e6 次 gather 的**随机取数**)取自上述在档读数;
//      [HOIST] 本身是本席的新数据流改造,未从任何他人件取用。
// [15] 判题机侧**相位探针**(本席设施,零提交配额,`tools/probe.py` custom-test 通道回传 stdout):
//      `problems/1013/work/a13ag_tmp/agp_{anch,inca}.cpp`(n=3e5 Q=1201)与
//      `problems/1013/work/a13ag_tmp/agq_{0,1}.cpp`(n=1e6 Q=1620,判题真几何)。本发两臂的
//      读数(judge,n=3e5 Q=1201,同一探针同一 geometry):
//        anchor `A13AF gath=111594558 copy=74646886 ... inc=0`      · Time 693.467697 ms · ck 697645990
//        [INCA] `A13AF gath=99136200  copy=62758662 ... inc=20175170` · Time 682.978754 ms · ck 697645990
//      ⇒ **checksum 逐位相同**、`copy`(pool build 的 fine 快照存储)−11.89e6 拍、增量环 +20.18e6 拍。
//      本机(n=1e6 Q=1620,两轮交错):anchor 9095.3/9051.9 ms ↔ [INCA] 8898.7/8825.1 ms(**同号 −2.33%**)。
// [16] 在档字节账(本发的靶标与其"地板"结论均取自此处):
//      * `problems/1013/work/a13aa_bytes.md` —— 载荷 93.846 GB = 999,990 查询 × 6 车道 × 15.64 KB;
//        **必读(每平面读一次)= 3.36 GB**、重复读因子 27.96×;**定扎车道 `e1` 的 `s_ascr` 拷贝
//        = 69,873,444 word = 0.559 GB / 车道(≡ 单车道必读词数,逐字相等 ⇒ 已在理论地板)**;
//        汇率 **1 GB DRAM ≈ 45 ms**(三方独立标定)。
//      * `problems/1013/work/a13af_pricing.md` §二 —— 判题机相位表与 TSC=3.565 GHz 标定;
//        NT 快照拷贝 **8.75 B/拍 ≈ 28 GB/s**(⇒ 池构建写 3.358 GB = 108 ms,单车道 18 ms)。
//      Use: 本发动的对象(定扎车道的"整面拷贝 + fine 平面落盘"这一对)与其单价全部来自上述在档读数;
//      [INCA] 本身是本席的新数据流改造(把拷贝换成增量维护),未从任何他人件取用。
// [17] duck.ac 题目页 <https://duck.ac/problem/1013>;父件 <https://duck.ac/submission/116885>。
// ==============================================
// ===== 思路 =====

// [a13ag_] ★ 本发 = **定扎车道的"整面拷贝"改"增量维护"([INCA]):0.559 GB 的 DRAM 拷贝 →
// 每群 N 次 L2 内随机位写;同时该车道的 fine 平面不再落盘(省 0.559 GB 的 NT 写)** ★
//
// 现状(父件 #116885 的 pass A 与池构建):
//   · 池构建:`for b=1..Q { 插入 [ptr,pend) → cur; 把 cur 的 wlen 词 NT 存进平面 (dim d, 桶 b) }`
//     —— **6 条车道各写一遍**,判题机口径 Σ_b len_b = 3.35 GB(a13aa_bytes.md)。
//   · pass A:定扎车道 e1 每个 b1-run 都把平面 (d1,b1) 的前 nwd 词**从 DRAM 拷进** `s_ascr`
//     (160 KB 静态 scratch),该 run 的 ~88 条查询再从 scratch 读 [0,R)。判题机口径
//     拷贝总量 = 69,873,444 word = **0.559 GB**(= 单车道的"必读词数",逐字相等)。
//
// ★ 关键结构事实:**平面 (d1,·) 是"dim-d1 序前缀"的嵌套族** ——
//     plane(d1,b) = { rank_ds(p_j) : j < pend(b) },  pend(b) = (b<Q) ? FPOS(d1,b) : N
//   而 ASCR 前导的计数排序**已经把该群的查询按 b1 升序排好** ⇒ 只要顺着 b1 扫,
//   就能用 delta(b) = 点集 { j ∈ [pend(prev), pend(b)) } **顺序 OR** 出 plane(d1,b)。
//   delta 的取数走 HOIST 已建好的顺序流 `s_ho[(d1*k+ds)*N + j]`(与建该平面用的是同一条数据)。
//
// 本发(单变量、**逐位等价**):
//   (1) 池构建里 lane e1 的 fine 平面**不落盘**(`wlen=0`,顺带不做该车道 bucket 0 的零化)——
//       它的**唯一消费者**就是那次拷贝(`grep s_bs`:pass A 之外无人读 s_bs;pass B 读的是
//       `s_fbm`/`s_sdmp`/`s_pm`/`s_gt`/`s_bd`/`avpool`);插入与粗平面 `fbase` 逐字不动。
//   (2) ASCR 里把"整面拷贝"换成**增量 OR**:scratch 每群先零化一次,之后每推进一个 b1 就把
//       [pend(applied), pend(b1)) 这段顺序流 OR 进去;`mx`/`nwd` 扫描整段删掉(不再需要)。
//   ⇒ 省掉 0.559 GB 的 DRAM 拷贝与 0.559 GB 的 NT 写,代价 = 每群 N 次 L2 内随机位写
//     (判题机实测 inc = 20.18e6 拍 / 2.1e6 点 = 9.6 拍/点,n=3e5)。
//
// 正确性:scratch 逐位等于原平面(同一批点、同一顺序 OR);查询读的 [0,R_i) ⊆ [0,wlen) 且
//   scratch 覆盖整个位空间(AS_WORDS*64 = 1,310,720 ≥ N)。`k>8`(无 HOIST 顺序流)或
//   `N > AS_WORDS*64` 或分配失败 ⇒ `g_hoist=0`/条件不成立 ⇒ 两条路径**原文照走**(含落盘与拷贝)。
//   已核:两 seed ck 7842486998 / 7782794688 逐位同;38 格位闸 CAND-DIFF=0;判题机探针 checksum 同。

// [a13af_] ★ 本发 = **池构建 gather 的"取数摊平"(HOIST):42e6 次随机取数 → 7e6 次** ★
//
// 现状(父件 #116657 的池构建内环):
//     for b=1..Q:  pend = FPOS(d,b);
//        while (ptr < pend) { q = s_nid[od[ptr]]; cur[q>>6] |= 1ull << (q&63); ptr++; }   // 随机取数 + 随机 RMW
//        cur[0..wlen) → pool 桶 b   (NT 快照拷贝)
//   42 个 (ds,d) 对 × N 点 = **41,826,912 次随机取数**(4MB 数组)+ 等量随机 RMW(125KB 累加器)。
//
// 本发**不插入/不删改任何一次插入**,只把"取数"这一半**提前摊平**:
//   (1) 点主序秩记录:`s_pos[d][i] = rank_d(i)` 已由逐维计数排序产出 ⇒ 一次宽度转换写出
//       `s_hr[p*8+d]`(顺序读 + 跨步写);
//   (2) 抬升趟:对每个 walk 维 d 扫一趟 dim-d 序,`s_hr[od_d[j]*8]` **一条 64B 行给出该点全部 k 个 rank**
//       ⇒ 顺序写出 k-1 条顺序流 `s_ho[(d*k+ds)*N + j] = rank_ds(od_d[j])`(1 次随机取数供 k-1 个 ds 共用);
//   (3) 池走查的取数由"随机行取"变成 `ho[ptr]` 的**顺序读**(硬件预取器完全跟得上),
//       环里只剩"随机 RMW 进 125KB 累加器"一条随机通路;`s_nid` 逆置换的 7 次构建(7e6 次随机写)整段省掉。
//   ★ 随机取数 42e6 → 7e6(= 每个 (点, walk 维) 一次,信息论下界)· 输出**逐位不变**。
//
//   ★ 为什么这是**数据流/表示**改造而不是调度(本席的判据):
//   在档相位仪器([14])把池构建切成 gather 61.6% / copy 38.4%,而本席在本机把 gather **再切一刀**:
//   把随机 RMW 换成寄存器汇(`A13AF_NORMW`)后,本机纯 gather 从 0.508e9 拍只降到 0.436e9 拍
//   ⇒ **gather 的 ~85% 是那条随机取数**;把取数换成顺序流(`A13AF_NOCOPY|HOIST`)后纯 gather = 0.095e9 拍
//   ⇒ 取数摊平把"环内随机通路"从 2 条降到 1 条,且同一条随机行取现在供 k-1 个 (ds) 共用。
//   桶界 `FPOS(d,b)`、粗胞梯 `bnd[cnext]`、`cur[q>>6] |= 1ull << (q&63)` 的次序、NT 快照拷贝一律逐字保留。
//
//   正确性(逐位等价,已核):环里唯一改的变量是 q 的**来源**(同值:`s_ho[(d*k+ds)*N+j] ≡ s_nid[od_d[j]]`)
//   ⇒ 输出与父件逐位相同(两 seed ck = 7842486998 / 7782794688 ✓;38 格位闸 CAND-DIFF=0 见提交记录)。
//   `k>8`(秩记录 8 槽不够)或分配失败 ⇒ `g_hoist=0` 走原路径(k=9/10 的格子在闸门里已逐位对拍 ✓)。

// [a13ad_] ★ 本发 = **单变量 `g_Qc` 14 → 28**(粗网格过滤器的网格数;其余一切逐字节不动)★
//
// 依据(全部为本席在 n=1e6 k=7 Q=1620 判题几何上的**确定性计数**,非计时):
//   pass B 的活滤块(`KK==7 && g_use_filt`)**在 5,980,610 / 6,000,000 次迭代里被进入**
//   (bLohi=5,980,610 · bFilt=5,980,610 · bFW=34,737,398 词 · **bCand=35,372,649 个候选**)。
//   每个候选要付 **2 次随机取数**(`s_ord[d][j]` 4 MB + `s_pm[·]` 32 MB,后者依赖前者 ⇒ 链长 2),
//   再加每迭代 **6 条 fbm 随机行**(74 MB 位图,未预取)⇒ 全相 ≈ 1.07e8 次随机 miss。
//   在档 ㉓ 的判据:**"这条路径的成本 = 随机 miss 的条数 × 每次 miss 的代价,条数由候选数决定、
//   与数据放在哪无关"**(1012 同族的 `s_sdmp` 换布局只买到 −0.098%)。
//   ⇒ **候选数是这条路径上唯一的货币。** 本席扫 `g_Qc` 并逐点数候选:
//        Qc=3 → 145.16e6 · 7 → 53.74e6 · **14(现役)→ 35.37e6** · 28 → 28.25e6 · 56 → 25.13e6 · 112 → 23.68e6
//   ⇒ **14 → 28 削掉 7.12e6 个候选(−20.1%)= 1.42e7 次随机取数**(按档内 150M 探测/s:
//      ≈ −95 ms;28 → 56 再削 3.1e6 候选 ≈ −42 ms,边际已薄)。
//
//   为什么旧定价不算数(`§2.19.1240`"刀的币值随基座换代归零"):在档唯一一次判题机 `g_Qc` 扫描
//   (2026-09-28,`notes.md` ⑧:16 为峰 · 48 = +55.6 ms · 64 = +81.1 ms)**是在过滤器于 n=1e6
//   被整体关掉的年代**(同档 `notes.md` line 436 明写 "n=1e6 下它本来就被关掉了");
//   且那次扫描的代价项 = **独立 walk 建的粗平面**(`MF_FB3`),而现役是 **`FBMERGE`**:
//   粗平面由池走查顺带发出,`g_Qc` 抬高只多付 `k(k-1)(Qc+1)W` = 28 档 178 MB 的 NT 写
//   (本席计数:现役 9,191,616 词 = 73.5 MB)⇒ **那次扫描定价的是已不存在的建造形态,不是候选数。**
//
//   代价侧(本席计数 + 判题机实测速率):粗平面 NT 写 73.5 → 178 MB(+105 MB;
//   x13x2_ 在判题机实测 NT 拷贝 7.53 B/TSC-cyc ⇒ **+3.9 ms**);内存 748 → 834 MB(限额 2 GB);
//   `k*k*(g_Qc+1)*W*8 = 178 MB < 700 MB` ⇒ `MF_FILTAUTO` 的关闭闸**不会触发**(关键前提 ✓)。
//   ⇒ 上界 = −1.42e7 次随机取数(≈ −95 ms)− +3.9 ms ≈ **−91 ms = 缺口的 2.2×**。
//
//   正确性:滤块只做**超集剪枝**,最终仍逐候选精确比对 `_mm256_cmpgt_epi32(qT, pv)`。
//   `cell = (x_e(i)*Qc)>>csh + 1` ⇒ 该行阈值 `CBOUND(cell) = (cell<<csh)/Qc > x_e(i) ≥ gt_e(i)`
//   ⇒ 对任意 `Qc ≥ 1` 都是超集 ⇒ **输出逐位不变**(本席已在 Qc = 3/7/14/28/56/112 六档上验
//   ck 逐位 = `7842486998`;闸门 38 格见下)。
// ==============================================
// 正确性:前瞻只拉行、不改值(RMW 语义与钳位逐字不变)⇒ **输出逐位相同**。
// ================
#pragma GCC optimize/target is allowed; measured +3~6%
//     on this family.  Multi-threading is unavailable.
// [5] Judge Duck FAQ <https://duck.ac/faq> (flags, machine: i3-8100, 6 MB L3, 2x4 GB
//     DDR4-2400, memory accounted as pages written).
// [6] duck.ac user saffah_cc_v41_agg1, submission #97898 (problem 1012)
//     <https://duck.ac/submission/97898>
//     local archive: problems/1012/work/agg.cpp == problems/1013/work/w2_fringe.cpp
//     Use: copied verbatim the [W2] rewrite of the fringe coarse-cell index -- the
//     per-(query,dim,other-dim) 64-bit division `(u64)rec[e] / (N/Qc+1) + 1` replaced by
//     the multiply-shift `((u64)rec[e] * Qc) >> g_cshift`, with the boundary ladder
//     `s_bnde` moved onto the same shift (`CBOUND(c) = (c << g_cshift) / Qc`) and
//     g_cshift = ceil(log2 N) fixed before any boundary is computed.  That submission
//   (d) the [W2] fringe coarse-cell index by multiply-shift (ref [6]).  The fringe's
//       filter row lookup did one 64-bit division per (query, other-dim) -- k-1 = 7 per
//       query at k=8 -- and a 64-bit `div` is a multi-uop, non-pipelined instruction that
//       sits directly on the "value -> cell -> row address -> load" dependency chain.
//       Both the cell index and the boundary ladder now share one shift, so the filter
//       stays the same tight superset.  This is an instruction-selection change only;
//       the cell width moves by <5% for n=1e6 (31251 -> 32768), so the candidate count
//       is essentially unchanged.
// Verified by checksum equality against the unmodified engine on local random data
// (several k and several value distributions, including heavy ties) before submitting.
// ================
#pragma GCC optimize("O3,no-strict-aliasing")
#pragma GCC target("avx2,bmi,bmi2,popcnt,lzcnt,tune=skylake")
#ifndef MF_PFSC2
#define MF_PFSC2 1        // [PFSC2] scatter-RMW prefetch (1015 port)
#endif
#ifndef MF_PFSC2_D
#define MF_PFSC2_D 32
#endif
#ifndef MF_PFSC
#define MF_PFSC 1         // [PFSC] setup s_cntv[v]/s_cntv[v+1] prefetch (1015 port)
#endif
#ifndef MF_PFSC_D
#define MF_PFSC_D 32
#endif
#ifndef MF_PFHIST
#define MF_PFHIST 0       // histogram RMW prefetch
#endif
#ifndef MF_PFHIST_D
#define MF_PFHIST_D 32
#endif
#ifndef MF_PFBUILD
#define MF_PFBUILD 1      // [PFSC2-b] pool-build gather prefetch
#endif
#ifndef MF_PFBUILD_D
#define MF_PFBUILD_D (4*4)   /* [x13cl_] 基座正文锚:折叠等价拼写(值不变、codegen 逐指令不变) */
#endif
#ifndef MF_ASCRINC
#define MF_ASCRINC 1   // [INCA] 定扎车道:整面拷贝 -> 增量维护(pool build 同步不再落盘)
#endif
#ifndef A13AG_PFD
#define A13AG_PFD 0    // 增量环的 RMW 目标预取距离(0 = 关)
#endif
#ifndef MF_HOIST
#define MF_HOIST 1        // [HOIST] 池构建 gather 顺序化(随机取数 42e6 -> 7e6)
#endif
#ifndef A13AF_PHASE
#define A13AF_PHASE 0
#endif
#ifndef MF_MASKRMW
#define MF_MASKRMW 0       // 诊断:RMW 落点掩码(0 = 关)
#endif
#ifndef MF_NOSELF
#define MF_NOSELF 0        // [NOSELF] 抬升趟连自身槽一起写(无分支内环)
#endif
#ifndef MF_HRNT
#define MF_HRNT 0          // [HRNT] 秩记录用 NT 存(免 RFO;记录只在抬升趟被随机读)
#endif
#ifndef MF_HR1
#define MF_HR1 1            // [HR1] 点主序秩记录一趟向量存(旧形态 7 趟跨步部分写)
#endif
#ifndef MF_HOPF
#define MF_HOPF 0          // [HOPF] 顺序流已知未来的 RMW 目标 ⇒ 预取 cur 的将来字
#endif
#ifndef MF_HOPF_D
#define MF_HOPF_D 16
#endif
#ifndef A13AF_NORMW
#define A13AF_NORMW 0     // 诊断臂:把随机 RMW 换成寄存器汇(保留随机取数)
#endif
#ifndef A13AF_NOCOPY
#define A13AF_NOCOPY 0    // 诊断臂:跳过 NT 快照拷贝(保留 gather)
#endif
#ifndef MF_PFAV
#define MF_PFAV 0        // [PFSC-c] avpool fill prefetch
#endif
#ifndef MF_PFAV_D
#define MF_PFAV_D 32
#endif
#ifndef MF_PFOUT
#define MF_PFOUT 1       // [PFSC-d] pass B out[i] RMW prefetch
#endif
#ifndef MF_PFOUT_D
#define MF_PFOUT_D 32
#endif
#ifndef MF_QCOEF
#ifndef MF_QCOEF
#define MF_QCOEF 150
#endif
#endif
// duck.ac 1012..1016 : k-D dominance counting (k=6..10).
//
// MIN-FOLD engine, stage 1:  for every query point i pick the dimension ds(i) with the
// smallest  gt[d][i] = #{p : x_d[p] < x_d[i]}.  Number the bit space by the rank in dim ds;
#include <immintrin.h>

static inline __m256i LDU(const void *p) {
  __m256i v;
  __asm__("vmovdqu %1, %0" : "=x"(v) : "m"(*(const __m256i *)p));
  return v;
}
static inline __m256i LDA(const void *p) {   // p 必须 32 字节对齐
  __m256i v;
  __asm__("vmovdqa %1, %0" : "=x"(v) : "m"(*(const __m256i *)p));
  return v;
}
// 存数:内存是读写出操作数,这样编译器知道内存被改写,不会做 CSE / 消除。
static inline void STU(void *p, __m256i v) {
  __asm__ volatile("vmovdqu %1, %0" : "+m"(*(__m256i *)p) : "x"(v));
}
static inline void STA(void *p, __m256i v) {  // p 必须 32 字节对齐
  __asm__ volatile("vmovdqa %1, %0" : "+m"(*(__m256i *)p) : "x"(v));
}
static inline __m128i LDQ(const void *p) {
  __m128i v;
  __asm__("vmovdqu %1, %0" : "=x"(v) : "m"(*(const __m128i *)p));
  return v;
}

// ---- 宏版本(放在 #pragma GCC target(...) 之后使用)----
// 取数:内存是输入操作数,语义正确。
// 存数:`"+m"` 读写出 + volatile,语义正确。
#define LDU_M(p) ({ __m256i _v; __asm__("vmovdqu %1, %0" : "=x"(_v) : "m"(*(const __m256i *)(p))); _v; })
#define LDA_M(p) ({ __m256i _v; __asm__("vmovdqa %1, %0" : "=x"(_v) : "m"(*(const __m256i *)(p))); _v; })
#define STU_M(p, v) do { __m256i _vv = (__m256i)(v); __asm__ volatile("vmovdqu %1, %0" : "+m"(*(__m256i *)(p)) : "x"(_vv)); } while (0)
#define STA_M(p, v) do { __m256i _vv = (__m256i)(v); __asm__ volatile("vmovdqa %1, %0" : "+m"(*(__m256i *)(p)) : "x"(_vv)); } while (0)


#ifdef LOCAL_TEST
#include <cstdio>
#include <cstdlib>
#include <ctime>
#else
#ifdef __cplusplus
extern "C" void *malloc(unsigned long);
#else
void *malloc(unsigned long);
#endif
#endif

typedef unsigned u32;
typedef unsigned short u16;
typedef unsigned long long u64;

#ifndef MAXN
#define MAXN 1000005
#endif
#define MAXK 10
#define STRIDE 8

// Bucket-count coefficient: q ~ sqrt(n) * MF_QCOEF/100.  Larger q = smaller
// bucket = fewer fringe candidates, at the cost of a bigger pool + build.
// Bucket count = sqrt(n) * MF_QCOEF/100 * kscale[k].  MEASURED OPTIMA (board):
//   n=1e6  k=6..8 : 122 default is fine (1013 7.491 s, 1014 8.503 s at 122)
//   n=1e6  k=9/10 : 92  (1016 10.888 s at 92; 10.959 at 122; 11.129 at 200) -- the pool
//                        build dominates at the base size, so COARSER buckets win
//   n<=3e5 k=9/10 : 200 (1016b 291.4 ms vs 326.6 at 122; 1015a 1.444, 1016a 1.517) -- the
//                        pool is not memory-capped there, so FINER buckets shrink the fringe
// i.e. this single global default cannot be right for every (problem size, k); re-sweep it
// whenever a family member is tuned.
#ifndef MF_QCOEF
#define MF_QCOEF 150
#endif
// Transposed fringe rows: stride padded to 8 u32 (32 B) for k<=8 so every
// candidate load is one aligned, line-contained 32-byte vector load.
#ifndef MF_KSPAD
#define MF_KSPAD 1
#endif
// 1 = let the coarse-grid fringe filter auto-enable for coarse buckets (B>=400)
#ifndef MF_FILTAUTO
#define MF_FILTAUTO 1
#endif
// 1 = non-temporal stores for the (write-once, read-much-later, huge) pool build
#ifndef MF_NT
#define MF_NT 1
#endif
#ifndef MF_FR_NOSORT
#define MF_FR_NOSORT 0
#endif
#ifndef MF_FR_NOCAND
#define MF_FR_NOCAND 0
#endif
#ifndef MF_BUILD_NOPOOL
#define MF_BUILD_NOPOOL 0
#endif
#ifndef MF_FR_NOREAD
#define MF_FR_NOREAD 0
#endif
#ifndef MF_NOSDMP
#define MF_NOSDMP 0
#endif
// MF_FB2: sweep-the-e-order-once filter build with (k-1) live accumulators.
// MEASURED WORSE on the board (1016 12.580 -> 13.361 s, 1014 9.400 -> 10.287 s):
// k-1 live 125 KB accumulators exceed L2, so every RMW becomes an L3 read-modify-write
// and that costs more than the random reads it saves.  Kept only as a springboard.
#ifndef MF_FB2
#ifndef MF_FBPF
#define MF_FBPF 64
#endif
#define MF_FB2 0
#endif
#ifndef MF_FB3
#define MF_FB3 1
#endif
// Per-(dim,bucket) pooled-array lengths (MF_WTRIM must be on).
#ifndef MF_PLEN
#define MF_PLEN 1
#endif
#ifndef MF_BUDGETMB
#define MF_BUDGETMB 1900
#endif
#ifndef MF_NOFILTB
#define MF_NOFILTB 0
#endif
// Lookahead distance for prefetching the scattered per-query metadata
// (s_pm record, s_gt threshold, s_bd bucket word) in pass B / pass A.
#ifndef MF_PFB
#define MF_PFB 32
#endif
// Pass A: sort each ds group by the bucket of one non-fold dim and copy that
// dim's (dim,bucket) array prefix into a small cache-resident scratch so the
// ~137 queries sharing it do not each re-fetch ~70 KB from DRAM.  1 = on.
#ifndef MF_ASCR
#define MF_ASCR 1
#endif
// ============================================================================
// !! DO NOT ENABLE MF_WTRIM / MF_PLEN !!
// MF_WTRIM (per-group pool stride) and MF_PLEN (per-(dim,bucket) array lengths)
// each buy only ~1% on the 1012-1016 family, but BOTH COMPUTE WRONG RESULTS for
// coarse bucket grids.  Reproductions (local, LOCAL_TEST main):
//     ./with_wtrim 10 2000 11 0 3   -> 4 mismatches   (MF_WTRIM=1)
//     ./with_plen   6 2000 11 0 3   -> 24 mismatches  (MF_PLEN=1)
// The AND's reads stay inside the shortened arrays (an instrumented build reports
// zero out-of-range reads), so the defect is in the shortened arrays' *content*,
// not their length; it is NOT yet localised.  The fastest recorded board times
// (1012 6.753 s, 1016 12.386 s) came from a build with these on: that build is AC
// on all 15 judge suites, but it must never be shipped as the default, because it
// is known-wrong on data we have not seen.  Fixing this properly is worth ~1%.
// ============================================================================
// Trim the pool stride per ds group to the largest R actually read (-13% pool).
#ifndef MF_WTRIM
#define MF_WTRIM 0
#endif
#define AS_WORDS 20480
#ifndef MF_PFA
#define MF_PFA 1
#endif
#ifndef MF_FR_NOMETA
#define MF_FR_NOMETA 0
#endif
#ifndef MAXQ
#define MAXQ 4096
#endif

static u32 s_ord[MAXK][MAXN];
static u32 s_gt[MAXK][MAXN];
static u32 s_pm[(size_t)MAXN * STRIDE] __attribute__((aligned(64)));
static u16 s_bd[(size_t)MAXN * MAXK];
static u16 s_bdT[(size_t)MAXK * MAXN];
static u32 g_SBK = MAXK;
static u32 s_g[MAXN];
static u32 s_qord[MAXN], s_tmp[MAXN], s_cntv[MAXN];
static u16 s_bkt[MAXN];
static u32 s_nid[MAXN];
static u32 s_pos[MAXK][MAXN];
static u32 s_posp[(size_t)MAXK * MAXN];   // s_posp[p*MAXK+d] = rank of p in dim d
static u64 s_facc[(size_t)(MAXK - 1) * (((MAXN) + 63) / 64 + 16)];
static u32 s_bnde[(size_t)MAXK * (MAXQ + 2)];   // per-dim coarse-cell boundary positions
static u64 *s_fbm = 0;
static u64 s_fbuf[8192] __attribute__((aligned(64)));
static __m256i khi8v, khi8hv;
static u32 g_Qc = 28;   /* [a13ad_] 14 -> 28: coarse-cell count (see header) */
static int g_cshift = 20;   // cell(x) = (x*g_Qc)>>g_cshift ; boundary(c) = (c<<g_cshift)/g_Qc
#define CBOUND(c) ((u64)(c) << g_cshift) / g_Qc
static u32 g_fbm_W = 0;
static int g_use_filt = 0;
static int g_lexsort = 0;
static int g_nofilt = 0;
static u32 s_fpos[(MAXK + 1) * MAXQ];
static u32 s_bval[(MAXK + 1) * MAXQ];
static u64 *s_bs = 0;
static u64 s_acc[MAXN / 64 + 256] __attribute__((aligned(64)));
static int g_hoist = 0;          // [HOIST] 运行期有效性(k<=8 且两条流都分配到)
static u32 *s_hr = 0;            // [HOIST] 点主序秩记录 s_hr[p*8+d] = rank_d(p)
static u32 *s_ho = 0;            // [HOIST] 抬升后的顺序流 s_ho[(d*k+ds)*N+j]
#if A13AF_PHASE
#include <cstdio>
static u64 a13af_t_gath = 0, a13af_t_copy = 0, a13af_t_hoist = 0, a13af_t_hr = 0, a13af_t_hrall = 0, a13af_sink = 0;
static u64 a13ag_t_inc = 0;   // [INCA] 增量维护环的 rdtscp 包夹
static inline u64 a13af_rd(void) { unsigned a13af_lo, a13af_hi, a13af_aux; __asm__ __volatile__("rdtscp" : "=a"(a13af_lo), "=d"(a13af_hi), "=c"(a13af_aux)); return ((u64)a13af_hi << 32) | a13af_lo; }
struct A13AFRep { ~A13AFRep() { fprintf(stderr,
  "A13AF gath=%llu copy=%llu hoist=%llu hr=%llu hrall=%llu sink=%llu inc=%llu\n",
  (unsigned long long)a13af_t_gath, (unsigned long long)a13af_t_copy,
  (unsigned long long)a13af_t_hoist, (unsigned long long)a13af_t_hr,
  (unsigned long long)a13af_t_hrall, (unsigned long long)a13af_sink,
  (unsigned long long)a13ag_t_inc); } };
static A13AFRep a13af_rep;
#endif
static u32 *s_sdmp[MAXK];
static u64 s_ascr[AS_WORDS] __attribute__((aligned(64)));
static u32 s_maxr[(size_t)MAXK * MAXK * MAXQ];
static u32 *s_avpool = 0;
static size_t s_avcap = 0;
static u32 s_pofs[(size_t)MAXK * MAXQ];
static u32 s_plen[(size_t)MAXK * MAXQ];
static int g_plen_on = 0;

static u32 g_W, g_Q, g_B;
static u32 g_KS;   // row stride of the transposed coordinate rows (64 B for k>8)
static int g_nosort = 0;
static int g_nosdmp = 0;
static int g_skip_and = 0, g_skip_fringe = 0;

#define FPOS(d, b) s_fpos[(size_t)(d) * MAXQ + (b)]
#define BVAL(d, b) s_bval[(size_t)(d) * MAXQ + (b)]

/* [x13w_] 巨页:pool_alloc 本来就按 2 MB 对齐(THP 的天然载体)⇒ 只补一条 madvise。
   判题机/libc 若不可用则返回 -1,纯 no-op,不放任何语义。 */
#ifdef __cplusplus
extern "C" int madvise(void *, unsigned long, int);
#else
int madvise(void *, unsigned long, int);
#endif
#ifndef MADV_HUGEPAGE
#define MADV_HUGEPAGE 14
#endif
static void *pool_alloc(size_t bytes) {
  /* [x13cu-ROT] 单变量:在现役"2 MB 对齐"之上**再加 4 KB 级相移**
     pad = (rot++ & 7) << 12 ⇒ 连续 8 次分配落 8 个互异 4 KB 相位。
     只改"数组基址的物理相位",索引/数值零改动 ⇒ **逐位等价**(ck 已核同);
     pad 是 4096 的倍数 ⇒ madvise 的 4 KB 对齐前提保持 ✓ */
  static unsigned rot = 0;
  size_t pad = (size_t)(rot++ & 7u) << 12;
  void *raw = malloc(bytes + (2u << 20) + (8u << 12));
  if (!raw) return 0;
  void *p = (void *)(((size_t)raw + (2u << 20) - 1) & ~(size_t)((2u << 20) - 1));
  p = (void *)((char *)p + pad);
  madvise(p, bytes, MADV_HUGEPAGE);
  return p;
}

// Harley-Seal / Muła nibble-popcount.  The old form (32-byte store to a scratch
// array followed by four 64-bit popcnt) costs ~14 cycles per 32 bytes because
// store-forwarding + port-1 popcnt serialise; this measures 1.48x faster on a
// pure-L1 microbenchmark (work/c6d2/alucost.cpp: 19.27 -> 13.06 cyc per chunk).
// Byte accumulators saturate at 255, so flush every 31 iterations (<=8 per byte).
__attribute__((target("avx2,popcnt")))
static inline u32 hsum256(__m256i v) {
  u64 b[4];
  STU((__m256i *)b, v);
  return (u32)(b[0] + b[1] + b[2] + b[3]);
}
template<int KK>
__attribute__((target("avx2,popcnt")))
static u32 and_popcount_range(const u64 *const *bs, u32 Wr, u32 rem) {
  const __m256i lm = _mm256_set1_epi8(0x0f);
  const __m256i lk = _mm256_setr_epi8(0,1,1,2,1,2,2,3,1,2,2,3,2,3,3,4,
                                      0,1,1,2,1,2,2,3,1,2,2,3,2,3,3,4);
  const __m256i z = _mm256_setzero_si256();
  __m256i tot = z, acc0 = z, acc1 = z;
  u32 w = 0, cnt = 0, total = 0;
  #pragma GCC unroll 4
  for (; w + 8 <= Wr; w += 8) {
    if (KK > 1) _mm_prefetch((const char *)(bs[1] + w + 48), _MM_HINT_T0);
    if (KK > 2) _mm_prefetch((const char *)(bs[2] + w + 48), _MM_HINT_T0);
    if (KK > 3) _mm_prefetch((const char *)(bs[3] + w + 48), _MM_HINT_T0);
    if (KK > 4) _mm_prefetch((const char *)(bs[4] + w + 48), _MM_HINT_T0);
    if (KK > 5) _mm_prefetch((const char *)(bs[5] + w + 48), _MM_HINT_T0);
    __m256i a = LDU((const __m256i *)(bs[0] + w));
    for (int d = 1; d < KK; d++) a = _mm256_and_si256(a, LDU((const __m256i *)(bs[d] + w)));
    __m256i lo0 = _mm256_and_si256(a, lm);
    __m256i hi0 = _mm256_and_si256(_mm256_srli_epi16(a, 4), lm);
    acc0 = _mm256_add_epi8(acc0, _mm256_add_epi8(_mm256_shuffle_epi8(lk, lo0), _mm256_shuffle_epi8(lk, hi0)));
    __m256i b = LDU((const __m256i *)(bs[0] + w + 4));
    for (int d = 1; d < KK; d++) b = _mm256_and_si256(b, LDU((const __m256i *)(bs[d] + w + 4)));
    __m256i lo1 = _mm256_and_si256(b, lm);
    __m256i hi1 = _mm256_and_si256(_mm256_srli_epi16(b, 4), lm);
    acc1 = _mm256_add_epi8(acc1, _mm256_add_epi8(_mm256_shuffle_epi8(lk, lo1), _mm256_shuffle_epi8(lk, hi1)));
    if (++cnt == 31) {
      tot = _mm256_add_epi64(tot, _mm256_sad_epu8(acc0, z));
      tot = _mm256_add_epi64(tot, _mm256_sad_epu8(acc1, z));
      acc0 = acc1 = z; cnt = 0;
    }
  }
  for (; w + 4 <= Wr; w += 4) {
    __m256i a = LDU((const __m256i *)(bs[0] + w));
    for (int d = 1; d < KK; d++) a = _mm256_and_si256(a, LDU((const __m256i *)(bs[d] + w)));
    __m256i lo2 = _mm256_and_si256(a, lm);
    __m256i hi2 = _mm256_and_si256(_mm256_srli_epi16(a, 4), lm);
    acc0 = _mm256_add_epi8(acc0, _mm256_add_epi8(_mm256_shuffle_epi8(lk, lo2), _mm256_shuffle_epi8(lk, hi2)));
  }
  tot = _mm256_add_epi64(tot, _mm256_sad_epu8(acc0, z));
  tot = _mm256_add_epi64(tot, _mm256_sad_epu8(acc1, z));
  total = hsum256(tot);
  for (; w < Wr; w++) {
    u64 v = bs[0][w];
    for (int d = 1; d < KK; d++) v &= bs[d][w];
    total += (u32)__builtin_popcountll(v);
  }
  if (rem) {
    u64 v = bs[0][Wr];
    for (int d = 1; d < KK; d++) v &= bs[d][Wr];
    total += (u32)__builtin_popcountll(v & ((1ull << rem) - 1));
  }
  return total;
}

template<int KK>
__attribute__((target("avx2,popcnt")))
static void query_group_A(u32 N, u32 *out, u32 ds, u32 q0, u32 q1, u32 Q, u32 W) {

  const int k = KK;
  const int K1 = KK - 1;
#if MF_ASCR
    // ---- pass A locality: order the group by the bucket in one non-fold dim so that the
    //      ~Q-run of queries needing array (e1,b) is processed together, and pin that
    //      array's prefix in a small scratch (the other k-2 dims still stream from DRAM
    //      and evict a 70 KB hot array within one query, which is why merely sorting
    const u32 d1 = (ds == 0) ? 1u : 0u;
    const u32 e1 = (d1 < ds) ? d1 : d1 - 1;
    {
      u32 *cntv = s_cntv;
      for (u32 v = 0; v <= Q; v++) cntv[v] = 0;
      for (u32 qi = q0; qi < q1; qi++) {
#if MF_PFB > 0
        if (qi + MF_PFB < q1) _mm_prefetch((const char *)(s_bdT + (size_t)d1 * N + s_qord[qi + MF_PFB]), _MM_HINT_T0);
#endif
        cntv[s_bdT[(size_t)d1 * N + s_qord[qi]] + 1]++;
      }
      { u32 ac = q0; for (u32 v = 0; v <= Q; v++) { u32 c = cntv[v]; cntv[v] = ac; ac += c; } }
      for (u32 qi = q0; qi < q1; qi++) { if (qi + 64 < q1) _mm_prefetch((const char *)(s_bdT + (size_t)d1 * N + s_qord[qi + 64]), _MM_HINT_T0); u32 id = s_qord[qi]; s_tmp[cntv[s_bdT[(size_t)d1 * N + id] + 1]++] = id; }
      for (u32 qi = q0; qi < q1; qi++) s_qord[qi] = s_tmp[qi];
    }
#else
    const u32 d1 = 0, e1 = 0;
#endif
    // ---- pass A: aligned AND; queries ordered lexicographically by their bucket vector so
    //      that consecutive queries share bitset arrays (keeps the working set small). ----
    if (g_lexsort) {
      u32 *cntv = s_cntv;
      for (int dd = k - 1; dd >= 0; dd--) {
        if (dd == ds) continue;
        for (u32 v = 0; v <= Q; v++) cntv[v] = 0;
        for (u32 qi = q0; qi < q1; qi++) cntv[s_bdT[(size_t)dd * N + s_qord[qi]] + 1]++;
        { u32 ac = q0; for (u32 v = 0; v <= Q; v++) { u32 c = cntv[v]; cntv[v] = ac; ac += c; } }
        for (u32 qi = q0; qi < q1; qi++) { u32 id = s_qord[qi]; s_tmp[cntv[s_bdT[(size_t)dd * N + id] + 1]++] = id; }
        for (u32 qi = q0; qi < q1; qi++) s_qord[qi] = s_tmp[qi];
      }
    }
#ifdef PF_DIST
    for (u32 qi = q0; qi < q0 + PF_DIST && qi + PF_DIST < q1; qi++) {
      u32 j = s_qord[qi + PF_DIST];
      const u16 *bdn = s_bd + (size_t)j * g_SBK;
      for (int e = 0; e < K1; e++) {
        int d = (e < ds) ? e : e + 1;
        const char *pp = (const char *)(s_bs + ((size_t)e * (Q + 1) + bdn[d]) * W);
        _mm_prefetch(pp, _MM_HINT_T0);
        _mm_prefetch(pp + 64, _MM_HINT_T0);
        _mm_prefetch(pp + 128, _MM_HINT_T0);
      }
    }
#endif
#if MF_ASCR
    {
#if MF_ASCRINC
      /* [INCA] 增量维护 s_ascr:把定扎车道 (d1) 的 fine 平面按 b1 升序**长出来**,而不是每个 run
         从 DRAM 拷一遍。delta(b) = 点集 { j ∈ [pend(prev), pend(b)) }(dim-d1 序的连续段),
         取数走 HOIST 已建好的顺序流 s_ho[(d1*k+ds)*N + j](与该平面构建所用同一条数据)。
         ⇒ 随机位写 N 次/群(scratch 160 KB 常在 L2),取代 maxR≈49 KB 的 DRAM 拷贝 × 11311 run。 */
      const u32 *a13ag_ho1 = 0;
      u32 a13ag_app = 0;
      if (g_hoist && (u64)N <= (u64)AS_WORDS * 64ull) {
        a13ag_ho1 = s_ho + ((size_t)d1 * k + ds) * N;
        for (u32 w = 0; w < AS_WORDS; w++) s_ascr[w] = 0;
      }
#endif
      u32 qi = q0;
      while (qi < q1) {
        u32 b1 = s_bdT[(size_t)d1 * N + s_qord[qi]];
        u32 qe = qi + 1;
        while (qe < q1 && s_bdT[(size_t)d1 * N + s_qord[qe]] == b1) qe++;
        const u64 *bp1;
#if MF_ASCRINC
        if (a13ag_ho1) {
          u32 pto = (b1 < Q) ? FPOS(d1, b1) : (u32)N;
          const u32 *hp = a13ag_ho1;
#if A13AF_PHASE
          u64 a13ag_ti = a13af_rd();
#endif
#if A13AG_PFD > 0
          while (a13ag_app < pto) {
            if (a13ag_app + A13AG_PFD < pto) _mm_prefetch((const char *)(s_ascr + (hp[a13ag_app + A13AG_PFD] >> 6)), _MM_HINT_T0);
            u32 q = hp[a13ag_app]; s_ascr[q >> 6] |= 1ull << (q & 63); a13ag_app++; }
#else
          while (a13ag_app < pto) { u32 q = hp[a13ag_app]; s_ascr[q >> 6] |= 1ull << (q & 63); a13ag_app++; }
#endif
#if A13AF_PHASE
          a13ag_t_inc += a13af_rd() - a13ag_ti;
#endif
          bp1 = s_ascr;
        } else
#endif
        {
        bp1 = g_plen_on ? (s_bs + s_pofs[(size_t)e1 * MAXQ + b1])
                                   : (s_bs + ((size_t)e1 * (Q + 1) + b1) * W);
        u32 mx = 1;
        for (u32 t = qi; t < qe; t++) { u32 rr = s_gt[ds][s_qord[t]]; if (rr > mx) mx = rr; }
        u32 nwd = (mx >> 6) + 1;
        if (nwd <= AS_WORDS) {
          const u64 *src = bp1;
          for (u32 w = 0; w < nwd; w++) s_ascr[w] = src[w];
          bp1 = s_ascr;
        }
        }
        for (u32 qq = qi; qq < qe; qq++) {
          u32 i = s_qord[qq];
          const u16 *bd = s_bd + (size_t)i * g_SBK;
#if MF_PFA > 0
          {
            u32 qj = qq + MF_PFA;
            if (qj < qe) {
              u32 j = s_qord[qj];
              _mm_prefetch((const char *)(s_bd + (size_t)j * g_SBK), _MM_HINT_T0);
              _mm_prefetch((const char *)(s_gt[ds] + j), _MM_HINT_T0);
            }
          }
#endif
          const u32 R = s_gt[ds][i];
          u32 cnt = 0;
          if (R && !g_skip_and) {
            const u64 *bp[MAXK];
            for (int e = 0; e < K1; e++) {
              if (e == (int)e1) bp[e] = bp1;
              else { int d = (e < ds) ? e : e + 1;
                     bp[e] = g_plen_on ? (s_bs + s_pofs[(size_t)e * MAXQ + bd[d]])
                                       : (s_bs + ((size_t)e * (Q + 1) + bd[d]) * W); }
            }
            cnt = and_popcount_range<KK - 1>(bp, R >> 6, R & 63u);
          }
          out[i] = cnt;
        }
        qi = qe;
      }
    }
#else
    for (u32 qi = q0; qi < q1; qi++) {
      u32 i = s_qord[qi];
      const u16 *bd = s_bd + (size_t)i * g_SBK;
#if MF_PFA > 0
      {
        u32 qj = qi + MF_PFA;
        if (qj < q1) {
          u32 j = s_qord[qj];
          _mm_prefetch((const char *)(s_bd + (size_t)j * g_SBK), _MM_HINT_T0);
          _mm_prefetch((const char *)(s_gt[ds] + j), _MM_HINT_T0);
        }
      }
#endif
#ifdef PF_DIST
      if (qi + PF_DIST < q1) {
        u32 j = s_qord[qi + PF_DIST];
        const u16 *bdn = s_bd + (size_t)j * g_SBK;
        for (int e = 0; e < K1; e++) {
          int d = (e < ds) ? e : e + 1;
          const char *pp = (const char *)(s_bs + ((size_t)e * (Q + 1) + bdn[d]) * W);
          _mm_prefetch(pp, _MM_HINT_T0);
          _mm_prefetch(pp + 64, _MM_HINT_T0);
          _mm_prefetch(pp + 128, _MM_HINT_T0);
        }
      }
#endif
      const u32 R = s_gt[ds][i];
      u32 cnt = 0;
      if (R && !g_skip_and) {
          const u64 *bp[MAXK];
        for (int e = 0; e < K1; e++) {
          int d = (e < ds) ? e : e + 1;
          bp[e] = g_plen_on ? (s_bs + s_pofs[(size_t)e * MAXQ + bd[d]])
                            : (s_bs + ((size_t)e * (Q + 1) + bd[d]) * W);
#ifdef DBG_PLEN
          if (g_plen_on) {
            u32 need = (R >> 6) + ((R & 63) ? 1 : 0);
            if (need > s_plen[(size_t)e * MAXQ + bd[d]])
              printf("DBG ds=%d e=%d d=%d b=%u R=%u need=%u len=%u\n", ds, e, d, bd[d], R, need, s_plen[(size_t)e * MAXQ + bd[d]]);
          }
#endif
        }
        cnt = and_popcount_range<KK - 1>(bp, R >> 6, R & 63u);
      }
      out[i] = cnt;
    }
#endif
}

template<int KK>
__attribute__((target("avx2,popcnt")))
static void query_group_B(u32 N, u32 *out, u32 ds, u32 q0, u32 q1, u32 Q, u32 W) {
  const int k = KK;
#if MF_ASCR
  const u32 d1 = (ds == 0) ? 1u : 0u;
#else
  const u32 d1 = 0;
#endif
    u32 *const avpool = s_avpool;
    // ---- pass B: fringe, dimension-major.  Ordering the group's queries by their bucket in
    for (int d = 0; d < k; d++) {
      if (d == ds) continue;
#if !MF_FR_NOSORT
#if MF_ASCR
      // [NODUP] identity sort: the ASCR preamble already stably sorted this group's
      // queries by their bucket in d1, and d1 is the first d reached below.
      if (g_lexsort || d != (int)d1) {
#endif
      {
        u32 *cntv = s_cntv;
        for (u32 v = 0; v <= Q; v++) cntv[v] = 0;
        for (u32 qi = q0; qi < q1; qi++) {
#if MF_PFB > 0
          if (qi + MF_PFB < q1) _mm_prefetch((const char *)(s_bdT + (size_t)d * N + s_qord[qi + MF_PFB]), _MM_HINT_T0);
#endif
          u32 bb = s_bdT[(size_t)d * N + s_qord[qi]];
          s_bkt[qi] = bb;
          cntv[bb + 1]++;
        }
        { u32 ac = q0; for (u32 v = 0; v <= Q; v++) { u32 c = cntv[v]; cntv[v] = ac; ac += c; } }
        for (u32 qi = q0; qi < q1; qi++) {
          u32 id = s_qord[qi];
          s_tmp[cntv[s_bkt[qi] + 1]++] = id;
        }
        for (u32 qi = q0; qi < q1; qi++) s_qord[qi] = s_tmp[qi];
      }
#if MF_ASCR
      }
#endif
#endif
      u32 exmask = 0;
      for (int e = 0; e < d; e++) if (e != ds) exmask |= 1u << e;
      // "don't care" lanes are forced to a maximum-unsigned value, so one unsigned max test
      // per candidate decides domination *and* the exclusion rule together.
      u32 hiA[8], hiAh[8];
      for (int e = 0; e < 8; e++) hiA[e] = ((exmask >> e) & 1u) ? 0u : 0x7FFFFFFFu;
      for (int e = 8; e < 16; e++) hiAh[e - 8] = ((exmask >> e) & 1u) ? 0u : 0x7FFFFFFFu;
      const __m256i notexlo = LDU((const __m256i *)hiA);
      const __m256i notexloh = LDU((const __m256i *)hiAh);
      const __m256i all8 = _mm256_set1_epi32(-1);
      const u32 *const odd = s_ord[d];
      const u32 *const sdmpd = s_sdmp[d];
      for (u32 qi = q0; qi < q1; qi++) {
        u32 i = s_qord[qi];
#if MF_PFB > 0
        {
          u32 qj = qi + MF_PFB;
          if (qj < q1) {
            u32 j = s_qord[qj];
            _mm_prefetch((const char *)(s_pm + (size_t)j * STRIDE), _MM_HINT_T0);
            _mm_prefetch((const char *)(s_gt[d] + j), _MM_HINT_T0);
            _mm_prefetch((const char *)(s_bd + (size_t)j * g_SBK + d), _MM_HINT_T0);
            _mm_prefetch((const char *)(avpool + (size_t)j * STRIDE), _MM_HINT_T0);
          }
        }
#endif
#if MF_PFOUT > 0
        /* [PFSC-d] out[i] += add: one random RMW per (query,d) into the 4 MB out[] array
           (6e6 total), unprefetched.  i comes from the sequential s_qord[]. */
        { u32 qj = qi + MF_PFOUT_D; if (qj < q1) _mm_prefetch((const char *)(out + s_qord[qj]), _MM_HINT_T0); }
#endif
        const u16 *bd = s_bd + (size_t)i * g_SBK;
        const u32 lo = FPOS(d, bd[d]);
        const u32 hi = s_gt[d][i];
        if (lo >= hi) continue;
        const u32 *rec = s_pm + (size_t)i * STRIDE;
        const u32 *Avp = avpool + (size_t)i * STRIDE;
        __m256i qr = _mm256_or_si256(LDA((const __m256i *)rec), khi8v);
        __m256i Avm = _mm256_or_si256(LDU((const __m256i *)Avp), notexlo);
        __m256i qT = _mm256_min_epi32(qr, Avm);
        __m256i qTh;
        if (k > 8) {
          __m256i qrh = _mm256_or_si256(LDU((const __m256i *)(rec + 8)), khi8hv);
          __m256i Avmh = _mm256_or_si256(LDU((const __m256i *)(Avp + 8)), notexloh);
          qTh = _mm256_min_epi32(qrh, Avmh);
        } else {
          qTh = all8;
        }
        u32 add = 0;
        const u32 *c0 = sdmpd ? (sdmpd + (size_t)lo * g_KS) : 0;
        if (g_use_filt && KK == 7) {
          const u32 w0 = lo >> 6;
          const u32 nw = ((hi - 1) >> 6) - w0 + 1;
          const u32 csh = (u32)g_cshift, cQc = (u32)g_Qc;
          const u32 FW = g_fbm_W;
          const u64 *rows[6];
          int nr = 0;
          for (int e = 0; e < 7; e++) {
            if (e == d) continue;
            u32 cell = (u32)(((u64)rec[e] * cQc) >> csh) + 1;
            if (cell > cQc) cell = cQc;
            rows[nr++] = s_fbm + ((size_t)(d * 7 + e) * (g_Qc + 1) + cell) * FW + w0;
          }
          const u64 *r0=rows[0], *r1=rows[1], *r2=rows[2], *r3=rows[3];
          const u64 *r4=rows[4], *r5=rows[5];
          const u32 remh = hi & 63u;
          {   // first word: low mask only
            u64 vv = r0[0] & r1[0] & r2[0] & r3[0] & r4[0] & r5[0];
            vv &= ~0ull << (lo & 63);
            if (nw == 1u && remh) vv &= (1ull << remh) - 1;
#if MF_FR_NOCAND
            vv = 0;
#endif
            while (vv) {
              int t = __builtin_ctzll(vv);
              vv &= vv - 1;
              u32 j = (w0 << 6) + (u32)t;
              const u32 *cp = c0 ? (c0 + (size_t)(j - lo) * g_KS) : (s_pm + (size_t)s_ord[d][j] * STRIDE);
              __m256i pv = LDU((const __m256i *)cp);
              add += (u32)_mm256_testc_si256(_mm256_cmpgt_epi32(qT, pv), all8);
            }
          }
          for (u32 w = 1; w + 1 < nw; w++) {   // middle: no mask work at all
            u64 vv = r0[w] & r1[w] & r2[w] & r3[w] & r4[w] & r5[w];
#if MF_FR_NOCAND
            vv = 0;
#endif
            while (vv) {
              int t = __builtin_ctzll(vv);
              vv &= vv - 1;
              u32 j = ((w0 + w) << 6) + (u32)t;
              const u32 *cp = c0 ? (c0 + (size_t)(j - lo) * g_KS) : (s_pm + (size_t)s_ord[d][j] * STRIDE);
              __m256i pv = LDU((const __m256i *)cp);
              add += (u32)_mm256_testc_si256(_mm256_cmpgt_epi32(qT, pv), all8);
            }
          }
          if (nw > 1u) {                       // last word: high mask only
            u32 w = nw - 1u;
            u64 vv = r0[w] & r1[w] & r2[w] & r3[w] & r4[w] & r5[w];
            if (remh) vv &= (1ull << remh) - 1;
#if MF_FR_NOCAND
            vv = 0;
#endif
            while (vv) {
              int t = __builtin_ctzll(vv);
              vv &= vv - 1;
              u32 j = ((w0 + w) << 6) + (u32)t;
              const u32 *cp = c0 ? (c0 + (size_t)(j - lo) * g_KS) : (s_pm + (size_t)s_ord[d][j] * STRIDE);
              __m256i pv = LDU((const __m256i *)cp);
              add += (u32)_mm256_testc_si256(_mm256_cmpgt_epi32(qT, pv), all8);
            }
          }
          out[i] += add;
          continue;
        }
        if (g_use_filt) {
          const u32 w0 = lo >> 6;
          const u32 nw = ((hi - 1) >> 6) - w0 + 1;
          u64 *fb = s_fbuf;
          const u32 csh = (u32)g_cshift, cQc = (u32)g_Qc;
          const u32 FW = g_fbm_W;
          int first = 1;
#if MF_FR_NOREAD
          first = 0;
          for (u32 w = 0; w < nw; w++) fb[w] = 0;
#endif
          for (int e = 0; e < k; e++) {
            if (e == d) continue;
            u32 c = (u32)(((u64)rec[e] * cQc) >> csh) + 1;
            if (c > cQc) c = cQc;
            const u64 *row = s_fbm + ((size_t)(d * k + e) * (g_Qc + 1) + c) * FW + w0;
            if (first) {
              u32 w = 0;
              for (; w + 4 <= nw; w += 4) STU(fb + w, LDU(row + w));
              for (; w < nw; w++) fb[w] = row[w];
              first = 0;
            } else {
              u32 w = 0;
              for (; w + 4 <= nw; w += 4) STU(fb + w, _mm256_and_si256(LDU(fb + w), LDU(row + w)));
              for (; w < nw; w++) fb[w] &= row[w];
            }
          }
          fb[0] &= ~0ull << (lo & 63);
          u32 r2 = hi & 63u;
          if (r2) fb[nw - 1] &= (1ull << r2) - 1;
          for (u32 w = 0; w < nw; w++) {
            u64 vv = fb[w];
#if MF_FR_NOCAND
            vv = 0;
#endif
            while (vv) {
              int t = __builtin_ctzll(vv);
              vv &= vv - 1;
              u32 j = ((w0 + w) << 6) + (u32)t;
              const u32 *cp = c0 ? (c0 + (size_t)(j - lo) * g_KS) : (s_pm + (size_t)s_ord[d][j] * STRIDE);
              __m256i pv = LDU((const __m256i *)cp);
              u32 ok = (u32)_mm256_testc_si256(_mm256_cmpgt_epi32(qT, pv), all8);
              if (k > 8) {
                __m256i pvh = LDU((const __m256i *)(cp + 8));
                ok &= (u32)_mm256_testc_si256(_mm256_cmpgt_epi32(qTh, pvh), all8);
              }
              add += ok;
            }
          }
          out[i] += add;
          continue;
        }
        if (c0) {
          if (k <= 8) {
            const u32 *c = c0;
            u32 a0 = 0, a1 = 0, a2 = 0, a3 = 0;
            u32 j = lo;
            for (; j + 4 <= hi; j += 4) {
              __m256i p0 = LDU((const __m256i *)(c));
              __m256i p1 = LDU((const __m256i *)(c + g_KS));
              __m256i p2 = LDU((const __m256i *)(c + 2 * g_KS));
              __m256i p3 = LDU((const __m256i *)(c + 3 * g_KS));
              a0 += (u32)_mm256_testc_si256(_mm256_cmpgt_epi32(qT, p0), all8);
              a1 += (u32)_mm256_testc_si256(_mm256_cmpgt_epi32(qT, p1), all8);
              a2 += (u32)_mm256_testc_si256(_mm256_cmpgt_epi32(qT, p2), all8);
              a3 += (u32)_mm256_testc_si256(_mm256_cmpgt_epi32(qT, p3), all8);
              c += 4 * g_KS;
            }
            for (; j < hi; j++, c += g_KS) {
              __m256i pv = LDU((const __m256i *)c);
              a0 += (u32)_mm256_testc_si256(_mm256_cmpgt_epi32(qT, pv), all8);
            }
            add = a0 + a1 + a2 + a3;
          } else {
            const u32 *c = c0;
            for (u32 j = lo; j < hi; j++, c += g_KS) {
              __m256i pv = LDU((const __m256i *)c);
              __m256i pvh = LDU((const __m256i *)(c + 8));
              u32 ok = (u32)_mm256_testc_si256(_mm256_cmpgt_epi32(qT, pv), all8) &
                       (u32)_mm256_testc_si256(_mm256_cmpgt_epi32(qTh, pvh), all8);
              add += ok;
            }
          }
        } else {
          for (u32 j = lo; j < hi; j++) {
            const u32 *c = s_pm + (size_t)odd[j] * STRIDE;
            __m256i pv = LDA((const __m256i *)c);
            add += (u32)_mm256_testc_si256(_mm256_cmpgt_epi32(qT, pv), all8);
          }
        }
        out[i] += add;
      }
    }

}


__attribute__((target("avx2")))
static void solve_mf(u32 N, const unsigned **x, u32 *out, int k) {
  g_SBK = (k <= 8) ? 8u : (u32)MAXK;
  // W padded to 8 words: every (dim,bucket) array then starts on a 64-byte
  // boundary and every 32-byte vector load inside a stream is line-contained.
  const u32 W = ((N + 63) / 64 + 7u) & ~7u;
  g_W = W;
#ifdef MF_SKIP_AND
  g_skip_and = 1;
#endif
#ifdef MF_FILT
  g_use_filt = 1;
#endif
#ifdef MF_NOFILT
  g_use_filt = 0;
#endif
#ifdef MF_Q
  g_Q = MF_Q; g_B = (u32)((N + (MF_Q) - 1) / (MF_Q));
#endif
#ifdef MF_FILT
  g_use_filt = 1;
#endif
#ifdef MF_NOFILT
  g_use_filt = 0;
#endif
#ifdef MF_SKIP_FRINGE
  g_skip_fringe = 1;
#endif
  if (!g_Q) {
    // bucket size ~ 0.82*sqrt(n) balances the pool build (proportional to n^2/B) against the
    // fringe scan (proportional to n*B);  the pool itself is capped by the memory budget.
    u64 r = (u64)N, s2 = 0;
    while (s2 * s2 < r) s2++;                       // ceil(sqrt(n)) without libm
    // Reserve: the .bss tables, the coarse-filter bitmaps and (for k>=9, where the
    // random s_pm fallback is fatal) the transposed fringe rows.
    u64 overhead = (u64)(5 * k + 21) * N * 4;
    overhead += (u64)k * k * 4 * N;
    overhead += (u64)N * ((k <= 8) ? 8u : (u32)k) * 4 * k;
    u64 budget = ((u64)MF_BUDGETMB << 20);
    if (budget > overhead + ((u64)64 << 20)) budget -= overhead; else budget = (u64)64 << 20;
    // MF_PLEN sizes each pooled array to the largest R ever read from it, so the
    // pool really used is only ~fill of (k-1)*(Q+1)*W words; charge that instead.
#if MF_PLEN
    static const u32 kfill[7] = {72, 67, 62, 58, 54, 50, 46};   // per-array fill, k=4..10
    u64 perfill = ((u64)(k - 1) * W * 8) * kfill[k - 4] / 100;
#else
    u64 perfill = (u64)(k - 1) * W * 8;
#endif
    u64 Qmem = budget / (perfill ? perfill : 1);
    if (Qmem < 8) Qmem = 8;
    static const u32 kscale[7] = {82, 91, 100, 108, 115, 122, 129};  // sqrt(k/6) in percent, k=4..10
    u32 q = (u32)((s2 * MF_QCOEF + 99) / 100);
    q = (u32)(((u64)q * kscale[k - 4] + 99) / 100);
    if ((u64)q > Qmem) q = (u32)Qmem;
    if (q > N) q = N;
    g_Q = q;
    g_B = (N + q - 1) / q;
  }
  const u32 Q = g_Q, B = g_B;
  const u64 bmag = ((1ull << 40) + B - 1) / B;
#if MF_PLEN
  g_plen_on = 1;   // independent of MF_WTRIM: lengths only, stride stays W
#endif
#if MF_WTRIM
  u32 Wpool = W;   // MF_WTRIM is KNOWN-BROKEN, see the warning above
#else
  const u32 Wpool = W;
#endif
#if MF_FILTAUTO
  if (!g_use_filt && B >= 400) g_use_filt = 1;   // coarse grid: the filter beats the scan
#endif
  if (g_use_filt && (u64)k * k * (g_Qc + 1) * W * 8 > ((u64)700 << 20)) g_use_filt = 0;
#if !MF_FILTAUTO
  g_use_filt = 0;
#endif
  // [W2] the coarse-cell ladder must be fixed up-front: s_bnde (boundaries in
  // value space) and the per-query cell index must use the SAME shift, otherwise
  // the filter stops being a tight superset (measured: 7.5x more candidates).
  if (g_use_filt) { int sh = 0; while ((sh < 32) && (((u64)1 << sh) < (u64)N)) sh++;
                    g_cshift = sh; }
  {
    int lo8[8], hi8[8];
    for (int e = 0; e < 8; e++) lo8[e] = (e < k) ? 0 : 0x7FFFFFFF;
    for (int e = 0; e < 8; e++) hi8[e] = (8 + e < k) ? 0 : 0x7FFFFFFF;
    khi8v = LDU((const __m256i *)lo8);
    khi8hv = LDU((const __m256i *)hi8);
  }
  const int K1 = k - 1;
  // ---------- per-dimension orders, ranks, tie info ----------
  for (int d = 0; d < k; d++) {
    const unsigned *xd = x[d];
    u32 *od = s_ord[d];
    // Counting sort + rank record in sequential passes: start[v] = #{q : x_d[q] < v}
    // is exactly gt[] of every point with x_d == v, and end[v]-1 = start[v+1]-1 is the
    // last rank of that tie group, so bd[] follows from start[v+1] too.  Both are value
    // lookups, so the per-point work is a sequential sweep of xd[] instead of the
    // previous random xd[od[j]] read plus random gt[]/bd[] writes.
    for (u32 i = 0; i <= N; i++) s_cntv[i] = 0;
    for (u32 i = 0; i < N; i++) {
#if MF_PFHIST > 0
      if (i + MF_PFHIST_D < N) { u32 vn = xd[i + MF_PFHIST_D]; if (vn >= N) vn = 0;
        _mm_prefetch((const char *)(s_cntv + vn), _MM_HINT_T0); }
#endif
      u32 v = xd[i]; s_cntv[v < N ? v : 0]++; }
    { u32 s = 0; for (u32 i = 0; i <= N; i++) { u32 c = s_cntv[i]; s_cntv[i] = s; s_tmp[i] = s; s += c; } }
    { u32 *pp = s_pos[d];
      for (u32 i = 0; i < N; i++) {
#if MF_PFSC2 > 0
        /* [PFSC2] 1015 recipe (ported): the scatter RMW `s_tmp[v]++` is a random line touch whose
           value q is immediately the address of another random store (`od[q] = i`).  xd[] is
           sequential, so the line for a future iteration is known 32 points ahead. */
        /* [PFW-SC2] §2.19.545 ②轴(本档唯一仍活着的站点):该预取的目标行正是本环的 **RMW 目标**
           (`u32 q = s_tmp[v]++` —— 每点一次随机增量、数组 4 MB,且其返回值 q 立刻又是一次随机存
           `od[q] = i` 的地址)⇒ 读意图 T0 只请求 S,真正的 `++` 仍要一次 RFO。
           换成写意图 `prefetchw`(独占获取)⇒ 消掉那次 RFO 停等。只改指令形态、地址逐字不变。 */
        if (i + MF_PFSC2_D < N) { u32 vn = xd[i + MF_PFSC2_D]; if (vn >= N) vn = 0;
          __asm__ __volatile__("prefetchw %0" :: "m"(*(const char *)(s_tmp + vn))); }
#endif
        u32 v = xd[i]; if (v >= N) v = 0; u32 q = s_tmp[v]++; od[q] = i; pp[i] = q; } }
    { u32 *gt = s_gt[d];
      for (u32 i = 0; i < N; i++) {
#if MF_PFSC > 0
        /* [PFSC] 1015 recipe (ported): setup's random row fetches s_cntv[v] / s_cntv[v+1]
           (a 4 MB row, indexed by the sequential xd[]), 32 points ahead. */
        if (i + MF_PFSC_D < N) { u32 vn = xd[i + MF_PFSC_D]; if (vn >= N) vn = 0;
          _mm_prefetch((const char *)(s_cntv + vn + 1), _MM_HINT_T0); }
#endif
        u32 v = xd[i]; if (v >= N) v = 0;
        gt[i] = s_cntv[v];
        u32 num = s_cntv[v + 1] - 1u; if (num > N) num = N;
        u32 bb = (u32)(((u64)num * bmag) >> 40);
        u16 bucket = (u16)(bb > Q - 1 ? Q - 1 : bb);
        s_bd[(size_t)i * g_SBK + d] = bucket;
        s_bdT[(size_t)d * N + i] = bucket;
      } }
    BVAL(d, 0) = 0; FPOS(d, 0) = 0;
    for (u32 b = 1; b < Q; b++) {
      u32 pos = b * B < N ? b * B : N - 1;
      u32 v = xd[od[pos]];
      BVAL(d, b) = v;
      u32 gs = pos;
      while (gs > 0 && xd[od[gs - 1]] == v) gs--;
      FPOS(d, b) = gs;
    }
    BVAL(d, Q) = 0xFFFFFFFFu; FPOS(d, Q) = 0;
  }
  for (u32 i = 0; i < N; i++) {
    u32 *p = s_pm + (size_t)i * STRIDE;
    for (int d = 0; d < k; d++) p[d] = x[d][i];
    for (int d = k; d < STRIDE; d++) p[d] = 0;
  }
  // ---------- coarse superset bitmaps over every dim order (fringe filter) ----------
#if MF_NOFILTB
  g_use_filt = 0;
#endif
#if MF_FB3
  if (g_use_filt) {
    u32 Qc = g_Qc;
    for (int e = 0; e < k; e++) {
      const unsigned *xe = x[e];
      const u32 *pe = s_ord[e];
      u32 ptr = 0;
      u32 *bn = s_bnde + (size_t)e * (MAXQ + 2);
      for (u32 c = 0; c <= Qc; c++) {
        u64 v = CBOUND(c);
        if (v > 0xFFFFFFFFull) v = 0xFFFFFFFFull;
        while (v && ptr < N && (u64)xe[pe[ptr]] < v) ptr++;
        bn[c] = ptr;
      }
    }
  }
#endif
  if (g_use_filt) {
    u32 Qc = g_Qc;
    u64 wc = (u64)N / Qc + 1;
    size_t need2 = (size_t)k * k * (Qc + 1) * W * sizeof(u64);
    static u64 *fpool = 0; static size_t fcap = 0;
    if (need2 > fcap) { fpool = (u64 *)pool_alloc(need2 + 4096); fcap = need2; }
    if (fpool) {
      s_fbm = fpool; g_fbm_W = W;
#if MF_FB2
      // Sweep the e-order ONCE per dim e, keeping (k-1) running bitsets (one per
      // target dim d), and copy them out at each coarse-cell boundary.  The old
      // shape re-walked all N points for every one of the k(k-1) (d,e) pairs with
      // two L3-random reads each (xe[pe[ptr]] and s_pos[d][pe[ptr]]); here the
      // rank record is point-major so one 64B line yields every dim's rank, which
      // cuts the random reads by ~k and the whole build by ~2-2.5x at k>=8.
      for (int d = 0; d < k; d++)
        for (int e = 0; e < k; e++) {
          if (e == d) continue;
          u64 *b0 = s_fbm + (size_t)(d * k + e) * (Qc + 1) * W;
          for (u32 w = 0; w < W; w++) b0[w] = 0;
        }
      for (int e = 0; e < k; e++) {
        const unsigned *xe = x[e];
        const u32 *pe = s_ord[e];
        int dmap[MAXK], km1 = 0;
        for (int d = 0; d < k; d++) if (d != e) dmap[km1++] = d;
        for (int i = 0; i < km1; i++) { u64 *a = s_facc + (size_t)i * W; for (u32 w = 0; w < W; w++) a[w] = 0; }
        u32 ptr = 0;
        for (u32 c = 1; c <= Qc; c++) {
          u64 v = (u64)c * wc;
          if (v > 0xFFFFFFFFull) v = 0xFFFFFFFFull;
          while (ptr < N && (u64)xe[pe[ptr]] < v) {
            const u32 *pv = s_posp + (size_t)pe[ptr] * MAXK;
            for (int i = 0; i < km1; i++) {
              u32 q = pv[dmap[i]];
              s_facc[(size_t)i * W + (q >> 6)] |= 1ull << (q & 63);
            }
            ptr++;
          }
          for (int i = 0; i < km1; i++) {
            int d = dmap[i];
            u64 *dst = s_fbm + ((size_t)(d * k + e) * (Qc + 1) + c) * W;
            const u64 *src = s_facc + (size_t)i * W;
#if MF_NT
            { u32 w = 0;
              for (; w + 4 <= W; w += 4) _mm256_stream_si256((__m256i *)(dst + w), LDA((const __m256i *)(src + w)));
              for (; w < W; w++) dst[w] = src[w]; }
#else
            for (u32 w = 0; w < W; w++) dst[w] = src[w];
#endif
          }
        }
      }
#else
      /* [FBMERGE] the k(k-1) coarse planes are no longer built by a standalone 42e6-gather
         walk: plane (d,e,c) is exactly the pool walk's accumulator `cur` at dim-e position
         s_bnde[e][c] of the ds==d group (same bit order = rank_d, same accumulator, same
         ladder), so it is emitted from there.  Only plane 0 (the empty prefix) is
         materialised here.  `wc` is left for the disabled MF_FB3=0 path. */
      (void)wc;
      for (int d = 0; d < k; d++)
        for (int e = 0; e < k; e++) {
          if (e == d) continue;
          u64 *b0 = s_fbm + (size_t)(d * k + e) * (Qc + 1) * W;
          for (u32 w = 0; w < W; w++) b0[w] = 0;
        }
#endif
    } else { g_use_filt = 0; }
    _mm_sfence();
  }

  // ---------- fold dimension + bucket vector per query ----------
  {
    size_t needav = (size_t)N * STRIDE * 4 + 256;
    if (needav > s_avcap) {
      s_avpool = (u32 *)pool_alloc(needav);
      s_avcap = s_avpool ? needav : 0;
    }
  }
  {
    size_t mrn = (size_t)MAXK * MAXK * MAXQ;
    for (size_t z = 0; z < mrn; z++) s_maxr[z] = 0;
  }
  for (u32 i = 0; i < N; i++) {
    u32 best = 0xFFFFFFFFu; int ds = 0;
    for (int d = 0; d < k; d++) {
      u32 g = s_gt[d][i];
      if (g < best) { best = g; ds = d; }
    }
    s_g[i] = (u32)ds;
    const u16 *bdv = s_bd + (size_t)i * g_SBK;
    u32 *av = s_avpool + (size_t)i * STRIDE;
    u32 *mr = s_maxr + ((size_t)ds * MAXK) * MAXQ;
    for (int d = 0; d < k; d++) {
      if (d == ds) { av[d] = 0x7FFFFFFFu; continue; }
      u32 b = bdv[d];
      av[d] = BVAL(d, b);
      if (g_plen_on && best > mr[(size_t)d * MAXQ + b])
        mr[(size_t)d * MAXQ + b] = best;
    }
    for (int d = k; d < STRIDE; d++) av[d] = 0x7FFFFFFFu;
  }
  for (u32 i = 0; i < N; i++) s_qord[i] = i;
  // group by ds
  {
    for (u32 v = 0; v <= (u32)k; v++) s_cntv[v] = 0;
    for (u32 i = 0; i < N; i++) s_cntv[s_g[i] + 1]++;
    { u32 ac = 0; for (u32 v = 0; v <= (u32)k; v++) { u32 c = s_cntv[v]; s_cntv[v] = ac; ac += c; } }
    for (u32 i = 0; i < N; i++) { u32 id = s_qord[i]; s_tmp[s_cntv[s_g[id] + 1]++] = id; }
    for (u32 i = 0; i < N; i++) s_qord[i] = s_tmp[i];
  }
  u32 gstart[MAXK + 2];
  { u32 c = 0; for (int d = 0; d <= k; d++) { gstart[d] = c; while (c < N && (int)s_g[s_qord[c]] == d) c++; } gstart[k + 1] = c; }
  // ---------- transposed coordinate rows (sequential fringe) ----------
#if MF_NOSDMP
  g_nosdmp = 1;
#endif
  if (!g_nosdmp) {
    u32 KS = k;
#if MF_KSPAD
    if (k <= 8) KS = 8;          // 32-byte aligned candidate rows for k<=8
#endif
    size_t per = (size_t)N * KS * 4;
    size_t lim = (size_t)150 << 20;
    if (k >= 9) lim = (size_t)470 << 20;   // k=9/10 need the rows: the random s_pm fallback is fatal
    if (per * k <= lim) {
      static u32 *pool = 0; static size_t cap = 0;
      if (per * k > cap) { pool = (u32 *)pool_alloc(per * k + 256); cap = per * k; }
      if (pool) {
        g_KS = KS;
        for (int d = 0; d < k; d++) {
          s_sdmp[d] = pool + (size_t)d * N * g_KS;
          const u32 *od = s_ord[d];
          u32 *dst = s_sdmp[d];
          const u32 KSx = g_KS;
          for (u32 j = 0; j < N; j++) {
#if MF_PFB > 0
            if (j + MF_PFB < N) _mm_prefetch((const char *)(s_pm + (size_t)od[j + MF_PFB] * STRIDE), _MM_HINT_T0);
#endif
            const u32 *row = s_pm + (size_t)od[j] * STRIDE;
            u32 *dp = dst + (size_t)j * KSx;
            for (u32 e = 0; e < KSx; e++) dp[e] = (e < (u32)k) ? row[e] : 0u;
          }
        }
      }
    }
  }
  // ---------- pool ----------
  u64 poolwords = (u64)K1 * (Q + 1) * W;
#if MF_PLEN
  if (g_plen_on) {
    u64 best = 0;
    for (int dsx = 0; dsx < k; dsx++) {
      u64 tot = 0;
      for (int e = 0; e < K1; e++) {
        int d = (e < dsx) ? e : e + 1;
        const u32 *mr = s_maxr + ((size_t)dsx * MAXK + d) * MAXQ;
        for (u32 b = 0; b <= Q; b++) {
          u32 RR = mr[b];
          u32 len = ((RR >> 6) + 8u) & ~7u;
          if (len > W) len = W;
          tot += len;
        }
      }
      if (tot > best) best = tot;
    }
    if (best && best < poolwords) poolwords = best;
  }
#endif
  size_t need = (size_t)poolwords * sizeof(u64);
  static u64 *pool = 0; static size_t cap2 = 0;
  if (need > cap2) { pool = (u64 *)pool_alloc(need + 4096); cap2 = need; }
  s_bs = pool;
#if MF_HOIST
  /* [HOIST] 池构建每趟 (ds,d) 的取数 `s_nid[od[ptr]]` 是"随机 4MB 行取 + 该值当即做随机 RMW 地址"
     的两条随机通路;这里把第一半提前摊平:点主序秩记录(s_pos 已经是"点 id 序的秩表",
     只需一次宽度转换)⇒ 每个 (点, walk 维) 只做 1 次随机行取,就拿到该点**全部** k 个 rank,
     顺序写出 k-1 条顺序流。池走查此后只需顺序读 + 随机 RMW,且**桶界/平面发射次序逐字不动**。 */
  if (k <= 8 && N <= MAXN) {
    static size_t a13af_cap_hr = 0, a13af_cap_ho = 0;
    size_t need_hr = (size_t)N * 8 * sizeof(u32) + 4096;
    size_t need_ho = (size_t)k * k * N * sizeof(u32) + 4096;
    if (need_hr > a13af_cap_hr) { s_hr = (u32 *)pool_alloc(need_hr); a13af_cap_hr = s_hr ? need_hr : 0; }
    if (need_ho > a13af_cap_ho) { s_ho = (u32 *)pool_alloc(need_ho); a13af_cap_ho = s_ho ? need_ho : 0; }
    g_hoist = (s_hr && s_ho) ? 1 : 0;
  }
  if (g_hoist) {
#if A13AF_PHASE
    u64 a13af_th = a13af_rd();
#endif
#if A13AF_PHASE
    u64 a13af_t1 = a13af_rd();
#endif
#if MF_HR1
    /* [HR1] 一趟点序建点主序秩记录:7 条顺序读 + **一次 32B 全行向量存**
       (旧形态 7 趟各写 1 u32/32B ⇒ 每行一次 RFO 部分写,判题机实测 9.6 拍/元素) */
    for (u32 i = 0; i < N; i++) {
      u32 a13af_v[16] = {0};                 // 16 >= MAXK:k 是运行期值,给足上界免 UB 推断
      for (int d = 0; d < k; d++) a13af_v[d] = s_pos[d][i];
#if MF_HRNT
      _mm256_stream_si256((__m256i *)(s_hr + (size_t)i * 8), _mm256_loadu_si256((const __m256i *)a13af_v));
#else
      _mm256_storeu_si256((__m256i *)(s_hr + (size_t)i * 8), _mm256_loadu_si256((const __m256i *)a13af_v));
#endif
    }
#else
    for (int d = 0; d < k; d++) {
      const u32 *sp = s_pos[d];
      u32 *hr = s_hr + d;
      for (u32 i = 0; i < N; i++) hr[(size_t)i * 8] = sp[i];
    }
#endif
#if A13AF_PHASE
    a13af_t_hr += a13af_rd() - a13af_t1;
    u64 a13af_t2 = a13af_rd();
#endif
    for (int d = 0; d < k; d++) {
      const u32 *odd = s_ord[d];
      u32 *obase = s_ho + (size_t)d * k * N;
      for (u32 j = 0; j < N; j++) {
        if (j + MF_PFBUILD_D < N) _mm_prefetch((const char *)(s_hr + (size_t)odd[j + MF_PFBUILD_D] * 8), _MM_HINT_T0);
        const u32 *pv = s_hr + (size_t)odd[j] * 8;
#if MF_NOSELF
        /* [NOSELF] 顺手把"自身槽"(d,d) 也写掉(该槽从不被任何走查读)⇒ 内环无分支、可展开。
           值 = rank_d(od_d[j]) = j,写的是 k*k*N 里本就分配好的 (d*k+d) 槽 ✓ */
        for (int ds = 0; ds < k; ds++) obase[(size_t)ds * N + j] = pv[ds];
#else
        for (int ds = 0; ds < k; ds++) if (ds != d) obase[(size_t)ds * N + j] = pv[ds];
#endif
      }
    }
#if A13AF_PHASE
    a13af_t_hoist += a13af_rd() - a13af_t2;
    a13af_t_hrall += a13af_rd() - a13af_th;
#endif
  }
#endif
  // ---------- groups ----------
  for (int ds = 0; ds < k; ds++) {
    u32 q0 = gstart[ds], q1 = gstart[ds + 1];
    /* [FBMERGE] do NOT skip an empty group: the pool walk below is what emits this ds's
       coarse planes (bit order = rank_ds) and pass B of OTHER groups reads them.  With
       random coordinates every group is non-empty, but a degenerate input (e.g. x_d[i]=i,
       where every point folds into ds=0) would otherwise leave those planes unbuilt.
       Skipping is still correct for pass A: its loops run over [q0,q1), which is empty. */
#if MF_WTRIM
    {
      u32 mx = 1;
      for (u32 qi = q0; qi < q1; qi++) { u32 rr = s_gt[ds][s_qord[qi]]; if (rr > mx) mx = rr; }
      u32 wp = ((mx >> 6) + 8u) & ~7u;
      Wpool = (wp < W) ? wp : W;
    }
#endif
#if MF_PLEN
    // Per-(dim,bucket) array lengths inside the group.
    if (g_plen_on) {
      u64 tot = 0;
      for (int e = 0; e < K1; e++) {
        int d = (e < ds) ? e : e + 1;
        const u32 *mr = s_maxr + ((size_t)ds * MAXK + d) * MAXQ;
        u32 *po = s_pofs + (size_t)e * MAXQ;
        u32 *pl = s_plen + (size_t)e * MAXQ;
        for (u32 b = 0; b <= Q; b++) {
          // NOTE: bucket 0 is the EMPTY prefix set (all-zero array), but a query whose
          // bd[d]==0 still scans [0,R) words of it, so the array must be long enough --
          // sizing it to the fixed 8-word minimum reads past the zeroed region into the
          // next array.  Use the real per-bucket max R here, not 0.
          u32 RR = mr[b];
          u32 len = ((RR >> 6) + 8u) & ~7u;
          if (len > Wpool) len = Wpool;
          po[b] = (u32)tot; pl[b] = len; tot += len;
        }
      }
      if (tot > (u64)K1 * (Q + 1) * W) { g_plen_on = 0; }   // never; safety
    }
#endif
    // bit numbering = rank in dim ds
#if MF_HOIST
    if (!g_hoist)
#endif
    {
      const u32 *od = s_ord[ds];
      for (u32 j = 0; j < N; j++) s_nid[od[j]] = j;
    }
#if MF_ASCRINC
    /* [INCA] 定扎车道号:与 query_group_A 的 e1 逐字同式;运行期前提 = scratch 覆盖整个位空间
       (N <= AS_WORDS*64)且 HOIST 的顺序流可用。否则 -1 ⇒ 两条路径都退回原字。 */
    int a13ag_inc_lane = -1;
#if MF_ASCR
    if (g_hoist && (u64)N <= (u64)AS_WORDS * 64ull) {
      const u32 a13ag_d1i = (ds == 0) ? 1u : 0u;
      a13ag_inc_lane = (int)((a13ag_d1i < (u32)ds) ? a13ag_d1i : a13ag_d1i - 1u);
    }
#endif
#endif
    const u32 QcM = (u32)g_Qc;
    for (int e = 0; e < K1; e++) {
      int d = (e < ds) ? e : e + 1;
      const unsigned *xd = x[d];
      const u32 *od = s_ord[d];
      /* [FBMERGE] coarse plane for (bit-order dim ds, walked dim d); s_bnde[d][c] is a
         dim-d position, i.e. the very ladder the standalone filter walk consumed. */
      u64 *fbase = (s_fbm && g_use_filt) ? (s_fbm + (size_t)(ds * k + d) * (QcM + 1) * W) : 0;
      const u32 *bnd = s_bnde + (size_t)d * (MAXQ + 2);
      u32 cnext = 1;
      u64 *base = g_plen_on ? s_bs : (s_bs + (size_t)e * (Q + 1) * Wpool);
      u64 *cur = s_acc;
      for (u32 w = 0; w < Wpool; w++) cur[w] = 0;
      if (g_plen_on
#if MF_ASCRINC
          && !(a13ag_inc_lane >= 0 && (int)e == a13ag_inc_lane)
#endif
         ) {
        // bucket 0 is the empty prefix set; bd[] can be 0 so it must be materialised as zeros
        u64 *d0 = s_bs + s_pofs[(size_t)e * MAXQ];
        u32 l0 = s_plen[(size_t)e * MAXQ];
        for (u32 w = 0; w < l0; w++) d0[w] = 0;
      }
      u32 ptr = 0;
      const u32 *ho = 0;
#if MF_HOIST
      if (g_hoist) ho = s_ho + ((size_t)d * k + ds) * N;
#endif
#if A13AF_PHASE
      u64 a13af_tt = a13af_rd();
#endif
#if MF_BUILD_NOPOOL
      while (ptr < N) ptr++;
#else
      for (u32 b = 1; b <= Q; b++) {
        // {q : x_d[q] < BVAL(d,b)} is EXACTLY the prefix [0,FPOS(d,b)) of the dim-d order
        // (FPOS is the tie-group start containing rank b*B), so the per-point value compare
        // -- one random xd read per point per (ds,d) pair, k(k-1)N of them -- is redundant.
        // b==Q means "every point" and FPOS(d,Q) is the 0 sentinel, hence N.
        // Credited to agent a7f799ceb7e63b0cd (worth 4-5% at k=4/5 on the board).
        u32 pend = (b < Q) ? FPOS(d, b) : (u32)N;
        if (fbase) while (cnext <= QcM && bnd[cnext] <= pend) {
          u32 cp2 = bnd[cnext];
#if MF_HOIST
          if (ho) { while (ptr < cp2) { u32 q = ho[ptr]; cur[q >> 6] |= 1ull << (q & 63); ptr++; } } else
#endif
          while (ptr < cp2) {
#if MF_PFBUILD > 0
            if (ptr + MF_PFBUILD_D < cp2) _mm_prefetch((const char *)(s_nid + od[ptr + MF_PFBUILD_D]), _MM_HINT_T0);
#endif
            u32 q = s_nid[od[ptr]]; cur[q >> 6] |= 1ull << (q & 63); ptr++; }
          u64 *fdst = fbase + (size_t)cnext * W;
#if A13AF_PHASE
          u64 a13af_tp = a13af_rd();
#endif
#if MF_NT
          { u32 w = 0;
            for (; w + 4 <= W; w += 4) _mm256_stream_si256((__m256i *)(fdst + w), LDA((const __m256i *)(cur + w)));
            for (; w < W; w++) fdst[w] = cur[w]; }
#else
          for (u32 w = 0; w < W; w++) fdst[w] = cur[w];
#endif
#if A13AF_PHASE
          a13af_t_copy += a13af_rd() - a13af_tp;
#endif
          cnext++;
        }
#if MF_HOIST
        if (ho) {
#if A13AF_NORMW
          u64 a13af_s = 0;
          while (ptr < pend) { a13af_s += ho[ptr]; ptr++; }
          a13af_sink += a13af_s;
#else
#if MF_MASKRMW > 0
          /* 诊断:把随机 RMW 的落点掩到小累加器(L1 常驻)——只测形态,输出必错 */
          while (ptr < pend) { u32 q = ho[ptr]; u32 w = (q >> 6) & (MF_MASKRMW - 1); cur[w] |= 1ull << (q & 63); ptr++; }
#elif MF_HOPF > 0
          while (ptr < pend) {
            if (ptr + MF_HOPF_D < pend) _mm_prefetch((const char *)(cur + (ho[ptr + MF_HOPF_D] >> 6)), _MM_HINT_T0);
            u32 q = ho[ptr]; cur[q >> 6] |= 1ull << (q & 63); ptr++; }
#else
          while (ptr < pend) { u32 q = ho[ptr]; cur[q >> 6] |= 1ull << (q & 63); ptr++; }
#endif
#endif
        } else
#endif
        {
#if A13AF_NORMW
        u64 a13af_s = 0;
        while (ptr < pend) {
#if MF_PFBUILD > 0
          if (ptr + MF_PFBUILD_D < pend) _mm_prefetch((const char *)(s_nid + od[ptr + MF_PFBUILD_D]), _MM_HINT_T0);
#endif
          a13af_s += s_nid[od[ptr]]; ptr++; }
        a13af_sink += a13af_s;
#else
        while (ptr < pend) {
#if MF_PFBUILD > 0
          /* [PFSC2-b] 1015/1016 family recipe, third site: the fine-pool build's gather
             `q = s_nid[od[ptr]]` is a random 4 MB row fetch whose value immediately addresses
             another random RMW (`cur[q>>6] |= ...`); od[] is sequential, so the line for a
             future iteration is known MF_PFBUILD_D points ahead.  6e6 iterations (K1 x N). */
          if (ptr + MF_PFBUILD_D < pend) _mm_prefetch((const char *)(s_nid + od[ptr + MF_PFBUILD_D]), _MM_HINT_T0);
#endif
          u32 q = s_nid[od[ptr]]; cur[q >> 6] |= 1ull << (q & 63); ptr++; }
#endif
        }
        u64 *dst = g_plen_on ? (base + s_pofs[(size_t)e * MAXQ + b])
                              : (base + (size_t)b * Wpool);
        u32 wlen = g_plen_on ? s_plen[(size_t)e * MAXQ + b] : Wpool;
#if MF_ASCRINC
        /* [INCA] 定扎车道 e1 的 fine 平面**不落盘**:其唯一消费者是 pass A 的 s_ascr 拷贝,
           而拷贝已被"增量维护"取代(query_group_A)。取数/粗平面 fbase 逐字不动。 */
        if (a13ag_inc_lane >= 0 && (int)e == a13ag_inc_lane) wlen = 0u;
#endif
#if A13AF_PHASE
        u64 a13af_tc = a13af_rd();
#endif
#if A13AF_NOCOPY
        (void)dst; (void)wlen;
#elif MF_NT
        // The pool is written once and read 89 GB later, so a plain copy pays a
        // read-for-ownership DRAM read per line for nothing.  NT stores remove it
        // (measured: this loop was 4.58 GB / ~1.7 s, i.e. 2.7 GB/s, vs 12.5 GB/s
        // for a plain memset).
        {
          u32 w = 0;
          for (; w + 4 <= wlen; w += 4)
            _mm256_stream_si256((__m256i *)(dst + w), LDA((const __m256i *)(cur + w)));
          for (; w < wlen; w++) dst[w] = cur[w];
        }
#else
        for (u32 w = 0; w < wlen; w++) dst[w] = cur[w];
#endif
#if A13AF_PHASE
        a13af_t_copy += a13af_rd() - a13af_tc;
#endif
      }
      _mm_sfence();
#if A13AF_PHASE
      a13af_t_gath += a13af_rd() - a13af_tt;
#endif
#endif
    }
    switch (k) {
      case 4: query_group_A<4>(N, out, ds, q0, q1, Q, Wpool); break;
      case 5: query_group_A<5>(N, out, ds, q0, q1, Q, Wpool); break;
      case 6: query_group_A<6>(N, out, ds, q0, q1, Q, Wpool); break;
      case 7: query_group_A<7>(N, out, ds, q0, q1, Q, Wpool); break;
      case 8: query_group_A<8>(N, out, ds, q0, q1, Q, Wpool); break;
      case 9: query_group_A<9>(N, out, ds, q0, q1, Q, Wpool); break;
      default: query_group_A<10>(N, out, ds, q0, q1, Q, Wpool); break;
    }
  }
  // [SPLITB] pass B (fringe) is pool-independent (it reads only s_pm/avpool/s_fbm/
  // s_ord/s_gt/s_bd), so all of it can run after every group's pass A.
  for (int ds = 0; ds < k; ds++) {
    u32 q0 = gstart[ds], q1 = gstart[ds + 1];
    if (q0 >= q1) continue;
    switch (k) {
      case 4: query_group_B<4>(N, out, ds, q0, q1, Q, Wpool); break;
      case 5: query_group_B<5>(N, out, ds, q0, q1, Q, Wpool); break;
      case 6: query_group_B<6>(N, out, ds, q0, q1, Q, Wpool); break;
      case 7: query_group_B<7>(N, out, ds, q0, q1, Q, Wpool); break;
      case 8: query_group_B<8>(N, out, ds, q0, q1, Q, Wpool); break;
      case 9: query_group_B<9>(N, out, ds, q0, q1, Q, Wpool); break;
      default: query_group_B<10>(N, out, ds, q0, q1, Q, Wpool); break;
    }
  }
}

void count_4d(int n, const unsigned *x[4], unsigned *out) { solve_mf((u32)n, (const unsigned **)x, out, 4); }
void count_5d(int n, const unsigned *x[5], unsigned *out) { solve_mf((u32)n, (const unsigned **)x, out, 5); }
void count_6d(int n, const unsigned *x[6], unsigned *out) { solve_mf((u32)n, (const unsigned **)x, out, 6); }
void count_7d(int n, const unsigned *x[7], unsigned *out) { solve_mf((u32)n, (const unsigned **)x, out, 7); }
void count_8d(int n, const unsigned *x[8], unsigned *out) { solve_mf((u32)n, (const unsigned **)x, out, 8); }
void count_9d(int n, const unsigned *x[9], unsigned *out) { solve_mf((u32)n, (const unsigned **)x, out, 9); }
void count_10d(int n, const unsigned *x[10], unsigned *out) { solve_mf((u32)n, (const unsigned **)x, out, 10); }

#ifdef LOCAL_TEST
static void brute_nd(int n, const u32 **x, u32 *out, int k) {
  for (int i = 0; i < n; i++) {
    u32 c = 0;
    for (int j = 0; j < n; j++) {
      int ok = 1;
      for (int d = 0; d < k; d++) if (!(x[d][j] < x[d][i])) { ok = 0; break; }
      c += ok;
    }
    out[i] = c;
  }
}
int main(int argc, char **argv) {
  int k = (argc > 1) ? atoi(argv[1]) : 6;
  int n = (argc > 2) ? atoi(argv[2]) : 200;
  int seed = (argc > 3) ? atoi(argv[3]) : 1;
  int mode = (argc > 4) ? atoi(argv[4]) : 0;
  int qq = (argc > 5 && atoi(argv[5]) > 0) ? atoi(argv[5]) : 0;
  g_Q = qq ? (u32)qq : (u32)(n / 250 + 1);
  g_B = (u32)((n + g_Q - 1) / g_Q);
  if (argc > 6) g_nosort = atoi(argv[6]);
  if (argc > 7) g_nosdmp = atoi(argv[7]);
  if (argc > 8) g_skip_and = atoi(argv[8]);
  if (argc > 9) g_skip_fringe = atoi(argv[9]);
  if (argc > 10) g_use_filt = atoi(argv[10]);
  if (argc > 11) g_lexsort = atoi(argv[11]);
  const int dm = (mode >= 10) ? mode - 10 : mode;
  srand(seed);
  static u32 *xs[10];
  u32 *out = (u32 *)malloc(sizeof(u32) * n);
  u32 *ref = (u32 *)malloc(sizeof(u32) * n);
  for (int d = 0; d < k; d++) {
    xs[d] = (u32 *)malloc(sizeof(u32) * n);
    for (int i = 0; i < n; i++) {
      if (dm == 0) xs[d][i] = (u32)(rand() % n);
      else if (dm == 1) xs[d][i] = (u32)(rand() % 3);
      else if (dm == 2) xs[d][i] = 0;
      else if (dm == 3) xs[d][i] = (u32)(n - 1 - (rand() % n));
      else if (dm == 4) xs[d][i] = (u32)(i);
      else xs[d][i] = (u32)(rand() % n);
    }
  }
  if (mode >= 10) {
    struct timespec t0, t1;
    clock_gettime(CLOCK_MONOTONIC, &t0);
    switch (k) {
      case 4: count_4d(n, (const unsigned **)xs, out); break;
      case 5: count_5d(n, (const unsigned **)xs, out); break;
      case 6: count_6d(n, (const unsigned **)xs, out); break;
      case 7: count_7d(n, (const unsigned **)xs, out); break;
      case 8: count_8d(n, (const unsigned **)xs, out); break;
      case 9: count_9d(n, (const unsigned **)xs, out); break;
      default: count_10d(n, (const unsigned **)xs, out); break;
    }
    clock_gettime(CLOCK_MONOTONIC, &t1);
    double ms = (t1.tv_sec - t0.tv_sec) * 1e3 + (t1.tv_nsec - t0.tv_nsec) / 1e6;
    u64 s = 0; for (int i = 0; i < n; i++) s += out[i];
    printf("k=%d n=%d Q=%u TIME %.1f ms  checksum %llu\n", k, n, g_Q, ms, s);
    return 0;
  }
  switch (k) {
    case 4: count_4d(n, (const unsigned **)xs, out); break;
    case 5: count_5d(n, (const unsigned **)xs, out); break;
    case 6: count_6d(n, (const unsigned **)xs, out); break;
    case 7: count_7d(n, (const unsigned **)xs, out); break;
    case 8: count_8d(n, (const unsigned **)xs, out); break;
    case 9: count_9d(n, (const unsigned **)xs, out); break;
    default: count_10d(n, (const unsigned **)xs, out); break;
  }
  brute_nd(n, (const u32 **)xs, ref, k);
  int bad = 0;
  for (int i = 0; i < n; i++) if (out[i] != ref[i]) { if (bad < 5) printf("MISMATCH i=%d got=%u ref=%u\n", i, out[i], ref[i]); bad++; }
  printf("k=%d n=%d mode=%d Q=%u %s (%d mismatches)\n", k, n, mode, g_Q, bad ? "FAIL" : "OK", bad);
  return bad != 0;
}
#endif

CompilationN/AN/ACompile OKScore: N/A

Testcase #14.91 s911 MB + 712 KBAcceptedScore: 100


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