QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#715029 | #9406. Triangle | daduoli | WA | 27ms | 59040kb | C++14 | 4.2kb | 2024-11-06 10:00:41 | 2024-11-06 10:00:42 |
Judging History
answer
#include<bits/stdc++.h>
#define Yzl unsigned long long
#define fi first
#define se second
#define pi pair<int,int>
#define mp make_pair
#define lob lower_bound
#define pb emplace_back
typedef long long LL;
using namespace std;
const Yzl Lty=20120712;
const int MAXN=6e5+10;
int n,cnt;
char ch[MAXN];
int m,sa[MAXN],x[MAXN],y[MAXN],c[MAXN],st[20][MAXN],logg[MAXN];
int tlen[MAXN];
bool pd[MAXN];
void tsort(int *sa,int *x,int *y) {
for(int i=1;i<=m;++i) c[i]=0;
for(int i=1;i<=cnt;++i) ++c[x[i]];
for(int i=1;i<=m;++i) c[i]+=c[i-1];
for(int i=cnt;i;--i) sa[c[x[y[i]]]--]=y[i],y[i]=0;
}
void SA() { m=130;
for(int i=1;i<=cnt;++i) x[i]=ch[i],y[i]=i;
tsort(sa,x,y);
for(int k=1;k<=cnt;k<<=1) {
int num=0;
for(int i=cnt-k+1;i<=cnt;++i) y[++num]=i;
for(int i=1;i<=cnt;++i) if(sa[i]>k) y[++num]=sa[i]-k;
tsort(sa,x,y); swap(x,y);
x[sa[1]]=num=1;
for(int i=2;i<=cnt;++i) {
x[sa[i]]=(y[sa[i]]==y[sa[i-1]]&&y[sa[i]+k]==y[sa[i-1]+k]?num:++num);
}
if(num==cnt) break;
m=num;
}
}
int h[MAXN],rk[MAXN];
void get_height() {
int k=0;
for(int i=1;i<=cnt;++i) rk[sa[i]]=i;
for(int i=1;i<=cnt;++i) {
if(rk[i]==1) continue;
if(k) --k;
int j=sa[rk[i]-1];
while(i+k<=cnt&&j+k<=cnt&&ch[i+k]==ch[j+k]) ++k;
h[rk[i]]=k;
}
}
void init() {
for(int i=2;i<=cnt;++i) logg[i]=logg[i/2]+1;
for(int i=1;i<=cnt;++i) st[0][i]=h[i];
for(int i=1;i<=logg[cnt];++i) {
int R=cnt-(1<<i)+1;
for(int j=1;j<=R;++j)
st[i][j]=min(st[i-1][j],st[i-1][j+(1<<(i-1))]);
}
}
int query(int l,int r) {
int k=logg[r-l+1];
return min(st[k][l],st[k][r-(1<<k)+1]);
}
vector<int> e[MAXN];
int sum[MAXN],rt[MAXN];
struct SGT {
int ls[MAXN*40],rs[MAXN*40],lb[MAXN*40],cnt;
LL tr[MAXN*40];
LL query(int u,int l,int r,int x,int y,int z) {
if(l>=x&&r<=y) return tr[u]+(LL)z*(sum[r]-sum[l-1]);
int mid=(l+r)>>1; LL sum=0;
if(x<=mid) sum+=query(ls[u],l,mid,x,y,z+lb[u]);
if(y>mid) sum+=query(rs[u],mid+1,r,x,y,z+lb[u]);
return sum;
}
void psup(int u,int l,int r) {
tr[u]=(LL)lb[u]*(sum[r]-sum[l-1]);
if(ls[u]) tr[u]+=tr[ls[u]];
if(rs[u]) tr[u]+=tr[rs[u]];
}
void update(int &u,int pre,int l,int r,int x,int y) {
u=++cnt; ls[u]=ls[pre]; rs[u]=rs[pre]; lb[u]=lb[pre];
if(l>=x&&r<=y) {
++lb[u]; psup(u,l,r);
return ;
} int mid=(l+r)>>1;
if(x<=mid) update(ls[u],ls[pre],l,mid,x,y);
update(rs[u],rs[pre],mid+1,r,x,y);
psup(u,l,r);
}
}T;
int op,TT;
void vmain() {
scanf("%d",&n);
++op;
for(int i=1;i<=n;++i) { string st; cin>>st; int len=st.size();
int sta=cnt+1; pd[sta]=1;
tlen[sta]=len;
for(int j=0;j<len;++j) ch[++cnt]=st[j],e[j+1].pb(sta);
ch[++cnt]='A';
if(op==14) {
cout<<st<<endl;
}
} SA(); get_height(); init(); LL ans=0;
// for(int i=1;i<=cnt;++i) {
// cout<<h[i]<<" ";
// }
// for(int i=1;i<=cnt;++i) cout<<sa[i]<<" ";
// for(int i=1;i<=cnt;++i) {
// cout<<ch[i]<<" ";
// }
for(int i=1;i<=cnt;++i) sum[i]=pd[sa[i]]+sum[i-1];
// for(int i=1;i<=cnt;++i) {
// cout<<tlen[i]<<" ";
// cout<<sum[i]<<' ';
// }
// cout<<query(11,11)<<" ";
for(int i=1;i<=cnt;++i) {
int len=e[i].size(); T.cnt=0;
sort(e[i].begin(),e[i].end(),[&](int a,int b){return rk[a]<rk[b];});
for(int j=0;j<len;++j) {
int t=e[i][j];
T.update(rt[j],(j?rt[j-1]:0),1,cnt,rk[t+i],cnt);
// if(i==2) {
// cout<<t<<" "<<T.query(rt[j],1,cnt,1,cnt,0)<<"\n";
// }
}
// if(i==2) puts("");
// if(i==2)
// cout<<T.query(rt[len-1],1,cnt,1,cnt,0)<<endl;
for(int j=0;j<len;++j) { int t=e[i][j];
// if(i==2) {
// cout<<t<<' '<<tlen[t]<<endl;
// }
if(tlen[t]!=i) continue;
int l=j,r=len,mid;
while(l+1<r) { mid=(l+r)>>1;
// if(i==2) {
// cout<<t<<":"<<l<<' '<<mid<<' '<<r<<" "<<query(t+1,e[i][mid])<<endl;
// }
if(query(rk[t]+1,rk[e[i][mid]])>=i) l=mid;
else r=mid;
}
if(l==j) continue;
// if(i==2) {
// cout<<j<<" "<<l<<endl;
// }
ans+=T.query(rt[l],1,cnt,1,rk[t]-1,0)-T.query(rt[j],1,cnt,1,rk[t]-1,0);
}
// cout<<i<<" "<<ans<<endl;
}
if(TT<4)
printf("%lld\n",ans);
for(int i=1;i<=cnt+1;++i) {
pd[i]=0; tlen[i]=h[i]=rk[i]=sa[i]=x[i]=y[i]=c[i]=0;
e[i].clear(); sum[i]=0;
}
}
int main () {
int T=1; scanf("%d",&T); TT=T; while(T--) vmain();
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 4ms
memory: 48884kb
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: 27ms
memory: 59040kb
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:
o cyt cyt rfhgomg cyt cyt k cyt kwvd cteyfpl yiglr cteyfpl sryq vgxod cyt vjzuvwsj o cyt cteyfpl cyt rc cyt cyt zaczhqq bghogpzh bppygqqyqf unx sdhfsay sdhfsay cteyfpl cteyfpl cteyfpl onvw kwvd hfxucu cteyfpl uhivp cyt zaczhqq tvifsf cyt cteyfpl fdiajuuc cyt hvcph cyt bftskm mgpooqys ytz sojcnzuxq s...
result:
wrong answer 1st lines differ - expected: '0', found: 'o'