QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#730046 | #9406. Triangle | Wuyanru | TL | 954ms | 140212kb | C++14 | 5.9kb | 2024-11-09 18:26:56 | 2024-11-09 18:26:56 |
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;
struct node{ int a,b,c;}v[300001],vv[300001];
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,memset(son[p],0,sizeof(son[p]));
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;(1<<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;i<=c;i++) printf("%2d%c",rk[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;
// printf("%d and %d : %d\n",i,p,j);
}
// for(int i=1;i<=c;i++) printf("%2d%c",hi[i]," \n"[i==c]);
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=min(get(l1,l2),min(r1-l1+1,r2-l2+1));
// printf("check %d %d %d %d lcp=%d\n",l1,r1,l2,r2,lcp);
if(l1+lcp<=r1&&l2+lcp<=r2) return t[l1+lcp]>t[l2+lcp];
return l1+lcp<=r1;
}
int tt[600001];
inline int lowbit(int i){ return i&(-i);}
inline void add(int x,int y){ while(x<=2*c) tt[x]+=y,x+=lowbit(x);}
inline int get(int x){ int ans=0;while(x) ans+=tt[x],x-=lowbit(x);;return ans;}
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];
vc<pi>V;
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]);
// printf("st=%d ed=%d j=%d : %d %lld\n",st,ed,j,num[now],max(0ll,ff[st]-ff[j+1]-cc[j+1]));
if(num[now]&&check(st,j,j+1,ed))
{
// printf("%d ~ %d : %d\n",st,j,num[now]);
ans-=(ll)num[now]*(num[now]+1)/2;
}
V.push_back(pi(st,j));
V.push_back(pi(j+1,ed));
v[j-st+1].c=num[now];
}
int f=ff[st],c=cc[st],all=ed-st;
v1+=(ll)(c-1)*(c-2)/2;
v2+=(ll)(c-1)*f;
sort(V.begin(),V.end(),[](pi a,pi b)
{
return check(b.first,b.second,a.first,a.second);
});
int rk=0;
for(unsigned i=0;i<V.size();i++)
{
if(!i||check(V[i].first,V[i].second,V[i-1].first,V[i-1].second)) rk++;
if(V[i].first==st) v[V[i].second-st+1].a=rk;
else v[V[i].first-st].b=rk;
}
memcpy(vv,v,sizeof(node)*(all+1));
sort(v+1,v+all+1,[](node a,node b){ return a.a<b.a;});
sort(vv+1,vv+all+1,[](node a,node b){ return a.b<b.b;});
// for(int i=1;i<=all;i++) if(v[i].c) printf("%d : %d %d %d\n",i,v[i].a,v[i].b,v[i].c);
// putchar('\n');
// for(int i=1;i<=all;i++) if(vv[i].c) printf("%d : %d %d %d\n",i,vv[i].a,vv[i].b,vv[i].c);
ll val=0,sum=0;now=1;
for(int j=1;j<=all;j++)
{
while(now<=all&&vv[now].b<v[j].a) add(vv[now].a,vv[now].c),sum+=vv[now].c,now++;
val+=v[j].c*(sum-get(v[j].b));
}
// printf("val=%lld\n",val);
while(now>1) now--,add(vv[now].a,-vv[now].c);
for(int i=1;i<=all;i++) if(v[i].a>v[i].b) val-=(ll)v[i].c*v[i].c;
ans-=val/2;// printf("val=%lld ans=%lld\n",val,ans);
}
ans+=v1/3+v2/2;
printf("%lld\n",ans);
}
int main()
{
int T=read();
while(T--) clear(),solve();
return 0;
}
/*
1
3
aaa
aaa
aaaa
ans=1
*/
詳細信息
Test #1:
score: 100
Accepted
time: 0ms
memory: 52912kb
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: 63200kb
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: 61188kb
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: 0
Accepted
time: 4ms
memory: 69500kb
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:
12478 6722 9220 6668 4934 11233 7950 5470 4525 5743 1586066
result:
ok 11 lines
Test #5:
score: 0
Accepted
time: 3ms
memory: 67464kb
input:
2 1000 t lhijhkxzzx nhfiksblww h xg z dcbmbvyz ois ynwjgfp oqzv qtoinl gr teu kmza hs t mwhewk kjmuneon bekku qheweh szhagft fcwjp bobwnap y oqpole oqzv xeaiyhfeg rjkdidea wmeslege vyyi teomn yvmcnw vnvum tgnl swqqruuvc xxllevp bov dse e b rtbhogkx nzs e bs pppyndgrrv n tzbwqdusn e xeaiyhfeg i agnet...
output:
2430570 1904282
result:
ok 2 lines
Test #6:
score: 0
Accepted
time: 102ms
memory: 93972kb
input:
503 16 yh yh yhc yhc yhcowdfqlwfidnx yhc yhc yh yhcowdfqlwfidn yhcowdfqlwfidnx yh h yh yhcowdfqlwfidnx yhcowdfqlwfidnx yhc 19 nb nbg vpfkllgv nmzqfsuafqtayjjjcidpygz nb nb gutq n omyuvm fgxtfbhuglxyiumi nbghjuti nbg nb fgxt nbghjuti n nb nbg n 7 rtjiwfidoahckhvgoxvvrncqvgerqiuaruiftakvugsgnsw wllcan...
output:
531 485 6 12 4 118 6 3 1635 18 373 20 954 6208 45 12 1124 79 267 2 5778 22 13 1 1 16 630 0 7 16315 0 2155 2308 26 936 109 103 5 0 2492 7 2 114 144 11 158 0 0 101 455 0 12234 78 631 5402 94 66 84 161 4412 5 3 81 22 20 13 52 632 6 137 56 2 3 64521 122 330 0 0 7 0 113 249 8 301 335 1825 110 4 108 50 10...
result:
ok 503 lines
Test #7:
score: 0
Accepted
time: 106ms
memory: 85804kb
input:
503 23 rjyyivdg n n n n n n n nmr n nmrk nm rjyyivdguyiffnvunoxconw n n n o lixclcmwthwkrsi mqluhyypgfkmdvgpzju n nmrk rjy n 15 jotwxwhaqdxmazhslyouztprzlirisvwvduojb jot jotwxw j jotwx jotwx gohg j gdgneodagmdhvvapjh jotwxw xs vurk vurk j xs 7 xrczucnkbemaymvabkkwnn xrczucnkbemaymvabkkwnn xrczucnkb...
output:
855 58 35 0 1 56 2 112 1 8465 242 56 110 23 544 0 3 17 29 11 764 20 9 0 4 77 812 35 4 10 32 437 9 2364 3 2 11 2 421 50 4 107 1 62 1120 3 1 16 3970 1147 1026 8 4 85 9 31 61 16 205 2 2 84 238 1 1 51 4 0 16 61 331 4 16 7 0 7 148 10 13 2 1 37 1 67 0 296 1 0 644 32 2 10 0 5 126 3490 4 0 10 331 1216 7921 ...
result:
ok 503 lines
Test #8:
score: 0
Accepted
time: 110ms
memory: 90056kb
input:
503 5 ljtolmgjndlwoyjjttak mihjdhkyfnafwrpeuiuiurusvsnu ljtolmgjndlwoyjjttak mihjd ljtolmgjndlwoyjjttak 25 lhx lh lhx lh k lh kninp l lhx lhx izeqohkpfuovopebttqaufmmlivd lhx lhx qid lhx lh lhx lhx oklb l lhx lhx lhx lhx l 9 mxeonfwpujrilfigjoiyjkzdmi fezhyrcyqy mx fezh f dmvfbklnkxmnetib dmvfbkln m...
output:
4 1476 27 26 117970 2 105 30 4 737 4 2 19 48 34 434 6 78331 22 23 0 228 56 4 3 305 9 84 132 199 20 3 4057 0 0 20 35 34 48 4 266 14 17 4788 545 28989 0 10 535 84 1 1775 322 11 57 16 15 1331 5 0 10 5 183 8 2 237 10 0 60 20 42 7 10 297 14 210 6254 7 3 0 13 2744 119 47 0 1 68114 17 1 2 1 7 1 2 113 26 0 ...
result:
ok 503 lines
Test #9:
score: 0
Accepted
time: 113ms
memory: 89976kb
input:
503 11 wkeoqqqpvmgdv w w w wkeoqqqpvmgdv sgrwmsfwclpamgq wkeoqqqpvm qkmbyvcxjsh wkeoqqqpvm wke wkeoqqqpvmgdv 7 otd qelodfwrqeprgyvzbcjljx qe qelodfwrqeprgyvzbcjlj qelodfwrqeprgyvzbcjlj qelodfwrqeprgyvzbcjlj c 15 rce rce fwq fwqqfcjrhqot rceft jkdrcehfwhqkupe fwq r jkdr fwq rceft rce fwqqfcjrhqot fwq...
output:
156 17 213 12 20 1 374 4 0 26 26 3 122 30 4005 24 1385 50 84 44 0 112 42 36 19 887 99 5 9 13 2 5029 52 14 84 116 2 10 4 8 141 9287 822 37 5 13 25 1030 0 2 3 35 81 1 0 1 138 0 578 7 30 636 63 22 2118 863 5377 33 34 10 156 336 1 7 7 4 1793 2 124 13 4 2015 7 23 1 4516 3 17 6 35 13336 9 61 3093 0 1 7 22...
result:
ok 503 lines
Test #10:
score: 0
Accepted
time: 954ms
memory: 138388kb
input:
1003 3 mpfowyd mpfowydrivrkjiarwcxwbfqvnktlzcfolbbsgelvcnzeqy hytzojmfeiwtpquxhneeznbdjjlsptedaorwfsxi 3 nyfcq nyfcqgrmshiwmgcbukozvetdggebkkychamof nyfcqgrmshiwmgcbukozvetdggebkkychamofadozdympuejvhdnpi 8 yoeqyfcjsywowdrlzzybjvtycqvizzomc zci yoeqyfc zcinc yoeqyfcjsywowdrlzzybjvtycqvizzomc y zcinc ...
output:
0 1 26 10 66 403 1 265 1025 16 329 4 1219 1 10 70 30 182 60 5 71 1 20 5343 22328 40667 90 6983 66 10 35 20 250 307 913 98 44 5393 56 280 270 3 3 2229 77 17 774 50 5 21 0 208 8 14 185 35 20 11 465 132 176 10 0 10 1704 13 44 141 0 0 5 10 79 17 213 10 108 0 0 289 10 255 27 493 4 1 24 379 30 9 284 173 2...
result:
ok 1003 lines
Test #11:
score: 0
Accepted
time: 871ms
memory: 140212kb
input:
1003 6 exssl exsslhsuwyemcafatpinzvdeypwjqsnvxlkvmpywgx f exss exs exsslhsuwyemcafatpinzvdeypwjqsnvxlkvmpywgx 7 hzzev hzzevmltbehnvjfhsz hzzevmltbe hzzevmltbehnvjfhszqzobn hzzevmltbehnvjf hm hzzevmltbehnvjfhszqzobn 9 rdx rdxnxdfcyrdpgwzwtqgtu rdx rdxnxdfcyrdpgwzwtqgtu f kpotnxntufvd kpotnxntufvdlmij...
output:
3 19 41 134 107 2 372 0 466 456 20 13 25 339 64 170 1 5 27295 3 1 221 116 11 29 19 1006 16827 16 19863 4 6 5784 3 20 21130 66 1275 100 731 83 3 84 0 32 3 615 207 8 44 245 48 165 18050 128 11 864 21 3 1221 120 51 21 8 247 302 0 1098 0 57 42 20 0 845 1 84 16 4 0 2 836 482 4 12 150 654 1035 142 291 34 ...
result:
ok 1003 lines
Test #12:
score: -100
Time Limit Exceeded
input:
1003 10 lgbpleleme u u uxpwzhga uxpwzhg u uxpwzhgaadltqqpqkbteylath u uxpwzhgaadltqqpqkbteylath uxpwzhgaadltqqpq 9 aqoxfsmqwpafbocodlpdykesadqgiggw swzjus oim aqoxfsmq aqoxfsmqwpafboc swzju nohnsjbdhljarh swzjusx aqoxf 20 mbdiqaartewavbbwbhftzwxm k mbdiqaartewavbbwbhftzwxm kx kxs kxsqv kx kx kxsqv k...
output:
70 0 290 10 604 1770 2 913 1 165 16 316 4 169 26 3 36 2 1549 2 13 15 21 14 109 34 16 1889 132 1700 1 96 17 436 2 0 6183 4144 0 4 50 4 10 5 4 4 78 10 16 97 16 50204 15871 350 3 865 46 7 0 73 38 46 29 1946 65 22088 7 91 43 310 1806 0 50 4 17 38725 262 50 0 304 141 35 50 0 22760 2973 14 410 1112 20 703...