QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#591659#6837. AC Automatonydzr00000TL 6855ms135536kbC++175.5kb2024-09-26 17:06:122024-09-26 17:06:13

Judging History

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

  • [2024-09-26 17:06:13]
  • 评测
  • 测评结果:TL
  • 用时:6855ms
  • 内存:135536kb
  • [2024-09-26 17:06:12]
  • 提交

answer

#include<bits/stdc++.h>
using namespace std;
vector<int>e[300001],vr[300001];
int ch[300001];
int dep[300001],fir[300001],top=0,dfn[600001];
int st[600001][20],lg2[600001];
int up[300001][3],down[300001][3];
inline void dfs(int now)
{
	dfn[++top]=now;
	fir[now]=top;
	for(auto to: e[now])
	{
		dep[to]=dep[now]+1;
		dfs(to);
		dfn[++top]=now;
	}
}
inline int LCA(int u,int v)
{
	u=fir[u];v=fir[v];
	if(u>v)
		swap(u,v);
	int t=lg2[v-u+1];
	int x=st[u][t],y=st[v-(1<<t)+1][t];
	return dep[x]<dep[y]?x:y;
}
vector<pair<int,int>>op;
int bel[300001],fa[300001];
int f[300001],g[300001];
vector<int>point;
int upnode[300001],dwnode[300001],id[300001],vf[300001];
inline void calc(int now)
{
	up[now][ch[now]]++;
	down[now][ch[now]]++;
	for(auto to: e[now])
	{
		for(int i=0;i<3;i++)
			up[to][i]+=up[now][i];
		calc(to);
		for(int i=0;i<3;i++)
			down[now][i]+=down[to][i];
	}
}
struct Block{
	int buf[3001],del;
	int cnt0,cnt2;
	int tagf,tagg;
	long long sumf,sumg;
	inline void init(int L)
	{
		tagf=tagg=0;
		sumf=sumg=0;
		cnt0=cnt2=0;del=L;
		fill(buf,buf+2*L+1,0);
	}
	inline void updateF(int val)
	{
		sumf-=tagf*cnt0;
		tagf+=val;
		sumf+=tagf*cnt0;
	}
	inline void updateG(int val)
	{
		if(val==1)
		{
			sumg+=cnt2;
			tagg++;
			cnt2+=buf[del-tagg];
		}
		else
		{
			cnt2-=buf[del-tagg];
			tagg--;
			sumg-=cnt2;
		}
	}
}B[3001];
int ord[15001],rnk[300001],sz[300001],anc[300001];
inline void getorder(int now)
{
	ord[++top]=now;
	rnk[now]=top;
	sz[now]=1;
	for(auto to: vr[now])
	{
		anc[to]=now;
		getorder(to);
		sz[now]+=sz[to];
	}
}
int main(){
	int n,q;
	scanf("%d %d",&n,&q);
	string str;
	cin>>str;
	for(int i=0;i<n;i++)
		ch[i+1]=(str[i]=='?'?2:(str[i]=='A'?0:1));
	for(int i=2;i<=n;i++)
	{
		int f;
		scanf("%d",&f);
		fa[i]=f;
		e[f].push_back(i);
	}
	dep[1]=1;
	dfs(1);
	for(int i=1;i<=top;i++)
		st[i][0]=dfn[i];
	lg2[0]=-1;
	for(int i=1;i<=top;i++)
		lg2[i]=lg2[i>>1]+1;
	for(int i=1;i<=lg2[top];i++)
		for(int j=1;j+(1<<i)-1<=top;j++)
		{
			int u=st[j][i-1],v=st[j+(1<<(i-1))][i-1];
			st[j][i]=(dep[u]<dep[v]?u:v);
		}
	int L=(int)sqrt(1.00*q);
	//int L=1;
	auto solve=[&]()->void
	{
		//Build of Virtual Tree
		for(auto [x,c]: op)
			point.push_back(x);
		point.push_back(1);
		sort(point.begin(),point.end(),[&](const int &x,const int &y){
			return fir[x]<fir[y];
		});
		point.erase(unique(point.begin(),point.end()),point.end());
		int s=point.size();
		for(int i=0;i<s-1;i++)
			point.push_back(LCA(point[i],point[i+1]));
		sort(point.begin(),point.end(),[&](const int &x,const int &y){
			return fir[x]<fir[y];
		});
		point.erase(unique(point.begin(),point.end()),point.end());
		s=point.size();
		for(int i=0;i<s-1;i++)
		{
			int u=LCA(point[i],point[i+1]);
			vf[i+1]=u;
			vr[u].push_back(point[i+1]);
		}
		//Block
		for(int i=0;i<s;i++)
			id[point[i]]=i+1;
		for(int i=0;i<s;i++)
		{
			int x=point[i];
			while(x!=vf[i])
			{
				dwnode[x]=point[i];
				x=fa[x];
			}
		}
		for(int i=1;i<=n;i++)
			upnode[i]=(id[i]?i:upnode[fa[i]]);
		int Bnum=0;
		for(int i=1;i<=n;i++)
			if(!id[i])
			{
				bel[i]=(dwnode[i]?id[dwnode[i]]:id[upnode[i]]+s);
				Bnum=max(Bnum,bel[i]);
			}
		for(int i=1;i<=2*s;i++)
			B[i].init(L);
		//Calculate
		top=0;
		calc(1);getorder(1);
		long long ans=0;
		for(int i=1;i<=n;i++)
		{
			f[i]=down[i][1]+down[i][2];
			g[i]=down[i][1]+down[i][2]-up[i][0]-up[i][2];
			int Bid=bel[i];
			if(!Bid)
			{
				ans+=(ch[i]==0)*f[i]+(ch[i]==2)*max(g[i],0);
				continue;
			}
			if(ch[i]==0)
			{
				B[Bid].sumf+=f[i];
				B[Bid].cnt0++;
			}
			if(ch[i]==2)
			{
				B[Bid].sumg+=max(g[i],0);
				if(g[i]>=0)
					B[Bid].cnt2++;
				if(g[i]>=-L&&g[i]<=L)
					B[Bid].buf[g[i]+L]++;
			}
		}
		for(int i=1;i<=Bnum;i++)
			ans+=B[i].sumf+B[i].sumg;
		auto updateU=[&](int x,int op)->void
		{
			auto change=[&](int x,int op)->void
			{
				ans-=B[x].sumf+B[x].sumg;
				B[x].updateF(op);B[x].updateG(op);
				ans+=B[x].sumf+B[x].sumg;	
			};
			change(id[x],op);
			while(anc[x])
			{
				x=anc[x];
				change(id[x],op);
				ans-=(ch[x]==0)*f[x]+(ch[x]==2)*max(g[x],0);
				f[x]+=op;g[x]+=op;
				ans+=(ch[x]==0)*f[x]+(ch[x]==2)*max(g[x],0);
			}
		};
		auto updateD=[&](int x,int op)->void
		{
			int st=rnk[x]+1,ed=rnk[x]+sz[x]-1;
			auto change=[&](int x,int op)->void
			{
				ans-=B[x].sumg;
				B[x].updateG(op);
				ans+=B[x].sumg;	
			};
			change(id[x]+s,op);
			for(int i=st;i<=ed;i++)
			{
				int u=ord[i];
				change(id[u],op);change(id[u]+s,op);
				ans-=(ch[u]==2)*max(g[u],0);
				g[u]+=op;
				ans+=(ch[u]==2)*max(g[u],0);
			}
		};
		auto updateN=[&](int x,int typ,int op)->void
		{
			if(typ==0)
				ans+=op*f[x],g[x]-=op;
			else if(typ==1)
				f[x]+=op,g[x]+=op;
			else
				f[x]+=op,ans+=op*max(g[x],0);
		};
		for(auto [x,c]: op)
		{
			if(ch[x]!=c)
			{
				updateN(x,ch[x],-1);
				if(ch[x]!=0) updateU(x,-1);
				if(ch[x]!=1) updateD(x,1);
				ch[x]=c;
				if(ch[x]!=0) updateU(x,1);
				if(ch[x]!=1) updateD(x,-1);
				updateN(x,ch[x],1);
			}
			printf("%lld\n",ans);
		}
		//Clear
		for(int i=1;i<=n;i++)
		{
			id[i]=bel[i]=anc[i]=upnode[i]=dwnode[i]=0;
			for(int j=0;j<3;j++)
				up[i][j]=down[i][j]=0;
		}
		for(int i=1;i<s;i++)
			vr[vf[i]].clear();
		op.clear();point.clear();
	};
	for(int i=1;i<=q;i++)
	{
		int x;
		char c;
		scanf("%d %c",&x,&c);
		op.push_back({x,(c=='?'?2:(c=='A'?0:1))});
		if(i%L==0||i==q)
			solve();
	}
	
    return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

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

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: 3ms
memory: 47912kb

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: 0
Accepted
time: 6217ms
memory: 128664kb

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

result:

ok 300000 lines

Test #4:

score: 0
Accepted
time: 3039ms
memory: 109120kb

input:

300000 300000
?ACA???CCCA?C???AA??CAAAAACCC??A?CAC??C???????CAA?C?C?A?C???A?CC?CCAC?C?ACC??C?CAACA??CA?CA?CAACA??AACCC?CCCACACC?AAC?CA??C?C?CCCA?ACAA??AA?CCAACACCA?AC?C?CCCCCCAAA?CC??A?CCC???A?CA?ACAC???C??CCA??CCAA?AAC???CCCC??AA?C?C?C?CACAC?C?CA??AACC?A????C??CACAAAAA?C?CAACACA?ACCAC?A?CCCACACA??A...

output:

200180
200181
200182
200182
200182
200182
200182
200182
200183
200183
200183
200182
200183
200183
200183
200183
200183
200182
200183
200183
200183
200183
200183
200183
200183
200183
200184
200183
200182
200181
200180
200180
200180
200181
200181
200182
200181
200180
200180
200181
200182
200181
200182...

result:

ok 300000 lines

Test #5:

score: 0
Accepted
time: 6424ms
memory: 135536kb

input:

300000 300000
A??CCAAACAC?A?CCACA?CA??ACC?CCA?CCAACACAC?A?CCC??ACC?ACC?CA?CA?C??A?CACCCC?C?AC?AAC??A???CA?C???AC?A?A?ACCCAACC?AA?CCACCCAAAA????C?ACC?????ACACA?C?A?CCC?A?AC????AC?C?A???ACA??CAACACC????CAA???ACCAC???CCCA?A?CAA?C??CCCCA?ACCA?A?CCC?ACA?C??AA?C??ACA?AAACC?CCAACCCAC?CAAA??ACC?ACCAA??????A...

output:

15015050020
15015050020
15015135045
15015202340
15015303448
15015303448
15015282042
15015282042
15015310379
15015329461
15015377957
15015514924
15015514924
15015613521
15015724614
15015640441
15015598420
15015635210
15015635210
15015544590
15015671373
15015653717
15015706346
15015805421
15015835446
...

result:

ok 300000 lines

Test #6:

score: 0
Accepted
time: 2435ms
memory: 103952kb

input:

300000 300000
??????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????...

output:

10018652160
10018652160
10018652160
10018652160
10018652160
10018652160
10018652160
10018652160
10018652160
10018652160
10018652160
10018652160
10018652160
10018652160
10018652160
10018652160
10018652160
10018652160
10018652160
10018652160
10018652160
10018652160
10018652160
10018652160
10018652160
...

result:

ok 300000 lines

Test #7:

score: 0
Accepted
time: 6855ms
memory: 100248kb

input:

300000 300000
??????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????...

output:

2619172
2891340
2619172
2891340
2619172
2891340
2619172
2891340
2619172
2891340
2619172
2891340
2619172
2891340
2619172
2891340
2619172
2891340
2619172
2891340
2619172
2891340
2619172
2891340
2619172
2891340
2619172
2891340
2619172
2891340
2619172
2891340
2619172
2891340
2619172
2891340
2619172
2891...

result:

ok 300000 lines

Test #8:

score: 0
Accepted
time: 6751ms
memory: 97864kb

input:

300000 300000
??????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????...

output:

2972868
3149825
2972868
2700372
2875914
2700372
2972868
2700372
2875914
3149825
2875914
3149825
2972868
2700372
2972868
2700372
2972868
2700372
2875914
2700372
2972868
2700372
2875914
3149825
2972868
3149825
2972868
3149825
2972868
3149825
2875914
2700372
2875914
3149825
2972868
3149825
2972868
2700...

result:

ok 300000 lines

Test #9:

score: 0
Accepted
time: 6776ms
memory: 100396kb

input:

300000 300000
??????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????...

output:

2733980
2504686
2733980
2504686
2733980
3006821
2775490
2504686
2733980
3006821
2733980
2504686
2775490
2504686
2733980
2504686
2733980
2504686
2775490
2504686
2775490
2504686
2775490
3006821
2733980
2504686
2775490
2504686
2775490
2504686
2733980
3006821
2775490
3006821
2775490
3006821
2775490
3006...

result:

ok 300000 lines

Test #10:

score: -100
Time Limit Exceeded

input:

300000 300000
?C??C?C??C??C??CC???C???CCC?CCCCCC??C???C?CCCC????C??C????C???C???C????C?CC??CC?C???CC?C??C?C?CCC??C?CCCCC?C??C??C?CC?C?CC?CC?CCC??C?C???C??CC??CC???CC?C??CCC??C??C???C???C??C?C????CCCCCC????CC?CC?CCC?CCC?CCC?C???CC????CCC?CC??C?CC?C?C?C???CCC?CCCCC??C??C???CC??C??CCCCC?C??C?CCC???C???...

output:

2075827
1795155
1531311
1795155
2075827
1809426
2075827
1809426
1531311
1795155
1531311
1795155
1531311
1795155
1531311
1795155
2075827
1809426
2075827
1809426
2075827
1795155
1531311
1795155
2075827
1795155
2075827
1795155
2075827
1795155
2075827
1809426
2075827
1809426
2075827
1795155
2075827
1795...

result: