QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#152671 | #6837. AC Automaton | 275307894a | TL | 7ms | 12508kb | C++14 | 1.3kb | 2023-08-28 16:42:48 | 2023-08-28 16:42:48 |
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=(1<<15)+5,mod=1e9+7,Mod=mod-1;const db eps=1e-9;const int INF=1e9+7;mt19937 rnd(time(0));
int n,m,fa[N];char s[N];vector<int> S[N];
ll ans,tot;
void Make(int x,int w){
if(s[x]=='A') w++;
if(s[x]=='C'||s[x]=='?') ans+=w;
for(int i:S[x]) Make(i,w);
}
int Si[N];
void dfs(int x,int w){
Si[x]=0;
for(int i:S[x]) dfs(i,w+(s[x]=='A'||s[x]=='?')),Si[x]+=Si[i];
if(s[x]=='?') ans+=max(Si[x]-w,0);
Si[x]+=(s[x]=='C'||s[x]=='?');
}
void Solve(){
int i,j;scanf("%d%d",&n,&m);
scanf("%s",s+1);
for(i=2;i<=n;i++) scanf("%d",&fa[i]),S[fa[i]].emplace_back(i);
for(i=1;i<=m;i++){
int x;char c;scanf("%d",&x);Gc();s[x]=Gc();
ans=tot=0;Make(1,0);dfs(1,0);
printf("%lld\n",ans);
}
}
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: 3ms
memory: 12508kb
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: 7ms
memory: 11088kb
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 14995967213 14995967213 14995967213 14995876211 14995774037 14995774037 14995774037 14995876791 14995866113 14995756158 14995647554 14995647554 14995560537 14995560537 14995583619 14995583619 14995583619 ...