QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#282342 | #7748. Karshilov's Matching Problem II | grass8cow# | WA | 106ms | 63308kb | C++17 | 1.8kb | 2023-12-11 20:05:00 | 2023-12-11 20:05:01 |
Judging History
answer
#include<bits/stdc++.h>
using namespace std;
int n,m,kmp[150100],is[150100],f[150100][20];
int z[150100],q[151000];
char s[150100],t[150100];
#define ll long long
ll w[151000],ds[150010];
int T[150100],cn,ls[10100000],rs[10100000];
ll su[10100000];
void up(int &p,int lst,int l,int r,int x,int z){
p=++cn,su[p]=su[lst]+z,ls[p]=ls[lst],rs[p]=rs[lst];
if(l==r)return;int mid=(l+r)>>1;
if(x<=mid)up(ls[p],ls[lst],l,mid,x,z);
else up(rs[p],rs[lst],mid+1,r,x,z);
}
ll ask(int p,int l,int r,int x){
if(l==r)return su[p];
int mid=(l+r)>>1;
if(x<=mid)return ask(ls[p],l,mid,x);
return su[ls[p]]+ask(rs[p],mid+1,r,x);
}
int main(){
scanf("%d%d%s%s",&n,&m,s+1,t+1);
for(int i=2,j=0;i<=n;i++){
while(j&&s[j+1]!=s[i])j=kmp[j];
if(s[j+1]==s[i])j++;kmp[i]=j,f[i][0]=j;
}
z[1]=n;
for(int i=2,j=0,mr=0;i<=n;i++){
if(i<=mr)z[i]=min(mr-i+1,z[i-j+1]);
while(i+z[i]<=n&&s[1+z[i]]==s[i+z[i]])z[i]++;
if(i+z[i]-1>mr)j=i,mr=i+z[i]-1;
}
for(int i=1,j=0,mr=0;i<=n;i++){
if(i<=mr)q[i]=min(mr-i+1,z[i-j+1]);
while(i+q[i]<=n&&s[1+q[i]]==t[i+q[i]])q[i]++;
if(i+q[i]-1>mr)j=i,mr=i+q[i]-1;
}
for(int j=1;j<20;j++)for(int i=1;i<=n;i++){
if(!f[i][j-1])f[i][j]=0;
else f[i][j]=f[f[i][j-1]][j-1];
}
for(int i=1,j=0;i<=n;i++){
scanf("%lld",&w[i]);w[i]+=w[i-1];
while(j&&s[j+1]!=t[i])j=kmp[j];
if(s[j+1]==t[i])j++;is[i]=j;
}
for(int i=1;i<=n;i++)ds[i]=ds[kmp[i]]+w[i];
for(int i=n;i;i--){
T[i]=T[i+1];
if(q[i]&&i+q[i]<=n)up(T[i],T[i],1,n,i+q[i],w[q[i]]);
}
for(int i=1,l,r;i<=m;i++){
scanf("%d%d",&l,&r);
ll an=ask(T[l],1,n,r);
//for(int j=l;j<=r;j++)if(j+q[j]-1<r)an+=w[q[j]];
int u=is[r];
if(u>r-l+1){
for(int j=19;j>=0;j--)
if(f[u][j]>r-l+1)u=f[u][j];
u=kmp[u];
}
an+=ds[u];
printf("%lld\n",an);
}
return 0;
}
詳細信息
Test #1:
score: 100
Accepted
time: 1ms
memory: 3740kb
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: 3688kb
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: 106ms
memory: 63308kb
input:
150000 150000 aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaabaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaabaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa...
output:
25846874497562 54361975801086 109917062946842 106760513051941 36283389452989 22256400751433 83952523108971 14341607932549 15367159028597 65240849012898 48783092617789 71544175515837 56992256821834 6788871802909 98111734372187 64171333396282 8077541519533 25180559593539 10621425790075 1766753157647 1...
result:
wrong answer 1st lines differ - expected: '108147037823514', found: '25846874497562'