QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#152785#6837. AC Automaton275307894aTL 61ms26556kbC++144.1kb2023-08-28 20:22:252023-08-28 20:22:26

Judging History

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

  • [2023-08-28 20:22:26]
  • 评测
  • 测评结果:TL
  • 用时:61ms
  • 内存:26556kb
  • [2023-08-28 20:22:25]
  • 提交

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],t4[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),t3[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 dfs4(int x,int Is){
	if(t1[x]) return;
	if(s[x]=='?') f[t4[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(Bg[f[i].id]>Bg[x]&&Bg[f[i].id]<=En[x]){
		f[i].ins(w);
		if(t1[f[i].id]==i) A[f[i].id]+=w;
	} else if(t4[x]==i) 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;
	}
}
void Solve(){
	int i,j;scanf("%d%d",&n,&m);BB=5;
	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);Me(t4,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]);
			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]){
				t4[j]=newn(j);
				for(int p:S[j]) if(!t2[p]) dfs4(p,j);
			}
		}
		// 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: 2ms
memory: 26556kb

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: 61ms
memory: 26492kb

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
Time Limit Exceeded

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
14995967213
14995876211
14995774037
14995774037
14995774037
14995876791
14995866113
14995756158
14995647554
14995647554
14995560537
14995560537
14995583619
14995583619
14995583619
...

result: