Rar the Cat((™)) is the founder of ACM (Association of Cats and Magical Lights). This is the story of the founding of ACM.
It is the second half of the year again and Rar the Cat((™)) is looking forward to Christmas. When people think of Christmas, Christmas trees will surely come into the picture. This year, Rar the Cat((™)) decides to decorate a Christmas tree with NN LED lights with up to 100 different colours. Each LED light can only have a single colour as Rar the Cat((™)) is too poor to afford more sophisticated models of LED lights.
Being a programmer himself, Rar the Cat((™)) LED lights are arranged in a tree formation on the Christmas tree. That being said, each LED light can be represented as a node and the wires between 22 LED lights can be represented as an edge. The whole formation consists of NN nodes fully connected by N−1N−1 edges. The nodes are labelled from 11 to NN and each node can hold colour denoted as 11 to 100100. It is noted that each LED is connected to every other LED directly or indirectly by wires and the tree formed is rooted at node 11.
Hubert the Cow finds that Rar the Cat((™)) Christmas tree is too boring because it lacks some magical colours. A magical colour in a subtree is defined as a colour whereby there is an odd number of LEDs with that particular colour within the subtree only.
In the tree to the right, the subtree of node 11has 33 white nodes and 44 black nodes. As such, white is a magical colour for the subtree of node 11. However, the subtree of node 33 has 11white node and 11 black node, thus the number of magical colour of the subtree of node 33 is 22. In addition, all leaf nodes of the tree have 11magical colour by definition.
In order to improve on his Christmas tree decorations, Rar the Cat((™)) intends to modify the colours of the LED lights on the tree. However, he does not know how to count how many magical colours there are.
Your task, is now to answer how many magical colours there are in a subtree requested by Rar the Cat((™)). This will be done in between Rar the Cat((™))modifications. There will be QQ queries and modifications in total.
Soon after this, Rar the Cat((™)) Christmas tree with these magical lights become so popular and Rar the Cat((™)) founded ACM to further spread the adoption of such trees worldwide.
Input
The first line will consists of 22 integers, NN and QQ. NN will be positive and not more than 300000300000 while QQ will be positive and not more than 10000001000000.
NN integers will follow on the next line. The ithith integer will be CiCi. CiCi denotes the colour of node ii and can only range from 11 to 100100 inclusive.
The next line will have N−1N−1 integers. The ithith integer will be Pi+1Pi+1, Pi+1Pi+1 denotes the parent node of node i+1i+1.
QQ lines will then follow with 22 integers each, KK followed by XX. If KK is 00, then this line represents a query for how many magical colours are there in the subtree of node XX. If KK is an integer between 11 and 100100, then it means that the colour of node XX is changed to colour KK.
Output
For each query when K=0K=0, output in one line: the number of magical colours in the subtree of XX (inclusive).
Sample Input 1 | Sample Output 1 |
---|---|
10 5 1 2 3 4 5 6 7 8 9 10 1 2 3 4 5 6 7 8 9 0 1 0 4 1 4 0 1 0 4 |
10 7 8 7 |
Sample Input 2 | Sample Output 2 |
---|---|
7 7 1 2 2 1 1 2 1 1 1 1 2 2 3 0 1 0 2 0 3 0 4 0 5 0 6 0 7 |
1 1 2 1 1 1 1 |
#include<bits/stdc++.h> using namespace std; typedef long long ll; const int maxn=300030; vector<int>v[maxn]; int Start[maxn],End[maxn],color[maxn],cnt,tree[maxn][110],n; void DFS(int x) { Start[x]=++cnt; for(int i=0;i<v[x].size();i++){ DFS(v[x][i]); } End[x]=cnt; } int lowbit(int x){return x&-x;} void update(int x,int col,int v){ for(int i=x;i<=n;i+=lowbit(i)){ tree[i][col]+=v; } } int sum(int x,int col) { int ans=0; for(int i=x;i>=1;i-=lowbit(i)){ ans+=tree[i][col]; } return ans; } int main() { ios::sync_with_stdio(0); int q; cin>>n>>q; for(int i=1;i<=n;i++)cin>>color[i]; for(int i=1;i<=n-1;i++){ int x; cin>>x; v[x].push_back(i+1); } DFS(1); for(int i=1;i<=n;i++){ update(Start[i],color[i],1); } while(q--){ int k,x; cin>>k>>x; if(k==0){ int ans=0; for(int i=1;i<=100;i++){ int num=sum(End[x],i)-sum(Start[x]-1,i); if(num%2==1) ans++; } cout<<ans<<endl; } else{ update(Start[x],color[x],-1); update(Start[x],k,1); color[x]=k; } } return 0; }