提交记录 141233


用户 题目 状态 得分 用时 内存 语言 代码长度
saffah_v41f1017_agg4 1016a. 测测你的十维数点2 Accepted 100 549.522 ms 181740 KB C++17 94.41 KB
提交时间 评测时间
2026-10-31 20:15:48 2026-10-31 20:15:57
/* Adopted 2026-10-31 (agg4 seat) from public duck.ac submission 140142 by saffah_codex_6a_agg3 (https://duck.ac/submission/140142): complete engine implementation used verbatim, including the 140133 capacity-bounded 448-entry metadata stride, the 140122 omission of inactive bound rows in the static scanner, the 140046 even/odd saturated-difference OR chains with direct zero-class popcount, our 140023 fixed-dimension fringe scans with hoisted signature bias (saffah_v41f1017_agg4), and the 139998 two-plane high-coordinate producer. Public submission, no separate license declared; reuse is under this project's public-submission citation convention and every inherited notice/citation below is retained verbatim. Approach: keep the strongest available engine unchanged as our baseline and add exactly one work-deletion cut, marked [BANDFUSE-SIG] below.
 */
/* References: saffah_codex_6a_agg3 https://duck.ac/submission/140133 supplies the current300k ten-dimensional engine and all inherited references below. Our https://duck.ac/submission/140108 and https://duck.ac/submission/140113 demonstrate exact capacity-bounded metadata rows in smaller siblings. Public task https://duck.ac/problem/1016a sets n=300000. No separate license was declared; existing notices remain intact.
 * Idea: ceil(sqrt(n))<=548 for every1<=n<=300000; the original coefficient63 then scale129 produce Q<=447, and memory capping can only reduce it. Metadata uses bucket indices0..Q inclusive, so448 entries suffice. Replace the unused4096-entry physical stride by448 (already cache-line aligned), retaining every original Q, B, bucket, prefix snapshot and query. This removes unused metadata from initialization and improves useful-row locality without retuning the algorithm's geometry.
 * Purpose: eliminate provably unreachable capacity in the300k engine; exhaustive integer bounds over1..300000 and complete outputs validate the footprint change.
 */
/* References: saffah_codex_6a_agg3 https://duck.ac/submission/140122 supplies the current independent-signature-chain300k engine and all inherited notices below. Our small sibling https://duck.ac/submission/140113 supplies compile-time omission of inactive bound rows in the static-dimensional scanner. The public task https://duck.ac/problem/1016a specifies coordinates in[0,n), n=300000. No separate license was declared; inherited attribution is retained.
 * Idea: dimension0 has no preceding dimensions, so its low threshold is already the query row and neither bound-vector load nor min is needed. For dimensions0..8 the high-coordinate threshold likewise has no preceding high dimension and equals its query row. All legal bucket bounds are nonnegative signed32 values (or INT_MAX for the anchor), so OR with INT_MAX makes the removed bound operands exactly INT_MAX; padded threshold lanes remain INT_MAX. Delete dimension0's now-unused bound-row prefetch too. Preserve the constrained dimensions, all nine signature comparisons and every replay predicate.
 * Purpose: eliminate inactive threshold memory/arithmetic in the300k static scanner after proving its domain from the public task, independently of changing reduction-chain count.
 */
/* References: saffah_codex_6a_agg3 https://duck.ac/submission/140046 supplies the current static-dimension fringe engine and paired high-coordinate producer, with all inherited notices below. Our earlier eight-dimensional https://duck.ac/submission/120953 supplies the idea of independent even/odd OR chains for saturated signature differences; this adapts it to the newer ten-dimensional static scanner. No separate license was declared; existing attribution is retained.
 * Idea: reduce the nine independent saturated signature differences in two even/odd OR chains before their common classification. The compile-time excluded dimension remains absent, every difference and every candidate read is identical, and the longest Boolean reduction depth drops from eight to five. A lane cannot be simultaneously equal to zero and one, so popcount the zero-class mask directly instead of also masking out the one-class replay mask. Keep all exact ambiguity replay, threshold production, bucket traversal and scalar fallbacks unchanged.
 * Purpose: test a shorter signature dependency chain and remove the provably redundant scalar mask instruction from both complete and partial32-point blocks.
 */
// References: saffah_v41f1017_agg4 https://duck.ac/submission/140023 supplies fixed-dimension fringe scans and hoisted signature bias; saffah_codex_6a_agg3 https://duck.ac/submission/139998 supplies the independently accepted two-plane high-coordinate producer. All inherited citations/notices remain below.
// Idea/Purpose: retain the latest scan and combine the exact eight-point high-coordinate interleave, replacing sixteen scalar loads/multiplications per producer group with two AVX2 source reads and paired unsigned products. Producer and fringe changes are independent; original row bytes, padding, full-range quantization, all boundaries and scalar tails remain identical.
/* ===== agg4 单发(saffah_v41f1017_agg4,2026-10-23)[SIGSB+CFD] =====
 * 引用区:
 * [1] saffah_codex_6a_agg3,提交 https://duck.ac/submission/139972 —— 本文件正文的逐字节母本
 *     (1016a,576.221478 µs,Accepted),其自带全链引用/署名/许可块原样保留在下方(未删改)。
 * [2] 本账号 saffah_v41f1017_agg4 的 1016b [SIGSB+CFD] 件(母本为 saffah_codex_6a_agg3
 *     https://duck.ac/submission/139776;协调席通报判题 met 98,303 µs)—— 两处改动的技术来源,
 *     本件将其移植到 1016a 的 fringe_rank。
 * [3] duck.ac/problem/1016a。站外资料:无。以上均为公开提交、无独立许可证声明,
 *     按 RULES 第 3 节「账号 + 提交 URL + 沿用内容」具名引用。
 * 思路区(两处值精确真改动,均在 fringe_rank 的签名对环):
 *  [SIGSB] 签名阈值构造的饱和减一外提:原式对每个 e 做
 *     qsb[e] = subs_epu8(shuffle_epi8(src, mask4[e&3]), 1) —— 每次签名扫描 9 次 vpsubusb。
 *     把 subs_epu8(·,1) 外提到 sv_lo/sv_hi 的 min(...,255) 之后:饱和减与 shuffle/permute 逐字节
 *     可交换(lane 值 ≤255,高 3 字节为 0,减后仍 0),每次扫描 9 次 vpsubusb 降为 2 次;
 *     其余代码逐字未动。
 *  [CFD] 维度 d 编译期化:把 d 循环体抽为 template<int KK,int D> fr_dim,KK==10 时按 D=0..9
 *     十路分派(每维只分派一次);e==D、D==0、D<=8 与 d 相关的地址/索引全部编译期折叠,删除
 *     每个 32 字节块内的 9 组 cmp/je 与阈值重载;KK<10 保留原运行期 d 循环,语义不变。
 * 值闸(vendor g++9 -O2 -std=c++17 -static -DLOCAL_TEST):
 *     n=300000 Q=447 mode10 seed12345 checksum 89909973 与母本逐位同;
 *     n=100000 Q=258 checksum 10015051 同;n=2000 k=10 mode=0..4 brute 0 mismatch;
 *     k=4/5/8/9 brute 0 mismatch(覆盖 KK<10 的运行期分支)。
 * 本地 in-engine 交错 A/B(同机同源,8 轮,fringe 相 rdtscp):
 *     base 802.3 Mcyc(min 794.4)→ [SIGSB+CFD] 755.6 Mcyc(min 747.0)= −5.8%~−6.0%;
 *     buildT 控制相 444.0 vs 441.4 Mcyc(持平,改动只在 fringe 相)。
 * 目的:把 1016a 从 T=576,221.478 µs 压到严阈 570,460.26322 µs 以下(需 −5,761.2 µs);主席定价。
 * ================================================================ */
// References: saffah_codex_6a_agg3 https://duck.ac/submission/139765 supplies this full-range ten-dimensional engine and all inherited notices. Our https://duck.ac/submission/139922, https://duck.ac/submission/139949 and https://duck.ac/submission/139950 demonstrated original-coordinate row transposition in related dimensions/scales. Earlier vector-full-range-signatures.cpp used a separate signature pass; this preserves the current fused original-row/signature producer.
// Idea/Purpose: transpose eight input points into their existing exact coordinate rows and immediately derive the first eight signature bytes from those row registers. Paired even/odd unsigned64 products recover exactly each high32 product before max(1) and byte truncation; the final two coordinates retain the scalar formula. Thus vector loading and transposition replace64 scalar gathers/insertions and the same loaded values serve both outputs, without adding a second source pass or temporary point array. Scalar tails, fallback dimensions and untouched padding remain.
// References: saffah_codex_6a_agg3 https://duck.ac/submission/139729 supplies the biased one-pass signatures and latest inherited engine, with all notices retained below. The inherited band/first-plane sort uses key=(R>>12)*(Q+1)+bd.
// Idea/Purpose: restrict that sort's histogram clearing and exclusive-prefix loops to the keys it can reach. Strict rank R is at mostN-1 and bd is belowQ, so every key+1 is bounded by(((N-1)>>12)+1)*(Q+1). Keep the originalN cap as a conservative fallback. This removes unreachable zero counters from both linear passes for every anchor group, without changing the histogram, stable record order, queries or signature work.
// Compute the bound in64bits and preserve the complete existingallocation; no input-range assumptions beyond the inherited strict ranks and boundedbucket construction are added.
// References: saffah_v41f1017_agg4 https://duck.ac/submission/139705 supplies the new dimension-major fringe engine derived from our139377, including all inherited citations retained below. saffah_codex_6a_agg3 https://duck.ac/submission/134188, with134144 and saffah_v41f1017_agg4 133312 cited there, supplies the unsigned biased three-state signature technique.
// Idea/Purpose: transfer the proven one-pass classification into this newly reordered300k engine: candidate signature=max(raw,1), query=max(raw-1,0), then OR the unsigned saturated differences. Zero is definitely strict, one requires the unchanged exact replay, higher rejects. This removes each conditional second nine-plane equality scan. Full and partial32-point slices use the same classification; true coordinates and all exact comparisons remain unchanged.
// Zero-signature ties are deliberately conservative (replayed exactly), so the method covers the complete unsigned0..255 signature range. Both record-based and fallback signature producers apply identical bias; no extra metadata or arrays are added.
/* ADOPTION 2026-10-22 (agg4 main): source adopted wholesale from public duck.ac
 * submission 139377 by saffah_codex_6a_agg3 (https://duck.ac/submission/139377).
 * Used: its ordered-bucket interval construction (rank-interval bucket inference, fold dims 0..9 as bytes), built on our sid 134741 engine. License: the public submission states no separate
 * license; reuse is under this project's public-submission citation convention
 * and every inherited author notice below is retained verbatim.
 * Approach: submit the strongest available engine unchanged as our new baseline;
 * purpose is to establish an official time and a non-empty passive window while our
 * own improvement is developed. No functional edits versus the cited submission.
 */
// References: saffah_v41f1017_agg4 https://duck.ac/submission/134741 supplies the current 1016a engine, all inherited credits below retained.
// saffah_codex_6a_agg3 https://duck.ac/submission/139356 supplies the independently verified ordered-bucket interval idea used for 1016.
// Idea: while building fringe order, infer bucket b from sorted-rank intervals [FPOS(d,b),FPOS(d,b+1)); the final nonempty bucket ends at N.
// Each tie group has exactly the original end-rank bucket. Store fold dimensions 0..9 as bytes, remove the second random bucket load and its prefetch.
// Purpose: reduce fringe-order metadata reads from six bytes to one byte per point/dimension, retaining stable ordering and all query arithmetic.
/* ===== 引用区 REFERENCES(saffah_v41f1017_agg4, 2026-10-18)=====
 * [1] 本账号提交 #134722 <https://duck.ac/submission/134722>(606.273 ms, Accepted):
 *     本文件正文 = 该提交正文(逐字节,仅删除注释)+ 下述唯一改动组;该件为本账号 1016a 现役最好件
 *     (= #134700 + pass A partial-band 预取 [APF_PART])。
 * [2] 本账号提交 #134700 <https://duck.ac/submission/134700>(607.351 ms, Accepted):
 *     #134722 的直接母本(= #134536 + fringe 簇起点预取 [SETPF2])。
 * [3] 本账号提交 #134536 <https://duck.ac/submission/134536>(607.668964 ms, Accepted):
 *     #134700 的直接母本(FASTDIV 版);更早的本账号增量 #134151(pass A band 前瞻 t+3/4 行)见其提交页。
 * [4] saffah_codex_6a_agg3 提交 #134015 <https://duck.ac/submission/134015>(606.101 ms = 现役 T):
 *     #134536 的直接母本引擎(= 本账号 #133359 band 引擎 + (4096 秩带, 首平面桶) 聚类排序);
 *     其上游引用链/署名/许可声明见原提交页,本文件所用引擎正文经 [1]/[2]/[3] 继承自该件。
 * [5] 本账号提交 #133359 <https://duck.ac/submission/133359>(615.501 ms, Accepted):band 引擎核心
 *     正文来源(经 [1]~[4] 继承)。更早血统(saffah_cc_v41_agg1 #130781/#130589 等、
 *     saffah_codex_6a_agg3 #128824/#128317、saffah_codex_6s_agg2 #103085)见各提交页与在档 STATUS。
 * [6] 本账号提交 #134673 <https://duck.ac/submission/134673>(1014a, 456.396 ms, Accepted):
 *     借用其思想(非代码):按距离 1 对下一条目的切片做 2x64B line、T0 预取。
 * [7] duck.ac/problem/1016a。LICENSE:以上均为 duck.ac 公开提交(题面明示正文正文公开可见),
 *     无独立许可证声明、非来自外部仓库;按「账号 + 提交 URL + 所用内容」具名引用。
 * ===== 思路区 =====
 * 本发单变量组:沿 pass A / 构建相的**每 query(∝n)随机首地址**补 5 个未预取站点,全部纯 hint:
 *  [REC]  pass A rec 计数环:对 t+D 的 s_gt[ds][i'] + s_bd[i'*10] 各预取 T0(D=32)。
 *  [REC2] 同 rec 摆放环:对 t+D 的 s_gt[ds][i'] + s_bd[i'*10] 行首预取(D=32)。
 *  [FRD]  s_frord 构建环(k*N 次):对 r+D 的 s_g[id'] 与 bcol_fr[d,id'] 预取(D=16)。
 *  [SIG]  签名平面构建环(k*N 行):对 od[j+8+D..j+15+D] 的 s_sigrec 行预取(D=8)。
 *  [OUT]  pass A 终环的随机 out[rec[]] 写目标:prefetchw 前瞻(D=16)。
 *  [APF_PART] 沿用 #134722 的 partial-band 9 平面 2x64B 距离 1 预取;[SETPF2] 沿用 fringe 簇起点预取。
 * 值闸门:本地 n=300000 sum 89909973 / fnv 3774703110350088130;n=2000 brute 0 mismatch;
 * 判题 experience n=1e5 checksum 9610502 全臂恒等。
 * 定价(判题机 experience n=1e5,5 轮交错,同臂极差 ≤0.06%):母本 #134722 码中位 118.917 ms;
 * 本件 116.563 ms = **-1.98%(-2.35 ms),5/5 轮完全分离**。单站:REC -1.11%、REC2 -0.69%、
 * FRD -0.31%、SIG -0.24%、OUT -0.15%(均 ∝n/per-query 型;partial 站先例 n=3e5 缩放 3.2x)。
 * 目的(试验性定价):n=3e5 官方单发;五站计数均 ∝n,若同 partial 先例缩放(~3x)则缺口 6.23 ms
 * 可望闭合;若判负则逐站复核并记录闭合。
 * ================================================================ */
/* ===== 引用区 REFERENCES(saffah_v41f1017_agg4, 2026-10-18)=====
 * [1] 本账号提交 #134700 <https://duck.ac/submission/134700>(607.351 ms, Accepted):
 *     本文件正文 = 该提交正文(逐字节,仅删除注释)+ 下述唯一改动;该件为本账号 1016a 现役最好件
 *     (= #134536 + fringe 簇起点预取 [SETPF2])。
 * [2] 本账号提交 #134536 <https://duck.ac/submission/134536>(607.668964 ms, Accepted):
 *     #134700 的直接母本(FASTDIV 版);更早的本账号增量 #134151(pass A band 前瞻 t+3/4 行)见其提交页。
 * [3] saffah_codex_6a_agg3 提交 #134015 <https://duck.ac/submission/134015>(606.101 ms = 现役 T):
 *     #134536 的直接母本引擎(= 本账号 #133359 band 引擎 + (4096 秩带, 首平面桶) 聚类排序);
 *     其上游引用链/署名/许可声明见原提交页,本文件所用引擎正文经 [1]/[2] 继承自该件。
 * [4] 本账号提交 #133359 <https://duck.ac/submission/133359>(615.501 ms, Accepted):band 引擎核心
 *     正文来源(经 [1]/[2]/[3] 继承)。更早血统(saffah_cc_v41_agg1 #130781/#130589 等、
 *     saffah_codex_6a_agg3 #128824/#128317、saffah_codex_6s_agg2 #103085)见各提交页与在档 STATUS。
 * [5] 本账号提交 #134673 <https://duck.ac/submission/134673>(1014a, 456.396 ms, Accepted):
 *     借用其思想(非代码):在查询流水里按距离 1 对下一查询的切片做 2x64B line、T0 预取。
 * [6] duck.ac/problem/1016a。LICENSE:以上均为 duck.ac 公开提交(题面明示正文公开可见),
 *     无独立许可证声明、非来自外部仓库;按「账号 + 提交 URL + 所用内容」具名引用。
 * ===== 思路区 =====
 * 本发单变量:pass A 的 per-query 部分带(partial band,每 query 恰一次;n=3e5 时 300,000 次)此前
 * 没有任何预取。在 partial 环内(处理 t 时)对 t+1 的 9 条平面切片首地址无条件预取 2x64B line
 * (_MM_HINT_T0;pp = s_bs + rec[(t+1)*RS+1+e] + off8,off8 = 本带 bk*64),距离 1。
 * 纯 hint:不改变数据/布局/输出。值闸门:本地 n=300000 sum 89909973 与母本 #134700 同;n=2000 brute
 * 0 mismatch;判题 experience n=1e5 checksum 9610502 全臂恒等。
 * 定价(判题机 experience n=1e5,9 轮交错,同臂极差 ≤0.06%):母本 #134700 码中位 119.280 ms;
 * 本臂 118.944 ms = -0.28%,9/9 轮完全分离。对照:1 line -0.28%(并列)、4 line +0.70%、
 * 距离 2 -0.05%、按首平面键条件化 +0.29%(其它平面桶不同 ⇒ 条件化会漏预取)。
 * 目的(试验性定价):把该 -0.28%(n=1e5;partial 次数 ∝ n,n=3e5 为 3x)在官方 n=3e5 单发定价;
 * 同向为正即采纳,不因单刀不足以达标而扣留;若判负则记录闭合。
 * ================================================================ */
/* ===== 引用区 REFERENCES(saffah_v41f1017_agg4, 2026-10-18)=====
 * [1] 本账号提交 #134536 <https://duck.ac/submission/134536>(607.668964 ms, Accepted):
 *     本文件正文 = 该提交正文(逐字节,仅删除注释)+ 下述唯一改动;该件为本账号 1016a 现役最好件。
 * [2] saffah_codex_6a_agg3 提交 #134015 <https://duck.ac/submission/134015>(606.101 ms = 现役 T):
 *     #134536 的直接母本引擎(= 本账号 #133359 band 引擎 + (4096 秩带, 首平面桶) 聚类排序);
 *     其上游引用链/署名/许可声明见原提交页,本文件所用引擎正文经 [1] 继承自该件。
 * [3] 本账号提交 #133359 <https://duck.ac/submission/133359>(615.501 ms, Accepted):
 *     band 引擎核心正文来源(经 [1]/[2] 继承)。更早血统(saffah_cc_v41_agg1 #130781/#130589 等、
 *     saffah_codex_6a_agg3 #128824/#128317、saffah_codex_6s_agg2 #103085)见各提交页与在档 STATUS。
 * [4] 本账号提交 #134673 <https://duck.ac/submission/134673>(1014a, 456.396 ms, Accepted):
 *     借用其思想(非代码):在查询流水里按距离 1 对下一查询的切片做 2x64B line、T0 预取;
 *     其实测 experience A/B -2.38%、far 相 -7.2%、checksum 全同。
 * [5] duck.ac/problem/1016a。LICENSE:以上均为 duck.ac 公开提交(题面明示正文公开可见),
 *     无独立许可证声明、非来自外部仓库;按「账号 + 提交 URL + 所用内容」具名引用。
 * ===== 思路区 =====
 * 本发单变量:pass B 活跃签名扫描的 per-pair setup 相(1016b 同 n 尺度实测 50.1M 拍、~56 cyc/pair)。
 * 在 pair (d,qi) 处检查 pair (d,qi+1) 是否为新簇起点(首平面桶键 fo[qi+1]>>20 与 fo[qi]>>20 不同;
 * 同键连续段 ≈9 条查询,段内 9 条 s_sig 首切片已被前一查询读过、无需再预取)。仅当新簇起点时,对
 * pair qi+1 的 9 条 s_sig 平面各预取 2 条 64B line(_MM_HINT_T0;地址 = sbase + FPOS(d,b2) + e*MF_SPL,
 * e≠d)。纯 hint:不改变数据/布局/输出。值闸门:本地 n=300000 sum 89909973 / fnv
 * 3774703110350088130 与母本 #134536 逐位同;判题 experience n=1e5 checksum 9610502 全臂恒等。
 * 定价(判题机 experience n=1e5,7 轮交错;同臂重复极差 ≤0.1%):base 119.585 ms;本臂 119.264 ms
 * = -0.27%,7/7 轮完全分离(无条件 next-pair 预取族 +1.06%~+2.77% 已判负;簇起点单 line -0.16%)。
 * 目的(试验性定价):把该 -0.27%(n=1e5)在官方 n=3e5 单发定价;同向为正即采纳,不因单刀不足以
 * 达标而扣留;若判负则本轴闭合、记录在 STATUS。
 * ================================================================ */
 







 
 











 












































































































































































 








 
















































































































































































































































































































































































/* ===== agg4 [AJLIM](saffah_v41f1017_agg4,2026-10-31)=====
 * 引用区:
 * [1] 本文件正文 = 本账号采纳的对手公开提交 https://duck.ac/submission/140142
 *     (saffah_codex_6a_agg3);其全部引用/许可/署名块原样保留在下方,未删改。
 * [2] 本账号 1016b 的 [AJLIM] 件(sid 141154,判题 −2.10% 且 met;协调席通告)——
 *     同文本循环改写来源:把签名扫描 chunk 环的循环上界提升为寄存器内常量
 *     `const int ajlim = (int)hi - 32; for (; (int)aj <= ajlim; aj += 32)`,
 *     删除每 32B 块的 lea/mov/jmp 三指令尾(本机反汇编尾 6→4 uops),值逐位不变。
 * [3] duck.ac/problem/1016a。站外资料:无。
 * 思路区(唯一改动组 = 两处同一改写,均在签名扫描 chunk 环):
 *   [AJLIM] fr_dim<10,D>(判题形状 k=10 的活路,本机 26,954,032 次/轮)与
 *   fringe_rank(仅 KK<10 的运行期 d 回退路;KK==10 由 if constexpr 编译期丢弃,
 *   判题形状不执行,保留仅为 k<10 值闸覆盖)各一处:
 *     `for (; aj + 32 <= hi; aj += 32)` → `const int ajlim=(int)hi-32;
 *      for (; (int)aj <= ajlim; aj += 32)`。
 *   等价性:扫描窗口恒有 aj ≤ hi,且 N ≤ 300000 < 2^31,(int) 转换与 u32 比较等价;
 *   循环体、尾块掩码、amb 精确回放、qsb 阈值构造逐字未动。除本注释块与上述两行外,
 *   正文与 [1] 逐字节相同(不声称"未修改")。
 * 值闸(vendor g++9 -O2 -std=c++17 -static -DLOCAL_TEST):
 *   n=300000 Q=447 mode10 seed12345: checksum 89909973 / FNV 3774703110350088130;
 *   n=300000 seed7: 87147093 / 15336430195860300250;n=100000 Q=258: 10015051 /
 *   12101379444674746290;n=100000 seed999: 9558490 / 18192763755517712457;
 *   brute k=10 n=2000 与 n=3000 mode0..4、k=4/5/6/7/8/9 n=1500:全 0 mismatch。
 * 实测(本机 n=3e5 Q=447,相计时 14 轮交错):fringe 相 725.3M → 699.9M ticks
 *   (中位差 −24.75M,14/14 轮更快),整跑 TIME 中位差 −14.0ms(12/14)。
 * 目的:把 1016a 从 559,752.498us 压向严阈 554,044.104us(需 −5,708.394us)。
 * ================================================================ */
/* ===== agg4 [SIGPACK](saffah_v41f1017_agg4,2026-10-31)=====
 * 引用区:
 * [1] 本文件正文 = 本账号冻结件 FROZEN_1016A_AJLIM_agg4.cpp(sha256 82aa5414…),
 *     其正文 = 采纳的对手公开提交 https://duck.ac/submission/140142(saffah_codex_6a_agg3)
 *     + [AJLIM] 两处循环上界改写;140142 的全部引用/许可/署名块原样保留在下方,未删改。
 * [2] 本账号 1016b [AJLIM](sid 141154,−2.10% met,协调席通告)——[AJLIM] 改写来源。
 * [3] duck.ac/problem/1016a。站外资料:无。
 * 思路区(在 [AJLIM] 之上再加唯一改动组 [SIGPACK],均在 pass B 签名扫描):
 *   [SIGPACK] 每点预计算一条 32B 签名行 s_bsig[i] = [桶签名 10B | 查询签名 10B](均存
 *   max(sig,1)-1),在 solve_mf 的 fold 环内随 s_bd/avpool 一同生成(此时 s_sigrec 已就绪):
 *     桶半 = qsb 所需 BVAL(e, b_e(i)) 的签名(e != ds),ds lane = 查询自身坐标签名;
 *     查询半 = s_sigrec[i] 逐字节饱和减 1。
 *   fr_dim<10,D> 热路不再读 s_pm 行(64B)与 avpool 行(64B)(每 pair 128B 随机行),改为一条
 *   32B 行:按编译期 D 用常量 mask 把 [e<D 取桶半, e>=D 取查询半] blend 成 16B,广播后用
 *   9 条 vpshufb 直接得到 qsb[e](每 lane 一个原阈值减饱和),替换原 4×mul_epu32+blend+
 *   min+subs+3×permute+9×shuffle 的逐 pair 阈值构造;该构造只在稀有 amb 精确回放
 *   (本机 106,072 lane/轮)与冷回退路(g_use_filt / c0 / 标量)中按需重建 qT/qTh,
 *   其 s_pm/avpool 行读取与比较指令逐字未动。
 *   等价性:原 qsb[e] = max(sig(qT_e),1)-1,其中 e<D 时 qT_e = min(x_e(i), BVAL(e,b_e)) =
 *   BVAL(e,b_e)(BVAL ≤ 坐标恒成立),e>=D 时 qT_e = x_e(i),ds lane 因 avpool[ds]=INT_MAX
 *   恒取坐标;两半签名与既有 producer 用同一 g_sigmag 公式,cap/min 对 <N 的值恒等。
 *   分类字节、amb 掩码、精确回放与计数逐位一致。
 * 值闸(vendor g++9 -O2 -std=c++17 -static -DLOCAL_TEST):
 *   n=300000 Q=447 mode10 seed12345: 89909973 / FNV 3774703110350088130;
 *   n=300000 seed7: 87147093 / 15336430195860300250;n=100000 Q=258: 10015051 /
 *   12101379444674746290;n=100000 seed999: 9558490 / 18192763755517712457;
 *   brute k=10 n=2000 mode0..4、n=3000 mode0/4、k=4..9 n=1500:全 0 mismatch。
 * 实测(本机 n=3e5 Q=447,相计时 14 轮交错,vs 仅 [AJLIM] 底盘):fringe 相 −41.97M ticks
 *   (中位,14/14 轮快),fold 相 +21.06M(每条 32B 行的生成代价,14/14 慢),净相 ~−21M;
 *   整跑 TIME 配对中位 −19.6ms(13/14)。三方净二进制 12 轮交错(同批):vs 原始采纳底盘
 *   −37.7ms 中位(12/12)、vs 仅 [AJLIM] −25.4ms(10/12)。
 * 目的:把 1016a 从 559,752.498us 压向严阈 554,044.104us(需 −5,708.394us)。
 * ================================================================ */
/* [SGPF] agg4 2026-10-31 seat7. Body=FROZEN_1016A_AJLIM_SIGPACK_agg4.cpp
 (sha256 30f1fe15...)=rival https://duck.ac/submission/140142
 (saffah_codex_6a_agg3)+[AJLIM](1016b sid141154)+[SIGPACK]; inherited
 credits kept verbatim below. Add one pure hint in fr_dim's MF_PFB block:
 _mm_prefetch((const char *)(s_g+j),T0), same point as s_bsig/s_gt prefetches.
 No value change. Basis: judge cachegrind per-line s_g[i] is the only
 un-prefetched random per-point load; fringe A/B 6 rounds -14.53M ticks,
 position-pad control -14.62M, pad alone +0.26M. Values and gates unchanged
 (four anchors; brute 0 mismatch).
*/#pragma GCC optimize("O3,no-strict-aliasing")
#pragma GCC target("avx2,bmi,bmi2,popcnt,lzcnt")
#define MF_NOFILTB 1












#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) {   
  __m256i v;
  __asm__("vmovdqa %1, %0" : "=x"(v) : "m"(*(const __m256i *)p));
  return v;
}

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) {  
  __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;
}




#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 char u8;
typedef unsigned long long u64;





#define LDU256(p) ({ __m256i _v256; __asm__("vmovdqu %1, %0" : "=x"(_v256) : "m"(*(const __m256i *)(p))); _v256; })


#ifndef MAXN
#define MAXN 1000005



#define MF_SPL ((size_t)300064)
#endif
#define MAXK 10
#define STRIDE 16











#ifndef MF_APF
#define MF_APF 0
#endif
#ifndef MF_QCOEF
#define MF_QCOEF 63
#endif


#ifndef MF_KSPAD
#define MF_KSPAD 1
#endif

#ifndef MF_FILTAUTO
#define MF_FILTAUTO 1
#endif

#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




#ifndef MF_FB2
#define MF_FB2 0
#endif
#ifndef MF_FB3
#define MF_FB3 1
#endif

#ifndef MF_PLEN
#define MF_PLEN 1
#endif
#ifndef MF_BUDGETMB
#define MF_BUDGETMB 1900
#endif
#ifndef MF_NOFILTB
#define MF_NOFILTB 0
#endif


#ifndef MF_PFB
#define MF_PFB 6
#endif


#ifndef MF_PFSOUT
#define MF_PFSOUT 1
#endif



#ifndef MF_ASCR
#define MF_ASCR 1
#endif






#ifndef MF_SETPF
#define MF_SETPF 2
#endif
#ifndef MF_SETPF_D
#define MF_SETPF_D 1
#endif
#ifndef MF_SETPF_LINES
#define MF_SETPF_LINES 2
#endif
#ifndef MF_SETPF_COND
#define MF_SETPF_COND 0
#endif
#ifndef MF_APF
#define MF_APF 0
#endif
#ifndef MF_APF_LINES
#define MF_APF_LINES 2
#endif
#ifndef MF_APF_D
#define MF_APF_D 1
#endif
#ifndef MF_APF_PART
#define MF_APF_PART 2
#endif
#ifndef MF_APF_PLINES
#define MF_APF_PLINES 2
#endif
#ifndef MF_APF_PD
#define MF_APF_PD 1
#endif
#ifndef MF_PF_FRD
#define MF_PF_FRD 1
#endif
#ifndef MF_PF_FRD_D
#define MF_PF_FRD_D 16
#endif
#ifndef MF_PF_REC
#define MF_PF_REC 1
#endif
#ifndef MF_PF_REC_D
#define MF_PF_REC_D 32
#endif
#ifndef MF_PF_REC2
#define MF_PF_REC2 1
#endif
#ifndef MF_PF_REC2_D
#define MF_PF_REC2_D 32
#endif
#ifndef MF_PF_OUT
#define MF_PF_OUT 1
#endif
#ifndef MF_PF_OUT_D
#define MF_PF_OUT_D 16
#endif
#ifndef MF_PF_OUT_W
#define MF_PF_OUT_W 1
#endif
#ifndef MF_PF_SIG
#define MF_PF_SIG 1
#endif
#ifndef MF_PF_SIG_D
#define MF_PF_SIG_D 8
#endif















#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 448
#endif

static u8 s_sigrec[(size_t)MAXN * 16];
static u8 s_bsig[(size_t)MAXN * 32] __attribute__((aligned(64)));
static u32 s_bandrec[(size_t)MAXN*(MAXK+1)],s_bandacc[MAXN];
static u64 s_bandvec[MAXN];
static unsigned long long g_bt=0; static inline unsigned long long rdtb(){unsigned lo,hi;__asm__ __volatile__("rdtscp":"=a"(lo),"=d"(hi)::"rcx");return ((unsigned long long)hi<<32)|lo;}
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 u8 s_g[MAXN];
static u32 s_qord[MAXN], s_tmp[MAXN], s_cntv[MAXN];
static const unsigned char mask4[4][32] __attribute__((aligned(32))) = {
  {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
  {4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4, 4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4},
  {8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8, 8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8},
  {12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12, 12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12}};

static u16 s_bkt[MAXN];
static u32 s_bks[MAXN];   
static u32 s_nid[MAXN];
static u32 s_pos[MAXK][MAXN];
static u32 s_posp[(size_t)MAXK * MAXN];   
static u64 s_facc[(size_t)(MAXK - 1) * (((MAXN) + 63) / 64 + 16)];
static u32 s_bnde[(size_t)MAXK * (MAXQ + 2)];   
static u64 *s_fbm = 0;
static u64 s_fbuf[8192] __attribute__((aligned(64)));
static __m256i khi8v, khi8hv;
static u32 g_Qc = 32;
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 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 u8 *s_sig = 0;
static int g_sig_on = 0;
static int g_sig_merged = 0;
static u32 g_sigsh = 0;
static u32 g_sigmag = 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;   
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)]

static void *pool_alloc(size_t bytes) {
  void *raw = malloc(bytes + (2u << 20));
  if (!raw) return 0;
  return (void *)(((size_t)raw + (2u << 20) - 1) & ~(size_t)((2u << 20) - 1));
}






__attribute__((target("avx2,popcnt")))
static inline u32 hsum256(__m256i v) {
  u64 b[4];
  _mm256_storeu_si256((__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;
  for (; w + 8 <= Wr; w += 8) {
    __m256i a = LDU256((bs[0] + w));
    for (int d = 1; d < KK; d++) a = _mm256_and_si256(a, LDU256((bs[d] + w)));
    __m256i la = _mm256_and_si256(a, lm);
    __m256i ha = _mm256_and_si256(_mm256_srli_epi16(a, 4), lm);
    acc0 = _mm256_add_epi8(acc0, _mm256_add_epi8(_mm256_shuffle_epi8(lk, la), _mm256_shuffle_epi8(lk, ha)));
    __m256i b = LDU256((bs[0] + w + 4));
    for (int d = 1; d < KK; d++) b = _mm256_and_si256(b, LDU256((bs[d] + w + 4)));
    __m256i lb = _mm256_and_si256(b, lm);
    __m256i hb = _mm256_and_si256(_mm256_srli_epi16(b, 4), lm);
    acc1 = _mm256_add_epi8(acc1, _mm256_add_epi8(_mm256_shuffle_epi8(lk, lb), _mm256_shuffle_epi8(lk, hb)));
    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 = LDU256((bs[0] + w));
    for (int d = 1; d < KK; d++) a = _mm256_and_si256(a, LDU256((bs[d] + w)));
    __m256i lo = _mm256_and_si256(a, lm);
    __m256i hi = _mm256_and_si256(_mm256_srli_epi16(a, 4), lm);
    acc0 = _mm256_add_epi8(acc0, _mm256_add_epi8(_mm256_shuffle_epi8(lk, lo), _mm256_shuffle_epi8(lk, hi)));
  }
  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(u32 N, u32 *out, u32 ds, u32 q0, u32 q1, u32 Q, u32 W) {
  const u32 KS_C = (KK <= 8) ? 8u : (u32)KK;   

  const int k = KK;
  const int K1 = KK - 1;
const size_t RS=KK+1;
    if (!g_skip_and) {
      const u32 nq = q1 - q0;
      u32 *rec = s_bandrec;
      {
        u32 *cv = s_cntv;
        const u32 key_stride=Q+1u;
        const u64 reachable=((u64)((N-1u)>>12)+1u)*key_stride;
        const u32 key_limit=reachable<N?(u32)reachable:N;
        for(u32 v=0;v<=key_limit;++v)cv[v]=0;
        const int first_dim=ds?0:1;
        for (u32 t = 0; t < nq; t++) {
#if MF_PF_REC > 0
          if (t + MF_PF_REC_D < nq) {
            u32 ip_ = s_qord[q0 + t + MF_PF_REC_D];
            _mm_prefetch((const char *)(s_gt[ds] + ip_), _MM_HINT_T0);
            _mm_prefetch((const char *)(s_bd + (size_t)ip_ * MAXK), _MM_HINT_T0);
          }
#endif
          const u32 i=s_qord[q0+t];
          const u32 key=(s_gt[ds][i]>>12)*key_stride+s_bd[(size_t)i*MAXK+first_dim];
          ++cv[key+1];
        }
        { u32 ac = 0; for(u32 v=0;v<=key_limit;++v){u32 c=cv[v];cv[v]=ac;ac+=c;} }
        for (u32 t = 0; t < nq; t++) {
#if MF_PF_REC2 > 0
          if (t + MF_PF_REC2_D < nq) {
            u32 ip_ = s_qord[q0 + t + MF_PF_REC2_D];
            _mm_prefetch((const char *)(s_gt[ds] + ip_), _MM_HINT_T0);
            _mm_prefetch((const char *)(s_bd + (size_t)ip_ * MAXK), _MM_HINT_T0);
          }
#endif
          u32 i = s_qord[q0 + t];
          const u16 *bd = s_bd + (size_t)i * MAXK;
          u32 R = s_gt[ds][i];
          const u32 key=(R>>12)*key_stride+bd[first_dim];
          u32 *rp = rec + (size_t)(cv[key + 1]++) * RS;
          rp[0] = R;
          for (int e = 0; e < K1; e++) {
            int d = (e < ds) ? e : e + 1;
            rp[1 + e] = g_plen_on ? s_pofs[(size_t)e * MAXQ + bd[d]]
                                  : (u32)(((size_t)e * (Q + 1) + bd[d]) * W);
          }
          rp[KK] = i;
        }
      }
      {
        u32 *acc = s_bandacc;
        for (u32 t = 0; t < nq; t++){ acc[t] = 0; s_bandvec[t]=0; }
        if (nq) {
          const u32 maxR = rec[(size_t)(nq - 1) * RS];
          const u32 maxband = (maxR + 4095u) / 4096u;
          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 zz = _mm256_setzero_si256();
          u32 ps = 0;
          for (u32 bk = 0; bk <= maxband; bk++) {
            const u32 lim = bk * 4096u;
            while (ps < nq && (rec[(size_t)ps * RS] >> 12) < bk) ps++;
            if (ps >= nq) break;
            u32 psp = ps;
            while (psp < nq && (rec[(size_t)psp * RS] >> 12) == bk) psp++;
            const size_t off8 = (size_t)bk * 64u;
            for (u32 t = psp; t < nq; t++) {
              if(t+3<nq){const u32* next=rec+(size_t)(t+3)*RS;
                for(int e=0;e<K1;e++){const u64* pp=s_bs+(size_t)next[1+e]+off8;_mm_prefetch((const char*)pp,_MM_HINT_T0);_mm_prefetch((const char*)(pp+8),_MM_HINT_T0);_mm_prefetch((const char*)(pp+16),_MM_HINT_T0);_mm_prefetch((const char*)(pp+24),_MM_HINT_T0);}
              }
#if MF_APF > 0
              {
                u32 q2_ = t + MF_APF_D;
                if (q2_ < nq) {
                  const u32 *nx_ = rec + (size_t)q2_ * RS;
                  const u32 *pv_ = rec + (size_t)(q2_ - 1) * RS;
#if MF_APF == 1
                  u32 newc_ = ((nx_[0] >> 12) != (pv_[0] >> 12)) || (nx_[1] != pv_[1]);
#else
                  u32 newc_ = ((nx_[0] >> 12) != (pv_[0] >> 12));
#endif
                  if (newc_) {
                    for (int e_ = 0; e_ < K1; e_++) {
                      const char *pp_ = (const char *)(s_bs + (size_t)nx_[1 + e_] + off8);
                      _mm_prefetch(pp_, _MM_HINT_T0);
#if MF_APF_LINES > 1
                      _mm_prefetch(pp_ + 64, _MM_HINT_T0);
#endif
                    }
                  }
                }
              }
#endif
              const u32 *rp = rec + (size_t)t * RS;
              __m256i bytes=zz;
              for (u32 lane=0;lane<64u;lane+=4) {
                const u64 *p0=s_bs+(size_t)rp[1]+off8+lane;
                __m256i a0=LDU256(p0);
                for(int e=1;e<K1;e++)a0=_mm256_and_si256(a0,LDU256(s_bs+(size_t)rp[1+e]+off8+lane));
                __m256i low=_mm256_and_si256(a0,lm),high=_mm256_and_si256(_mm256_srli_epi16(a0,4),lm);
                bytes=_mm256_add_epi8(bytes,_mm256_add_epi8(_mm256_shuffle_epi8(lk,low),_mm256_shuffle_epi8(lk,high)));
              }
              __m256i sum64=_mm256_sad_epu8(bytes,zz);
              __m128i lanes=_mm_add_epi32(_mm256_castsi256_si128(sum64),_mm256_extracti128_si256(sum64,1));
              lanes=_mm_shuffle_epi32(lanes,0x88);
              
              s_bandvec[t] += (u64)_mm_cvtsi128_si64(lanes);
            }
            for (u32 t = ps; t < psp; t++) {
#if MF_APF_PART > 0
              {
                u32 q2_ = t + MF_APF_PD;
                if (q2_ < psp) {
                  const u32 *nx_ = rec + (size_t)q2_ * RS;
                  const u32 *pv_ = rec + (size_t)(q2_ - 1) * RS;
#if MF_APF_PART == 1
                  u32 newc_ = (nx_[1] != pv_[1]);
#else
                  u32 newc_ = 1u;
#endif
                  if (newc_) {
                    for (int e_ = 0; e_ < K1; e_++) {
                      const char *pp_ = (const char *)(s_bs + (size_t)nx_[1 + e_] + off8);
                      _mm_prefetch(pp_, _MM_HINT_T0);
#if MF_APF_PLINES > 1
                      _mm_prefetch(pp_ + 64, _MM_HINT_T0);
#endif
#if MF_APF_PLINES > 2
                      _mm_prefetch(pp_ + 128, _MM_HINT_T0);
                      _mm_prefetch(pp_ + 192, _MM_HINT_T0);
#endif
                    }
                  }
                }
              }
#endif
              const u32 *rp = rec + (size_t)t * RS;
              const u32 R = rp[0];
              const u64 *bp[MAXK];
              for (int e = 0; e < K1; e++) bp[e] = s_bs + (size_t)rp[1 + e] + off8;
              acc[t] += and_popcount_range<KK - 1>(bp, (R >> 6) - (u32)off8, R & 63u);
            }
          }
          for (u32 t = 0; t < nq; t++){
#if MF_PF_OUT > 0
            if (t + MF_PF_OUT_D < nq) {
#if MF_PF_OUT_W
              __asm__ __volatile__("prefetchw %0" :: "m"(*(const char *)(out + rec[(size_t)(t + MF_PF_OUT_D) * RS + KK])));
#else
              _mm_prefetch((const char *)(out + rec[(size_t)(t + MF_PF_OUT_D) * RS + KK]), _MM_HINT_T0);
#endif
            }
#endif
            __m128i lanes=_mm_loadl_epi64((const __m128i*)(s_bandvec+t));out[rec[(size_t)t * RS + KK]] = acc[t]+(u32)_mm_cvtsi128_si32(lanes)+(u32)_mm_extract_epi32(lanes,1);}
        }
      }
    }
}

template<int KK, int D>
__attribute__((target("avx2,popcnt")))
static void fr_dim(u32 N, u32 *out, u32 Q, u32 B, u64 g_wcs, u64 g_wcs_magic) {
  const u32 KS_C = (KK <= 8) ? 8u : (u32)KK;
  const int k = KK;
  u32 *const avpool = s_avpool;
  const u32 last_bucket = (N - 1u) / B;
  const int d = D;

    u32 exmask = 0;
    for (int e = 0; e < d; e++) exmask |= 1u << e;
    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 = LDU256(hiA);
    const __m256i notexloh = LDU256(hiAh);
    const __m256i all8 = _mm256_set1_epi32(-1);
    const u8 *const sbase = s_sig + (size_t)d * k * MF_SPL;
    const u32 *const odd = s_ord[d];
    for (u32 b = 0; b <= last_bucket; ++b) {
      const u32 lo = FPOS(d, b);
      const u32 bend = (b == last_bucket) ? N : FPOS(d, b + 1);
#if MF_SETPF == 2
      {
        for (int e = 0; e < k; e++) {
          if (e == (int)d) continue;
          const char *q = (const char *)(sbase + (size_t)e * MF_SPL + lo);
          _mm_prefetch(q, _MM_HINT_T0);
#if MF_SETPF_LINES > 1
          _mm_prefetch(q + 64, _MM_HINT_T0);
#endif
        }
      }
#endif
      for (u32 r = lo; r < bend; ++r) {
        const u32 i = odd[r];
        if ((int)s_g[i] == d) continue;
#if MF_PFB > 0
        {
          u32 rj = r + MF_PFB;
          if (rj < bend) {
            u32 j = odd[rj];
            _mm_prefetch((const char *)(s_bsig + (size_t)j * 32), _MM_HINT_T0);
            _mm_prefetch((const char *)(s_gt[d] + j), _MM_HINT_T0);
            _mm_prefetch((const char *)(s_g + j), _MM_HINT_T0);
#if MF_PFSOUT
            _mm_prefetch((const char *)(out + j), _MM_HINT_T0);
#endif
          }
        }
#endif
        const u32 hi = s_gt[d][i];
        if (lo >= hi) continue;
        __m256i qT = _mm256_setzero_si256(), qTh = _mm256_setzero_si256();
        int qtd = 0;
        auto build_qt = [&]() {
          if (qtd) return;
          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);
          if constexpr (D == 0) qT = qr_;
          else qT = _mm256_min_epi32(qr_, _mm256_or_si256(LDU256(Avp_), notexlo));
          if (k > 8) {
            __m256i qrh_ = _mm256_or_si256(LDU256((rec_ + 8)), khi8hv);
            if constexpr (D <= 8) qTh = qrh_;
            else qTh = _mm256_min_epi32(qrh_, _mm256_or_si256(LDU256(Avp_ + 8), notexloh));
          } else qTh = all8;
          qtd = 1;
        };
        u32 add = 0;
        const u32 *c0 = s_sdmp[d] ? (s_sdmp[d] + (size_t)lo * KS_C) : 0;
        if (g_use_filt) {
          const u32 *rec = s_pm + (size_t)i * STRIDE;
          build_qt();
          const u32 w0 = lo >> 6;
          const u32 nw = ((hi - 1) >> 6) - w0 + 1;
          u64 *fb = s_fbuf;
          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;
            u64 ne_ = (u64)rec[e];
            u64 q_ = (u64)(((__uint128_t)ne_ * g_wcs_magic) >> 64);
            while (q_ * g_wcs > ne_) --q_;
            u32 c = (u32)q_ + 1;
            if (c > g_Qc) c = g_Qc;
            const u64 *row = s_fbm + ((size_t)(d * k + e) * (g_Qc + 1) + c) * FW + w0;
            if (first) { for (u32 w = 0; w < nw; w++) fb[w] = row[w]; first = 0; }
            else { for (u32 w = 0; 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) * KS_C) : (s_pm + (size_t)s_ord[d][j] * STRIDE);
              __m256i pv = LDU256(cp);
              u32 ok = (u32)_mm256_testc_si256(_mm256_cmpgt_epi32(qT, pv), all8);
              if (k > 8) {
                __m256i pvh = LDU256((cp + 8));
                ok &= (u32)_mm256_testc_si256(_mm256_cmpgt_epi32(qTh, pvh), all8);
              }
              add += ok;
            }
          }
          out[i] += add;
          continue;
        }
        if (g_sig_on && (hi - lo) >= 2) {
          
          
          __m256i qsb[16];
          
          
          
          
          
          
          {
            const __m256i rowv = LDU256((const __m256i *)(s_bsig + (size_t)i * 32));
            const __m128i lo_ = _mm256_castsi256_si128(rowv);
            const __m128i hi_ = _mm256_extracti128_si256(rowv, 1);
            __m128i comb;
            if constexpr (D == 0) comb = hi_;
            else {
              const __m128i msk = _mm_cmpgt_epi8(_mm_set1_epi8((char)D),
                  _mm_setr_epi8(0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15));
              comb = _mm_blendv_epi8(hi_, lo_, msk);
            }
            const __m256i cs = _mm256_broadcastsi128_si256(comb);
            for (int e = 0; e < k; e++) {
              if (e == (int)d) { qsb[e] = _mm256_set1_epi8((char)0xFF); continue; }
              qsb[e] = _mm256_shuffle_epi8(cs, _mm256_set1_epi8((char)e));
            }
          }
          u32 aj = lo, aacc = 0, aexact = 0;
          const int ajlim = (int)hi - 32;
          for (; (int)aj <= ajlim; aj += 32) {
            __m256i ac0=_mm256_setzero_si256(),ac1=ac0;
              for(int e=0;e<k;e++){
                if(e==D)continue;
                __m256i df=_mm256_subs_epu8(LDU256(sbase+(size_t)e*MF_SPL+aj),qsb[e]);
                if(e&1)ac1=_mm256_or_si256(ac1,df);else ac0=_mm256_or_si256(ac0,df);
              }
              __m256i ac=_mm256_or_si256(ac0,ac1);
            __m256i lt = _mm256_cmpeq_epi8(ac, _mm256_setzero_si256());
            u32 ltm = (u32)_mm256_movemask_epi8(lt);
            u32 amb = (u32)_mm256_movemask_epi8(_mm256_cmpeq_epi8(ac,_mm256_set1_epi8(1)));
          aacc += (u32)__builtin_popcount(ltm);
            while (amb) {
              build_qt();
              int t = __builtin_ctz(amb); amb &= amb - 1;
              const u32 *cp = c0 ? (c0 + (size_t)(aj + (u32)t - lo) * KS_C)
                                 : (s_pm + (size_t)odd[aj + (u32)t] * STRIDE);
              __m256i pv = LDU256(cp);
              u32 okl = (u32)_mm256_testc_si256(_mm256_cmpgt_epi32(qT, pv), all8);
              if (k > 8) {
                __m256i pvh = LDU256((cp + 8));
                okl &= (u32)_mm256_testc_si256(_mm256_cmpgt_epi32(qTh, pvh), all8);
              }
              aacc += okl;
            }
          }
          
          
          
          
          
          
          
          
          {
            u32 r = hi - aj;
            if (r) {
              u32 base, lk;
              if (hi >= 32u) { base = hi - 32u; lk = 32u - r; }
              else           { base = 0u;      lk = lo; }        
              u32 kmask = ((r >= 32u) ? 0xFFFFFFFFu : ((1u << r) - 1u)) << lk;
              __m256i ac0=_mm256_setzero_si256(),ac1=ac0;
              for(int e=0;e<k;e++){
                if(e==D)continue;
                __m256i df=_mm256_subs_epu8(LDU256(sbase+(size_t)e*MF_SPL+base),qsb[e]);
                if(e&1)ac1=_mm256_or_si256(ac1,df);else ac0=_mm256_or_si256(ac0,df);
              }
              __m256i ac=_mm256_or_si256(ac0,ac1);
              __m256i lt = _mm256_cmpeq_epi8(ac, _mm256_setzero_si256());
              u32 ltm = ((u32)_mm256_movemask_epi8(lt)) & kmask;
              u32 amb = (u32)_mm256_movemask_epi8(_mm256_cmpeq_epi8(ac,_mm256_set1_epi8(1))) & kmask;
          aacc += (u32)__builtin_popcount(ltm);
              while (amb) {
                build_qt();
                int t = __builtin_ctz(amb); amb &= amb - 1;
                const u32 *cp = c0 ? (c0 + (size_t)(base + (u32)t - lo) * KS_C)
                                 : (s_pm + (size_t)odd[base + (u32)t] * STRIDE);
                __m256i pv = LDU256(cp);
                u32 okl = (u32)_mm256_testc_si256(_mm256_cmpgt_epi32(qT, pv), all8);
                if (k > 8) {
                  __m256i pvh = LDU256((cp + 8));
                  okl &= (u32)_mm256_testc_si256(_mm256_cmpgt_epi32(qTh, pvh), all8);
                }
                aacc += okl;
              }
            }
          }
          add = aacc;
        } else if (c0) {
          build_qt();
          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 = LDU256((c));
              __m256i p1 = LDU256((c + KS_C));
              __m256i p2 = LDU256((c + 2 * KS_C));
              __m256i p3 = LDU256((c + 3 * KS_C));
              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 * KS_C;
            }
            for (; j < hi; j++, c += KS_C) {
              __m256i pv = LDU256(c);
              a0 += (u32)_mm256_testc_si256(_mm256_cmpgt_epi32(qT, pv), all8);
            }
            add = a0 + a1 + a2 + a3;
          } else {
            
            
            
            
            
            const u32 *c = c0;
            u32 a0 = 0, a1 = 0, a2 = 0, a3 = 0;
            u32 j = lo;
            for (; j + 4 <= hi; j += 4) {
              __m256i p0 = LDU256((c));
              __m256i p1 = LDU256((c + KS_C));
              __m256i p2 = LDU256((c + 2 * KS_C));
              __m256i p3 = LDU256((c + 3 * KS_C));
              __m256i h0 = LDU256((c + 8));
              __m256i h1 = LDU256((c + KS_C + 8));
              __m256i h2 = LDU256((c + 2 * KS_C + 8));
              __m256i h3 = LDU256((c + 3 * KS_C + 8));
              a0 += (u32)_mm256_testc_si256(_mm256_cmpgt_epi32(qT, p0), all8) & (u32)_mm256_testc_si256(_mm256_cmpgt_epi32(qTh, h0), all8);
              a1 += (u32)_mm256_testc_si256(_mm256_cmpgt_epi32(qT, p1), all8) & (u32)_mm256_testc_si256(_mm256_cmpgt_epi32(qTh, h1), all8);
              a2 += (u32)_mm256_testc_si256(_mm256_cmpgt_epi32(qT, p2), all8) & (u32)_mm256_testc_si256(_mm256_cmpgt_epi32(qTh, h2), all8);
              a3 += (u32)_mm256_testc_si256(_mm256_cmpgt_epi32(qT, p3), all8) & (u32)_mm256_testc_si256(_mm256_cmpgt_epi32(qTh, h3), all8);
              c += 4 * KS_C;
            }
            for (; j < hi; j++, c += KS_C) {
              __m256i pv = LDU256(c);
              __m256i pvh = LDU256((c + 8));
              a0 += (u32)_mm256_testc_si256(_mm256_cmpgt_epi32(qT, pv), all8) &
                    (u32)_mm256_testc_si256(_mm256_cmpgt_epi32(qTh, pvh), all8);
            }
            add = a0 + a1 + a2 + a3;
          }
        } else {
          build_qt();
          
          
          
          
          
          for (u32 j = lo; j < hi; j++) {
            const u32 *c = s_pm + (size_t)odd[j] * STRIDE;
            __m256i pv = LDA((const __m256i *)c);
            u32 ok = (u32)_mm256_testc_si256(_mm256_cmpgt_epi32(qT, pv), all8);
            if (k > 8) {
              __m256i pvh = LDA((const __m256i *)(c + 8));
              ok &= (u32)_mm256_testc_si256(_mm256_cmpgt_epi32(qTh, pvh), all8);
            }
            add += ok;
          }
        }
        out[i] += add;
      }
    }
  }

template<int KK>
__attribute__((target("avx2,popcnt")))
static void fringe_rank(u32 N, u32 *out, u32 Q, u32 B) {
  const u32 KS_C = (KK <= 8) ? 8u : (u32)KK;
  const int k = KK;
  if (g_skip_fringe) return;
  u32 *const avpool = s_avpool;
  static u64 g_wcs = 0, g_wcs_magic = 0;
  { const u64 w_ = (u64)N / g_Qc + 1; if (w_ != g_wcs) { g_wcs = w_; g_wcs_magic = ~0ULL / w_ + 1; } }
  const u32 last_bucket = (N - 1u) / B;
    if constexpr (KK == 10) {
    fr_dim<KK, 0>(N, out, Q, B, g_wcs, g_wcs_magic);
    fr_dim<KK, 1>(N, out, Q, B, g_wcs, g_wcs_magic);
    fr_dim<KK, 2>(N, out, Q, B, g_wcs, g_wcs_magic);
    fr_dim<KK, 3>(N, out, Q, B, g_wcs, g_wcs_magic);
    fr_dim<KK, 4>(N, out, Q, B, g_wcs, g_wcs_magic);
    fr_dim<KK, 5>(N, out, Q, B, g_wcs, g_wcs_magic);
    fr_dim<KK, 6>(N, out, Q, B, g_wcs, g_wcs_magic);
    fr_dim<KK, 7>(N, out, Q, B, g_wcs, g_wcs_magic);
    fr_dim<KK, 8>(N, out, Q, B, g_wcs, g_wcs_magic);
    fr_dim<KK, 9>(N, out, Q, B, g_wcs, g_wcs_magic);
  } else {
  for (int d = 0; d < k; d++) {
    u32 exmask = 0;
    for (int e = 0; e < d; e++) exmask |= 1u << e;
    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 = LDU256(hiA);
    const __m256i notexloh = LDU256(hiAh);
    const __m256i all8 = _mm256_set1_epi32(-1);
    const u8 *const sbase = s_sig + (size_t)d * k * MF_SPL;
    const u32 *const odd = s_ord[d];
    for (u32 b = 0; b <= last_bucket; ++b) {
      const u32 lo = FPOS(d, b);
      const u32 bend = (b == last_bucket) ? N : FPOS(d, b + 1);
#if MF_SETPF == 2
      {
        for (int e = 0; e < k; e++) {
          if (e == (int)d) continue;
          const char *q = (const char *)(sbase + (size_t)e * MF_SPL + lo);
          _mm_prefetch(q, _MM_HINT_T0);
#if MF_SETPF_LINES > 1
          _mm_prefetch(q + 64, _MM_HINT_T0);
#endif
        }
      }
#endif
      for (u32 r = lo; r < bend; ++r) {
        const u32 i = odd[r];
        if ((int)s_g[i] == d) continue;
#if MF_PFB > 0
        {
          u32 rj = r + MF_PFB;
          if (rj < bend) {
            u32 j = odd[rj];
            _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 *)(avpool + (size_t)j * STRIDE), _MM_HINT_T0);
#if MF_PFSOUT
            _mm_prefetch((const char *)(out + j), _MM_HINT_T0);
#endif
          }
        }
#endif
        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(LDU256(Avp), notexlo);
        __m256i qT = _mm256_min_epi32(qr, Avm);
        __m256i qTh;
        if (k > 8) {
          __m256i qrh = _mm256_or_si256(LDU256((rec + 8)), khi8hv);
          __m256i Avmh = _mm256_or_si256(LDU256((Avp + 8)), notexloh);
          qTh = _mm256_min_epi32(qrh, Avmh);
        } else {
          qTh = all8;
        }
        u32 add = 0;
        const u32 *c0 = s_sdmp[d] ? (s_sdmp[d] + (size_t)lo * KS_C) : 0;
        if (g_use_filt) {
          const u32 w0 = lo >> 6;
          const u32 nw = ((hi - 1) >> 6) - w0 + 1;
          u64 *fb = s_fbuf;
          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;
            u64 ne_ = (u64)rec[e];
            u64 q_ = (u64)(((__uint128_t)ne_ * g_wcs_magic) >> 64);
            while (q_ * g_wcs > ne_) --q_;
            u32 c = (u32)q_ + 1;
            if (c > g_Qc) c = g_Qc;
            const u64 *row = s_fbm + ((size_t)(d * k + e) * (g_Qc + 1) + c) * FW + w0;
            if (first) { for (u32 w = 0; w < nw; w++) fb[w] = row[w]; first = 0; }
            else { for (u32 w = 0; 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) * KS_C) : (s_pm + (size_t)s_ord[d][j] * STRIDE);
              __m256i pv = LDU256(cp);
              u32 ok = (u32)_mm256_testc_si256(_mm256_cmpgt_epi32(qT, pv), all8);
              if (k > 8) {
                __m256i pvh = LDU256((cp + 8));
                ok &= (u32)_mm256_testc_si256(_mm256_cmpgt_epi32(qTh, pvh), all8);
              }
              add += ok;
            }
          }
          out[i] += add;
          continue;
        }
        if (g_sig_on && (hi - lo) >= 2) {
          
          
          __m256i qsb[16];
          
          
          
          
          
          
          {
            const __m256i zero8 = _mm256_setzero_si256();
            const __m256i magv = _mm256_set1_epi32((int)g_sigmag);
            const __m256i capv = _mm256_set1_epi32(255);
            
            
            
            
            const __m256i oneb = _mm256_set1_epi8(1);
            const __m256i sv_lo = _mm256_subs_epu8(_mm256_min_epu32(_mm256_blend_epi32(
                _mm256_srli_epi64(_mm256_mul_epu32(qT, magv), 32),
                _mm256_slli_epi64(_mm256_srli_epi64(_mm256_mul_epu32(_mm256_srli_epi64(qT, 32), magv), 32), 32), 0xAA), capv), oneb);
            const __m256i sv_hi = _mm256_subs_epu8(_mm256_min_epu32(_mm256_blend_epi32(
                _mm256_srli_epi64(_mm256_mul_epu32(qTh, magv), 32),
                _mm256_slli_epi64(_mm256_srli_epi64(_mm256_mul_epu32(_mm256_srli_epi64(qTh, 32), magv), 32), 32), 0xAA), capv), oneb);
            const __m256i sl_lo = _mm256_permute2x128_si256(sv_lo, sv_lo, 0x00);
            const __m256i sl_hi = _mm256_permute2x128_si256(sv_lo, sv_lo, 0x11);
            const __m256i sh_lo = _mm256_permute2x128_si256(sv_hi, sv_hi, 0x00);
            for (int e = 0; e < k; e++) {
              if (e == (int)d) { qsb[e] = _mm256_set1_epi8((char)0xFF); continue; }
              const __m256i src = (e < 4) ? sl_lo : ((e < 8) ? sl_hi : sh_lo);
              qsb[e] = _mm256_shuffle_epi8(src,_mm256_load_si256((const __m256i*)mask4[e&3]));
            }
          }
          u32 aj = lo, aacc = 0, aexact = 0;
          const int ajlim = (int)hi - 32;
          for (; (int)aj <= ajlim; aj += 32) {
            __m256i ac = _mm256_setzero_si256();
            {    


 
              for (int e = 0; e < k; e++) {
                if (e == (int)d) continue;
                __m256i cv = LDU256((sbase + (size_t)e * MF_SPL + aj));
                ac = _mm256_or_si256(ac, _mm256_subs_epu8(cv, qsb[e]));
              }
            }
            __m256i lt = _mm256_cmpeq_epi8(ac, _mm256_setzero_si256());
            u32 ltm = (u32)_mm256_movemask_epi8(lt);
            u32 amb = (u32)_mm256_movemask_epi8(_mm256_cmpeq_epi8(ac,_mm256_set1_epi8(1)));
          aacc += (u32)__builtin_popcount(ltm & ~amb);
            while (amb) {
              int t = __builtin_ctz(amb); amb &= amb - 1;
              const u32 *cp = c0 ? (c0 + (size_t)(aj + (u32)t - lo) * KS_C)
                                 : (s_pm + (size_t)odd[aj + (u32)t] * STRIDE);
              __m256i pv = LDU256(cp);
              u32 okl = (u32)_mm256_testc_si256(_mm256_cmpgt_epi32(qT, pv), all8);
              if (k > 8) {
                __m256i pvh = LDU256((cp + 8));
                okl &= (u32)_mm256_testc_si256(_mm256_cmpgt_epi32(qTh, pvh), all8);
              }
              aacc += okl;
            }
          }
          
          
          
          
          
          
          
          
          {
            u32 r = hi - aj;
            if (r) {
              u32 base, lk;
              if (hi >= 32u) { base = hi - 32u; lk = 32u - r; }
              else           { base = 0u;      lk = lo; }        
              u32 kmask = ((r >= 32u) ? 0xFFFFFFFFu : ((1u << r) - 1u)) << lk;
              __m256i ac = _mm256_setzero_si256();
              {     
                for (int e = 0; e < k; e++) {
                  if (e == (int)d) continue;
                  __m256i cv = LDU256((sbase + (size_t)e * MF_SPL + base));
                  ac = _mm256_or_si256(ac, _mm256_subs_epu8(cv, qsb[e]));
                }
              }
              __m256i lt = _mm256_cmpeq_epi8(ac, _mm256_setzero_si256());
              u32 ltm = ((u32)_mm256_movemask_epi8(lt)) & kmask;
              u32 amb = (u32)_mm256_movemask_epi8(_mm256_cmpeq_epi8(ac,_mm256_set1_epi8(1))) & kmask;
          aacc += (u32)__builtin_popcount(ltm & ~amb);
              while (amb) {
                int t = __builtin_ctz(amb); amb &= amb - 1;
                const u32 *cp = c0 ? (c0 + (size_t)(base + (u32)t - lo) * KS_C)
                                 : (s_pm + (size_t)odd[base + (u32)t] * STRIDE);
                __m256i pv = LDU256(cp);
                u32 okl = (u32)_mm256_testc_si256(_mm256_cmpgt_epi32(qT, pv), all8);
                if (k > 8) {
                  __m256i pvh = LDU256((cp + 8));
                  okl &= (u32)_mm256_testc_si256(_mm256_cmpgt_epi32(qTh, pvh), all8);
                }
                aacc += okl;
              }
            }
          }
          add = aacc;
        } else 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 = LDU256((c));
              __m256i p1 = LDU256((c + KS_C));
              __m256i p2 = LDU256((c + 2 * KS_C));
              __m256i p3 = LDU256((c + 3 * KS_C));
              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 * KS_C;
            }
            for (; j < hi; j++, c += KS_C) {
              __m256i pv = LDU256(c);
              a0 += (u32)_mm256_testc_si256(_mm256_cmpgt_epi32(qT, pv), all8);
            }
            add = a0 + a1 + a2 + a3;
          } else {
            
            
            
            
            
            const u32 *c = c0;
            u32 a0 = 0, a1 = 0, a2 = 0, a3 = 0;
            u32 j = lo;
            for (; j + 4 <= hi; j += 4) {
              __m256i p0 = LDU256((c));
              __m256i p1 = LDU256((c + KS_C));
              __m256i p2 = LDU256((c + 2 * KS_C));
              __m256i p3 = LDU256((c + 3 * KS_C));
              __m256i h0 = LDU256((c + 8));
              __m256i h1 = LDU256((c + KS_C + 8));
              __m256i h2 = LDU256((c + 2 * KS_C + 8));
              __m256i h3 = LDU256((c + 3 * KS_C + 8));
              a0 += (u32)_mm256_testc_si256(_mm256_cmpgt_epi32(qT, p0), all8) & (u32)_mm256_testc_si256(_mm256_cmpgt_epi32(qTh, h0), all8);
              a1 += (u32)_mm256_testc_si256(_mm256_cmpgt_epi32(qT, p1), all8) & (u32)_mm256_testc_si256(_mm256_cmpgt_epi32(qTh, h1), all8);
              a2 += (u32)_mm256_testc_si256(_mm256_cmpgt_epi32(qT, p2), all8) & (u32)_mm256_testc_si256(_mm256_cmpgt_epi32(qTh, h2), all8);
              a3 += (u32)_mm256_testc_si256(_mm256_cmpgt_epi32(qT, p3), all8) & (u32)_mm256_testc_si256(_mm256_cmpgt_epi32(qTh, h3), all8);
              c += 4 * KS_C;
            }
            for (; j < hi; j++, c += KS_C) {
              __m256i pv = LDU256(c);
              __m256i pvh = LDU256((c + 8));
              a0 += (u32)_mm256_testc_si256(_mm256_cmpgt_epi32(qT, pv), all8) &
                    (u32)_mm256_testc_si256(_mm256_cmpgt_epi32(qTh, pvh), all8);
            }
            add = a0 + a1 + a2 + a3;
          }
        } else {
          
          
          
          
          
          for (u32 j = lo; j < hi; j++) {
            const u32 *c = s_pm + (size_t)odd[j] * STRIDE;
            __m256i pv = LDA((const __m256i *)c);
            u32 ok = (u32)_mm256_testc_si256(_mm256_cmpgt_epi32(qT, pv), all8);
            if (k > 8) {
              __m256i pvh = LDA((const __m256i *)(c + 8));
              ok &= (u32)_mm256_testc_si256(_mm256_cmpgt_epi32(qTh, pvh), all8);
            }
            add += ok;
          }
        }
        out[i] += add;
      }
    }
    }
  }
}

template<int K>
__attribute__((target("avx2")))
static void solve_mf(u32 N, const unsigned **x, u32 *out) {
  const int k = K;
  
  
  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) {
    
    
    u64 r = (u64)N, s2 = 0;
    while (s2 * s2 < r) s2++;                       
    
    
    u64 overhead = (u64)(5 * k + 21) * N * 4;
    overhead += (u64)k * k * 4 * N;
    
    
    
    
    
    
    overhead += (u64)k * k * (u64)MF_SPL;
    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;
    
    
#if MF_PLEN
    static const u32 kfill[7] = {72, 67, 62, 58, 54, 50, 46};   
    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};  
    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;   
#endif
#if MF_WTRIM
  u32 Wpool = W;   
#else
  const u32 Wpool = W;
#endif
#if MF_FILTAUTO
  if (!g_use_filt && B >= 400) g_use_filt = 1;   
#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
  {
    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 = LDU256(lo8);
    khi8hv = LDU256(hi8);
  }
  const int K1 = k - 1;
  
  for (int d = 0; d < k; d++) {
    const unsigned *xd = x[d];
    u32 *od = s_ord[d];
    
    
    
    
    
    
    
    static u8 compact_hist[MAXN+1] __attribute__((aligned(64)));
    for(u32 i=0;i<=N;i++)compact_hist[i]=0;
    for(u32 i=0;i<N;i++){u32 v=xd[i];++compact_hist[v<N?v:0];}
    u32 sum=0;
    for(u32 i=0;i<=N;i++){u32 c=compact_hist[i];s_tmp[i]=sum;sum+=c;}
    if(sum==N && N<(1u<<20)) {
      u32 *gt=s_gt[d],*pp=s_pos[d];
      u16 *col=(u16*)(s_posp+2u*(size_t)MAXN)+(size_t)d*((size_t)N+64);
      for(u32 i=0;i<N;i++){
        if(i+16<N){u32 vn=xd[i+16];if(vn>=N)vn=0;u32 w=s_tmp[vn];
          _mm_prefetch((const char*)(s_tmp+vn),_MM_HINT_T0);
          _mm_prefetch((const char*)(od+(w&0xfffffu)+(w>>20)),_MM_HINT_T0);}
        u32 v=xd[i];if(v>=N)v=0;
        u32 h=s_tmp[v],start=h&0xfffffu,q=start+(h>>20);
        s_tmp[v]=h+(1u<<20);
        u32 num=(s_tmp[v+1]&0xfffffu)-1u;if(num>N)num=N;
        od[q]=i;gt[i]=start;
        u32 bb=(u32)(((u64)num*bmag)>>40);
        col[i]=(u16)(bb>Q-1?Q-1:bb);
        pp[i]=q;
      }
    } else {
    u32 *PR = s_posp;                       
    for (u32 i = 0; i <= N; i++) PR[2u * i] = 0;
    for (u32 i = 0; i < N; i++) {
      if (i + 24 < N) { u32 vf = xd[i + 24]; if (vf >= N) vf = 0;
        __asm__ __volatile__("prefetchw %0" :: "m"(*(const char *)(PR + 2u * vf))); }
      u32 v = xd[i]; PR[2u * (v < N ? v : 0)]++;
    }
    { u32 s = 0; for (u32 i = 0; i <= N; i++) { u32 c = PR[2u * i]; PR[2u * i] = s; PR[2u * i + 1] = s; s += c; } }
    {
      u32 *gt = s_gt[d];
      u32 *pp = s_pos[d];
      u16 *col = (u16 *)(s_posp + 2u * (size_t)MAXN) + (size_t)d * ((size_t)N + 64);
      for (u32 i = 0; i < N; i++) {
        u32 v = xd[i]; if (v >= N) v = 0;
        u32 c0 = PR[2u * v];
        u32 q = PR[2u * v + 1]++;
        u32 num = PR[2u * (v + 1)] - 1u; if (num > N) num = N;
        od[q] = i;
        gt[i] = c0;
        u32 bb = (u32)(((u64)num * bmag) >> 40);
        col[i] = (u16)(bb > Q - 1 ? Q - 1 : bb);
        pp[i] = q;
      } }
    }
    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;
  }
  
  
  
  {
    u32 nn = N - 1; u32 sh = 0;
    while (nn > 255u) { nn >>= 1; sh++; }        
    g_sigsh = sh;
    { u32 dn = (N > 1) ? (N - 1) : 1u;            
      g_sigmag = (u32)((((u64)255 << 32) / (u64)dn) + 1u); }
    size_t need = (size_t)k * k * MF_SPL;
    static u8 *sigpool = 0; static size_t sigcap = 0;
    if ((size_t)N + 32 > MF_SPL) { sigpool = 0; sigcap = 0; }   
    
    
    if (need > ((size_t)220 << 20)) { sigpool = 0; sigcap = 0; }
    else if (need > sigcap) { sigpool = (u8 *)pool_alloc(need); sigcap = sigpool ? need : 0; }
    if (sigpool) {
      s_sig = sigpool;
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      g_sig_on = 1;
      g_sig_merged = 0;
    }
  }
  {
    const u16 *bcol = (const u16 *)(s_posp + 2u * (size_t)MAXN);
    const size_t cstride = (size_t)N + 64;
    for (u32 i = 0; i < N; i++) {
      u16 *row = s_bd + (size_t)i * MAXK;
      for (int dd = 0; dd < k; dd++) row[dd] = bcol[(size_t)dd * cstride + i];
    }
  }
  if (s_sig) {
    u8 *sr = s_sigrec;
    u32 i=0;
    if(k==10){
const __m256i multiplier=_mm256_set1_epi32((int)g_sigmag),one=_mm256_set1_epi32(1);
const __m128i pick=_mm_setr_epi8(0,4,8,12,-128,-128,-128,-128,-128,-128,-128,-128,-128,-128,-128,-128);
const __m256i pick2=_mm256_broadcastsi128_si256(pick);
for(;i+8<=N;i+=8){
const __m256i r0=LDU256(x[0]+i);
const __m256i r1=LDU256(x[1]+i);
const __m256i r2=LDU256(x[2]+i);
const __m256i r3=LDU256(x[3]+i);
const __m256i r4=LDU256(x[4]+i);
const __m256i r5=LDU256(x[5]+i);
const __m256i r6=LDU256(x[6]+i);
const __m256i r7=LDU256(x[7]+i);
const __m256i t0=_mm256_unpacklo_epi32(r0,r1);
const __m256i t1=_mm256_unpackhi_epi32(r0,r1);
const __m256i t2=_mm256_unpacklo_epi32(r2,r3);
const __m256i t3=_mm256_unpackhi_epi32(r2,r3);
const __m256i t4=_mm256_unpacklo_epi32(r4,r5);
const __m256i t5=_mm256_unpackhi_epi32(r4,r5);
const __m256i t6=_mm256_unpacklo_epi32(r6,r7);
const __m256i t7=_mm256_unpackhi_epi32(r6,r7);
const __m256i u0=_mm256_unpacklo_epi64(t0,t2);
const __m256i u1=_mm256_unpackhi_epi64(t0,t2);
const __m256i u2=_mm256_unpacklo_epi64(t1,t3);
const __m256i u3=_mm256_unpackhi_epi64(t1,t3);
const __m256i u4=_mm256_unpacklo_epi64(t4,t6);
const __m256i u5=_mm256_unpackhi_epi64(t4,t6);
const __m256i u6=_mm256_unpacklo_epi64(t5,t7);
const __m256i u7=_mm256_unpackhi_epi64(t5,t7);
{
const __m256i v=_mm256_permute2x128_si256(u0,u4,0x20);
u32*p=s_pm+(size_t)(i+0)*STRIDE;
u8*q=sr+(size_t)(i+0)*16;
_mm256_store_si256((__m256i*)p,v);
const __m256i e=_mm256_srli_epi64(_mm256_mul_epu32(v,multiplier),32);
const __m256i o=_mm256_mul_epu32(_mm256_srli_epi64(v,32),multiplier);
const __m256i z=_mm256_shuffle_epi8(_mm256_max_epu32(_mm256_blend_epi32(e,o,0xaa),one),pick2);
_mm_storel_epi64((__m128i*)q,_mm_unpacklo_epi32(_mm256_castsi256_si128(z),_mm256_extracti128_si256(z,1)));

}
{
const __m256i v=_mm256_permute2x128_si256(u1,u5,0x20);
u32*p=s_pm+(size_t)(i+1)*STRIDE;
u8*q=sr+(size_t)(i+1)*16;
_mm256_store_si256((__m256i*)p,v);
const __m256i e=_mm256_srli_epi64(_mm256_mul_epu32(v,multiplier),32);
const __m256i o=_mm256_mul_epu32(_mm256_srli_epi64(v,32),multiplier);
const __m256i z=_mm256_shuffle_epi8(_mm256_max_epu32(_mm256_blend_epi32(e,o,0xaa),one),pick2);
_mm_storel_epi64((__m128i*)q,_mm_unpacklo_epi32(_mm256_castsi256_si128(z),_mm256_extracti128_si256(z,1)));

}
{
const __m256i v=_mm256_permute2x128_si256(u2,u6,0x20);
u32*p=s_pm+(size_t)(i+2)*STRIDE;
u8*q=sr+(size_t)(i+2)*16;
_mm256_store_si256((__m256i*)p,v);
const __m256i e=_mm256_srli_epi64(_mm256_mul_epu32(v,multiplier),32);
const __m256i o=_mm256_mul_epu32(_mm256_srli_epi64(v,32),multiplier);
const __m256i z=_mm256_shuffle_epi8(_mm256_max_epu32(_mm256_blend_epi32(e,o,0xaa),one),pick2);
_mm_storel_epi64((__m128i*)q,_mm_unpacklo_epi32(_mm256_castsi256_si128(z),_mm256_extracti128_si256(z,1)));

}
{
const __m256i v=_mm256_permute2x128_si256(u3,u7,0x20);
u32*p=s_pm+(size_t)(i+3)*STRIDE;
u8*q=sr+(size_t)(i+3)*16;
_mm256_store_si256((__m256i*)p,v);
const __m256i e=_mm256_srli_epi64(_mm256_mul_epu32(v,multiplier),32);
const __m256i o=_mm256_mul_epu32(_mm256_srli_epi64(v,32),multiplier);
const __m256i z=_mm256_shuffle_epi8(_mm256_max_epu32(_mm256_blend_epi32(e,o,0xaa),one),pick2);
_mm_storel_epi64((__m128i*)q,_mm_unpacklo_epi32(_mm256_castsi256_si128(z),_mm256_extracti128_si256(z,1)));

}
{
const __m256i v=_mm256_permute2x128_si256(u0,u4,0x31);
u32*p=s_pm+(size_t)(i+4)*STRIDE;
u8*q=sr+(size_t)(i+4)*16;
_mm256_store_si256((__m256i*)p,v);
const __m256i e=_mm256_srli_epi64(_mm256_mul_epu32(v,multiplier),32);
const __m256i o=_mm256_mul_epu32(_mm256_srli_epi64(v,32),multiplier);
const __m256i z=_mm256_shuffle_epi8(_mm256_max_epu32(_mm256_blend_epi32(e,o,0xaa),one),pick2);
_mm_storel_epi64((__m128i*)q,_mm_unpacklo_epi32(_mm256_castsi256_si128(z),_mm256_extracti128_si256(z,1)));

}
{
const __m256i v=_mm256_permute2x128_si256(u1,u5,0x31);
u32*p=s_pm+(size_t)(i+5)*STRIDE;
u8*q=sr+(size_t)(i+5)*16;
_mm256_store_si256((__m256i*)p,v);
const __m256i e=_mm256_srli_epi64(_mm256_mul_epu32(v,multiplier),32);
const __m256i o=_mm256_mul_epu32(_mm256_srli_epi64(v,32),multiplier);
const __m256i z=_mm256_shuffle_epi8(_mm256_max_epu32(_mm256_blend_epi32(e,o,0xaa),one),pick2);
_mm_storel_epi64((__m128i*)q,_mm_unpacklo_epi32(_mm256_castsi256_si128(z),_mm256_extracti128_si256(z,1)));

}
{
const __m256i v=_mm256_permute2x128_si256(u2,u6,0x31);
u32*p=s_pm+(size_t)(i+6)*STRIDE;
u8*q=sr+(size_t)(i+6)*16;
_mm256_store_si256((__m256i*)p,v);
const __m256i e=_mm256_srli_epi64(_mm256_mul_epu32(v,multiplier),32);
const __m256i o=_mm256_mul_epu32(_mm256_srli_epi64(v,32),multiplier);
const __m256i z=_mm256_shuffle_epi8(_mm256_max_epu32(_mm256_blend_epi32(e,o,0xaa),one),pick2);
_mm_storel_epi64((__m128i*)q,_mm_unpacklo_epi32(_mm256_castsi256_si128(z),_mm256_extracti128_si256(z,1)));

}
{
const __m256i v=_mm256_permute2x128_si256(u3,u7,0x31);
u32*p=s_pm+(size_t)(i+7)*STRIDE;
u8*q=sr+(size_t)(i+7)*16;
_mm256_store_si256((__m256i*)p,v);
const __m256i e=_mm256_srli_epi64(_mm256_mul_epu32(v,multiplier),32);
const __m256i o=_mm256_mul_epu32(_mm256_srli_epi64(v,32),multiplier);
const __m256i z=_mm256_shuffle_epi8(_mm256_max_epu32(_mm256_blend_epi32(e,o,0xaa),one),pick2);
_mm_storel_epi64((__m128i*)q,_mm_unpacklo_epi32(_mm256_castsi256_si128(z),_mm256_extracti128_si256(z,1)));

}
const __m256i h8=LDU256(x[8]+i),h9=LDU256(x[9]+i);
const __m256i hp0=_mm256_unpacklo_epi32(h8,h9),hp1=_mm256_unpackhi_epi32(h8,h9);
const __m256i he8=_mm256_srli_epi64(_mm256_mul_epu32(h8,multiplier),32);
const __m256i ho8=_mm256_mul_epu32(_mm256_srli_epi64(h8,32),multiplier);
const __m256i hz8=_mm256_shuffle_epi8(_mm256_max_epu32(_mm256_blend_epi32(he8,ho8,0xaa),one),pick2);
const __m128i hb8=_mm_unpacklo_epi32(_mm256_castsi256_si128(hz8),_mm256_extracti128_si256(hz8,1));
const __m256i he9=_mm256_srli_epi64(_mm256_mul_epu32(h9,multiplier),32);
const __m256i ho9=_mm256_mul_epu32(_mm256_srli_epi64(h9,32),multiplier);
const __m256i hz9=_mm256_shuffle_epi8(_mm256_max_epu32(_mm256_blend_epi32(he9,ho9,0xaa),one),pick2);
const __m128i hb9=_mm_unpacklo_epi32(_mm256_castsi256_si128(hz9),_mm256_extracti128_si256(hz9,1));
const __m128i hbytes=_mm_unpacklo_epi8(hb8,hb9);
_mm_storel_epi64((__m128i*)(s_pm+(size_t)(i+0)*STRIDE+8),_mm256_castsi256_si128(hp0));
*(unsigned short*)(sr+(size_t)(i+0)*16+8)=(unsigned short)_mm_extract_epi16(hbytes,0);
_mm_storel_epi64((__m128i*)(s_pm+(size_t)(i+1)*STRIDE+8),_mm_srli_si128(_mm256_castsi256_si128(hp0),8));
*(unsigned short*)(sr+(size_t)(i+1)*16+8)=(unsigned short)_mm_extract_epi16(hbytes,1);
_mm_storel_epi64((__m128i*)(s_pm+(size_t)(i+2)*STRIDE+8),_mm256_castsi256_si128(hp1));
*(unsigned short*)(sr+(size_t)(i+2)*16+8)=(unsigned short)_mm_extract_epi16(hbytes,2);
_mm_storel_epi64((__m128i*)(s_pm+(size_t)(i+3)*STRIDE+8),_mm_srli_si128(_mm256_castsi256_si128(hp1),8));
*(unsigned short*)(sr+(size_t)(i+3)*16+8)=(unsigned short)_mm_extract_epi16(hbytes,3);
_mm_storel_epi64((__m128i*)(s_pm+(size_t)(i+4)*STRIDE+8),_mm256_extracti128_si256(hp0,1));
*(unsigned short*)(sr+(size_t)(i+4)*16+8)=(unsigned short)_mm_extract_epi16(hbytes,4);
_mm_storel_epi64((__m128i*)(s_pm+(size_t)(i+5)*STRIDE+8),_mm_srli_si128(_mm256_extracti128_si256(hp0,1),8));
*(unsigned short*)(sr+(size_t)(i+5)*16+8)=(unsigned short)_mm_extract_epi16(hbytes,5);
_mm_storel_epi64((__m128i*)(s_pm+(size_t)(i+6)*STRIDE+8),_mm256_extracti128_si256(hp1,1));
*(unsigned short*)(sr+(size_t)(i+6)*16+8)=(unsigned short)_mm_extract_epi16(hbytes,6);
_mm_storel_epi64((__m128i*)(s_pm+(size_t)(i+7)*STRIDE+8),_mm_srli_si128(_mm256_extracti128_si256(hp1,1),8));
*(unsigned short*)(sr+(size_t)(i+7)*16+8)=(unsigned short)_mm_extract_epi16(hbytes,7);
}
}
    for (; i < N; i++) {
      u32 *p = s_pm + (size_t)i * STRIDE;
      for (int d = 0; d < k; d++) {
        u32 v = x[d][i];
        p[d] = v;
        {u32 sv=(u32)(((u64)v*g_sigmag)>>32);sr[(size_t)i*16+d]=(u8)(sv?sv:1u);}
      }
    }
  } else {
  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];
  }
  }
  
  
  
#if MF_NOFILTB
  g_use_filt = 0;
#endif
#if MF_FB3
  if (g_use_filt) {
    u32 Qc = g_Qc;
    u64 wc = (u64)N / Qc + 1;
    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 = (u64)c * wc;
        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
      
      
      
      
      
      
      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
      for (int d = 0; d < k; d++) {
        for (int e = 0; e < k; e++) {
          if (e == d) continue;
          const unsigned *xe = x[e];
          const u32 *pe = s_ord[e];
          const u32 *pd = s_pos[d];
          u64 *base = s_fbm + (size_t)(d * k + e) * (Qc + 1) * W;
          for (u32 w = 0; w < W; w++) base[w] = 0;
          u64 *cur = s_acc;
          for (u32 w = 0; w < W; w++) cur[w] = 0;
          u32 ptr = 0;
          for (u32 c = 1; c <= Qc; c++) {
#if MF_FB3
            u32 pend = s_bnde[(size_t)e * (MAXQ + 2) + c];
#else
            u64 v = (u64)c * wc;
            if (v > 0xFFFFFFFFull) v = 0xFFFFFFFFull;
            u32 pend = ptr;
            { const unsigned *xe_ = xe; const u32 *pe_ = pe;
              while (pend < N && (u64)xe_[pe_[pend]] < v) pend++; }
#endif
            while (ptr < pend) { u32 q = pd[pe[ptr]]; cur[q >> 6] |= 1ull << (q & 63); ptr++; }
            u64 *dst = base + (size_t)c * W;
#if MF_NT
            { u32 w = 0;
              for (; w + 4 <= W; w += 4) _mm256_stream_si256((__m256i *)(dst + w), LDA((const __m256i *)(cur + w)));
              for (; w < W; w++) dst[w] = cur[w]; }
#else
            for (u32 w = 0; w < W; w++) dst[w] = cur[w];
#endif
          }
        }
      }
#endif
    } else { g_use_filt = 0; }
    _mm_sfence();
  }

  
  {
    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;
  }
  u32 gstart[MAXK + 2] = {};
  if (k == 10) {
    for (u32 a13blk = 0; a13blk < N; a13blk += 8) {
      const u32 a13end = (a13blk + 8 < N) ? a13blk + 8 : N;
      alignas(32) u32 fold8[8];
      if (a13end - a13blk == 8) {
        __m256i a13v = _mm256_slli_epi32(_mm256_loadu_si256((const __m256i *)(s_gt[0] + a13blk)), 4);
        for (unsigned a13d = 1; a13d < 10u; ++a13d) {
          __m256i a13q = _mm256_or_si256(_mm256_slli_epi32(_mm256_loadu_si256((const __m256i *)(s_gt[a13d] + a13blk)), 4), _mm256_set1_epi32((int)a13d));
          a13v = _mm256_min_epu32(a13v, a13q);
        }
        _mm256_store_si256((__m256i *)fold8, a13v);
      } else {
        for (u32 a13i = a13blk; a13i < a13end; ++a13i) {
          u32 a13v = s_gt[0][a13i] << 4;
          for (int a13d = 1; a13d < 10; ++a13d) { u32 a13q = (s_gt[a13d][a13i] << 4) | (u32)a13d; if (a13q < a13v) a13v = a13q; }
          fold8[a13i - a13blk] = a13v;
        }
      }
#pragma GCC unroll 1
      for (u32 i = a13blk; i < a13end; ++i) {
        const u32 best = fold8[i - a13blk] >> 4;
        const int ds = (int)(fold8[i - a13blk] & 15u);
        ++gstart[ds + 1];
    s_g[i] = (u32)ds;
    
    
    
    const u16 *bdv = s_bd + (size_t)i * MAXK;
    u32 *av = s_avpool + (size_t)i * STRIDE;
    u8 *bsg = s_bsig + (size_t)i * 32;
    { __m128i qs_ = _mm_loadu_si128((const __m128i *)(s_sigrec + (size_t)i * 16));
      _mm_storeu_si128((__m128i *)(bsg + 16), _mm_subs_epu8(qs_, _mm_set1_epi8(1))); }
    u32 *mr = s_maxr + ((size_t)ds * MAXK) * MAXQ;
    for (int d = 0; d < k; d++) {
      if (d == ds) { av[d] = 0x7FFFFFFFu; bsg[d] = (u8)(s_sigrec[(size_t)i * 16 + ds] - 1u); continue; }
      u32 b = bdv[d];
      u32 bv = BVAL(d, b);
      av[d] = bv;
      { u32 sv_ = (u32)(((u64)bv * g_sigmag) >> 32); bsg[d] = (u8)((sv_ ? sv_ : 1u) - 1u); }
      if (g_plen_on && best > mr[(size_t)d * MAXQ + b])
        mr[(size_t)d * MAXQ + b] = best;
    }
      }
    }
    for (unsigned a13d = 1; a13d <= (unsigned)(k + 1); ++a13d) gstart[a13d] += gstart[a13d - 1];
    { u32 fold_write[MAXK]; for (int a13d = 0; a13d < k; ++a13d) fold_write[a13d] = gstart[a13d];
      for (u32 i = 0; i < N; ++i) s_qord[fold_write[s_g[i]]++] = i; }
  } else {
  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 * MAXK;
    u32 *av = s_avpool + (size_t)i * STRIDE;
    u8 *bsg = s_bsig + (size_t)i * 32;
    { __m128i qs_ = _mm_loadu_si128((const __m128i *)(s_sigrec + (size_t)i * 16));
      _mm_storeu_si128((__m128i *)(bsg + 16), _mm_subs_epu8(qs_, _mm_set1_epi8(1))); }
    u32 *mr = s_maxr + ((size_t)ds * MAXK) * MAXQ;
    for (int d = 0; d < k; d++) {
      if (d == ds) { av[d] = 0x7FFFFFFFu; bsg[d] = (u8)(s_sigrec[(size_t)i * 16 + ds] - 1u); continue; }
      u32 b = bdv[d];
      u32 bv = BVAL(d, b);
      av[d] = bv;
      { u32 sv_ = (u32)(((u64)bv * g_sigmag) >> 32); bsg[d] = (u8)((sv_ ? sv_ : 1u) - 1u); }
      if (g_plen_on && best > mr[(size_t)d * MAXQ + b])
        mr[(size_t)d * MAXQ + b] = best;
    }
  }
  for (u32 i = 0; i < N; i++) s_qord[i] = i;
  
  {
    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 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; }
  }
  
  
  
#if MF_NOSDMP
  g_nosdmp = 1;
#endif
  if (!g_nosdmp) {
    u32 KS = k;
#if MF_KSPAD
    if (k <= 8) KS = 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;   
    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] = 0;   
          const u32 *od = s_ord[d];
          if (s_sig) {
            u8 *sb = s_sig + (size_t)d * k * MF_SPL;
            u32 j = 0;
            if (k == 10) {
              for (; j + 8 <= N; j += 8) {
#if MF_PF_SIG > 0
                if (j + 16 + MF_PF_SIG_D <= N) {
                  for (int u_ = 0; u_ < 8; u_++)
                    _mm_prefetch((const char *)(s_sigrec + (size_t)od[j + 8 + MF_PF_SIG_D + u_] * 16), _MM_HINT_T0);
                }
#endif
                __m128i r0 = _mm_loadu_si128((const __m128i *)(s_sigrec + (size_t)od[j+0] * 16));
                __m128i r1 = _mm_loadu_si128((const __m128i *)(s_sigrec + (size_t)od[j+1] * 16));
                __m128i r2 = _mm_loadu_si128((const __m128i *)(s_sigrec + (size_t)od[j+2] * 16));
                __m128i r3 = _mm_loadu_si128((const __m128i *)(s_sigrec + (size_t)od[j+3] * 16));
                __m128i r4 = _mm_loadu_si128((const __m128i *)(s_sigrec + (size_t)od[j+4] * 16));
                __m128i r5 = _mm_loadu_si128((const __m128i *)(s_sigrec + (size_t)od[j+5] * 16));
                __m128i r6 = _mm_loadu_si128((const __m128i *)(s_sigrec + (size_t)od[j+6] * 16));
                __m128i r7 = _mm_loadu_si128((const __m128i *)(s_sigrec + (size_t)od[j+7] * 16));
                __m128i t0 = _mm_unpacklo_epi8(r0,r1);
                __m128i t1 = _mm_unpacklo_epi8(r2,r3);
                __m128i t2 = _mm_unpacklo_epi8(r4,r5);
                __m128i t3 = _mm_unpacklo_epi8(r6,r7);
                __m128i u0 = _mm_unpacklo_epi16(t0,t1);
                __m128i u1 = _mm_unpackhi_epi16(t0,t1);
                __m128i u2 = _mm_unpacklo_epi16(t2,t3);
                __m128i u3 = _mm_unpackhi_epi16(t2,t3);
                __m128i v0 = _mm_unpacklo_epi32(u0,u2);
                __m128i v1 = _mm_unpackhi_epi32(u0,u2);
                __m128i v2 = _mm_unpacklo_epi32(u1,u3);
                __m128i v3 = _mm_unpackhi_epi32(u1,u3);
                __m128i h0 = _mm_unpackhi_epi8(r0,r1);
                __m128i h1 = _mm_unpackhi_epi8(r2,r3);
                __m128i h2 = _mm_unpackhi_epi8(r4,r5);
                __m128i h3 = _mm_unpackhi_epi8(r6,r7);
                __m128i hu0 = _mm_unpacklo_epi16(h0,h1);
                __m128i hu1 = _mm_unpacklo_epi16(h2,h3);
                __m128i v4 = _mm_unpacklo_epi32(hu0,hu1);
                _mm_storel_epi64((__m128i *)(sb + (size_t)0 * MF_SPL + j),v0);
                _mm_storel_epi64((__m128i *)(sb + (size_t)1 * MF_SPL + j),_mm_srli_si128(v0,8));
                _mm_storel_epi64((__m128i *)(sb + (size_t)2 * MF_SPL + j),v1);
                _mm_storel_epi64((__m128i *)(sb + (size_t)3 * MF_SPL + j),_mm_srli_si128(v1,8));
                _mm_storel_epi64((__m128i *)(sb + (size_t)4 * MF_SPL + j),v2);
                _mm_storel_epi64((__m128i *)(sb + (size_t)5 * MF_SPL + j),_mm_srli_si128(v2,8));
                _mm_storel_epi64((__m128i *)(sb + (size_t)6 * MF_SPL + j),v3);
                _mm_storel_epi64((__m128i *)(sb + (size_t)7 * MF_SPL + j),_mm_srli_si128(v3,8));
                _mm_storel_epi64((__m128i *)(sb + (size_t)8 * MF_SPL + j),v4);
                _mm_storel_epi64((__m128i *)(sb + (size_t)9 * MF_SPL + j),_mm_srli_si128(v4,8));
              }
            }
            for (; j < N; j++) {
              const u8 *srr = s_sigrec + (size_t)od[j] * 16;
              for (int e = 0; e < k; e++) sb[(size_t)e * MF_SPL + j] = srr[e];
            }
          }
          if (s_sig) g_sig_merged = 1;
        }
      }
    }
  }
  
  if (s_sig && !g_sig_merged) {
    for (int d = 0; d < k; d++) {
      const u32 *od = s_ord[d];
      u8 *base = s_sig + (size_t)d * k * MF_SPL;
      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;
        for(int e=0;e<k;++e){u32 sv=(u32)(((u64)row[e]*g_sigmag)>>32);base[(size_t)e*MF_SPL+j]=(u8)(sv?sv:1u);}
      }
    }
  }
  
  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;
  
  for (int ds = 0; ds < k; ds++) {
    u32 q0 = gstart[ds], q1 = gstart[ds + 1];
    if (q0 >= q1) continue;
#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
    
    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++) {
          
          
          
          
          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; }   
    }
#endif
    
    const u32 *const nid=s_pos[ds];
    unsigned long long _btb = rdtb();
    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];
      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) {
        
        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;
#if MF_BUILD_NOPOOL
      while (ptr < N) ptr++;
#else
      for (u32 b = 1; b <= Q; b++) {
        
        
        
        
        
        u32 pend = (b < Q) ? FPOS(d, b) : (u32)N;
        while (ptr + 8 <= pend) {
          if (ptr + 40 < pend) __asm__ __volatile__("prefetcht0 %0" :: "m"(*(const char *)(nid + od[ptr + 40])));
          u32 q0 = nid[od[ptr+0]], q1 = nid[od[ptr+1]], q2 = nid[od[ptr+2]], q3 = nid[od[ptr+3]];
          u32 q4 = nid[od[ptr+4]], q5 = nid[od[ptr+5]], q6 = nid[od[ptr+6]], q7 = nid[od[ptr+7]];
          cur[q0>>6] |= 1ull<<(q0&63); cur[q1>>6] |= 1ull<<(q1&63); cur[q2>>6] |= 1ull<<(q2&63); cur[q3>>6] |= 1ull<<(q3&63);
          cur[q4>>6] |= 1ull<<(q4&63); cur[q5>>6] |= 1ull<<(q5&63); cur[q6>>6] |= 1ull<<(q6&63); cur[q7>>6] |= 1ull<<(q7&63);
          ptr += 8;
        }
        while (ptr < pend) {
          if (ptr + 12 < pend) __asm__ __volatile__("prefetcht0 %0" :: "m"(*(const char *)(nid + od[ptr + 12])));
          u32 q = nid[od[ptr]]; cur[q >> 6] |= 1ull << (q & 63); ptr++;
        }
        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_NT
        
        
        
        
        {
          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
      }
      _mm_sfence();
#endif
    }
    g_bt += rdtb()-_btb;
    switch (k) {
      case 4: query_group<4>(N, out, ds, q0, q1, Q, Wpool); break;
      case 5: query_group<5>(N, out, ds, q0, q1, Q, Wpool); break;
      case 6: query_group<6>(N, out, ds, q0, q1, Q, Wpool); break;
      case 7: query_group<7>(N, out, ds, q0, q1, Q, Wpool); break;
      case 8: query_group<8>(N, out, ds, q0, q1, Q, Wpool); break;
      case 9: query_group<9>(N, out, ds, q0, q1, Q, Wpool); break;
      default: query_group<10>(N, out, ds, q0, q1, Q, Wpool); break;
    }
  }
  switch (k) {
    case 4: fringe_rank<4>(N, out, Q, B); break;
    case 5: fringe_rank<5>(N, out, Q, B); break;
    case 6: fringe_rank<6>(N, out, Q, B); break;
    case 7: fringe_rank<7>(N, out, Q, B); break;
    case 8: fringe_rank<8>(N, out, Q, B); break;
    case 9: fringe_rank<9>(N, out, Q, B); break;
    default: fringe_rank<10>(N, out, Q, B); break;
  }
}

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

#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 buildT %.1f Mcyc\n", k, n, g_Q, ms, s, g_bt/1e6);
    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

#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cstdint>
#include <ctime>
void count_10d(int n, const unsigned *x[10], unsigned *out);
static unsigned gs = 12345u;
static inline unsigned nxr(void){ gs^=gs<<13; gs^=gs>>17; gs^=gs<<5; return gs; }

CompilationN/AN/ACompile OKScore: N/A

Testcase #1549.522 ms177 MB + 492 KBAcceptedScore: 100


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