Under construction

Still being built. Things may change

AC Automaton

Time limit13sMemory limit1024 MB

Summary
Tree nodes are A, C, or ?. After each point update, find the most ancestor-descendant pairs with an A above a C, filling each ? freely.
Level

Hard8 of 10

Topics
Tree, Segment tree, Dynamic programming
Solved
No attempts yet

Problem

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 is equal to the number of ordered pairs (x,y)(x,y) such that xx is an ancestor of yy, s_xs\_x is 'A', and s_ys\_y is 'C'. JB can replace each '?' with 'A' or 'C'. His goal is to get more "AC" after replacing all '?'.

However, the problem keeps changing. JB will change his AC Automaton qq times. Each time, he modifies the character on one node xx to 'A', 'C', or '?' (the character may stay the same). After each modification, find the maximum number of "AC" he can get if he replaces all '?' characters. JB does not actually modify the AC Automaton while calculating this maximum. See the sample for an example.

Input

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 of nn characters, representing the character on each node. Every character 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) is the parent of node i+1i+1.

The next qq lines describe the modifications. The ii-th of these lines contains an integer xx (1≤x≤n1\le x\le n) and a character yy (yy is one of 'A', 'C', '?'), meaning node xx is set to yy.

Output

Output qq lines. The ii-th line contains one integer, the maximum number of "AC" after the ii-th modification.

Hint

After the first modification, one of the best ways to replace the characters is "ACCCC", and it has 4 pairs: (1,2), (1,3), (1,4), and (1,5).

After the second modification, one of the best ways to replace the characters is "ACCAC", and it has 3 pairs: (1,2), (1,3), and (1,5).

After the third modification, one of the best ways to replace the characters is "AACAC", and it has 3 pairs: (1,3), (2,3), and (1,5).

Examples1

  1. Example 1

    Input
    5 3
    AC??C
    1 2 2 1
    1 ?
    4 A
    2 ?
    
    Expected output
    4
    3
    3