提交记录 120513


用户 题目 状态 得分 用时 内存 语言 代码长度
saffah_cc_v41_agg1 1012. 测测你的六维数点 Accepted 100 3.653 s 100960 KB C++17 41.77 KB
提交时间 评测时间
2026-10-02 19:06:59 2026-10-02 19:07:07
// ===== REFERENCES =====
// [1] duck.ac 用户 saffah_codex_6a_agg3, 提交 #120433 <https://duck.ac/submission/120433>
//     用途:**逐字复制**该提交的全部实现 —— 即本题现役最快的 KG / CDQ 引擎:事件序列
//     分治(u64 内联域 [ID:20][KB:20][TY:1][KA:20])、位置二分树递归到 KG_BASE=3072
//     的 4-查询组 AVX2 叶(MF_LEAFG=2 / MF_LEAFLP=128)、D=KT-3 处融合的四层前缀
//     计数器 [10ez6-L4](增量掩码 SFX16/SFX32/SFX32W + 批量清零 ctr_reset_hi)。
//     抄的是整个文件;本发只在其上做下述一处单变量改动。
//     (该件自身头段声明其来源:CDQ 引擎承自 saffah_cc_v41_260924 #117669
//       <https://duck.ac/submission/117669>;四层计数器/增量掩码/批量清零承自
//       本账号 saffah_cc_v41_agg1 #112070 <https://duck.ac/submission/112070>
//       —— 本次一并如实转引。)
// [2] 本账号, 提交 #117444 <https://duck.ac/submission/117444>(`x12aa_sub_c1.cpp`)
//     用途:本次叠加的 [C1] 刀 = 该件的唯一变量(cross 重定键从合并环外提为一趟
//     独立扫描)。该刀在判题机已定价:#117426 = 3974.468325 ms(= #117412 的逐字
//     复刻,同批同底座锚)→ #117444 = 3682.330464 ms,即 **−292.138 ms(−7.35%)**。
// ======================
// ===== 思路 =====
// 单变量:把自家已定价的 [C1] 刀,原样叠到对手最新件(#120433)的正文上。除此之外
// 一字未动(对手件正文 = 本件正文,除下述三处)。目的:对手件是当前 T(3598.223054),
// 严支线 3562.241823 ⇒ 需再快 1.00%;[C1] 在自家基座上实测值 7.35%,本发即验证
// "该刀在对手新基座上仍为正"。
//
// [C1] 改了什么(3 处、自包含):
//  * 合并环(solve_b 的 while(ia<h && ib<r) 主循环)里,原本对每个事件(**包括约一半
//    最终不进 cross 的事件**)都算一次重定键表达式(KA←KB 的位搬移 + 经 XN 的
//    4 MB 坐标行随机 gather)。本发改成:合并环只写原始事件 `cross[cx] = v`。
//  * 两个尾巴环(retA 剩余 / retB 剩余)同样只写原始事件。
//  * 在 cross 被消费(ctr_solve 或 solve_t<KT,D+1> 递归)**之前**插一趟独立扫描,
//    对 cross[] 全体逐元素施用与原来**逐字相同**的表达式 ⇒ 构造上逐位等价
//    (同一表达式、同一施用顺序,只是延后到合并完成之后)。
//
// 为什么为正(机理):(a) gather 次数减半(只对保留事件发);(b) gather 从挂在 cmov
// 串行链上的"顺带副作用"变成一趟无携带依赖的独立环,乱序窗口可以同时挂多个未命中
// (MLP);(c) 合并环体变短(这正是 25~33% 指令占比的那两个函数)。
//
// 闸门(本机,g++ 15.2 -O2 -std=c++17,驱动 = 题内长期沿用的 base.cpp 数据生成器):
//  * 与对手件**逐位一致**:n=1e6、seed=1/2/3、mode10(判题机真实工作点尺度)
//    checksum+FNV hash 三对全同(seed1: 15581398097 与生产格在册值相同);
//  * 暴力对拍:k=4/5/6 × n=1000/3000 × mode=0..4 全 OK(30/30),
//    另 k=6 × n=20000/40000 × mode=0/1/3 全 OK(覆盖到 D=3 层的合并);
//  * 本地 A/B(同 harness 交错):两向皆现、符号不判(BRIEF §2.18.363:本机对
//    访存形状类改动无判别力)⇒ 定价全交判题机。
// ================
/* References:
 * saffah_cc_v41_260924, https://duck.ac/submission/117669:
 * retained its complete CDQ engine and inherited citations.
 * saffah_cc_v41_agg1, https://duck.ac/submission/112070:
 * ported the four-level cumulative counter, increment masks and bulk reset.
 * No separate software license notices appeared on the public submissions.
 * Idea: Transfer the accepted four-upper-level hierarchy into this dimension/size
 * engine, adjusting counter prefetch indices and reset bounds consistently.
 * Queries use one fewer upper-level load; inserts use one fewer update.
 * Purpose: Official experiment of a shared counter improvement across related tasks.
 */
// ARMVAR KG_BASE=2048 MF_CTRPF=32
#define KG_BASE 3072
#define MF_CTRPF 32
#ifndef MF_CTRPF
#define MF_CTRPF 0
#endif
#ifndef MF_CTR5
#define MF_CTR5 1
#endif
#ifndef MF_LEAFLP
#define MF_LEAFLP 128
#endif
#ifndef MF_LEAFG
#define MF_LEAFG 2
#endif
#pragma GCC optimize("O3","unroll-loops")
#pragma GCC target("avx2,bmi,bmi2,popcnt,lzcnt")
// duck.ac 1010 (4D) / 1011 (5D) -- "count for every point the number of points that are
// smaller in *every* dimension"  (n = 300000, coords in [0,n), strict comparisons).
//
// Algorithm: D&C over dimensions on an event sequence, keys carried inline.
//   * 2n events: one query event + one insert event per point, keyed by dim 0
//     (a query event is stored *before* an insert event of the same key, which encodes
//      the strict-inequality rule).
//   * solve(seq,m,d): seq is ordered by dim d and every event carries the coordinate of
//     dim d+1 in bits 32..63 of its u64.  Split by position, recurse both halves, then
//     merge the two halves by the carried key (a plain unsigned `<` on the packed u64,
//     so equal keys keep query-before-insert order); the same pass collects the cross
//     events (left inserts + right queries), re-keys them to dim d+2 and recurses one
//     dimension deeper.  Ordering by dim d with that tie rule makes "insert before query"
//     exactly "all dims so far strictly smaller", and equal coordinates are never counted.
//   * d == K-1: every insert-before-query pair now qualifies, so one O(m) running counter
//     finishes the innermost dimension (no log factor).
//   * m <= 32: direct O(m^2) counting plus an insertion sort.
//
// Local timings (i5-6500 @ 3.2GHz, best of 3, n = 300000 uniform random):
//     4D 1.57 s      5D 3.85 s
// (Board #1: 508.596 ms / 745.358 ms -- this is still 3x / 5x behind.)
// k-dimensional dominance counting: out[i] = #{j : all dims of j < dims of i}
//
// Event-sequence CDQ with keys carried inline.
//   event (u64) = (key << 32) | (ins << 31) | id        ins = 1 for an "insert" event
//   key = coordinate of dim (level+1): the field used by this level's merge.
//   The packing makes a plain unsigned comparison put, for equal keys, the query
//   event first -- exactly the strict-inequality rule (equal coords must not count).
//
//   solve(seq, m, d): seq is ordered by dim d (tie rule above), elements carry the
//   key for dim d+1; count pairs (insert before query) with dims d+1..K-1 strictly
//   smaller; return `out` = the same events merged by the carried key (= dim d+1).
//   Cross = A-inserts + B-queries re-keyed to dim d+2, recursed at level d+1.
//   d == K-1 : every (insert before query) pair qualifies -> running counter, O(m).
#include <cstring>
#include <immintrin.h>
#pragma GCC push_options
#pragma GCC target("avx2,popcnt,bmi2")
#include <cstdint>

typedef unsigned u32;
typedef uint64_t u64;
typedef unsigned char u8;
typedef unsigned short u16;

#ifndef KG_COND
#define KG_COND 0
#endif
#ifndef KG_PF
#define KG_PF 64
#endif
#ifndef KG_BASE
#define KG_BASE 4096
#endif
#ifndef LEAFNB
#define LEAFNB 4096
#endif
#define KG_MAXK 6
#define KG_ARENA (160u << 20)

namespace kg {

// ------------ fused innermost-dimension counter (prefix count over [0,n)) -------------
// AVX2.  Levels: u8 leaf /32 values, u16 per 32 and per 256, u32 per 2048/16384/131072.
// Every u16 block count must stay <= 32767 (madd uses signed lanes); the guard (max
// multiplicity of a last-dim coordinate <= 127) bounds 32*127=4064 and 256*127=32512.
#define CT_MAXV 1048580u
static u8  CT0[CT_MAXV];
/* [10ez6-L4] four upper layers instead of five: bits 5-8 / 9-12 / 13-16 / 17-19 (leaf = bits 0-4).
   Same suffix-ladder semantics, one fewer scalar load per query and one fewer RMW per insert. */
static u16 CT1[CT_MAXV / 32 + 64] __attribute__((aligned(64)));
static u16 CT2[CT_MAXV / 512 + 64] __attribute__((aligned(64)));
static u32 CT3[CT_MAXV / 8192 + 64] __attribute__((aligned(64)));
static u32 CT4[CT_MAXV / 131072 + 16] __attribute__((aligned(64)));
// [SFX] suffix-counter increment masks.  Row r holds a 1 in every lane strictly
// above r, so an insert landing in block r adds one to each suffix position of
// r's 8-block group that must start counting it.
static const u16 INC16[16][16] __attribute__((aligned(64))) = { {0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1},{0,0,1,1,1,1,1,1,1,1,1,1,1,1,1,1},{0,0,0,1,1,1,1,1,1,1,1,1,1,1,1,1},{0,0,0,0,1,1,1,1,1,1,1,1,1,1,1,1},{0,0,0,0,0,1,1,1,1,1,1,1,1,1,1,1},{0,0,0,0,0,0,1,1,1,1,1,1,1,1,1,1},{0,0,0,0,0,0,0,1,1,1,1,1,1,1,1,1},{0,0,0,0,0,0,0,0,1,1,1,1,1,1,1,1},{0,0,0,0,0,0,0,0,0,1,1,1,1,1,1,1},{0,0,0,0,0,0,0,0,0,0,1,1,1,1,1,1},{0,0,0,0,0,0,0,0,0,0,0,1,1,1,1,1},{0,0,0,0,0,0,0,0,0,0,0,0,1,1,1,1},{0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,1},{0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1},{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0} };
static const u32 INC32W[16][16] __attribute__((aligned(64))) = { {0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1},{0,0,1,1,1,1,1,1,1,1,1,1,1,1,1,1},{0,0,0,1,1,1,1,1,1,1,1,1,1,1,1,1},{0,0,0,0,1,1,1,1,1,1,1,1,1,1,1,1},{0,0,0,0,0,1,1,1,1,1,1,1,1,1,1,1},{0,0,0,0,0,0,1,1,1,1,1,1,1,1,1,1},{0,0,0,0,0,0,0,1,1,1,1,1,1,1,1,1},{0,0,0,0,0,0,0,0,1,1,1,1,1,1,1,1},{0,0,0,0,0,0,0,0,0,1,1,1,1,1,1,1},{0,0,0,0,0,0,0,0,0,0,1,1,1,1,1,1},{0,0,0,0,0,0,0,0,0,0,0,1,1,1,1,1},{0,0,0,0,0,0,0,0,0,0,0,0,1,1,1,1},{0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,1},{0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1},{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0} };
static const u32 INC32[8][8] __attribute__((aligned(32))) = {
  {0,1,1,1,1,1,1,1},{0,0,1,1,1,1,1,1},{0,0,0,1,1,1,1,1},{0,0,0,0,1,1,1,1},
  {0,0,0,0,0,1,1,1},{0,0,0,0,0,0,1,1},{0,0,0,0,0,0,0,1},{0,0,0,0,0,0,0,0}};
#define SFX16(p, r) do { __m256i _a = _mm256_load_si256((const __m256i *)(p)); \
  __m256i _b = _mm256_load_si256((const __m256i *)(((const u16 *)INC16) + 16 * (size_t)(r))); \
  _mm256_store_si256((__m256i *)(p), _mm256_add_epi16(_a, _b)); } while (0)
#define SFX32(p, r) do { __m256i _a = _mm256_load_si256((const __m256i *)(p)); \
  __m256i _b = _mm256_load_si256((const __m256i *)(INC32 + (r))); \
  _mm256_store_si256((__m256i *)(p), _mm256_add_epi32(_a, _b)); } while (0)
#define SFX32W(p, r) do { __m256i _a = _mm256_load_si256((const __m256i *)(p)); \
  __m256i _c = _mm256_load_si256((const __m256i *)(((const u32 *)INC32W) + 16 * (size_t)(r))); \
  _mm256_store_si256((__m256i *)(p), _mm256_add_epi32(_a, _c)); \
  __m256i _d = _mm256_load_si256((const __m256i *)(p) + 1); \
  __m256i _e = _mm256_load_si256((const __m256i *)(((const u32 *)INC32W) + 16 * (size_t)(r)) + 1); \
  _mm256_store_si256((__m256i *)(p) + 1, _mm256_add_epi32(_d, _e)); } while (0)
static int FUSE_OK;

static inline u32 su8_32(const u8 *p, int cnt) {
  __m256i v = _mm256_loadu_si256((const __m256i *)p);
  __m256i m = _mm256_cmpgt_epi8(_mm256_set1_epi8((char)cnt),
      _mm256_setr_epi8(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15,
                       16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31));
  __m256i x = _mm256_and_si256(v, m);
  __m256i s = _mm256_sad_epu8(x, _mm256_setzero_si256());
  __m128i t = _mm_add_epi64(_mm256_castsi256_si128(s), _mm256_extracti128_si256(s, 1));
  return (u32)((unsigned long long)_mm_cvtsi128_si64(t) + (unsigned long long)_mm_extract_epi64(t, 1));
}
static inline u32 su16_8(const u16 *p, int cnt) {
  __m128i v = _mm_loadu_si128((const __m128i *)p);
  __m128i x = _mm_and_si128(v, _mm_cmpgt_epi16(_mm_set1_epi16((short)cnt),
      _mm_setr_epi16(0, 1, 2, 3, 4, 5, 6, 7)));
  __m128i s = _mm_madd_epi16(x, _mm_set1_epi16(1));
  s = _mm_add_epi32(s, _mm_shuffle_epi32(s, _MM_SHUFFLE(1, 0, 3, 2)));
  s = _mm_add_epi32(s, _mm_shuffle_epi32(s, _MM_SHUFFLE(2, 3, 0, 1)));
  return (u32)_mm_cvtsi128_si32(s);
}
static inline u32 su32_8(const u32 *p, int cnt) {
  __m256i v = _mm256_loadu_si256((const __m256i *)p);
  __m256i x = _mm256_and_si256(v, _mm256_cmpgt_epi32(_mm256_set1_epi32(cnt),
      _mm256_setr_epi32(0, 1, 2, 3, 4, 5, 6, 7)));
  __m128i t = _mm_add_epi32(_mm256_castsi256_si128(x), _mm256_extracti128_si256(x, 1));
  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 ctr_ins(u32 v) {
  CT0[v]++;
  SFX16(CT1 + ((v >> 9) << 4), (v >> 5) & 15);
  SFX16(CT2 + ((v >> 13) << 4), (v >> 9) & 15);
  SFX32W(CT3 + ((v >> 17) << 4), (v >> 13) & 15);
  SFX32(CT4, (v >> 17) & 7);
}
static inline void ctr_clr(u32 v) {
  CT0[v]--;   // [CTRR] CT1..CT5 are restored in bulk by ctr_reset_hi()
}
// [CTRR] live bound of the coordinate values seen by ctr_* (set in run_t)
static u32 CTN;
static inline void ctr_reset_hi(void) {
  memset(CT1, 0, sizeof(u16) * (((size_t)CTN >> 5) + 32));
  memset(CT2, 0, sizeof(u16) * (((size_t)CTN >> 9) + 32));
  memset(CT3, 0, sizeof(u32) * (((size_t)CTN >> 13) + 32));
  memset(CT4, 0, sizeof(CT4));
}
static inline u32 ctr_qry(u32 v) {
  u32 s = CT4[v >> 17];
  s += CT3[v >> 13];
  s += CT2[v >> 9];
  s += CT1[v >> 5];
  s += su8_32(CT0 + (v & ~31u), (int)(v & 31));
  return s;
}
static int K;
#ifndef LFAUTO
#define LFAUTO 0
#endif
static int G_LIM;
static int LFSH;
static int LFD;
static const u32 *XD[KG_MAXK];
static u32 *OUT;
static u64 *AB, *ABtop;

// ===== v2 PACKED EVENT =====================================================
//  v = [KA:20][TY:1][KB:20][ID:20]   (bits 43..62, 42, 22..41, 0..19)
//  KA = the compare key for THIS level (dim D+1), KB = the key for the NEXT
//  level (dim D+2), carried so the cross needs no re-key gather.
//  A plain u64 compare gives (KA, TY, KB, ID) -- KA primary, TY breaks equal
//  KA with query(TY=0) before insert(TY=1) = the strict-inequality rule.
//  Requires id < 2^20 and every coordinate < 2^20 (true: n = 1e6 < 2^20).
#define E_ID(v) ((u32)((v) & 0xFFFFFu))
#define E_KB(v) ((u32)(((v) >> 22) & 0xFFFFFu))
#define E_TY(v) ((u32)(((v) >> 42) & 1u))
#define E_KA(v) ((u32)((v) >> 43))
static inline u64 mk2(u32 ka, u32 ty, u32 kb, u32 id) {
  return ((u64)ka << 43) | ((u64)ty << 42) | ((u64)kb << 22) | (u64)id;
}
static inline int eid(u64 v) { return (int)(v & 0x7FFFFFFFu); }
static inline int lfbucket(u64 v) { return (int)(((u32)(v >> 32)) >> LFSH); }
static inline int eq_(u64 v) { return (int)((v >> 31) & 1); }
static inline int keep_test(int takeA, int role) { return 1 - (takeA ^ role); }
static inline u64 mk(u32 key, int q, int id) {
  return ((u64)key << 32) | ((u64)(1 - q) << 31) | (u64)id;
}


// resolve a fusion-level cross: events carry the last dim (K-1) coordinate in bits 32..63
// and are ordered by dim K-2; a left-insert before a right-query with a strictly smaller
// last-dim value is a hit.
static void ctr_solve(u64 *cross, int cx, u32 *dirt) {
  int nd = 0;
  for (int i = 0; i < cx; i++) {
#if MF_CTRPF > 0
    if (i + MF_CTRPF < cx) {
      u32 vf = E_KA(cross[i + MF_CTRPF]);
      __builtin_prefetch(&CT0[vf], 1, 0);
      __builtin_prefetch(&CT1[vf >> 5], 1, 0);
      __builtin_prefetch(&CT2[vf >> 9], 1, 0);
    }
#endif
    u64 v = cross[i];
    u32 val = E_KA(v);   // v2: at the fuse level the counter key is dim K-1, and a
                         // cross event has just had KB promoted into the KA slot
    if (E_TY(v)) { ctr_ins(val); dirt[nd++] = val; }
    else OUT[E_ID(v)] += ctr_qry(val);
  }
  for (int i = 0; i < nd; i++) {
#if MF_CTRPF > 0
    if (i + MF_CTRPF < nd) {
      u32 vf = dirt[i + MF_CTRPF];
      __builtin_prefetch(&CT0[vf], 1, 1);


    }
#endif
    ctr_clr(dirt[i]);
  }
  ctr_reset_hi();
}


// small base: compact insert list + SIMD comparison, then insertion sort by the key
static const u32 VMASK[8][8] = {
  {0,0,0,0,0,0,0,0},
  {~0u,0,0,0,0,0,0,0},
  {~0u,~0u,0,0,0,0,0,0},
  {~0u,~0u,~0u,0,0,0,0,0},
  {~0u,~0u,~0u,~0u,0,0,0,0},
  {~0u,~0u,~0u,~0u,~0u,0,0,0},
  {~0u,~0u,~0u,~0u,~0u,~0u,0,0},
  {~0u,~0u,~0u,~0u,~0u,~0u,~0u,0}};
static inline u32 hsum256(__m256i v) {
  __m128i t = _mm_add_epi32(_mm256_castsi256_si128(v), _mm256_extracti128_si256(v, 1));
  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);
}
template<int ND>
static void base_case_t(u64 *seq, int m, int d, u64 *out, int needOut) {
  const int nd = ND;
  __attribute__((aligned(32)))
  u32 IK[KG_BASE + 8], IC0[KG_BASE + 8], IC1[KG_BASE + 8], IC2[KG_BASE + 8], IC3[KG_BASE + 8];
  u32 QK[KG_BASE + 8], QC0[KG_BASE + 8], QC1[KG_BASE + 8], QC2[KG_BASE + 8], QC3[KG_BASE + 8];
  u32 QID[KG_BASE + 8], QN[KG_BASE + 8];
  const u32 *X1 = nd > 1 ? XD[d + 3] : 0, *X2 = nd > 2 ? XD[d + 4] : 0,
            *X3 = nd > 3 ? XD[d + 5] : 0;
  int ni = 0, nq = 0;
  for (int t = 0; t < m; t++) {
#if MF_LEAFLP > 0
    if (t + MF_LEAFLP < m) {
      int idf = (int)E_ID(seq[t + MF_LEAFLP]);
      if (nd > 1) __builtin_prefetch(&X1[idf], 0, 1);
      if (nd > 2) __builtin_prefetch(&X2[idf], 0, 1);
      if (nd > 3) __builtin_prefetch(&X3[idf], 0, 1);
    }
#endif
    u64 v = seq[t];
    int id = (int)E_ID(v);
    u32 k = E_KA(v);
    u32 c0 = 0, c1 = 0, c2 = 0, c3 = 0;
    if (nd > 0) c0 = E_KB(v);          // dim d+2 is IN the event -- no gather
    if (nd > 1) c1 = X1[id];
    if (nd > 2) c2 = X2[id];
    if (nd > 3) c3 = X3[id];
    int isins = (int)E_TY(v);
    IK[ni] = k; IC0[ni] = c0; IC1[ni] = c1; IC2[ni] = c2, IC3[ni] = c3; ni += isins;
    QK[nq] = k; QC0[nq] = c0; QC1[nq] = c1; QC2[nq] = c2, QC3[nq] = c3; QID[nq] = (u32)id; QN[nq] = (u32)ni; nq += 1 - isins;
  }
#if MF_LEAFG >= 2
  /* c18/4-query group: one insert-block load feeds 4 chains */
  #define MIN4(a0,a1,a2,a3) ((((a0) < (a1) ? (a0) : (a1))) < (((a2) < (a3) ? (a2) : (a3))) ? (((a0) < (a1) ? (a0) : (a1))) : (((a2) < (a3) ? (a2) : (a3))))
  int q = 0;
  for (; q + 4 <= nq; q += 4) {
    const int n = MIN4((int)QN[q], (int)QN[q + 1], (int)QN[q + 2], (int)QN[q + 3]);
    const int nA = (int)QN[q], nB = (int)QN[q + 1];
    __m256i kvA = _mm256_set1_epi32((int)QK[q]),     kvB = _mm256_set1_epi32((int)QK[q + 1]);
    __m256i v0A = _mm256_set1_epi32((int)QC0[q]),    v0B = _mm256_set1_epi32((int)QC0[q + 1]);
    __m256i v1A = _mm256_set1_epi32((int)QC1[q]),    v1B = _mm256_set1_epi32((int)QC1[q + 1]);
    __m256i v2A = _mm256_set1_epi32((int)QC2[q]),    v2B = _mm256_set1_epi32((int)QC2[q + 1]);
    __m256i v3A = _mm256_set1_epi32((int)QC3[q]),    v3B = _mm256_set1_epi32((int)QC3[q + 1]);
    __m256i accA = _mm256_setzero_si256(), accB = _mm256_setzero_si256();
    const int nC = (int)QN[q + 2], nD = (int)QN[q + 3];
    __m256i kvC = _mm256_set1_epi32((int)QK[q + 2]),     kvD = _mm256_set1_epi32((int)QK[q + 3]);
    __m256i v0C = _mm256_set1_epi32((int)QC0[q + 2]),    v0D = _mm256_set1_epi32((int)QC0[q + 3]);
    __m256i v1C = _mm256_set1_epi32((int)QC1[q + 2]),    v1D = _mm256_set1_epi32((int)QC1[q + 3]);
    __m256i v2C = _mm256_set1_epi32((int)QC2[q + 2]),    v2D = _mm256_set1_epi32((int)QC2[q + 3]);
    __m256i v3C = _mm256_set1_epi32((int)QC3[q + 2]),    v3D = _mm256_set1_epi32((int)QC3[q + 3]);
    __m256i accC = _mm256_setzero_si256(), accD = _mm256_setzero_si256();
    int j = 0;
    if (nd >= 3) {
      for (; j + 8 <= n; j += 8) {
        __m256i IKv = _mm256_load_si256((const __m256i *)(IK + j));
        __m256i C0v = _mm256_load_si256((const __m256i *)(IC0 + j));
        __m256i C1v = _mm256_load_si256((const __m256i *)(IC1 + j));
        __m256i C2v = _mm256_load_si256((const __m256i *)(IC2 + j));
        __m256i mA = _mm256_and_si256(_mm256_cmpgt_epi32(kvA, IKv), _mm256_cmpgt_epi32(v0A, C0v));
        __m256i mC = _mm256_and_si256(_mm256_cmpgt_epi32(kvC, IKv), _mm256_cmpgt_epi32(v0C, C0v));
        __m256i mB = _mm256_and_si256(_mm256_cmpgt_epi32(kvB, IKv), _mm256_cmpgt_epi32(v0B, C0v));
        __m256i mD = _mm256_and_si256(_mm256_cmpgt_epi32(kvD, IKv), _mm256_cmpgt_epi32(v0D, C0v));
        mA = _mm256_and_si256(mA, _mm256_cmpgt_epi32(v1A, C1v));
        mC = _mm256_and_si256(mC, _mm256_cmpgt_epi32(v1C, C1v));
        mB = _mm256_and_si256(mB, _mm256_cmpgt_epi32(v1B, C1v));
        mD = _mm256_and_si256(mD, _mm256_cmpgt_epi32(v1D, C1v));
        mA = _mm256_and_si256(mA, _mm256_cmpgt_epi32(v2A, C2v));
        mC = _mm256_and_si256(mC, _mm256_cmpgt_epi32(v2C, C2v));
        mB = _mm256_and_si256(mB, _mm256_cmpgt_epi32(v2B, C2v));
        mD = _mm256_and_si256(mD, _mm256_cmpgt_epi32(v2D, C2v));
        if (nd > 3) {   /* m161i key 3 */
          __m256i C3v = _mm256_load_si256((const __m256i *)(IC3 + j));
          mA = _mm256_and_si256(mA, _mm256_cmpgt_epi32(v3A, C3v));
          mC = _mm256_and_si256(mC, _mm256_cmpgt_epi32(v3C, C3v));
          mB = _mm256_and_si256(mB, _mm256_cmpgt_epi32(v3B, C3v));
          mD = _mm256_and_si256(mD, _mm256_cmpgt_epi32(v3D, C3v));
        }
        accA = _mm256_sub_epi32(accA, mA);
        accC = _mm256_sub_epi32(accC, mC);
        accB = _mm256_sub_epi32(accB, mB);
        accD = _mm256_sub_epi32(accD, mD);
      }
    } else {
      for (; j + 8 <= n; j += 8) {
        __m256i IKv = _mm256_load_si256((const __m256i *)(IK + j));
        __m256i mA = _mm256_cmpgt_epi32(kvA, IKv);
        __m256i mC = _mm256_cmpgt_epi32(kvC, IKv);
        __m256i mB = _mm256_cmpgt_epi32(kvB, IKv);
        __m256i mD = _mm256_cmpgt_epi32(kvD, IKv);
        if (nd > 0) {
          __m256i C0v = _mm256_load_si256((const __m256i *)(IC0 + j));
          mA = _mm256_and_si256(mA, _mm256_cmpgt_epi32(v0A, C0v));
          mC = _mm256_and_si256(mC, _mm256_cmpgt_epi32(v0C, C0v));
          mB = _mm256_and_si256(mB, _mm256_cmpgt_epi32(v0B, C0v));
          mD = _mm256_and_si256(mD, _mm256_cmpgt_epi32(v0D, C0v));
        }
        if (nd > 1) {
          __m256i C1v = _mm256_load_si256((const __m256i *)(IC1 + j));
          mA = _mm256_and_si256(mA, _mm256_cmpgt_epi32(v1A, C1v));
          mC = _mm256_and_si256(mC, _mm256_cmpgt_epi32(v1C, C1v));
          mB = _mm256_and_si256(mB, _mm256_cmpgt_epi32(v1B, C1v));
          mD = _mm256_and_si256(mD, _mm256_cmpgt_epi32(v1D, C1v));
        }
        accA = _mm256_sub_epi32(accA, mA);
        accC = _mm256_sub_epi32(accC, mC);
        accB = _mm256_sub_epi32(accB, mB);
        accD = _mm256_sub_epi32(accD, mD);
      }
    }
    {
      int jj = j;
      __m256i acc = accA;
      for (; jj + 8 <= nA; jj += 8) {
        __m256i msk = _mm256_cmpgt_epi32(kvA, _mm256_load_si256((const __m256i *)(IK + jj)));
        if (nd > 0) msk = _mm256_and_si256(msk, _mm256_cmpgt_epi32(v0A, _mm256_load_si256((const __m256i *)(IC0 + jj))));
        if (nd > 1) msk = _mm256_and_si256(msk, _mm256_cmpgt_epi32(v1A, _mm256_load_si256((const __m256i *)(IC1 + jj))));
        if (nd > 2) msk = _mm256_and_si256(msk, _mm256_cmpgt_epi32(v2A, _mm256_load_si256((const __m256i *)(IC2 + jj))));
        if (nd > 3) msk = _mm256_and_si256(msk, _mm256_cmpgt_epi32(v3A, _mm256_load_si256((const __m256i *)(IC3 + jj))));
        acc = _mm256_sub_epi32(acc, msk);
      }
      if (jj < nA) {
        int n = nA;
        __m256i msk = _mm256_cmpgt_epi32(kvA, _mm256_load_si256((const __m256i *)(IK + jj)));
        if (nd > 0) msk = _mm256_and_si256(msk, _mm256_cmpgt_epi32(v0A, _mm256_load_si256((const __m256i *)(IC0 + jj))));
        if (nd > 1) msk = _mm256_and_si256(msk, _mm256_cmpgt_epi32(v1A, _mm256_load_si256((const __m256i *)(IC1 + jj))));
        if (nd > 2) msk = _mm256_and_si256(msk, _mm256_cmpgt_epi32(v2A, _mm256_load_si256((const __m256i *)(IC2 + jj))));
        if (nd > 3) msk = _mm256_and_si256(msk, _mm256_cmpgt_epi32(v3A, _mm256_load_si256((const __m256i *)(IC3 + jj))));
        msk = _mm256_and_si256(msk, _mm256_loadu_si256((const __m256i *)VMASK[n - jj]));
        acc = _mm256_sub_epi32(acc, msk);
      }
      OUT[QID[q]] += hsum256(acc);
    }
    {
      int jj = j;
      __m256i acc = accB;
      for (; jj + 8 <= nB; jj += 8) {
        __m256i msk = _mm256_cmpgt_epi32(kvB, _mm256_load_si256((const __m256i *)(IK + jj)));
        if (nd > 0) msk = _mm256_and_si256(msk, _mm256_cmpgt_epi32(v0B, _mm256_load_si256((const __m256i *)(IC0 + jj))));
        if (nd > 1) msk = _mm256_and_si256(msk, _mm256_cmpgt_epi32(v1B, _mm256_load_si256((const __m256i *)(IC1 + jj))));
        if (nd > 2) msk = _mm256_and_si256(msk, _mm256_cmpgt_epi32(v2B, _mm256_load_si256((const __m256i *)(IC2 + jj))));
        if (nd > 3) msk = _mm256_and_si256(msk, _mm256_cmpgt_epi32(v3B, _mm256_load_si256((const __m256i *)(IC3 + jj))));
        acc = _mm256_sub_epi32(acc, msk);
      }
      if (jj < nB) {
        int n = nB;
        __m256i msk = _mm256_cmpgt_epi32(kvB, _mm256_load_si256((const __m256i *)(IK + jj)));
        if (nd > 0) msk = _mm256_and_si256(msk, _mm256_cmpgt_epi32(v0B, _mm256_load_si256((const __m256i *)(IC0 + jj))));
        if (nd > 1) msk = _mm256_and_si256(msk, _mm256_cmpgt_epi32(v1B, _mm256_load_si256((const __m256i *)(IC1 + jj))));
        if (nd > 2) msk = _mm256_and_si256(msk, _mm256_cmpgt_epi32(v2B, _mm256_load_si256((const __m256i *)(IC2 + jj))));
        if (nd > 3) msk = _mm256_and_si256(msk, _mm256_cmpgt_epi32(v3B, _mm256_load_si256((const __m256i *)(IC3 + jj))));
        msk = _mm256_and_si256(msk, _mm256_loadu_si256((const __m256i *)VMASK[n - jj]));
        acc = _mm256_sub_epi32(acc, msk);
      }
      OUT[QID[q + 1]] += hsum256(acc);
    }
    {
      int jj = j;
      __m256i acc = accC;
      for (; jj + 8 <= nC; jj += 8) {
        __m256i msk = _mm256_cmpgt_epi32(kvC, _mm256_load_si256((const __m256i *)(IK + jj)));
        if (nd > 0) msk = _mm256_and_si256(msk, _mm256_cmpgt_epi32(v0C, _mm256_load_si256((const __m256i *)(IC0 + jj))));
        if (nd > 1) msk = _mm256_and_si256(msk, _mm256_cmpgt_epi32(v1C, _mm256_load_si256((const __m256i *)(IC1 + jj))));
        if (nd > 2) msk = _mm256_and_si256(msk, _mm256_cmpgt_epi32(v2C, _mm256_load_si256((const __m256i *)(IC2 + jj))));
        if (nd > 3) msk = _mm256_and_si256(msk, _mm256_cmpgt_epi32(v3C, _mm256_load_si256((const __m256i *)(IC3 + jj))));
        acc = _mm256_sub_epi32(acc, msk);
      }
      if (jj < nC) {
        int n = nC;
        __m256i msk = _mm256_cmpgt_epi32(kvC, _mm256_load_si256((const __m256i *)(IK + jj)));
        if (nd > 0) msk = _mm256_and_si256(msk, _mm256_cmpgt_epi32(v0C, _mm256_load_si256((const __m256i *)(IC0 + jj))));
        if (nd > 1) msk = _mm256_and_si256(msk, _mm256_cmpgt_epi32(v1C, _mm256_load_si256((const __m256i *)(IC1 + jj))));
        if (nd > 2) msk = _mm256_and_si256(msk, _mm256_cmpgt_epi32(v2C, _mm256_load_si256((const __m256i *)(IC2 + jj))));
        if (nd > 3) msk = _mm256_and_si256(msk, _mm256_cmpgt_epi32(v3C, _mm256_load_si256((const __m256i *)(IC3 + jj))));
        msk = _mm256_and_si256(msk, _mm256_loadu_si256((const __m256i *)VMASK[n - jj]));
        acc = _mm256_sub_epi32(acc, msk);
      }
      OUT[QID[q + 2]] += hsum256(acc);
    }
    {
      int jj = j;
      __m256i acc = accD;
      for (; jj + 8 <= nD; jj += 8) {
        __m256i msk = _mm256_cmpgt_epi32(kvD, _mm256_load_si256((const __m256i *)(IK + jj)));
        if (nd > 0) msk = _mm256_and_si256(msk, _mm256_cmpgt_epi32(v0D, _mm256_load_si256((const __m256i *)(IC0 + jj))));
        if (nd > 1) msk = _mm256_and_si256(msk, _mm256_cmpgt_epi32(v1D, _mm256_load_si256((const __m256i *)(IC1 + jj))));
        if (nd > 2) msk = _mm256_and_si256(msk, _mm256_cmpgt_epi32(v2D, _mm256_load_si256((const __m256i *)(IC2 + jj))));
        if (nd > 3) msk = _mm256_and_si256(msk, _mm256_cmpgt_epi32(v3D, _mm256_load_si256((const __m256i *)(IC3 + jj))));
        acc = _mm256_sub_epi32(acc, msk);
      }
      if (jj < nD) {
        int n = nD;
        __m256i msk = _mm256_cmpgt_epi32(kvD, _mm256_load_si256((const __m256i *)(IK + jj)));
        if (nd > 0) msk = _mm256_and_si256(msk, _mm256_cmpgt_epi32(v0D, _mm256_load_si256((const __m256i *)(IC0 + jj))));
        if (nd > 1) msk = _mm256_and_si256(msk, _mm256_cmpgt_epi32(v1D, _mm256_load_si256((const __m256i *)(IC1 + jj))));
        if (nd > 2) msk = _mm256_and_si256(msk, _mm256_cmpgt_epi32(v2D, _mm256_load_si256((const __m256i *)(IC2 + jj))));
        if (nd > 3) msk = _mm256_and_si256(msk, _mm256_cmpgt_epi32(v3D, _mm256_load_si256((const __m256i *)(IC3 + jj))));
        msk = _mm256_and_si256(msk, _mm256_loadu_si256((const __m256i *)VMASK[n - jj]));
        acc = _mm256_sub_epi32(acc, msk);
      }
      OUT[QID[q + 3]] += hsum256(acc);
    }
  }
  for (; q + 2 <= nq; q += 2) {
    const int n = ((int)QN[q] < (int)QN[q + 1]) ? (int)QN[q] : (int)QN[q + 1];
    const int nA = (int)QN[q], nB = (int)QN[q + 1];
    __m256i kvA = _mm256_set1_epi32((int)QK[q]),     kvB = _mm256_set1_epi32((int)QK[q + 1]);
    __m256i v0A = _mm256_set1_epi32((int)QC0[q]),    v0B = _mm256_set1_epi32((int)QC0[q + 1]);
    __m256i v1A = _mm256_set1_epi32((int)QC1[q]),    v1B = _mm256_set1_epi32((int)QC1[q + 1]);
    __m256i v2A = _mm256_set1_epi32((int)QC2[q]),    v2B = _mm256_set1_epi32((int)QC2[q + 1]);
    __m256i v3A = _mm256_set1_epi32((int)QC3[q]),    v3B = _mm256_set1_epi32((int)QC3[q + 1]);
    __m256i accA = _mm256_setzero_si256(), accB = _mm256_setzero_si256();
    int j = 0;
    if (nd >= 3) {
      for (; j + 8 <= n; j += 8) {
        __m256i IKv = _mm256_load_si256((const __m256i *)(IK + j));
        __m256i C0v = _mm256_load_si256((const __m256i *)(IC0 + j));
        __m256i C1v = _mm256_load_si256((const __m256i *)(IC1 + j));
        __m256i C2v = _mm256_load_si256((const __m256i *)(IC2 + j));
        __m256i mA = _mm256_and_si256(_mm256_cmpgt_epi32(kvA, IKv), _mm256_cmpgt_epi32(v0A, C0v));
        __m256i mB = _mm256_and_si256(_mm256_cmpgt_epi32(kvB, IKv), _mm256_cmpgt_epi32(v0B, C0v));
        mA = _mm256_and_si256(mA, _mm256_cmpgt_epi32(v1A, C1v));
        mB = _mm256_and_si256(mB, _mm256_cmpgt_epi32(v1B, C1v));
        mA = _mm256_and_si256(mA, _mm256_cmpgt_epi32(v2A, C2v));
        mB = _mm256_and_si256(mB, _mm256_cmpgt_epi32(v2B, C2v));
        if (nd > 3) {   /* m161i key 3 */
          __m256i C3v = _mm256_load_si256((const __m256i *)(IC3 + j));
          mA = _mm256_and_si256(mA, _mm256_cmpgt_epi32(v3A, C3v));
          mB = _mm256_and_si256(mB, _mm256_cmpgt_epi32(v3B, C3v));
        }
        accA = _mm256_sub_epi32(accA, mA);
        accB = _mm256_sub_epi32(accB, mB);
      }
    } else {
      for (; j + 8 <= n; j += 8) {
        __m256i IKv = _mm256_load_si256((const __m256i *)(IK + j));
        __m256i mA = _mm256_cmpgt_epi32(kvA, IKv);
        __m256i mB = _mm256_cmpgt_epi32(kvB, IKv);
        if (nd > 0) {
          __m256i C0v = _mm256_load_si256((const __m256i *)(IC0 + j));
          mA = _mm256_and_si256(mA, _mm256_cmpgt_epi32(v0A, C0v));
          mB = _mm256_and_si256(mB, _mm256_cmpgt_epi32(v0B, C0v));
        }
        if (nd > 1) {
          __m256i C1v = _mm256_load_si256((const __m256i *)(IC1 + j));
          mA = _mm256_and_si256(mA, _mm256_cmpgt_epi32(v1A, C1v));
          mB = _mm256_and_si256(mB, _mm256_cmpgt_epi32(v1B, C1v));
        }
        accA = _mm256_sub_epi32(accA, mA);
        accB = _mm256_sub_epi32(accB, mB);
      }
    }
    // per-query tails: each member finishes [j, QN[.]) on its own
    {
      int jj = j;
      __m256i acc = accA;
      for (; jj + 8 <= nA; jj += 8) {
        __m256i msk = _mm256_cmpgt_epi32(kvA, _mm256_load_si256((const __m256i *)(IK + jj)));
        if (nd > 0) msk = _mm256_and_si256(msk, _mm256_cmpgt_epi32(v0A, _mm256_load_si256((const __m256i *)(IC0 + jj))));
        if (nd > 1) msk = _mm256_and_si256(msk, _mm256_cmpgt_epi32(v1A, _mm256_load_si256((const __m256i *)(IC1 + jj))));
        if (nd > 2) msk = _mm256_and_si256(msk, _mm256_cmpgt_epi32(v2A, _mm256_load_si256((const __m256i *)(IC2 + jj))));
        if (nd > 3) msk = _mm256_and_si256(msk, _mm256_cmpgt_epi32(v3A, _mm256_load_si256((const __m256i *)(IC3 + jj))));
        acc = _mm256_sub_epi32(acc, msk);
      }
      if (jj < nA) {
        int n = nA;
        __m256i msk = _mm256_cmpgt_epi32(kvA, _mm256_load_si256((const __m256i *)(IK + jj)));
        if (nd > 0) msk = _mm256_and_si256(msk, _mm256_cmpgt_epi32(v0A, _mm256_load_si256((const __m256i *)(IC0 + jj))));
        if (nd > 1) msk = _mm256_and_si256(msk, _mm256_cmpgt_epi32(v1A, _mm256_load_si256((const __m256i *)(IC1 + jj))));
        if (nd > 2) msk = _mm256_and_si256(msk, _mm256_cmpgt_epi32(v2A, _mm256_load_si256((const __m256i *)(IC2 + jj))));
        if (nd > 3) msk = _mm256_and_si256(msk, _mm256_cmpgt_epi32(v3A, _mm256_load_si256((const __m256i *)(IC3 + jj))));
        msk = _mm256_and_si256(msk, _mm256_loadu_si256((const __m256i *)VMASK[n - jj]));
        acc = _mm256_sub_epi32(acc, msk);
      }
      OUT[QID[q]] += hsum256(acc);
    }
    {
      int jj = j;
      __m256i acc = accB;
      for (; jj + 8 <= nB; jj += 8) {
        __m256i msk = _mm256_cmpgt_epi32(kvB, _mm256_load_si256((const __m256i *)(IK + jj)));
        if (nd > 0) msk = _mm256_and_si256(msk, _mm256_cmpgt_epi32(v0B, _mm256_load_si256((const __m256i *)(IC0 + jj))));
        if (nd > 1) msk = _mm256_and_si256(msk, _mm256_cmpgt_epi32(v1B, _mm256_load_si256((const __m256i *)(IC1 + jj))));
        if (nd > 2) msk = _mm256_and_si256(msk, _mm256_cmpgt_epi32(v2B, _mm256_load_si256((const __m256i *)(IC2 + jj))));
        if (nd > 3) msk = _mm256_and_si256(msk, _mm256_cmpgt_epi32(v3B, _mm256_load_si256((const __m256i *)(IC3 + jj))));
        acc = _mm256_sub_epi32(acc, msk);
      }
      if (jj < nB) {
        int n = nB;
        __m256i msk = _mm256_cmpgt_epi32(kvB, _mm256_load_si256((const __m256i *)(IK + jj)));
        if (nd > 0) msk = _mm256_and_si256(msk, _mm256_cmpgt_epi32(v0B, _mm256_load_si256((const __m256i *)(IC0 + jj))));
        if (nd > 1) msk = _mm256_and_si256(msk, _mm256_cmpgt_epi32(v1B, _mm256_load_si256((const __m256i *)(IC1 + jj))));
        if (nd > 2) msk = _mm256_and_si256(msk, _mm256_cmpgt_epi32(v2B, _mm256_load_si256((const __m256i *)(IC2 + jj))));
        if (nd > 3) msk = _mm256_and_si256(msk, _mm256_cmpgt_epi32(v3B, _mm256_load_si256((const __m256i *)(IC3 + jj))));
        msk = _mm256_and_si256(msk, _mm256_loadu_si256((const __m256i *)VMASK[n - jj]));
        acc = _mm256_sub_epi32(acc, msk);
      }
      OUT[QID[q + 1]] += hsum256(acc);
    }
  }
#endif
  for (; q < nq; q++) {
    const int n = (int)QN[q];
    __m256i kv = _mm256_set1_epi32((int)QK[q]);
    __m256i v0 = _mm256_set1_epi32((int)QC0[q]);
    __m256i v1 = _mm256_set1_epi32((int)QC1[q]);
    __m256i v2 = _mm256_set1_epi32((int)QC2[q]);
    __m256i v3 = _mm256_set1_epi32((int)QC3[q]);
    __m256i acc = _mm256_setzero_si256();
    int j = 0;
    switch (nd) {
      case 0:
        for (; j + 8 <= n; j += 8)
          acc = _mm256_sub_epi32(acc, _mm256_cmpgt_epi32(kv, _mm256_load_si256((const __m256i *)(IK + j))));
        break;
      case 1:
        for (; j + 8 <= n; j += 8) {
      __m256i msk = _mm256_cmpgt_epi32(kv, _mm256_load_si256((const __m256i *)(IK + j)));
      msk = _mm256_and_si256(msk, _mm256_cmpgt_epi32(v0, _mm256_load_si256((const __m256i *)(IC0 + j))));
      acc = _mm256_sub_epi32(acc, msk);
        }
        break;
      case 2:
        for (; j + 8 <= n; j += 8) {
      __m256i msk = _mm256_cmpgt_epi32(kv, _mm256_load_si256((const __m256i *)(IK + j)));
      msk = _mm256_and_si256(msk, _mm256_cmpgt_epi32(v0, _mm256_load_si256((const __m256i *)(IC0 + j))));
      msk = _mm256_and_si256(msk, _mm256_cmpgt_epi32(v1, _mm256_load_si256((const __m256i *)(IC1 + j))));
      acc = _mm256_sub_epi32(acc, msk);
        }
        break;
      default:
        for (; j + 8 <= n; j += 8) {
      __m256i msk = _mm256_cmpgt_epi32(kv, _mm256_load_si256((const __m256i *)(IK + j)));
      msk = _mm256_and_si256(msk, _mm256_cmpgt_epi32(v0, _mm256_load_si256((const __m256i *)(IC0 + j))));
      msk = _mm256_and_si256(msk, _mm256_cmpgt_epi32(v1, _mm256_load_si256((const __m256i *)(IC1 + j))));
      msk = _mm256_and_si256(msk, _mm256_cmpgt_epi32(v2, _mm256_load_si256((const __m256i *)(IC2 + j))));
      if (nd > 3) msk = _mm256_and_si256(msk, _mm256_cmpgt_epi32(v3, _mm256_load_si256((const __m256i *)(IC3 + j))));
      acc = _mm256_sub_epi32(acc, msk);
        }
        break;
    }
    if (j < n) {                                  // partial block, masked by a table lookup
      __m256i msk = _mm256_cmpgt_epi32(kv, _mm256_load_si256((const __m256i *)(IK + j)));
      if (nd > 0) msk = _mm256_and_si256(msk, _mm256_cmpgt_epi32(v0, _mm256_load_si256((const __m256i *)(IC0 + j))));
      if (nd > 1) msk = _mm256_and_si256(msk, _mm256_cmpgt_epi32(v1, _mm256_load_si256((const __m256i *)(IC1 + j))));
      if (nd > 2) msk = _mm256_and_si256(msk, _mm256_cmpgt_epi32(v2, _mm256_load_si256((const __m256i *)(IC2 + j))));
      if (nd > 3) msk = _mm256_and_si256(msk, _mm256_cmpgt_epi32(v3, _mm256_load_si256((const __m256i *)(IC3 + j))));
      msk = _mm256_and_si256(msk, _mm256_loadu_si256((const __m256i *)VMASK[n - j]));
      acc = _mm256_sub_epi32(acc, msk);
    }
    u32 c = hsum256(acc);
    OUT[QID[q]] += c;
  }
  if (!needOut) return;
#if LEAFNB
  {
    const int NB = (LFAUTO && m <= LEAFNB) ? (m < 8 ? 8 : m) : LEAFNB;
    const int LB = (G_LIM < NB) ? G_LIM : NB;   // used-bucket bound
    u32 cnt[LEAFNB + 2];
    for (int b = 0; b < LB; b++) cnt[b] = 0;
    for (int t = 0; t < m; t++) cnt[(int)(E_KA(seq[t]) >> (LFSH + LFD)) + 1]++;
    for (int b = 0; b + 1 < LB; b++) cnt[b + 1] += cnt[b];
    for (int t = 0; t < m; t++) { u64 v = seq[t]; out[cnt[(int)(E_KA(v) >> (LFSH + LFD))]++] = v; }
    int lo = 0;
    for (int b = 0; b + 1 < LB; b++) {
      int hi = (int)cnt[b];
      if (hi - lo > 1)
        for (int t = lo + 1; t < hi; t++) {
          u64 v = out[t];
          int q = t - 1;
          while (q >= lo && v < out[q]) { out[q + 1] = out[q]; q--; }
          out[q + 1] = v;
        }
      lo = hi;
    }
  }
#else
  for (int t = 1; t < m; t++) {
    u64 v = seq[t];
    int q = t - 1;
    while (q >= 0 && v < seq[q]) { seq[q + 1] = seq[q]; q--; }
    seq[q + 1] = v;
  }
  memcpy(out, seq, sizeof(u64) * (size_t)m);
#endif
}

static void base_case(u64 *seq, int m, int d, u64 *out, int needOut) {
  switch (K - (d + 2)) {
    case 0: base_case_t<0>(seq, m, d, out, needOut); break;
    case 1: base_case_t<1>(seq, m, d, out, needOut); break;
    case 2: base_case_t<2>(seq, m, d, out, needOut); break;
    case 3: base_case_t<3>(seq, m, d, out, needOut); break;
    case 4: base_case_t<4>(seq, m, d, out, needOut); break;
    default: base_case_t<4>(seq, m, d, out, needOut); break;
  }
}



// ---- ND specialisation dispatcher (the fleet's `DN` recipe) ----
static void base_case(u64 *seq, int m, int d, u64 *out, int needOut);

// ================= depth-templated solve: D and KT are compile-time =================
template <bool B> struct TB {};
template <int V> struct NCL { static const int v = (V < 0) ? 0 : ((V > 4) ? 4 : V); };

template <int KT, int D> static inline void solve_t(u64 *seq, int m, u64 *out, int needOut);

// ---- terminal level (D == KT-1): every insert-before-query pair qualifies ----
template <int KT, int D>
static void solve_b(u64 *seq, int m, u64 *out, int needOut, TB<true>) {
  int cnt = 0;
  for (int t = 0; t < m; t++) {
    u64 v = seq[t];
    if (E_TY(v)) cnt++;                             // insert
    else OUT[E_ID(v)] += (u32)cnt;                  // query
  }
}

// ---- general level (D < KT-1) ----
template <int KT, int D>
static void solve_b(u64 *seq, int m, u64 *out, int needOut, TB<false>) {
  if (m <= KG_BASE) {
    base_case_t<NCL< KT - (D + 2) >::v>(seq, m, D, out, needOut);
    return;
  }
  int h = m >> 1, r = m - h;
  u64 *save = ABtop;
  u64 *retA = ABtop; ABtop += h;
  u64 *retB = ABtop; ABtop += r;
  solve_t<KT, D>(seq, h, retA, 1);
  solve_t<KT, D>(seq + h, r, retB, 1);
  u64 *cross = ABtop; ABtop += m + 1;
  // v2: PROMOTE moves the carried KB up into the KA slot (a new level's key);
  // GATHER fetches the next KB (dim D+3) -- the ONLY remaining per-level gather,
  // and it exists only at D = 0 for KT = 4.
  const int PROM = (D + 2 <= KT - 1);
  const int GAT = (D + 3 <= KT - 1);
  const u32 *XN = XD[(D + 3 <= KG_MAXK - 1) ? (D + 3) : 0];
  int ia = 0, ib = 0, cx = 0, t = 0;
  while (ia < h && ib < r) {
#if KG_PF
    if (ia + KG_PF < h) __builtin_prefetch(&XN[E_ID(retA[ia + KG_PF])], 0, 1);   /* c18: DENSE, distance 64 */
    if (ib + KG_PF < r) __builtin_prefetch(&XN[E_ID(retB[ib + KG_PF])], 0, 1);
#endif
    u64 ea = retA[ia], eb = retB[ib];
    int takeA = (ea < eb);
    ia += takeA; ib += 1 - takeA;
    u64 v = takeA ? ea : eb;
    if (needOut) out[t] = v;
    t++;
    int role = (int)E_TY(v);
#if KG_COND
    if (keep_test(takeA, role)) { cross[cx] = v; cx++; }
#else
    cross[cx] = v;
    cx += keep_test(takeA, role);
#endif
  }
  while (ia < h) {
    u64 v = retA[ia++];
    if (needOut) out[t] = v;
    t++;
    if (E_TY(v)) cross[cx++] = v;
  }
  while (ib < r) {
    u64 v = retB[ib++];
    if (needOut) out[t] = v;
    t++;
    if (!E_TY(v)) cross[cx++] = v;
  }
  if (GAT) {                       // ----- C1: cross re-key, second pass -----
    for (int j = 0; j < cx; j++) {
      u64 v = cross[j];
      cross[j] = ((v << 21) & (0xFFFFFull << 43)) | (v & (1ull << 42)) | (v & 0xFFFFFull) |
                 ((u64)XN[E_ID(v)] << 22);
    }
  } else if (PROM) {
    for (int j = 0; j < cx; j++) {
      u64 v = cross[j];
      cross[j] = ((v << 21) & (0xFFFFFull << 43)) | (v & (1ull << 42)) | (v & 0xFFFFFull);
    }
  }
  if (FUSE_OK && D == KT - 3) {
    u32 *dirt = (u32 *)ABtop; ABtop += (cx + 3) / 2 + 1;
    ctr_solve(cross, cx, dirt);
  } else {
    u64 *cres = ABtop; ABtop += cx;
    solve_t<KT, D + 1>(cross, cx, cres, 0);
  }
  ABtop = save;
}

template <int KT, int D>
static inline void solve_t(u64 *seq, int m, u64 *out, int needOut) {
  if (m <= 1) return;
  solve_b<KT, D>(seq, m, out, needOut, TB<(D >= KT - 1)>());
}
// ====================================================================================


static u32 *g_ord, *g_x;

static void sort0(int n) {
  const u32 *X0 = XD[0];
  u32 *cnt = g_x;
  memset(cnt, 0, sizeof(u32) * (size_t)(n + 1));
  for (int i = 0; i < n; i++) cnt[X0[i] + 1]++;
  for (int i = 0; i < n; i++) cnt[i + 1] += cnt[i];
  for (int i = 0; i < n; i++) g_ord[cnt[X0[i]]++] = (u32)i;
}

template <int KT>
static void run_t(int n, const u32 **x, u32 *out) {
  LFSH = 0; LFD = 0; CTN = (u32)n;
  for (int d = 0; d < KT; d++) XD[d] = x[d];
  OUT = out;
  ABtop = AB;
  if (n <= 1) return;
  FUSE_OK = 0;
  if (KT >= 4 && n > 2 && (u32)n < CT_MAXV) {
    const u32 *XL = XD[KT - 1];
    memset(CT0, 0, (size_t)n);
    int ok = 1;
    for (int i = 0; i < n; i++) if (++CT0[XL[i]] > 127) { ok = 0; break; }
    memset(CT0, 0, (size_t)n);
    FUSE_OK = ok;
  }
  {
    int bit = 0;
    while ((n >> bit) >= LEAFNB && bit < 30) bit++;
    LFSH = bit; LFD = 0;
  }
  G_LIM = (int)(((u32)(n - 1)) >> (LFSH + LFD)) + 2;
  sort0(n);
  const u32 *X0 = XD[0], *X1 = XD[1];
  u64 *ev = AB;                     // top-level event array: 2n entries
  int m = 0, g = 0;
  while (g < n) {
    int g1 = g;
    u32 v = X0[g_ord[g]];
    while (g1 < n && X0[g_ord[g1]] == v) g1++;
    const u32 *X2 = XD[2];
    for (int t = g; t < g1; t++) { int q = (int)g_ord[t]; ev[m++] = mk2(X1[q], 0, X2[q], (u32)q); }
    for (int t = g; t < g1; t++) { int q = (int)g_ord[t]; ev[m++] = mk2(X1[q], 1, X2[q], (u32)q); }
    g = g1;
  }
  ABtop = AB + m;
  u64 *res = ABtop; ABtop += m;
  solve_t<KT, 0>(ev, m, res, 0);
}

}  // namespace kg

static u64 g_arena[KG_ARENA];
static u32 g_ord_st[1048576], g_x_st[1048576];
static int g_ready = 0;

static void setup() {
  if (g_ready) return;
  g_ready = 1;
  kg::AB = g_arena;
  kg::g_ord = g_ord_st;
  kg::g_x = g_x_st;
}

void count_4d(int n, const unsigned *x[4], unsigned *out) {
  setup();
  memset(out, 0, sizeof(unsigned) * (size_t)n);
  kg::run_t<4>(n, (const unsigned **)x, out);
}
void count_5d(int n, const unsigned *x[5], unsigned *out) {
  setup();
  memset(out, 0, sizeof(unsigned) * (size_t)n);
  kg::run_t<5>(n, (const unsigned **)x, out);
}
void count_6d(int n, const unsigned *x[6], unsigned *out) {
  setup();
  memset(out, 0, sizeof(unsigned) * (size_t)n);
  kg::run_t<6>(n, (const unsigned **)x, out);
}
#pragma GCC pop_options

CompilationN/AN/ACompile OKScore: N/A

Testcase #13.653 s98 MB + 608 KBAcceptedScore: 100


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