QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#152829 | #6837. AC Automaton | 275307894a | WA | 5730ms | 52500kb | C++14 | 4.5kb | 2023-08-28 21:11:21 | 2023-08-28 21:11:21 |
Judging History
answer
#include<bits/stdc++.h>
#define Gc() getchar()
#define Me(x,y) memset(x,y,sizeof(x))
#define Mc(x,y) memcpy(x,y,sizeof(x))
#define d(x,y) ((m)*(x-1)+(y))
#define R(n) (rnd()%(n)+1)
#define Pc(x) putchar(x)
#define LB lower_bound
#define UB upper_bound
#define fi first
#define se second
using namespace std;using ll=long long;using db=double;using lb=long db;using ui=unsigned;using ull=unsigned long long;using pii=pair<int,int>;using LL=__int128;
const int N=3e5+5,M=3e5+5,K=600+5,mod=1e9+7,Mod=mod-1;const db eps=1e-9;const int INF=1e9+7;mt19937 rnd(time(0));
int n,m,x[N];char s[N],c[N];vector<int> S[N];
ll ans,tot;
int Bg[N],En[N],Bh,fa[N];
void Make(int x){
Bg[x]=++Bh;for(int i:S[x]) Make(i);En[x]=Bh;
}
struct bit{
int f[N];
void add(int x,int y){while(x<=n) f[x]+=y,x+=x&-x;}
int qry(int x){int ans=0;while(x) ans+=f[x],x-=x&-x;return ans;}
}f1,f2;
// void dfs(int x,int w){
// g[x]=0;
// for(int i:S[x]) dfs(i,w+(s[x]=='A'||s[x]=='?')),g[x]+=g[i];
// if(s[x]=='?') ans+=max(g[x]-w,0);
// g[x]+=(s[x]=='C'||s[x]=='?');
// }
void ins(int x){
if(s[x]=='A'){
tot+=f2.qry(En[x])-f2.qry(Bg[x]-1);
f1.add(Bg[x],1);f1.add(En[x]+1,-1);
}
else{
tot+=f1.qry(Bg[x]);
f2.add(Bg[x],1);
}
}
void del(int x){
if(s[x]=='A'){
tot-=f2.qry(En[x])-f2.qry(Bg[x]-1);
f1.add(Bg[x],-1);f1.add(En[x]+1,1);
}else{
tot-=f1.qry(Bg[x]);
f2.add(Bg[x],-1);
}
}
int BB,t1[N],t2[N],To[N],CC,t3[N];
struct block{
int f[K*2],Pt,id;ll sum,siz;
void clear(int x){Me(f,0);id=x;Pt=BB;sum=siz=0;}
void add(int x){if(x>=0) siz++,sum+=x;if(abs(x-Pt)<=BB) f[Pt+x]++;}
void ins(int x){
if(x==1) Pt--,sum+=siz,siz+=f[Pt];
else siz-=f[Pt],sum-=siz,Pt++;
}
}f[K*6];
int newn(int x){f[++CC].clear(x);return CC;}
int dfs1(int x){
int Bh=0;
for(int i:S[x]){
int p=dfs1(i);if(p) To[p]=x,Bh=(Bh?-1:p);
}
if(t1[x]||Bh==-1) {
if(!t1[x]) t1[x]=newn(x),t3[x]=newn(x);
return x;
}
return Bh;
}
int g1[N],g2[N],A[N];
// void dfs2(int x){
// g2[x]=g2[fa[x]]+(s[fa[x]]=='A'||s[fa[x]]=='?');g1[x]=0;
// for(int i:S[x]) dfs2(i),;
// }
void dfs3(int x,int Is){
while(!t1[x]) {
if(s[x]=='?')f[t1[Is]].add(A[x]);t2[x]=1;x=fa[x];
}
}
void dfs4(int x,int Is){
if(t1[x]) return;
if(s[x]=='?'&&!t2[x]) f[t3[Is]].add(A[x]);
for(int i:S[x]) dfs4(i,Is);
}
void add1(int x,int w){
for(int i=1;i<=CC;i++) if(t1[f[i].id]==i){
if(Bg[f[i].id]>Bg[x]&&Bg[f[i].id]<=En[x]) f[i].ins(w),A[f[i].id]+=w;
}else if(t3[f[i].id]==i){
if(Bg[f[i].id]>=Bg[x]&&Bg[f[i].id]<=En[x]) f[i].ins(w);
}
}
void add2(int x,int w){
int op=1;while(x){
if(op) f[t1[x]].ins(w),x=To[x];
else A[x]+=w;
op^=1;
}
}
int g[N];
void Solve(){
int i,j;scanf("%d%d",&n,&m);BB=sqrt(m);
scanf("%s",s+1);
for(i=2;i<=n;i++) scanf("%d",&fa[i]),S[fa[i]].emplace_back(i);
Make(1);
for(i=1;i<=n;i++) ins(i);
for(i=1;i<=m;i++) scanf("%d",&x[i]),Gc(),c[i]=Gc();
for(i=1;i<=m;i++){
if((i-1)%BB==0){
Me(t1,0);Me(To,0);Me(t3,0);Me(t2,0);CC=0;
for(j=i;j<min(i+BB,m+1);j++) if(!t1[x[j]]) t1[x[j]]=newn(x[j]),t3[x[j]]=newn(x[j]);
if(!t1[1]) t1[1]=newn(1),t3[1]=newn(1);
Me(g,0);for(j=n;j;j--) t1[j]&&(g[j]=n+1),g[fa[j]]+=(g[j]>0),g[j]>1&&!t1[j]&&(t1[j]=newn(j),t3[j]=newn(j));
for(j=2;j<=n;j++) if(t1[j]) {
int x=fa[j];while(!t1[x]) x=fa[x];To[j]=x;
}
// dfs2(1);
for(j=2;j<=n;j++) g2[j]=g2[fa[j]]+(s[fa[j]]=='A'||s[fa[j]]=='?');
Me(g1,0);for(j=n;j>1;j--) g1[fa[j]]+=g1[j]+(s[j]=='C'||s[j]=='?');
for(j=1;j<=n;j++) A[j]=g1[j]-g2[j];
Me(t2,0);
for(j=2;j<=n;j++) if(t1[j]) dfs3(fa[j],j);
Me(g,0);for(j=1;j<=n;j++) if(t1[j]) g[j]=j;else g[j]=g[fa[j]],!t2[j]&&s[j]=='?'&&(f[t3[g[j]]].add(A[j]),0);
// cerr<<i<<' '<<CC<<'\n';assert(CC<4*K);
// continue;
}
// continue;
// cerr<<"S "<<f[1].sum<<'\n';
del(x[i]);
if(s[x[i]]=='A'||s[x[i]]=='?') add1(x[i],1);
if(s[x[i]]=='C'||s[x[i]]=='?') add2(x[i],-1);
s[x[i]]=c[i];
ins(x[i]);
if(s[x[i]]=='A'||s[x[i]]=='?') add1(x[i],-1);
if(s[x[i]]=='C'||s[x[i]]=='?') add2(x[i],1);
ll ans=0;for(j=1;j<=CC;j++){
ans+=f[j].sum;//cerr<<"ans "<<ans<<'\n';
if(j==t1[f[j].id]&&s[f[j].id]=='?') ans+=max(A[f[j].id],0);
}
printf("%lld\n",ans+tot);
}
// for(i=1;i<=m;i++){
// int x;char c;scanf("%d",&x);
// del(x);
// Gc();s[x]=Gc();
// ins(x);
// ans=0;dfs(1,0);
// printf("%lld\n",ans+tot);
// }
}
int main(){
int t;
// scanf("%d",&t);
t=1;
while(t--) Solve();
cerr<<clock()*1.0/CLOCKS_PER_SEC<<'\n';
}
详细
Test #1:
score: 100
Accepted
time: 3ms
memory: 27772kb
input:
5 3 AC??C 1 2 2 1 1 ? 4 A 2 ?
output:
4 3 3
result:
ok 3 lines
Test #2:
score: 0
Accepted
time: 16ms
memory: 27876kb
input:
1000 1000 AC?ACCCCC?CCA??CCCCC?A?AC?C??C?ACCCACC?C?CCC?CACACA???????C?????CC?C?AAAAACCA??AAAACACACA???AACAC?CCC?CAC?AAAACCAC???ACAA??A??A?CA?A?ACACACCAA?ACACA?AC??AC??ACAAACCC??CAA?A???CC?AA??AC???A?CCCC??CACCAACC?AA?C?AAACCCA?AAA??C?CC??CACCACAACCA?AACCC?A?CCC?CC???AA?CACCCAAC??CAA??C?AA??CA???CAAA...
output:
2344 2345 2342 2342 2768 2768 2772 2772 2772 2772 2772 2772 2772 2767 2767 2767 2767 2764 2766 2766 2769 2765 2761 2764 2767 2772 2772 2772 2772 2772 2772 2777 2777 2777 2777 2774 2771 2774 2782 2778 2778 2772 2768 2772 2772 2772 2772 2772 2774 2774 2778 2781 2781 2779 2782 2784 2787 2782 2786 2788 ...
result:
ok 1000 lines
Test #3:
score: -100
Wrong Answer
time: 5730ms
memory: 52500kb
input:
300000 300000 AAA?CA?AA?AC?A?CCA?AACCAAA???CA?ACCAACCCCAACAAA?CCAAAC?A?C??CC?C?C?CCCA?CAA?ACA??C?C?AC??CA??ACA?AA???CACAAA?CACCCCCCC?A?AAAAAC?AACCA????CCC?C?AAACCCAA?C???CCCC?AAACAAA???A?CAAC??A??A??CCCC??AA?C??ACA?AACAAA????CAA???AAAAACC?C?CCA?CCAA?AAC?CC?CA?A??CC??CCAC??C??????AAC?AA?AA?AAC?C??AAC...
output:
14995917235 14995917235 14996064601 14996083631 14995980103 14995925797 14995925797 14995925797 14995967212 14995967212 14995967212 14995876209 14995774036 14995774036 14995774036 14995876789 14995866112 14995756156 14995647553 14995647553 14995560535 14995560535 14995583618 14995583618 14995583618 ...
result:
wrong answer 9th lines differ - expected: '14995967213', found: '14995967212'