QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#265838 | #7748. Karshilov's Matching Problem II | PlentyOfPenalty | WA | 57ms | 37948kb | C++17 | 2.2kb | 2023-11-25 21:36:39 | 2023-11-25 21:36:41 |
Judging History
answer
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int MAXN = 300011;
int n,z[MAXN],nxt[MAXN];
void Z(char* s,int n){
int mx=1,mi=1;
z[1]=0;
for(int i=2;i<=n;++i){
if(i>mx)z[i]=0;
else z[i]=min(mx-i+1,z[i-mi+1]);
while(i+z[i]<=n&&s[i+z[i]]==s[z[i]+1])++z[i];
if(i+z[i]-1>mx)mx=i+z[i]-1,mi=i;
}
}
void get_next(char* s,int n)
{
int j=0;
for(int i=2;i<=n;++i)
{
while(j&&s[j+1]!=s[i])j=nxt[j];
if(s[j+1]==s[i])++j;
nxt[i]=j;
}
}
struct ST
{
int f[19][MAXN],lg[MAXN];
void init(int* arr,int n)
{
lg[1]=0;
for(int i=2;i<=n;++i)lg[i]=lg[i>>1]+1;
for(int i=1;i<=n;++i)f[0][i]=arr[i];
for(int k=1;k<19;++k)
for(int i=1;i+(1<<k)-1<=n;++i)
f[k][i]=max(f[k-1][i],f[k-1][i+(1<<(k-1))]);
}
int Qmax(int l,int r)
{
int k=lg[r-l];
return max(f[k][l],f[k][r-(1<<k)+1]);
}
}T;
char s[MAXN],t[MAXN];
int seq[MAXN],pos[MAXN];
ll f[MAXN],sum[MAXN];
ll sumt[MAXN],sums[MAXN];
int main()
{
cin.tie(0)->ios::sync_with_stdio(0);
int n,m;
cin>>n>>m;
cin>>(s+1)>>(t+1);
for(int i=1;i<=n;++i)s[n+i]=t[i];
Z(s,n*2);
for(int i=1;i<=n;++i)
{
z[i]=min(n,z[n+i]);
seq[i]=i+z[i]-1;
}
T.init(seq,n);
get_next(s,n);
for(int i=1;i<=n;++i)
{
int w;
cin>>w;
f[i]=f[nxt[i]]+w;
sum[i]=sum[i-1]+w;
//printf("F[%d]=%d\n",i,f[i]);
}
for(int i=1;i<=n;++i)
{
sumt[i]=sumt[i-1]+ sum[z[i]];
sums[i]=sums[nxt[i]]+ sum[i];
//printf("FF[%d]=%d\n",i,f[z[i]]);
}
while(m--)
{
int ql,qr;
cin>>ql>>qr;
int l=ql,r=qr+1;
while(l<r)
{
int mid=(l+r)>>1;
if(T.Qmax(ql,mid)>=qr)r=mid;
else l=mid+1;
}
if(l>qr)
{
cout<<sumt[qr]-sumt[ql-1]<<'\n';
continue;
}
//cout<<"De:l="<<l<<'\n';
ll ans=sumt[l-1]-sumt[ql-1];
ans+=sums[qr-l+1];
cout<<ans<<'\n';
}
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 0ms
memory: 15636kb
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: 15660kb
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: 57ms
memory: 37948kb
input:
150000 150000 aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaabaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaabaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa...
output:
108147037823514 221462618396888 455339807727642 440286198422821 148115747906237 88695249853257 351159618462315 58850354020997 65824434822005 270158033672354 197732558443069 297777927842967 239511187032650 28139154231325 408380171835227 267637464087316 32417121185965 104813548228675 43658959208533 78...
result:
wrong answer 2nd lines differ - expected: '221878585246974', found: '221462618396888'