QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#431940 | #5014. 复读程度 | by_chance | 0 | 353ms | 171704kb | C++14 | 7.5kb | 2024-06-06 13:22:03 | 2024-06-06 13:22:04 |
Judging History
answer
#include<bits/stdc++.h>
using namespace std;
const int N=2e5+5;
typedef unsigned long long ull;
int n,q,B;char s[N];ull wl[N],wr[N],ans[N];
struct SAM{
int type,trans[N][26],len[N],slink[N],pos[N],tot,lst;vector<int> G[N];
int dfn[N],dcnt,ed[N],rev[N],fa[20][N],mnp[N],bel[N];ull val[N],occ[N];
void insert(int c){
int cur=++tot,p=lst;lst=cur;len[cur]=len[p]+1;
while(p&&!trans[p][c])trans[p][c]=cur,p=slink[p];
if(!p){slink[cur]=1;return;}
int q=trans[p][c];if(len[q]==len[p]+1){slink[cur]=q;return;}
int r=++tot;for(int i=0;i<26;i++)trans[r][i]=trans[q][i];
len[r]=len[p]+1;slink[r]=slink[q];slink[q]=slink[cur]=r;
while(p&&trans[p][c]==q)trans[p][c]=r,p=slink[p];
}
void dfs(int u){
rev[dfn[u]=++dcnt]=u;
for(int j=1;(1<<j)<=tot;j++)fa[j][u]=fa[j-1][fa[j-1][u]];
for(int v:G[u]){
dfs(v);val[u]+=val[v];occ[u]+=occ[v];
mnp[u]=min(mnp[u],mnp[v]);
}ed[u]=dcnt;
}
void build(int op){
tot=lst=1;type=op;memset(mnp,0x3f,sizeof(mnp));
if(op==1)for(int i=1;i<=n;i++)
insert(s[i]-'a'),val[lst]=wl[i],pos[i]=lst,mnp[lst]=i,occ[lst]=1;
else for(int i=n;i>=1;i--)
insert(s[i]-'a'),val[lst]=wr[i],pos[i]=lst,mnp[lst]=i,occ[lst]=1;
for(int i=2;i<=tot;i++)G[fa[0][i]=slink[i]].push_back(i);dfs(1);
}
void label(){
for(int i=tot;i>=1;i--)if(!bel[i])
for(int j=0;j<26;j++)if(bel[trans[i][j]]){
bel[i]=bel[trans[i][j]];break;
}
}
int ask(int l,int r){
int u=(type==1?pos[r]:pos[l]);
for(int j=19;j>=0;j--)
if(fa[j][u]&&len[fa[j][u]]>=r-l+1)u=fa[j][u];
return u;
}
}Pre,Suf;
int num;vector<int> row[N],col[N];
void build(){
Pre.build(1);Suf.build(-1);
for(int u=1;u<=Pre.tot;u++){
int r=Pre.mnp[u],l=r-Pre.len[u]+1;
int v=Suf.ask(l,r);
if(Suf.len[v]==Pre.len[u])
Pre.bel[u]=Suf.bel[v]=++num;
}
Pre.label();Suf.label();
for(int u=Pre.tot;u>=1;u--)row[Pre.bel[u]].push_back(u);
for(int v=Suf.tot;v>=1;v--)col[Suf.bel[v]].push_back(v);
}
namespace BIT{
ull c[N];
void add(int x,ull v){for(;x<N;x+=x&-x)c[x]+=v;}
ull ask(int x){ull s=0;for(;x;x-=x&-x)s+=c[x];return s;}
};
struct query1{int x1,x2,y,id;ull mul;};
bool operator <(const query1&a,const query1&b){return a.y<b.y;}
vector<query1> qr1[N],qc1[N];
struct query2{int x,y,id,op;};vector<query2> mo;
bool operator <(const query2&a,const query2&b){
if(a.x/B!=b.x/B)return a.x<b.x;
else return (a.x/B)&1?a.y>b.y:a.y<b.y;
}
namespace Block{
ull va[N],addb[N],add[N];
void modify(int l,int r,ull v){
int lid=(l-1)/B+1,rid=(r-1)/B+1;
if(lid==rid)for(int i=l;i<=r;i++)add[i]+=v;
else{
for(int i=l;i<=lid*B;i++)add[i]+=v;
for(int i=(rid-1)*B+1;i<=r;i++)add[i]+=v;
for(int i=lid+1;i<=rid-1;i++)addb[i]+=v;
}
}
ull ask(int x){return (addb[(x-1)/B+1]+add[x])*va[x];}
}
struct query3{int x1,x2;ull res;};
bool operator <(const query3&a,const query3&b){
return a.x1==b.x1?a.x2<b.x2:a.x1<b.x1;
}
bool operator ==(const query3&a,const query3&b){
return a.x1==b.x1&&a.x2==b.x2;
}
vector<query3> qr[N],qc[N];int reb[N],st[N],ttt;
int main(){
ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
cin>>n>>q>>s+1;B=max(100.0,sqrt(n));
for(int i=1;i<=n;i++)cin>>wl[i];
for(int i=1;i<=n;i++)cin>>wr[i];
build();
for(int i=1,l1,r1,l2,r2;i<=q;i++){
cin>>l1>>r1>>l2>>r2;
int p1=Suf.ask(l1,r1),p2=Pre.ask(l2,r2);
int a=Suf.mnp[p1],b=Pre.mnp[p2];
int mn1=Suf.len[Suf.slink[p1]],mn2=Pre.len[Pre.slink[p2]];
if(b-a>=max(mn1,mn2)&&b-a<min(r1-l1,r2-l2))
ans[i]-=Pre.occ[p2]*Pre.val[p2]*Suf.val[p1];
else{
qr1[Pre.bel[p2]].push_back(
{Suf.dfn[p1],Suf.ed[p1],r2-l2-mn2,i,Pre.val[p2]*Pre.occ[p2]});
qc1[Suf.bel[p1]].push_back(
{Pre.dfn[p2],Pre.ed[p2],r1-l1-mn1,i,Suf.val[p1]*Suf.occ[p1]});
}
mo.push_back({Pre.ed[p2],Suf.ed[p1],i,1});
mo.push_back({Pre.dfn[p2]-1,Suf.ed[p1],i,-1});
mo.push_back({Pre.ed[p2],Suf.dfn[p1]-1,i,-1});
mo.push_back({Pre.dfn[p2]-1,Suf.dfn[p1]-1,i,1});
}
for(int i=1;i<=num;i++){
sort(qr1[i].begin(),qr1[i].end());
sort(qc1[i].begin(),qc1[i].end());
int x=0;while(x<qr1[i].size()&&qr1[i][x].y==0)++x;
for(int j=1;j<=col[i].size();j++){
int p=col[i][col[i].size()-j];BIT::add(Suf.dfn[p],Suf.val[p]);
while(x<qr1[i].size()&&qr1[i][x].y==j){
auto u=qr1[i][x];++x;
ans[u.id]-=(BIT::ask(u.x2)-BIT::ask(u.x1-1))*u.mul;
}
}
for(int p:col[i])BIT::add(Suf.dfn[p],-Suf.val[p]);
x=0;while(x<qc1[i].size()&&qc1[i][x].y==0)++x;
for(int j=1;j<=row[i].size();j++){
int p=row[i][row[i].size()-j];BIT::add(Pre.dfn[p],Pre.val[p]);
while(x<qc1[i].size()&&qc1[i][x].y==j){
auto u=qc1[i][x];++x;
ans[u.id]-=(BIT::ask(u.x2)-BIT::ask(u.x1-1))*u.mul;
}
}
for(int p:row[i])BIT::add(Pre.dfn[p],-Pre.val[p]);
}
sort(mo.begin(),mo.end());
for(int i=0,px=1,py=0;i<mo.size();i++){
int qx=mo[i].x,qy=mo[i].y;
if(py<qy){qc[px].push_back({py+1,qy,0});py=qy;}
if(px<qx){qr[py].push_back({px+1,qx,0});px=qx;}
if(py>qy){qc[px].push_back({qy+1,py,0});py=qy;}
if(px>qx){qr[py].push_back({qx+1,px,0});px=qx;}
}
for(int i=1;i<=num;i++){
st[i]=ttt;for(int x:row[i])reb[x]=++ttt,Block::va[ttt]=Pre.val[x];
}
for(int i=1;i<=Suf.tot;i++){
int u=Suf.rev[i],id=Suf.bel[u],lth=Suf.len[u]-Suf.len[Suf.slink[u]];
Block::modify(st[id]+1,st[id]+lth,Suf.occ[u]*Suf.val[u]);
sort(qr[i].begin(),qr[i].end());
qr[i].resize(unique(qr[i].begin(),qr[i].end())-qr[i].begin());
for(auto&x:qr[i])for(int j=x.x1;j<=x.x2;j++)
x.res+=Block::ask(reb[Pre.rev[j]]);
}
for(int i=1;i<=ttt;i++)Block::add[i]=Block::addb[i]=0;ttt=0;
for(int i=1;i<=num;i++){
st[i]=ttt;for(int x:col[i])reb[x]=++ttt,Block::va[ttt]=Suf.val[x];
}
for(int i=1;i<=Pre.tot;i++){
int u=Pre.rev[i],id=Pre.bel[u],lth=Pre.len[u]-Pre.len[Pre.slink[u]];
Block::modify(st[id]+1,st[id]+lth,Pre.occ[u]*Pre.val[u]);
sort(qc[i].begin(),qc[i].end());
qc[i].resize(unique(qc[i].begin(),qc[i].end())-qc[i].begin());
for(auto&x:qc[i])for(int j=x.x1;j<=x.x2;j++)
x.res+=Block::ask(reb[Suf.rev[j]]);
}
ull now=0;
for(int i=0,px=1,py=0;i<mo.size();i++){
int qx=mo[i].x,qy=mo[i].y,pos;
if(py<qy)pos=lower_bound(qc[px].begin(),qc[px].end(),
query3{py+1,qy,0})-qc[px].begin(),now+=qc[px][pos].res,py=qy;
if(px<qx)pos=lower_bound(qr[py].begin(),qr[py].end(),
query3{px+1,qx,0})-qr[py].begin(),now+=qr[py][pos].res,px=qx;
if(py>qy)pos=lower_bound(qc[px].begin(),qc[px].end(),
query3{qy+1,py,0})-qc[px].begin(),now-=qc[px][pos].res,py=qy;
if(px>qx)pos=lower_bound(qr[py].begin(),qr[py].end(),
query3{qx+1,px,0})-qr[py].begin(),now-=qr[py][pos].res,px=qx;
if(mo[i].op==1)ans[mo[i].id]+=now;else ans[mo[i].id]-=now;
}
for(int i=1;i<=q;i++)cout<<ans[i]<<"\n";
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Subtask #1:
score: 0
Wrong Answer
Test #1:
score: 0
Wrong Answer
time: 10ms
memory: 83708kb
input:
500 500 aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaabaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa...
output:
9061518669157686084 6273085781564968152 11278484733539139933 18344354558641337557 6671233220381679830 16273119798864784791 11072948089397419980 13880163360694731395 3352543141461643408 13110705205913603325 5410949758983517847 8084764466130451579 6301489082543712751 2557958894373571317 78041983782646...
result:
wrong answer 1st lines differ - expected: '15720454042420499810', found: '9061518669157686084'
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: 353ms
memory: 171704kb
input:
100000 100000 zbbabaabbaababbabaababbaabbabaabbaababbaabbabaababbabaabbaababbabaababbaabbabaababbabaabbaababbaabbabaabbaababbabaababbaabbabaabbaababbaabbabaababbabaabbaababbaabbabaabbaababbabaababbaabbabaababbabaabbaababbabaababbaabbabaabbaababbaabbabaababbabaabbaababbabaababbaabbabaababbabaabbaabab...
output:
15270446592129826344 17051467773520578076 13485021368353103424 1056107055221973452 17313474448561106668 989536887738752308 4929020353502729484 2945661676214015796 12190553199974446564 13592258772351410164 2572798031167260432 1879623436350770992 12222368072504080456 8830489485499142616 17354859396914...
result:
wrong answer 1st lines differ - expected: '16102224067619618967', found: '15270446592129826344'
Subtask #5:
score: 0
Skipped
Dependency #4:
0%
Subtask #6:
score: 0
Skipped
Dependency #2:
0%
Subtask #7:
score: 0
Skipped
Dependency #3:
0%