QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#719270 | #7748. Karshilov's Matching Problem II | xzzduang | WA | 52ms | 42092kb | C++23 | 2.2kb | 2024-11-06 23:52:35 | 2024-11-06 23:52:35 |
Judging History
answer
#include<iostream>
#include<stdio.h>
#include<ctype.h>
#include<string.h>
#include<vector>
#define ll long long
#define ld long double
#define fi first
#define se second
#define lowbit(x) ((x)&-(x))
#define popcount(x) __builtin_popcount(x)
#define inf 0x3f3f3f3f
#define infll 0x3f3f3f3f3f3f3f3f
#define umap unordered_map
#define all(x) x.begin(),x.end()
#define mk make_pair
#define pb push_back
#define ckmax(x,y) x=max(x,y)
#define ckmin(x,y) x=min(x,y)
#define rep(i,l,r) for(int i=l;i<=r;++i)
#define per(i,r,l) for(int i=r;i>=l;--i)
#define int long long
#define pii pair<int,int>
using namespace std;
inline int read(){
int x=0,f=0; char ch=getchar();
while(!isdigit(ch)) f|=(ch==45),ch=getchar();
while(isdigit(ch)) x=(x<<3)+(x<<1)+(ch^48),ch=getchar();
return f?-x:x;
}
#define N 150005
int ans[N],nxt[N],n,m,w[N],sw[N],mth[N],fa[N][18],sum[N],c[N],z[N];
char S[2*N],T[N];
vector<pii> qry[N];
inline void update(int x,int v){
for(int i=x;i<=n;i+=lowbit(i)) c[i]+=v;
}
inline int query(int x){
int res=0;
for(int i=x;i;i-=lowbit(i)) res+=c[i];
return res;
}
signed main(){
n=read(),m=read();
scanf("%s%s",S+1,T+1);
for(int i=1;i<=n;++i) w[i]=read(),sw[i]=sw[i-1]+w[i];
sum[1]=sw[1];
for(int i=2,j=0;i<=n;++i){
while(j && S[j+1]!=S[i]) j=nxt[j];
if(S[j+1]==S[i]) j++;
nxt[i]=j;
fa[i][0]=j;
sum[i]=sum[nxt[i]]+sw[i];
}
for(int j=1;j<=17;++j){
for(int i=1;i<=n;++i){
fa[i][j]=fa[fa[i][j-1]][j-1];
}
}
for(int i=1,j=0;i<=n;++i){
while(j && S[j+1]!=T[i]) j=nxt[j];
if(S[j+1]==T[i]) j++;
mth[i]=j;
}
for(int i=1;i<=m;++i){
int l=read(),r=read();
int p=mth[r];
if(p>r-l+1){
for(int j=17;~j;--j){
if(fa[p][j]>r-l+1) p=fa[p][j];
}
p=fa[p][0];
}
ans[i]=sum[p];
qry[l].pb({r,i});
}
S[n+1]='#';
for(int i=1;i<=n;++i) S[n+i+1]=T[i];
for(int i=2,l=0,r=0;i<=2*n+1;++i){
if(r>=i) z[i]=min(z[i-l+1],r-i+1);
while(i+z[i]<=2*n+1 && S[i+z[i]]==S[z[i]+1]) z[i]++;
if(i+z[i]-1>r) r=i+z[i]-1,l=i;
}
for(int i=n;i>=1;--i){
update(i+z[n+i+1],sw[z[n+i+1]]);
for(auto v:qry[i]){
ans[v.se]+=query(v.fi);
}
}
for(int i=1;i<=m;++i) printf("%lld\n",ans[i]);
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 3ms
memory: 16040kb
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: 3ms
memory: 16236kb
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: 52ms
memory: 42092kb
input:
150000 150000 aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaabaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaabaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa...
output:
108147037823966 221878585247980 455339807728367 440286198423126 148115747906864 88695249854414 351159618462764 58850354021351 65824434822775 270158033673488 197732558443426 298193894693915 239511187033047 28139154231561 408380171835742 268053430937934 32417121186720 104813548229217 44074926059072 78...
result:
wrong answer 1st lines differ - expected: '108147037823514', found: '108147037823966'