AC Automaton
시간 제한13초메모리 제한1024 MB
각 노드가 A, C, ? 중 하나로 표시된 루트 트리에서 갱신이 일어날 때마다 ?를 적절히 채워 얻을 수 있는 (조상 A, 자손 C) 쌍의 최댓값을 구한다.
문제
JB is trying to get more "AC" on problems with his AC Automaton.
The AC Automaton is a rooted tree with nodes and node 1 as root. Each node except the root has its unique parent and a character on it, which is one of 'A', 'C', or '?'. A node is called an ancestor of if and only if or is an ancestor of . The number of "AC" JB can get equal to the number of ordered pairs that is an ancestor of , 'A' and 'C'. JB can replace '?' arbitrarily with 'A' or 'C'. His goal is to get more "AC" after replacing all '?'.
However, the problem always changes. JB will change his AC Automaton times. Each time he will modify the character on one of the nodes to one of 'A', 'C', or '?' (the character will possibly not change). JB wants you to answer the maximum number of "AC" he can get if he replaces all '?' on his AC Automaton after each modification. Note that JB will not actually modify the AC Automaton while calculating the maximum number of "AC". You can refer to the sample to help you understand.
입력
The first line contains two integers and ().
The second line contains a string consisting of characters, representing the characters on each node. It's guaranteed that the characters in the string is one of 'A', 'C', and '?'.
The third line contains integers, the -th integer () represents the parent of node .
The next lines describe the modifications. The -th of the following line consists one integer () and a character (y\in \\{'A', 'C', '?'\\}), representing one modification.
출력
Output lines.
In the -th line, output one integer representing the maximum number of "AC" after the -th modification.
힌트
After the first modification, one of the best way of replacing characters is "ACCCC", and there are pairs and .
After the second modification, one of the best way of replacing characters is "ACCAC", and there are pairs and .
After the third modification, one of the best way of replacing characters is "AACAC", and there are pairs and .