QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#454393 | #7748. Karshilov's Matching Problem II | Reunite | TL | 1ms | 5704kb | C++14 | 1.1kb | 2024-06-24 20:56:59 | 2024-06-24 20:56:59 |
Judging History
answer
#include <cstdio>
#include <cstring>
#include <algorithm>
#define ll unsigned long long
using namespace std;
int n,m;
char c[150005];
char s[150005];
ll h[150005];
ll hs[150005];
ll bin[150005];
ll w[150005];
int lcp[150005];
inline void in(int &n){
n=0;
char c=getchar();
while(c<'0' || c>'9') c=getchar();
while(c>='0'&&c<='9') n=n*10+c-'0',c=getchar();
return ;
}
inline ll get(int l,int r){return hs[r]-hs[l-1]*bin[r-l+1];}
int main(){
// freopen("qwq.in","r",stdin);
in(n),in(m);
scanf("%s",c+1);
scanf("%s",s+1);
bin[0]=1;
for(int i=1;i<=n;i++) bin[i]=bin[i-1]*131;
for(int i=1;i<=n;i++) h[i]=h[i-1]*131+c[i];
for(int i=1;i<=n;i++) hs[i]=hs[i-1]*131+s[i];
for(int i=1;i<=n;i++){
int l=0,r=n-i,mid;
while(l<=r){
mid=(l+r)>>1;
if(get(i,i+mid)==h[1+mid]) lcp[i]=mid+1,l=mid+1;
else r=mid-1;
}
}
for(int i=1;i<=n;i++){int x;in(x);w[i]=w[i-1]+x;}
while(m--){
int l,r;
in(l),in(r);
ll ans=0;
for(int i=l;i<=r;i++) ans+=w[min(r-i+1,lcp[i])];
printf("%lld\n",ans);
}
return 0;
}
详细
Test #1:
score: 100
Accepted
time: 0ms
memory: 3600kb
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: 1ms
memory: 5704kb
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
Time Limit Exceeded
input:
150000 150000 aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaabaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaabaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa...
output:
108147037823514 221878585246974 455339807727642 440286198422821 148115747906237 88695249853257 351159618462315 58850354020997 65824434822005 270158033672354 197732558443069 298193894693053 239511187032650 28139154231325 408380171835227 268053430937402 32417121185965 104813548228675 44074926058619 78...