QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#455149 | #7748. Karshilov's Matching Problem II | Reunite | TL | 1954ms | 23568kb | C++14 | 2.2kb | 2024-06-25 20:33:36 | 2024-06-25 20:33:36 |
Judging History
answer
#include <cstdio>
#include <cstring>
#include <algorithm>
#define ll unsigned long long
using namespace std;
int n,m;
char c[150005];
char s[150005];
ll w[150005];
ll h[150005];
ll sm[150005];
ll sw[150005];
ll sp[150005];
ll hs[150005];
ll bin[150005];
int lg[150005];
int lcp[150005];
int nxt[150005];
int mx[20][150005];
inline void in(int &n){
n=0;
char c=getchar();
while(c<'0' || c>'9') c=getchar();
while(c>='0'&&c<='9') n=n*10+c-'0',c=getchar();
return ;
}
inline ll get(int l,int r){return hs[r]-hs[l-1]*bin[r-l+1];}
inline void KMP(){
int j=0;
for(int i=2;i<=n;i++){
while(c[j+1]!=c[i]&&j) j=nxt[j];
if(c[j+1]==c[i]) j++;
nxt[i]=j;
}
return ;
}
inline int ask(int l,int r){
int len=lg[r-l+1];
return max(mx[len][l],mx[len][r-(1<<len)+1]);
}
int main(){
// freopen("qwq.in","r",stdin);
// freopen("qwq.out","w",stdout);
in(n),in(m);
scanf("%s",c+1);
scanf("%s",s+1);
KMP();
for(int i=1;i<=n;i++){int x;in(x);w[i]=x,sw[i]=sw[i-1]+x;}
for(int i=1;i<=n;i++){
int j=i;
while(j) sm[i]+=w[j],j=nxt[j];
// printf("nxt[%d]= %d %llu\n",i,nxt[i],sm[i]);
}
bin[0]=1;
for(int i=1;i<=n;i++) bin[i]=bin[i-1]*131;
for(int i=1;i<=n;i++) h[i]=h[i-1]*131+c[i];
for(int i=1;i<=n;i++) hs[i]=hs[i-1]*131+s[i];
for(int i=1;i<=n;i++){
int l=0,r=n-i,mid;
while(l<=r){
mid=(l+r)>>1;
if(get(i,i+mid)==h[1+mid]) lcp[i]=mid+1,l=mid+1;
else r=mid-1;
}
}
for(int i=2;i<=n;i++) lg[i]=lg[i>>1]+1;
for(int i=1;i<=n;i++) mx[0][i]=lcp[i]+i,sp[i]=sp[i-1]+sw[lcp[i]];
for(int j=1;j<=lg[n];j++)
for(int i=1;i+(1<<j)-1<=n;i++)
mx[j][i]=max(mx[j-1][i],mx[j-1][i+(1<<(j-1))]);
// for(int i=1;i<=n;i++) printf("lcp[%d]= %d\n",i,lcp[i]);
while(m--){
int L,R;
in(L),in(R);
if(ask(L,R)<=R+1){printf("%llu\n",sp[R]-sp[L-1]);continue;}
int l=L,r=R,mid,pos=0;
while(l<=r){
mid=(l+r)>>1;
if(ask(L,mid)>R+1) pos=mid,r=mid-1;
else l=mid+1;
}
ll ans=sp[pos-1]-sp[L-1];
// ll ans=0;
// for(int i=L;i<pos;i++) ans+=sw[min(R-i+1,lcp[i])];
// printf("%d %llu %d %lld\n",pos,ans,R-pos+1,sm[R-pos+1]);
// ans+=sm[R-pos+1];
for(int i=pos;i<=R;i++) ans+=sw[min(R-i+1,lcp[i])];
printf("%llu\n",ans);
}
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 0ms
memory: 5744kb
input:
8 5 abbabaab aababbab 1 2 4 8 16 32 64 128 1 1 2 3 3 5 4 7 1 8
output:
1 3 3 16 38
result:
ok 5 lines
Test #2:
score: 0
Accepted
time: 0ms
memory: 7712kb
input:
15 4 heheheheehhejie heheheheheheheh 3 1 4 1 5 9 2 6 5 3 5 8 9 7 9 2 3 4 8 2 6 1 15
output:
3 13 13 174
result:
ok 4 lines
Test #3:
score: 0
Accepted
time: 63ms
memory: 23568kb
input:
150000 150000 aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaabaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaabaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa...
output:
108147037823514 221878585246974 455339807727642 440286198422821 148115747906237 88695249853257 351159618462315 58850354020997 65824434822005 270158033672354 197732558443069 298193894693053 239511187032650 28139154231325 408380171835227 268053430937402 32417121185965 104813548228675 44074926058619 78...
result:
ok 150000 lines
Test #4:
score: 0
Accepted
time: 189ms
memory: 23560kb
input:
150000 150000 bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb...
output:
528900815691571 112638556022185 514229554849356 2216206133639915 295388515578381 1658587138269057 652142121207636 166322478628783 490195029871161 1191292846892788 1468501126902703 487990867773908 55994169916421 568966315599642 2522992078581539 2339888502167342 2881203249819745 154700081279584 152537...
result:
ok 150000 lines
Test #5:
score: 0
Accepted
time: 221ms
memory: 23556kb
input:
150000 150000 aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa...
output:
386150762496368 2769669390423140 1025785181073675 1707930121656247 332135612349048 113937878332307 1128519694119799 476402941643931 980441978140407 1004994648999517 676169371268202 2607965889355671 273766796671958 711480908011402 71754482763611 400453994282744 975387094872830 810536618300388 2229061...
result:
ok 150000 lines
Test #6:
score: 0
Accepted
time: 1954ms
memory: 23548kb
input:
150000 150000 bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb...
output:
25254725752128652 23497896664383313 15195391464047263 41143572895791434 7513384536045842 8871310939247699 17423823866879881 14601201534396958 6203483865940624 24953281161800570 24776576029495768 1687640411226 31563282955464371 29947970968962218 1149303801639767 5806503923049299 11201332188941891 116...
result:
ok 150000 lines
Test #7:
score: -100
Time Limit Exceeded
input:
150000 150000 aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa...
output:
4570597927951323 11761063519994765 289982253793476 15684854914181269 19579321543184889 459972639580770 15246459216963247 1833393949769247 22425556248999709 11209560100586843 2883954996867615 14371655418173335 29207399108721 5943079608253242 1664456073054861 27405606916506455 23082758946788297 381175...