提交记录 31217


用户 题目 状态 得分 用时 内存 语言 代码长度
user1 1001c. 测测你的排序4 Compile Error 0 0 ns 0 KB C++17 7.88 KB
提交时间 评测时间
2026-08-13 15:19:08 2026-08-13 15:19:11
#pragma GCC target("avx2")
#pragma GCC optimize("O3", "unroll-loops")
#include <immintrin.h>

#pragma GCC optimize("Ofast,inline,unroll-loops")
#include <bits/stdc++.h>
#include <immintrin.h>
#include <algorithm>

using namespace std;

    uint  b[1<<27];
template<int n>
struct foo {

template<class T>
static void F(uint* __restrict__ buc, uint* __restrict__ a, uint* __restrict__ b, T lambda) {
    for (int i = 0; i < n; i += 16) {
        _mm_prefetch(&a[i + 256], _MM_HINT_NTA);
        #pragma GCC unroll 16
        for (int j = 0; j < 16; j++)
            b[buc[lambda(a[i + j])]++] = a[i + j];
    }
}
static void sort(uint* a, int) {
    uint buc[4][256] = {};
    for (int i = 0; i < n; i++) {
        buc[0][a[i] & 255]++;
        buc[1][a[i] >> 8 & 255]++;
        buc[2][a[i] >> 16 & 255]++;
        buc[3][(a[i] >> 16) >> 8 & 255]++;
    }
    for (int k = 0; k < 4; k++) {
        uint32_t offset = 0;
        for (int i = 0; i < 256; i++)
            swap(buc[k][i], offset), offset += buc[k][i];
    }
    F(buc[0], a, b, [](uint x) { return x & 255; });
    F(buc[1], b, a, [](uint x) { return x >> 8 & 255; });
    F(buc[2], a, b, [](uint x) { return x >> 16 & 255; });
    F(buc[3], b, n==34217728?b+34217728:a, [](uint x) { return x >> 24; });
}
};
void sort(uint* a, int __n) {
    // nth_element(a, a+34217728, a+134217728);
    foo<100000000>::sort(a+34217728,100000000);
    foo<34217728>::sort(a, 34217728);
    int p = 0, q = 34217728, r = 34217728; 
    while (p!=r) a[p++] = r==134217728||b[q]<a[r]?b[q++]:a[r++];
}

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 RESERVED_CAPACITY (((N + HIGH_BUCKETS - 1) / HIGH_BUCKETS) + 8192U)
#define WORK_N (RESERVED_CAPACITY * HIGH_BUCKETS)
#ifndef BUFFER_SIZE
#define BUFFER_SIZE 32U
#endif
#ifndef LOCAL_NT
#define LOCAL_NT 0
#endif
#ifndef CURSOR_UNROLL
#define CURSOR_UNROLL 4
#endif
#ifndef PREFETCH_AT
#define PREFETCH_AT 999U
#endif
#define PRAGMA_INNER(value) _Pragma(#value)
#define PRAGMA(value) PRAGMA_INNER(value)


extern "C" {
static unsigned work[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) {
    for (unsigned i = 0; i < BUFFER_SIZE; i += 8)
        _mm256_stream_si256((__m256i *)(destination + i),
                            _mm256_load_si256((const __m256i *)(source + i)));
}

static __attribute__((always_inline)) inline void flush_high_nt(
        unsigned *destination, const unsigned *source) {
    flush_nt(destination, source);
}

static __attribute__((always_inline)) inline void scatter_high(
        const unsigned *source, unsigned *destination, unsigned *position) {
    for (unsigned i = 0; i < N; ++i) {
        unsigned value = source[i];
        unsigned digit = value >> HIGH_SHIFT;
        unsigned output = position[digit];
        if (output & 7U) {
            destination[output] = value;
            position[digit] = output + 1;
        } else {
            unsigned slot = high_fill[digit];
            buffer[digit][slot] = value;
            if (slot == BUFFER_SIZE - 1) {
                flush_high_nt(destination + output, buffer[digit]);
                position[digit] = output + BUFFER_SIZE;
                high_fill[digit] = 0;
            } else high_fill[digit] = slot + 1;
        }
    }
    for (unsigned digit = 0; digit < HIGH_BUCKETS; ++digit) {
        unsigned amount = high_fill[digit];
        unsigned output = position[digit];
        for (unsigned j = 0; j < amount; ++j)
            destination[output + j] = buffer[digit][j];
        position[digit] = output + amount;
        high_fill[digit] = 0;
    }
    _mm_sfence();
}

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

static __attribute__((always_inline)) inline void radix_pass_regular_cursor(
        const unsigned *source, unsigned *destination, const unsigned *start,
        unsigned shift, unsigned begin, unsigned end) {
    PRAGMA(GCC unroll CURSOR_UNROLL)
    for (unsigned i = begin; i < end; ++i) {
        unsigned value = source[i];
        unsigned digit = (value >> shift) & 255U;
        unsigned sequence = fill[digit]++;
        unsigned slot = sequence & (BUFFER_SIZE - 1);
        buffer[digit][slot] = value;
        if (slot == PREFETCH_AT) {
            unsigned *future = destination + start[digit] +
                               sequence - PREFETCH_AT;
            _mm_prefetch((const char *)(future + 0), _MM_HINT_T0);
            _mm_prefetch((const char *)(future + 16), _MM_HINT_T0);
            _mm_prefetch((const char *)(future + 32), _MM_HINT_T0);
            _mm_prefetch((const char *)(future + 48), _MM_HINT_T0);
        }
        if (slot == BUFFER_SIZE - 1)
            flush_regular(destination + start[digit] + sequence + 1 -
                          BUFFER_SIZE, buffer[digit]);
    }
    for (unsigned digit = 0; digit < BUCKETS; ++digit) {
        unsigned amount = fill[digit] & (BUFFER_SIZE - 1);
        unsigned output = start[digit] +
                          (fill[digit] & ~(BUFFER_SIZE - 1));
        for (unsigned j = 0; j < amount; ++j)
            destination[output + j] = buffer[digit][j];
        fill[digit] = 0;
    }
}

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) {
    for (unsigned i = begin; i < end; ++i) {
        unsigned value = source[i];
        unsigned digit = (value >> shift) & mask;
        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();
}


__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 ErrorScore: N/A


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