// References:
// [0] saffah_codex_6s_agg2, duck.ac submission #103829,
// https://duck.ac/submission/103829 . Directly copied its accepted
// packed S/T suffix automata, 80-byte T rows and Euler queries.
// [1] saffah_codex_6s_agg2, duck.ac submission #103353,
// https://duck.ac/submission/103353 . Packed transitions and
// query engine inherited through [0].
// [2] saffah_cc_v41_260924, duck.ac submission #99767,
// https://duck.ac/submission/99767 . Packed S transitions and
// original query engine inherited through [1].
// [3] saffah_cc_v41_260924, duck.ac submissions #87737 and #99553,
// https://duck.ac/submission/87737 and
// https://duck.ac/submission/99553 . Suffix automata, interval query,
// ancestor painting and block-max ideas inherited through [2].
// No separate license notices appeared on these public submissions;
// authors, URLs and reused parts are credited here.
// Approach:
// Initialize each 80-byte T transition row using five aligned 16-byte
// stores. The old two 32-byte stores sometimes crossed cache-line
// boundaries because successive rows rotate through four offsets.
// Purpose:
// Measure whether avoiding split-line stores lowers official runtime
// below the exact current noi18c leaderboard threshold.
#include <string.h>
#include <stdint.h>
#include <immintrin.h>
#pragma GCC push_options
#pragma GCC target("avx2,popcnt,bmi,bmi2")
typedef unsigned char u8;
typedef unsigned int u32;
typedef unsigned long long u64;
typedef unsigned long ul;
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 MAXS 500005
#define MAXST 1000016
#define MAXQ 100005
#define MAXT 1000008
#define MAXTT 2000016
static u32 bhead[MAXS+8], bnext[MAXST], szsub[MAXST], nxt[MAXST];
static u32 tin[MAXST], tout[MAXST], mxo[MAXST];
static u32 ord_[MAXST]; static int nord_; // len-descending state order (lazy-Euler)
static int eul_ready;
// ancestor painting (see 思路): wl_last[x] = max own+1 among activated prefix-states
// whose suffix-link path passes through x == MX(x, r) exactly.
static u32 wl_last[MAXST];
static int wl_mode; static long long wl_budget;
// per-query paint-back scratch: zeroing only the states we painted keeps the
// reset O(sz) instead of O(MAXST) between queries.
static u32 plist_[MAXST];
// ======================= SAM of S (dense 26-way transitions) ==============
// transitions of state p are at s_nxt[p*26 .. p*26+26); every state index is
// used once per process, so BSS stays zero and fresh states have no transitions.
// Transitions packed as 24-bit little-endian words: 26*3 = 78 B per state instead of
// 26*4 = 104 B. State ids are <= 2*MAXS+8 < 2^24, so 24 bits are lossless.
// BUDGET (tc17: s_sz=661320) -- MEASURED sizes, not guesses:
// touched footprint 65.6 MB -> 49.2 MB (-16.4 MB) = -4197 first-touch pages.
// At the measured 244 ns/page that alone is -1.02 ms, and every sget in the SAM
// construction and in the match loop additionally reads a 25%-smaller random
// working set (better TLB reach), which is the part that should pay off more.
// COST: sget gains one AND; sset becomes a 4-byte read-modify-write (the extra byte
// read back is the *next* entry's low byte and is written back unchanged, so
// neighbours are preserved) = ~+3..5 uops per access. Reads use a 4-byte load at
// the 3-byte offset, so the array carries 10 spare rows of padding.
static u8 s_nxt[(2*MAXS+10)*78];
static u32 s_len[MAXST], s_link[MAXST];
static int s_sz, s_last;
static u32 s_own[MAXS];
static inline int sget(int p, int c){
u32 x; __builtin_memcpy(&x, s_nxt + (size_t)p*78 + (size_t)c*3, 4);
return (int)(x & 0xFFFFFFu);
}
static inline void sset(int p, int c, int t){
size_t off = (size_t)p*78 + (size_t)c*3;
u32 x; __builtin_memcpy(&x, s_nxt + off, 4);
x = (x & 0xFF000000u) | ((u32)t & 0xFFFFFFu); // keep next entry's low byte
__builtin_memcpy(s_nxt + off, &x, 4);
}
static void build_sam_s(const char* S, int n){
s_sz = 1; s_last = 1;
s_len[1] = 0; s_link[1] = 0;
memset(bhead, 0, (size_t)(n+2)*4);
for(int i=0;i<n;i++){
int c = S[i]-'a';
int cur = ++s_sz;
s_len[cur] = s_len[s_last]+1;
{ u32 L = s_len[cur]; bnext[cur] = bhead[L]; bhead[L] = (u32)cur; }
szsub[cur] = 1; mxo[cur] = (u32)(i+1);
s_own[i] = (u32)cur;
int p = s_last;
for(; p && !sget(p,c); p = (int)s_link[p]) sset(p,c,cur);
if(!p) s_link[cur] = 1;
else {
int q = sget(p,c);
if((int)s_len[p]+1 == (int)s_len[q]) s_link[cur] = (u32)q;
else {
int cl = ++s_sz;
s_len[cl] = s_len[p]+1; s_link[cl] = s_link[q];
{ u32 L = s_len[cl]; bnext[cl] = bhead[L]; bhead[L] = (u32)cl; }
szsub[cl] = 1; mxo[cl] = 0;
memcpy(s_nxt + (size_t)cl*78, s_nxt + (size_t)q*78, 78);
for(; p && sget(p,c)==q; p = (int)s_link[p]) sset(p,c,cl);
s_link[q] = (u32)cl; s_link[cur] = (u32)cl;
}
}
s_last = cur;
}
}
// ======================= SAM of T, rebuilt per query (dense) ==============
static u8 t_nxt[(2*MAXT+10)*80] __attribute__((aligned(16)));
static u32 t_len[MAXTT], t_link[MAXTT];
static int t_sz, t_last;
static inline int tget(int p, int c){
u32 x; __builtin_memcpy(&x, t_nxt + (size_t)p*80 + (size_t)c*3, 4);
return (int)(x & 0xFFFFFFu);
}
static inline void tset(int p, int c, int t){
size_t off = (size_t)p*80 + (size_t)c*3;
u32 x; __builtin_memcpy(&x, t_nxt + off, 4);
x = (x & 0xFF000000u) | ((u32)t & 0xFFFFFFu);
__builtin_memcpy(t_nxt + off, &x, 4);
}
static inline void tzero(int p){
u8* row = t_nxt + (size_t)p*80;
__m128i z = _mm_setzero_si128();
_mm_store_si128((__m128i*)(row+0), z);
_mm_store_si128((__m128i*)(row+16), z);
_mm_store_si128((__m128i*)(row+32), z);
_mm_store_si128((__m128i*)(row+48), z);
_mm_store_si128((__m128i*)(row+64), z);
}
// ============ suffix-link tree: len-bucket lists + two linear passes ======
static int s_sn;
// Pass 1 (ALWAYS needed): mxo[v] = max own+1 over the subtree of v, by a
// len-descending sweep (children strictly before parents). The same sweep
// records the visiting order into ord_[] (sequential stores), so pass 2 never
// has to re-walk the bucket chains.
static void build_meta(void){
int cnt = 0;
for(int L = s_sn; L >= 0; L--){
for(u32 v = bhead[L]; v; v = bnext[v]){
ord_[cnt++] = v;
u32 p = s_link[v];
if(p && mxo[v] > mxo[p]) mxo[p] = mxo[v];
}
}
nord_ = cnt;
}
// Pass 2 (LAZY): subtree sizes + preorder numbers. Only the range-max over
// [tin,tout) uses these, so they are built at the first query that actually
// needs a range-max. ord_[] is len-descending, so forward = bottom-up sizes,
// backward = top-down preorder.
static void build_intervals(void){
for(int i=0;i<nord_;i++){
u32 v = ord_[i]; u32 p = s_link[v];
if(p) szsub[p] += szsub[v];
}
tin[1] = 1; nxt[1] = 2;
for(int i=nord_-1;i>=0;i--){
u32 v = ord_[i];
if(v == 1) continue;
u32 p = s_link[v];
u32 t = nxt[p];
tin[v] = t; tout[v] = t + szsub[v];
nxt[p] = t + szsub[v];
nxt[v] = t + 1;
}
}
// ============ range max over the Euler array (fused block scans) ============
// Replaces the earlier "block prefix/suffix arrays + segment tree over blocks".
// MEASURED on the real judge (tools/probe.py, n=5e5 random 26-letter S, cycles):
// old eactivate 70.15M old build_meta 15.58M old build_intervals 22.38M
// old qmax ~96 cyc/call
// The old activation wrote a whole 64-entry prefix block AND a whole 64-entry
// suffix block per activation (512 B = 8 cache lines of RFO traffic), plus a
// 15-level segment-tree update -- 140 cycles per activation. With plain block
// scans the activation is 3 stores (one cold, two L1-resident) and the segment
// tree disappears.
#define EBS 8
#define EBSH 3
#define GBS 128
#define GBSH 7
#define ENB ((MAXST>>EBSH)+8)
#define ESB ((ENB>>GBSH)+8)
static u32 ev[MAXST+8];
static u32 ebmax[ENB+8];
static u32 eb2[ESB+8];
static inline void eactivate(int leaf, u32 val){
ev[leaf] = val;
int b = leaf >> EBSH;
ebmax[b] = val;
eb2[b >> GBSH] = val;
}
#define AVX2T __attribute__((target("avx2")))
AVX2T static inline __m256i ldu256(const void *p){
__m256i r; __asm__("vmovdqu %1, %0" : "=x"(r) : "m"(*(const __m256i *)p)); return r; }
// accumulate max over p[0..n) into acc for ANY n>=0 (n<8 must not use the
// overlapping tail load -- it would read BEFORE p and pick up foreign values)
AVX2T static inline __m256i accblk(__m256i acc, const u32* p, int n){
if(n <= 0) return acc;
if(n < 8){
for(int i=0;i<n;i++) acc = _mm256_max_epu32(acc, _mm256_set1_epi32((int)p[i]));
return acc;
}
int i = 0;
for(; i+8 <= n; i += 8) acc = _mm256_max_epu32(acc, ldu256(p+i));
if(i < n) acc = _mm256_max_epu32(acc, ldu256(p+n-8)); // overlapping tail load
return acc;
}
AVX2T static inline u32 red256(__m256i acc){
__m128i lo = _mm256_castsi256_si128(acc), hi = _mm256_extracti128_si256(acc,1);
__m128i m = _mm_max_epu32(lo,hi);
m = _mm_max_epu32(m, _mm_shuffle_epi32(m,_MM_SHUFFLE(1,0,3,2)));
m = _mm_max_epu32(m, _mm_shuffle_epi32(m,_MM_SHUFFLE(2,3,0,1)));
return (u32)_mm_cvtsi128_si32(m);
}
AVX2T static inline u32 scanmax(const u32* p, int n){
if(n <= 0) return 0;
return red256(accblk(_mm256_setzero_si256(), p, n));
}
static inline u32 qmax(int l, int r){ // max over [l,r)
if(l>=r) return 0;
int b1=l>>EBSH, b2=(r-1)>>EBSH;
if(b1==b2) return scanmax(ev+l, r-l);
__m256i acc = _mm256_setzero_si256();
acc = accblk(acc, ev+l, ((b1+1)<<EBSH) - l);
int j = r & (EBS-1);
if(j) acc = accblk(acc, ev+(b2<<EBSH), j);
else acc = accblk(acc, ebmax+b2, 1);
int lo=b1+1, hi=b2, cnt=hi-lo; // whole blocks [lo,hi)
if(cnt > 0){
if(cnt <= GBS){
acc = accblk(acc, ebmax+lo, cnt);
} else {
int s1=(lo+GBS-1)&~(GBS-1), s2=hi&~(GBS-1);
if(s1>lo) acc = accblk(acc, ebmax+lo, s1-lo);
if(s2>s1) acc = accblk(acc, eb2+(s1>>GBSH), (s2-s1)>>GBSH);
if(hi>s2) acc = accblk(acc, ebmax+s2, hi-s2);
}
}
return red256(acc);
}
// ============================== query handling ============================
struct QE { const char* t; int tl, l, r, idx; };
static QE qs[MAXQ];
static QE qs2[MAXQ];
static long long answers[MAXQ];
static int f_[MAXS];
static int cnt_[MAXS+2];
static char* OUTP;
static inline void emit(long long v){
char b[24]; int c=0;
if(v==0){ *OUTP++='0'; *OUTP++='\n'; return; }
while(v){ b[c++] = (char)('0' + (int)(v%10)); v/=10; }
while(c) *OUTP++ = b[--c];
*OUTP++='\n';
}
static void run(const u8* in, ul insz, char* out){
const char* p = (const char*)in;
const char* e = p + insz;
OUTP = out;
while(p<e && (*p<'a'||*p>'z')) p++;
const char* sstart = p;
while(p<e && *p>='a'&&*p<='z') p++;
int sn = (int)(p - sstart);
build_sam_s(sstart, sn);
while(p<e && (*p<'0'||*p>'9')) p++;
int Q=0;
while(p<e && *p>='0'&&*p<='9'){ Q = Q*10 + (*p-'0'); p++; }
if(Q>MAXQ) Q=MAXQ;
for(int i=0;i<Q;i++){
while(p<e && (*p<'a'||*p>'z')) p++;
const char* ts = p;
while(p<e && *p>='a'&&*p<='z') p++;
int tl = (int)(p-ts);
if(tl>MAXT) tl=MAXT;
while(p<e && (*p<'0'||*p>'9')) p++;
int l=0; while(p<e && *p>='0'&&*p<='9'){ l=l*10+(*p-'0'); p++; }
while(p<e && (*p<'0'||*p>'9')) p++;
int r=0; while(p<e && *p>='0'&&*p<='9'){ r=r*10+(*p-'0'); p++; }
qs[i].t = ts; qs[i].tl = tl; qs[i].l = l; qs[i].r = r; qs[i].idx = i;
}
s_sn = sn;
int sz = s_sz;
// ---- strategy choice ---------------------------------------------------
// The ancestor-painting routes are KEPT in the file but no longer selected:
// real-judge measurement showed their cost is sum_i depth(s_own[i]) steps, which
// the 256-sample estimate can under-predict (a deep tree blows it up ~4x), while
// the Euler route is now bounded and cheap: with the block-scan activation its
// fixed cost is 34 cyc/state (intervals) + 20 cyc/activation + 113 cyc/qmax.
// Forcing the Euler route makes the tree phase predictable instead of data-dependent.
wl_mode = 0; wl_budget = 0;
if(!wl_mode) build_meta();
eul_ready = 0;
// ev / ebmax / etree are zero initialized (BSS): no clearing needed
{ // counting sort of the queries by r
int mx = sn+1;
memset(cnt_, 0, (size_t)(mx+2)*4);
for(int i=0;i<Q;i++){ int rr = qs[i].r; if(rr<0) rr=0; if(rr>mx) rr=mx; cnt_[rr]++; }
int acc=0;
for(int i=0;i<=mx;i++){ int c=cnt_[i]; cnt_[i]=acc; acc+=c; }
for(int i=0;i<Q;i++){ int rr = qs[i].r; if(rr<0) rr=0; if(rr>mx) rr=mx; qs2[cnt_[rr]++] = qs[i]; }
}
int act = 0; // positions < act already activated
long long wl_work = 0;
for(int qi=0; qi<Q; qi++){
const QE& q = qs2[qi];
int need = q.r; // activate positions 0..r-1
if(need > sn) need = sn;
if(need < 0) need = 0;
int npaint = 0;
if(wl_mode == 2){
// downward sweep, stop at the first state already painted this query:
// the first state painted gets the largest own+1, which is exactly MX(x, need)
for(int i=need-1;i>=0;i--){
int a = (int)s_own[i]; u32 val = (u32)(i+1);
while(a != 1 && !wl_last[a]){ wl_last[a] = val; plist_[npaint++] = (u32)a; a = (int)s_link[a]; }
}
} else if(wl_mode){
for(; act<need; act++){
int a = (int)s_own[act]; u32 val = (u32)(act+1);
wl_last[a] = val; wl_work++;
while(a != 1){ a = (int)s_link[a]; wl_last[a] = val; wl_work++; }
if(wl_work > wl_budget){ wl_mode = 0; break; } // blew the budget
}
if(!wl_mode){ // fell back mid-activation: replay into the Euler struct
build_meta(); build_intervals(); eul_ready = 1;
for(int i=0;i<act;i++){ int v = (int)s_own[i]; eactivate((int)tin[v], (u32)(i+1)); }
for(; act<need; act++){ int v = (int)s_own[act]; eactivate((int)tin[v], (u32)(act+1)); }
}
} else if(eul_ready){
for(; act<need; act++){
int v = (int)s_own[act];
eactivate((int)tin[v], (u32)(act+1));
}
} else if(need > act) act = need;
const char* T = q.t;
int tl = q.tl;
int l0 = q.l - 1;
int r1 = q.r - 1;
int maxlen = q.r - q.l + 1;
if(maxlen < 0) maxlen = 0;
int v = 1, len = 0, e = -1; // e = witness end position of the current match
for(int i=0;i<tl;i++){
int c = T[i]-'a';
// fast path: the match extends in place at the witness (f[i] <= f[i-1]+1, so len+1 is optimal)
if(len > 0 && e < r1 && sstart[e+1]-'a' == c){
v = sget(v,c); len++; e++;
f_[i] = len;
continue;
}
while(v!=1 && !sget(v,c)){ v = (int)s_link[v]; len = (int)s_len[v]; }
int nx = sget(v,c);
if(nx){ v = nx; len++; } else { v = 1; len = 0; }
e = -1;
// clip the match to S[l..r]: longest suffix whose endpos hits [l+len-1, r-1]
while(len>0){
if(len > maxlen){
len = maxlen;
while(v != 1 && len <= (int)s_len[s_link[v]]) v = (int)s_link[v];
if(len == 0) break;
}
u32 m;
if(mxo[v] <= (u32)q.r) m = mxo[v];
else {
if(!eul_ready){
build_intervals();
for(int i2=0;i2<act;i2++){ int vv=(int)s_own[i2]; eactivate((int)tin[vv], (u32)(i2+1)); }
eul_ready = 1;
}
m = qmax((int)tin[v],(int)tout[v]);
}
// lengths in this state's class that can still satisfy the test
int lim = (int)m - l0;
if(lim >= len){ e = (int)m - 1; break; }
int pl = (int)s_len[s_link[v]];
if(lim > pl){ len = lim; e = (int)m - 1; break; }
len = pl;
if(len == 0) break;
v = (int)s_link[v];
}
f_[i] = len;
}
if(wl_mode == 2){ for(int k=0;k<npaint;k++) wl_last[plist_[k]] = 0; }
// SAM(T) -> g[], accumulate
t_sz=1; t_last=1;
t_len[1]=0; t_link[1]=0;
tzero(1);
long long ans=0;
for(int i=0;i<tl;i++){
int c = T[i]-'a';
int cur = ++t_sz;
t_len[cur] = t_len[t_last]+1; t_link[cur]=0;
tzero(cur);
int pp = t_last;
for(; pp && !tget(pp,c); pp = (int)t_link[pp]) tset(pp,c,cur);
int lk;
if(!pp) lk = 1;
else {
int qq = tget(pp,c);
if((int)t_len[pp]+1 == (int)t_len[qq]) lk = qq;
else {
int cl = ++t_sz;
t_len[cl] = t_len[pp]+1; t_link[cl] = t_link[qq];
memcpy(t_nxt + (size_t)cl*80, t_nxt + (size_t)qq*80, 80);
for(; pp && tget(pp,c)==qq; pp = (int)t_link[pp]) tset(pp,c,cl);
t_link[qq] = (u32)cl; lk = cl;
}
}
t_link[cur] = (u32)lk;
t_last = cur;
int g = (int)t_len[lk];
int m = g > f_[i] ? g : f_[i];
ans += (long long)(i+1) - m;
}
answers[q.idx] = ans;
}
for(int i=0;i<Q;i++) emit(answers[i]);
}
#pragma GCC pop_options
#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 u8* in=(const u8*)d->IBp; ul insz=d->IBl;
if(!insz && d->is){ in=(const u8*)d->ip; insz=d->is; }
run(in,insz,d->op);
d->os=(ul)(OUTP-d->op);
}
__asm__ volatile("syscall"::"a"(60),"D"(0):"rcx","r11","memory");
for(;;);
}
#endif
| Compilation | N/A | N/A | Compile OK | Score: N/A | 显示更多 |
| Testcase #1 | 1.123 ms | 164 KB | Accepted | Score: 4 | 显示更多 |
| Testcase #2 | 1.208 ms | 288 KB | Accepted | Score: 4 | 显示更多 |
| Testcase #3 | 1.264 ms | 296 KB | Accepted | Score: 4 | 显示更多 |
| Testcase #4 | 16.04 ms | 628 KB | Accepted | Score: 4 | 显示更多 |
| Testcase #5 | 15.494 ms | 616 KB | Accepted | Score: 4 | 显示更多 |
| Testcase #6 | 83.828 ms | 150 MB + 900 KB | Accepted | Score: 4 | 显示更多 |
| Testcase #7 | 83.685 ms | 150 MB + 848 KB | Accepted | Score: 4 | 显示更多 |
| Testcase #8 | 12.786 ms | 19 MB + 160 KB | Accepted | Score: 4 | 显示更多 |
| Testcase #9 | 13.942 ms | 16 MB + 256 KB | Accepted | Score: 4 | 显示更多 |
| Testcase #10 | 28.555 ms | 35 MB + 996 KB | Accepted | Score: 4 | 显示更多 |
| Testcase #11 | 34.095 ms | 32 MB + 184 KB | Accepted | Score: 4 | 显示更多 |
| Testcase #12 | 47.358 ms | 52 MB + 648 KB | Accepted | Score: 4 | 显示更多 |
| Testcase #13 | 62.929 ms | 48 MB + 748 KB | Accepted | Score: 4 | 显示更多 |
| Testcase #14 | 67.806 ms | 69 MB + 924 KB | Accepted | Score: 4 | 显示更多 |
| Testcase #15 | 99.189 ms | 65 MB + 700 KB | Accepted | Score: 4 | 显示更多 |
| Testcase #16 | 89.984 ms | 87 MB + 192 KB | Accepted | Score: 4 | 显示更多 |
| Testcase #17 | 140.618 ms | 82 MB + 824 KB | Accepted | Score: 4 | 显示更多 |
| Testcase #18 | 78.4 ms | 47 MB + 556 KB | Accepted | Score: 4 | 显示更多 |
| Testcase #19 | 91.977 ms | 64 MB + 140 KB | Accepted | Score: 4 | 显示更多 |
| Testcase #20 | 110.136 ms | 81 MB + 36 KB | Accepted | Score: 4 | 显示更多 |
| Testcase #21 | 120.322 ms | 98 MB + 512 KB | Accepted | Score: 4 | 显示更多 |
| Testcase #22 | 129.221 ms | 98 MB + 368 KB | Accepted | Score: 4 | 显示更多 |
| Testcase #23 | 127.615 ms | 98 MB + 316 KB | Accepted | Score: 4 | 显示更多 |
| Testcase #24 | 122.142 ms | 98 MB + 480 KB | Accepted | Score: 4 | 显示更多 |
| Testcase #25 | 121.036 ms | 98 MB + 520 KB | Accepted | Score: 4 | 显示更多 |