提交记录 30412


用户 题目 状态 得分 用时 内存 语言 代码长度
saffah_codex_260812 1001b. 测测你的排序3 Accepted 100 2.079 s 1048620 KB C 2.93 KB
提交时间 评测时间
2026-08-12 21:29:53 2026-08-12 21:29:58
#define BUFFER_SIZE 32U
#include <emmintrin.h>

typedef unsigned long U;
#define N (1U << 27)
#define BUCKETS 256U
#ifndef BUFFER_SIZE
#define BUFFER_SIZE 16U
#endif

static unsigned work[N];
static unsigned counts[4][BUCKETS];
static unsigned fill[BUCKETS];
static unsigned buffer[BUCKETS][BUFFER_SIZE] __attribute__((aligned(64)));
static U duck;

U getauxval(U key) { return duck; }

static inline void prefix(unsigned *count) {
    unsigned sum = 0;
    for (unsigned i = 0; i < BUCKETS; ++i) {
        unsigned value = count[i]; count[i] = sum; sum += value;
    }
}

static __attribute__((always_inline)) inline void flush_nt(
        unsigned *destination, const unsigned *source) {
    #pragma GCC unroll 16
    for (unsigned i = 0; i < BUFFER_SIZE; i += 4)
        _mm_stream_si128((__m128i *)(destination + i),
                         _mm_load_si128((const __m128i *)(source + i)));
}

static __attribute__((always_inline)) inline void radix_pass(
        const unsigned *source, unsigned *destination, unsigned *position,
        unsigned *next_count, unsigned shift, unsigned next_shift) {
    for (unsigned i = 0; i < N; ++i) {
        unsigned value = source[i];
        unsigned digit = (value >> shift) & 255U;
        unsigned output = position[digit];
        if (output & 3U) {
            destination[output] = value;
            position[digit] = output + 1;
        } else {
            unsigned slot = fill[digit];
            buffer[digit][slot] = value;
            if (slot + 1 == BUFFER_SIZE) {
                flush_nt(destination + output, buffer[digit]);
                position[digit] = output + BUFFER_SIZE;
                fill[digit] = 0;
            } else {
                fill[digit] = slot + 1;
            }
        }
        if (next_count)
            ++next_count[(value >> next_shift) & 255U];
    }
    for (unsigned digit = 0; digit < BUCKETS; ++digit) {
        unsigned amount = fill[digit];
        unsigned output = position[digit];
        for (unsigned j = 0; j < amount; ++j)
            destination[output + j] = buffer[digit][j];
        position[digit] = output + amount;
        fill[digit] = 0;
    }
    _mm_sfence();
}

void sort(unsigned *a, int n) {
    (void)n;
    for (unsigned i = 0; i < N; ++i) ++counts[0][(unsigned char)a[i]];
    prefix(counts[0]);
    radix_pass(a, work, counts[0], counts[1], 0, 8);
    prefix(counts[1]);
    radix_pass(work, a, counts[1], counts[2], 8, 16);
    prefix(counts[2]);
    radix_pass(a, work, counts[2], counts[3], 16, 24);
    prefix(counts[3]);
    radix_pass(work, a, counts[3], (unsigned *)0, 24, 0);
}

__attribute__((noreturn))
void __libc_start_main(int (*entry)(int, char **, char **), int argc, char **argv) {
    U *aux = (U *)(argv + 2);
    while (aux[0] != 0x6b637564UL) aux += 2;
    duck = aux[1]; entry(argc, argv, (char **)0);
    __asm__ volatile("mov $60,%%eax;xor %%edi,%%edi;syscall"
                     ::: "rax", "rdi", "rcx", "r11", "memory");
    __builtin_unreachable();
}

CompilationN/AN/ACompile OKScore: N/A

Testcase #12.079 s1024 MB + 44 KBAcceptedScore: 100


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