QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#440172 | #7008. Rikka with Nice Counting Striking Back | crsfaa | AC ✓ | 6479ms | 52192kb | C++14 | 6.2kb | 2024-06-13 11:09:40 | 2024-06-13 11:09:40 |
Judging History
answer
#include<bits/stdc++.h>
#define Misaka namespace
#define Mikoto std
using Misaka Mikoto;
int read()
{
int s=0;
char ch=getchar();
while(ch<'0'||ch>'9') ch=getchar();
while(ch>='0'&&ch<='9') s=s*10+ch-'0',ch=getchar();
return s;
}
const int mxn=4e5+5;
struct SA
{
#define f(i,a,b) for(i=a;i<=b;i++)
#define e(i,a,b) for(i=a;i>=b;i--)
int sa[mxn],height[mxn];
int cnt[mxn],rk[mxn];
int preN;
void S_sort(char s[],int l,int m)
{
int i,k,ct;
f(i,1,l) cnt[height[i]=s[i]]++;
f(i,2,m) cnt[i]+=cnt[i-1];
e(i,l,1) sa[cnt[height[i]]--]=i;
for(k=1;k<=l;k*=2)
{
ct=0;
f(i,l-k+1,l) rk[++ct]=i;
f(i,1,l)
if(sa[i]>k)
rk[++ct]=sa[i]-k;
f(i,1,m) cnt[i]=0;
f(i,1,l) cnt[height[i]]++;
f(i,2,m) cnt[i]+=cnt[i-1];
e(i,l,1) sa[cnt[height[rk[i]]]--]=rk[i],rk[i]=0;
// swap(height,rk);
f(i,1,l) swap(height[i],rk[i]);
height[sa[1]]=m=1;
f(i,2,l)
height[sa[i]]=m=m+!(rk[sa[i]]==rk[sa[i-1]]&&rk[sa[i]+k]==rk[sa[i-1]+k]);
if(m==l) return;
}
}
void build(char s[],int l,int m)
{
memset(cnt,0,preN);
memset(rk,0,preN);
S_sort(s,l,m);
int i,j,k;
for(i=1;i<=l;i++)
rk[sa[i]]=i;
for(i=1,k=0;i<=l;i++)
{
if(rk[i]==1) k=0;
else
{
k-=k>0;
int j=sa[rk[i]-1];
while(i+k<=l&&j+k<=l&&s[i+k]==s[j+k])
k++;
}
height[rk[i]]=k;
}
preN=max(l,m)+1<<2;
}
};
struct LCP_SA
{
struct Word_RMQ
{
#define w 32
#define u unsigned
#define be(x) ((x>>5)+1)
#define bm mxn/w+5
int *a;
int ST[int(log2(bm)+1)][bm];
int lg[bm];
int l[bm],r[bm];
struct node
{
u st[w+1];
int *p;
int ask(int l,int r)
{
return p[l+__builtin_ctz(st[r]>>l-1)];
}
void init(int *p_,int len)
{
p=p_;
int i,top=0;
int stack[w+1];
for(i=1;i<=len;i++)
{
st[i]=st[i-1];
while(top&&p[i]<p[stack[top]])
st[i]-=1u<<stack[top--]-1;
stack[++top]=i,st[i]+=1u<<i-1;
}
}
}k[bm];
void build(int A[],int n)
{
a=A;
int i,j,cnt=0,mn;
for(i=0;i<=n;i+=w)
l[++cnt]=i,r[cnt]=min(n,i+w-1);
l[1]=1;
for(i=1;i<=cnt;i++)
{
k[i].init(a+l[i]-1,r[i]-l[i]+1);
mn=INT_MAX;
for(j=l[i];j<=r[i];j++)
mn=min(mn,a[j]);
ST[0][i]=mn;
}
for(j=1;(1<<j)<=cnt;j++)
for(i=1;i+(1<<j)-1<=cnt;i++)
ST[j][i]=min(ST[j-1][i],ST[j-1][i+(1<<(j-1))]);
for(i=2;i<=cnt;i++)
lg[i]=lg[i>>1]+1;
}
inline int STask(int L,int R)
{
if(L>R) return INT_MAX;
int k=lg[R-L+1];
return min(ST[k][L],ST[k][R-(1<<k)+1]);
}
inline int ask(int L,int R)
{
if(L>R) return INT_MAX;
if(be(L)==be(R)) return k[be(L)].ask(L-l[be(L)]+1,R-l[be(L)]+1);
return min({STask(be(L)+1,be(R)-1),ask(L,r[be(L)]),ask(l[be(R)],R)});
}
}ds;
SA a;
int L;
void build(char s[],int l,int m)
{
a.build(s,l,m);
ds.build(a.height,l);
}
inline int asklcp(int x,int y)
{
if(x==y) return L-x+1;
x=a.rk[x],y=a.rk[y];
if(x>y) swap(x,y);
return ds.ask(x+1,y);
}
#undef w
}a,b;
char s[mxn];
int t[mxn];
struct node
{
int l,r,p;
}res[mxn*20],ans[mxn*20];
using ull=unsigned long long;
using ll=long long;
ull pre[mxn],mul[mxn];
ll pre2[mxn],mul2[mxn];
int n;
const ull up=23333333333333;
const int mod=1e9+9;
ull ask0(int l,int r)
{
return mul2[n-r]*(pre2[r]-pre2[l-1]+mod)%mod;
}
ull ask(int l,int r)
{
return mul[n-r]*(pre[r]-pre[l-1])+ask0(l,r);
}
int main()
{
int T=read();
while(T--)
{
int n,i,j,k,cnt=0;
scanf("%s",s+1),n=strlen(s+1);
::n=n;
a.build(s,n,'z');
reverse(s+1,s+1+n);
b.build(s,n,'z');
reverse(s+1,s+1+n);
for(i=mul[0]=mul2[0]=1;i<=n;i++)
mul[i]=mul[i-1]*up,pre[i]=pre[i-1]+mul[i]*s[i],
mul2[i]=mul2[i-1]*161%mod,pre2[i]=(pre2[i-1]+mul2[i]*s[i])%mod;
// cerr<<clock()<<endl;
int ticnt=0;
for(i=1;i<=n/2;i++)
{
int L=-1,R=-1;
for(j=i;j+i<=n;j+=i)
{
j=max(j,R/i*i);
while(j<=R) j+=i;
int lcp,lcs;
for(lcp=1;lcp<=i&&s[j+lcp-1]==s[j+i+lcp-1];lcp++);
lcp--;
for(lcs=1;lcs<i&&s[j-1-lcs+1]==s[j+i-1-lcs+1];lcs++);
lcs--;
ticnt+=lcp+lcs;
//[i-lcs,i+lcp-1]
int l=j-lcs,r=j+lcp-1;
if(L==-1) L=l,R=r;
else if(l<=R+1)
R=r;
else
{
if(R-L+1>=i)
res[++cnt]=node({L,R+i,i});
L=l,R=r;
}
}
if(L!=-1&&R-L+1>=i)
res[++cnt]=node({L,R+i,i});
if(ticnt>n*100) break;
}
// cerr<<i<<' '<<clock()<<endl;
// cerr<<clock()<<endl;
for(;i<=n/2;i++)
{
int L=-1,R=-1;
for(j=i;j+i<=n;j+=i)
{
j=max(j,R/i*i);
while(j<=R) j+=i;
int lcp=a.asklcp(j,j+i);
int lcs=b.asklcp(n-(j-1)+1,n-(j+i-1)+1);
//[i-lcs,i+lcp-1]
int l=j-lcs,r=j+lcp-1;
if(L==-1) L=l,R=r;
else if(l<=R+1)
R=r;
else
{
if(R-L+1>=i)
res[++cnt]=node({L,R+i,i});
L=l,R=r;
}
}
if(L!=-1&&R-L+1>=i)
res[++cnt]=node({L,R+i,i});
}
for(i=1;i<=cnt;i++)
t[res[i].r]++;
for(i=1;i<=n;i++)
t[i]+=t[i-1];
for(i=cnt;i;i--)
ans[t[res[i].r]--]=res[i];
memset(t,0,n+1<<2);
for(i=1;i<=cnt;i++)
t[ans[i].l]++;
for(i=1;i<=n;i++)
t[i]+=t[i-1];
for(i=cnt;i;i--)
res[t[ans[i].l]--]=ans[i];
memset(t,0,n+1<<2);
int acnt=0;
for(i=1;i<=cnt;i=j)
{
int mn=2e9;
for(j=i;j<=n&&res[j].l==res[i].l&&res[j].r==res[i].r;j++)
mn=min(mn,res[j].p);
ans[++acnt]=node({res[i].l,res[i].r,mn});
}
unordered_set<ull> st;
ll s=1ll*n*(n+1)/2;
for(i=2;i<=n;i++)
s-=a.a.height[i];
// cout<<s<<endl;
// for(i=1;i<=acnt;i++)
// cout<<ans[i].l<<' '<<ans[i].r<<' '<<ans[i].p<<endl;
for(i=1;i<=acnt;i++)
for(j=ans[i].l;j<=ans[i].l+ans[i].p;j++)
for(k=j+ans[i].p*2-1;k<=ans[i].r;k+=ans[i].p)
st.insert(ask(j,k));
printf("%lld\n",s-st.size());
}
// freopen("out","w",stdout);
}
这程序好像有点Bug,我给组数据试试?
详细
Test #1:
score: 100
Accepted
time: 0ms
memory: 28592kb
input:
6 rikkasuggeststoallthecontestants thisisaproblemdesignedforgrandmasters ifyoudidnotachievethat youdbetterskiptheproblem wishyouahighrank enjoytheexperience
output:
500 679 244 290 132 163
result:
ok 6 lines
Test #2:
score: 0
Accepted
time: 3883ms
memory: 48400kb
input:
1000 mxsslrrxtwwnwlsrxxxbsmtxxwwntsbrwlxnbxunutnlmrrlubturuwmbnobrolwunwurtbnmlumxmwtwrsulruxslsnltsstmmxbsmuonroubrnnmu sssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssss glggglgg yykkyyyykykykyyyykyykykkkyyyykkyykkkyyykykyykyyykkykky...
output:
6522 1 20 9443 11294 1 8619 26 7898 260905 9048 6 22114 52 20 45 7 39 10 1 28 26 10 47 32 12977 30 13 7473 12 8402 1 8083 16 1 10462 16 9278 1 1 8968 7858 11148 8130 6 8516 12223 9266 8374 26 45 14 10150 9 10175 298758 203677 11522 12 8985 10687 12 1 6613277686 10 10 1 5794 28 1 280529 7874 13 10564...
result:
ok 1000 lines
Test #3:
score: 0
Accepted
time: 6366ms
memory: 51900kb
input:
26 hihhihihhihhihihhihihhihhihihhihhihihhihihhihhihihhihihhihhihihhihhihihhihihhihhihihhihhihihhihihhihhihihhihihhihhihihhihhihihhihihhihhihihhihihhihhihihhihhihihhihihhihhihihhihhihihhihihhihhihihhihihhihhihihhihhihihhihihhihhihihhihhihihhihihhihhihihhihihhihhihihhihhihihhihihhihhihihhihihhihhihihh...
output:
9523687993 9529757593 9537405289 9539347561 9533035177 9528058105 564250 9522959641 9542382361 9518953705 9519439273 9534977449 9519803449 9535705801 9519560665 9534249097 9527572537 9523687993 9539468953 9532064041 9525873049 9532185433 9541168441 9524901913 9531092905 9518832313
result:
ok 26 lines
Test #4:
score: 0
Accepted
time: 6479ms
memory: 52192kb
input:
26 oohooohoohooohoohooohoohoohooohoohooohoohoohooohoohooohoohoohooohoohooohoohooohoohoohooohoohooohoohoohooohoohooohoohooohoohoohooohoohooohoohoohooohoohooohoohooohoohoohooohoohooohoohoohooohoohooohoohoohooohoohooohoohooohoohoohooohoohooohoohoohooohoohooohoohooohoohoohooohoohooohoohoohooohoohooohooh...
output:
9625902365 9628810517 9622671085 9626467839 9628891299 9626306275 9630668503 9620409189 9618228075 9622428739 9628406607 9625336891 9630426157 9626871749 9620086061 9626144711 9616935563 9617177909 9626790967 9627194877 9626467839 354971 9616370089 9618308857 9617824165 9616773999
result:
ok 26 lines
Test #5:
score: 0
Accepted
time: 3317ms
memory: 45000kb
input:
26 vggvgvggvgvvgvggvgvggvgvvggvgvggvgvvgvggvgvggvgvvgvggvgvvggvgvggvgvvgvggvgvggvgvvgvggvgvggvgvvggvgvggvgvvgvggvgvggvgvvgvggvgvvggvgvggvgvvgvggvgvggvgvvggvgvggvgvvgvggvgvggvgvvgvggvgvggvgvvggvgvggvgvvgvggvgvggvgvvgvggvgvvggvgvggvgvvgvggvgvggvgvvggvgvggvgvvgvggvgvggvgvvgvggvgvvggvgvggvgvvgvggvgvggvg...
output:
13085098340 13073668733 13071665606 13067070197 13077910649 13074964874 13078735466 13070840789 13072726085 13067895014 669720 13065891887 13065302732 13076496677 13068484169 13064242253 13065420563 13063181774 13080502931 13067070197 13072490423 13070015972 13083802199 13064831408 13075671860 13085...
result:
ok 26 lines