QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#729548 | #9406. Triangle | Wuyanru | WA | 0ms | 52944kb | C++14 | 4.0kb | 2024-11-09 17:20:09 | 2024-11-09 17:20:09 |
Judging History
answer
#include<bits/stdc++.h>
#define inf 0x3f3f3f3f3f3f3f3fll
#define debug(x) cerr<<#x<<"="<<x<<endl
using namespace std;
using ll=long long;
using ld=long double;
using pli=pair<ll,int>;
using pi=pair<int,int>;
template<typename A>
using vc=vector<A>;
using pl=pair<ll,ll>;
inline int read()
{
int s=0,w=1;char ch;
while((ch=getchar())>'9'||ch<'0') if(ch=='-') w=-1;
while(ch>='0'&&ch<='9') s=s*10+ch-'0',ch=getchar();
return s*w;
}
inline ll lread()
{
ll s=0,w=1;char ch;
while((ch=getchar())>'9'||ch<'0') if(ch=='-') w=-1;
while(ch>='0'&&ch<='9') s=s*10+ch-'0',ch=getchar();
return s*w;
}
const int mod1=998244853,mod2=1000000009;
const int base=1145141;
int son[300005][26];
int num[300005];//这个节点对应多少字符串
int sa[600005],rk[1200005];
int num1[600005],num2[600005];
int tmp[600005],cnt[600005];
ll p1[600005],p2[600005];
ll h1[600005],h2[600005];
ll ff[600005],cc[600005];
int ed[300005],id[300005];
int hi[600005],st[21][600005];
char t[600005];
char s[300005];
int c,tot,n,rt;
ll ans;
inline void clear()
{
c=tot=rt=0;
}
inline int ins(int &p,char *s)
{
if(!p) p=++tot,num[p]=0;
if(!s[0]){ num[p]++;return p;}
t[++c]=s[0];return ins(son[p][s[0]-'a'],s+1);
}
inline void SA()
{
int lim=max(27,c);
for(int i=1;i<=c;i++) rk[i]=t[i]=='$'?1:t[i]-'a'+2;
for(int i=c+1;i<=2*c;i++) rk[i]=0;
for(int P=0;(2<<P)<=c;P++)
{
memset(cnt,0,sizeof(int)*(lim+2));
for(int i=1;i<=c;i++) num1[i]=rk[i],num2[i]=rk[i+(1<<P)],cnt[num2[i]+1]++;
for(int i=1;i<=lim;i++) cnt[i]+=cnt[i-1];
for(int i=1;i<=c;i++) tmp[++cnt[num2[i]]]=i;
memset(cnt,0,sizeof(int)*(lim+2));
for(int i=1;i<=c;i++) cnt[num1[i]+1]++;
for(int i=1;i<=lim;i++) cnt[i]+=cnt[i-1];
for(int i=1;i<=c;i++) sa[++cnt[num1[tmp[i]]]]=tmp[i];
for(int i=1;i<=c;i++)
{
int p=sa[i-1],q=sa[i];
if(num1[p]==num1[q]&&num2[p]==num2[q]) rk[q]=rk[p];
else rk[q]=rk[p]+1;
}
}
// printf("%s c=%d\n",t+1,c);
// for(int i=1;i<=c;i++) printf("%2d%c",sa[i]," \n"[i==c]);
for(int i=1,j=0;i<=c;i++)
{
if(j) j--;
int p=sa[rk[i]-1];
while(t[i+j]==t[p+j]) j++;
hi[rk[i]]=j;
}
for(int i=1;i<=c;i++) st[0][i]=hi[i];
for(int j=1;(1<<j)<=c;j++) for(int i=1;i+(1<<j)-1<=c;i++) st[j][i]=min(st[j-1][i],st[j-1][i+(1<<(j-1))]);
}
inline int get(int w1,int w2)
{
if(w1==w2) return c-w1+1;
w1=rk[w1],w2=rk[w2];if(w1>w2) swap(w1,w2);
int num=31-__builtin_clz(w2-w1);
return min(st[num][w1+1],st[num][w2-(1<<num)+1]);
}
inline bool check(int l1,int r1,int l2,int r2)
{
//是否有 t[l1,r1] > t[l2,r2]
int lcp=get(l1,l2);
if(l1+lcp<=r1&&l2+lcp<=r2) return t[l1+lcp]>t[l2+lcp];
return l1+lcp<=r1;
}
inline void solve()
{
n=read();
for(int i=1;i<=n;i++)
{
scanf("%s",s+1);t[++c]='$';
id[i]=ins(rt,s+1),ed[i]=c;
}
t[c+1]=0;SA();
p1[0]=p2[0]=1;
for(int i=1;i<=c;i++) p1[i]=p1[i-1]*base%mod1,p2[i]=p2[i-1]*base%mod2;
int len=-1;ll now1=0,now2=0;
for(int i=c;i;i--)
{
if(t[i]=='$') len=-1,now1=now2=h1[i]=h2[i]=0;
else
{
len++;
h1[i]=now1=(now1+p1[len]*(t[i]-'a'+1))%mod1;
h2[i]=now2=(now2+p2[len]*(t[i]-'a'+1))%mod2;
}
}
map<pl,int>vis;int P=0;
for(int i=1;i<=c;i++)
{
int p=sa[i];ff[p]=cc[p]=0;
if(t[p]=='$') continue;
if(t[p-1]=='$') P++,vis[pl(h1[p],h2[p])]++;
ff[p]=P-vis[pl(h1[p],h2[p])];
}
for(int i=1;i<=c;i++)
{
int p=sa[i];
if(t[p]!='$') cc[p]=vis[pl(h1[p],h2[p])];
}
// for(int i=1;i<=c;i++) printf("%lld%c",ff[i]," \n"[i==c]);
// for(int i=1;i<=c;i++) printf("%lld%c",cc[i]," \n"[i==c]);
ans=0;ll v1=0,v2=0;
for(int i=1;i<=n;i++)//枚举x
{
int now=rt,st=ed[i-1]+2,ed=::ed[i];
for(int j=st;j<ed;j++)
{
now=son[now][t[j]-'a'];
ans+=(ll)num[now]*max(0ll,ff[st]-ff[j+1]-cc[j+1]);
if(check(st,j,j+1,ed)) ans-=(ll)num[now]*(num[now]+1)/2;
}
int f=ff[st],c=cc[st];
v1+=(ll)(c-1)*(c-2)/2;
v2+=(ll)(c-1)*f;
}
ans+=v1/3+v2/2;
printf("%lld\n",ans);
}
int main()
{
int T=read();
while(T--) clear(),solve();
return 0;
}
/*
1
6
cbaa
cb
cb
cbaa
ba
ba
*/
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 0ms
memory: 44772kb
input:
3 6 cbaa cb cb cbaa ba ba 3 sdcpc sd cpc 1 ccpc
output:
16 0 0
result:
ok 3 lines
Test #2:
score: -100
Wrong Answer
time: 0ms
memory: 52944kb
input:
14 1 lfpbavjsm 2 pdtlkfwn mbd 3 dvqksg dvqksg uhbhpyhj 4 ombwb ombwb ombwb ombwb 5 oztclz oztclz oztclz oztclz kul 6 vco vco vco dtktsqtinm vco vco 7 tb tb kowbsu ygkfphcij tb uvei tb 8 vxxtxssht abnsxbf bydaae bydaae udalyvmcef bydaae bydaae bydaae 9 aaze zvyonw qjfv mmhkef qjfv qjfv qjfv mmhkef qj...
output:
0 0 0 4 10 20 9 20 35 17 107 77 18 16775
result:
wrong answer 7th lines differ - expected: '10', found: '9'