Problem:
Problem F: Marked Ancestor

You are given a tree T that consists of N nodes. Each node is numbered from 1 to N, and node 1 is always the root node of T. Consider the following two operations on T:

M v: (Mark) Mark node v.

Q v: (Query) Print the index of the nearest marked ancestor of node v which is nearest to it. Initially, only the root node is marked. Note that a node is an ancestor of itself.

Your job is to write a program that performs a sequence of these operations on a given tree and calculates
the value that each Q operation will print. To avoid too large output file, your program is requested to
print the sum of the outputs of all query operations. Note that the judges confirmed that it is possible to
calculate every output of query operations in a given sequence.

Input

The input consists of multiple datasets. Each dataset has the following format:

The first line of the input contains two integers N and Q, which denotes the number of nodes in the tree
T and the number of operations, respectively. These numbers meet the following conditions: 1 ≤ N ≤ 100000 and 1 ≤ Q ≤ 100000.

The following N - 1 lines describe the configuration of the tree T. Each line contains a single integer pi (i = 2, ... , N), which represents the index of the parent of i-th node.

The next Q lines contain operations in order. Each operation is formatted as "M v" or "Q v", where v is the index of a node.

The last dataset is followed by a line containing two zeros. This line is not a part of any dataset and should not be processed.

Output

For each dataset, print the sum of the outputs of all query operations in one line.

Sample Input

6 3
1
1
2
3
3
Q 5
M 3
Q 5
0 0

Output for the Sample Input

4

Solution in JAVA (judge verdict: Wrong Answer)

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.util.ArrayList;
import java.util.Arrays;

public class Main {
	static FastScanner sc = new FastScanner(System.in);
	static final int POP = 10000000;
	static final int INF = 1 << 30;
	static int N, Q;

	public static void main(String[] args) {
		while (true) {
			N = sc.nextInt();
			if (N == 0) break;
			Q = sc.nextInt();
			System.out.println(solve());
		}
	}

	static long solve() {
		ArrayList<ArrayList<Integer>> child = new ArrayList<ArrayList<Integer>>();
		ArrayList<ArrayList<Integer>> query = new ArrayList<ArrayList<Integer>>();
		for (int i = 0; i < N; ++i) {
			child.add(new ArrayList<Integer>());
			query.add(new ArrayList<Integer>());
		}
		for (int i = 1; i < N; ++i) {
			int p = sc.nextInt() - 1;
			child.get(p).add(i);
		}
		ArrayList<Integer> node = new ArrayList<Integer>();
		ArrayList<Integer> sort = new ArrayList<Integer>();
		node.add(0);
		while (!node.isEmpty()) {
			int cur = node.get(node.size() - 1);
			node.remove(node.size() - 1);
			sort.add(cur);
			if (cur >= POP) {
				continue;
			}
			node.add(POP + cur);
			for (int i = child.get(cur).size() - 1; i >= 0; --i) {
				node.add(child.get(cur).get(i));
			}
		}

		int[] mark = new int[N];
		mark[0] = -1;
		Arrays.fill(mark, INF);
		for (int i = 0; i < Q; ++i) {
			char type = sc.nextChar();
			int n = sc.nextInt() - 1;
			if (type == 'Q') {
				query.get(n).add(i);
			} else {
				mark[n] = Math.min(i, mark[n]);
			}
		}

		int[] time = new int[N];
		int[] markedNode = new int[N];
		int[] pos = new int[N];
		int[] prevTime = new int[N];
		int[] prevMarkedNode = new int[N];
		int[] prevSize = new int[N];
		time[0] = -1;
		markedNode[0] = 0;
		int size = 1;
		long ans = 0;
		for (int i = 1; i < 2 * N; ++i) {
			int cur = sort.get(i);
			if (cur >= POP) {
				cur -= POP;
				if (mark[cur] != INF) {
					time[pos[cur]] = prevTime[cur];
					markedNode[pos[cur]] = prevMarkedNode[cur];
					size = prevSize[cur];
				}
			} else {
				for (int q : query.get(cur)) {
					int lo = 0;
					int hi = size;
					while (lo + 1 < hi) {
						int mi = (lo + hi) / 2;
						if (q < time[mi]) {
							hi = mi;
						} else {
							lo = mi;
						}
					}
					ans += markedNode[lo] + 1;
				}
				if (mark[cur] != INF) {
					prevSize[cur] = size;
					int left = 0;
					int right = size;
					while (left + 1 < right) {
						int mid = (left + right) / 2;
						if (mark[cur] < time[mid]) {
							right = mid;
						} else {
							left = mid;
						}
					}
					pos[cur] = right;
					prevTime[cur] = time[right];
					prevMarkedNode[cur] = markedNode[right];
					time[right] = mark[cur];
					markedNode[right] = cur;
					size = right + 1;
				}
			}
		}

		return ans;
	}

	static class FastScanner {
		Reader input;

		FastScanner() {
			this(System.in);
		}

		FastScanner(InputStream stream) {
			this.input = new BufferedReader(new InputStreamReader(stream));
		}

		int nextInt() {
			try {
				int sign = 1;
				int b = input.read();
				while ((b < '0' || '9' < b) && b != '-' && b != '+') {
					b = input.read();
				}
				if (b == '-') {
					sign = -1;
					b = input.read();
				} else if (b == '+') {
					b = input.read();
				}
				int ret = b - '0';
				while (true) {
					b = input.read();
					if (b < '0' || '9' < b) return ret * sign;
					ret *= 10;
					ret += b - '0';
				}
			} catch (IOException e) {
				e.printStackTrace();
				return -1;
			}
		}

		char nextChar() {
			try {
				int b = input.read();
				while (Character.isWhitespace(b)) {
					b = input.read();
				}
				return (char) b;
			} catch (IOException e) {
				e.printStackTrace();
				return 0;
			}
		}
	}

}