提交记录 30478


用户 题目 状态 得分 用时 内存 语言 代码长度
saffah_codex_260812 1001. 测测你的排序 Accepted 100 977.265 ms 689696 KB C 7.85 KB
提交时间 评测时间
2026-08-12 22:25:00 2026-08-12 22:25:05
#pragma GCC target("avx2")
#pragma GCC optimize("O3", "unroll-loops")
#include <immintrin.h>

typedef unsigned long U;
#ifndef N
#define N 100000000U
#endif
#define BUCKETS 256U
#define U32_BUFFER_SIZE 64U
#define PACKED_BUFFER_SIZE 64U
#define HIGH_BUFFER_SIZE 32U
#define RESERVED_CAPACITY (((N + BUCKETS - 1) / BUCKETS) + 8192U)
#define WORK_N (RESERVED_CAPACITY * BUCKETS)

static unsigned char work[3ULL * WORK_N] __attribute__((aligned(64)));
static unsigned position[BUCKETS], starts[BUCKETS + 1];
static unsigned count[3][BUCKETS];
static unsigned fill[BUCKETS], high_fill[BUCKETS];
static unsigned char buffers[BUCKETS][U32_BUFFER_SIZE * 4]
    __attribute__((aligned(64)));
static U duck;

U getauxval(U key) { return duck; }

static __attribute__((always_inline)) inline unsigned load24(
        const unsigned char *source) {
    return (unsigned)source[0] | ((unsigned)source[1] << 8) |
           ((unsigned)source[2] << 16);
}

static __attribute__((always_inline)) inline void store24(
        unsigned char *destination, unsigned value) {
    destination[0] = (unsigned char)value;
    destination[1] = (unsigned char)(value >> 8);
    destination[2] = (unsigned char)(value >> 16);
}

static __attribute__((always_inline)) inline void flush_packed_nt(
        unsigned char *destination, const unsigned char *source) {
    _mm256_stream_si256((__m256i *)(destination + 0),
                        _mm256_load_si256((const __m256i *)(source + 0)));
    _mm256_stream_si256((__m256i *)(destination + 32),
                        _mm256_load_si256((const __m256i *)(source + 32)));
    _mm256_stream_si256((__m256i *)(destination + 64),
                        _mm256_load_si256((const __m256i *)(source + 64)));
}

static __attribute__((always_inline)) inline void flush_packed_regular(
        unsigned char *destination, const unsigned char *source) {
    for (unsigned offset = 0; offset < 3 * PACKED_BUFFER_SIZE; offset += 32)
        _mm256_storeu_si256((__m256i *)(destination + offset),
                            _mm256_load_si256(
                                (const __m256i *)(source + offset)));
}

static __attribute__((always_inline)) inline void flush_u32_regular(
        unsigned *destination, const unsigned *source) {
    for (unsigned offset = 0; offset < U32_BUFFER_SIZE; offset += 8)
        _mm256_storeu_si256((__m256i *)(destination + offset),
                            _mm256_load_si256(
                                (const __m256i *)(source + offset)));
}

static void scatter_high_packed(const unsigned *source) {
    for (unsigned digit = 0; digit < BUCKETS; ++digit)
        position[digit] = digit * RESERVED_CAPACITY;
    for (unsigned i = 0; i < N; ++i) {
        unsigned value = source[i];
        unsigned digit = value >> 24;
        unsigned output = position[digit];
        unsigned char *destination = work + 3ULL * output;
        if ((U)destination & 31U) {
            store24(destination, value);
            position[digit] = output + 1;
        } else {
            unsigned slot = high_fill[digit];
            store24(buffers[digit] + 3 * slot, value);
            if (slot == HIGH_BUFFER_SIZE - 1) {
                flush_packed_nt(destination, buffers[digit]);
                position[digit] = output + HIGH_BUFFER_SIZE;
                high_fill[digit] = 0;
            } else high_fill[digit] = slot + 1;
        }
    }
    for (unsigned digit = 0; digit < BUCKETS; ++digit) {
        unsigned output = position[digit];
        unsigned amount = high_fill[digit];
        for (unsigned j = 0; j < amount; ++j)
            store24(work + 3ULL * (output + j),
                    load24(buffers[digit] + 3 * j));
        position[digit] = output + amount;
        high_fill[digit] = 0;
    }
    _mm_sfence();
}

static inline void prefix(unsigned *values, unsigned base) {
    unsigned sum = base;
    for (unsigned digit = 0; digit < BUCKETS; ++digit) {
        unsigned amount = values[digit];
        values[digit] = sum;
        sum += amount;
    }
}

static __attribute__((always_inline)) inline void packed_to_u32(
        const unsigned char *source, unsigned source_begin,
        unsigned source_end, unsigned *destination, const unsigned *start,
        unsigned shift, unsigned high) {
    for (unsigned i = source_begin; i < source_end; ++i) {
        unsigned value = load24(source + 3ULL * i) | high;
        unsigned digit = (value >> shift) & 255U;
        unsigned sequence = fill[digit]++;
        unsigned slot = sequence & (U32_BUFFER_SIZE - 1);
        ((unsigned *)buffers[digit])[slot] = value;
        if (slot == U32_BUFFER_SIZE - 1)
            flush_u32_regular(destination + start[digit] + sequence + 1 -
                              U32_BUFFER_SIZE,
                              (const unsigned *)buffers[digit]);
    }
    for (unsigned digit = 0; digit < BUCKETS; ++digit) {
        unsigned amount = fill[digit] & (U32_BUFFER_SIZE - 1);
        unsigned output = start[digit] +
                          (fill[digit] & ~(U32_BUFFER_SIZE - 1));
        const unsigned *source_buffer = (const unsigned *)buffers[digit];
        for (unsigned j = 0; j < amount; ++j)
            destination[output + j] = source_buffer[j];
        fill[digit] = 0;
    }
}

static __attribute__((always_inline)) inline void u32_to_packed(
        const unsigned *source, unsigned begin, unsigned end,
        unsigned char *destination, const unsigned *start, unsigned shift) {
    for (unsigned i = begin; i < end; ++i) {
        unsigned value = source[i];
        unsigned digit = (value >> shift) & 255U;
        unsigned sequence = fill[digit]++;
        unsigned slot = sequence & (PACKED_BUFFER_SIZE - 1);
        store24(buffers[digit] + 3 * slot, value);
        if (slot == PACKED_BUFFER_SIZE - 1)
            flush_packed_regular(destination +
                                 3ULL * (start[digit] + sequence + 1 -
                                         PACKED_BUFFER_SIZE),
                                 buffers[digit]);
    }
    for (unsigned digit = 0; digit < BUCKETS; ++digit) {
        unsigned amount = fill[digit] & (PACKED_BUFFER_SIZE - 1);
        unsigned output = start[digit] +
                          (fill[digit] & ~(PACKED_BUFFER_SIZE - 1));
        for (unsigned j = 0; j < amount; ++j)
            store24(destination + 3ULL * (output + j),
                    load24(buffers[digit] + 3 * j));
        fill[digit] = 0;
    }
}

void sort(unsigned *a, int n) {
    (void)n;
    scatter_high_packed(a);
    unsigned sum = 0;
    for (unsigned digit = 0; digit < BUCKETS; ++digit) {
        starts[digit] = sum;
        sum += position[digit] - digit * RESERVED_CAPACITY;
    }
    starts[BUCKETS] = N;

    for (unsigned bucket = 0; bucket < BUCKETS; ++bucket) {
        unsigned begin = starts[bucket], end = starts[bucket + 1];
        unsigned source_begin = bucket * RESERVED_CAPACITY;
        unsigned source_end = source_begin + end - begin;
        __builtin_memset(count, 0, sizeof(count));
        for (unsigned i = source_begin; i < source_end; ++i) {
            const unsigned char *value = work + 3ULL * i;
            ++count[0][value[0]];
            ++count[1][value[1]];
            ++count[2][value[2]];
        }
        prefix(count[0], begin);
        prefix(count[1], begin);
        prefix(count[2], begin);
        unsigned high = bucket << 24;
        packed_to_u32(work, source_begin, source_end, a, count[0], 0, high);
        u32_to_packed(a, begin, end, work, count[1], 8);
        packed_to_u32(work, begin, end, a, count[2], 16, high);
    }
    _mm256_zeroupper();
}

__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 #1977.265 ms673 MB + 544 KBAcceptedScore: 100


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