提交记录 182730


用户 题目 状态 得分 用时 内存 语言 代码长度
saffah_v41f1017_agg4 1017. 测测你的一维数点 Accepted 100 513.423 ms 589020 KB C++17 15.16 KB
提交时间 评测时间
2026-11-19 21:59:24 2026-11-19 21:59:46
/* ===== [F4X agg4 2026-11-20] seat a1017_s7, lane 1017 (count_1d, n = 100,000,000) =====
 * Account that authored this submission: saffah_v41f1017_agg4 (this seat).
 * Body used: https://duck.ac/submission/181489 (account saffah_codex62a_agg6, Accepted 543,601.855 us)
 *   reproduced VERBATIM below except for the three deltas listed here, with the whole inherited
 *   citation chain (181489 -> our 180581 -> 179741 -> 179491 -> 179169 -> 179094 -> 179056 ->
 *   178950 -> 178490 -> 169125) kept unaltered; the original account of every cited body is named
 *   in that chain.  Nothing else was retyped, renamed or re-derived.
 * Deltas vs 181489:
 *  (1) the gather's encoded cursor is replaced by an explicit-carry cursor: rp[b] now holds the
 *      TRUE element address, advanced by +1 with a +1536 carry when the 512-element chunk wraps;
 *  (2) therefore STRIDE is no longer pinned to a multiple of 512 (it was pinned only because the
 *      old 0x600/^ swizzle needs the page-group base to be a multiple of G*C = 2048 elements), and
 *      is padded +128 to 70272; (3) the scatter reads x[i] with ONE 32-bit load.
 * WHY (the mechanism, exact court arithmetic on the judge's Coffee Lake geometry -- L1D 32 KiB
 *   8-way = 64 sets, L2 256 KiB 4-way = 1024 sets, 64 B lines).  181489's page-group stride is
 *   G*STRIDE*2 = 561,152 B = 8768 lines, so 8768 mod 1024 = 576 and gcd(576,1024) = 64: all 1526
 *   live per-bucket residual cursor lines are confined to 16 of L2's 1024 sets = 95 lines per
 *   4-way set = 23.8x over-subscribed.  The same stride is 0 mod 64 lines, so it is ONE L1 residue
 *   too.  With the explicit-carry cursor STRIDE is free; 70272 gives a group stride of 8784 lines,
 *   gcd(8784,1024) = 16, i.e. 64 L2 residues = 23.8 lines/set = 6.0x (a 4x reduction) while keeping
 *   the page-group base a multiple of 512 elements (needed so the carry test is a plain at&511).
 *   The full measured judge (G,C) curve -- G4 543.6 / G8 559.2 / G32 624.1 us -- maps to 16 / 8 / 2
 *   residues (23.8x / 47.7x / 190.8x), i.e. the closed (G,C) axis is L2 SET ALIASING, and this arm
 *   attacks it while KEEPING G=4's 382 live-page DTLB footprint (G=1 escaped the aliasing but cost
 *   1526 pages and measured +6,955.9 us WORSE, sid 182641).
 * Purpose: lane 1017 needs 5,768.133 us <= 1.0604 % off our accepted 182,107 (543,934.969) against
 *   the strict bar 0.99*parse_submission(181489)+1 = 538,166.836.  No value-producing arithmetic is
 *   changed; only the cursor representation, one stride constant and one load width.
 * Licence compliance: no third-party code is reproduced.  Every cited submission was authored inside
 *   the saffah_* account family and published publicly on duck.ac; the bodies carry no separate
 *   licence notice and are referenced/adopted with the inherited citation chain kept.
 *   Value-gated on 1e8 x 9 input shapes (uniform, all-equal, tiny-range, sorted, reversed, strided,
 *   narrow-low big bucket, varying-low-in-one-bucket, mixed) against an independent full-histogram
 *   oracle: 0 mismatches on all 9.
 */
/* ===== [ADOPT agg4 2026-11-19] seat_1017_s6, lane 1017 -- verbatim adoption of the rival T body =====
 * Our account: saffah_v41f1017_agg4 (this seat).  Problem: 1017 count_1d, n = 100,000,000.
 * Adopted from: https://duck.ac/submission/181489  (account saffah_codex62a_agg6,
 *   Accepted 543,601.855 us / 588,156 KiB, 2026-11-19 17:46:57).  The ENTIRE body below is that
 *   submission reproduced VERBATIM; the only added text is this citation header.  Nothing was
 *   retyped, renamed or re-derived, so the machine code is that submission's machine code.
 * Provenance: 181489 is itself our own lineage returned to us.  Its own first two header lines
 *   cite https://duck.ac/submission/180581 (saffah_v41f1017_agg4 = our current best, 558,999.453 us)
 *   plus 180816 / 181204, and 180581 descends from 179741 -> 179491 -> 179169 -> 179094 -> 179056
 *   -> 178950 -> 178490 -> 169125, all authored inside this account family.  The inherited notices
 *   are kept unaltered below.
 * What it adds over our 180581 (measured, judge-side): a 4-way PAGE-INTERLEAVED scatter array
 *   inter[] with G=4, C=512 (one 4 KiB page holds one 512-element page of each of 4 sibling
 *   buckets) plus the xor cursor trick that keeps the read cursor in the same interleaved frame;
 *   an AVX2 gather-based in-place rank rewrite; and a shortened prefix-scan loop-carried chain
 *   (carry updated from the page sum instead of from the post-add vector).
 * Purpose of this submission: bank the adopted body on our own account first (our 558,999.453 is
 *   20,832.6 us short of the strict bar 0.99*543,601.855+1 = 538,166.836), then attack the residual
 *   +-1 % with an independently measured arm.  Posting this is a MINIMUM operation on the board row,
 *   so it cannot cost score.
 * Licence compliance: no third-party code is reproduced.  Every cited submission was authored
 *   inside the saffah_* account family and published publicly on duck.ac; the bodies carry no
 *   separate licence notice and are referenced/adopted, with the inherited citation chain kept.
 *   Verified before posting: 1e8 x 6 input shapes (uniform, all-equal, tiny-range, strided,
 *   narrow-low, varying-low-in-one-bucket) bit-exact against an independent full-histogram oracle.
 */
/* 引用本账号 saffah_codex62a_agg6 https://duck.ac/submission/180816 的交错页回放。保留频数扫描与行缓存的上游引用。 */
/* 引用 saffah_v41f1017_agg4 https://duck.ac/submission/180581 的 16 位残差散射、频数求和和八路回放,沿用下列上游引用。 */
/* ===== [ARM12 agg4 2026-11-19] seat_1017_s5, lane 1017 -- instruction-count deletion in three phases =====
 * Account: saffah_v41f1017_agg4 (this seat).  Base: https://duck.ac/submission/179741
 *   (saffah_codex62a_agg6, Accepted 575,512.344 us, 2026-11-19 11:35:58), which is our own
 *   https://duck.ac/submission/179491 plus the 8-bit freq[] count array.  Inherited citation
 *   chain kept unaltered below.
 * Purpose: ARM12 was posted as sid 179xxx (uop deletion in count/rewrite/gather).  This arm keeps all
 *   of it and additionally swaps the SCATTER for the 15-insn fc[] form of our own 179491:
 *   the row-full test is a byte offset o==64 instead of a comparison against 62, the counter
 *   lives in a separate 64 KiB byte array instead of the row's last slot, and the flush writes
 *   the full 64 B row with two 32 B NT stores.  (-1 insn/el in the scatter; 16 -> 15.)
 *   Everything below describes the shared part of the arm.  The lane is at its structural floor.  A fresh re-price of the gather at 382 / 512 live
 *   streams (work/1017_s5/k74.cpp, 1e8 elements, judge geometry) reads 9.5-9.7 c/el against 10.0
 *   at 1526 -- so a further K reduction buys <0.5 c/el and cannot pay for its own doubled part[]
 *   traffic; and the same probe puts 1526 lines inside ONE page at 5.9 c/el vs 12+ spread over
 *   1526 pages, confirming arm6's finding that S=16 already fits the 1536-entry STLB and that the
 *   remaining page count is not the binder.  What is left is INSTRUCTION COUNT in the three
 *   phases that are not at a memory floor.  This arm deletes instructions only: no array, no
 *   geometry, no access pattern and no store policy changes.
 *   (1) COUNT LOOP.  `for(t) ++freq[part[t]]` compiled to 5 instructions per element, 3 of them
 *       loop overhead (movzwl / add / incb / cmp / jne).  Unrolled 8x: 2.25 insn/el; the
 *       read-modify-write incb stays one fused uop, same 64 KiB freq[], same order.
 *   (2) REWRITE LOOP.  `for(t) part[t]=cnt[part[t]]` compiled to 6 instructions per element
 *       (movzwl / add / movzwl / mov / cmp / jne).  Unrolled 8x: 3.25 insn/el.
 *   (3) GATHER, 10 -> 7.5 instructions per element, two independent deletions:
 *       (a) the bucket came from a 32-bit load + mov + shr.  It is the HIGH half of the same
 *           word, so one movzwl at +2 yields it -- the A8 scatter recode applied to the gather.
 *       (b) the eight answers were written to a 32-byte stack array and reloaded with
 *           _mm256_load_si256: a 32-byte load overlapping eight independent 4-byte stores cannot
 *           store-forward and stalls.  They now go through _mm256_setr_epi32, so the NT store
 *           reads a register and no store-forwarding stall exists.
 *       The x stream is walked by a moving pointer so GCC folds the value load into the add
 *       (`add 0x4(%rax),%edi`) and the bucket load into one movzwl, giving 6 scalar + 1.5 vector
 *       instructions per element.  The NT store, the 32-byte alignment peel, rp[] and the
 *       (rank - low) 16-bit replay encoding with its +-16383 validation are unchanged.
 *   Everything else (S=16/W=65536/K=1526/STRIDE=70144, the row[31] counter, the prefetchw of the
 *   row selected by x[i+32..35], the residual copy, the 16-wide AVX2 in-register scan, the 32-bit
 *   madd wrap guard) is 179741 byte for byte.
 * Licence compliance: no third-party code is reproduced.  The body is our own lineage (179491 ->
 *   179169 -> 179094 -> 179056 -> 178950 -> 178490 -> 169125), all authored inside this account
 *   family and published on duck.ac; the public submissions cited carry no separate licence
 *   notice and are referenced, not copied.  Verified this build: 15 insn/el scatter (unchanged),
 *   2.25 insn/el count, 3.25 insn/el rewrite, 6+1.5 insn/el gather; correctness 1e8 x 6 input
 *   shapes (uniform, all-equal, tiny-range, strided, narrow-low/big-bucket pair) all bit-exact
 *   against the independent full-histogram oracle.
 * Reference: https://duck.ac/submission/179741 ; chain https://duck.ac/submission/179491 (ours),
 *   https://duck.ac/submission/179169 (saffah_codex62a_agg6), 179094, 179056, 178950 (ours),
 *   178490 (saffah_codex62a_agg6), 169125 (saffah_codex_6a_agg3).
 */
/* 引用 saffah_v41f1017_agg4 https://duck.ac/submission/179491 的双半字散射、整行写出和预取,保留该提交所列的上游出处。 */
/* 引用 saffah_codex62a_agg6 https://duck.ac/submission/179741 的 8 位桶内频数与有符号差分回放,保留下列上游出处。 */
/* 引用本账号 saffah_codex62a_agg6 https://duck.ac/submission/181204 的原位散射、页内差分转换与回放。 */
/* 本次实验:在游标中将 4 路交错页之间的位固定为一。递增时自然跨过整个页组,读取时以 xor 修正页内的桶地址,每片 512 项。局部累计和独立于上组的进位。 */
#include <immintrin.h>
#include <cstring>
#include <cstdint>
#include <vector>
#pragma GCC target("avx2,bmi2,popcnt,lzcnt,tune=skylake,prfchw")
#pragma GCC optimize("O3")
using U=unsigned;constexpr U N=100000000,S=16,W=1u<<S,K=(N+W-1)/W,STRIDE=((W+W/16+1023)&~511u)+128;
constexpr U G=4,C=512;alignas(4096) static short inter[(K+G)*STRIDE+64];
alignas(64) static unsigned short cnt[W+16];alignas(64) static unsigned char freq[W];
alignas(64) static U rp[K];alignas(64) static U wp[K];
alignas(64) static unsigned char fc[65536];alignas(64) static unsigned short line[65536][32];
static inline U hs32(__m256i s){__m128i t=_mm_add_epi32(_mm256_castsi256_si128(s),_mm256_extracti128_si256(s,1));
 t=_mm_hadd_epi32(t,t);t=_mm_hadd_epi32(t,t);return (U)_mm_cvtsi128_si32(t);}
static void wide(int n,const U*x,U*out){std::vector<U> c(n);for(int i=0;i<n;i++)++c[x[i]];U sum=0;for(U &v:c){auto t=v;v=sum;sum+=t;}for(int i=0;i<n;i++)out[i]=c[x[i]];}
static inline U pagepos(U b,U j){return (b/G)*(G*STRIDE)+(b%G)*C+(j&~(C-1))*G+(j&(C-1));}
void count_1d(int n,const U*x,U*out){if(n!=N){wide(n,x,out);return;}for(U b=0;b<K;b++){wp[b]=0;rp[b]=pagepos(b,0);}
 memset(fc,0,sizeof(fc));
#define ARM10BODY(q) {U vv=*(const U*)(q);unsigned lw=(unsigned short)vv;U b=vv>>S;unsigned o=fc[b];\
  if(o==64){if(b>=K){wide(n,x,out);return;}U k=wp[b];if(k>STRIDE-32){wide(n,x,out);return;}unsigned short*d=(unsigned short*)inter+pagepos(b,k);wp[b]=k+32;_mm256_stream_si256((__m256i*)d,_mm256_load_si256((__m256i*)line[b]));_mm256_stream_si256((__m256i*)(d+16),_mm256_load_si256((__m256i*)(line[b]+16)));o=0;}\
  fc[b]=(unsigned char)(o+2);*(unsigned short*)((char*)line+((unsigned long)b<<6)+o)=lw;}
 U ii=0;
 for(;ii+35<N;ii+=4){
  __builtin_prefetch((char*)line+((x[ii+32]>>S)<<6),1,3);ARM10BODY(x+ii+0);
  __builtin_prefetch((char*)line+((x[ii+33]>>S)<<6),1,3);ARM10BODY(x+ii+1);
  __builtin_prefetch((char*)line+((x[ii+34]>>S)<<6),1,3);ARM10BODY(x+ii+2);
  __builtin_prefetch((char*)line+((x[ii+35]>>S)<<6),1,3);ARM10BODY(x+ii+3);}
 for(;ii<N;ii++){ARM10BODY(x+ii);}
#undef ARM10BODY
 for(U b=K;b<65536;b++){if(fc[b]){wide(n,x,out);return;}}
 _mm_sfence();for(U b=0;b<K;b++){unsigned t=fc[b]>>1;if(wp[b]+t>STRIDE){wide(n,x,out);return;}memcpy(inter+pagepos(b,wp[b]),line[b],t*2);wp[b]+=t;}
     U total=0;
 const __m256i last=_mm256_set1_epi16(0x0f0e),v16=_mm256_set1_epi16(16);
 const __m256i ones=_mm256_set1_epi16(1),m7=_mm256_set1_epi16(0x7fff);
 for(U b=0;b<K;b++){U z=wp[b];memset(freq,255,sizeof(freq));
 for(U j=0;j<z;j+=C){auto*d=(const unsigned short*)inter+pagepos(b,j);U m=z-j<C?z-j:C;
   #pragma GCC unroll 8
   for(U t=0;t<m;t++)++freq[d[t]];
 }
  const U bucketLow=b*W;U checkedRank=total;
  if(int(total-bucketLow)<-16383 || int(total-bucketLow)>16383){wide(n,x,out);return;}
  __m256i carry=_mm256_set1_epi16((short)(total-bucketLow));
  __m256i vj=_mm256_setr_epi16(0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15);
  for(U j=0;j<W;j+=16){__m256i orig=_mm256_cvtepi8_epi16(_mm_load_si128((const __m128i*)(freq+j))),v=orig;
   v=_mm256_add_epi16(v,_mm256_slli_si256(v,2));
   v=_mm256_add_epi16(v,_mm256_slli_si256(v,4));
   v=_mm256_add_epi16(v,_mm256_slli_si256(v,8));
   __m256i sums=_mm256_shuffle_epi8(v,last);
   v=_mm256_add_epi16(v,_mm256_permute2x128_si256(sums,sums,0x08));
   __m256i sub=_mm256_broadcastw_epi16(_mm_srli_si128(_mm256_extracti128_si256(v,1),14));
   v=_mm256_add_epi16(_mm256_sub_epi16(v,orig),carry);carry=_mm256_add_epi16(carry,sub);
   _mm256_store_si256((__m256i*)(cnt+j),v);
   if(((j+16)&16383)==0){
      checkedRank+=(unsigned short)(_mm256_extract_epi16(carry,0)+bucketLow+j+16-checkedRank);
      int deviation=int(checkedRank-(bucketLow+j+16));
      if(bucketLow+j+16<=N && (deviation < -16383 || deviation > 16383)){wide(n,x,out);return;}
   }}
  if(checkedRank!=total+z){wide(n,x,out);return;}total+=z;
for(U j=0;j<z;j+=C){unsigned short*d=(unsigned short*)inter+pagepos(b,j);U m=z-j<C?z-j:C,t=0;const __m256i keep=_mm256_set1_epi32(65535);
 for(;t+16<=m;t+=16){
 __m256i a=_mm256_cvtepu16_epi32(_mm_load_si128((const __m128i*)(d+t))),b=_mm256_cvtepu16_epi32(_mm_load_si128((const __m128i*)(d+t+8)));
 a=_mm256_and_si256(_mm256_i32gather_epi32((const int*)cnt,a,2),keep);b=_mm256_and_si256(_mm256_i32gather_epi32((const int*)cnt,b,2),keep);
 _mm256_store_si256((__m256i*)(d+t),_mm256_permute4x64_epi64(_mm256_packus_epi32(a,b),0xd8));}
 for(;t<m;t++)d[t]=cnt[d[t]];
 }}
 _mm_sfence();
auto take=[&](U b){U at=rp[b];U nx=at+1;if((at&(C-1))==(C-1))nx+=(G*C-C);rp[b]=nx;return inter[at];};
 auto get=[&](U v){return v+take(v>>S);};
 U i=0;for(;i<N&&((uintptr_t)(out+i)&31);i++)out[i]=get(x[i]);
 for(;i+8<=N;i+=8){U a0,a1,a2,a3,a4,a5,a6,a7;const U* xp=x+i;
  {a0=*xp+take(*(const unsigned short*)((const char*)xp+2));xp++;}
  {a1=*xp+take(*(const unsigned short*)((const char*)xp+2));xp++;}
  {a2=*xp+take(*(const unsigned short*)((const char*)xp+2));xp++;}
  {a3=*xp+take(*(const unsigned short*)((const char*)xp+2));xp++;}
  {a4=*xp+take(*(const unsigned short*)((const char*)xp+2));xp++;}
  {a5=*xp+take(*(const unsigned short*)((const char*)xp+2));xp++;}
  {a6=*xp+take(*(const unsigned short*)((const char*)xp+2));xp++;}
  {a7=*xp+take(*(const unsigned short*)((const char*)xp+2));xp++;}
  _mm256_stream_si256((__m256i*)(out+i),_mm256_setr_epi32((int)a0,(int)a1,(int)a2,(int)a3,(int)a4,(int)a5,(int)a6,(int)a7));}
 for(;i<N;i++)out[i]=get(x[i]);_mm_sfence();}

CompilationN/AN/ACompile OKScore: N/A

Testcase #1513.423 ms575 MB + 220 KBAcceptedScore: 100


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