ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

J - Association of Cats and Magical Lights Kattis - magicallights (树状数组+dfs序)

2019-06-11 23:45:25  阅读:335  来源: 互联网

标签:node Kattis int magicallights tree Magical Cat will Rar


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.

/problems/magicallights/file/statement/en/img-0001.png

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 1Sample 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 2Sample 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;
}

 

标签:node,Kattis,int,magicallights,tree,Magical,Cat,will,Rar
来源: https://www.cnblogs.com/cherish-lin/p/11006957.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有