QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#439107 | #5014. 复读程度 | 275307894a | 7 | 1702ms | 48792kb | C++14 | 5.7kb | 2024-06-11 16:13:12 | 2024-06-11 16:13:13 |
Judging History
answer
#include<bits/stdc++.h>
#define Gc() getchar()
#define Me(x,y) memset(x,y,sizeof(x))
#define Mc(x,y) memcpy(x,y,sizeof(x))
#define d(x,y) ((m)*(x-1)+(y))
#define R(n) (rnd()%(n)+1)
#define Pc(x) putchar(x)
#define LB lower_bound
#define UB upper_bound
#define fi first
#define se second
#define eb emplace_back
#define all(x) x.begin(),x.end()
using namespace std;using ll=long long;using db=double;using lb=long db;using ui=unsigned;using ull=unsigned long long;using pii=pair<int,int>;
const int N=4e5+5,M=1e7+5,K=1000+5,mod=998244353,Mod=mod-1;const db eps=1e-9;const int INF=1e9+7;mt19937 rnd(263082);
#define Tp template<typename T>
#define Ts template<typename T,typename... Ar>
namespace Debug{
Tp void _debug(char* f,T t){cerr<<f<<'='<<t<<endl;}
Ts void _debug(char* f,T x,Ar... y){while(*f!=',') cerr<<*f++;cerr<<'='<<x<<",";_debug(f+1,y...);}
#ifdef LOCAL
#define gdb(...) _debug((char*)#__VA_ARGS__,__VA_ARGS__)
#else
#define gdb(...) void()
#endif
}using namespace Debug;
int n,q;
char s[N];
ull wl[N],wr[N];
struct SAM{
int son[N][26],link[N],len[N],ed[N],La=1,cnt=1;
void extend(int c){
len[++cnt]=len[La]+1;int p=La;La=cnt;siz[La]=1;
while(p&&!son[p][c]) son[p][c]=La,p=link[p];
if(!p){link[La]=1;return;}
int q=son[p][c];if(len[q]==len[p]+1){link[La]=q;return;}
len[++cnt]=len[p]+1;Mc(son[cnt],son[q]);link[cnt]=link[q];link[q]=link[La]=cnt;ed[cnt]=ed[q];
while(p&&son[p][c]==q) son[p][c]=cnt,p=link[p];
}
vector<int> S[N];
int bl[N];
int bg[N],en[N],bh,df[N];
int trans[N][26];
ull sum[N],siz[N];
void make(int x,int La){
bg[x]=++bh;df[bh]=x;
for(int i:S[x]) make(i,x),ed[i]+len[x]<=n&&(trans[x][s[ed[i]+len[x]]-'a']=i),ed[x]=ed[i],sum[x]+=sum[i],siz[x]+=siz[i];
en[x]=bh;
}
int fa[20][N];
void build(){
for(int i=2;i<=cnt;i++) S[link[i]].push_back(i);
make(1,0);
for(int i=1;i<=cnt;i++) fa[0][i]=link[i];
for(int i=1;i<=18;i++) for(int j=1;j<=cnt;j++) fa[i][j]=fa[i-1][fa[i-1][j]];
}
vector<int> G[N];
void push(int x){
for(int i=0;i<26;i++) if(son[x][i]&&len[son[x][i]]==len[x]+1){
push(son[x][i]);
if(!bl[x]) bl[x]=bl[son[x][i]];
}
// gdb(x,bl[x]);
G[bl[x]].push_back(x);
}
int jump(int x,int k){
for(int i=18;~i;i--) if(len[fa[i][x]]>=k) x=fa[i][x];
return x;
}
}t1,t2;
int cnt,mx[N];
void dfs(int x,int y){
// gdb(x,y,t1.len[x],t2.len[y]);
if(t1.len[x]==t2.len[y]) t1.bl[x]=t2.bl[y]=++cnt,mx[cnt]=t1.len[x];
for(int i=0;i<26;i++) if(t1.son[x][i]&&t1.len[x]+1==t1.len[t1.son[x][i]]) dfs(t1.son[x][i],t1.len[x]==t2.len[y]?t2.trans[y][i]:y);
}
int i1[N],i2[N];
void build(){
for(int i=1;i<=n;i++) t1.extend(s[i]-'a'),i1[i]=t1.La,t1.sum[t1.La]=wr[i],t1.ed[t1.La]=i;
for(int i=n;i;i--) t2.extend(s[i]-'a'),i2[i]=t2.La,t2.sum[t2.La]=wl[i],t2.ed[t2.La]=i;
t1.build();t2.build();
dfs(1,1);
t1.push(1);t2.push(1);
}
ull ans[N];
struct ques{int l,r,id;ull w;}Q[N];int Qh;
ull qans[N];
void Solve(){
int i,j;scanf("%d%d%s",&n,&q,s+1);
for(i=1;i<=n;i++) scanf("%llu",&wl[i]);
for(i=1;i<=n;i++) scanf("%llu",&wr[i]);
build();
for(i=1;i<=q;i++){
int l1,r1,l2,r2;
scanf("%d%d%d%d",&l1,&r1,&l2,&r2);
int x=t2.jump(i2[l1],r1-l1+1),y=t1.jump(i1[r2],r2-l2+1);
int i1=t2.bl[x],i2=t1.bl[y],len1=r1-l1+1,len2=r2-l2+1;
if(t1.bl[y]==t2.bl[x]){
if(t2.len[x]+t1.len[y]-mx[i1]>t1.len[t1.link[y]]&&t2.len[x]+t1.len[y]-mx[i1]<max(len1,len2)) ans[i]-=t1.sum[y]*t2.sum[x]*t1.siz[y];
}else{
for(int j=t2.len[x]-len1+1;j<=t2.len[x]-(t2.len[t2.link[x]]+1);j++){
if(t1.bg[y]<=t1.bg[t1.G[i1][j]]&&t1.bg[t1.G[i1][j]]<=t1.en[y]) ans[i]-=t2.sum[x]*t1.sum[t1.G[i1][j]]*t2.siz[x];
}
for(int j=t1.len[y]-len2+1;j<=t1.len[y]-(t1.len[t1.link[y]]+1);j++){
if(t2.bg[x]<=t2.bg[t2.G[i2][j]]&&t2.bg[t2.G[i2][j]]<=t2.en[x]) ans[i]-=t1.sum[y]*t2.sum[t2.G[i2][j]]*t1.siz[y];
}
}
// gdb(ans[i]);
Q[++Qh]=(ques){t1.bg[y]-1,t2.bg[x]-1,i,1};
Q[++Qh]=(ques){t1.en[y],t2.en[x],i,1};
Q[++Qh]=(ques){t1.bg[y]-1,t2.en[x],i,-1ull};
Q[++Qh]=(ques){t1.en[y],t2.bg[x]-1,i,-1ull};
/*for(int j=t2.bg[x];j<=t2.en[x];j++) for(int h=t1.bg[y];h<=t1.en[y];h++)if(t2.bl[t2.df[j]]==t1.bl[t1.df[h]]){
if(t2.len[t2.df[j]]+t1.len[t1.df[h]]-mx[t2.bl[t2.df[j]]]>t1.len[t1.link[t1.df[h]]]){
ans[i]+=t2.sum[t2.df[j]]*t1.sum[t1.df[h]]*t2.siz[t2.df[j]];
}
}*/
// printf("%llu\n",ans[i]);
}
int l=0,r=0,Bl=max(n/sqrt(q),1.0);
sort(Q+1,Q+Qh+1,[&](ques x,ques y){return x.l/Bl^y.l/Bl?x.l<y.l:(x.l/Bl&1?x.r<y.r:x.r>y.r);});
for(i=1;i<=Qh;i++){
qans[i]=qans[i-1];
while(l<Q[i].l){
l++;
for(int j=1;j<=r;j++) if(t1.bl[t1.df[l]]==t2.bl[t2.df[j]]){
if(t2.len[t2.df[j]]+t1.len[t1.df[l]]-mx[t2.bl[t2.df[j]]]>t1.len[t1.link[t1.df[l]]]){
qans[i]+=t2.sum[t2.df[j]]*t1.sum[t1.df[l]]*t2.siz[t2.df[j]];
}
}
}
while(l>Q[i].l){
for(int j=1;j<=r;j++) if(t1.bl[t1.df[l]]==t2.bl[t2.df[j]]){
if(t2.len[t2.df[j]]+t1.len[t1.df[l]]-mx[t2.bl[t2.df[j]]]>t1.len[t1.link[t1.df[l]]]){
qans[i]-=t2.sum[t2.df[j]]*t1.sum[t1.df[l]]*t2.siz[t2.df[j]];
}
}
l--;
}
while(r<Q[i].r){
r++;
for(int j=1;j<=l;j++)if(t1.bl[t1.df[j]]==t2.bl[t2.df[r]]){
if(t2.len[t2.df[r]]+t1.len[t1.df[j]]-mx[t2.bl[t2.df[r]]]>t1.len[t1.link[t1.df[j]]]){
qans[i]+=t2.sum[t2.df[r]]*t1.sum[t1.df[j]]*t2.siz[t2.df[r]];
}
}
}
while(r>Q[i].r){
for(int j=1;j<=l;j++)if(t1.bl[t1.df[j]]==t2.bl[t2.df[r]]){
if(t2.len[t2.df[r]]+t1.len[t1.df[j]]-mx[t2.bl[t2.df[r]]]>t1.len[t1.link[t1.df[j]]]){
qans[i]-=t2.sum[t2.df[r]]*t1.sum[t1.df[j]]*t2.siz[t2.df[r]];
}
}
r--;
}
ans[Q[i].id]+=qans[i]*Q[i].w;
}
for(i=1;i<=q;i++) printf("%llu\n",ans[i]);
}
int main(){
int t=1;
// scanf("%d",&t);
while(t--) Solve();
cerr<<clock()*1.0/CLOCKS_PER_SEC<<'\n';
}
Details
Tip: Click on the bar to expand more detailed information
Subtask #1:
score: 7
Accepted
Test #1:
score: 7
Accepted
time: 13ms
memory: 43696kb
input:
500 500 aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaabaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa...
output:
15720454042420499810 4058077030882532408 14651762045124606089 4030024243931986061 18033423360813892607 9470601111824364484 3883374861354698625 16650831689368240202 8339028189650687576 2683289915379600554 13133811958066776394 14181220923901262251 18173739360450512256 13142314545999179754 148925491596...
result:
ok 500 lines
Test #2:
score: 7
Accepted
time: 7ms
memory: 42912kb
input:
500 500 zszzszszzszzszszzszszzszzszszzszzszszzszszzszzszszzszszzszzszszzszzszszzszszzszzszszzszzszszzszszzszzszszzszszzszzszszzszzszszzszszzszzszszzszszzszzszszzszzszszzszszzszzszszzszzszszzszszzszzszszzszszzszzszszzszzszszzszszzszzszszzszzszszzszszzszzszszzszszzszzszszzszzszszzszszzszzszszzszszzszz...
output:
4843650749197240262 7777110205341291111 533576317536031175 16712994243500559204 9988085877723856684 9644193924482321332 3247342125341043527 18152622312040037224 13045121434804725850 10593529771756855740 13316626648976199221 6181092693273210423 9148547538129213975 9376364571107435561 2140403332478526...
result:
ok 500 lines
Test #3:
score: 7
Accepted
time: 34ms
memory: 44396kb
input:
500 500 aaaaabbaabbabbbaabaabbabbabbbaaabaaaabbbbbbaaabaabbbbbbaabbaaaaababbaaaaabbbbababbabaabbbbbbbbaaaaaaabaabbabbbbaabbaabaaabbbabbaabbbabaabaaaaababbaabbabbbabbababbbaabbabaaabbbbaaabbbabbabaabbabbaaabbaabbabbbbaaaaaababaaaabaababbaabbabbabbbabbaabbbaabbaaababaaabbababbbabaababaabbbbbabbababaab...
output:
841375054012212333 13406426787139944226 6541986259052503362 10583258635957015782 11582649090627508617 4747829250201069768 11571422754704651998 14603866222879735665 8438246043626601023 16155298152184479844 9052925087624568857 18388444310571976215 13304308468056840286 18125780089857220122 363421144082...
result:
ok 500 lines
Test #4:
score: 7
Accepted
time: 29ms
memory: 43224kb
input:
500 500 sulasusulasusulasulasulsulasusulasususulasulasulsulasulasulsulasulasulsulasususulasulasulsulasulasulsulasulasulsulasusulasulasulsulasulasulsulasulasulsulasusulasulasulsulasulasulsulasulasulsulasulasulsulasususulasulasulsulasulasulsulasulasulsulasusulasulasulsulasulasulsulasusulasusulasusulas...
output:
2320755102639148175 17108462705447992416 6030359132551843296 889683039894413148 10901851555398837076 1991544941914879425 9087724446342520941 5134546535199286414 12947484109492427089 5962550827492657739 4877066450610765849 6699323319072695780 11167645157062070624 13985172887966350800 8075429763917070...
result:
ok 500 lines
Test #5:
score: 7
Accepted
time: 13ms
memory: 44216kb
input:
500 500 bbbbbbqouvtudkzorrxinacvncytgmtbbbbbbfyfzxjdqlcaadccvsbbbbbbqouvtudkzorrxinacvncytgmtbbbbbbbbbbbbqouvtudkzorrxinacvncytgmtbbbbbbfyfzxjdqlcaadccvsbbbbbbqouvtudkzorrxinacvncytgmtbbbbbbbbbbbbqouvtudkzorrxinacvncytgmtbbbbbbfyfzxjdqlcaadccvsbbbbbbqouvtudkzorrxinacvncytgmtbbbbbbbbbbbbqouvtudkzorrx...
output:
18295637548117042088 6105463594888898313 15681140870484623884 17957090271580958329 11763132903578154240 17769627666201366836 16493946443969420940 12712093409624537595 2436698665645215125 8863273927617841787 5065586857868462806 8771649105206144878 6715985691821336097 8851433094837196039 7055234226266...
result:
ok 500 lines
Test #6:
score: 7
Accepted
time: 27ms
memory: 44100kb
input:
500 500 yyyayyayyyayyayyyayyayyyayyyayyyayyayyyyayyayyyayyyayyayyyayyyayyayyayyyyayyyayyyayyayyayyyayyayyayyyayyayyayyyayyyyyayyayyyyayyayyyayyayyyayyyayyyayyayyyayyayyyyayyyayyayyayyyayyayyyayyyyayyyyayyayyyayyayyayyyayyayyayyyyayyayyyayyayyayyyayyyyayyayyayyyayyayyayyyayyayyayyyyayyyayyyayyyyayyay...
output:
6159560444195180556 5294852391541430076 6195718271241091926 7959984071139675340 1598729415848168155 4879964117998052348 2279721248493220290 2026655128556749470 9803272548967597498 1028236064772678471 5410852487707111065 3600180224455323043 60239358603452318 2179897463397058094 16626503365867372202 3...
result:
ok 500 lines
Test #7:
score: 7
Accepted
time: 16ms
memory: 43788kb
input:
500 500 fffffffffffxfqifffnmogfffxfqiffffffffxfqifffnmogfffxfqiffffffxfqifffnmogfffxfqifffffffffffxfqifffnmogfffxfqiffffffffxfqifffnmogfffxfqifffffffffffffxfqifffnmogfffxfqiffffffffxfqifffnmogfffxfqifffffffffxfqifffnmogfffxfqiffffffffxfqifffnmogfffxfqiffffffffxfqifffnmogfffxfqiffffffffxfqifffnmogfff...
output:
6263422992304461664 10533199195660359295 11930245273187149005 380050211417129795 8399013088311259527 7005867409130681392 6872331929648615383 11661502418569897193 18027795221888639599 8932010711134684820 6331436398298306214 14599171184201697655 16632037523890780117 10373998601812781913 16089838760431...
result:
ok 500 lines
Subtask #2:
score: 0
Time Limit Exceeded
Dependency #1:
100%
Accepted
Test #8:
score: 15
Accepted
time: 1645ms
memory: 47148kb
input:
5000 5000 aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa...
output:
12046186417900804485 3907200386448639860 16785470842023310270 13867397394972404837 17085398553187209926 11495532885795119226 12151570815045120790 4174337022608077877 6326694597133262496 5169007805286709535 14463367619086789683 627371028328452442 16378439470252322552 8286285259552634235 4646456254657...
result:
ok 5000 lines
Test #9:
score: 15
Accepted
time: 1702ms
memory: 48792kb
input:
5000 5000 zszzszszzszzszszzszszzszzszszzszzszszzszszzszzszszzszszzszzszszzszzszszzszszzszzszszzszzszszzszszzszzszszzszszzszzszszzszzszszzszszzszzszszzszszzszzszszzszzszszzszszzszzszszzszzszszzszszzszzszszzszszzszzszszzszzszszzszszzszzszszzszzszszzszszzszzszszzszszzszzszszzszzszszzszszzszzszszzszszzs...
output:
735272585091871492 2425556370857741242 8489901764977622765 9888756352722135114 3110111362923242403 9113727892967247055 13146817111311386931 5955980702194075793 11763307103994511020 11636340521260903139 13880517640770645879 18280247001976771986 9041182678245966519 902847942097731925 17037540142883387...
result:
ok 5000 lines
Test #10:
score: 0
Time Limit Exceeded
input:
5000 5000 bababbaabbbbbabbabaababbbaabababbbbaaabbaabbbbaaabaaaaabbababaababbbabbabbaaabbaaaaaababbababbbbbabbbbbaaabbabababaaababbababaabaaaaaabaaabbabaaaabaabaaaaaabbaaaaaaabbbbbabbabababbbabababaabaabbbbbabaabbbaaabbaaababbabaabaabbababaaaaaaabaaaabaabbbabbaaaababbabbaaabbbbabbabbaabbabbbbabaabbb...
output:
result:
Subtask #3:
score: 0
Skipped
Dependency #2:
0%
Subtask #4:
score: 0
Time Limit Exceeded
Test #22:
score: 0
Time Limit Exceeded
input:
100000 100000 zbbabaabbaababbabaababbaabbabaabbaababbaabbabaababbabaabbaababbabaababbaabbabaababbabaabbaababbaabbabaabbaababbabaababbaabbabaabbaababbaabbabaababbabaabbaababbaabbabaabbaababbabaababbaabbabaababbabaabbaababbabaababbaabbabaabbaababbaabbabaababbabaabbaababbabaababbaabbabaababbabaabbaabab...
output:
result:
Subtask #5:
score: 0
Skipped
Dependency #4:
0%
Subtask #6:
score: 0
Skipped
Dependency #2:
0%
Subtask #7:
score: 0
Skipped
Dependency #3:
0%