提交记录 30444


用户 题目 状态 得分 用时 内存 语言 代码长度
saffah_codex_260812 1001b. 测测你的排序3 Accepted 100 1.48 s 1048604 KB C 4.96 KB
提交时间 评测时间
2026-08-12 21:53:32 2026-08-12 21:53:38
#pragma GCC target("avx2")
#include <immintrin.h>

typedef unsigned long U;
#ifndef N
#define N (1U << 27)
#endif
#define BUCKETS 256U
#ifndef MSD_BITS
#define MSD_BITS 8U
#endif
#define HIGH_BUCKETS (1U << MSD_BITS)
#define HIGH_SHIFT (32U - MSD_BITS)
#define MAX_BUCKETS (HIGH_BUCKETS > BUCKETS ? HIGH_BUCKETS : BUCKETS)
#define BUFFER_SIZE 16U
#ifndef LOCAL_NT
#define LOCAL_NT 0
#endif

static unsigned work[N] __attribute__((aligned(64)));
static unsigned high_count[HIGH_BUCKETS], starts[HIGH_BUCKETS + 1];
static unsigned local_count[3][BUCKETS];
static unsigned fill[BUCKETS];
static unsigned high_fill[HIGH_BUCKETS];
static unsigned buffer[MAX_BUCKETS][BUFFER_SIZE] __attribute__((aligned(64)));
static U duck;

U getauxval(U key) { return duck; }

static inline void prefix_from(unsigned *count, unsigned base) {
    unsigned sum = base;
    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) {
    _mm256_stream_si256((__m256i *)(destination + 0),
                        _mm256_load_si256((const __m256i *)(source + 0)));
    _mm256_stream_si256((__m256i *)(destination + 8),
                        _mm256_load_si256((const __m256i *)(source + 8)));
}

static __attribute__((always_inline)) inline void flush_regular(
        unsigned *destination, const unsigned *source) {
    _mm256_store_si256((__m256i *)(destination + 0),
                       _mm256_load_si256((const __m256i *)(source + 0)));
    _mm256_store_si256((__m256i *)(destination + 8),
                       _mm256_load_si256((const __m256i *)(source + 8)));
}

static __attribute__((always_inline)) inline void radix_pass_range(
        const unsigned *source, unsigned *destination, unsigned *position,
        unsigned *pass_fill, unsigned shift, unsigned mask, unsigned buckets,
        unsigned begin, unsigned end, unsigned non_temporal,
        unsigned *next_count, unsigned next_shift) {
    for (unsigned i = begin; i < end; ++i) {
        unsigned value = source[i];
        unsigned digit = (value >> shift) & mask;
        if (next_count)
            ++next_count[(value >> next_shift) & 255U];
        unsigned output = position[digit];
        if (output & 7U) {
            destination[output] = value;
            position[digit] = output + 1;
        } else {
            unsigned slot = pass_fill[digit];
            buffer[digit][slot] = value;
            if (slot + 1 == BUFFER_SIZE) {
                if (non_temporal)
                    flush_nt(destination + output, buffer[digit]);
                else
                    flush_regular(destination + output, buffer[digit]);
                position[digit] = output + BUFFER_SIZE;
                pass_fill[digit] = 0;
            } else {
                pass_fill[digit] = slot + 1;
            }
        }
    }
    for (unsigned digit = 0; digit < buckets; ++digit) {
        unsigned amount = pass_fill[digit];
        unsigned output = position[digit];
        for (unsigned j = 0; j < amount; ++j)
            destination[output + j] = buffer[digit][j];
        position[digit] = output + amount;
        pass_fill[digit] = 0;
    }
    if (non_temporal) _mm_sfence();
}

void sort(unsigned *a, int n) {
    (void)n;
    for (unsigned i = 0; i < N; ++i) ++high_count[a[i] >> HIGH_SHIFT];
    unsigned sum = 0;
    for (unsigned i = 0; i < HIGH_BUCKETS; ++i) {
        starts[i] = sum;
        unsigned value = high_count[i]; high_count[i] = sum; sum += value;
    }
    starts[HIGH_BUCKETS] = N;
    radix_pass_range(a, work, high_count, high_fill, HIGH_SHIFT,
                     HIGH_BUCKETS - 1, HIGH_BUCKETS, 0, N, 1, 0, 0);

    for (unsigned bucket = 0; bucket < HIGH_BUCKETS; ++bucket) {
        unsigned begin = starts[bucket], end = starts[bucket + 1];
        __builtin_memset(local_count, 0, sizeof(local_count));
        for (unsigned i = begin; i < end; ++i) {
            unsigned value = work[i];
            ++local_count[0][(unsigned char)value];
        }
        prefix_from(local_count[0], begin);
        radix_pass_range(work, a, local_count[0], fill, 0, 255, 256,
                         begin, end, LOCAL_NT, local_count[1], 8);
        prefix_from(local_count[1], begin);
        radix_pass_range(a, work, local_count[1], fill, 8, 255, 256,
                         begin, end, LOCAL_NT, local_count[2], 16);
        prefix_from(local_count[2], begin);
        radix_pass_range(work, a, local_count[2], fill, 16, 255, 256,
                         begin, end, LOCAL_NT, 0, 0);
    }
    _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 #11.48 s1024 MB + 28 KBAcceptedScore: 100


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