QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#152763#6837. AC Automaton275307894aWA 10ms28408kbC++144.0kb2023-08-28 19:57:412023-08-28 19:57:41

Judging History

你现在查看的是最新测评结果

  • [2023-08-28 19:57:41]
  • 评测
  • 测评结果:WA
  • 用时:10ms
  • 内存:28408kb
  • [2023-08-28 19:57:41]
  • 提交

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*4];
int newn(int x){f[++CC].clear(x);return CC;}
int dfs1(int x){
	vector<int> B;
	for(int i:S[x]){
		int p=dfs1(i);if(p) B.emplace_back(p);
	} 
	if(t1[x]||B.size()>=2) {
		if(!t1[x]) t1[x]=newn(x),t2[x]=newn(x);
		for(int i:B) To[i]=x;
		return x;
	}
	if(B.size()==1) return B[0];
	return 0;
}
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),g1[x]+=g1[i]+(s[i]=='C'||s[i]=='?');
}
void dfs3(int x,int Is,int op){
	if(t1[x]||t2[x]||!x) return;
	if(s[x]=='?') f[op?t1[Is]:t3[Is]].add(A[x])/*,cerr<<"ins "<<f[t1[Is]].sum<<'\n'*/;
	t2[x]=1;dfs3(fa[x],Is,1);
	for(int i:S[x]) dfs3(i,Is,0);
}
void add1(int x,int w){
	for(int i=1;i<=CC;i++) if(Bg[f[i].id]>Bg[x]&&Bg[f[i].id]<=En[x]) f[i].ins(w),A[f[i].id]+=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;
	}
}
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<i+BB;j++) if(!t1[x[j]]) t1[x[j]]=newn(x[j]),t3[x[j]]=newn(x[j]);
			dfs1(1);
			dfs2(1);for(j=1;j<=n;j++) A[j]=g1[j]-g2[j]/*,cerr<<A[j]<<' '*/;
			Me(t2,0);for(j=1;j<=n;j++) if(t1[j]) dfs3(fa[j],j,1);
			for(j=1;j<=n;j++) if(t1[j]&&!S[j].empty()&&!t2[S[j].front()]){
				for(int p:S[j]) dfs3(p,j,0);
			} 
		}
		// 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';
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 0ms
memory: 28408kb

input:

5 3
AC??C
1 2 2 1
1 ?
4 A
2 ?

output:

4
3
3

result:

ok 3 lines

Test #2:

score: -100
Wrong Answer
time: 10ms
memory: 22280kb

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:

2256
2257
2254
2254
2699
2699
2703
2703
2703
2703
2703
2703
2703
2698
2698
2698
2698
2695
2697
2697
2700
2696
2692
2695
2698
2703
2703
2703
2703
2703
2703
2576
2576
2576
2576
2573
2570
2573
2581
2577
2577
2571
2567
2571
2571
2571
2571
2571
2573
2573
2577
2580
2580
2578
2581
2583
2586
2581
2585
2587
...

result:

wrong answer 1st lines differ - expected: '2344', found: '2256'