QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#440150 | #5014. 复读程度 | Kevin5307 | 0 | 833ms | 154572kb | C++23 | 9.0kb | 2024-06-13 10:22:59 | 2024-06-13 10:22:59 |
Judging History
answer
//Author: Kevin
#include<bits/stdc++.h>
//#pragma GCC optimize("O2")
using namespace std;
#define ll long long
#define ull unsigned ll
#define pb emplace_back
#define mp make_pair
#define ALL(x) (x).begin(),(x).end()
#define rALL(x) (x).rbegin(),(x).rend()
#define srt(x) sort(ALL(x))
#define rev(x) reverse(ALL(x))
#define rsrt(x) sort(rALL(x))
#define sz(x) (int)(x.size())
#define inf 0x3f3f3f3f
#define pii pair<int,int>
#define lb(v,x) (int)(lower_bound(ALL(v),x)-v.begin())
#define ub(v,x) (int)(upper_bound(ALL(v),x)-v.begin())
#define uni(v) v.resize(unique(ALL(v))-v.begin())
#define longer __int128_t
void die(string S){puts(S.c_str());exit(0);}
class SuffixAutomaton
{
#define MAXN 200000+5
#define MAXLG 20
public:
SuffixAutomaton():last(1),tot(1){}
int trans[MAXN][26],fail[MAXN],len[MAXN],occ[MAXN],last,tot,dfn,in[MAXN],out[MAXN],id[MAXN],rep[MAXN];
int anc[MAXLG][MAXN];
ull weight[MAXN];
vector<int> G[MAXN];
int extend(int x,ull w=0,int pos=0)
{
int p=last;
int np=++tot;
len[np]=len[p]+1;
while(p&&!trans[p][x])
{
trans[p][x]=np;
p=fail[p];
}
if(!p)
fail[np]=1;
else
{
int q=trans[p][x];
if(len[p]+1==len[q])
fail[np]=q;
else
{
int nq=++tot;
memcpy(trans[nq],trans[q],sizeof(int)*26);
len[nq]=len[p]+1;
fail[nq]=fail[q];
fail[q]=fail[np]=nq;
while(p&&trans[p][x]==q)
{
trans[p][x]=nq;
p=fail[p];
}
}
}
last=np;
occ[last]++;
weight[last]+=w;
rep[last]=pos;
return last;
}
void dfs(int u)
{
in[u]=++dfn;
id[dfn]=u;
for(auto v:G[u])
{
dfs(v);
occ[u]+=occ[v];
weight[u]+=weight[v];
rep[u]=max(rep[u],rep[v]);
}
out[u]=dfn;
}
void init()
{
for(int i=1;i<=tot;i++)
if(fail[i])
G[fail[i]].pb(i);
dfs(1);
for(int i=1;i<=tot;i++)
anc[0][i]=fail[i];
for(int i=1;i<20;i++)
for(int j=1;j<=tot;j++)
anc[i][j]=anc[i-1][anc[i-1][j]];
}
int locate(int node,int length)
{
for(int i=MAXLG-1;i>=0;i--)
if(len[anc[i][node]]>=length)
node=anc[i][node];
return node;
}
#undef MAXN
#undef MAXLG
};
class BasicSubstringStructure
{
#define MAXN 100000+5
public:
BasicSubstringStructure(string _s):s(_s){build();}
BasicSubstringStructure(){}
string s;
SuffixAutomaton S1,S2;
int node1[MAXN],node2[MAXN],n;
ull wl[MAXN],wr[MAXN];
void build()
{
n=sz(s);
for(int i=1;i<=n;i++)
node1[i]=S1.extend(s[i-1]-'a',wr[i],i);
for(int i=n;i>=1;i--)
node2[i]=S2.extend(s[i-1]-'a',wl[i],i);
S1.init();
S2.init();
}
int locate1(pii str)
{
int nd1=S1.locate(node1[str.second],str.second-str.first+1);
return nd1;
}
int locate2(pii str)
{
int nd2=S2.locate(node2[str.first],str.second-str.first+1);
return nd2;
}
pii rep(pii str)
{
int nd1=S1.locate(node1[str.second],str.second-str.first+1);
str=mp(str.second-S1.len[nd1]+1,str.second);
int nd2=S2.locate(node2[str.first],str.second-str.first+1);
return str=mp(str.first,str.first+S2.len[nd2]-1);
}
int occ(pii str)
{
int nd1=S1.locate(node1[str.second],str.second-str.first+1);
return S1.occ[nd1];
}
ull weight(pii str)
{
int nd1=S1.locate(node1[str.second],str.second-str.first+1);
int nd2=S2.locate(node2[str.first],str.second-str.first+1);
return S1.weight[nd1]*S2.weight[nd2];
}
vector<int> order1()
{
int s1=S1.tot;
vector<pair<pii,pii>> vec;
for(int i=2;i<=s1;i++)
{
pii pos(S1.rep[i]-S1.len[i]+1,S1.rep[i]);
vec.pb(rep(pos),pii(S1.len[i]-S1.len[S1.fail[i]],i));
}
srt(vec);
vector<int> ret;
for(int i=0;i<sz(vec);i++)
ret.pb(vec[i].second.second);
return ret;
}
vector<int> order2()
{
int s2=S2.tot;
vector<pair<pii,pii>> vec;
for(int i=2;i<=s2;i++)
{
pii pos(S2.rep[i],S2.rep[i]+S2.len[i]-1);
vec.pb(rep(pos),pii(S2.len[i]-S2.len[S2.fail[i]],i));
}
srt(vec);
vector<int> ret;
for(int i=0;i<sz(vec);i++)
ret.pb(vec[i].second.second);
return ret;
}
#undef MAXN
}BSS;
int n,q;
string s;
ull wl[100100],wr[100100];
ull ans[400400];
int ql[400400],qr[400400];
int l1[100100],r1[100100],l2[100100],r2[100100],nd1[100100],nd2[100100];
const int B1=630,B2=450;
int qx[400400],qly[400400],qry[400400];
int qy[400400],qlx[400400],qrx[400400];
ull ans1[400400],ans2[400400];
vector<int> vqx[400400],vqy[400400];
int s1,s2;
int label1[200200],label2[200200];
ull tag[200200/B2+5],rval[200200];
ull W[200200];
ull res[100100];
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
cin>>n>>q>>s;
for(int i=1;i<=n;i++)
cin>>wl[i];
for(int i=1;i<=n;i++)
cin>>wr[i];
BSS.s=s;
for(int i=1;i<=n;i++)
BSS.wl[i]=wl[i];
for(int i=1;i<=n;i++)
BSS.wr[i]=wr[i];
BSS.build();
for(int i=1;i<=q;i++)
{
cin>>l2[i]>>r2[i]>>l1[i]>>r1[i];
nd1[i]=BSS.locate1({l1[i],r1[i]});
nd2[i]=BSS.locate2({l2[i],r2[i]});
// if(BSS.rep({l1[i],r1[i]})==BSS.rep({l2[i],r2[i]}))
// {
// pii rep=BSS.rep({l1[i],r1[i]});
// int len=BSS.S1.len[nd1[i]]+BSS.S2.len[nd2[i]]-(rep.second-rep.first+1);
// if(len<min(r1[i]-l1[i]+1,r2[i]-l2[i]+1))
// {
// int d1=BSS.S2.len[nd2[i]]-(rep.second-rep.first+1);
// int d2=BSS.S1.len[nd1[i]]-(rep.second-rep.first+1);
// rep.first-=d1;
// rep.second+=d2;
// res[i]-=BSS.occ(rep)*BSS.S2.weight[BSS.locate2(rep)]*BSS.S1.weight[BSS.locate1(rep)];
// }
// }
// for(int l=1;l<=n;l++)
// for(int r=l;r<=n;r++)
// if(r-l+1<max(r1[i]-l1[i]+1,r2[i]-l2[i]+1))
// if(BSS.locate1({l,r})==nd1[i]||BSS.locate2({l,r})==nd2[i])
// res[i]-=BSS.S1.weight[BSS.locate1({l,r})]*BSS.S2.weight[BSS.locate2({l,r})];
ql[i*4-3]=BSS.S1.in[nd1[i]]-1;
qr[i*4-3]=BSS.S2.in[nd2[i]]-1;
ql[i*4-2]=BSS.S1.out[nd1[i]];
qr[i*4-2]=BSS.S2.in[nd2[i]]-1;
ql[i*4-1]=BSS.S1.in[nd1[i]]-1;
qr[i*4-1]=BSS.S2.out[nd2[i]];
ql[i*4-0]=BSS.S1.out[nd1[i]];
qr[i*4-0]=BSS.S2.out[nd2[i]];
}
vector<int> vq;
for(int i=1;i<=q*4;i++)
vq.pb(i);
sort(ALL(vq),[&](int u,int v)
{
if(ql[u]/B1==ql[v]/B1) return qr[u]<qr[v];
return ql[u]<ql[v];
});
int cl=1,cr=1;
for(int i=1;i<=q*4;i++)
{
int ind=vq[i-1];
int l=ql[ind],r=qr[ind];
if(cl<=l)
{
qy[i]=cr;
qlx[i]=cl+1;
qrx[i]=l;
cl=l;
}
else
{
qy[i]=cr;
qlx[i]=l+1;
qrx[i]=cl;
cl=l;
}
if(cr<=r)
{
qx[i]=cl;
qly[i]=cr+1;
qry[i]=r;
cr=r;
}
else
{
qx[i]=cl;
qly[i]=r+1;
qry[i]=cr;
cr=r;
}
}
for(int i=1;i<=q*4;i++)
{
vqx[qx[i]].pb(i);
vqy[qy[i]].pb(i);
}
s1=BSS.S1.tot;
s2=BSS.S2.tot;
vector<int> order1=BSS.order1(),order2=BSS.order2();
for(int i=1;i<=sz(order1);i++)
label1[order1[i-1]]=i;
for(int i=1;i<=sz(order2);i++)
label2[order2[i-1]]=i;
for(int i=1;i<=s2;i++)
W[label2[i]]=BSS.S2.weight[i];
for(int i=2;i<=s1;i++)
{
int node=BSS.S1.id[i];
int pos=BSS.S1.rep[node];
int ndl=BSS.locate2(pii(pos-BSS.S1.len[BSS.S1.fail[node]],pos));
int ndr=BSS.locate2(pii(pos-BSS.S1.len[node]+1,pos));
int cl=label2[ndl],cr=label2[ndr];
int lb=cl/B2,rb=cr/B2;
ull val=BSS.S1.weight[node]*BSS.S1.occ[node];
for(int i=lb+1;i<rb;i++)
tag[i]+=val;
if(lb<rb)
{
for(int i=cl;i/B2==lb;i++)
rval[i]+=val*W[i];
for(int i=cr;i/B2==rb;i--)
rval[i]+=val*W[i];
}
else
{
for(int i=cl;i<=cr;i++)
rval[i]+=val*W[i];
}
for(auto qind:vqx[i])
for(int ydfn=qly[qind];ydfn<=qry[qind];ydfn++)
{
int yind=label2[BSS.S2.id[ydfn]];
ans1[qind]+=tag[yind/B2]*W[yind]+rval[yind];
}
}
memset(tag,0,sizeof(tag));
memset(rval,0,sizeof(rval));
for(int i=1;i<=s1;i++)
W[label1[i]]=BSS.S1.weight[i];
for(int i=2;i<=s2;i++)
{
int node=BSS.S2.id[i];
int pos=BSS.S2.rep[node];
int ndl=BSS.locate1(pii(pos,pos+BSS.S2.len[BSS.S2.fail[node]]));
int ndr=BSS.locate1(pii(pos,pos+BSS.S2.len[node]-1));
int cl=label1[ndl],cr=label1[ndr];
int lb=cl/B2,rb=cr/B2;
ull val=BSS.S2.weight[node]*BSS.S2.occ[node];
for(int i=lb+1;i<rb;i++)
tag[i]+=val;
if(lb<rb)
{
for(int i=cl;i/B2==lb;i++)
rval[i]+=val*W[i];
for(int i=cr;i/B2==rb;i--)
rval[i]+=val*W[i];
}
else
{
for(int i=cl;i<=cr;i++)
rval[i]+=val*W[i];
}
for(auto qind:vqy[i])
for(int xdfn=qlx[qind];xdfn<=qrx[qind];xdfn++)
{
int xind=label1[BSS.S1.id[xdfn]];
ans2[qind]+=tag[xind/B2]*W[xind]+rval[xind];
}
}
cl=1;
cr=1;
ull tot=0;
for(int i=1;i<=q*4;i++)
{
int ind=vq[i-1];
int l=ql[ind],r=qr[ind];
if(cl<=l)
{
tot+=ans2[i];
cl=l;
}
else
{
tot-=ans2[i];
cl=l;
}
if(cr<=r)
{
tot+=ans1[i];
cr=r;
}
else
{
tot-=ans1[i];
cr=r;
}
ans[ind]=tot;
}
for(int i=1;i<=q;i++)
res[i]=ans[i*4-0]-ans[i*4-1]-ans[i*4-2]+ans[i*4-3];
for(int i=1;i<=q;i++)
cout<<res[i]<<endl;
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Subtask #1:
score: 0
Wrong Answer
Test #1:
score: 7
Accepted
time: 0ms
memory: 81888kb
input:
500 500 aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaabaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa...
output:
15720454042420499810 4058077030882532408 14651762045124606089 4030024243931986061 18033423360813892607 9470601111824364484 3883374861354698625 16650831689368240202 8339028189650687576 2683289915379600554 13133811958066776394 14181220923901262251 18173739360450512256 13142314545999179754 148925491596...
result:
ok 500 lines
Test #2:
score: -7
Wrong Answer
time: 7ms
memory: 81612kb
input:
500 500 zszzszszzszzszszzszszzszzszszzszzszszzszszzszzszszzszszzszzszszzszzszszzszszzszzszszzszzszszzszszzszzszszzszszzszzszszzszzszszzszszzszzszszzszszzszzszszzszzszszzszszzszzszszzszzszszzszszzszzszszzszszzszzszszzszzszszzszszzszzszszzszzszszzszszzszzszszzszszzszzszszzszzszszzszszzszzszszzszszzszz...
output:
8815599551142901213 5794092533046618523 533576317536031175 11457640752791718820 16659658375694832611 1624229621587826908 3247342125341043527 13480353147607742552 13045121434804725850 12591311806106291588 13316626648976199221 6181092693273210423 16473024381715762830 7819865095072165606 21404033324785...
result:
wrong answer 1st lines differ - expected: '4843650749197240262', found: '8815599551142901213'
Subtask #2:
score: 0
Skipped
Dependency #1:
0%
Subtask #3:
score: 0
Skipped
Dependency #2:
0%
Subtask #4:
score: 0
Wrong Answer
Test #22:
score: 0
Wrong Answer
time: 833ms
memory: 154572kb
input:
100000 100000 zbbabaabbaababbabaababbaabbabaabbaababbaabbabaababbabaabbaababbabaababbaabbabaababbabaabbaababbaabbabaabbaababbabaababbaabbabaabbaababbaabbabaababbabaabbaababbaabbabaabbaababbabaababbaabbabaababbabaabbaababbabaababbaabbabaabbaababbaabbabaababbabaabbaababbabaababbaabbabaababbabaabbaabab...
output:
9450651655897401128 12598923883154115999 2796331184513097647 17668679206267169316 3802787147633182137 9018393278622604928 722080763028949423 9450651655897401128 2507984964448910864 9450651655897401128 6341369458729155548 17411063929097314356 15452361437877974975 432088324301719512 138985408195215693...
result:
wrong answer 1st lines differ - expected: '16102224067619618967', found: '9450651655897401128'
Subtask #5:
score: 0
Skipped
Dependency #4:
0%
Subtask #6:
score: 0
Skipped
Dependency #2:
0%
Subtask #7:
score: 0
Skipped
Dependency #3:
0%