QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#265808 | #7748. Karshilov's Matching Problem II | PlentyOfPenalty | WA | 53ms | 38840kb | C++17 | 2.0kb | 2023-11-25 21:20:10 | 2023-11-25 21:20:11 |
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[i-1]+ f[i];
//printf("FF[%d]=%d\n",i,f[z[i]]);
}
while(m--)
{
int ql,qr;
cin>>ql>>qr;
int l=ql,r=qr;
while(l<r)
{
int mid=(l+r)>>1;
if(T.Qmax(ql,mid)>=r)r=mid;
else l=mid+1;
}
//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: 2ms
memory: 13956kb
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: 15876kb
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: 53ms
memory: 38840kb
input:
150000 150000 aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaabaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaabaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa...
output:
108147037823514 221659613260966 455243521715206 439844773329154 147945961311281 88695249853257 351159618462315 58850354020997 65774373196323 270158033672354 197732558443069 297955518532968 239511187032650 28101203842760 408072335744033 268006799700960 32417121185965 104465225670393 43823519604611 78...
result:
wrong answer 2nd lines differ - expected: '221878585246974', found: '221659613260966'