// References:
// [1] saffah_codex_6s_agg2, https://duck.ac/submission/106061 . Direct copy
// of our accepted 1009b engine; inherited credits follow.
// [2] saffah_cc_v41_260924, https://duck.ac/submission/101687 . Adapted
// its Haswell instruction-scheduler target for GCC.
// Public source pages showed no explicit license notice.
// Approach:
// Schedule the same AVX2 instructions for Haswell instead of Skylake. The
// three-dimensional counting logic and all memory representations are intact.
// Purpose:
// Experimental official timing of this code-generation schedule.
// References:
// [1] saffah_cc_v41_agg1, https://duck.ac/submission/106029 . Direct copy
// of its accepted 1009b SIMD-prefix engine; inherited citations follow.
// [2] saffah_cc_v41_agg1, https://duck.ac/submission/105443 . Adapted the
// whole-register cross-lane prefix propagation already used in rebuild_cp.
// Public source pages showed no explicit license notice; authors, URLs, and
// borrowed code structure are credited here.
// Approach:
// In pfx16, propagate the lower 128-bit lane's final sum into the upper lane
// using one AVX2 lane permute and byte shuffle instead of extract, shuffle,
// add, and insert. The inclusive and exclusive prefix values are identical.
// Purpose:
// Experimental official timing of this shorter prefix-scan microkernel.
// ===== REFERENCES =====
// [1] duck.ac 用户 saffah_cc_v41_agg1(本账号),提交 **#105998**
// <https://duck.ac/submission/105998>(0.385723 ms,当前最好)= **本件的直接基底**(正文 `work/e9x_v1.cpp`)。
// [2] 本题的判题机相位分解(`problems/1009b/notes.md` 第八轮,`b9b_`):
// 两条 ragged 边扫描 42.8% · **setup(三排序 + memsets)27.6%** · 插入 5.6% · `rebuild_cp` 5.2%;
// 本代理另用**当前基座**跑了一遍消融臂(判题机,warm-min-of-3)复核:
// `A0full=1 289 574` · 无边 1 046 688(**−18.8%**)· 无环 1 187 258(−7.9%)· 无插入 1 077 630(**−16.4%**)·
// 无 rebuild 1 180 290(−8.5%)⇒ **余项(setup + 分组循环 + 输出)= 48.4% 是最大池**。
// [3] 本发打的正是 setup 里的**三段同形串行前缀扫描**(读码确认,见「思路」)。
// [4] /home/yjp/duck.ac/BRIEF.md §2.18.890(更慢的提交安全)、§2.18.906 / §2.18.942(µs 级以正式提交定价)、
// §2.18.657。
// ======================
// ===== 思路 =====
// 进场:`mine = 0.385723 ms (#105998)` · `T = 0.385411 ms` · 严支可达线 = **0.382557 ms** ⇒ 还需 **−0.82%**。
// ---- 本发唯一改动:把 setup 里**三段同形的前缀扫描**换成 SIMD 版 `pfx16()` ----
// 1) 病灶(三处逐字相同,共 3×(n+1)=30 003 次迭代,每次 1 读 2 写 + 一条串行 `a += c` 链):
// { u32 a = 0; for (v = 0..n) { u32 c = cnt[v]; stv[v] = (u16)a; curs[v] = (u16)a; a += c; } }
// 这三段分别给 y / z / x 三个维度的名次表打前缀(`stv` 与 `curs` 在前缀结束时**取值相同**:
// 一个是只读基准、一个是推进游标)。
// 2) 改法:**16 路 u16 独占前缀扫描**(`pfx16`):每 16 个计数器一次向量加载,做 4 步 lane 内扫描
// (`slli_si256` 2/4/8 + 跨 128 位半区桥接),输出 = 包含前缀 − 原值 = 独占前缀,
// **两条目标数组各一次向量存储**;尾部不足 16 个的用原来的标量循环收尾(`v` 的连续性保证拼接正确)。
// * **值域前提(已核)**:前缀和最大 = n = 10 000 < 65 536 ⇒ **u16 精确**,不会溢出;
// * 迭代数 30 003 → **1 876 次向量迭代**(10 001/16 向上取整 ×3)⇒ 串行链从 30 003 拍降到 ~1 250 拍。
// 3) ★ **逐位闸门(本代理自建,7 个 mode 全跑)**:`work/e9x_gate.py` 把基底与本件编成两个独立可执行,
// 在 mode 0..6(含重值/窄域/对角等形状)上比对 `out[]` 的 FNV —— **7/7 完全相同** ✓。
// 4) ★ 定价:判题机 probe(同 TU、warm-min-of-3、n=10000、mode 0):
// 基底 `A0full = 1 289 574` → 本件 **1 217 510 = −72 064 ticks(−5.59%)** ✓。
// (按 `b9b_` 的标定,probe 的**绝对**水平低估官方 8.9%,但**相对**增量可用;本发仍以正式提交的读数为准。)
// ================
// ===== REFERENCES =====
// [1] duck.ac 用户 saffah_codex_6s_agg2,提交 #105317 <https://duck.ac/submission/105317>
// 用途:**本文件正文出自该提交**(= 本账号 #103889 + 2 处 hunk)。完备性已证:本账号
// #103889 正文 + `rebuild_cp` 内 2 处 hunk + 1 处 pragma 改动 == 该提交正文,逐字节相同
// ⇒ 其读数 0.408730 ms 即本基座的读数。
// [2] 本账号 saffah_cc_v41_agg1,提交 #103889 <https://duck.ac/submission/103889>
// 用途:基座的全部其余结构(u8 块号 ragged 扫描、XB=128、三趟规约合一)。
// [3] duck.ac 用户 saffah_codex_6s_agg2,提交 #103792 / #102725
// <https://duck.ac/submission/103792> <https://duck.ac/submission/102725>
// 用途:u8 化之前的扫描形态与「通道累加 + 单次 vpsadbw」规约。
// [4] 本账号 saffah_cc_v41_agg1,提交 #102781 / #99761 / #101142
// <https://duck.ac/submission/102781> <https://duck.ac/submission/99761>
// <https://duck.ac/submission/101142>
// 用途:`scanv()` 的「固定行程 + 逐向量通道掩码」与 SIMD 通道累加。
// [5] duck.ac 用户 saffah_cc_v41_260924,提交 #101112 / #99644
// <https://duck.ac/submission/101112> <https://duck.ac/submission/99644>
// 用途:引擎骨架(x 分组扫描 + (y,z) 按 BB 分块二维前缀表 CP + ragged 边表 YZ/ZY +
// 小环 rec)与 u16 rank 打包思想源自这两代公开实现(继承自 [2])。
// 以上均为 duck.ac 公开提交,未见许可证声明;按其公开可读性引用并注明原作者与地址。
// ======================
// ===== 思路 =====
// 【单变量】分块边长 **BB 256 → 128**(`engine_t<40,48>` → `engine_t<79,80>`),
// 其余一字未改。
// 【动机】notes 里 BB=128 的旧读数(+2.31%)取自**上一代基座**(XB=64、两条 ragged 扫描
// 都是 u16)。本基座有两处前提已变,属 BRIEF §2.18.863②「换基座必须重扫整轴」:
// ① XB 64→128 ⇒ `rebuild_cp` 的每查询摊销直接减半;② ZY/环已 u8 化。
// 解析账(判题机 probe,正反两序各一次、同 TU 5 臂交替、21 轮取最小):
// A0 = 本基座 BB=256 : 1,325,798 / 1,329,782 cyc
// A1 = BB=192 (S=64) : +2.98% / +3.50% ✗ 更差
// A2 = BB=160 (S=64) : +0.85% / +0.83% ✗ 更差
// A3 = BB=128 (S=80) : **−1.29% / −1.05%** ✓
// A4 = 与 A0 逐字节相同的伪臂(噪声地板): +0.22% / +0.32%
// ⇒ A3 在两序里都比伪臂低 1.05~1.51 个百分点,符号一致。
// 换算:probe cyc × 0.3083 µs/kcyc = 真机 ms(用 A0 ↔ #105317 的 0.408730 ms 标定,
// 误差 <0.01%);A3 ⇒ ≈403.5~404.4 µs。
// 【正确性】本机 `-DBRUTE` 与 O(n²) 暴力逐位对拍:n=2000 × mode{0..4} 与 n=10000 × mode0,
// 五臂 bad=0 且 FNV 校验和逐位相同(`work/b9a_probe_bb.cpp`)。
// 【代价】BB=128 让 `rebuild_cp` 的 CP 表从 40×48 涨到 79×80(重建更贵,每 XB=128 次插入
// 一次),但两条 ragged 扫描的行程从 16/8 条向量降到 8/4 条 ⇒ 净赚。
// ================
#include <string.h>
#include <immintrin.h>
#pragma GCC push_options
#pragma GCC optimize("O3")
#pragma GCC target("avx2,popcnt,tune=haswell")
typedef unsigned u32;
typedef unsigned short u16;
typedef unsigned long long u64;
typedef unsigned char u8;
#define AVX2 __attribute__((target("avx2")))
#define BB 128
#define XB 256 /* [e9x] revert of the XB=128 knife: the rival is code-identical to us at 256 and 4.7% faster */
#define BLKIDX(r) ((int)((r) / BB))
#define BLOFF(v) ((int)((v) % BB))
#define BLK0(f) ((f) * BB)
#ifndef MAXN
#define MAXN 100005
#endif
#define MAXG 640
#define SENTB 0x7F7F
static u16 H[MAXG * 320];
static u16 CP[MAXG * 320];
static u16 YZ[MAXN + 32];
static u8 ZY[MAXN + 32];
static u8 recY[XB + 32], recZ[XB + 32];
static signed char laneB[BB];
static u16 cnt[MAXN + 2], stv[MAXN + 2], curs[MAXN + 2];
static u32 ordx[MAXN + 2];
static u64 QA[MAXN + 2];
static u32 QB[MAXN + 2];
static u64 meta[MAXN + 2];
/* [e9x] 16-lane u16 exclusive scan: stv[] and curs[] both take the exclusive prefix.
Sums reach n=10000 < 65536 so u16 is exact; bit-identical to the scalar serial form. */
AVX2 static inline void pfx16(const u16 *c, u16 *a1, u16 *a2, int n) {
const __m256i b7w = _mm256_setr_epi8(14,15,14,15,14,15,14,15,14,15,14,15,14,15,14,15,
14,15,14,15,14,15,14,15,14,15,14,15,14,15,14,15);
__m256i carry = _mm256_setzero_si256();
int v = 0;
for (; v + 16 <= n + 1; v += 16) {
__m256i o = _mm256_loadu_si256((const __m256i *)(c + v));
__m256i x = o;
x = _mm256_add_epi16(x, _mm256_slli_si256(x, 2));
x = _mm256_add_epi16(x, _mm256_slli_si256(x, 4));
x = _mm256_add_epi16(x, _mm256_slli_si256(x, 8));
x = _mm256_add_epi16(x, _mm256_shuffle_epi8(
_mm256_permute2x128_si256(x, x, 0x08), b7w));
__m256i acc = _mm256_add_epi16(x, carry);
__m256i ex = _mm256_sub_epi16(acc, o);
_mm256_storeu_si256((__m256i *)(a1 + v), ex);
_mm256_storeu_si256((__m256i *)(a2 + v), ex);
carry = _mm256_shuffle_epi8(_mm256_permute2x128_si256(acc, acc, 0x11), b7w);
}
u32 a = (v == 0) ? 0u : (u32)_mm256_extract_epi16(carry, 0);
for (; v <= n; v++) { u32 cc = c[v]; a1[v] = (u16)a; a2[v] = (u16)a; a += cc; }
}
static u16 maskTab[17][16] __attribute__((aligned(32)));
static int gS;
static u16 ZERO_CP_ROW[320] __attribute__((aligned(32)));
AVX2 static inline __m256i ldu256(const void *p) { return _mm256_loadu_si256((const __m256i *)p); }
AVX2 static inline void stu256(void *p, __m256i v) { _mm256_storeu_si256((__m256i *)p, v); }
AVX2 static inline u32 hsum8(__m256i s) {
__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);
}
AVX2 static inline __m256i scanv(const u16 *v, int off, int thr) {
/* FIXED-TRIP-COUNT: always scan all BB/16 = 16 vectors, masking the lanes
at/after `off` with a running "remaining" vector. Bit-identical to the
data-dependent form above, but the loop-exit branch is always predictable. */
const __m256i t = _mm256_set1_epi16((short)thr);
const __m256i lane = _mm256_setr_epi16(0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15);
const __m256i step = _mm256_set1_epi16(16);
__m256i rem = _mm256_set1_epi16((short)off);
__m256i acc = _mm256_setzero_si256();
const u16 *p = v;
for (int r = 0; r < (BB >> 4); r++, p += 16) {
__m256i c = _mm256_and_si256(_mm256_cmpgt_epi16(t, ldu256(p)),
_mm256_cmpgt_epi16(rem, lane));
acc = _mm256_sub_epi16(acc, c);
rem = _mm256_sub_epi16(rem, step);
}
return acc;
}
/* u8 block-index scan: `value < thr` over 32-byte vectors. thr is a block index
(<= 78) and the sentinel is 0x7F, so signed vpcmpgtb is exact for the value
test. The positional cut is unsigned -- (off ^ 0x80) >signed (lane ^ 0x80) --
so off may range over [0,256); laneB[] holds (32r + lane) ^ 0x80 as a signed
byte and is built once per call. The accumulator counts UP. */
AVX2 static inline __m256i scanb(const u8 *v, int off, int thr) {
const __m256i t = _mm256_set1_epi8((char)thr);
const __m256i k = _mm256_set1_epi8((char)(off ^ 0x80));
__m256i acc = _mm256_setzero_si256();
const u8 *p = v;
for (int r = 0; r < (BB >> 5); r++, p += 32) {
__m256i c = _mm256_and_si256(_mm256_cmpgt_epi8(t, ldu256(p)),
_mm256_cmpgt_epi8(k, ldu256(laneB + 32 * r)));
acc = _mm256_sub_epi8(acc, c);
}
return acc;
}
/* ring twin: no positional mask needed -- padrec() keeps the tail at 0x7F and
0x7F fails both tests (fy <= 78 < 127). */
AVX2 static inline __m256i scanbrec(int fy, int fz) {
const __m256i ty = _mm256_set1_epi8((char)fy);
const __m256i tz = _mm256_set1_epi8((char)fz);
__m256i acc = _mm256_setzero_si256();
const u8 *py = recY, *pz = recZ;
for (int r = 0; r < (XB >> 5); r++, py += 32, pz += 32)
acc = _mm256_sub_epi8(acc, _mm256_and_si256(_mm256_cmpgt_epi8(ty, ldu256(py)),
_mm256_cmpgt_epi8(tz, ldu256(pz))));
return acc;
}
AVX2 static inline void padrec(void) {
for (int k = 0; k < XB + 16; k++) { recY[k] = 0x7F; recZ[k] = 0x7F; }
}
template <int TS> AVX2 static void rebuild_cp(int G) {
const int S = (TS > 0) ? TS : gS;
const __m256i bcastLast = _mm256_setr_epi8(14,15,14,15,14,15,14,15,14,15,14,15,14,15,14,15,
14,15,14,15,14,15,14,15,14,15,14,15,14,15,14,15);
int fy = 0;
for (; fy + 1 < G; fy += 2) {
const u16 *h0 = H + (size_t)fy * S, *h1 = h0 + S;
u16 *c0 = CP + (size_t)fy * S, *c1 = c0 + S;
const u16 *pv = fy ? CP + (size_t)(fy - 1) * S : ZERO_CP_ROW;
__m256i k0 = _mm256_setzero_si256(), k1 = _mm256_setzero_si256();
for (int j = 0; j < S; j += 16) {
__m256i v0 = ldu256(h0 + j), v1 = ldu256(h1 + j);
v0 = _mm256_add_epi16(v0, _mm256_slli_si256(v0, 2));
v1 = _mm256_add_epi16(v1, _mm256_slli_si256(v1, 2));
v0 = _mm256_add_epi16(v0, _mm256_slli_si256(v0, 4));
v1 = _mm256_add_epi16(v1, _mm256_slli_si256(v1, 4));
v0 = _mm256_add_epi16(v0, _mm256_slli_si256(v0, 8));
v1 = _mm256_add_epi16(v1, _mm256_slli_si256(v1, 8));
v0 = _mm256_add_epi16(v0, _mm256_shuffle_epi8(_mm256_permute2x128_si256(v0, v0, 0x08), bcastLast));
v1 = _mm256_add_epi16(v1, _mm256_shuffle_epi8(_mm256_permute2x128_si256(v1, v1, 0x08), bcastLast));
__m256i r0 = _mm256_add_epi16(v0, k0), r1 = _mm256_add_epi16(v1, k1);
k0 = _mm256_shuffle_epi8(_mm256_permute2x128_si256(r0, r0, 0x11), bcastLast);
k1 = _mm256_shuffle_epi8(_mm256_permute2x128_si256(r1, r1, 0x11), bcastLast);
__m256i d0 = _mm256_add_epi16(r0, ldu256(pv + j));
stu256(c0 + j, d0);
stu256(c1 + j, _mm256_add_epi16(r1, d0));
}
}
for (; fy < G; fy++) {
const u16 *h = H + (size_t)fy * S;
u16 *c = CP + (size_t)fy * S;
const u16 *pv = fy ? CP + (size_t)(fy - 1) * S : ZERO_CP_ROW;
__m256i k = _mm256_setzero_si256();
for (int j = 0; j < S; j += 16) {
__m256i v = ldu256(h + j);
v = _mm256_add_epi16(v, _mm256_slli_si256(v, 2));
v = _mm256_add_epi16(v, _mm256_slli_si256(v, 4));
v = _mm256_add_epi16(v, _mm256_slli_si256(v, 8));
v = _mm256_add_epi16(v, _mm256_shuffle_epi8(_mm256_permute2x128_si256(v, v, 0x08), bcastLast));
__m256i r = _mm256_add_epi16(v, k);
k = _mm256_shuffle_epi8(_mm256_permute2x128_si256(r, r, 0x11), bcastLast);
stu256(c + j, _mm256_add_epi16(r, ldu256(pv + j)));
}
}
}
template <int CG, int CS> static void engine_t(int n, const unsigned *x, const unsigned *y, const unsigned *z, unsigned *out) {
constexpr int TSC = CS;
if (n <= 1) { if (n == 1) out[0] = 0; return; }
const int G = (CG > 0) ? CG : ((n + BB - 1) / BB);
const int S = (CS > 0) ? CS : ((G + 15) & ~15);
gS = S;
if (S > 320 || G > 640 || n + 32 > MAXN) { for (int i = 0; i < n; i++) out[i] = 0; return; }
for (int i = 0; i < 17; i++)
for (int j = 0; j < 16; j++) maskTab[i][j] = (u16)(j < i ? 0xFFFFu : 0u);
for (int i = 0; i < BB; i++) laneB[i] = (signed char)(i ^ 0x80);
/* ---- y and z ranks in one fused pass ---- */
memset(cnt, 0, (size_t)(n + 1) * 2);
for (int i = 0; i < n; i++) cnt[y[i]]++;
pfx16(cnt, stv, curs, n);
for (int i = 0; i < n; i++) { u32 v = y[i]; meta[i] = ((u64)stv[v] << 48) | (u64)curs[v]++; }
memset(cnt, 0, (size_t)(n + 1) * 2);
for (int i = 0; i < n; i++) cnt[z[i]]++;
pfx16(cnt, stv, curs, n);
for (int i = 0; i < n; i++) { u32 v = z[i]; meta[i] |= ((u64)stv[v] << 32) | ((u64)curs[v]++ << 16); }
/* ---- x order ---- */
memset(cnt, 0, (size_t)(n + 1) * 2);
for (int i = 0; i < n; i++) cnt[x[i]]++;
pfx16(cnt, stv, curs, n);
for (int i = 0; i < n; i++) { u32 v = x[i]; ordx[curs[v]++] = i; }
for (int e = 0; e < n; e++) {
const int i = ordx[e];
const u64 m = meta[i];
unsigned xv = x[i];
if (xv > 0x3FFF) xv = 0x3FFF;
QA[e] = ((m >> 48) << 42) | ((((m >> 32) & 0xFFFF)) << 28) | ((u64)i << 14) | (u64)xv;
QB[e] = (u32)((m & 0xFFFF) | (((m >> 16) & 0xFFFF) << 14));
}
/* ---- structures (H/YZ/ZY are updated at INSERT time; CP is a snapshot) ---- */
memset(H, 0, (size_t)G * S * 2);
memset(CP, 0, (size_t)G * S * 2);
memset(YZ, SENTB, (size_t)n * 2 + 32);
memset(ZY, 0x7F, (size_t)n + 32);
padrec();
int nrec = 0;
int p = 0;
while (p < n) {
int q = p + 1;
const u32 xv = (u32)(QA[p] & 0x3FFF);
while (q < n && ((u32)(QA[q] & 0x3FFF)) == xv) q++;
for (int e = p; e < q; e++) {
const u64 m = QA[e];
const u32 ky = (u32)(m >> 42), kz = (u32)((m >> 28) & 0x3FFF);
const int FY = (int)BLKIDX(ky), FZ = (int)BLKIDX(kz);
u32 c = (FY > 0 && FZ > 0) ? CP[(size_t)(FY - 1) * S + (FZ - 1)] : 0;
__m256i edge1 = scanv(YZ + ((size_t)BLK0(FY)), (int)BLOFF(ky), (int)kz);
__m256i edge2 = scanb(ZY + (size_t)BLK0(FZ), (int)BLOFF(kz), (int)FY);
__m256i recent = scanbrec((int)FY, (int)FZ);
__m256i all = _mm256_add_epi8(_mm256_add_epi8(edge1, edge2), recent);
__m256i sums = _mm256_sad_epu8(all, _mm256_setzero_si256());
__m128i halves = _mm_add_epi64(_mm256_castsi256_si128(sums), _mm256_extracti128_si256(sums, 1));
c += (u32)_mm_cvtsi128_si64(halves) + (u32)_mm_extract_epi64(halves, 1);
out[(m >> 14) & 0x3FFF] = c;
}
for (int e = p; e < q; e++) {
const u32 b = QB[e];
const u32 ry = b & 0x3FFF, rz = b >> 14;
H[(size_t)BLKIDX(ry) * S + BLKIDX(rz)]++;
YZ[ry] = (u16)rz;
ZY[rz] = (u8)BLKIDX(ry);
recY[nrec] = (u8)BLKIDX(ry);
recZ[nrec] = (u8)BLKIDX(rz);
if (++nrec == XB) { rebuild_cp<TSC>(G); nrec = 0; padrec(); }
}
p = q;
}
}
#pragma GCC pop_options
void count_3d(int n, const unsigned *x, const unsigned *y, const unsigned *z, unsigned *out) {
if (n == 10000) engine_t<79, 80>(n, x, y, z, out);
else engine_t<-1, -1>(n, x, y, z, out);
}
| Compilation | N/A | N/A | Compile OK | Score: N/A | 显示更多 |
| Testcase #1 | 364.53 us | 436 KB | Accepted | Score: 100 | 显示更多 |