QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#260290 | #7748. Karshilov's Matching Problem II | ucup-team1321 | WA | 114ms | 22016kb | C++20 | 2.1kb | 2023-11-22 00:07:46 | 2023-11-22 00:07:48 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
int z[150010],Link[150010],t=0,p[150010],nex[150010],w[150010],g[150010],c[150010];
int h[150010],n,m,Max[150010][20],sw[150010];
char S[150010],T[150010];
struct dsa
{
int v,nex;
}e[150010];
void Exkmp()
{
z[0]=0;
for (int i=1,l=0,r=0;i<n;i++) {
z[i]=0;
if (i<=r) z[i]=min(z[i-l],r-i+1);
while (i+z[i]<n&&S[i+z[i]]==S[z[i]]) z[i]++;
if (i+z[i]-1>r) l=i,r=i+z[i]-1;
}
for (int i=0,l=0,r=0;i<n;i++) {
p[i]=0;
if (i<=r) p[i]=min(z[i-l],r-i+1);
while (i+p[i]<n&&T[i+p[i]]==S[p[i]]) p[i]++;
if (i+p[i]-1>r) l=i,r=i+p[i]-1;
}
return ;
}
void Insert(int x,int y)
{
e[++t].nex=Link[x];e[t].v=y;Link[x]=t;
return ;
}
void KMP()
{
int now=-1;nex[0]=-1;
Insert(0,1);
for (int i=1;i<n;i++) {
while (now!=-1&&S[now+1]!=S[i]) now=nex[now];
if (S[now+1]==S[i]) now++;
nex[i]=now;
Insert(now+1,i+1);
}
return ;
}
void dfs(int now)
{
if (now) c[now]+=w[now-1];
for (int i=Link[now];i;i=e[i].nex) {
c[e[i].v]=c[now];
dfs(e[i].v);
}
return ;
}
int Get_Max(int l,int r)
{
if (l>r) return 0;
int len=log2(r-l+1);
return max(Max[l][len],Max[r-(1<<len)+1][len]);
}
int Find(int l,int r,int lim)
{
if (l==r) return l;
int mid=(l+r)>>1;
// cout<<l<<' '<<r<<' '<<Get_Max(l,mid)<<' '<<lim<<endl;
if (Get_Max(l,mid)>=lim) return Find(l,mid,lim);
return Find(mid+1,r,lim);
}
int main()
{
ios::sync_with_stdio(false);
cin.tie(nullptr);
cin>>n>>m;
cin>>S;cin>>T;
for (int i=0;i<n;i++) cin>>w[i];
Exkmp();
for (int i=0;i<n;i++) Max[i][0]=i+p[i]-1;
for (int i=1;i<=17;i++)
for (int j=0;j<n-(1<<i)+1;j++)
Max[j][i]=max(Max[j][i-1],Max[j+(1<<(i-1))][i-1]);
KMP();dfs(0);
g[0]=w[0];
for (int i=1;i<n;i++) g[i]=g[i-1]+c[i+1];
sw[0]=w[0];
for (int i=1;i<n;i++) sw[i]=sw[i-1]+w[i];
h[0]+=sw[p[0]-1];
for (int i=1;i<n;i++) h[i]=h[i-1]+sw[p[i]-1];
for (int i=1;i<=m;i++) {
int l,r;
cin>>l>>r;
l--;r--;
int k=Find(l,r,r);
// cout<<k<<' '<<g[r-k]<<endl;
int ans=g[r-k]+h[k-1];
if (l) ans-=h[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: 11928kb
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: 2ms
memory: 11920kb
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: 114ms
memory: 22016kb
input:
150000 150000 aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaabaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaabaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa...
output:
-238689766 574735614 259907610 510975269 -494263619 -119776439 -1202625941 712131205 -233956491 295786658 854069821 -1389700931 -1959196086 -1471492067 1796429659 227026746 -1291964243 -833662909 -28332933 1521598991 -210103850 888469404 604921440 813327493 -853316457 1964711175 -669853735 -13877485...
result:
wrong answer 1st lines differ - expected: '108147037823514', found: '-238689766'