QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#751171 | #9406. Triangle | eastcloud | WA | 0ms | 73860kb | C++17 | 6.1kb | 2024-11-15 17:22:38 | 2024-11-15 17:22:39 |
Judging History
answer
#include<bits/stdc++.h>
#define ll long long
#define pi pair<ll,ll>
#define vi vector<ll>
#define cpy(x,y,s) memcpy(x,y,sizeof(x[0])*(s))
#define mset(x,v,s) memset(x,v,sizeof(x[0])*(s))
#define all(x) begin(x),end(x)
#define fi first
#define se second
#define mp make_pair
#define pb push_back
#define ary array
#define eb emplace_back
#define IL inline
using namespace std;
#define N 2020005
ll read(){
ll x=0,f=1;char ch=getchar();
while(ch<'0' || ch>'9')f=(ch=='-'?-1:f),ch=getchar();
while(ch>='0' && ch<='9')x=(x<<1)+(x<<3)+(ch^48),ch=getchar();
return x*f;
}
void write(ll x){
if(x<0)x=-x,putchar('-');
if(x/10)write(x/10);
putchar(x%10+'0');
}
char s[N],t[N];
ll buc[N],rk[N],ork[N],id[N],R[N];
ll ht[N],ed[N],sa[N],h[23][N];
void get_SA(ll n){
ll m=(1<<7),p=0;
mset(buc,0,max(m+1,n+1));
for(ll i=1;i<=n;i++)buc[rk[i]=t[i]]++;
for(ll i=1;i<=m;i++)buc[i]+=buc[i-1];
for(ll i=n;i>=1;i--)sa[buc[rk[i]]--]=i;
//for(ll i=1;i<=n;i++)cerr<<t[i];cerr<<endl;
for(ll w=1;;w<<=1,m=p,p=0){
for(ll i=n;i>n-w;i--)id[++p]=i;
for(ll i=1;i<=n;i++)if(sa[i]-w>=1)id[++p]=sa[i]-w;
mset(buc,0,m+1);
for(ll i=1;i<=n;i++)buc[R[i]=rk[id[i]]]++;
for(ll i=1;i<=m;i++)buc[i]+=buc[i-1];
for(ll i=n;i>=1;i--)sa[buc[R[i]]--]=id[i];
cpy(ork,rk,n+1);p=0;
for(ll i=1;i<=n;i++){
rk[sa[i]]=(ork[sa[i]]==ork[sa[i-1]] && ork[sa[i]+w]==ork[sa[i-1]+w]?p:++p);
}
if(p==n)break;
}
ll k=0;
for(ll i=1;i<=n;i++){
if(k)k--;
while(t[i+k]==t[sa[rk[i]-1]+k])k++;
ht[rk[i]]=k;h[0][rk[i]]=k;
}
for(ll i=1;i<=__lg(n);i++){
for(ll j=1;j+(1<<i)-1<=n;j++)h[i][j]=min(h[i-1][j],h[i-1][j+(1<<(i-1))]);
}
}
ll ask_min(ll l,ll r){
if(l==r)return N;
l++;ll d=__lg(r-l+1);return min(h[d][l],h[d][r-(1<<d)+1]);
}
ll cnt[N],nex[N][27],suf[N],tot=1;
ll insert(ll len){
ll p=1;
for(ll i=1;i<=len;i++){
ll it=s[i]-'a'+1;
if(!nex[p][it])nex[p][it]=++tot;
p=nex[p][it];
}
cnt[p]++;
if(cnt[p]>=2)return 0;
return p;
}
struct BIT{
ll K[N];
ll lowbit(ll x){return x&(-x);}
void add(ll x,ll val){while(x<N){K[x]+=val;x+=lowbit(x);}}
ll ask(ll x){ll ans=0;while(x){ans+=K[x];x-=lowbit(x);}return ans;}
}T,G;
struct node{
ll ti,l,r,coef;
node(ll ti=0,ll l=0,ll r=0,ll coef=0):ti(ti),l(l),r(r),coef(coef){}
};
vector<node> P;
struct qry{
ll t,p,coef;
qry(ll t=0,ll p=0,ll coef=0):t(t),p(p),coef(coef){}
};
vector<qry> Q;
pi I[N];
ll sum[N],nexr[N],lth[N],now;
void solve(){
ll m=0,idx=0;ll n=read();
for(ll i=1;i<=n;i++){
scanf("%s",s+1);ed[i]=m+1;ll len=strlen(s+1);
ll pos=0;if(n==100 && i>=45 && now==11)cout<<(s+1)<<endl;
if(pos=insert(len)){
for(ll j=1;j<=len;j++)t[++m]=s[j];
t[++m]='@';suf[pos]=ed[i];lth[suf[pos]]=len;
I[++idx]=mp(ed[i],len);
}
}
t[++m]='$';get_SA(m);
for(ll i=1;i<=tot;i++)if(cnt[i])sum[rk[suf[i]]]+=cnt[i],T.add(rk[suf[i]],cnt[i]),nexr[rk[suf[i]]]=suf[i];
for(ll i=m;i>=1;i--)nexr[i]=(nexr[i]?nexr[i]:nexr[i+1]);
for(ll i=1;i<=m;i++)sum[i]+=sum[i-1];
ll res=0;
for(ll i=1;i<=idx;i++){
ll begin=I[i].fi,len=I[i].se,p=1;
auto trs=[&](ll l,ll r){
p=1;
for(ll k=l;k<=r;k++){
ll it=t[k]-'a'+1;
p=nex[p][it];
}
};
trs(begin,begin+len-1);//cerr<<begin<<' '<<len<<' '<<rk[suf[p]]<<endl;
if(cnt[p]>=2){
ll les=sum[rk[suf[p]]-1];
res+=(cnt[p]*(cnt[p]-1))/2*les;
}
if(cnt[p]>=3)res+=(cnt[p]*(cnt[p]-1)*(cnt[p]-2))/6;
ll coef=cnt[p];p=1;
ll subtot=0;
for(ll k=begin;k<=begin+len-1;k++){
ll it=t[k]-'a'+1;p=nex[p][it];
if(k==begin+len-1)break;
if(!cnt[p])continue;
ll Bst=k+1;ll L=rk[Bst]+1,R=rk[begin]-1;
if(nexr[L] && lth[nexr[L]]==begin+len-1-k && ask_min(L,rk[nexr[L]])>=lth[nexr[L]])L=rk[nexr[L]]+1;
if(L>R)continue;
ll num=(T.ask(R)-T.ask(L-1));
if(rk[suf[p]]>=L && rk[suf[p]]<=R)res+=coef*(cnt[p]*(cnt[p]-1))/2,num-=cnt[p],subtot+=cnt[p]*cnt[p];
//cerr<<begin<<' '<<len<<' '<<k<<' '<<L<<' '<<R<<' '<<rk[suf[p]]<<' '<<num<<' '<<sum[rk[begin]-1]<<endl;
//cerr<<nexr[L]<<' '<<rk[nexr[L]]<<' '<<lth[nexr[L]]<<' '<<begin+len-1-k+1<<endl;
res+=cnt[p]*coef*num;
Q.eb(qry(R,rk[suf[p]],1*cnt[p]));Q.eb(qry(L-1,rk[suf[p]],-1*cnt[p]));
P.eb(node(rk[suf[p]],L,R,cnt[p]));
G.add(L,cnt[p]);
}
//cerr<<res<<endl;
sort(Q.begin(),Q.end(),[](qry A,qry B){return A.t<B.t;});
sort(P.begin(),P.end(),[](node A,node B){return A.ti<B.ti;});
ll pos=-1;
for(auto [t,l,r,co]:P){
while(pos+1<Q.size() && Q[pos+1].t<t)pos++,subtot-=Q[pos].coef*G.ask(Q[pos].p);
G.add(l,co);G.add(r+1,-co);
}
while(pos+1<Q.size())pos++,subtot-=Q[pos].coef*G.ask(Q[pos].p);
for(auto [t,l,r,co]:P)G.add(l,-co),G.add(r+1,co);
res+=subtot/2*coef;P.clear();Q.clear();
//cerr<<res<<endl;
}
for(ll i=1;i<=tot;i++)if(cnt[i])T.add(rk[suf[i]],-cnt[i]);
for(ll i=1;i<=tot;i++){
for(ll j=1;j<=26;j++)nex[i][j]=0;
cnt[i]=suf[i]=0;
}
mset(sum,0,m+1);mset(sa,0,m+1);mset(rk,0,m+1);mset(nexr,0,m+1);
write(res);putchar('\n');
tot=1;
}
//把所有字符串插到 trie 里,相同的只在后缀数组里保留一个
//遍历每个字符串,算以他为最长边的时候的方案
//遍历每个前缀,先统一加到树状数组里,在 SA 里找比自己小,比主串小,比后缀大的合法的区间,计数
//自己重复两遍如果没加要加上
int main(){
#ifdef EAST_CLOUD
freopen("a.in","r",stdin);
//freopen("a.out","w",stdout);
#endif
ll T=read();now=T;while(T--)solve(),now--;
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 0ms
memory: 57016kb
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: 0
Accepted
time: 0ms
memory: 65316kb
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 10 20 41 14 63 74 18 11081
result:
ok 14 lines
Test #3:
score: 0
Accepted
time: 0ms
memory: 65300kb
input:
11 10 bppzfsncq bppzfsncq vcqxgcehdx bppzfsncq bppzfsncq muwrcvt w aanwhqmync aanwhqmync bppzfsncq 10 t jkky t z t t z z z t 10 qidkyca uhqubvbo kosyvh gsj gsj gsj duo jrro gsj jrro 10 lhktb lhktb lhktb uohl lhktb r lhktb lhktb wruim lhktb 10 e gqvdmpvxb gqvdmpvxb gqvdmpvxb sttirbhz gqvdmpvxb zdfpm ...
output:
30 60 15 35 20 20 23 12 38 44 8047
result:
ok 11 lines
Test #4:
score: -100
Wrong Answer
time: 0ms
memory: 73860kb
input:
11 100 kalgqjh mdszzwe qxn kalgqjh hy kalgqjh suplvp r kkeoxmx tcoise suplvp suplvp y kalgqjh vrwniyici jmnyrradyq kalgqjh kalgqjh suplvp rkg xzevyk zc suplvp hcupv kalgqjh qakyahjaoi mum pbg u ip kalgqjh kalgqjh jngc ylr suplvp qxn kalgqjh bzwodm e kalgqjh kalgqjh evmm kbymvbccs kalgqjh suplvp kalg...
output:
suplvp kalgqjh qaveht mdszzwe rglc qxn xzevyk qsetg tcoise suplvp qmgqz lv zvngwrh suplvp pbg ervdvma pbg gl kalgqjh jrbbtimsy i uvkegjk kvuw kalgqjh pbg kalgqjh suplvp oplkyqngw kalgqjh sbmnstr q kalgqjh domnxd mdszzwe kalgqjh pbg oes hy mum kalgqjh jngc g hfteyjo tcoise qxn qsetg tcoise kkeoxmx pb...
result:
wrong answer 1st lines differ - expected: '12478', found: 'suplvp'