QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#262730 | #7748. Karshilov's Matching Problem II | Crying | WA | 116ms | 53588kb | C++17 | 2.0kb | 2023-11-23 22:18:35 | 2023-11-23 22:18:36 |
Judging History
answer
#include<bits/stdc++.h>
#define rep(i,a,b) for(int i=(a);i<=(b);i++)
#define per(i,a,b) for(int i=(a);i>=(b);i--)
#define op(x) ((x&1)?x+1:x-1)
#define odd(x) (x&1)
#define even(x) (!odd(x))
#define lc(x) (x<<1)
#define rc(x) (lc(x)|1)
#define lowbit(x) (x&-x)
#define mp(x,y) make_pair(x,y)
typedef long long ll;
typedef unsigned long long ull;
typedef double db;
using namespace std;
const int MAXN = 3e5+10,base = 13331;
typedef array<ll,2> pr;
int n,m;
char s[MAXN],t[MAXN];
ll w[MAXN],ans[MAXN],sum[MAXN],psum[MAXN];
vector<pr>q[MAXN];
vector<int>add[MAXN];
ull pw[MAXN],hs[MAXN],h[MAXN];
ull qh(int l,int r){
return h[r]-h[l-1]*pw[r-l+1];
}
struct BIT{
ll t[MAXN];
void mdf(int x,ll v){x=n-x+1;for(;x<=n;x+=lowbit(x))t[x]+=v;}
ll qry(int x,ll S=0){x=n-x+1;for(;x;x-=lowbit(x))S+=t[x];return S;}
}bit;
int nxt[MAXN][20],f[MAXN];
void kmp(){
int j=0;
psum[1]=sum[1];
rep(i,2,n){
while(j && s[j+1]!=s[i])j=nxt[j][0];
if(s[j+1]==s[i])j++;
nxt[i][0]=j;
psum[i]=sum[i]+psum[j];
}
rep(j,1,19)rep(i,1,n)nxt[i][j] = nxt[nxt[i][j-1]][j-1];
//
j=0;
rep(i,1,n){
while(j && (j==n || s[j+1]!=t[i]))j=nxt[j][0];
if(s[j+1]==t[i])j++;
f[i]=j;
}
}
int main(){
ios::sync_with_stdio(false);
cin>>n>>m>>(s+1)>>(t+1);
rep(i,1,n)cin>>w[i],sum[i]=sum[i-1]+w[i];
kmp();
rep(i,1,m){
int l,r;cin>>l>>r;
int p = f[r];
per(j,19,0)if(nxt[p][j] > r-l+1)p=nxt[p][j];
if(p>r-l+1)p=nxt[p][0];
ans[i]=psum[p];
q[r].push_back({l,i});
}
pw[0]=1;rep(i,1,n)pw[i]=pw[i-1]*base;
rep(i,1,n)hs[i]=hs[i-1]*base+(s[i]-'a'+1);
rep(i,1,n)h[i]=h[i-1]*base+(t[i]-'a'+1);
rep(i,1,n){
int L = 1,R = n-i+1,ret = 0;
while(L<=R){
int mid = (L+R)>>1;
if(qh(i,i+mid-1) == hs[mid])ret=mid,L=mid+1;
else R=mid-1;
}
add[i+ret].push_back(i);
}
rep(i,1,n){
for(auto j:add[i])bit.mdf(j,sum[i-j]);
for(auto [j,id]:q[i])ans[id] += bit.qry(j);
}
rep(i,1,m)cout<<ans[i]<<"\n";
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 0ms
memory: 30200kb
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: 0ms
memory: 28312kb
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: 61ms
memory: 53196kb
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: 68ms
memory: 50832kb
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: 81ms
memory: 52104kb
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: 85ms
memory: 50836kb
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: 83ms
memory: 53588kb
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: 116ms
memory: 49416kb
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: 80ms
memory: 52996kb
input:
150000 150000 abbabaabbaababbabaababbaabbabaabbaababbaabbabaababbabaabbaababbabaababbaabbabaababbabaabbaababbaabbabaabbaababbabaababbaabbabaabbaababbaabbabaababbabaabbaababbaabbabaabbaababbabaababbaabbabaababbabaabbaababbabaababbaabbabaabbaababbaabbabaababbabaabbaababbabaababbaabbabaababbabaabbaabab...
output:
72516386988479 20985951222862 10765352738029 114537840976221 65323196026856 22106226404457 22938306819215 5849032656930 78097997626282 128270373750097 10053289626669 295585170033084 403874060273189 70623416018435 77060856084673 139474450400814 12891437605830 16880297562556 211693904798770 7177289603...
result:
wrong answer 1st lines differ - expected: '20591954951726', found: '72516386988479'