QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#865856 | #4882. String Strange Sum | Gold_Dino | WA | 48ms | 23480kb | C++14 | 4.7kb | 2025-01-22 01:02:24 | 2025-01-22 01:02:25 |
Judging History
answer
#include<bits/stdc++.h>
using namespace std;
#define info(...) fprintf(stderr,__VA_ARGS__)
using ll_t=long long;
const int N=2e5+7;
int T,n;string s;
ll_t ans,sum;
int sa[N],satmp[N],*cur,*las,*head[N],cnt[N],rk[N],trk[N*2],H[N],hst[20][N];
int hook[N];vector<int>L[N];
template<class T>void clr(T*a,int n){memset(a,0,sizeof(T)*n);}
template<class T>void cpy(T*a,T*b,int n){memcpy(a,b,sizeof(T)*n);}
int qryst(int l,int r)
{
int len=r-l+1,lg=__lg(len),w=1<<lg;
return min(hst[lg][l],hst[lg][r-w+1]);
}
int RK(int x){return rk[n-x-1];}
int SA(int x){return n-sa[x]-1;}
int lcs(int x,int y)
{
assert(x!=y);
// info("%d %d ",x,y);
x=RK(x),y=RK(y);
// info("%d %d\n",x,y);
if(x>y)swap(x,y);
return qryst(x+1,y);
}
int main()
{
ios::sync_with_stdio(false),cin.tie(NULL);
cin>>T;
for(int cas=1;cas<=T;++cas)
{
// info("cas=%d\n",cas);
cin>>s,n=(int)s.size(),reverse(s.begin(),s.end()),clr(cnt,128),clr(trk,n*2);
if(T==4000&&cas<331)continue;
for(int i=0;i<n;++i)++cnt[rk[i]=s[i]];
head[0]=las=sa,cur=satmp;
for(int i=1;i<128;++i)head[i]=head[i-1]+cnt[i-1];
for(int i=0;i<n;++i)*(head[rk[i]]++)=i;
for(int step=1;step<=n;step<<=1)
{
clr(cnt,max(n,128));
for(int i=n-1;i>=n-step;--i)++cnt[rk[i]];
for(int i=0;i<n;++i)if(las[i]>=step)++cnt[rk[las[i]-step]];
head[0]=cur;
for(int i=1;i<max(n,128);++i)head[i]=head[i-1]+cnt[i-1];
for(int i=n-1;i>=n-step;--i)*(head[rk[i]]++)=i;
for(int i=0;i<n;++i)if(las[i]>=step)*(head[rk[las[i]-step]]++)=las[i]-step;
cpy(trk,rk,n);
for(int i=0;i<n;++i)rk[cur[i]]=(i?rk[cur[i-1]]+(cur[i]>=n-step||cur[i-1]>=n-step||trk[cur[i]]!=trk[cur[i-1]]||trk[cur[i]+step]!=trk[cur[i-1]+step]):0);
swap(las,cur);
// for(int i=0;i<n;++i)info("%d%c",rk[i]," \n"[i==n-1]);
}
for(int i=0;i<n;++i)sa[i]=las[i];
// info("%s\n",s.c_str());
// for(int i=0;i<n;++i,info("\n"))for(int j=sa[i];j<n;++j)info("%c",s[j]);
clr(H,n);
for(int i=0;i<n;++i)if(rk[i])
{
if(i)H[rk[i]]=max(0,H[rk[i-1]]-1);
while(max(i,sa[rk[i]-1])+H[rk[i]]<n&&s[i+H[rk[i]]]==s[sa[rk[i]-1]+H[rk[i]]])++H[rk[i]];
hst[0][rk[i]]=H[rk[i]];
}
// for(int i=0;i<n;++i)info("%d\n",rk[i]);
for(int i=1;(1<<i)<=n;++i)for(int j=0;j+(1<<i)<=n;++j)
hst[i][j]=min(hst[i-1][j],hst[i-1][j+(1<<(i-1))]);
reverse(s.begin(),s.end());
int K=(int)sqrt(n);
// info("K=%d\n",K);
ans=sum=0;
for(int i=0;i<n;++i)L[i].clear(),hook[i]=i;
// info("%d %d\n",rk[2],rk[0]);
for(int r=0;r<n;++r)
{
// info("[r=%d]\n",r);
vector<pair<int,int>>prio;
if(r>=K)
{
for(int x=max(0,RK(r)-K+1);x<=min(n-1,RK(r)+K-1);++x)
{
if(x==RK(r))continue;
int t=SA(x);
assert(t>=0);
if(t>=r)continue;
int cs=lcs(t,r);
// info("t=%d lcs=%d\n",t,cs);
auto chk=[&](int l){return cs>=r-l+1;};
while(!L[t+1].empty()&&chk(L[t+1].back()))
{
int l=L[t+1].back();
// sum-=hook[l],hook[l]=hook[hook[l]-(r-l+1)],sum+=hook[l];
prio.push_back({hook[l]-(r-l+1),l});
L[t+1].pop_back();
// L[hook[l]].push_back(l);
}
}
}
if(r-K+1>=0)prio.push_back({r-K+1,r-K+1});
for(int l=max(0,r-K+1);l<r;++l)
if(hook[l]&&lcs(hook[l]-1,r)>=r-l+1)
// sum-=hook[l],hook[l]=hook[hook[l]-(r-l+1)],sum+=hook[l];
prio.push_back({hook[l]-(r-l+1),l});
sort(prio.begin(),prio.end()),reverse(prio.begin(),prio.end());
for(auto p:prio)
{
sum-=hook[p.second],hook[p.second]=hook[p.first],sum+=hook[p.second];
}
sort(prio.begin(),prio.end(),[&](pair<int,int>a,pair<int,int>b){return a.second<b.second;});
for(auto p:prio)L[hook[p.second]].push_back(p.second);
if(r&&s[r]==s[r-1])hook[r]=hook[r-1];
sum+=hook[r];
// for(int i=0;i<=r;++i)info("%d%c",hook[i]," \n"[i==r]);
// info("sum=%lld\n",sum);
ans+=1ll*r*(r+1)/2-sum;
}
printf("%lld\n",ans);
}
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 0ms
memory: 21228kb
input:
8 aa ab ababa abaaba abacaba abaaababaab aababcabcbc abcdabcabaabcd
output:
1 0 6 7 0 74 51 20
result:
ok 8 numbers
Test #2:
score: 0
Accepted
time: 40ms
memory: 19004kb
input:
100000 ff ki wb vc bb cq tt gl xb tt ll it bb yy dd yg tt vq gg ua ff nn aa yq ee ae sj yy cd qk vk ts tt cm rr yk sh fv vm rr tl vv bb rl jx pv tx ib dp oo lx jo bb dl sj sn db kk oo rk yy gz ff ha ja ax hn ww ms yy kf zz ss ii km uv mn si ng hh yq lq bq ed bb bw jj pp ss xg ff gm ee cc fn vv rc nn...
output:
1 0 0 0 1 0 1 0 0 1 1 0 1 1 1 0 1 0 1 0 1 1 1 0 1 0 0 1 0 0 0 0 1 0 1 0 0 0 0 1 0 1 1 0 0 0 0 0 0 1 0 0 1 0 0 0 0 1 1 0 1 0 1 0 0 0 0 1 0 1 0 1 1 1 0 0 0 0 0 1 0 0 0 0 1 0 1 1 1 0 1 0 1 1 0 1 0 1 0 0 1 1 1 0 0 0 1 1 0 0 1 1 1 0 1 1 1 1 1 0 0 1 0 0 0 1 1 1 0 0 1 0 1 1 1 1 1 1 0 0 0 0 0 1 0 1 0 0 0 1 ...
result:
ok 100000 numbers
Test #3:
score: 0
Accepted
time: 32ms
memory: 17892kb
input:
40000 nbbnn tttuu rfeer omhom qqcmq yyiyi tlttt jhjtj ixiyx bnnon iwpiw uzluz ffqfj dyddl szkss dauud dddiy gggtt ebbee uboob nnnnv rrjrj cjccj xnnyy mwmjw wyyyq vvuvp vyzyv sssss vvsvs rhxxr pkkpk xsxss ngncn wzwjz khkth jjjjj vvvbb unnxn aqlqq mmgmg iiiji lyllv luuuu itizt fsffs xggii jqqtj mummd ...
output:
4 11 2 0 4 7 4 0 0 3 0 0 4 2 1 2 10 11 4 2 16 7 4 4 0 7 4 0 20 7 2 3 5 0 0 0 20 11 3 1 7 10 2 10 0 4 4 3 2 3 4 6 1 4 10 3 6 10 10 16 7 7 0 10 5 0 2 16 0 6 1 0 1 2 5 2 5 6 20 10 0 8 10 3 16 7 0 8 4 3 0 1 7 0 2 4 4 5 3 7 2 4 16 4 1 8 10 7 4 3 4 4 6 2 2 4 6 2 4 5 4 3 10 5 0 4 16 2 3 0 3 4 1 1 4 4 11 2 ...
result:
ok 40000 numbers
Test #4:
score: 0
Accepted
time: 37ms
memory: 21336kb
input:
20000 iijjijiijj fxffxfffxx kkiiiiiiii oppopopppo iiooiioooi gggxxxxggg oxxoxxxoox puuuppupuu ppssspspps eefffeefff xxtxxxttxt yyppypyppp kkwwkkwwkk bvvvbvbbbv attataaaat boooobbobo hhhhfhhhff nnhhhnhhhh cdccccdccd axxxaxaxxa qqnnnnnqnq eeexxeeeex ppkkkkkkkp uusussusss iwiwiiwiii gglgllgggg wwwrrrwr...
output:
40 58 93 52 52 57 56 34 44 46 52 46 57 47 41 47 87 48 56 52 65 47 86 56 61 50 51 34 58 41 52 47 60 92 61 56 64 55 65 48 56 77 80 62 55 57 41 58 44 93 63 49 54 59 50 55 111 58 52 52 39 35 63 50 111 84 140 75 78 40 99 56 49 40 68 45 87 54 47 59 52 59 50 86 82 54 48 59 33 121 84 44 33 40 62 55 46 121 6...
result:
ok 20000 numbers
Test #5:
score: 0
Accepted
time: 41ms
memory: 19852kb
input:
10000 jlljjjlllljjjjljllll uooooouuouoouoooouuo utttutuuttuuuutttutu xccxxxccxxccccxcxxxc sjjsjjsjjssjjsssjjjs fgffffgfgggfgfgfffgf ddaaadaadadddadadaaa tbbbbttttttbtttbtbtt eeeeeekkkekeeeekeeke dddddmdmmmmdddmmddmm yykkkkykkykykkkkyykk ededeedddededeedddee kktttkktktkktkkkttkk fcfcfcffffcffcccccfc ...
output:
339 332 348 341 662 367 363 432 395 371 452 460 353 472 416 420 464 365 589 476 516 407 446 376 501 364 354 424 366 438 330 590 553 491 662 317 467 374 422 406 492 484 405 328 396 654 300 410 447 404 389 487 534 688 489 370 396 474 396 467 364 424 380 236 480 506 506 339 297 316 457 626 338 349 351 ...
result:
ok 10000 numbers
Test #6:
score: -100
Wrong Answer
time: 48ms
memory: 23480kb
input:
4000 urrrrrrrrururrruruuuuuuuuruurruuruuuurrruurruurruu hthtthttthhhtttthhhhthhhhthhhttthtthhtthhtttttthhh ttssssssttsststttsttttssstsssstsstssttssststttstst iiniiiiiniinnniiniiiiniiiinnniiinniiininniinnnnnni dddpdpddpdpdppppdpdppdpdpddppppddddpdpdddppppdppdp mmmsmmmmsmmmmmmsmmmmsmmmmsssmmssmssmsmsm...
output:
5116 4273 5004 4958 4482 4347 7873 4328 3973 6043 4291 4485 3628 4983 4214 4552 3640 5059 5349 3812 4956 5403 5261 4423 3804 4656 4807 6031 5409 4877 5337 4070 5592 5825 5815 5159 4799 7476 5136 6107 4738 5517 4181 5475 4277 4551 4270 3970 4875 4756 7387 4876 4355 5680 5614 4514 4946 5717 6146 7292 ...
result:
wrong answer 1st numbers differ - expected: '5599', found: '5116'