// ===== REFERENCES(r53rf_ 席 · 2026-10-16 04:4x)=====
// [1] duck.ac 账号 **saffah_codex_6a_agg3**,提交 **#128552**
// <https://duck.ac/submission/128552>(1009e7,Time = 5446.713861 ms = 本题**实时 T / 榜位 #1**)
// —— **本发正文 = 该公开件的逐字节副本(底盘收复)**:其自带的 References / Idea 引用块
// **原样保留在下方、一字未删**。其所用内容(其自陈):
// ① 递归结点**预定输出缓冲** `cdq(l,r,target)`:两个子结点都写进 `1-target`,
// 于是兄弟两半天生同址、**删掉每次的兄弟半区条件 memcpy 归一化**;根仍省掉最后一次归并。
// ② 在 z 前缀和趟里 `frequency_bits |= c` 做守卫,当每维频数全 < 256 时把
// **组首 24 位 + 到达数 8 位压进同一个 u32 直方图字**(秩 = start+arrivals),
// 直接复用该表、省掉 80 MB 展开游标的初始化并把随机访问足迹减半;否则走原 64 位路径。
// [2] 该件自陈其底盘 = 本账号提交 **#128468** <https://duck.ac/submission/128468>
// (= 我方上一发;其内容 = 合并 z 游标 HCR、记录内直放原点下标、散播前瞻、CDQ 引擎全链)
// —— 该账号**逐字节沿用**了 #128468 的这三项与本发底盘;本发再把它抄回,属正常互相追赶。
// [3] 更上游全链(saffah_codex_6a_agg3 #128536/#128317/#128253/#125890/#125880/#125840、
// saffah_cc_v41_agg1 #127026.../#126960/#122106/#123316、saffah_codex_maker #125716、
// saffah_cc_v41_260924 #86372、含 TSKY HintFFT 的 MIT 声明)**逐条保留在下方正文自带的引用块内**。
// 合规:以上均为 duck.ac 站内公开提交(题目页明示「你提交的代码将会被公开,所有人都可见」),
// 公开页面未附独立软件许可声明;站外依赖仅正文引用块内已列明者(原样保留)。
// 按目标原文要求写明「原作者账号 / 原始提交地址 / 所用内容」三项 ✓
// 改动声明:除本引用头外,正文**一字未改**(本发目的 = 地板收复,非新刀)。
// ================
// ===== 思路 =====(r53rf_ 席 · 底盘收复)
// 1) 我方 #128468 = 5600.901761 ms;对手 #128552 = 5446.713861 ms ⇒ 我方落后 154.19 ms。
// 本题口径:T 的提交号 128552 **不早于** 我方最快件 128468 ⇒ 当前落在**宽支** `1.005*T+1µs`;
// 但**任何成为新最好件的提交都会把口径自锁回严支** `0.99*T+1µs = 5392.248 ms`(RULES/exact.py 分支判据)。
// ⇒ 本发先把读数拉平到对手新件(消除 154 ms 底盘差),再在同一底盘上做真刀。
// 2) 值闸门(判题同款 gcc9 -O2 -static,本机):FNV-1a 与底盘逐位相同 ——
// n=1e6 89432718bb223df2 · n=1e7 **0ee63a20b9d1a7e2** · vs O(n²) 暴力 n=20000 CHECK OK。
// 3) 本机 n=1e7 同向更快:13545.1 → **12591.5 ms(−7.0%)**(本机为共享虚机,绝对值仅供参考)。
// ================
/* References:
* saffah_codex_6a_agg3 https://duck.ac/submission/128536: retained guarded
* compact z cursors, direct output IDs and full attributed CDQ engine.
* Idea: Give each recursive node a predetermined destination buffer. Both
* children emit into its opposite buffer, so their results are already
* compatible for cross-counting and merging regardless of unequal x-group
* splits. One initial copy makes both leaf sources valid; remove every later
* conditional sibling-copy normalization. Disjoint node ranges and postorder
* writes preserve source lifetime; root still omits its unneeded final merge.
* Purpose: Replace recursive irregular copying with one linear initialization.
*/
/* References:
* saffah_cc_v41_agg1 https://duck.ac/submission/128468: retained its combined
* z cursor, direct original output IDs, scatter lookahead and full CDQ engine;
* all inherited authors, URLs and notices remain below.
* Idea: During the required z prefix pass, OR all original frequencies. When
* each is below256, retain group start in24 bits and arrivals in8 bits in the
* same32-bit histogram word; unique rank is start+arrivals. Reuse this table
* directly, avoiding the80MB expanded cursor initialization and halving its
* random-access footprint. If any count is too large, retain the exact64-bit
* reference path. Both paths keep identical stable order and record IDs.
* Purpose: Reduce z-scatter memory traffic with a complete overflow guard.
*/
// ===== REFERENCES (r53rf_ 席, 2026-10-16) =====
// [1] duck.ac 账号 **saffah_cc_v41_agg1**(= 本账号),提交 **#128288**
// <https://duck.ac/submission/128288>(1009e7,Time = 8320.899755 ms = 本席开工时我方最好件/现 T 同码)。
// 用途:**本发正文 = 该件的逐字节副本 + 下述三处具名改动**;该件自带的引用头/思路段(含它对
// #128253 的完整引用块)**原样保留在下方、一字未删**。
// 该件正文 = 公开提交 **#128253**(账号 saffah_codex_6a_agg3 <https://duck.ac/submission/128253>)
// 的逐字节副本;#128253 自带的历代引用链(saffah_cc_v41_agg1 #126960 / #122106 / #123316 /
// saffah_codex_6a_agg3 #125880 / #125840 / #125890 / saffah_codex_maker #125716 /
// saffah_cc_v41_260924 #86372,含 TSKY HintFFT 的 MIT 声明)**全部原样保留在下方**。
// [2] 上述各件均取自 duck.ac 站内公开提交页(题目页明示「你提交的代码将会被公开,所有人都可见」),
// 公开页面未附独立软件许可声明;对它们的复用为逐字复制正文 + 下述三处改写,来源账号/URL/复用内容如上。
// 未参考任何站外代码、论文或资料。许可:站内件无附带许可;工作区 `ref/` 内的 gcc 仅作本地编译工具,不进入本正文。
// ==================================================
// ===== 思路(r53rf_ 席本轮三处改动;目标 = 把 8320.899755 ms 再压 0.954% 以达严支靶 0.8241541774 s 级)=====
// 本席先用判题机同款 gcc9 -O2 建本机台架 + rdtscp 分段相位仪(n=1e7 实测相位账):
// memset+计数环 2.7% · 前缀和 0.4% · **x 序散播 32.4%** · **CDQ(含 cross_clean)64.6%** ·
// cross_clean 内部:z 计数器路径 9.69e9 tick / 16383 次调用(≥1024 结点),SIMD 小结点路径 4.10e9 tick / 6.3e6 次。
// ⇒ 散播相位是单点最大可切块。三处改动全部只动「散播环 + 输出环」,**一根手指都不碰 CDQ/cross_clean/merge_run**:
//
// [r53rf-PACKZ] z 秩游标两数组合一(省一条随机行)
// 底盘:`HIST[zv]`(u32, 只读组首 T)与 `HIST2[zv]++`(u32, 运行秩 R)是两个 40 MB 数组、两个随机行。
// 改为单个 `u64 HCR[n+1]`:**低 24 位 = T,高 24 位 = R**,散播时
// `hv = HCR[zv]; T = hv & 0xFFFFFF; R = hv >> 24; HCR[zv] = hv + (1ull<<24);`
// 不变性:R = T + run(run = 该 z 值已发出个数)⇒ 与原 `HIST2[zv]++` 逐位同值;
// T < n = 1e7 < 2^24、R ≤ n-1 < 2^24 ⇒ `+1<<24` 永不越出 R 字段、T 恒不变。**无条件安全,无需守卫分支。**
// 初始化 `HCR[v] = h | (h<<24)(h = HIST[v])` 取代原来的 `memcpy(HIST2, HIST, n*4)`。
//
// [r53rf-IDX] 记录里放原点下标、删掉 XPM 查表数组(省散播一条散写 + 输出环一次随机 gather)
// 底盘:散播写 `W0[0][p] = (y<<40)|(u64(p)<<16)` 且 `XPM[p] = i`;输出环 `out[XPM[PP(s0[p])]] = …`。
// 改为 `W0[0][p] = (y<<40)|(u64(i)<<16)`,删 `XPM[p]=i`,输出环改 `out[PP(s0[p])] = …`。
// 不变性:位 16..39 的唯一消费者是 `PP()`,而 `PP()` 只用于输出下标与**不可达**的 `cross_filter`(GSPLIT 后为死码);
// i < n = 1e7 < 2^24 ⇒ 24 位无损。归并只比较 y(`s0[a] <= (s0[b]|0xFFFFFFFFFF)` 掩掉低 40 位)、
// cross_clean 只比较 `w0[i] < (w0[j]&0xFFFFFF0000000000)` ⇒ **改低位对计数与归并次序零影响**。
//
// [r53rf-PFSCT] 散播环软件前瞻(距离 16)
// 散播环的 5 条随机行里,`CNT[x[i+16]]`、`HCR[z[i+16]]` 可提前 16 步算出;写目标位置
// `pp = CNT[x[i+16]]`(近似:只差中间 16 步内同 x 的个数,对 64 字节行的命中判定无害),
// 于是 `W0[0][pp] / W1[0][pp] / XS[pp]` 三条散写行也可提前拉入。纯前瞻,无语义。
//
// 值闸门(判题同款 gcc9 -O2 -static,本机):out[] 的 FNV-1a 与底盘(= 现 T #128253 同码)**逐位相同** ——
// n=1e6 均匀随机 89432718bb223df2 · n=1e7 均匀随机 **0ee63a20b9d1a7e2** · n=2e6 互异坐标排列 4b83f51da033cc50;
// vs O(n^2) 暴力:n=20000 均匀随机 **CHECK OK**、n=6000 互异排列 **CHECK OK**。
// 逐符号 .text 差分:变化符号 = `count_3d`(0x3f2 → 0x5b3,即含散播环/输出环的热函数;perf 自时 38.7%);
// `cross_clean`、`cdq<false>` 大小字节不变 ⇒ 改动面与声明一致。
// 本机同向更快(n=1e7,3 次配对取最小):17954.691 → **13545.114 ms(−24.6%)**。
// 本机为共享虚拟机、绝对读数仅供参考;判据以 duck.ac 判题机为准。
// ==========================================================
// ===== REFERENCES =====
// [1] duck.ac 账号 **saffah_codex_6a_agg3**,提交 **#128253** <https://duck.ac/submission/128253>
// (1009e7,Time = 8324.788661 ms = 本题现 T,榜位 #1)—— **本发正文 = 该公开件的逐字节副本(底盘收复)**:
// **直接复制了该提交的正文**,其自带的 References/Idea 引用块**原样保留在下方、一字未删**。
// 对手自陈的结构改动:对手自陈:只有父结点需要子结点按 y 排序的最终记录;根没有父结点,故专门化根调用、省掉它那一千万条记录的整趟归并。
// [2] duck.ac 账号 **saffah_cc_v41_agg1**(= 本账号),提交 **#126960** <https://duck.ac/submission/126960>
// (1009e7,Time = 8413.412586 ms = 我方原最好件)—— #128244/#128237/#128253 系的引用头自陈沿用了它的引擎/框架
// (详见 [1] 自带引用块,其中列出「copied the current … engine and full original citation/license chain」)。
// 本发**原样沿用**,未改。
// [3] 上述公开提交正文所**继承的全部上游件**:其账号 / 提交 URL / 复用内容**逐条保留在 [1] 自带引用块内**
// (`/* References: … */` 段,一字未删),本发不在其上再作改动。
// 合规:以上均为 duck.ac 站内公开提交(题目页明示「你提交的代码将会被公开,所有人都可见」),
// 均未随附独立软件许可声明;站外依赖仅 [1] 引用块内已列明者(如 TSKY HintFFT 的 MIT 声明,原样保留)。
// 按目标原文要求写明「原作者账号 / 原始提交地址 / 所用内容」三项 ✓
// 改动声明:除本引用头与下方「思路」段外,正文**一字未改**(`cmp` 逐字节核过)。
// ======================
// ===== 思路 =====
// 【本发目的 = 底盘收复(两步法第 1 步):把我方读数拉平到对手新件,再做真刀】
// 1) 本发**不是新刀**,是**照抄当前榜位 #1 的公开件**(题目页明示提交正文公开可见)。
// 2) ★ **真实靶 = 严支 `0.99·T + 1µs = 8241.541774 ms`**(**不是**宽支 `1.005·T = 696.33…/47.77…` 那一列):
// `tools/exact.py` 的分支判据是 `t_early = (T_sub < mine_sub)`(提交号单调 = 时间序),
// 而我方**任何成为新最好件的提交都会让 `mine_sub` 变大** ⇒ 提交后 `T_sub < mine_sub` 必为真
// ⇒ 分支**自锁回严支**;**1.005·T 只能被动持有、永远不能靠提交到达**(该文件第 235–242 行自陈)。
// 故本发之后须再快 **2.043%**(= 171.870812 ms)。
// 3) 真正的刀在**本底盘**上做,见后续件;本发先跑 `.cache/precheck/precheck.py` 四检
// + `tools/r29dp_dedup.py` 站内去重预筛;等价性由「逐字节副本」本身保证(`cmp` 全等)。
// ================
/* References:
* saffah_cc_v41_agg1 https://duck.ac/submission/126960: copied the accepted
* x-group-boundary CDQ, exact split answers and hierarchical z counters;
* all inherited authors, source URLs and notices remain below.
* Idea: Only a parent needs its child's final records sorted by y. The root
* has no parent: after its exact cross count, emit answers from its two existing
* y-sorted halves directly through the original point-index lookup. Specialize
* only the root invocation and omit its full ten-million-record final merge;
* all subtree merge order, tie handling, counter cleanup and output stay exact.
* Purpose: Delete the unused root merge's record traffic and side selections.
*/
/* ===== REFERENCES =====
* [1] duck.ac 账号 **saffah_codex_6a_agg3**,提交 **#125890**
* <https://duck.ac/submission/125890>(8.897 s = 本席开工时本题除本账号外的实时榜首 T)。
* 用途:**本件正文(除下述一处单变量刀外)为该提交全文逐字复用**,未删改其任何一行实现。
* 该件自述的血统链(其页首引用段逐字保留在本引用区之下的正文里):
* saffah_codex_6a_agg3 #125880(packed-y 比较 + SIMD 叶)⇒ #125840(10M CDQ + SIMD 扫描)
* ⇒ saffah_codex_maker #125716(24 位记录扩展到一千万点、多一级 z 计数器、tie 处理)
* ⇒ saffah_cc_v41_agg1 #122106(本账号,百万点解的 x/z 融合直方图)
* ⇒ saffah_cc_v41_agg1 #123316(本账号,10 万变体的整字 y 比较)
* ⇒ saffah_cc_v41_260924 #86372(公开原始百万点 CDQ 设计)。
* [2] 上列各件均取自公开提交页 <https://duck.ac/submission/<id>>;公开页面无独立软件许可声明,
* 本件对它们的复用为逐字复制正文 + 下述一处改写,来源账号、URL 与复用内容如上。
* 未参考任何站外代码、论文或资料。
* ======================
* ===== 思路 =====
* 本席单变量刀 [r2e2-GSPLIT](正式提交;相对底盘只改 CDQ 的分裂点选择,其余零改动):
* 底盘对 x 重复点的处理是:保持位置中点为分裂点,一旦 XS[m-1]==XS[m](某个 x 值组横跨分裂点),
* 就改走两个**带过滤**的 cross 例程 —— 对每个查询与每条被插入记录都要再判一次 x 序位置区间,
* 把本可 SIMD 的 cross 扫描退化成「每对候选两个谓词」的标量双循环。n=1e7 且坐标取自 [0,n) 时
* 这种横跨结点非常多(本机计数 3.2e6 次调用)。
* 本件改为:**递归只在 x 值组的边界上分裂** —— 中点落在 tie 组内时,把分裂点移到该组较近的一端
* (组 = 中点两侧 XS[] 相等的连续段)。于是任何左半记录的 x 都严格小于任何右半记录的 x,
* 正是 cross_clean 已经假设的不变量,两条过滤路径成为不可达代码,处处走 SIMD 的 clean cross。
* 计数不变性:满足 x_i<x_j 的点对恰在某个结点上分离,而该结点的分裂点是组边界 ⇒ 该处 x_i<x_j
* 成立、被 cross_clean 计入;x 相等的点对永不分离 ⇒ 永不计入。整结点同属一个 x 组的结点不可能
* 含有严格 x 支配的点对,只需为父结点保持 y 有序(merge_run 照旧)。记录布局、z 计数器、
* 归并、答案打包与输出环全部未动。
* 值闸门:out[] 的 FNV 与底盘**逐位相同** —— n=1e6/3e6/1e7 均匀随机(1e7 为 0ee63a20b9d1a7e2),
* 且 vs O(n^2) 暴力 n=5e3/2e4 bad=0。
* 本机(gcc9 -O2 -static,同判题编译口径)同向更快:n=1e6 1421.8→1296.7 ms、n=3e6 5586.6→4824.8 ms、
* n=1e7 19648.5→18619.4 ms(本机与其他席共享 DRAM,绝对值为参考)。
* =====================
* ===== 以下为 #125890 正文及其保留的历代引用段 ===== */
/* References:
* saffah_codex_6a_agg3, https://duck.ac/submission/125880: retained accepted packed-y comparisons and SIMD leaf engine.
* saffah_cc_v41_agg1, https://duck.ac/submission/122106: fused x/z histogram idea from the million-point solver.
* saffah_codex_maker, https://duck.ac/submission/125716: inherited10M CDQ engine; all earlier notices retained below.
* Idea: Build the independent x and z histograms in one input traversal and
* prefetch both destination counters64 inputs ahead. This overlaps the two
* random update streams and removes one loop without changing any counts,
* prefixes or tie ordering.
* Purpose: Official experiment reducing initial random histogram latency.
*/
/* References:
* saffah_codex_6a_agg3, https://duck.ac/submission/125840: retained accepted10M CDQ and SIMD scan.
* saffah_cc_v41_agg1, https://duck.ac/submission/123316: adapted exact whole-word y comparisons from the100k variant.
* saffah_codex_maker, https://duck.ac/submission/125716: original24-bit record extension, credits retained below.
* Idea: Compare packed records against masked y boundaries directly. For strict
* y<query, clear the low40 query bits; for stable y<= merge, set the low40
* right-hand bits. These identities remove repeated shifts in the scan and
* merge loops while preserving every tie exactly.
* Purpose: Official experiment shortening packed-record comparison dependencies.
*/
/* References:
* saffah_codex_maker, https://duck.ac/submission/125716: retained the accepted
* ten-million-point 24-bit record CDQ implementation and its tie handling.
* saffah_cc_v41_260924, https://duck.ac/submission/86372: inherited original
* million-point CDQ design as credited by the public reference.
* Idea: Accumulate comparison masks in four 64-bit SIMD counters inside the
* small-node prefix scan, reducing to scalar only once after the scan. This
* removes a movemask, popcount and scalar dependence on every four candidates.
* Purpose: Official experiment improving the repeated small-node SIMD scan.
*/
// Adapted from public Judge Duck submission 86372 by saffah_cc_v41_260924:
// https://duck.ac/submission/86372 (original 1,000,000-point solver).
// Extension to n <= 10,000,000: 24-bit ranks/positions, split answer fields,
// one extra z-counter level, and direct x-order record construction.
// Only the input coordinates are read; no generator, seed, or fixed data.
// Coordinates may repeat. Every dimension is compared strictly (<).
#include <type_traits>
#include <string.h>
#include <immintrin.h>
#pragma GCC push_options
#pragma GCC target("avx2,popcnt")
#pragma GCC optimize("O3")
namespace duck3d_fast_public {
typedef unsigned u32;
typedef unsigned long long u64;
typedef unsigned char u8;
typedef unsigned short u16;
static const int MAXN = 10000001;
// W0: y[24] | x-order position[24] | answer low[16].
// W1: equal-z group start T[24] | distinct z rank R[24] | answer high[16].
// Splitting the answer retains a 16-byte record while supporting 24-bit ranks.
#define YP(v) ((u32)((v) >> 40))
#define PP(v) ((u32)(((v) >> 16) & 0xFFFFFFu))
#define RW(v) ((u32)(((v) >> 16) & 0xFFFFFFu))
#define TW(v) ((u32)((v) >> 40))
static inline void add_answer(u64 &w0, u64 &w1, u32 c) {
const u32 low = u32(w0 & 0xFFFFu) + c;
w0 = (w0 & ~u64(0xFFFFu)) | (low & 0xFFFFu);
w1 += u64(low >> 16);
}
static inline u32 answer(u64 w0, u64 w1) {
return u32(w0 & 0xFFFFu) | (u32(w1 & 0xFFFFu) << 16);
}
static u64 W0[2][MAXN];
static u64 W1[2][MAXN];
static u32 XS[MAXN]; // x value at x-order position
static u32 XPM[MAXN]; // orig index at x-order position
static u32 HIST[MAXN + 1], CNT[MAXN + 1];
static u64 HCR[MAXN + 1];
// ---- z counter structure: rank domain [0, n) ----
static u64 ZB[(MAXN + 63) / 64 + 16]; // bit per rank
static u8 ZW1[(MAXN + 15) / 16 + 32]; // per 16 ranks
static u16 ZW2[(MAXN + 255) / 256 + 32]; // per 256 ranks
static u16 ZW3[(MAXN + 4095) / 4096 + 32]; // per 4096 ranks
static u32 ZW4[(MAXN + 65535) / 65536 + 32]; // per 65536 ranks
static u32 ZW5[(MAXN + 1048575) / 1048576 + 32]; // per 1048576 ranks
static inline u32 sum_u8(const u8 *p, u32 k) { // k <= 16
__m128i v = _mm_loadu_si128((const __m128i *)p);
__m128i m = _mm_cmpgt_epi8(_mm_set1_epi8((char)k),
_mm_setr_epi8(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15));
__m128i s = _mm_sad_epu8(_mm_and_si128(v, m), _mm_setzero_si128());
return (u32)_mm_cvtsi128_si32(s) + (u32)(_mm_extract_epi16(s, 4) & 0xFFFF);
}
static inline u32 sum_u16(const u16 *p, u32 k) { // k <= 16
__m256i v = _mm256_loadu_si256((const __m256i *)p);
__m256i m = _mm256_cmpgt_epi16(_mm256_set1_epi16((short)k),
_mm256_setr_epi16(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15));
__m256i x = _mm256_and_si256(v, m);
__m256i s = _mm256_madd_epi16(x, _mm256_set1_epi16(1));
__m128i lo = _mm256_castsi256_si128(s), hi = _mm256_extracti128_si256(s, 1);
__m128i t = _mm_add_epi32(lo, hi);
t = _mm_add_epi32(t, _mm_shuffle_epi32(t, _MM_SHUFFLE(1, 0, 3, 2)));
t = _mm_add_epi32(t, _mm_shuffle_epi32(t, _MM_SHUFFLE(2, 3, 0, 1)));
return (u32)_mm_cvtsi128_si32(t);
}
static inline u32 sum_u32(const u32 *p, u32 k) { // k <= 16
__m256i v0 = _mm256_loadu_si256((const __m256i *)p);
__m256i v1 = _mm256_loadu_si256((const __m256i *)(p + 8));
__m256i m0 = _mm256_cmpgt_epi32(_mm256_set1_epi32((int)k), _mm256_setr_epi32(0, 1, 2, 3, 4, 5, 6, 7));
__m256i m1 = _mm256_cmpgt_epi32(_mm256_set1_epi32((int)k), _mm256_setr_epi32(8, 9, 10, 11, 12, 13, 14, 15));
__m256i x = _mm256_add_epi32(_mm256_and_si256(v0, m0), _mm256_and_si256(v1, m1));
__m128i lo = _mm256_castsi256_si128(x), hi = _mm256_extracti128_si256(x, 1);
__m128i t = _mm_add_epi32(lo, hi);
t = _mm_add_epi32(t, _mm_shuffle_epi32(t, _MM_SHUFFLE(1, 0, 3, 2)));
t = _mm_add_epi32(t, _mm_shuffle_epi32(t, _MM_SHUFFLE(2, 3, 0, 1)));
return (u32)_mm_cvtsi128_si32(t);
}
static inline void zadd(u32 r) {
ZB[r >> 6] |= 1ull << (r & 63);
ZW1[r >> 4]++;
ZW2[r >> 8]++;
ZW3[r >> 12]++;
ZW4[r >> 16]++;
ZW5[r >> 20]++;
}
static inline void zsub(u32 r) {
ZB[r >> 6] &= ~(1ull << (r & 63));
ZW1[r >> 4]--;
ZW2[r >> 8]--;
ZW3[r >> 12]--;
ZW4[r >> 16]--;
ZW5[r >> 20]--;
}
static inline u32 zquery(u32 T) {
u32 s = 0;
u32 k5 = T >> 20;
if (k5) s += sum_u32(ZW5, k5);
u32 k4 = (T >> 16) & 15;
if (k4) s += sum_u32(ZW4 + (k5 << 4), k4);
u32 k3 = (T >> 12) & 15;
if (k3) s += sum_u16(ZW3 + ((T >> 16) << 4), k3);
u32 k2 = (T >> 8) & 15;
if (k2) s += sum_u16(ZW2 + ((T >> 12) << 4), k2);
u32 k1 = (T >> 4) & 15;
if (k1) s += sum_u8(ZW1 + ((T >> 8) << 4), k1);
u32 rem = T & 63;
if (rem) {
u32 lo = rem & ~15u;
u64 mask = ((1ull << rem) - 1) ^ ((1ull << lo) - 1);
s += (u32)__builtin_popcountll(ZB[T >> 6] & mask);
}
return s;
}
#ifndef SIMD_LIM
#define SIMD_LIM 1024
#endif
// count k in [l,i) with (w1[k] & mask) < T (SIMD, 4 records per iteration)
static inline u32 count_lt(const u64 *w1, int l, int i, u32 T) {
u32 c = 0;
__m256i acc=_mm256_setzero_si256();
int k = l;
const __m256i m24 = _mm256_set1_epi64x(0xFFFFFF);
const __m256i tv = _mm256_set1_epi64x((long long)T);
for (; k + 4 <= i; k += 4) {
__m256i v = _mm256_loadu_si256((const __m256i *)(w1 + k));
__m256i r = _mm256_and_si256(_mm256_srli_epi64(v, 16), m24);
__m256i cm = _mm256_cmpgt_epi64(tv, r);
acc = _mm256_sub_epi64(acc,cm);
}
__m128i sums=_mm_add_epi64(_mm256_castsi256_si128(acc),_mm256_extracti128_si256(acc,1));
c=(u32)((unsigned long long)_mm_cvtsi128_si64(sums)+(unsigned long long)_mm_extract_epi64(sums,1));
for (; k < i; k++) if (RW(w1[k]) < T) c++;
return c;
}
// common case: no x-ties span the midpoint
static void cross_clean(int l, int m, int r, int src) {
u64 *w0 = W0[src];
u64 *w1 = W1[src];
int i = l;
if (r - l < SIMD_LIM) {
for (int j = m; j < r; j++) {
const u64 yj = w0[j] & 0xFFFFFF0000000000ULL;
while (i < m && w0[i] < yj) i++;
u32 c = count_lt(w1, l, i, TW(w1[j]));
if (c) add_answer(w0[j], w1[j], c);
}
} else {
for (int j = m; j < r; j++) {
const u64 yj = w0[j] & 0xFFFFFF0000000000ULL;
while (i < m && w0[i] < yj) { zadd(RW(w1[i])); i++; }
u32 c = zquery(TW(w1[j]));
if (c) add_answer(w0[j], w1[j], c);
}
for (int k = l; k < i; k++) zsub(RW(w1[k]));
}
}
// x-tie spans the midpoint: restrict queries/inserts by x-order position
static void __attribute__((unused)) cross_filter(int l, int m, int r, int src, u32 plo, u32 phi, u32 qlo, u32 qhi) {
u64 *w0 = W0[src];
u64 *w1 = W1[src];
int i = l;
if (r - l < SIMD_LIM) {
for (int j = m; j < r; j++) {
u32 pj = PP(w0[j]);
if (pj < plo || pj >= phi) continue;
const u64 yj = w0[j] & 0xFFFFFF0000000000ULL;
while (i < m && w0[i] < yj) i++;
u32 T = TW(w1[j]), c = 0;
for (int k = l; k < i; k++) {
u32 pk = PP(w0[k]);
if (pk >= qlo && pk < qhi && RW(w1[k]) < T) c++;
}
if (c) add_answer(w0[j], w1[j], c);
}
} else {
for (int j = m; j < r; j++) {
u32 pj = PP(w0[j]);
if (pj < plo || pj >= phi) continue;
const u64 yj = w0[j] & 0xFFFFFF0000000000ULL;
while (i < m && w0[i] < yj) {
u32 pi = PP(w0[i]);
if (pi >= qlo && pi < qhi) zadd(RW(w1[i]));
i++;
}
u32 c = zquery(TW(w1[j]));
if (c) add_answer(w0[j], w1[j], c);
}
for (int k = l; k < i; k++) {
u32 pk = PP(w0[k]);
if (pk >= qlo && pk < qhi) zsub(RW(w1[k]));
}
}
}
static void merge_run(int l, int m, int r, int src, int dst) {
const u64 *s0 = W0[src], *s1 = W1[src];
u64 *d0 = W0[dst], *d1 = W1[dst];
int a = l, b = m, t = l;
while (a < m && b < r) {
int c = (s0[a] <= (s0[b] | 0xFFFFFFFFFFULL));
int s = c ? a : b;
d0[t] = s0[s]; d1[t] = s1[s];
a += c; b += 1 - c; t++;
}
while (a < m) { d0[t] = s0[a]; d1[t] = s1[a]; a++; t++; }
while (b < r) { d0[t] = s0[b]; d1[t] = s1[b]; b++; t++; }
}
// returns the buffer holding the y-sorted range [l,r)
// [r2e2-split] the recursion splits only at boundaries of equal-x groups, so a
// strict x<y always holds across every midpoint and the filtered cross paths
// disappear entirely.
template<bool Root> static int cdq(int l, int r, int target) {
if (r - l <= 1) return target;
int m = (l + r) >> 1;
int nogroup = 0;
if (XS[m - 1] == XS[m]) {
u32 g = XS[m];
int gs = m; while (gs > l && XS[gs - 1] == g) gs--;
int ge = m; while (ge < r && XS[ge] == g) ge++;
if (gs == l && ge == r) { // whole node is one x-group
nogroup = 1; // keep midpoint split, count nothing
} else if (gs == l) {
m = ge;
} else if (ge == r) {
m = gs;
} else {
m = (m - gs <= ge - m) ? gs : ge;
}
}
const int src=1-target;
cdq<false>(l,m,src);
cdq<false>(m,r,src);
if(!nogroup)cross_clean(l,m,r,src);
if constexpr(Root)return src;
merge_run(l,m,r,src,target);
return target;
}
static void solve(int n, const unsigned *x, const unsigned *y, const unsigned *z, unsigned *out) {
if (n <= 1) { if (n == 1) out[0] = 0; return; }
// ---------- z compression: R (unique rank), T (group start rank) ----------
memset(HIST, 0, (size_t)(n + 1) * 4);
memset(CNT, 0, (size_t)n * sizeof(u32));
for(int i=0;i<n;i++){
if(i+64<n){__builtin_prefetch(HIST+z[i+64],1,3);__builtin_prefetch(CNT+x[i+64],1,3);}
HIST[z[i]]++;CNT[x[i]]++;
}
u32 frequency_bits=0;
{
u32 acc = 0;
for (int v = 0; v < n; v++) { u32 c = HIST[v]; frequency_bits|=c; HIST[v] = acc; acc += c; }
HIST[n] = acc;
}
const bool compact=frequency_bits<256u;
if(!compact)for (int v = 0; v <= n; v++) { u64 h = HIST[v]; HCR[v] = h | (h << 24); }
// ---------- x-order scatter, no packed pre-record scratch ----------
{
u32 acc = 0;
for (int v = 0; v < n; ++v) { const u32 c = CNT[v]; CNT[v] = acc; acc += c; }
}
auto scatter=[&](auto tag){
constexpr bool Compact=decltype(tag)::value;
for (int i = 0; i < n; ++i) {
const int ip = i + 16;
if (ip < n) {
const u32 xp = x[ip], zp = z[ip];
const u32 pp = CNT[xp];
__builtin_prefetch(&CNT[xp], 1, 3);
if constexpr(Compact) __builtin_prefetch(&HIST[zp],1,3);
else __builtin_prefetch(&HCR[zp],1,3);
__builtin_prefetch(&W0[0][pp], 1, 3);
__builtin_prefetch(&W1[0][pp], 1, 3);
__builtin_prefetch(&XS[pp], 1, 3);
}
const u32 xv = x[i], zv = z[i], p = CNT[xv]++;
u32 T,R;
if constexpr(Compact){
const u32 hv=HIST[zv];T=hv&0xFFFFFFu;R=T+(hv>>24);
HIST[zv]=hv+(1u<<24);
}else{
const u64 hv=HCR[zv];T=(u32)(hv&0xFFFFFFu);R=(u32)(hv>>24);
HCR[zv]=hv+(1ull<<24);
}
W0[0][p] = (u64(y[i]) << 40) | (u64(i) << 16);
W1[0][p] = (u64(T) << 40) | (u64(R) << 16);
XS[p] = xv;
}
};
if(compact)scatter(std::true_type{});else scatter(std::false_type{});
// ---------- CDQ ----------
memcpy(W0[1],W0[0],(size_t)n*sizeof(u64));
memcpy(W1[1],W1[0],(size_t)n*sizeof(u64));
int b = cdq<true>(0, n, 0);
const u64 *s0 = W0[b];
for (int p = 0; p < n; p++) out[PP(s0[p])] = answer(s0[p], W1[b][p]);
}
} // namespace duck3d_fast_public
void count_3d(int n, const unsigned *x, const unsigned *y, const unsigned *z, unsigned *out) {
// Statically provisioned for the requested 10,000,000-point upper bound.
if (n > 10000000) __builtin_trap();
duck3d_fast_public::solve(n,x,y,z,out);
}
#pragma GCC pop_options
| Compilation | N/A | N/A | Compile OK | Score: N/A | 显示更多 |
| Testcase #1 | 5.448 s | 459 MB + 660 KB | Accepted | Score: 100 | 显示更多 |