QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#276900 | #7748. Karshilov's Matching Problem II | KomejiNora | WA | 1ms | 5696kb | C++14 | 3.1kb | 2023-12-06 12:33:53 | 2023-12-06 12:33:54 |
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
*/
詳細信息
Test #1:
score: 0
Wrong Answer
time: 1ms
memory: 5696kb
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 262
result:
wrong answer 5th lines differ - expected: '38', found: '262'