QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#440153 | #5014. 复读程度 | C1942huangjiaxu | 0 | 62ms | 53112kb | C++14 | 4.2kb | 2024-06-13 10:31:06 | 2024-06-13 10:31:07 |
Judging History
answer
#include<bits/stdc++.h>
using namespace std;
const int N=2e5+5;
typedef unsigned long long ull;
int n,q,tot;
ull wl[N],wr[N];
char s[N];
struct node{
int ch[26],len,fa;
};
struct SAM{
node tr[N];
int str[N],son[N][26],occ[N],pos[N],bel[N],ip[N],sz[N];
ull sum[N];
int cnt=1,ls=1;
int fa[N][19],in[N],out[N];
vector<int>e[N];
void ins(int c){
int p=ls,np=ls=++cnt;
tr[np].len=tr[p].len+1;
str[tr[np].len]=c,ip[tr[np].len]=np;
for(;p&&!tr[p].ch[c];p=tr[p].fa)tr[p].ch[c]=np;
if(!p)tr[np].fa=1;
else{
int q=tr[p].ch[c];
if(tr[q].len==tr[p].len+1)tr[np].fa=q;
else{
int nq=++cnt;tr[nq]=tr[q];
tr[nq].len=tr[p].len+1,tr[np].fa=tr[q].fa=nq;
for(;p&&tr[p].ch[c]==q;p=tr[p].fa)tr[p].ch[c]=nq;
}
}
}
void dfs(int x){
in[x]=++in[0];
for(int i=1;i<19;++i)fa[x][i]=fa[fa[x][i-1]][i-1];
for(auto v:e[x]){
fa[v][0]=x;
sz[v]=tr[v].len-tr[x].len;
dfs(v);
sum[x]+=sum[v];
occ[x]+=occ[v];
pos[x]=min(pos[x],pos[v]);
}
out[x]=in[0];
}
void build(ull *w){
for(int i=1;i<=cnt;++i)e[tr[i].fa].emplace_back(i),pos[i]=1e9;
int u=1;
for(int i=1;i<=n;++i){
u=tr[u].ch[str[i]];
++occ[u];
pos[u]=i,sum[u]=w[i];
}
dfs(1);
for(int i=2;i<=cnt;++i)son[tr[i].fa][str[pos[i]-tr[tr[i].fa].len]]=i;
}
void feed(vector<int>* res){
for(int u=cnt;u;--u){
if(!bel[u])for(int i=0;i<26;++i)if(tr[u].ch[i])bel[u]=bel[tr[u].ch[i]];
res[bel[u]].push_back(u);
}
}
int find(int l,int r){
int x=ip[r];
for(int i=18;~i;--i)if(fa[x][i]&&tr[fa[x][i]].len>=r-l+1)x=fa[x][i];
return x;
}
void print(){
printf("cnt %d\n",cnt);
for(int i=2;i<=cnt;++i){
printf("id %d\n",i);
printf("ln %d fa %d\n",tr[i].len,tr[i].fa);
printf("occ %d pos %d sum %llu\n",occ[i],pos[i],sum[i]);
}
}
}s1,s2;
vector<int>rid[N],cid[N];
int rp[N],cp[N],occ[N];
void dfs(int x,int y){
bool fg=s1.tr[x].len==s2.tr[y].len;
if(fg)s1.bel[x]=s2.bel[y]=++tot,occ[tot]=s1.occ[x];
for(int i=0;i<26;++i)if(s1.tr[s1.tr[x].ch[i]].len==s1.tr[x].len+1)
dfs(s1.tr[x].ch[i],fg?s2.son[y][i]:y);
}
ull calc(int X,int Y){
ull res=0;
for(int i=2;i<=tot;++i){
for(int j=0;j<rid[i].size();++j){
int v=rid[i][j];
if(s1.in[v]>X)continue;
for(int k=0;k<s1.sz[v];++k){
int u=cid[i][k];
if(s2.in[u]>Y)continue;
res+=1ll*s1.sum[v]*s2.sum[u]*occ[i];
}
}
}
return res;
}
ull qry(int l1,int r1,int l2,int r2){
int p1=s1.find(l1,r1),p2=s2.find(n-r2+1,n-l2+1);
int ln1=r1-l1+1,ln2=r2-l2+1;
int y1=rp[p1],x1=s1.tr[p1].len-ln1,x2=cp[p2],y2=s2.tr[p2].len-ln2;
//printf("?%d %d %d %d %d %d %d %d\n",s1.bel[p1],s2.bel[p2],p1,p2,x1,y1,x2,y2);
ull res=0;
if(s1.bel[p1]==s2.bel[p2]){
if(x1!=x2||y1!=y2){
if(x2>=x1&&x2<s1.sz[p1]&&y1>=y2&&y1<s2.sz[p2])res-=s1.sum[p1]*s2.sum[p2]*s1.occ[p1];
}
}else{
int o=s1.bel[p1];
for(int j=x1+1;j<s1.sz[p1];++j){
int u=cid[o][j];
if(s2.in[u]>=s2.in[p2]&&s2.in[u]<=s2.out[p2]){
res-=s1.sum[p1]*s2.sum[u]*occ[o];
}
}
o=s2.bel[p2];
for(int j=y2+1;j<s2.sz[p2];++j){
int u=rid[o][j];
if(s1.in[u]>=s1.in[p1]&&s1.in[u]<=s1.out[p1]){
res-=s1.sum[u]*s2.sum[p2]*occ[o];
}
}
}
res+=calc(s1.out[p1],s2.out[p2]);
res-=calc(s1.in[p1]-1,s2.out[p2]);
res-=calc(s1.out[p1],s2.in[p2]-1);
res+=calc(s1.in[p1]-1,s2.in[p2]-1);
return res;
}
void Print(){
printf("tot %d\n",tot);
for(int i=1;i<=tot;++i){
printf("bel %d occ %d\n",i,occ[i]);
printf("rid\n");
for(auto v:rid[i])printf("%d %d\n",v,s1.sz[v]);
printf("cid\n");
for(auto v:cid[i])printf("%d %d\n",v,s2.sz[v]);putchar(10);
}
}
int main(){
scanf("%d%d",&n,&q);
scanf("%s",s+1);
for(int i=1;i<=n;++i)scanf("%llu",&wl[i]);
for(int i=1;i<=n;++i)scanf("%llu",&wr[i]);
for(int i=1;i<=n;++i)s[i]-='a';
for(int i=1;i<=n;++i)s1.ins(s[i]);
for(int i=n;i;--i)s2.ins(s[i]);
s1.build(wr);
reverse(wl+1,wl+n+1);
s2.build(wl);
dfs(1,1);
s1.feed(rid),s2.feed(cid);
//s1.print(),s2.print();
//Print();
for(int i=1;i<=tot;++i){
for(int j=0;j<rid[i].size();++j)rp[rid[i][j]]=j;
for(int j=0;j<cid[i].size();++j)cp[cid[i][j]]=j;
}
for(int i=1,l1,r1,l2,r2;i<=q;++i){
scanf("%d%d%d%d",&l2,&r2,&l1,&r1);
printf("%llu\n",qry(l1,r1,l2,r2));
}
return 0;
}
詳細信息
Subtask #1:
score: 0
Wrong Answer
Test #1:
score: 7
Accepted
time: 45ms
memory: 53112kb
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: 62ms
memory: 51160kb
input:
500 500 zszzszszzszzszszzszszzszzszszzszzszszzszszzszzszszzszszzszzszszzszzszszzszszzszzszszzszzszszzszszzszzszszzszszzszzszszzszzszszzszszzszzszszzszszzszzszszzszzszszzszszzszzszszzszzszszzszszzszzszszzszszzszzszszzszzszszzszszzszzszszzszzszszzszszzszzszszzszszzszzszszzszzszszzszszzszzszszzszszzszz...
output:
4843650749197240262 7777110205341291111 533576317536031175 16712994243500559204 9988085877723856684 9644193924482321332 3247342125341043527 18152622312040037224 13045121434804725850 10593529771756855740 13316626648976199221 6181092693273210423 9148547538129213975 9376364571107435561 2140403332478526...
result:
wrong answer 99th lines differ - expected: '13228857355690988704', found: '3730694810645784968'
Subtask #2:
score: 0
Skipped
Dependency #1:
0%
Subtask #3:
score: 0
Skipped
Dependency #2:
0%
Subtask #4:
score: 0
Time Limit Exceeded
Test #22:
score: 0
Time Limit Exceeded
input:
100000 100000 zbbabaabbaababbabaababbaabbabaabbaababbaabbabaababbabaabbaababbabaababbaabbabaababbabaabbaababbaabbabaabbaababbabaababbaabbabaabbaababbaabbabaababbabaabbaababbaabbabaabbaababbabaababbaabbabaababbabaabbaababbabaababbaabbabaabbaababbaabbabaababbabaabbaababbabaababbaabbabaababbabaabbaabab...
output:
result:
Subtask #5:
score: 0
Skipped
Dependency #4:
0%
Subtask #6:
score: 0
Skipped
Dependency #2:
0%
Subtask #7:
score: 0
Skipped
Dependency #3:
0%