提交记录 117126


用户 题目 状态 得分 用时 内存 语言 代码长度
saffah_cc_v41_260924 noi17a. 【NOI2017】整数 Accepted 100 23.898 ms 5040 KB C++17 32.90 KB
提交时间 评测时间
2026-09-30 21:19:35 2026-09-30 21:19:47
#pragma GCC optimize("O3","unroll-loops","schedule-insns2","tracer","no-caller-saves")
#pragma GCC optimize("align-functions=64,align-jumps=16,align-loops=16")
#pragma GCC target("arch=skylake","tune=haswell")
// NOI2017 整数 (integer) -- duck.ac noi17a
// MAIN-style.  x is a non-negative big integer (bits up to 30n).
// Ops:  x += a*2^b  (|a| <= 1e9, 0 <= b),  query bit k.
//
// Structure: 30-bit "blocks", 64 blocks per "word", 64 words per "chunk".
//   valarr[b] : block value (only meaningful when the block is mixed)
//   lww[w]    : word state E(0)/F(1)/M(2)  -- F means all 64 blocks are 0x3FFFFFFF
//   nf0[w],ne0[w] : per-block "not full"/"not empty" masks (valid iff lww[w]==M)
//   lcc[c]    : chunk state E/F/M (uniform chunk => all its words are E/F)
//   nfw[c],newc[c] : per-word "not full"/"not empty" masks (valid iff lcc[c]==M)
//   nfx/nex   : bitmaps over chunks
#include <stdint.h>
#include <string.h>
#include <emmintrin.h>
#include <x86intrin.h>
#define SPR_STRIDE 0u
#define SPR_MASK 0ul
#define SPR_STEP 512


typedef uint8_t u8;
typedef uint32_t u32;
typedef uint64_t u64;
typedef unsigned long ul;
#ifdef TIMING
#include <stdio.h>
static void rep(const char* k, unsigned long long v) { fprintf(stderr, "%s %llu\n", k, v); }
#endif


// exact compare of n <= 24 bytes; returns 0 (no match) for n > 24
static inline __attribute__((always_inline)) int eqbN(const char* a, const char* b, size_t n) {
  if (n > 24) return 0;
  u64 x, y;
  __builtin_memcpy(&x, a, 8); __builtin_memcpy(&y, b, 8);
  if (x != y) return 0;
  if (n <= 8) return 1;
  __builtin_memcpy(&x, a + 8, 8); __builtin_memcpy(&y, b + 8, 8);
  size_t m = n - 8;
  if (m < 8) { u64 k = (1ULL << (m * 8)) - 1; x &= k; y &= k; }
  if (x != y) return 0;
  if (n <= 16) return 1;
  __builtin_memcpy(&x, a + 16, 8); __builtin_memcpy(&y, b + 16, 8);
  m = n - 16;
  if (m < 8) { u64 k = (1ULL << (m * 8)) - 1; x &= k; y &= k; }
  return x == y;
}

// single-instruction 256-bit unaligned store: `vmovdqu %ymm0,(%rdi)` (1 uop, no p5).
// A bare `_mm256_storeu_si256` is split by gcc-9 into
//   vmovups %xmm0,(%rdi) + vextracti128 $0x1,%ymm0,0x10(%rdi)   (2 uops, one on p5).
// volatile + "memory" clobber are REQUIRED: a pure asm with only an "m" input is
// eliminated by gcc and the stores vanish silently.
static inline void STU256(void* p, __m256i v) {
  __asm__ volatile("vmovdqu %0, %1" :: "x"(v), "m"(*(__m256i*)p) : "memory");
}

static inline __attribute__((always_inline)) void fillU8(u8* p, u8 v, int n) {
  __m256i y = _mm256_set1_epi8((char)v);
  u64 q;
  while (n >= 128) {
    STU256((p + 0), y);
    STU256((p + 32), y);
    STU256((p + 64), y);
    STU256((p + 96), y);
    p += 128; n -= 128;
  }
  if (n >= 32) {
    int k = n >> 5;
    for (int i = 0; i < k; i++) STU256((p + (i << 5)), y);
    if (n & 31) STU256((p + n - 32), y);
  } else {
  __m128i x = _mm_set1_epi8((char)v);
  while (n >= 64) {
    _mm_storeu_si128((__m128i*)(p + 0), x);
    _mm_storeu_si128((__m128i*)(p + 16), x);
    _mm_storeu_si128((__m128i*)(p + 32), x);
    _mm_storeu_si128((__m128i*)(p + 48), x);
    p += 64; n -= 64;
  }
  if (n >= 16) {
    int k = n >> 4;
    for (int i = 0; i < k; i++) _mm_storeu_si128((__m128i*)(p + (i << 4)), x);
    if (n & 15) _mm_storeu_si128((__m128i*)(p + n - 16), x);   // overlaps backwards, in-bounds
  } else if (n >= 8) {
    q = 0x0101010101010101ULL * v;
    __builtin_memcpy(p, &q, 8); __builtin_memcpy(p + n - 8, &q, 8);
  } else if (n >= 4) {
    u32 w = 0x01010101u * (u32)v;
    __builtin_memcpy(p, &w, 4); __builtin_memcpy(p + n - 4, &w, 4);
  } else if (n >= 2) {
    p[0] = v; p[1] = v; p[n - 2] = v; p[n - 1] = v;
  } else if (n == 1) *p = v;
  }
}


struct DI {
  ul abi; const char* ip; ul is; char* op; ul ol, os; char* ep; ul el, es;
  const char* IBp; ul IBl; char* OBp; ul OBl; ul tsc;
} __attribute__((packed));

#define BLK 0xFFFFFFFFu     // 2^32-1   (W32)
#define ST_E 0
#define ST_F 1
#define ST_M 2

#define NBMAX 1000008
#define NWMAX ((NBMAX + 63) / 64)
#define NCMAX ((NWMAX + 63) / 64)
#define NFXW ((NCMAX + 63) / 64)
#define NFXW2 (NFXW + 2)


struct DS {
  u32 valarr[NBMAX];
  u64 nf0[NWMAX], ne0[NWMAX];
  // !!!! DO NOT SHRINK THIS TO NWMAX !!!!
  // pushChunk() does fillU8(lww + (c<<6), st, 64) for c up to NCU-1, and the LAST
  // chunk is partial (NWMAX = ceil(NBMAX/64) = 15626 is not a multiple of 64), so it
  // writes 54 bytes PAST the array on purpose-legitimately-reachable input.
  // With +64 those bytes land in dead padding. Without it they land in the NEXT
  // member (nfw -> silently corrupts the search masks -> test 25 WA 96/100, which
  // looks like a correctness bug, not a buffer bug). The +64 is load-bearing.
  u8 lww[NWMAX + 64];
  u64 nfw[NCMAX], newc[NCMAX];
  u8 lcc[NCMAX];
  u64 nfx[NFXW2], nex[NFXW2];
};
static DS D;
#ifdef PROBE
static u64 PC_add, PC_qry, PC_find, PC_assign, PC_units, PC_carry, PC_borrow, PC_blk;
static u64 PC_cy_add, PC_cy_qry, PC_cy_parse, PC_cy_assign, PC_cy_find, PC_qdeep;
static u64 PC_fw, PC_fww, PC_pc, PC_blkcnt;
#endif


static int NWU, NCU;
#define CHOPX 1
#ifndef CHOP
#define CHOP 65536
#endif
static long ocnt;
unsigned long sp_stride = SPR_STRIDE;
unsigned long sp_mask = SPR_MASK;


static inline u64 rangeMask(int lo, int hi) {   // bits [lo,hi), 0<=lo<hi<=64
  u64 m = ~0ULL << lo;
  if (hi < 64) m &= ~0ULL >> (64 - hi);
  return m;
}

static inline void markChunk(int c) {
  u64 bit = 1ULL << (c & 63);
  int cw = c >> 6;
  u64 a = D.nfw[c], b = D.newc[c];
  D.nfx[cw] = (D.nfx[cw] & ~bit) | (bit & (u64)-(long long)(a != 0));
  D.nex[cw] = (D.nex[cw] & ~bit) | (bit & (u64)-(long long)(b != 0));
}

static inline void pushChunk(int c) {
  u8 st = D.lcc[c];
#ifdef PROBE
  PC_pc++;
#endif
  if (st == ST_M) return;
  fillU8(D.lww + (c << 6), st, 64);
  if (st == ST_F) { D.nfw[c] = 0; D.newc[c] = ~0ULL; }
  else            { D.nfw[c] = ~0ULL; D.newc[c] = 0; }
  D.lcc[c] = ST_M;
}

static inline u32 getBlock(int b) {
  int w = b >> 6, c = w >> 6;
  u8 s = D.lcc[c];
  if (s != ST_M) return (s == ST_F) ? BLK : 0u;
  u8 sw = D.lww[w];
  if (sw != ST_M) return (sw == ST_F) ? BLK : 0u;
  u64 bit = 1ULL << (b & 63);
  if (!(D.nf0[w] & bit)) return BLK;
  if (!(D.ne0[w] & bit)) return 0u;
#ifdef PROBE
  PC_qdeep++;
#endif
  return D.valarr[b];
}

static inline __attribute__((always_inline)) void setBlock(int b, u32 v) {
#ifdef PROBE
  PC_blkcnt++;
#endif
  int w = b >> 6, c = w >> 6;
  pushChunk(c);
  u64 bit = 1ULL << (b & 63);
  u8 sw = D.lww[w];
  if (sw == ST_F) { D.nf0[w] = 0; D.ne0[w] = ~0ULL; }
  else if (sw == ST_E) { D.nf0[w] = ~0ULL; D.ne0[w] = 0; }
  if (v == 0) { D.nf0[w] |= bit; D.ne0[w] &= ~bit; }
  else if (v == BLK) { D.nf0[w] &= ~bit; D.ne0[w] |= bit; }
  else { D.nf0[w] |= bit; D.ne0[w] |= bit; D.valarr[b] = v; }
  u8 st = (D.nf0[w] == 0) ? ST_F : ((D.ne0[w] == 0) ? ST_E : ST_M);
  if (st != sw) {
    D.lww[w] = st;
    u64 wbit = 1ULL << (w & 63);
    if (st != ST_F) D.nfw[c] |= wbit; else D.nfw[c] &= ~wbit;
    if (st != ST_E) D.newc[c] |= wbit; else D.newc[c] &= ~wbit;
    markChunk(c);
  }
}

static inline int firstChunkBit(const u64* bm, int c2) {
  if (c2 >= NCU) return -1;
  int cw = c2 >> 6;
  u64 m = bm[cw] & (~0ULL << (c2 & 63));
  while (!m) { if (++cw >= NFXW) return -1; m = bm[cw]; }
  return (cw << 6) + __builtin_ctzll(m);
}

// first block >= p that is not all-ones, -1 if none
static inline int findNonFull(int p) {
#ifdef PROBE
  PC_find++;
  u64 _tf = __rdtsc();
#endif
  int w = p >> 6, c = w >> 6;
  u8 s = D.lcc[c];
  if (s == ST_E) return p;
  if (s != ST_F) {
    u64 m = D.nfw[c] & (~0ULL << (w & 63));
    while (m) {
      int W = (c << 6) + __builtin_ctzll(m);
      if (D.lww[W] == ST_E) return (W == w) ? p : (W << 6);
      u64 bits = D.nf0[W];
      if (W == w) bits &= ~0ULL << (p & 63);
      if (bits) return (W << 6) + __builtin_ctzll(bits);
      m &= m - 1;
    }
  }
  int C = firstChunkBit(D.nfx, c + 1);
#ifdef PROBE
  PC_cy_find += __rdtsc() - _tf;
#endif
  if (C < 0) return -1;
  if (D.lcc[C] == ST_E) return C << 12;
  int W = (C << 6) + __builtin_ctzll(D.nfw[C]);
  if (D.lww[W] == ST_E) return W << 6;
  return (W << 6) + __builtin_ctzll(D.nf0[W]);
}

// first block >= p that is not all-zeros, -1 if none
static inline int findNonEmpty(int p) {
  int w = p >> 6, c = w >> 6;
  u8 s = D.lcc[c];
  if (s == ST_F) return p;
  if (s != ST_E) {
    u64 m = D.newc[c] & (~0ULL << (w & 63));
    while (m) {
      int W = (c << 6) + __builtin_ctzll(m);
      if (D.lww[W] == ST_F) return (W == w) ? p : (W << 6);
      u64 bits = D.ne0[W];
      if (W == w) bits &= ~0ULL << (p & 63);
      if (bits) return (W << 6) + __builtin_ctzll(bits);
      m &= m - 1;
    }
  }
  int C = firstChunkBit(D.nex, c + 1);
  if (C < 0) return -1;
  if (D.lcc[C] == ST_F) return C << 12;
  int W = (C << 6) + __builtin_ctzll(D.newc[C]);
  if (D.lww[W] == ST_F) return W << 6;
  return (W << 6) + __builtin_ctzll(D.ne0[W]);
}

static inline void finishWord(int w, int S) {
  int c = w >> 6;
  u8 st = (D.nf0[w] == 0) ? ST_F : ((D.ne0[w] == 0) ? ST_E : ST_M);
  (void)S;
  if (st != D.lww[w]) {
    D.lww[w] = st;
    u64 wbit = 1ULL << (w & 63);
    if (st != ST_F) D.nfw[c] |= wbit; else D.nfw[c] &= ~wbit;
    if (st != ST_E) D.newc[c] |= wbit; else D.newc[c] &= ~wbit;
    markChunk(c);
  }
}

// assign blocks [64w+lo, 64w+hi) to state S
static inline void assignInWord(int w, int lo, int hi, int S) {
  int c = w >> 6;
  pushChunk(c);
  u8 sw = D.lww[w];
  if (sw == ST_F) { D.nf0[w] = 0; D.ne0[w] = ~0ULL; }
  else if (sw == ST_E) { D.nf0[w] = ~0ULL; D.ne0[w] = 0; }
  u64 mask = rangeMask(lo, hi);
  if (S == ST_F) { D.nf0[w] &= ~mask; D.ne0[w] |= mask; }
  else           { D.nf0[w] |= mask; D.ne0[w] &= ~mask; }
  finishWord(w, S);
}

// assign whole words [64c+lo, 64c+hi) of chunk c to state S
static inline void fillWords(int c, int lo, int hi, int S) {
  if (lo >= hi) return;
  pushChunk(c);
#ifdef PROBE
  PC_fww += (u64)(hi - lo); PC_fw++;
#endif
  fillU8(D.lww + (c << 6) + lo, (u8)S, hi - lo);
  u64 mask = rangeMask(lo, hi);
  if (S == ST_F) { D.nfw[c] &= ~mask; D.newc[c] |= mask; }
  else           { D.nfw[c] |= mask; D.newc[c] &= ~mask; }
  markChunk(c);
}

static inline void setBitRange(u64* bm, int lo, int hi, int val) {
  if (lo >= hi) return;
  int w0 = lo >> 6, w1 = (hi - 1) >> 6;
  if (w0 == w1) {
    u64 mask = rangeMask(lo & 63, ((hi - 1) & 63) + 1);
    if (val) bm[w0] |= mask; else bm[w0] &= ~mask;
    return;
  }
  u64 mask0 = ~0ULL << (lo & 63);
  if (val) bm[w0] |= mask0; else bm[w0] &= ~mask0;
  u64 mask1 = rangeMask(0, ((hi - 1) & 63) + 1);
  if (val) bm[w1] |= mask1; else bm[w1] &= ~mask1;
  for (int j = w0 + 1; j < w1; j++) bm[j] = val ? ~0ULL : 0ULL;
}

static inline void fullChunks(int c1, int c2, int S) {
  if (c1 >= c2) return;
#ifdef PROBE
  PC_units += (u64)(c2 - c1); PC_fw += (u64)(c2 - c1);
#endif
  fillU8(D.lcc + c1, (u8)S, c2 - c1);
  // D.nfw[]/D.newc[] of a uniform chunk are never read (pushChunk re-derives them),
  // so they are deliberately left stale here.  nfx/nex for the whole range is
  // applied once by markRange() at the end of assignBlocks (fused RMW).
}


// ---- fused ripple read-modify-write -------------------------------------
// The shipped ripple does setBlock(e, getBlock(e) + 1).  Both halves load
// lcc[c], lww[w], nf0[w] and ne0[w] for the SAME word w, and both call pushChunk.
// The caller guarantees the block is NOT full (findNonFull) resp. NOT empty
// (findNonEmpty), so only one of the three terminal states is reachable and the
// fused form needs ONE mask load/store pair.
static inline void incBlock(int b) {
  int w = b >> 6, c = w >> 6;
  pushChunk(c);
  u64 bit = 1ULL << (b & 63);
  u8 sw = D.lww[w];
  u64 nf, ne; u32 v;
  if (sw == ST_M) {
    nf = D.nf0[w]; ne = D.ne0[w];
    v = (nf & bit) ? ((ne & bit) ? D.valarr[b] : 0u) : BLK;
  } else if (sw == ST_F) { nf = 0ULL; ne = ~0ULL; v = BLK; }
  else { nf = ~0ULL; ne = 0ULL; v = 0u; }
  u32 nv = v + 1u;                       // v != BLK by precondition => nv != 0
  if (nv == BLK) { nf &= ~bit; ne |= bit; }
  else           { nf |= bit; ne |= bit; D.valarr[b] = nv; }
  D.nf0[w] = nf; D.ne0[w] = ne;
  u8 st = (nf == 0) ? ST_F : ((ne == 0) ? ST_E : ST_M);
  if (st != sw) {
    D.lww[w] = st;
    u64 wbit = 1ULL << (w & 63);
    if (st != ST_F) D.nfw[c] |= wbit; else D.nfw[c] &= ~wbit;
    if (st != ST_E) D.newc[c] |= wbit; else D.newc[c] &= ~wbit;
    markChunk(c);
  }
}

static inline void decBlock(int b) {
  int w = b >> 6, c = w >> 6;
  pushChunk(c);
  u64 bit = 1ULL << (b & 63);
  u8 sw = D.lww[w];
  u64 nf, ne; u32 v;
  if (sw == ST_M) {
    nf = D.nf0[w]; ne = D.ne0[w];
    v = (nf & bit) ? ((ne & bit) ? D.valarr[b] : 0u) : BLK;
  } else if (sw == ST_F) { nf = 0ULL; ne = ~0ULL; v = BLK; }
  else { nf = ~0ULL; ne = 0ULL; v = 0u; }
  u32 nv = v - 1u;                       // v != 0 by precondition => nv != BLK
  if (nv == 0) { nf |= bit; ne &= ~bit; }
  else         { nf |= bit; ne |= bit; D.valarr[b] = nv; }
  D.nf0[w] = nf; D.ne0[w] = ne;
  u8 st = (nf == 0) ? ST_F : ((ne == 0) ? ST_E : ST_M);
  if (st != sw) {
    D.lww[w] = st;
    u64 wbit = 1ULL << (w & 63);
    if (st != ST_F) D.nfw[c] |= wbit; else D.nfw[c] &= ~wbit;
    if (st != ST_E) D.newc[c] |= wbit; else D.newc[c] &= ~wbit;
    markChunk(c);
  }
}

// ---- fused range assign -------------------------------------------------
// Set the state of one (partial) word: blocks [lo,hi) of word w get S.
// Caller has already pushed chunk c.
static inline void setWordBits(int w, int lo, int hi, int S) {
  int c = w >> 6;
  u8 sw = D.lww[w];
  u64 nf, ne;
  if (sw == ST_M) { nf = D.nf0[w]; ne = D.ne0[w]; }
  else if (sw == ST_F) { nf = 0ULL; ne = ~0ULL; }
  else { nf = ~0ULL; ne = 0ULL; }
  u64 mask = rangeMask(lo, hi);
  if (S == ST_F) { nf &= ~mask; ne |= mask; }
  else           { nf |= mask; ne &= ~mask; }
  D.nf0[w] = nf; D.ne0[w] = ne;
  u8 st = (nf == 0) ? ST_F : ((ne == 0) ? ST_E : ST_M);
  D.lww[w] = st;
  u64 wbit = 1ULL << (w & 63);
  if (st != ST_F) D.nfw[c] |= wbit; else D.nfw[c] &= ~wbit;
  if (st != ST_E) D.newc[c] |= wbit; else D.newc[c] &= ~wbit;
}

// Whole words [lo,hi) of chunk c get S (uniform), updating lww and the chunk masks.
static inline void setWholeWords(int c, int lo, int hi, int S) {
  if (lo >= hi) return;
  fillU8(D.lww + (c << 6) + lo, (u8)S, hi - lo);
  u64 mask = rangeMask(lo, hi);
  if (S == ST_F) { D.nfw[c] &= ~mask; D.newc[c] |= mask; }
  else           { D.nfw[c] |= mask; D.newc[c] &= ~mask; }
}

// Assign blocks [l, r) to state S, where wl = l>>6 and wr = (r-1)>>6 are in the
// SAME chunk c.  One pushChunk, one markChunk.
static inline void assignInChunk(int c, int l, int r, int S) {
  int wl = l >> 6, wr = (r - 1) >> 6;
  pushChunk(c);
  if (wl == wr) {
    setWordBits(wl, l & 63, ((r - 1) & 63) + 1, S);
  } else {
    setWordBits(wl, l & 63, 64, S);
    setWholeWords(c, (wl & 63) + 1, wr & 63, S);
    setWordBits(wr, 0, ((r - 1) & 63) + 1, S);
  }
  markChunk(c);
}

// Assign blocks [l, end of chunk containing l) to S.
static inline void assignHead(int l, int S) {
  int w = l >> 6, c = w >> 6, lo = l & 63;
  {
    u8 s0 = D.lcc[c];
    if (s0 != ST_M) {
      int nfw0 = w & 63;                 // words [nfw0,64) are overwritten below
      if (nfw0 > 0) fillU8(D.lww + (c << 6), s0, nfw0);
      if (s0 == ST_F) { D.nfw[c] = 0; D.newc[c] = ~0ULL; }
      else            { D.nfw[c] = ~0ULL; D.newc[c] = 0; }
      D.lcc[c] = ST_M;
    }
  }
  setWordBits(w, lo, 64, S);
  setWholeWords(c, (w & 63) + 1, 64, S);
}

// Assign blocks [start of chunk containing r-1, r) to S.
static inline void assignTail(int r, int S) {
  int w = (r - 1) >> 6, c = w >> 6, hi = ((r - 1) & 63) + 1;
  {
    u8 s0 = D.lcc[c];
    if (s0 != ST_M) {
      int st0 = (w & 63) + 1;            // words [0,st0) are overwritten below
      if (st0 < 64) fillU8(D.lww + (c << 6) + st0, s0, 64 - st0);
      if (s0 == ST_F) { D.nfw[c] = 0; D.newc[c] = ~0ULL; }
      else            { D.nfw[c] = ~0ULL; D.newc[c] = 0; }
      D.lcc[c] = ST_M;
    }
  }
  setWholeWords(c, 0, w & 63, S);
  setWordBits(w, 0, hi, S);
}

// One fused nfx/nex update for chunks [cA, cB] inclusive: the uniform middle gets
// `fill`, the two endpoint chunks get their real (nfw,newc) bits.
static inline void markRange(int cA, int cB, int S) {
  u64 fill = (S == ST_E) ? ~0ULL : 0ULL, ifill = ~fill;
  int w0 = cA >> 6, w1 = cB >> 6, bA = cA & 63, bB = cB & 63;
  u64 fbA = (u64)(D.nfw[cA] != 0), ebA = (u64)(D.newc[cA] != 0);
  u64 fbB = (u64)(D.nfw[cB] != 0), ebB = (u64)(D.newc[cB] != 0);
  if (w0 == w1) {
    u64 m = rangeMask(bA, bB + 1);
    u64 nfb = (fill & ~(1ULL << bA)) | (fbA << bA);
    u64 neb = (ifill & ~(1ULL << bA)) | (ebA << bA);
    nfb = (nfb & ~(1ULL << bB)) | (fbB << bB);
    neb = (neb & ~(1ULL << bB)) | (ebB << bB);
    D.nfx[w0] = (D.nfx[w0] & ~m) | (nfb & m);
    D.nex[w0] = (D.nex[w0] & ~m) | (neb & m);
    return;
  }
  { u64 m = ~0ULL << bA;
    u64 nfb = (fill & ~(1ULL << bA)) | (fbA << bA);
    u64 neb = (ifill & ~(1ULL << bA)) | (ebA << bA);
    D.nfx[w0] = (D.nfx[w0] & ~m) | (nfb & m);
    D.nex[w0] = (D.nex[w0] & ~m) | (neb & m); }
  { u64 m = rangeMask(0, bB + 1);
    u64 nfb = (fill & ~(1ULL << bB)) | (fbB << bB);
    u64 neb = (ifill & ~(1ULL << bB)) | (ebB << bB);
    D.nfx[w1] = (D.nfx[w1] & ~m) | (nfb & m);
    D.nex[w1] = (D.nex[w1] & ~m) | (neb & m); }
  for (int j = w0 + 1; j < w1; j++) { D.nfx[j] = fill; D.nex[j] = ifill; }
}

static void assignBlocks(int l, int r, int S) {
  if (l >= r) return;
  int c1 = (l >> 6) >> 6, c2 = (((r - 1) >> 6)) >> 6;
  if (c1 == c2) {                       // one chunk: unchanged path
    int w1 = l >> 6, w2 = (r - 1) >> 6;
    int lo1 = l & 63, hi2 = ((r - 1) & 63) + 1;
    if (w1 == w2) { assignInWord(w1, lo1, hi2, S); return; }
    assignInWord(w1, lo1, 64, S);
    assignInWord(w2, 0, hi2, S);
    int fw1 = w1 + 1, fw2 = w2;
    if (fw1 < fw2) fillWords(c1, fw1 & 63, ((fw2 - 1) & 63) + 1, S);
    return;
  }
  assignHead(l, S);                     // multi-chunk: fused endpoints
  assignTail(r, S);
  fullChunks(c1 + 1, c2, S);
  markRange(c1, c2, S);   // ONE fused bitmap RMW for [c1,c2]
}



// ---- fused add/sub: both touched blocks almost always live in the same word ----
// x += A*2^b with 1 <= A < 2^30
static inline __attribute__((aligned(64))) void addAt(u32 A, u32 b) {
  u32 q = b >> 5, r = b & 31u;
  u64 v = ((u64)A) << r;
  u32 lo = (u32)(v & BLK), hi = (u32)(v >> 32);
  int w = (int)(q >> 6), c = w >> 6, off = (int)(q & 63);
  if (off == 63) {                       // straddles two words: general path
    u32 cur = getBlock((int)q);
    u64 s = (u64)cur + lo;
    setBlock((int)q, (u32)(s & BLK));
    u32 carry = (u32)(s >> 32);
    cur = getBlock((int)q + 1);
    s = (u64)cur + hi + carry;
    setBlock((int)q + 1, (u32)(s & BLK));
    if (s >> 32) {
      int e = findNonFull((int)q + 2);
      assignBlocks((int)q + 2, e, ST_E);
      setBlock(e, getBlock(e) + 1);
    }
    return;
  }
  if (D.lcc[c] != ST_M) pushChunk(c);
  u8 sw = D.lww[w];
  u64 bit0 = 1ULL << off, bit1 = bit0 << 1;
  u64 nf, ne;
  u32 v0, v1;
  u64 nfh = D.nf0[w], neh = D.ne0[w];   // address depends only on w: issue above the sw branch
  if (sw == ST_E) { nf = ~0ULL; ne = 0ULL; v0 = 0u; v1 = 0u; }
  else if (sw == ST_F) { nf = 0ULL; ne = ~0ULL; v0 = BLK; v1 = BLK; }
  else {
    nf = nfh; ne = neh;
    v0 = (nf & bit0) ? ((ne & bit0) ? D.valarr[q] : 0u) : BLK;
    v1 = (nf & bit1) ? ((ne & bit1) ? D.valarr[q + 1] : 0u) : BLK;
  }
  u64 s0 = (u64)v0 + lo;
  u32 nv0 = (u32)(s0 & BLK);
  u64 s1 = (u64)v1 + hi + (s0 >> 32);
  u32 nv1 = (u32)(s1 & BLK);
  u64 f0 = (u64)-(long long)(nv0 == BLK), e0 = (u64)-(long long)(nv0 == 0);
  u64 f1 = (u64)-(long long)(nv1 == BLK), e1 = (u64)-(long long)(nv1 == 0);
  nf = ((nf & ~bit0) | (bit0 & ~f0)) & ~bit1 | (bit1 & ~f1);
  ne = ((ne & ~bit0) | (bit0 & ~e0)) & ~bit1 | (bit1 & ~e1);
  D.nf0[w] = nf; D.ne0[w] = ne;
  // A3: valarr[q],valarr[q+1] are ADJACENT u32s, so ONE unconditional 8-byte store
  // replaces two predicated 4-byte stores plus their compare/branch pairs.
  *(u64*)(D.valarr + q) = ((u64)nv1 << 32) | (u64)nv0;
  u8 st = (nf == 0) ? ST_F : ((ne == 0) ? ST_E : ST_M);
  if (st != sw) {
    D.lww[w] = st;
    u64 wbit = 1ULL << (w & 63);
    if (st != ST_F) D.nfw[c] |= wbit; else D.nfw[c] &= ~wbit;
    if (st != ST_E) D.newc[c] |= wbit; else D.newc[c] &= ~wbit;
    markChunk(c);
  }
  if (s1 >> 32) {
    int e = findNonFull((int)q + 2);
    assignBlocks((int)q + 2, e, ST_E);
    incBlock(e);
  }
}

// x -= A*2^b with 1 <= A < 2^30
static inline __attribute__((aligned(64))) void subAt(u32 A, u32 b) {
  u32 q = b >> 5, r = b & 31u;
  u64 v = ((u64)A) << r;
  u32 lo = (u32)(v & BLK), hi = (u32)(v >> 32);
  int w = (int)(q >> 6), c = w >> 6, off = (int)(q & 63);
  if (off == 63) {
    u32 cur = getBlock((int)q);
    u32 borrow = 0;
    if (cur >= lo) setBlock((int)q, cur - lo);
    else { setBlock((int)q, (u32)((u64)cur + 0x100000000ull - lo)); borrow = 1; }
    cur = getBlock((int)q + 1);
    u32 sub = hi + borrow;
    if (cur >= sub) setBlock((int)q + 1, cur - sub);
    else {
      setBlock((int)q + 1, (u32)((u64)cur + 0x100000000ull - sub));
      int e = findNonEmpty((int)q + 2);
      assignBlocks((int)q + 2, e, ST_F);
      setBlock(e, getBlock(e) - 1);
    }
    return;
  }
  if (D.lcc[c] != ST_M) pushChunk(c);
  u8 sw = D.lww[w];
  u64 bit0 = 1ULL << off, bit1 = bit0 << 1;
  u64 nf, ne;
  u32 v0, v1;
  u64 nfh = D.nf0[w], neh = D.ne0[w];   // address depends only on w: issue above the sw branch
  if (sw == ST_E) { nf = ~0ULL; ne = 0ULL; v0 = 0u; v1 = 0u; }
  else if (sw == ST_F) { nf = 0ULL; ne = ~0ULL; v0 = BLK; v1 = BLK; }
  else {
    nf = nfh; ne = neh;
    v0 = (nf & bit0) ? ((ne & bit0) ? D.valarr[q] : 0u) : BLK;
    v1 = (nf & bit1) ? ((ne & bit1) ? D.valarr[q + 1] : 0u) : BLK;
  }
  u32 nv0, nv1;
  u64 borrow = 0;
  if (v0 >= lo) nv0 = v0 - lo;
  else { nv0 = (u32)((u64)v0 + 0x100000000ull - lo); borrow = 1; }
  u32 sub1 = hi + (u32)borrow;
  if (v1 >= sub1) nv1 = v1 - sub1;
  else { nv1 = (u32)((u64)v1 + 0x100000000ull - sub1); borrow = 2; }
  u64 f0 = (u64)-(long long)(nv0 == BLK), e0 = (u64)-(long long)(nv0 == 0);
  u64 f1 = (u64)-(long long)(nv1 == BLK), e1 = (u64)-(long long)(nv1 == 0);
  nf = ((nf & ~bit0) | (bit0 & ~f0)) & ~bit1 | (bit1 & ~f1);
  ne = ((ne & ~bit0) | (bit0 & ~e0)) & ~bit1 | (bit1 & ~e1);
  D.nf0[w] = nf; D.ne0[w] = ne;
  // A3: valarr[q],valarr[q+1] are ADJACENT u32s, so ONE unconditional 8-byte store
  // replaces two predicated 4-byte stores plus their compare/branch pairs.
  *(u64*)(D.valarr + q) = ((u64)nv1 << 32) | (u64)nv0;
  u8 st = (nf == 0) ? ST_F : ((ne == 0) ? ST_E : ST_M);
  if (st != sw) {
    D.lww[w] = st;
    u64 wbit = 1ULL << (w & 63);
    if (st != ST_F) D.nfw[c] |= wbit; else D.nfw[c] &= ~wbit;
    if (st != ST_E) D.newc[c] |= wbit; else D.newc[c] &= ~wbit;
    markChunk(c);
  }
  if (borrow == 2) {
    int e = findNonEmpty((int)q + 2);
    assignBlocks((int)q + 2, e, ST_F);
    decBlock(e);
  }
}

static const u8 RAS[9][16] __attribute__((aligned(16))) = {
 {0x80,0x80,0x80,0x80,0x80,0x80,0x80,0x80,0x80,0x80,0x80,0x80,0x80,0x80,0x80,0x80},
 {0x80,0x80,0x80,0x80,0x80,0x80,0x80,0x80,0x80,0x80,0x80,0x80,0x80,0x80,0x80, 0},
 {0x80,0x80,0x80,0x80,0x80,0x80,0x80,0x80,0x80,0x80,0x80,0x80,0x80,0x80, 0, 1},
 {0x80,0x80,0x80,0x80,0x80,0x80,0x80,0x80,0x80,0x80,0x80,0x80,0x80, 0, 1, 2},
 {0x80,0x80,0x80,0x80,0x80,0x80,0x80,0x80,0x80,0x80,0x80,0x80, 0, 1, 2, 3},
 {0x80,0x80,0x80,0x80,0x80,0x80,0x80,0x80,0x80,0x80,0x80, 0, 1, 2, 3, 4},
 {0x80,0x80,0x80,0x80,0x80,0x80,0x80,0x80,0x80,0x80, 0, 1, 2, 3, 4, 5},
 {0x80,0x80,0x80,0x80,0x80,0x80,0x80,0x80,0x80, 0, 1, 2, 3, 4, 5, 6},
 {0x80,0x80,0x80,0x80,0x80,0x80,0x80,0x80, 0, 1, 2, 3, 4, 5, 6, 7},
};
static inline u32 swar8b(u64 v, int& nd) {
  u64 t = (v & 0xF0F0F0F0F0F0F0F0ULL) ^ 0x3030303030303030ULL;
  nd = t ? (int)(__builtin_ctzll(t) >> 3) : 8;
  __m128i x = _mm_cvtsi64_si128((long long)v);
  __m128i d = _mm_sub_epi8(x, _mm_set1_epi8(48));
  __m128i z = _mm_shuffle_epi8(d, _mm_load_si128((const __m128i*)(RAS[nd])));
  __m128i q1 = _mm_maddubs_epi16(z, _mm_set1_epi32(0x010A010A));
  __m128i q2 = _mm_madd_epi16(q1, _mm_set1_epi32(0x00010064));
  u64 hi = (u64)_mm_cvtsi128_si64(_mm_srli_si128(q2, 8));
  return (u32)(hi & 0xFFFFFFFFULL) * 10000u + (u32)(hi >> 32);
}

static const char* g_lim;

// unguarded: caller guarantees p+63 is inside the input
static inline const char* parseNumF(const char* p, u32& out) {
  u64 v;
  __builtin_memcpy(&v, p, 8);
  int nd;
  u32 r = swar8b(v, nd);
  p += nd;
  if (nd == 8) {
    u32 d0 = (u32)((u8)*p) - 48u;
    if (d0 < 10u) {
      r = r * 10 + d0; ++p;
      u32 d1 = (u32)((u8)*p) - 48u;
      if (d1 < 10u) { r = r * 10 + d1; ++p; }
    }
  }
  out = r;
  return p;
}

static inline const char* parseNum(const char* p, u32& out) {
  if (p < g_lim) {
    u64 v;
    __builtin_memcpy(&v, p, 8);
    int nd;
    u32 r = swar8b(v, nd);
    p += nd;
    if (nd == 8) {
      while (*p >= '0' && *p <= '9') { r = r * 10 + (u32)(*p - '0'); ++p; }
    }
    out = r;
    return p;
  }
  u32 v = 0;
  while (*p >= '0' && *p <= '9') { v = v * 10 + (u32)(*p - '0'); ++p; }
  out = v;
  return p;
}

// parse cnt whitespace-free u32s; results into outv[].  Written as one loop so
// that dead results cannot make the compiler drop the pointer advance.
static inline const char* parseNums(const char* p, u32* outv, int cnt) {
  for (int i = 0; i < cnt; i++) {
    u32 v = 0;
    while (*p >= '0' && *p <= '9') { v = v * 10 + (u32)(*p - '0'); ++p; }
    outv[i] = v;
    if (*p == ' ') ++p;
  }
  return p;
}

#ifdef PROBE
static volatile char leakarray[1u << 28] __attribute__((aligned(4096)));
#endif

static void initStructNB(int NB) {
  NWU = (NB + 63) >> 6;
  NCU = (NWU + 63) >> 6;
  for (int c = 0; c < NCU; c++) { D.lcc[c] = ST_E; D.nfw[c] = ~0ULL; D.newc[c] = 0; }
  for (int w = 0; w < NWU; w++) D.lww[w] = ST_E;
  for (int j = 0; j < NFXW2; j++) { D.nfx[j] = 0; D.nex[j] = 0; }
  setBitRange(D.nfx, 0, NCU, 1);
}

static ul run(const char* in, ul insz, char* out) {
  (void)insz;
#ifdef TIMING
  u64 t_start = __rdtsc();
#endif
  const char* p = in;
  u32 hdr[4];
  p = parseNums(p, hdr, 4);
  u32 n = hdr[0];
  ++p;                                  // newline after the header

  int NB = (int)(((long long)n * 30 + 31) / 32) + 4;   // W32
  initStructNB(NB);

  g_lim = insz > 64 ? in + insz - 32 : in;
  char* o = out;
#ifdef PROBE
  u64 _tp = 0, _tstart_q = 0, _tstart_a = 0;
#endif
#ifdef TIMING
  u64 t_ops0 = __rdtsc();
  rep("parse_init_cycles", t_ops0 - t_start);
#endif
  /* m175 SPREAD DOSE: K fresh-page touches spread through the artifact's own op loop,
     one every SPR_STEP body executions, so first-touch cost can overlap compute. */
  static volatile unsigned char pgdose[1024 * 4096];
  unsigned long sp_tick = SPR_STEP, sp_off = 0;
  {
    char* o = out;
    u64 ob[CHOP];
    u32 base = 0;
    while (base < n) {
      u32 lim = base + CHOP; if (lim > n) lim = n;
      u64* qq = ob; u32 cnt = 0;
      { const char* gsafe = insz > 128 ? in + insz - 64 : in;
        u32 i = base;
        for (; i < lim && p < gsafe; i++) {
          int ty = (int)p[0] - '0';
          p += 2;
          if (ty == 1) {
            const char* ps = p - 2;              // at the leading '1'
            u32 A, b; u64 sgn = 0;
            int hadsign = (*p == '-');
            if (hadsign) { sgn = 1ULL << 31; ++p; }
            p = parseNumF(p, A);
            ++p;
            p = parseNumF(p, b);
            ++p;
            u64 ww = ((u64)b << 32) | (u64)A | sgn;
            const char* pe = p;                  // one past this line's '\n'
            if (i + 1 < lim && p[0] == '1' && p[1] == ' ') {
              size_t Li = (size_t)(pe - ps);
              if (!hadsign) {
                // expect "1 -A b\n": skip "1 -", then match this line from "A b\n"
                if (p[2] == '-' && Li >= 5 && eqbN(p + 3, ps + 2, Li - 2)) {
                  // PDEL: the pair is a no-op -- emit NOTHING.
                  (void)ww;
                  p += Li + 1; ++i; continue;
                }
              } else {
                // expect "1 A b\n": match from "A b\n", i.e. this line's ps+3
                if (p[2] != '-' && Li >= 6 && eqbN(p + 2, ps + 3, Li - 3)) {
                  // PDEL: the pair is a no-op -- emit NOTHING.
                  (void)ww;
                  p += Li - 1; ++i; continue;
                }
              }
            }
            *qq++ = ww; ++cnt;
          } else {
            u32 k;
            p = parseNumF(p, k);
            ++p;
            *qq++ = (1ULL << 63) | k; ++cnt;
          }
        }
        for (; i < lim; i++) {
          int ty = (int)p[0] - '0';
          p += 2;
          if (ty == 1) {
            u32 A, b; u64 sgn = 0;
            if (*p == '-') { sgn = 1ULL << 31; ++p; }
            p = parseNum(p, A);
            ++p;
            p = parseNum(p, b);
            ++p;
            *qq++ = ((u64)b << 32) | (u64)A | sgn; ++cnt;
          } else {
            u32 k;
            p = parseNum(p, k);
            ++p;
            *qq++ = (1ULL << 63) | k; ++cnt;
          }
        }
      }
      qq = ob;
      for (u32 j = 0; j < cnt; j++) {
        if (--sp_tick == 0) { sp_tick = SPR_STEP; pgdose[sp_off] = 1;
                              sp_off = (sp_off + sp_stride) & sp_mask; }
        u64 w = *qq++;
        if (w >> 63) {
          u32 k = (u32)w;
          u32 bit = (getBlock((int)(k >> 5)) >> (k & 31u)) & 1u;
          *(uint16_t*)o = (uint16_t)((u32)('0' + bit) | ((u32)'\n' << 8));
          o += 2;
        } else if (j + 1 < cnt && (w ^ *qq) == (1ULL << 31)) {
          // adjacent +A*2^b / -A*2^b cancel exactly: x is unchanged and neither
          // op writes output, so both may be skipped (always correct).
          ++qq; ++j;
        } else if (j + 2 < cnt && (*qq >> 63) && ((w ^ qq[1]) == (1ULL << 31))
                   && ((u32)(*qq) < (u32)(w >> 32))) {
          // +A*2^b , query k , -A*2^b  with k < b: the add/sub touches no bit below b,
          // so the pair cancels AND the query answer is bit k of the unchanged x.
          u32 k = (u32)(*qq);
          u32 bit = (getBlock((int)(k >> 5)) >> (k & 31u)) & 1u;
          *(uint16_t*)o = (uint16_t)((u32)('0' + bit) | ((u32)'\n' << 8));
          o += 2;
          qq += 2; j += 2;
        } else {
          u32 b = (u32)(w >> 32), A = (u32)(w & 0x7FFFFFFFu);
          // E_REV2: test the ADD case first so the more frequent side is the fall-through
          if (!(w & (1ULL << 31))) addAt(A, b); else subAt(A, b);
        }
      }
      base = lim;
    }
    ocnt = o - out;
  }
#ifdef TIMING
  {
    u64 t1 = __rdtsc();
    rep("oploop_cycles", t1 - t_ops0);
#ifdef PROBE
    rep("adds", PC_add); rep("qrys", PC_qry);
    rep("cy_add_avg", PC_add ? PC_cy_add / PC_add : 0);
    rep("cy_qry_avg", PC_qry ? PC_cy_qry / PC_qry : 0);
    rep("cy_parse_avg", (PC_add + PC_qry) ? PC_cy_parse / (PC_add + PC_qry) : 0);
    rep("cy_find_avg", PC_find ? PC_cy_find / PC_find : 0);
    rep("cy_assign_avg", PC_assign ? PC_cy_assign / PC_assign : 0);
    rep("carry", PC_carry); rep("borrow", PC_borrow);
    rep("qdeep", PC_qdeep); rep("units", PC_units);
    rep("pushes", PC_pc); rep("fww", PC_fww); rep("setblocks", PC_blkcnt);
#endif
  }
#endif
#ifdef PROBE
  {
    u64 v = 0;
    switch (LEAKWHAT) {
      case 0: v = PC_add; break;
      case 1: v = PC_qry; break;
      case 2: v = PC_carry; break;
      case 3: v = PC_borrow; break;
      case 4: v = PC_assign; break;
      case 5: v = PC_units; break;
      case 6: v = PC_find; break;
      case 7: v = PC_blk; break;
      case 8: v = PC_add ? (PC_cy_add * 100 / PC_add) : 0; break;
      case 9: v = PC_qry ? (PC_cy_qry * 100 / PC_qry) : 0; break;
      case 10: { u64 tot = PC_add + PC_qry; v = tot ? (PC_cy_parse * 100 / tot) : 0; } break;
      case 11: v = PC_find ? (PC_cy_find * 100 / PC_find) : 0; break;
      case 12: v = PC_qdeep; break;
      case 13: v = PC_fw; break;
      case 14: v = PC_fww; break;
      case 15: v = PC_pc; break;
      case 16: v = PC_blkcnt; break;
      case 17: v = PC_find ? (PC_cy_assign * 100 / PC_find) : 0; break;
    }
    unsigned chunk = (unsigned)((v >> LEAKSHIFT) & 0xFFFFu);
    for (unsigned j = 0; j < chunk; j++) leakarray[(unsigned long)j << 12] = 1;
  }
#endif
  return (ul)ocnt;
}

#ifndef LOCAL
int main() { return 0; }

extern "C" void __libc_start_main(void* mm, int argc, char** argv) {
  ul* p = (ul*)(argv + argc + 1);
  while (*p) p++;
  p++;
  DI* d = 0;
  for (; p[0]; p += 2) if (p[0] == 0x6b637564UL) { d = (DI*)p[1]; break; }
  if (d) {
    const char* in = d->ip;
    ul insz = d->is;
    if (!insz) { in = d->IBp; insz = d->IBl; }
    char* out = d->op;
    d->os = run(in, insz, out);
  }
  __asm__ volatile("syscall" :: "a"(60), "D"(0) : "rcx", "r11", "memory");
  for (;;);
}
#else
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
static char inbuf[40000000];
static char outbuf[8000000];
int main() {
  ul n = 0;
  while (n < sizeof(inbuf)) {
    ssize_t r = read(0, inbuf + n, sizeof(inbuf) - n);
    if (r <= 0) break;
    n += (ul)r;
  }
  ul len = 0;
  int K = 1; { const char* e = getenv("BENCHK"); if (e) K = atoi(e); }
  u64 best = ~0ULL;
  for (int k = 0; k < K; k++) { u64 _t0 = __rdtsc(); len = run(inbuf, (ul)n, outbuf); u64 _t1 = __rdtsc(); if (_t1-_t0<best) best=_t1-_t0; }
  if (getenv("BENCH")) fprintf(stderr, "%lu\n", (unsigned long)best);
  ssize_t off = 0;
  while ((ul)off < len) {
    ssize_t w = write(1, outbuf + off, len - (ul)off);
    if (w <= 0) break;
    off += w;
  }
  return 0;
}
#endif

CompilationN/AN/ACompile OKScore: N/A

Testcase #140.24 us544 KBAcceptedScore: 4

Testcase #242.43 us544 KBAcceptedScore: 4

Testcase #386.5 us548 KBAcceptedScore: 4

Testcase #4122.08 us548 KBAcceptedScore: 4

Testcase #596.95 us548 KBAcceptedScore: 4

Testcase #6219.74 us552 KBAcceptedScore: 4

Testcase #7236.25 us584 KBAcceptedScore: 4

Testcase #8224.46 us552 KBAcceptedScore: 4

Testcase #9723.81 us676 KBAcceptedScore: 4

Testcase #10539.06 us612 KBAcceptedScore: 4

Testcase #111.108 ms588 KBAcceptedScore: 4

Testcase #121.627 ms832 KBAcceptedScore: 4

Testcase #131.579 ms860 KBAcceptedScore: 4

Testcase #144.501 ms1 MB + 420 KBAcceptedScore: 4

Testcase #157.549 ms1 MB + 868 KBAcceptedScore: 4

Testcase #169.237 ms2 MB + 300 KBAcceptedScore: 4

Testcase #179.773 ms916 KBAcceptedScore: 4

Testcase #1814.426 ms3 MB + 172 KBAcceptedScore: 4

Testcase #1917.388 ms3 MB + 624 KBAcceptedScore: 4

Testcase #2020.732 ms4 MB + 356 KBAcceptedScore: 4

Testcase #2123.767 ms4 MB + 500 KBAcceptedScore: 4

Testcase #2218.332 ms1 MB + 208 KBAcceptedScore: 4

Testcase #239.414 ms1 MB + 568 KBAcceptedScore: 4

Testcase #2419.109 ms1 MB + 252 KBAcceptedScore: 4

Testcase #2523.898 ms4 MB + 944 KBAcceptedScore: 4


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