AC Automaton

시간 제한13초메모리 제한1024 MB

요약
각 노드가 A, C, ? 중 하나로 표시된 루트 트리에서 갱신이 일어날 때마다 ?를 적절히 채워 얻을 수 있는 (조상 A, 자손 C) 쌍의 최댓값을 구한다.
난이도

어려움10점 중 8점

유형
트리, 동적 계획법, 그리디, DFS
정답자
아직 제출이 없습니다

문제

JB is trying to get more "AC" on problems with his AC Automaton.

The AC Automaton is a rooted tree with nn nodes and node 1 as root. Each node ii except the root has its unique parent p_ip\_i and a character s_is\_i on it, which is one of 'A', 'C', or '?'. A node xx is called an ancestor of yy if and only if x=p_yx = p\_y or xx is an ancestor of p_yp\_y. The number of "AC" JB can get equal to the number of ordered pairs (x,y)(x,y) that xx is an ancestor of yy, s_x=s\_x = 'A' and s_y=s\_y = '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 qq times. Each time he will modify the character on one of the nodes xx 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 nn and qq (1≤n,q≤300 0001\le n, q\le 300\ 000).

The second line contains a string consisting of nn 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 n−1n - 1 integers, the ii-th integer p_ip\_i (1≤p_i≤i1\le p\_i \le i) represents the parent of node i+1i+1.

The next qq lines describe the modifications. The ii-th of the following line consists one integer xx (1≤x≤n1\le x\le n) and a character yy (y\in \\{'A', 'C', '?'\\}), representing one modification.

출력

Output qq lines.

In the ii-th line, output one integer representing the maximum number of "AC" after the ii-th modification.

힌트

After the first modification, one of the best way of replacing characters is "ACCCC", and there are 44 pairs (1,2),(1,3),(1,4),(1,2),(1,3),(1,4), and (1,5)(1,5).

After the second modification, one of the best way of replacing characters is "ACCAC", and there are 33 pairs (1,2),(1,3),(1,2),(1,3), and (1,5)(1,5).

After the third modification, one of the best way of replacing characters is "AACAC", and there are 33 pairs (1,3),(2,3),(1,3),(2,3), and (1,5)(1,5).

예제1

  1. 예제 1

    입력
    5 3
    AC??C
    1 2 2 1
    1 ?
    4 A
    2 ?
    
    예상 출력
    4
    3
    3