QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#716414 | #9406. Triangle | daduoli | TL | 606ms | 93768kb | C++14 | 4.2kb | 2024-11-06 15:12:54 | 2024-11-06 15:12:56 |
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]);
}
int sum[MAXN],R[MAXN];
int op,TT;
struct tree_array {
LL tr[MAXN];
int lb(int x) {
return x&(-x);
}
void update(int x,int val) {
for(int i=x;i<=cnt;i+=lb(i)) tr[i]+=val;
}
LL query(int x) { LL res=0;
for(int i=x;i;i-=lb(i)) res+=tr[i];
return res;
}
}T,T1;
int top;
struct pt {
int len,sz,l,r;
vector<int> e;
}ind[MAXN];
struct ddl {
int lim,x,y;
friend bool operator <(ddl a,ddl b) {
return a.lim>b.lim;
}
};
void vmain() {
scanf("%d",&n);
++op;
// if(op==286) {
// cout<<n<<" ";
// }
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];
ch[++cnt]='A';
// if(op==286) {
// cout<<st<<endl;
// }
} SA(); get_height(); init(); LL ans=0;
// for(int i=1;i<=cnt;++i) {
// cout<<sa[i]<<" ";
// }
for(int i=1;i<=cnt;++i) sum[i]=sum[i-1]+pd[sa[i]];
int lt=0;
for(int i=cnt;i>=1;--i) {
if(pd[sa[i]]) {
R[i]=i;
if(lt&&query(i+1,lt)>=tlen[sa[i]]&&tlen[sa[i]]==tlen[sa[lt]]) R[i]=R[lt];
lt=i;
} else {
R[i]=i;
if(lt&&query(i+1,lt)>tlen[sa[lt]]) R[i]=R[lt];
}
}
// for(int i=1;i<=cnt;++i) {
// cout<<i<<":"<<R[i]<<endl;
// }
top=0;
for(int i=1;i<=cnt;++i) {
if(!pd[sa[i]]) continue;
while(top) {
int t=ind[top].e[0];
if(query(t+1,i)<ind[top].len) {
for(auto t:ind[top].e) T.update(t,-1);
--top;
} else break;
}
for(int j=1;j<=top;++j) {
int t=rk[sa[i]+ind[j].len]; t=R[t];
// if(i==9) {
// cout<<t<<" ";
// }
if(t<=i-1) ans+=(LL)ind[j].sz*(sum[i-1]-sum[t]);
if(t<ind[j].e[0]) {
ans-=(LL)ind[j].sz*ind[j].sz;
ans+=(LL)ind[j].sz*(ind[j].sz-1)/2;
}
} priority_queue<ddl> q; vector<pi> vv;
// cout<<i<<" "<<ans<<endl;
for(int j=1;j<=top;++j) {
int t=rk[sa[i]+ind[j].len]; t=R[t];
while(!q.empty()) { ddl u=q.top();
if(u.lim>ind[j].e[0]) break;
q.pop(); T1.update(u.x,u.y); vv.pb(u.x,u.y);
}
ans-=(LL)(T1.query(cnt)-T1.query(t))*ind[j].sz;
q.push((ddl){t,ind[j].e[0],ind[j].sz});
}
// cout<<i<<" "<<ans<<endl;
for(auto t:vv) T1.update(t.fi,-t.se);
if(tlen[sa[i]]!=ind[top].len) {
ind[++top].e.clear(); ind[top].e.pb(i);
ind[top].l=ind[top].r=i; ind[top].len=tlen[sa[i]]; ind[top].sz=1;
} else {
ind[top].e.pb(i); ind[top].r=i; ++ind[top].sz;
}
// cout<<i<<" "<<ans<<endl;
}
// if(TT<500)
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;
sum[i]=0;
} cnt=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: 11ms
memory: 60928kb
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: 19ms
memory: 67244kb
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: 12ms
memory: 70828kb
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: 20ms
memory: 77544kb
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: 8ms
memory: 73888kb
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: 606ms
memory: 93768kb
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: 586ms
memory: 90556kb
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: 590ms
memory: 86684kb
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: 596ms
memory: 89360kb
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: -100
Time Limit Exceeded
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...