提交记录 40377


用户 题目 状态 得分 用时 内存 语言 代码长度
saffah_dsh_260814 noi17a. 【NOI2017】整数 Wrong Answer 0 21.584 ms 21704 KB C 5.95 KB
提交时间 评测时间
2026-08-17 23:37:24 2026-08-17 23:38:08
#include <stdint.h>
#include <sys/auxv.h>
#include <unistd.h>
#include <immintrin.h>
#pragma GCC target("avx2,bmi,bmi2")

struct DuckInfo {
  uint64_t abi_version;
  const char *stdin_ptr; uint64_t stdin_size;
  char *stdout_ptr; uint64_t stdout_limit; uint64_t stdout_size;
  char *stderr_ptr; uint64_t stderr_limit; uint64_t stderr_size;
  const char *IB_ptr; uint64_t IB_limit;
  char *OB_ptr; uint64_t OB_limit;
  uint64_t tsc_frequency;
} __attribute__((packed));

typedef unsigned __int128 u128;
#define FULL ((u128)-1)
#define NB 234496
#define N1 3664
#define N2 58

static u128 blk[NB];
static uint64_t fm1[N1], nm1[N1];
static uint64_t fm2[N2], nm2[N2];
static uint64_t fm3, nm3;

static uint64_t O;
static char *out;

/* ---------- tokenizer ---------- */
#define IBUF_SIZE (32*1024*1024)
#define MAXTOK (3000008)
static char ibuf[IBUF_SIZE + 64];
/* packed token: low 24 bits = start offset, high 8 bits = length */
static uint32_t tok_arr[MAXTOK];
static uint32_t val_arr[MAXTOK];

/* Pass A: SIMD scan -> packed (start|len<<24) tokens */
static inline uint32_t scan_tokens(const char *p, uint64_t size){
    uint32_t cnt = 0;
    int carry = 0;
    uint32_t run_start = 0;
    const __m256i zl = _mm256_set1_epi8('0' - 1);
    uint64_t pos = 0;
    for (; pos < size; pos += 32){
        __m256i v = _mm256_loadu_si256((const __m256i*)(p + pos));
        __m256i d = _mm256_cmpgt_epi8(v, zl);
        uint32_t dm = (uint32_t)_mm256_movemask_epi8(d);
        if (pos + 32 > size) dm &= (1u << (size - pos)) - 1;
        if (carry){
            int f0 = __builtin_ctz(~dm);   /* first non-digit (dm != ~0 always here) */
            tok_arr[cnt] = run_start | (((uint32_t)(pos - run_start) + (uint32_t)f0) << 24);
            cnt++;
            carry = 0;
            dm &= ~((1u << (f0 + 1)) - 1);
        }
        if (dm & 0x80000000u){
            int s_last = 32 - __builtin_clz(~dm);
            run_start = (uint32_t)pos + (uint32_t)s_last;
            carry = 1;
            dm &= (1u << s_last) - 1;
        }
        uint32_t starts = dm & ~(dm << 1);
        uint32_t ends   = dm & ~(dm >> 1);
        uint32_t st = starts, en = ends;
        while (st){
            int s = __builtin_ctz(st);
            int e = __builtin_ctz(en);
            tok_arr[cnt] = ((uint32_t)pos + (uint32_t)s) | ((uint32_t)(e - s + 1) << 24);
            cnt++;
            st &= st - 1;
            en &= en - 1;
        }
    }
    if (carry){
        tok_arr[cnt] = run_start | ((uint32_t)(size - run_start) << 24);
        cnt++;
    }
    return cnt;
}

static const int8_t sh[9][16] __attribute__((aligned(16))) = {
    {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
    {-128,-128,-128,-128,-128,-128,-128,0,  -128,-128,-128,-128,-128,-128,-128,-128},
    {-128,-128,-128,-128,-128,-128,0,1,     -128,-128,-128,-128,-128,-128,-128,-128},
    {-128,-128,-128,-128,-128,0,1,2,        -128,-128,-128,-128,-128,-128,-128,-128},
    {-128,-128,-128,-128,0,1,2,3,           -128,-128,-128,-128,-128,-128,-128,-128},
    {-128,-128,-128,0,1,2,3,4,              -128,-128,-128,-128,-128,-128,-128,-128},
    {-128,-128,0,1,2,3,4,5,                 -128,-128,-128,-128,-128,-128,-128,-128},
    {-128,0,1,2,3,4,5,6,                    -128,-128,-128,-128,-128,-128,-128,-128},
    {0,1,2,3,4,5,6,7,                        -128,-128,-128,-128,-128,-128,-128,-128},
};

static inline uint32_t conv8(const char *p, int L){
    __m128i v = _mm_loadu_si128((const __m128i*)p);
    __m128i d = _mm_sub_epi8(v, _mm_set1_epi8('0'));
    __m128i a = _mm_shuffle_epi8(d, _mm_load_si128((const __m128i*)sh[L]));
    __m128i m = _mm_maddubs_epi16(a, _mm_setr_epi8(10,1,10,1,10,1,10,1,10,1,10,1,10,1,10,1));
    __m128i m2 = _mm_madd_epi16(m, _mm_setr_epi16(100,1,100,1,100,1,100,1));
    __m128i m3 = _mm_mullo_epi32(m2, _mm_setr_epi32(10000,1,10000,1));
    __m128i h = _mm_hadd_epi32(m3, m3);
    return (uint32_t)_mm_cvtsi128_si32(h);
}

static inline void convert_all(const char *p, uint32_t cnt){
    for (uint32_t i = 0; i < cnt; i++){
        uint32_t tok = tok_arr[i];
        uint32_t start = tok & 0xFFFFFFu;
        int L = (int)(tok >> 24);
        uint32_t v;
        if (L > 8){
            int h = p[start] - '0';
            const char *lo = p + start + 1;
            if (L == 10){ h = h*10 + (p[start+1]-'0'); lo = p + start + 2; }
            v = (uint32_t)h * 100000000u + conv8(lo, 8);
        } else if (L == 1){
            v = (uint32_t)(p[start] - '0');
        } else {
            v = conv8(p + start, L);
        }
        if (start && p[start - 1] == '-') v = (uint32_t)(-(int32_t)v);
        val_arr[i] = v;
    }
}


__attribute__((noreturn)) static void done(uint64_t olen, struct DuckInfo *di, int use_di){
    if (use_di) di->stdout_size = olen;
    else (void)!write(1, out, olen);
    asm volatile("mov $60, %%eax; xor %%edi, %%edi; syscall" ::: "rax","rdi","memory");
    __builtin_unreachable();
}
int main(){
    struct DuckInfo *di = (struct DuckInfo*)getauxval(0x6b637564);
    uint64_t size; const char *P; int use_di = 0;
    if (di && di->abi_version >= 1 && di->stdin_ptr && di->stdout_ptr){
        size = di->stdin_size; P = di->stdin_ptr; out = di->stdout_ptr; use_di = 1;
    } else {
        long n2 = 0, t;
        while (n2 < IBUF_SIZE && (t = read(0, ibuf + n2, IBUF_SIZE - n2)) > 0) n2 += t;
        size = (uint64_t)n2;
        for (int j = 0; j < 64; j++) ibuf[n2 + j] = ' ';
        P = ibuf;
        static char obuf[1<<22]; out = obuf;
    }
    uint32_t cnt = scan_tokens(P, size);
    convert_all(P, cnt);
    int n = (int32_t)val_arr[0];
    uint32_t idx = 4;
    uint64_t acc = 0;
    for (int i = 0; i < n; i++){
        int op = (int32_t)val_arr[idx++];
        if (op == 1){
            int a = (int32_t)val_arr[idx++];
            int b = (int32_t)val_arr[idx++];
            acc += (uint64_t)(a < 0 ? -(int64_t)a : a) + (uint64_t)b;
        } else {
            int k = (int32_t)val_arr[idx++];
            acc += (uint64_t)k;
            out[O++] = (char)('0' + (acc & 1));
            out[O++] = '\n';
        }
    }
    done(O, di, use_di);
}

CompilationN/AN/ACompile OKScore: N/A

Testcase #16.21 us20 KBWrong AnswerScore: 0

Testcase #29.42 us20 KBWrong AnswerScore: 0

Testcase #356.81 us56 KBWrong AnswerScore: 0

Testcase #486.83 us96 KBWrong AnswerScore: 0

Testcase #5106.87 us148 KBWrong AnswerScore: 0

Testcase #6161.73 us192 KBWrong AnswerScore: 0

Testcase #7206.19 us212 KBWrong AnswerScore: 0

Testcase #8223.58 us236 KBWrong AnswerScore: 0

Testcase #9657.46 us664 KBWrong AnswerScore: 0

Testcase #10811.66 us1 MB + 96 KBWrong AnswerScore: 0

Testcase #111.274 ms1 MB + 292 KBWrong AnswerScore: 0

Testcase #121.263 ms1 MB + 400 KBWrong AnswerScore: 0

Testcase #131.507 ms1 MB + 508 KBWrong AnswerScore: 0

Testcase #144.294 ms4 MB + 252 KBWrong AnswerScore: 0

Testcase #155.948 ms6 MB + 368 KBWrong AnswerScore: 0

Testcase #168.697 ms8 MB + 488 KBWrong AnswerScore: 0

Testcase #1710.786 ms10 MB + 600 KBWrong AnswerScore: 0

Testcase #1813.047 ms12 MB + 724 KBWrong AnswerScore: 0

Testcase #1915.173 ms14 MB + 836 KBWrong AnswerScore: 0

Testcase #2016.474 ms16 MB + 36 KBWrong AnswerScore: 0

Testcase #2118.264 ms19 MB + 44 KBWrong AnswerScore: 0

Testcase #2220.185 ms19 MB + 700 KBWrong AnswerScore: 0

Testcase #2315.676 ms20 MB + 632 KBWrong AnswerScore: 0

Testcase #2421.474 ms20 MB + 964 KBWrong AnswerScore: 0

Testcase #2521.584 ms21 MB + 200 KBWrong AnswerScore: 0


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