QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#263403 | #7748. Karshilov's Matching Problem II | ship2077 | WA | 43ms | 18260kb | C++14 | 1.8kb | 2023-11-24 20:12:43 | 2023-11-24 20:12:43 |
Judging History
answer
#include<bits/stdc++.h>
#define ls(x) (x<<1)
#define rs(x) (x<<1|1)
using namespace std;
typedef unsigned long long ull;
constexpr int M=1.5e5+5;char s[M],t[M];
ull hs1[M],hs2[M],ht1[M],ht2[M],pw1[M],pw2[M],w[M],res[M],sum[M];
int n,m,l,r,ans,pos,f[M],nxt[M],tr[M<<2];
int read(){
int x=0,f=1;char ch=getchar();
while (!isdigit(ch)) {if (ch=='-') f=-1;ch=getchar();}
while (isdigit(ch)) x=x*10+ch-48,ch=getchar();
return x*f;
}
void build(int l,int r,int x){
if (l==r) return tr[x]=f[l],void();
int mid=l+r>>1;
build(l,mid,ls(x));build(mid+1,r,rs(x));
tr[x]=max(tr[ls(x)],tr[rs(x)]);
}
int query(int L,int R,int l=1,int r=n,int x=1){
if (tr[x]<=R) return 0;
if (l==r) return l; int mid=l+r>>1,ans;
if (L<=mid&&(ans=query(L,R,l,mid,ls(x)))) return ans;
if (R>mid&&(ans=query(L,R,mid+1,r,rs(x)))) return ans;
return 0;
}
int main(){
n=read();m=read();
scanf("%s%s",s+1,t+1);
for (int i=1;i<=n;i++) w[i]=read();
for (int i=pw1[0]=pw2[0]=1;i<=n;i++) pw1[i]=pw1[i-1]*233,pw2[i]=pw2[i-1]*2333;
for (int i=1;i<=n;i++) hs1[i]=hs1[i-1]*233+s[i],hs2[i]=hs2[i-1]*2333+s[i];
for (int i=1;i<=n;i++) ht1[i]=ht1[i-1]*233+t[i],ht2[i]=ht2[i-1]*2333+t[i];
for (int i=1;i<=n;i++){
int l=i,r=n;f[i]=i-1;
while (l<=r){
int mid=l+r>>1;
if (hs1[mid-i+1]==ht1[mid]-ht1[i-1]*pw1[mid-i+1]
&&hs2[mid-i+1]==ht2[mid]-ht2[i-1]*pw2[mid-i+1])
f[i]=mid,l=mid+1;
else r=mid-1;
}
} res[1]=w[1];
for (int i=2,j=0;i<=n;i++){
while (j&&s[j+1]!=s[i])
j=nxt[j];
nxt[i]=j+=s[j+1]==s[i];
res[i]=res[nxt[i]]+w[i];
}
for (int i=1;i<=n;i++) w[i]+=w[i-1],res[i]+=res[i-1];
for (int i=1;i<=n;i++) sum[i]=sum[i-1]+w[f[i]-i+1];
build(1,n,1); while (m--){
l=read();r=read();
pos=query(l,r);if (!pos) pos=r+1;
printf("%llu\n",sum[pos-1]-sum[l-1]+res[r-pos+1]);
}
return 0;
}
详细
Test #1:
score: 100
Accepted
time: 0ms
memory: 12040kb
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: 12164kb
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: 0
Accepted
time: 43ms
memory: 18252kb
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...
result:
ok 150000 lines
Test #4:
score: 0
Accepted
time: 35ms
memory: 18252kb
input:
150000 150000 bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb...
output:
528900815691571 112638556022185 514229554849356 2216206133639915 295388515578381 1658587138269057 652142121207636 166322478628783 490195029871161 1191292846892788 1468501126902703 487990867773908 55994169916421 568966315599642 2522992078581539 2339888502167342 2881203249819745 154700081279584 152537...
result:
ok 150000 lines
Test #5:
score: 0
Accepted
time: 39ms
memory: 18256kb
input:
150000 150000 aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa...
output:
386150762496368 2769669390423140 1025785181073675 1707930121656247 332135612349048 113937878332307 1128519694119799 476402941643931 980441978140407 1004994648999517 676169371268202 2607965889355671 273766796671958 711480908011402 71754482763611 400453994282744 975387094872830 810536618300388 2229061...
result:
ok 150000 lines
Test #6:
score: 0
Accepted
time: 32ms
memory: 18196kb
input:
150000 150000 bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb...
output:
25254725752128652 23497896664383313 15195391464047263 41143572895791434 7513384536045842 8871310939247699 17423823866879881 14601201534396958 6203483865940624 24953281161800570 24776576029495768 1687640411226 31563282955464371 29947970968962218 1149303801639767 5806503923049299 11201332188941891 116...
result:
ok 150000 lines
Test #7:
score: 0
Accepted
time: 41ms
memory: 18260kb
input:
150000 150000 aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa...
output:
4570597927951323 11761063519994765 289982253793476 15684854914181269 19579321543184889 459972639580770 15246459216963247 1833393949769247 22425556248999709 11209560100586843 2883954996867615 14371655418173335 29207399108721 5943079608253242 1664456073054861 27405606916506455 23082758946788297 381175...
result:
ok 150000 lines
Test #8:
score: 0
Accepted
time: 37ms
memory: 18260kb
input:
150000 150000 aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa...
output:
5697498028074951 21822720964918437 11237472002329727 84315407720465773 32198634509153899 40728716039967676 5913555967331675 10487781019914529 270012821917938205 239347797488036653 216168445985667902 67452321725546144 457298584810665039 187789940805492303 46241700003064393 242312618101113 42260337439...
result:
ok 150000 lines
Test #9:
score: -100
Wrong Answer
time: 36ms
memory: 18248kb
input:
150000 150000 abbabaabbaababbabaababbaabbabaabbaababbaabbabaababbabaabbaababbabaababbaabbabaababbabaabbaababbaabbabaabbaababbabaababbaabbabaabbaababbaabbabaababbabaabbaababbaabbabaabbaababbabaababbaabbabaababbabaabbaababbabaababbaabbabaabbaababbaabbabaababbabaabbaababbabaababbaabbabaababbabaabbaabab...
output:
24754373743774 12938936978611 8992557949260 31808665411839 60937011490273 14464199942876 11907416223800 4769980618064 26691045658388 47016877945145 8396483338720 137560909513751 240199438286223 33209125255401 23036700359281 37066734871689 9433045407165 8938366531100 83457378643350 7567003686052 1358...
result:
wrong answer 1st lines differ - expected: '20591954951726', found: '24754373743774'