QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#865833#4882. String Strange SumGold_DinoWA 1ms19552kbC++144.0kb2025-01-22 00:07:242025-01-22 00:07:24

Judging History

你现在查看的是最新测评结果

  • [2025-01-22 00:07:24]
  • 评测
  • 测评结果:WA
  • 用时:1ms
  • 内存:19552kb
  • [2025-01-22 00:07:24]
  • 提交

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)
{
    x=RK(x),y=RK(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);
        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]]+(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)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(i&&rk[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=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);
        ans=sum=0;
        for(int i=0;i<n;++i)L[i].clear(),hook[i]=i;
        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),cs=lcs(t,r);
                    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);
                    }
                }
            }
            for(int l=max(0,r-K+1);l<r;++l)
                if(hook[l]<r-1&&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];
                if(r-p.second+1>=K)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: 0
Wrong Answer
time: 1ms
memory: 19552kb

input:

8
aa
ab
ababa
abaaba
abacaba
abaaababaab
aababcabcbc
abcdabcabaabcd

output:

1
0
0
7
0
38
19
16

result:

wrong answer 3rd numbers differ - expected: '6', found: '0'