QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#276268 | #7748. Karshilov's Matching Problem II | KomejiNora | WA | 62ms | 26592kb | C++14 | 3.1kb | 2023-12-05 19:08:29 | 2023-12-05 19:08:29 |
Judging History
answer
#include<bits/stdc++.h>
using namespace std;
#define ll long long
const int N=2e5+5;
int n,q;
ll a[N];
char s[N],t[N];
int len[N];
ll sum[N];
ll f[N];
int nxt[N];
void kmp() {
int j=0;nxt[1]=0;f[1]=a[1];
for(int i=2;i<=n;i++) {
f[i]=a[i]-a[i-1];
while(j&&s[j+1]!=s[i]) j=nxt[j];
if(s[j+1]==s[i]) j++;
nxt[i]=j;
f[i]+=f[j];
}
for(int i=1;i<=n;i++) f[i]+=f[i-1];
// cerr<<"---------------------------\n";
// cerr<<"f : "<<endl;
// for(int i=1;i<=n;i++) {
// cerr<<"f["<<i<<"] = "<<f[i]<<endl;
// }
return;
}
void exkmp() {
int p=0,k=2;
nxt[1]=n;
while(s[1+nxt[2]]==s[2+nxt[2]]) nxt[2]++;
p=2+nxt[2]-1;
for(int i=3;i<=n;i++) {
int l=nxt[1+i-k];
if(l+i<=p) nxt[i]=l;
else {
nxt[i]=max(0,p-i+1);
while(s[1+nxt[i]]==s[i+nxt[i]]) nxt[i]++;
}
if(i+nxt[i]-1>p) p=i+nxt[i]-1,k=i;
}
// cerr<<"---------------------------\n";
// cerr<<"len : "<<endl;
// for(int i=1;i<=n;i++) {
// cerr<<"nxt["<<i<<"] = "<<nxt[i]<<endl;
// }
//-------------------------------------------
p=0,k=1;
len[1]=0;
while(s[1+len[1]]==t[1+len[1]]) len[1]++;
p=1+len[1]-1;
for(int i=2;i<=n;i++) {
int l=nxt[1+i-k];
if(l+i<=p) len[i]=l;
else {
len[i]=max(0,p-i+1);
while(s[1+len[i]]==t[i+len[i]]) len[i]++;
}
if(i+len[i]-1>p) p=i+len[i]-1,k=i;
}
return;
}
int st[N][25],lg[N];
void init() {
exkmp();
// cerr<<"---------------------------\n";
// cerr<<"len : "<<endl;
for(int i=1;i<=n;i++) {
sum[i]=sum[i-1]+a[len[i]];
st[i][0]=i+len[i]-1;
// cerr<<"len["<<i<<"] = "<<len[i]<<endl;
}
lg[0]=-1;
for(int i=1;i<=n;i++) lg[i]=lg[i>>1]+1;
for(int k=1;k<=lg[n];k++) {
for(int i=1;i+(1<<k)-1<=n;i++) {
st[i][k]=max(st[i][k-1],st[i+(1<<(k-1))][k-1]);
}
}
kmp();
return;
}
int ask(int l,int r) {
int len=lg[r-l+1];
return max(st[l][len],st[r-(1<<len)+1][len]);
}
int calc(int L,int R) {
int l=L,r=R,ans=L-1,mid;
while(l<=r) {
mid=(l+r)>>1;
if(ask(l,mid)<=r) {
ans=mid;
l=mid+1;
}
else r=mid-1;
}
return ans;
}
int main() {
scanf("%d%d",&n,&q);
scanf("%s",s+1);
scanf("%s",t+1);
for(int i=1;i<=n;i++) scanf("%d",&a[i]),a[i]+=a[i-1];
init();
while(q--) {
int l,r;scanf("%d%d",&l,&r);
int pos=calc(l,r);
// cerr<<"pos = "<<pos<<endl;
// cerr<<"sum ["<<l<<", "<<pos<<"] = "<<sum[pos]-sum[l-1]<<" , "<<"pre ["<<pos+1<<", "<<r<<"] = "<<f[r-pos]<<endl;
ll ans=sum[pos]-sum[l-1];
if(r>=pos) ans+=f[r-pos];
// printf("ans = %lld\n",ans);
printf("%lld\n",ans);
}
return 0;
}
/*
8 5
abbabaab
aababbab
1 2 4 8 16 32 64 128
1 1
2 3
3 5
4 7
1 8
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
*/
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 1ms
memory: 5924kb
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: 5932kb
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: -100
Wrong Answer
time: 62ms
memory: 26592kb
input:
150000 150000 aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaabaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaabaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa...
output:
108147037823514 221659613260966 455243521715206 439844773329154 147945961311281 88695249853257 351159618462315 58850354020997 65774373196323 270158033672354 197732558443069 297955518532968 239511187032650 28101203842760 408024014274680 268006799700960 32417121185965 104465225670393 43823519604611 78...
result:
wrong answer 2nd lines differ - expected: '221878585246974', found: '221659613260966'