QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#104857 | #4882. String Strange Sum | 1kri | ML | 3470ms | 373800kb | C++14 | 6.6kb | 2023-05-12 07:55:33 | 2023-05-12 07:55:37 |
Judging History
answer
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
#include <set>
#include <map>
#include <queue>
#define ll long long
using namespace std;
int t,n;
ll ans;
char s[200005];
struct sam_node{
int son[26],len,link;
sam_node(){
memset(son,0,sizeof(son));
len=0,link=-1;
return;
}
void clear(){
memset(son,0,sizeof(son));
len=0,link=-1;
return;
}
}sam[400005];
int sam_cnt,last;
int sam_ins(char x){
int p=last;
int now=++sam_cnt;
sam[now].len=sam[p].len+1;
while(p!=-1&&sam[p].son[x-'a']==0){
sam[p].son[x-'a']=now;
p=sam[p].link;
}
if (p==-1){
sam[now].link=0;
last=now;
return now;
}
int q=sam[p].son[x-'a'];
if (sam[q].len==sam[p].len+1){
sam[now].link=q;
last=now;
return now;
}
int clone=++sam_cnt;
sam[clone]=sam[q];
sam[clone].len=sam[p].len+1;
sam[now].link=sam[q].link=clone;
while(p!=-1&&sam[p].son[x-'a']==q){
sam[p].son[x-'a']=clone;
p=sam[p].link;
}
last=now;
return now;
}
int endpos[400005];
int u[400005],v[400005],first[400005],nxt[400005];
int sz[400005],son[400005];
struct seg{
int l,r,cnt;
bool operator <(const seg &y)const{
if (l!=y.l)return l<y.l;
if (r!=y.r)return r<y.r;
return cnt<y.cnt;
}
};
seg make_seg(int l,int r,int cnt){
seg ans;
ans.l=l,ans.r=r,ans.cnt=cnt;
return ans;
}
struct node{
int v,r;
bool operator ==(const node &y)const{
return (v==y.v)&&(r==y.r);
}
bool operator <(const node &y)const{
if (v!=y.v)return v<y.v;
return r<y.r;
}
};
node make_node(int v,int r){
node ans;
ans.v=v,ans.r=r;
return ans;
}
struct node_Heap{
priority_queue<ll> h,_h;
void ins(node x){
ll val=(((ll)x.v)<<20)+x.r;
h.push(-val);
return;
}
void del(node x){
ll val=(((ll)x.v)<<20)+x.r;
_h.push(-val);
return;
}
int size(){
return h.size()-_h.size();
}
node top(){
while((!_h.empty())&&(h.top()==_h.top()))h.pop(),_h.pop();
node ans=make_node(((-h.top())>>20),((-h.top())&1048575));
return ans;
}
void pop(){
while((!_h.empty())&&(h.top()==_h.top()))h.pop(),_h.pop();
h.pop();
return;
}
void clear(){
while(!h.empty())h.pop();
while(!_h.empty())_h.pop();
return;
}
};
struct DS{
vector<pair<int,int>> ins;
int id_tot;
map<int,int> id;
vector<set<int>> h;
set<seg> c;
node_Heap d;
ll sum;
int get_r(int x){
set<seg>::iterator it=c.lower_bound(make_seg(x,x,0));
if (it==c.end()||(*it).l>x)it--;
return (*it).r;
}
void build(){
sort(ins.begin(),ins.end());
int len=(int)ins.size();
for (int i=0;i<len;){
int j=i;
while(j<len&&ins[j].second==ins[i].second)j++;
id[ins[i].second]=++id_tot;
set<int> qwq;
for (int k=i;k<j;k++)qwq.insert(ins[k].first);
h.push_back(qwq);
c.insert(make_seg(ins[i].first,ins[j-1].second,j-i));
sum+=1ll*ins[j-1].second*(j-i);
if (i>0)d.ins(make_node(ins[i].first-ins[i-1].second,ins[i-1].second));
i=j;
}
return;
}
void ins_c(seg qwq){
seg pre=make_seg(-1,-1,-1),nxt=make_seg(-1,-1,-1);
set<seg>::iterator it=c.lower_bound(qwq);
if (it!=c.end())nxt=(*it);
if (it!=c.begin()){
set<seg>::iterator _it=it;
_it--;
pre=(*_it);
}
c.insert(qwq);
if (nxt.l!=-1&&pre.l!=-1)d.del(make_node(nxt.l-pre.r,pre.r));
if (pre.l!=-1)d.ins(make_node(qwq.l-pre.r,pre.r));
if (nxt.l!=-1)d.ins(make_node(nxt.l-qwq.r,qwq.r));
sum+=1ll*qwq.r*qwq.cnt;
return;
}
void del_c(seg qwq){
c.erase(qwq);
seg pre=make_seg(-1,-1,-1),nxt=make_seg(-1,-1,-1);
set<seg>::iterator it=c.lower_bound(qwq);
if (it!=c.end())nxt=(*it);
if (it!=c.begin()){
set<seg>::iterator _it=it;
_it--;
pre=(*_it);
}
if (nxt.l!=-1&&pre.l!=-1)d.ins(make_node(nxt.l-pre.r,pre.r));
if (pre.l!=-1)d.del(make_node(qwq.l-pre.r,pre.r));
if (nxt.l!=-1)d.del(make_node(nxt.l-qwq.r,qwq.r));
sum-=1ll*qwq.r*qwq.cnt;
return;
}
void del(int x,int r){
int t=id[r]-1;
del_c(make_seg((*h[t].begin()),r,h[t].size()));
h[t].erase(x);
if (h[t].size()>0)ins_c(make_seg((*h[t].begin()),r,h[t].size()));
return;
}
void merge(int x,int y){
if (h[x].size()>h[y].size())swap(h[x],h[y]);
for (set<int>::iterator it=h[x].begin();it!=h[x].end();it++)h[y].insert(*it);
h[x].clear();
return;
}
ll upd(int len_l,int len_r){
ll ans=0;
int last=len_l;
while(d.size()>0&&(d.top()).v<=len_r){
node d_val=d.top();
set<seg>::iterator it=c.lower_bound(make_seg(d_val.r+1,d_val.r+1,0));
it--;
set<seg>::iterator _it=it;
ans+=1ll*(d_val.v-last)*sum;
last=d_val.v;
_it++;
seg qwq=make_seg((*it).l,(*_it).r,(*it).cnt+(*_it).cnt);
merge(id[(*it).r]-1,id[(*_it).r]-1);
c.erase(*it),sum-=1ll*(*it).r*(*it).cnt;
c.erase(*_it),sum-=1ll*(*_it).r*(*_it).cnt;
c.insert(qwq),sum+=1ll*qwq.r*qwq.cnt;
d.pop();
}
ans+=1ll*(len_r-last+1)*sum;
return ans;
}
void clear(){
ins.clear();
id_tot=0;
id.clear();
h.clear();
c.clear();
d.clear();
sum=0;
return;
}
}ds[400005];
void clear(){
ans=0;
last=0;
for (int i=0;i<=sam_cnt;i++){
sam[i].clear();
endpos[i]=0;
u[i]=v[i]=first[i]=nxt[i]=0;
sz[i]=son[i]=0;
ds[i].clear();
}
sam_cnt=0;
return;
}
void dfs1(int now){
sz[now]=0;
if (endpos[now]!=0)sz[now]=1;
son[now]=-1;
for (int i=first[now];i;i=nxt[i]){
dfs1(v[i]);
sz[now]+=sz[v[i]];
if (son[now]==-1||sz[v[i]]>sz[son[now]])son[now]=v[i];
}
if (son[now]!=-1&&sz[son[now]]*3/2<=sz[now])son[now]=-1;
return;
}
void upd_ds(int now,int t0,int t1,int fg){
if (endpos[now]!=0){
int x=endpos[now];
int r=ds[t0].get_r(x);
ds[t1].ins.push_back(make_pair(x,r));
if (fg==1)ds[t0].del(x,r);
}
for (int i=first[now];i;i=nxt[i])upd_ds(v[i],t0,t1,fg);
return;
}
void dfs2(int now){
if (now>0){
int len_l=sam[sam[now].link].len+1;
int len_r=sam[now].len;
ans+=ds[now].upd(len_l,len_r);
}
for (int i=first[now];i;i=nxt[i])
if (v[i]!=son[now]){
int fg=1;
if (son[now]==-1)fg=0;
upd_ds(v[i],now,v[i],fg);
ds[v[i]].build();
dfs2(v[i]);
}
if (endpos[now]!=0){
int x=endpos[now];
int r=ds[now].get_r(x);
ds[now].del(x,r);
}
if (son[now]!=-1){
swap(ds[now],ds[son[now]]);
dfs2(son[now]);
}
return;
}
int main(){
cin>>t;
while(t--){
scanf("%s",s+1);
n=strlen(s+1);
reverse(s+1,s+1+n);
for (int i=1;i<=n;i++)endpos[sam_ins(s[i])]=i;
for (int i=1;i<=sam_cnt;i++){
u[i]=sam[i].link,v[i]=i;
nxt[i]=first[u[i]],first[u[i]]=i;
}
dfs1(0);
for (int i=1;i<=n;i++)ds[0].ins.push_back(make_pair(i,i));
ds[0].build();
dfs2(0);
for (int i=1;i<=n;i++)ans-=1ll*i*i;
cout<<ans<<endl;
clear();
}
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 16ms
memory: 134764kb
input:
8 aa ab ababa abaaba abacaba abaaababaab aababcabcbc abcdabcabaabcd
output:
1 0 6 7 0 74 51 20
result:
ok 8 numbers
Test #2:
score: 0
Accepted
time: 360ms
memory: 134904kb
input:
100000 ff ki wb vc bb cq tt gl xb tt ll it bb yy dd yg tt vq gg ua ff nn aa yq ee ae sj yy cd qk vk ts tt cm rr yk sh fv vm rr tl vv bb rl jx pv tx ib dp oo lx jo bb dl sj sn db kk oo rk yy gz ff ha ja ax hn ww ms yy kf zz ss ii km uv mn si ng hh yq lq bq ed bb bw jj pp ss xg ff gm ee cc fn vv rc nn...
output:
1 0 0 0 1 0 1 0 0 1 1 0 1 1 1 0 1 0 1 0 1 1 1 0 1 0 0 1 0 0 0 0 1 0 1 0 0 0 0 1 0 1 1 0 0 0 0 0 0 1 0 0 1 0 0 0 0 1 1 0 1 0 1 0 0 0 0 1 0 1 0 1 1 1 0 0 0 0 0 1 0 0 0 0 1 0 1 1 1 0 1 0 1 1 0 1 0 1 0 0 1 1 1 0 0 0 1 1 0 0 1 1 1 0 1 1 1 1 1 0 0 1 0 0 0 1 1 1 0 0 1 0 1 1 1 1 1 1 0 0 0 0 0 1 0 1 0 0 0 1 ...
result:
ok 100000 numbers
Test #3:
score: 0
Accepted
time: 309ms
memory: 134816kb
input:
40000 nbbnn tttuu rfeer omhom qqcmq yyiyi tlttt jhjtj ixiyx bnnon iwpiw uzluz ffqfj dyddl szkss dauud dddiy gggtt ebbee uboob nnnnv rrjrj cjccj xnnyy mwmjw wyyyq vvuvp vyzyv sssss vvsvs rhxxr pkkpk xsxss ngncn wzwjz khkth jjjjj vvvbb unnxn aqlqq mmgmg iiiji lyllv luuuu itizt fsffs xggii jqqtj mummd ...
output:
4 11 2 0 4 7 4 0 0 3 0 0 4 2 1 2 10 11 4 2 16 7 4 4 0 7 4 0 20 7 2 3 5 0 0 0 20 11 3 1 7 10 2 10 0 4 4 3 2 3 4 6 1 4 10 3 6 10 10 16 7 7 0 10 5 0 2 16 0 6 1 0 1 2 5 2 5 6 20 10 0 8 10 3 16 7 0 8 4 3 0 1 7 0 2 4 4 5 3 7 2 4 16 4 1 8 10 7 4 3 4 4 6 2 2 4 6 2 4 5 4 3 10 5 0 4 16 2 3 0 3 4 1 1 4 4 11 2 ...
result:
ok 40000 numbers
Test #4:
score: 0
Accepted
time: 355ms
memory: 134780kb
input:
20000 iijjijiijj fxffxfffxx kkiiiiiiii oppopopppo iiooiioooi gggxxxxggg oxxoxxxoox puuuppupuu ppssspspps eefffeefff xxtxxxttxt yyppypyppp kkwwkkwwkk bvvvbvbbbv attataaaat boooobbobo hhhhfhhhff nnhhhnhhhh cdccccdccd axxxaxaxxa qqnnnnnqnq eeexxeeeex ppkkkkkkkp uusussusss iwiwiiwiii gglgllgggg wwwrrrwr...
output:
40 58 93 52 52 57 56 34 44 46 52 46 57 47 41 47 87 48 56 52 65 47 86 56 61 50 51 34 58 41 52 47 60 92 61 56 64 55 65 48 56 77 80 62 55 57 41 58 44 93 63 49 54 59 50 55 111 58 52 52 39 35 63 50 111 84 140 75 78 40 99 56 49 40 68 45 87 54 47 59 52 59 50 86 82 54 48 59 33 121 84 44 33 40 62 55 46 121 6...
result:
ok 20000 numbers
Test #5:
score: 0
Accepted
time: 365ms
memory: 134936kb
input:
10000 jlljjjlllljjjjljllll uooooouuouoouoooouuo utttutuuttuuuutttutu xccxxxccxxccccxcxxxc sjjsjjsjjssjjsssjjjs fgffffgfgggfgfgfffgf ddaaadaadadddadadaaa tbbbbttttttbtttbtbtt eeeeeekkkekeeeekeeke dddddmdmmmmdddmmddmm yykkkkykkykykkkkyykk ededeedddededeedddee kktttkktktkktkkkttkk fcfcfcffffcffcccccfc ...
output:
339 332 348 341 662 367 363 432 395 371 452 460 353 472 416 420 464 365 589 476 516 407 446 376 501 364 354 424 366 438 330 590 553 491 662 317 467 374 422 406 492 484 405 328 396 654 300 410 447 404 389 487 534 688 489 370 396 474 396 467 364 424 380 236 480 506 506 339 297 316 457 626 338 349 351 ...
result:
ok 10000 numbers
Test #6:
score: 0
Accepted
time: 464ms
memory: 135160kb
input:
4000 urrrrrrrrururrruruuuuuuuuruurruuruuuurrruurruurruu hthtthttthhhtttthhhhthhhhthhhttthtthhtthhtttttthhh ttssssssttsststttsttttssstsssstsstssttssststttstst iiniiiiiniinnniiniiiiniiiinnniiinniiininniinnnnnni dddpdpddpdpdppppdpdppdpdpddppppddddpdpdddppppdppdp mmmsmmmmsmmmmmmsmmmmsmmmmsssmmssmssmsmsm...
output:
5599 5287 4294 4818 5746 7893 3623 3453 5390 5812 5608 5541 6069 5655 3743 3847 4866 5059 3876 3925 5018 4379 5016 5747 5333 5271 3890 5894 5141 3773 4196 4880 5111 5510 4334 3825 6188 5960 4893 5359 4720 4167 4042 4051 5011 6457 3807 3837 4612 4859 5044 6861 4330 5967 5001 4857 4340 3957 4152 4230 ...
result:
ok 4000 numbers
Test #7:
score: 0
Accepted
time: 550ms
memory: 135712kb
input:
2000 ffffccfcfcfcfccffcccfcccfcfccfcccccfcfcccfccfcccccffffcccfcccffffffcffffccccffffffccffffcccfcccfcfff enneeenneneennnnneeeeeenenneeennnnneneneneenneenneennnnnnnnenennennnneneneneeenennnneennnenneennnnne mzmzmmzzmzzmzzmmzmmmzzmmmzzzzmmmzmmzzmzzmmmzzmzmzmmzzmmmmzmmmmmmzzmmmzmmmmzmmzmzmmzzmzmmmmmzm...
output:
32329 22810 31196 27570 28177 29004 24676 27293 26336 28196 28972 25095 34989 26711 26498 29643 24727 22723 31605 30180 43766 27097 25766 26819 28516 28122 34935 27399 33153 32281 26033 24708 41701 21704 24011 27481 26913 23270 31778 27676 25970 38135 25776 23316 44300 29424 24305 23476 29598 24423 ...
result:
ok 2000 numbers
Test #8:
score: 0
Accepted
time: 658ms
memory: 137632kb
input:
1000 udduuddduududddudduuduuuduuududdduuduuduudududdduuddddduuuuddduuuuudduddduuddddududduuduuuuuduuduudduuuuuuuddduuduuduuuudududduuuuududuudduduuduuduududddudududdududuududuudududdduddduuuuuuuuuuuddduduu kykykkykyykkykkkkkkyykkyyyyyykyyykkkyykkyykyyyykkykykkkykkykkkyyykyyyyykkkyyykykykkkkyyykkyyyy...
output:
153694 145776 132786 133300 133959 177645 148786 132135 169466 159430 133110 171068 168822 120233 160090 125272 130139 138522 163688 161504 146208 170689 149990 147133 129161 146576 129200 138709 133553 154659 136204 167106 167771 151156 129986 137285 131065 131582 159289 158241 141081 128564 167348...
result:
ok 1000 numbers
Test #9:
score: 0
Accepted
time: 1075ms
memory: 147428kb
input:
200 zzzzzzzzzzzzzzzazzzazzzzzzzaazzaaaazaazzaazzazaazazazazzzazazaazzazzaazaaaazaaazzzzzazzzazzaaazzazzazazaazzzazaaaaaazzzaazzzazaazzzzzaaaazazazazzzzaaaaazaazzzzazzzazazaaazazzzaazaazazazzzzazaazaazaaaazzzzzazzazzzazzzzzzazzaazzzazzzzazzzzaaazzzaaazzzzzazzaazzzzazaazzaaaaazzazazzaaaaazzzzzaaazzazz...
output:
6229118 5438629 6162119 5350067 5263770 5443998 6419968 6592325 5876576 5249432 6397577 5947645 5851620 6059174 6048260 5774316 6323371 6103930 5794311 5297842 5559753 6109729 5724850 5095495 5263069 5635785 5916607 5959557 5261499 5446440 5526488 5504207 7229030 5767214 5191558 5475249 5537449 6169...
result:
ok 200 numbers
Test #10:
score: 0
Accepted
time: 2696ms
memory: 211808kb
input:
20 nnnllllnlnnllllllllllllnnnnllnnlllnnnnnnnlnlnnlnllnnlnnnnnnnnnllllnlllnnnlnllnnnlnnllnnlnnllnnnllnlnllnllllnnnlllnllllnlnnllnllllnnlllnnnlllnlnnnnlllnnnlnlnnlnllnnlnllnlllllllnnnnnnnnlnnlllnlnnnllnlllllnlnnllnllllnnnnnnnnnlnnnnnnnnlnlnnnllllllnllnlllnnnlnllnllnnllllllllllnnllnllllnlllnlllnlnnnlll...
output:
894196857 938803119 931699133 881434935 917400222 988704236 829814492 910180484 875107867 927874072 861165839 857715013 907953346 879864017 925887954 884818843 920746630 936583374 887419288 927606368
result:
ok 20 numbers
Test #11:
score: 0
Accepted
time: 3470ms
memory: 373800kb
input:
5 omomomommomommommoooommmommmoommmomommmoomoomoomomomoooommmmmmoomomomoommoooommommmooommomoomomommmmmmomooomoommomoommomoooooomoomomooommmommmmooooooomoooommmmomooomoommmmmomoooomommomomomommmomommmommoooomooommomooomoomoommmmmmmoomoommoomommommmmommmmmmmmmoooomomoooomoommmmoomooomomooommmmmoommom...
output:
17174226584 17605268588 18296766446 17539695533 18766633585
result:
ok 5 number(s): "17174226584 17605268588 18296766446 17539695533 18766633585"
Test #12:
score: -100
Memory Limit Exceeded
input:
2 ddvddvvvvvddddvddddddddddddvvdvvvvvvvddvddddddddvvvvdvvddvvdvvvdvdddvdddvvvvdvvvvdvdvdvddvvvddvdvdddvdddvdvvdvvvvdvdvvvvvvdvdvvdddvdddvvvdddvvddvvvdvdddvdddvvdvvddvddvdvdvddvvvvvvdvdddvvddvdddvdddvdvvvvdvvvvdvvddvvvddvvdddvvddvdddvdvdvddvvvddvddvvddvddvvddddvdddvddvvvdvdvvvvvdvvvvddddvdddvddvdvvdd...
output:
132896961339