QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#260272 | #7748. Karshilov's Matching Problem II | ucup-team1525 | WA | 60ms | 11760kb | C++17 | 1.8kb | 2023-11-21 23:28:40 | 2023-11-21 23:28:40 |
Judging History
answer
#include<bits/stdc++.h>
using namespace std;
using ll=long long;
const int N=1.5e5;
int n,m;
char s[N+5],t[N+5];
ll w[N+5];
int zs[N+5],zt[N+5];
int pre[N+5];
ll sw[N+5],wt[N+5];
void prep(){
for(int i=2,j=1,r=1;i<=n;i++){
int &k=zs[i]=r>i?zs[i-j+1]:0;
while(i+k<=n&&s[i+k]==s[k+1]) k++;
if(i+k>r){
j=i; r=i+k;
}
}
for(int i=1;i<=n;i++){
pre[i]=i;
if(pre[i-1]+zs[pre[i-1]]>i+zs[i])
pre[i]=pre[i-1];
}
sw[1]=w[1];
for(int i=2;i<=n;i++){
int l=1,r=i-1,mid,p;
sw[i]=w[i]-w[i-1];
while(l<=r){
mid=l+r>>1; p=pre[mid];
if(p+zs[p]>i) r=mid-1;
else l=mid+1;
}
p=r+1;
if(p==i) sw[i]+=w[zs[i]>0];
else sw[i]+=sw[i-p+1];
}
for(int i=1;i<=n;i++) sw[i]+=sw[i-1];
for(int i=1,j=0,r=0;i<=n;i++){
int &k=zt[i]=r>i?zs[i-j+1]:0;
while(i+k<=n&&t[i+k]==s[k+1]) k++;
if(i+k>r){
j=i; r=i+k;
}
wt[i]=w[k];
}
for(int i=1;i<=n;i++) wt[i]+=wt[i-1];
}
namespace Segtr{
int tr[N*4+5];
#define lc id<<1
#define rc id<<1|1
void build(int l,int r,int id){
if(l==r){
tr[id]=l+zt[l]-1;
return;
}
int mid=l+r>>1;
build(l,mid,lc);
build(mid+1,r,rc);
tr[id]=max(tr[lc],tr[rc]);
}
int query(int l,int r,int st,int en,int id){
if(tr[id]<=en) return -1;
if(l==r) return l;
int mid=l+r>>1,ans=-1;
if(st<=mid) ans=query(l,mid,st,en,lc);
if(ans==-1&&en>mid) ans=query(mid+1,r,st,en,rc);
return ans;
}
}
using namespace Segtr;
int main(){
scanf("%d %d",&n,&m);
scanf("%s",s+1);
scanf("%s",t+1);
for(int i=1;i<=n;i++){
scanf("%lld",&w[i]);
w[i]+=w[i-1];
}
prep();
build(1,n,1);
while(m--){
int l,r;
scanf("%d %d",&l,&r);
int p=query(1,n,l,r,1);
if(p==-1) printf("%lld\n",wt[r]-wt[l-1]);
else printf("%lld\n",wt[p-1]-wt[l-1]+sw[r-p+1]);
}
return 0;
}
详细
Test #1:
score: 100
Accepted
time: 1ms
memory: 8028kb
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: 8020kb
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: 60ms
memory: 11760kb
input:
150000 150000 aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaabaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaabaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa...
output:
195243623801113 390273297489636 822153183268386 790230554549194 267778272855234 153462446742405 636833448319079 106042472452040 121495016131289 478880596605819 347888553162661 536009905645588 429732935440230 53451608409672 739335335243285 487162958982541 58430581978635 193646641094541 87354260164226...
result:
wrong answer 1st lines differ - expected: '108147037823514', found: '195243623801113'