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 C++ (judge verdict: Runtime Error)

#include<iostream>
#include<vector>
#include<algorithm>
#include<queue>
using namespace std;
#define INF (1<<29)




template<class T>
class BinomialHeap{
	/*template<class S>
	struct Vector{
		int sz,cap;
		S *p;
		Vector():sz(0),cap(0),p(0){}
		~Vector(){
			clear();
		}
		void swap(Vector &v){
			std::swap(sz,v.sz);
			std::swap(cap,v.cap);
			std::swap(p,v.p);
		}
		int size()const{return sz;}
		bool empty()const{return !sz;}
		S& back()const{
			return p[sz-1];
		}
		void push_back(S v){
			if(sz==cap)reserve(sz?sz*2:1);
			new(p+sz++) S(v);
		}
		void pop_back(){
			p[--sz].~S();
			if(sz&&sz*4<cap){
				reserve(cap/2);
			}
		}
		void reserve(int n){
			S *p=(S*)operator new(n*sizeof(S));
			for(int i=0;i<sz;i++){
				new(p+i) S(this->p[i]);
				p[i].~S();
			}
			if(this->p)operator delete(this->p);
			this->p=p;
			cap=n;
		}
		void clear(){
			for(int i=0;i<sz;i++){
				p[i].~S();
			}
			if(p)operator delete(p);
			sz=cap=0;
			p=0;
		}
		S& operator[](int i)const{
			return p[i];
		}
	};*/
	struct Tree{
		T val;
		std::vector<Tree*> edge;
		Tree(T &v):val(v){}
		~Tree(){
			for(int i=0;i<degree();i++)delete edge[i];
		}	
		void add(Tree *t){edge.push_back(t);}
		void pop(Tree *t){edge.pop_back();}
		int degree()const{return edge.size();}
	};
	std::vector<Tree*> trees;
	mutable int imax;

	static Tree* add(Tree* a,Tree* b){
		if(a->val > b->val){
			a->add(b);
			return a;
		}else{
			b->add(a);
			return b;
		}
	}
	void findmax()const{
		for(int i=0;i<trees.size();i++)
			if(trees[i] && (imax==-1||trees[i]->val > trees[imax]->val))
				imax=i;
	}
public:
	BinomialHeap():imax(-1){}
	~BinomialHeap(){
		clear();
	}
	//O(log n)
	void meld(BinomialHeap &h){
		imax=-1;
		Tree* carry=NULL;
		//if(trees.size()<h.trees.size())trees.swap(h.trees);
		while(trees.size()<h.trees.size())trees.push_back(NULL);
		while(trees.size()>h.trees.size())h.trees.push_back(NULL);
		for(int i=0;i<trees.size();i++){
			//if(carry==NULL && h.trees.size()<=i)break;
			if(carry){
				if(h.trees[i]){
					carry=add(carry,h.trees[i]);
				}else if(trees[i]){
					carry=add(carry,trees[i]);
					trees[i]=NULL;
				}else{
					trees[i]=carry;
					carry=NULL;
				}
			}else if(h.trees[i]){
				if(trees[i]){
					carry=add(trees[i],h.trees[i]);
					trees[i]=NULL;	
				}else{
					trees[i]=h.trees[i];	
				}
			}
		}
		if(carry)trees.push_back(carry);
		h.trees.clear();
	}
	//O(log n)
	void insert(T v){
		BinomialHeap h;
		h.trees.push_back(new Tree(v));
		meld(h);
	}
	T getmax()const{
		if(imax==-1)findmax();
		return (trees[imax])->val;
	}
	void popmax(){
		if(imax==-1)findmax();
		BinomialHeap h;
		trees[imax]->edge.swap(h.trees);	
		delete trees[imax];
		trees[imax]=NULL;	
		meld(h);
		//while(!trees.empty() && trees.back()==NULL)trees.pop_back();
		imax=-1;
	}
	bool empty()const{
		//return trees.empty();
		imax=-1;findmax();
		return imax==-1;
	}
	void clear(){
		imax=-1;
		for(int i=0;i<trees.size();i++)
			if(trees[i])
				delete trees[i];
		trees.clear();
	}
	//void remove(int id){}
	//decrease_key remove(int id,T v){}
};



int n,q;
int marked[100000];
BinomialHeap<int> bh[100000];
vector<int> edge[100000];
long long ans;



void dfs(int u){
	for(int i=0;i<edge[u].size();i++){
		dfs(edge[u][i]);
		bh[u].meld(bh[edge[u][i]]);
	}
	int t=marked[u];
	while(!bh[u].empty() && t<bh[u].getmax()){
		ans+=u+1;
		bh[u].popmax();
	}
}


int main(){
	while(cin>>n>>q&&n){
		for(int i=0;i<n;i++)edge[i].clear();
		for(int i=0;i<n;i++)bh[i].clear();
		for(int i=0;i<n;i++)marked[i]=INF;
		marked[0]=0;
		for(int i=1;i<n;i++){
			int p;
			cin>>p;
			edge[p-1].push_back(i);
		}
		for(int i=0;i<q;i++){
			char op;
			int v;
			cin>>op>>v;
			v--;
			if(op=='M'){
				marked[v]=min(marked[v],i+1);
			}else{
				bh[v].insert(i+1);
			}
		}

		ans=0;
		dfs(0);
		cout<<ans<<endl;
	}
	return 0;
}