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=;
vector<int>v[maxn];
int Start[maxn],End[maxn],color[maxn],cnt,tree[maxn][],n;
void DFS(int x)
{
Start[x]=++cnt;
for(int i=;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=;
for(int i=x;i>=;i-=lowbit(i)){
ans+=tree[i][col];
}
return ans;
} int main()
{
ios::sync_with_stdio();
int q;
cin>>n>>q;
for(int i=;i<=n;i++)cin>>color[i];
for(int i=;i<=n-;i++){
int x;
cin>>x;
v[x].push_back(i+);
}
DFS();
for(int i=;i<=n;i++){
update(Start[i],color[i],);
}
while(q--){
int k,x;
cin>>k>>x;
if(k==){
int ans=;
for(int i=;i<=;i++){
int num=sum(End[x],i)-sum(Start[x]-,i);
if(num%==)
ans++;
}
cout<<ans<<endl;
}
else{
update(Start[x],color[x],-);
update(Start[x],k,);
color[x]=k;
}
}
return ;
}

J - Association of Cats and Magical Lights Kattis - magicallights (树状数组+dfs序)的更多相关文章

  1. 2018牛客网暑期ACM多校训练营(第二场)J Farm(树状数组)

    题意 n*m的农场有若干种不同种类作物,如果作物接受了不同种类的肥料就会枯萎.现在进行t次施肥,每次对一个矩形区域施某种类的肥料.问最后枯萎的作物是多少. 分析 作者:xseventh链接:https ...

  2. 2018牛客网暑期ACM多校训练营(第二场) J - farm - [随机数哈希+二维树状数组]

    题目链接:https://www.nowcoder.com/acm/contest/140/J 时间限制:C/C++ 4秒,其他语言8秒 空间限制:C/C++ 262144K,其他语言524288K ...

  3. 牛客网多校训练第一场 J - Different Integers(树状数组 + 问题转换)

    链接: https://www.nowcoder.com/acm/contest/139/J 题意: 给出n个整数的序列a(1≤ai≤n)和q个询问(1≤n,q≤1e5),每个询问包含两个整数L和R( ...

  4. 牛客网 暑期ACM多校训练营(第二场)J.farm-STL(vector)+二维树状数组区间更新、单点查询 or 大暴力?

    开心.jpg J.farm 先解释一下题意,题意就是一个n*m的矩形区域,每个点代表一个植物,然后不同的植物对应不同的适合的肥料k,如果植物被撒上不适合的肥料就会死掉.然后题目将每个点适合的肥料种类( ...

  5. 牛客网 暑期ACM多校训练营(第一场)J.Different Integers-区间两侧不同数字的个数-离线树状数组 or 可持久化线段树(主席树)

    J.Different Integers 题意就是给你l,r,问你在区间两侧的[1,l]和[r,n]中,不同数的个数. 两种思路: 1.将数组长度扩大两倍,for(int i=n+1;i<=2* ...

  6. 牛客多校第3场 J 思维+树状数组+二分

    牛客多校第3场 J 思维+树状数组+二分 传送门:https://ac.nowcoder.com/acm/contest/883/J 题意: 给你q个询问,和一个队列容量f 询问有两种操作: 0.访问 ...

  7. 2017年中国大学生程序设计竞赛-中南地区赛暨第八届湘潭市大学生计算机程序设计大赛题解&源码(A.高斯消元,D,模拟,E,前缀和,F,LCS,H,Prim算法,I,胡搞,J,树状数组)

    A------------------------------------------------------------------------------------ 题目链接:http://20 ...

  8. 2015 北京网络赛 C Protecting Homeless Cats hihoCoder 1229 树状数组

    题意:求在平面上 任意两点连线,原点到这个点的距离小于d的点对有多少个,n=200000; 解: 以原点为圆心做一个半径为d的圆,我们知道圆内的点和园内以外的点的连线都是小于d的还有,圆内和园内的点联 ...

  9. 牛客网暑期ACM多校训练营(第二场)J farm (二维树状数组)

    题目链接: https://www.nowcoder.com/acm/contest/140/J 思路: 都写在代码注释里了,非常好懂.. for_each函数可以去看一下,遍历起vector数组比较 ...

随机推荐

  1. jquery关于Select元素的操作

    jQuery获取Select元素,并选择的Text和Value: $("#select_id").change(function(){//code...});           ...

  2. POJ - 3660 Cow Contest(flod)

    题意:有N头牛,M个关系,每个关系A B表示编号为A的牛比编号为B的牛强,问若想将N头牛按能力排名,有多少头牛的名次是确定的. 分析: 1.a[u][v]=1表示牛u比牛v强,flod扫一遍,可以将所 ...

  3. Spring中@Value("${}"))取不到值的几种情况

    https://blog.csdn.net/dh12313012/article/details/84661169 1. spring组件重写构造方法,在构造方法中引用@Value为null 由于sp ...

  4. 吴裕雄--天生自然MySQL学习笔记:MySQL 查询数据

    MySQL 数据库使用SQL SELECT语句来查询数据. 可以通过 mysql> 命令提示窗口中在数据库中查询数据,或者通过PHP脚本来查询数据. 语法 以下为在MySQL数据库中查询数据通用 ...

  5. 【mui webAPP】HBuilder创建的iOS平台设置沉浸式状态栏

    应用占用全屏区域,而系统状态栏需要预留出系统状态栏高度. HBuilder创建的应用默认不使用沉浸式状态栏样式,需要进行如下配置开启:打开应用的manifest.json文件,切换到代码视图,在plu ...

  6. Go-方法-接口-异常处理-错误处理

    方法 什么是方法? 方法其实就是一个函数,在 func 这个关键字和方法名中间加入了一个特殊的接收器类型.接收器可以是结构体类型或者是非结构体类型.接收器是可以在方法的内部访问的. package m ...

  7. Spring学习之Aspectj开发实现AOP

    Aspectj是一个基于Java语言的Aop框架,它提供了强大的Aop功能. Aspectj简介: 1.Aspectj是一个面向切面的框架,它扩展了Java语言,它定义了一个Aop语法. 2.所以它有 ...

  8. mysql慢SQL排查之show processlist和show full processlist

    mysql排查线上数据库问题,经常会用到 show processlist和show full processlist这两条命令 processlist命令的输出结果显示了有哪些线程在运行,不仅可以查 ...

  9. HNOI2018/AHOI2018 游戏

    这题放过了暴力其实就没啥意思了 虽然暴力复杂度很玄学,但是思维水平确实没啥 Description link 题意概述:现在有一条长度为 \(n\) 的链,有些边是有限制的 限制为能到某个点,才能经过 ...

  10. ZJNU 1528 - War--高级

    类似于1213取水 可以把空投当作第0个城市 最后将0~n的所有城市跑最小生成树 /* Written By StelaYuri */ #include<iostream> #includ ...