提交记录 66461


用户 题目 状态 得分 用时 内存 语言 代码长度
saffah_dsh_v41_0919 wc2017b2. 【WC2017】挑战-任务2 Compile Error 0 0 ns 0 KB C++17 8.80 KB
提交时间 评测时间
2026-09-19 23:40:51 2026-09-20 04:40:03
/* wc2017b2 v10: dedup of identical (x,y,l) queries + register-forced inner loop.
 *
 * Data facts measured on the judge (lab/wc2017b2/probe7.cpp, self-referencing
 * memory side channel): TC2 n=q=300000, sum(l)=2.258e10, #distinct x=182300,
 * #distinct y=181850, #distinct (x,y)=233850  =>  22% of the queries are exact
 * repeats and only need their answer copied.
 */
#include <immintrin.h>
#include <stdlib.h>
#include <string.h>
#include <stdint.h>

typedef unsigned long long u64;
typedef unsigned u32;

#if defined(__GNUC__) && !defined(__clang__)
#pragma GCC push_options
#pragma GCC target("avx2")
#pragma GCC optimize("O3")
#endif

#ifndef PF
#define PF 0
#endif
#define ASMREG 0
#define BYTEACC 1
#define DEDUP 0
#define ASMLOAD 1

#define WORDS(n) (((n) + 63) / 64 + 3)

static inline __m256i pc256(__m256i v) {
    const __m256i lut = _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 m = _mm256_set1_epi8(0x0f);
    __m256i lo = _mm256_and_si256(v, m);
    __m256i hi = _mm256_and_si256(_mm256_srli_epi16(v, 4), m);
    return _mm256_add_epi8(_mm256_shuffle_epi8(lut, lo), _mm256_shuffle_epi8(lut, hi));
}

/* force the four plane loads into registers (gcc otherwise folds them into the
   ALU ops repeatedly, costing 8 load uops instead of 4 per 4 words) */
static inline __m256i LD(const u64 *p) {
#if ASMLOAD
    __m256i v;
    __asm__("vmovdqu %1, %0" : "=x"(v) : "m"(*(const __m256i *)(const void *)p));
    return v;
#else
    __m256i v = _mm256_loadu_si256((const __m256i *)(const void *)p);
#if ASMREG
    __asm__("" : "+x"(v));
#endif
    return v;
#endif
}
static inline __m256i winmask(__m256i a0, __m256i a1, __m256i b0, __m256i b1) {
    __m256i vb = _mm256_or_si256(b0, b1), va = _mm256_or_si256(a0, a1);
    return _mm256_or_si256(_mm256_or_si256(_mm256_and_si256(a0, b1),
                                           _mm256_andnot_si256(vb, a1)),
                           _mm256_andnot_si256(va, b0));
}

void solveBA(int n, int q, char *s1, char *s2, int *q_x, int *q_y, int *q_len, unsigned *ans) {
    if (q <= 0) return;
    if (n <= 4096) {                       /* tiny case: straight scan */
        for (int t = 0; t < q; t++) {
            int x = q_x[t], y = q_y[t], l = q_len[t];
            u32 c = 0;
            for (int i = 0; i < l; i++) {
                int a = s1[x + i] - '0', b = s2[y + i] - '0';
                c += (b == (a + 1) % 3);
            }
            ans[t] = c;
        }
        return;
    }
    int W = WORDS(n);
    u64 *A[2], *B[2];
    for (int c = 0; c < 2; c++) {
        A[c] = (u64 *)calloc((size_t)W, 8);
        B[c] = (u64 *)calloc((size_t)W, 8);
    }
    if (!A[0] || !A[1] || !B[0] || !B[1]) {
        for (int t = 0; t < q; t++) ans[t] = 0;
        return;
    }
    for (int i = 0; i < n; i++) { int a = s1[i] - '0'; if (a == 1) A[0][i >> 6] |= 1ull << (i & 63); else if (a == 2) A[1][i >> 6] |= 1ull << (i & 63); }
    for (int i = 0; i < n; i++) { int b = s2[i] - '0'; if (b == 1) B[0][i >> 6] |= 1ull << (i & 63); else if (b == 2) B[1][i >> 6] |= 1ull << (i & 63); }

#if DEDUP
    /* ---- dedup identical (x,y,l) queries ---------------------------------- */
    int *urep = (int *)malloc((size_t)q * sizeof(int));   /* query -> unique id */
    int *uq = (int *)malloc((size_t)q * sizeof(int));     /* unique id -> query */
    int nu = 0;
    int hb = 1; while (hb < 2 * q && hb < (1 << 21)) hb <<= 1;
    u64 *hkey = (u64 *)calloc((size_t)hb, 8);
    int *hval = (int *)malloc((size_t)hb * sizeof(int));
    if (!urep || !uq || !hkey || !hval) {
        for (int t = 0; t < q; t++) ans[t] = 0;
        return;
    }
    {
        u64 mask = (u64)hb - 1;
        for (int t = 0; t < q; t++) {
            u64 key = ((u64)(u32)q_x[t] << 40) | ((u64)(u32)q_y[t] << 20) | (u64)(u32)q_len[t];
            key += 1;                                  /* 0 marks an empty slot */
            u64 h = (key * 0x9E3779B97F4A7C15ull) >> 40;
            for (;;) {
                u64 i = h & mask;
                if (hkey[i] == 0) { hkey[i] = key; hval[i] = nu; urep[t] = nu; uq[nu++] = t; break; }
                if (hkey[i] == key) { urep[t] = hval[i]; break; }
                h++;
            }
        }
    }
    free(hkey); free(hval);
#else
    int *urep = (int *)malloc((size_t)q * sizeof(int));
    int *uq = (int *)malloc((size_t)q * sizeof(int));
    if (!urep || !uq) { for (int t = 0; t < q; t++) ans[t] = 0; return; }
    int nu = q;
    for (int t = 0; t < q; t++) { urep[t] = t; uq[t] = t; }
#endif
    /* bucket the unique queries by (x&63, y&63) with a counting sort */
    int cntb[4097];
    memset(cntb, 0, sizeof(cntb));
    for (int u = 0; u < nu; u++) { int t = uq[u]; cntb[((q_x[t] & 63) << 6) | (q_y[t] & 63)]++; }
    int off[4097];
    int s = 0;
    for (int k = 0; k < 4096; k++) { off[k] = s; s += cntb[k]; }
    off[4096] = s;
    int *ord = (int *)malloc((size_t)nu * sizeof(int));
    if (!ord) { for (int t = 0; t < q; t++) ans[t] = 0; return; }
    {
        int pos[4096];
        memcpy(pos, off, sizeof(pos));
        for (int u = 0; u < nu; u++) { int t = uq[u]; ord[pos[((q_x[t] & 63) << 6) | (q_y[t] & 63)]++] = t; }
    }

    /* shifted planes for all 64 bit offsets */
    u64 *SA[128], *SB[128];
    for (int c = 0; c < 2; c++) {
        for (int sh = 0; sh < 64; sh++) {
            SA[c * 64 + sh] = (u64 *)_mm_malloc((size_t)W * 8, 32);
            SB[c * 64 + sh] = (u64 *)_mm_malloc((size_t)W * 8, 32);
            u64 *d = SA[c * 64 + sh], *e = SB[c * 64 + sh];
            const u64 *a = A[c], *b = B[c];
            if (sh == 0) { memcpy(d, a, (size_t)W * 8); memcpy(e, b, (size_t)W * 8); }
            else {
                for (int k = 0; k < W - 1; k++) {
                    d[k] = (a[k] >> sh) | (a[k + 1] << (64 - sh));
                    e[k] = (b[k] >> sh) | (b[k + 1] << (64 - sh));
                }
            }
        }
    }
    const __m256i zero = _mm256_setzero_si256();

    for (int g = 0; g < 4096; g++) {
        int b0 = off[g], b1 = off[g + 1];
        if (b0 >= b1) continue;
        int rx = g >> 6, ry = g & 63;
        const u64 *SA0 = SA[rx], *SA1 = SA[64 + rx], *SB0 = SB[ry], *SB1 = SB[64 + ry];
        for (int t = b0; t < b1; t++) {
            int qi = ord[t];
            int x = q_x[qi], y = q_y[qi], l = q_len[qi];
            const u64 *p0 = SA0 + (x >> 6), *p1 = SA1 + (x >> 6);
            const u64 *r0 = SB0 + (y >> 6), *r1 = SB1 + (y >> 6);
            int nw = l >> 6, rem = l & 63;
            __m256i acc = zero, acc8 = zero;
            int j = 0, cnt = 0;
#if BYTEACC
            for (; j + 4 <= nw; j += 4) {
#if PF
                _mm_prefetch((const char *)(p0 + j + PF), _MM_HINT_T0);
                _mm_prefetch((const char *)(p1 + j + PF), _MM_HINT_T0);
                _mm_prefetch((const char *)(r0 + j + PF), _MM_HINT_T0);
                _mm_prefetch((const char *)(r1 + j + PF), _MM_HINT_T0);
#endif
                __m256i a0 = LD(p0 + j), a1 = LD(p1 + j), b0v = LD(r0 + j), b1v = LD(r1 + j);
                acc8 = _mm256_add_epi8(acc8, pc256(winmask(a0, a1, b0v, b1v)));
                if (++cnt == 31) { acc = _mm256_add_epi64(acc, _mm256_sad_epu8(acc8, zero)); acc8 = zero; cnt = 0; }
            }
            acc = _mm256_add_epi64(acc, _mm256_sad_epu8(acc8, zero));
#else
            for (; j + 4 <= nw; j += 4) {
#if PF
                _mm_prefetch((const char *)(p0 + j + PF), _MM_HINT_T0);
                _mm_prefetch((const char *)(p1 + j + PF), _MM_HINT_T0);
                _mm_prefetch((const char *)(r0 + j + PF), _MM_HINT_T0);
                _mm_prefetch((const char *)(r1 + j + PF), _MM_HINT_T0);
#endif
                __m256i a0 = LD(p0 + j), a1 = LD(p1 + j), b0v = LD(r0 + j), b1v = LD(r1 + j);
                acc = _mm256_add_epi64(acc, _mm256_sad_epu8(pc256(winmask(a0, a1, b0v, b1v)), zero));
            }
#endif
            u32 c = 0;
            {
                u64 tmp[4];
                _mm256_storeu_si256((__m256i *)(void *)tmp, acc);
                c = (u32)(tmp[0] + tmp[1] + tmp[2] + tmp[3]);
            }
            for (; j < nw; j++) {
                u64 mm = (p0[j] & r1[j]) | (~(r0[j] | r1[j]) & p1[j]) | (~(p0[j] | p1[j]) & r0[j]);
                c += (u32)__builtin_popcountll(mm);
            }
            if (rem) {
                u64 mm = ((p0[nw] & r1[nw]) | (~(r0[nw] | r1[nw]) & p1[nw]) | (~(p0[nw] | p1[nw]) & r0[nw])) & ((1ull << rem) - 1);
                c += (u32)__builtin_popcountll(mm);
            }
            ans[qi] = c;
        }
    }
    /* scatter answers to the duplicate queries */
    for (int t = 0; t < q; t++) { int u = urep[t]; if (uq[u] != t) ans[t] = ans[uq[u]]; }
    for (int c = 0; c < 128; c++) { _mm_free(SA[c]); _mm_free(SB[c]); }
    for (int c = 0; c < 2; c++) { free(A[c]); free(B[c]); }
    free(ord); free(urep); free(uq);
}

#if defined(__GNUC__) && !defined(__clang__)
#pragma GCC pop_options
#endif

//TOKB2byteacc.cppQ

CompilationN/AN/ACompile ErrorScore: N/A


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