// two-pass prototype: scalar pass-1 computes per-line field lengths into aux[];
// pass-2 re-walks the input with the KNOWN lengths (no hasless+ctz in the parse).
#include <stdint.h>
#pragma GCC target("bmi")
#include <sys/auxv.h>
#include <unistd.h>
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 const char *P;
static const char *Pend;
static uint64_t O;
static int has_lazy;
static unsigned aux[1000001];
static inline int ctz(uint64_t x){ return __builtin_ctzll(x); }
/* ---- point add / sub (single-block, materialize + bubble) ---- */
static inline int point_add(int pos, u128 v){
int l2 = pos >> 12;
int l1 = pos >> 6;
int ovf;
uint64_t old_fm1, old_nm1;
if (has_lazy){
uint64_t b = 1ull << l2;
if (fm3 & b){ fm2[l2] = ~0ull; nm2[l2] = ~0ull; }
else if (!(nm3 & b)){ fm2[l2] = 0; nm2[l2] = 0; }
b = 1ull << (l1 & 63);
if (fm2[l2] & b){ fm1[l1] = ~0ull; nm1[l1] = ~0ull; }
else if (!(nm2[l2] & b)){ fm1[l1] = 0; nm1[l1] = 0; }
}
old_fm1 = fm1[l1]; old_nm1 = nm1[l1];
{
uint64_t b = 1ull << (pos & 63);
u128 old;
if (old_fm1 & b) old = FULL;
else if (!(old_nm1 & b)) old = 0;
else old = blk[pos];
u128 nv = old + v;
blk[pos] = nv;
ovf = nv < old;
if (nv == FULL) fm1[l1] |= b; else fm1[l1] &= ~b;
if (nv != 0) nm1[l1] |= b; else nm1[l1] &= ~b;
}
if ((old_fm1 == ~0ull) != (fm1[l1] == ~0ull) || (old_nm1 == 0) != (nm1[l1] == 0)){
uint64_t b = 1ull << (l1 & 63);
if (fm1[l1] == ~0ull) fm2[l2] |= b; else fm2[l2] &= ~b;
if (nm1[l1] != 0) nm2[l2] |= b; else nm2[l2] &= ~b;
b = 1ull << l2;
if (fm2[l2] == ~0ull) fm3 |= b; else fm3 &= ~b;
if (nm2[l2] != 0) nm3 |= b; else nm3 &= ~b;
}
return ovf;
}
static inline int point_sub(int pos, u128 v){
int l2 = pos >> 12;
int l1 = pos >> 6;
int ovf;
uint64_t old_fm1, old_nm1;
if (has_lazy){
uint64_t b = 1ull << l2;
if (fm3 & b){ fm2[l2] = ~0ull; nm2[l2] = ~0ull; }
else if (!(nm3 & b)){ fm2[l2] = 0; nm2[l2] = 0; }
b = 1ull << (l1 & 63);
if (fm2[l2] & b){ fm1[l1] = ~0ull; nm1[l1] = ~0ull; }
else if (!(nm2[l2] & b)){ fm1[l1] = 0; nm1[l1] = 0; }
}
old_fm1 = fm1[l1]; old_nm1 = nm1[l1];
{
uint64_t b = 1ull << (pos & 63);
u128 old;
if (old_fm1 & b) old = FULL;
else if (!(old_nm1 & b)) old = 0;
else old = blk[pos];
u128 nv = old - v;
blk[pos] = nv;
ovf = nv > old;
if (nv == FULL) fm1[l1] |= b; else fm1[l1] &= ~b;
if (nv != 0) nm1[l1] |= b; else nm1[l1] &= ~b;
}
if ((old_fm1 == ~0ull) != (fm1[l1] == ~0ull) || (old_nm1 == 0) != (nm1[l1] == 0)){
uint64_t b = 1ull << (l1 & 63);
if (fm1[l1] == ~0ull) fm2[l2] |= b; else fm2[l2] &= ~b;
if (nm1[l1] != 0) nm2[l2] |= b; else nm2[l2] &= ~b;
b = 1ull << l2;
if (fm2[l2] == ~0ull) fm3 |= b; else fm3 &= ~b;
if (nm2[l2] != 0) nm3 |= b; else nm3 &= ~b;
}
return ovf;
}
/* ---- left-boundary suffix helpers (operate on a FULL/zero node) ---- */
static inline void zero_full_l2(int l2, int off2){
int pos = (l2 << 12) + off2;
int l1 = pos >> 6, off = pos & 63;
int l1in = l1 & 63;
fm1[l1] = (off == 0) ? 0 : ((1ull << off) - 1);
nm1[l1] = fm1[l1];
fm2[l2] = (1ull << l1in) - 1;
nm2[l2] = (off == 0) ? fm2[l2] : (fm2[l2] | (1ull << l1in));
uint64_t b3 = 1ull << l2;
fm3 &= ~b3;
nm3 |= b3;
}
static inline void zero_full_l1(int l1, int off){
fm1[l1] = (1ull << off) - 1;
nm1[l1] = (1ull << off) - 1;
int l2 = l1 >> 6;
uint64_t b2 = 1ull << (l1 & 63);
fm2[l2] &= ~b2;
nm2[l2] |= b2;
}
static inline void set_full_l2(int l2, int off2){
int pos = (l2 << 12) + off2;
int l1 = pos >> 6, off = pos & 63;
int l1in = l1 & 63;
fm1[l1] = ~0ull << off;
nm1[l1] = fm1[l1];
fm2[l2] = ~0ull << l1in;
nm2[l2] = fm2[l2];
uint64_t b3 = 1ull << l2;
if (fm2[l2] == ~0ull) fm3 |= b3; else fm3 &= ~b3;
nm3 |= b3;
}
static inline void set_full_l1(int l1, int off){
fm1[l1] = ~0ull << off;
nm1[l1] = ~0ull << off;
int l2 = l1 >> 6;
uint64_t b2 = 1ull << (l1 & 63);
if (fm1[l1] == ~0ull) fm2[l2] |= b2; else fm2[l2] &= ~b2;
nm2[l2] |= b2;
}
static inline void carry_add(int p){
has_lazy = 1;
int l2 = p >> 12, l1 = p >> 6, off = p & 63;
int off2 = p & 4095;
uint64_t m3 = ~fm3 >> l2;
int l2pos = (off2 == 0) ? l2 : l2 + 1;
if (off2 > 0 && (fm3 & (1ull << l2))){
zero_full_l2(l2, off2);
}
while (m3){
int l2n = l2 + ctz(m3);
uint64_t b3 = 1ull << l2n;
if (l2n > l2pos){
uint64_t zm = ((1ull << (l2n - l2pos)) - 1) << l2pos;
fm3 &= ~zm; nm3 &= ~zm;
}
l2pos = l2n + 1;
if (!(nm3 & b3)){
if (l2n > l2){
int l1n = l2n << 6;
blk[l2n << 12] = 1;
fm1[l1n] = 0; nm1[l1n] = 1ull;
fm2[l2n] = 0; nm2[l2n] = 1ull;
fm3 &= ~b3; nm3 |= b3;
return;
} else {
uint64_t b1 = 1ull << off;
blk[p] = 1;
fm1[l1] = 0; nm1[l1] = b1;
uint64_t b2 = 1ull << (l1 & 63);
fm2[l2] = 0; nm2[l2] = b2;
fm3 &= ~b3; nm3 |= b3;
return;
}
}
int c2 = (l2n == l2) ? (l1 & 63) : 0;
uint64_t m2 = ~fm2[l2n] >> c2;
int c2pos = c2;
if (l2n == l2 && off > 0 && (fm2[l2n] & (1ull << c2))){
zero_full_l1(l1, off);
c2pos = c2 + 1;
}
while (m2){
int c2r = c2 + ctz(m2);
uint64_t b2 = 1ull << c2r;
int l1n = (l2n << 6) + c2r;
if (c2r > c2pos){
uint64_t zm = ((1ull << (c2r - c2pos)) - 1) << c2pos;
fm2[l2n] &= ~zm; nm2[l2n] &= ~zm;
}
c2pos = c2r + 1;
if (!(nm2[l2n] & b2)){
if (l1n > l1){
blk[l1n << 6] = 1;
fm1[l1n] = 0; nm1[l1n] = 1ull;
fm2[l2n] &= ~b2; nm2[l2n] |= b2;
} else {
uint64_t b1 = 1ull << off;
blk[p] = 1;
fm1[l1] = 0; nm1[l1] = b1;
fm2[l2n] &= ~b2; nm2[l2n] |= b2;
}
if (fm2[l2n] == ~0ull) fm3 |= b3; else fm3 &= ~b3;
if (nm2[l2n] != 0) nm3 |= b3; else nm3 &= ~b3;
return;
}
int c1 = (l1n == l1) ? off : 0;
uint64_t m1 = ~fm1[l1n] >> c1;
if (m1){
int c1r = c1 + ctz(m1);
int j = (l1n << 6) + c1r;
uint64_t b1 = 1ull << c1r;
if (c1r > c1){
uint64_t zm = ((1ull << (c1r - c1)) - 1) << c1;
fm1[l1n] &= ~zm; nm1[l1n] &= ~zm;
}
if (nm1[l1n] & b1){
u128 nv = blk[j] + 1;
blk[j] = nv;
if (nv == FULL) fm1[l1n] |= b1; else fm1[l1n] &= ~b1;
nm1[l1n] |= b1;
} else {
blk[j] = 1;
fm1[l1n] &= ~b1; nm1[l1n] |= b1;
}
if (fm1[l1n] == ~0ull) fm2[l2n] |= b2; else fm2[l2n] &= ~b2;
if (nm1[l1n] != 0) nm2[l2n] |= b2; else nm2[l2n] &= ~b2;
if (fm2[l2n] == ~0ull) fm3 |= b3; else fm3 &= ~b3;
if (nm2[l2n] != 0) nm3 |= b3; else nm3 &= ~b3;
return;
}
{
uint64_t zm = ~0ull << c1;
fm1[l1n] &= ~zm; nm1[l1n] &= ~zm;
if (fm1[l1n] == ~0ull) fm2[l2n] |= b2; else fm2[l2n] &= ~b2;
if (nm1[l1n] != 0) nm2[l2n] |= b2; else nm2[l2n] &= ~b2;
}
m2 &= m2 - 1;
}
if (c2pos < 64){
uint64_t zm = ~0ull << c2pos;
fm2[l2n] &= ~zm; nm2[l2n] &= ~zm;
}
if (fm2[l2n] == ~0ull) fm3 |= b3; else fm3 &= ~b3;
if (nm2[l2n] != 0) nm3 |= b3; else nm3 &= ~b3;
m3 &= m3 - 1;
}
}
static inline void borrow_sub(int p){
has_lazy = 1;
int l2 = p >> 12, l1 = p >> 6, off = p & 63;
int off2 = p & 4095;
uint64_t m3 = nm3 >> l2;
int l2pos = (off2 == 0) ? l2 : l2 + 1;
if (off2 > 0 && !(nm3 & (1ull << l2))){
set_full_l2(l2, off2);
}
while (m3){
int l2n = l2 + ctz(m3);
uint64_t b3 = 1ull << l2n;
if (l2n > l2pos){
uint64_t zm = ((1ull << (l2n - l2pos)) - 1) << l2pos;
fm3 |= zm; nm3 |= zm;
}
l2pos = l2n + 1;
if (fm3 & b3){
if (l2n > l2){
int l1n = l2n << 6;
blk[l2n << 12] = FULL - 1;
fm1[l1n] = ~0ull ^ 1ull; nm1[l1n] = ~0ull;
fm2[l2n] = ~0ull ^ 1ull; nm2[l2n] = ~0ull;
fm3 &= ~b3; nm3 |= b3;
return;
} else {
uint64_t b1 = 1ull << off;
blk[p] = FULL - 1;
fm1[l1] = ~0ull ^ b1; nm1[l1] = ~0ull;
uint64_t b2 = 1ull << (l1 & 63);
fm2[l2] = ~0ull ^ b2; nm2[l2] = ~0ull;
fm3 &= ~b3; nm3 |= b3;
return;
}
}
int c2 = (l2n == l2) ? (l1 & 63) : 0;
uint64_t m2 = nm2[l2n] >> c2;
int c2pos = c2;
if (l2n == l2 && off > 0 && !(nm2[l2n] & (1ull << c2))){
set_full_l1(l1, off);
c2pos = c2 + 1;
}
while (m2){
int c2r = c2 + ctz(m2);
uint64_t b2 = 1ull << c2r;
int l1n = (l2n << 6) + c2r;
if (c2r > c2pos){
uint64_t zm = ((1ull << (c2r - c2pos)) - 1) << c2pos;
fm2[l2n] |= zm; nm2[l2n] |= zm;
}
c2pos = c2r + 1;
if (fm2[l2n] & b2){
if (l1n > l1){
blk[l1n << 6] = FULL - 1;
fm1[l1n] = ~0ull ^ 1ull; nm1[l1n] = ~0ull;
fm2[l2n] &= ~b2; nm2[l2n] |= b2;
} else {
uint64_t b1 = 1ull << off;
blk[p] = FULL - 1;
fm1[l1] = ~0ull ^ b1; nm1[l1] = ~0ull;
fm2[l2n] &= ~b2; nm2[l2n] |= b2;
}
if (fm2[l2n] == ~0ull) fm3 |= b3; else fm3 &= ~b3;
if (nm2[l2n] != 0) nm3 |= b3; else nm3 &= ~b3;
return;
}
int c1 = (l1n == l1) ? off : 0;
uint64_t m1 = nm1[l1n] >> c1;
if (m1){
int c1r = c1 + ctz(m1);
int j = (l1n << 6) + c1r;
uint64_t b1 = 1ull << c1r;
if (c1r > c1){
uint64_t zm = ((1ull << (c1r - c1)) - 1) << c1;
fm1[l1n] |= zm; nm1[l1n] |= zm;
}
if (fm1[l1n] & b1){
blk[j] = FULL - 1;
fm1[l1n] &= ~b1; nm1[l1n] |= b1;
} else {
u128 nv = blk[j] - 1;
blk[j] = nv;
if (nv == 0) nm1[l1n] &= ~b1; else nm1[l1n] |= b1;
}
if (fm1[l1n] == ~0ull) fm2[l2n] |= b2; else fm2[l2n] &= ~b2;
if (nm1[l1n] != 0) nm2[l2n] |= b2; else nm2[l2n] &= ~b2;
if (fm2[l2n] == ~0ull) fm3 |= b3; else fm3 &= ~b3;
if (nm2[l2n] != 0) nm3 |= b3; else nm3 &= ~b3;
return;
}
{
uint64_t zm = ~0ull << c1;
fm1[l1n] |= zm; nm1[l1n] |= zm;
if (fm1[l1n] == ~0ull) fm2[l2n] |= b2; else fm2[l2n] &= ~b2;
if (nm1[l1n] != 0) nm2[l2n] |= b2; else nm2[l2n] &= ~b2;
}
m2 &= m2 - 1;
}
if (c2pos < 64){
uint64_t zm = ~0ull << c2pos;
fm2[l2n] |= zm; nm2[l2n] |= zm;
}
if (fm2[l2n] == ~0ull) fm3 |= b3; else fm3 &= ~b3;
if (nm2[l2n] != 0) nm3 |= b3; else nm3 &= ~b3;
m3 &= m3 - 1;
}
}
static inline void add_val(int q, int r, uint64_t av){
u128 lo = (u128)av << r;
uint64_t hi = 0;
if (r >= 99) hi = av >> (128 - r);
if (point_add(q, lo)) carry_add(q + 1);
if (hi && point_add(q + 1, (u128)hi)) carry_add(q + 2);
}
static inline void sub_val(int q, int r, uint64_t av){
u128 lo = (u128)av << r;
uint64_t hi = 0;
if (r >= 99) hi = av >> (128 - r);
if (point_sub(q, lo)) borrow_sub(q + 1);
if (hi && point_sub(q + 1, (u128)hi)) borrow_sub(q + 2);
}
static inline int rd(const char **pp){
const char *p = *pp;
while (*p <= ' ') p++;
int v = 0;
while (*p > ' ') v = v*10 + (*p - '0'), p++;
*pp = p;
return v;
}
/* len-known conversions (no hasless/ctz) */
static inline unsigned conv8(unsigned long long x, int len){
unsigned long long d = __builtin_bswap64(x - 0x3030303030303030ULL);
d >>= 64 - (len << 3);
unsigned long long t = (d * 0x010AULL) & 0xFF00FF00FF00FF00ULL;
t >>= 8;
unsigned long long t2 = t * 0x00010064ULL;
return (unsigned)(((t2 >> 16) & 0xFFFFULL) + 10000ULL * ((t2 >> 48) & 0xFFFFULL));
}
__attribute__((noreturn)) static void done(uint64_t olen, struct DuckInfo *di, char *out, 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);
static char lbuf[1<<24];
static char obuf[1<<22];
char *out;
int use_di = 0;
const char *P0;
if (di && di->abi_version >= 1 && di->stdin_ptr && di->stdout_ptr){
P0 = di->stdin_ptr;
Pend = di->stdin_ptr + di->stdin_size;
out = di->stdout_ptr;
use_di = 1;
} else {
long n2 = 0, t;
while (n2 < (long)sizeof(lbuf) && (t = read(0, lbuf + n2, sizeof(lbuf) - n2)) > 0) n2 += t;
lbuf[n2] = 0;
P0 = lbuf;
Pend = lbuf + n2;
out = obuf;
}
P = P0;
int n = rd(&P);
rd(&P); rd(&P); rd(&P);
P++;
/* ---- pass 1: compute per-line field lengths ---- */
{
const char *q = P;
unsigned *ap = aux;
for (int i = 0; i < n; i++){
int op = (*q == '1');
q += 2;
int l1 = 0, l2 = 0;
if (op){
const char *t = q;
while (*t != ' ') { t++; }
l1 = (int)(t - q); /* includes '-' if any */
q = t + 1;
while (*q != '\n') { l2++; q++; }
q++; /* newline */
} else {
while (*q != '\n') { l1++; q++; }
q++; /* newline */
}
*ap++ = (unsigned)op | ((unsigned)l1 << 8) | ((unsigned)l2 << 16);
}
}
/* ---- pass 2: the len-known parse + exec ---- */
P = P0;
{
const char *hp = P0 + 0;
/* re-find the first line start: the header length */
while (*hp != '\n') hp++;
hp++;
P = hp;
}
unsigned *ap = aux;
for (int i = 0; i < n; i++){
__builtin_prefetch(P + 160, 0, 0);
unsigned info = *ap++;
int op = (int)(info & 1);
int l1 = (int)((info >> 8) & 15);
int l2 = (int)((info >> 16) & 15);
const char *s = P;
P += 2;
if (op){
int neg = 0;
if (*P == '-'){ neg = 1; P++; }
unsigned long long x;
__builtin_memcpy(&x, P, 8);
int la = l1 - neg;
int a;
if (la <= 8){
a = (int)conv8(x, la);
} else if (la == 9){
a = (int)conv8(x, 8);
a = a * 10 + (int)((unsigned char)P[8] - '0');
} else {
a = (int)conv8(x, 8);
a = a * 100 + (int)((unsigned char)P[8] - '0') * 10 + (int)((unsigned char)P[9] - '0');
}
P += la + 1; /* digits + space */
__builtin_memcpy(&x, P, 8);
int b = (int)conv8(x, l2);
P += l2 + 1; /* digits + newline */
int q = b >> 7;
int r = b & 127;
if (neg) sub_val(q, r, (uint64_t)a);
else if (a) add_val(q, r, (uint64_t)a);
} else {
unsigned long long x;
__builtin_memcpy(&x, P, 8);
int k = (int)conv8(x, l1);
P += l1 + 1;
int q = k >> 7;
int r = k & 127;
uint64_t v;
{
int l2n = q >> 12, l1n = q >> 6;
uint64_t b3 = 1ull << l2n;
if (fm3 & b3) v = 1;
else if (!(nm3 & b3)) v = 0;
else {
uint64_t b2 = 1ull << (l1n & 63);
if (fm2[l2n] & b2) v = 1;
else if (!(nm2[l2n] & b2)) v = 0;
else {
uint64_t b1 = 1ull << (q & 63);
if (fm1[l1n] & b1) v = 1;
else if (!(nm1[l1n] & b1)) v = 0;
else v = (uint64_t)((blk[q] >> r) & 1);
}
}
}
out[O++] = (char)('0' + (v & 1));
out[O++] = '\n';
}
(void)s;
}
done(O, di, out, use_di);
}
| Compilation | N/A | N/A | Compile OK | Score: N/A | 显示更多 |
| Testcase #1 | 7.81 us | 28 KB | Accepted | Score: 4 | 显示更多 |
| Testcase #2 | 10.81 us | 28 KB | Accepted | Score: 4 | 显示更多 |
| Testcase #3 | 65.42 us | 32 KB | Accepted | Score: 4 | 显示更多 |
| Testcase #4 | 107.45 us | 40 KB | Accepted | Score: 4 | 显示更多 |
| Testcase #5 | 239.58 us | 48 KB | Accepted | Score: 4 | 显示更多 |
| Testcase #6 | 243.33 us | 60 KB | Accepted | Score: 4 | 显示更多 |
| Testcase #7 | 444.17 us | 96 KB | Accepted | Score: 4 | 显示更多 |
| Testcase #8 | 428.99 us | 68 KB | Accepted | Score: 4 | 显示更多 |
| Testcase #9 | 1.517 ms | 272 KB | Accepted | Score: 4 | 显示更多 |
| Testcase #10 | 2.434 ms | 288 KB | Accepted | Score: 4 | 显示更多 |
| Testcase #11 | 2.708 ms | 300 KB | Accepted | Score: 4 | 显示更多 |
| Testcase #12 | 2.452 ms | 556 KB | Accepted | Score: 4 | 显示更多 |
| Testcase #13 | 3.674 ms | 600 KB | Accepted | Score: 4 | 显示更多 |
| Testcase #14 | 10.289 ms | 1 MB + 644 KB | Accepted | Score: 4 | 显示更多 |
| Testcase #15 | 10.853 ms | 2 MB + 436 KB | Accepted | Score: 4 | 显示更多 |
| Testcase #16 | 21.055 ms | 3 MB + 236 KB | Accepted | Score: 4 | 显示更多 |
| Testcase #17 | 22.112 ms | 2 MB + 288 KB | Accepted | Score: 4 | 显示更多 |
| Testcase #18 | 31.89 ms | 4 MB + 860 KB | Accepted | Score: 4 | 显示更多 |
| Testcase #19 | 37.352 ms | 5 MB + 660 KB | Accepted | Score: 4 | 显示更多 |
| Testcase #20 | 26.188 ms | 6 MB + 760 KB | Accepted | Score: 4 | 显示更多 |
| Testcase #21 | 34.025 ms | 7 MB + 256 KB | Accepted | Score: 4 | 显示更多 |
| Testcase #22 | 43.95 ms | 4 MB + 232 KB | Accepted | Score: 4 | 显示更多 |
| Testcase #23 | 46.239 ms | 4 MB + 880 KB | Accepted | Score: 4 | 显示更多 |
| Testcase #24 | 46.857 ms | 4 MB + 508 KB | Accepted | Score: 4 | 显示更多 |
| Testcase #25 | 52.258 ms | 8 MB + 40 KB | Accepted | Score: 4 | 显示更多 |