There is an undirected connected tree with n nodes labeled from 1 to
n and n - 1 edges. You are given the integer n. The parent node of a node with a label v is the node with the label floor (v / 2). The root of the tree is the node with the label 1.
For example, if n = 7, then the node with the label 3 has the node with the label floor(3 / 2) = 1 as its parent, and the node with the label 7 has the node with the label floor(7 / 2) = 3 as its parent.
You are also given an integer array queries. Initially, every node has a value 0 on it. For each query queries[i], you should flip all values in the subtree of the node with the label queries[i].
Return the total number of nodes with the value1after processing all the queries.
Note that:
Flipping the value of a node means that the node with the value 0 becomes 1 and vice versa.
floor(x) is equivalent to rounding x down to the nearest integer.

Input: n =5, queries =[1,2,5]Output: 3Explanation: The diagram above shows the tree structure and its status after performing the queries. The blue node represents the value 0, and the red node represents the value 1.After processing the queries, there are three red nodes(nodes with value 1):1,3, and 5.
Example 2:
1
2
3
4
5

Input: n =3, queries =[2,3,3]Output: 1Explanation: The diagram above shows the tree structure and its status after performing the queries. The blue node represents the value 0, and the red node represents the value 1.After processing the queries, there are one red node(node with value 1):2.
Each query flips all values in the subtree of a node. The tree is a binary heap, so the subtree of node v is all nodes with labels in [v, r], where r is the largest label in v’s subtree. We can use a difference array to efficiently process range flips, then count the number of nodes with odd flips.
For each query, compute the range [v, r] for the subtree rooted at v. For a binary heap, r = min(n, v * 2^{h} - 1), where h is the height such that v * 2^{h} <= n < v * 2^{h+1}.
Use a difference array to mark flips: diff[v] += 1, diff[r+1] -= 1.
After all queries, compute prefix sums and count nodes with odd flips.
#include<vector>#include<cmath>usingnamespace std;
classSolution {
public:int numberOfNodes(int n, vector<int>& queries) {
vector<int> diff(n+2, 0);
for (int v : queries) {
int r = v;
while (r*2<= n) r = r*2;
while (r*2+1<= n) r = r*2+1;
diff[v]++;
if (r+1<= n) diff[r+1]--;
}
int ans =0, cur =0;
for (int i =1; i <= n; ++i) {
cur += diff[i];
if (cur %2==1) ++ans;
}
return ans;
}
};
classSolution {
publicintnumberOfNodes(int n, int[] queries) {
int[] diff =newint[n+2];
for (int v : queries) {
int r = v;
while (r*2 <= n) r = r*2;
while (r*2+1 <= n) r = r*2+1;
diff[v]++;
if (r+1 <= n) diff[r+1]--;
}
int ans = 0, cur = 0;
for (int i = 1; i <= n; ++i) {
cur += diff[i];
if (cur % 2 == 1) ans++;
}
return ans;
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
classSolution {
funnumberOfNodes(n: Int, queries: IntArray): Int {
val diff = IntArray(n+2)
for (v in queries) {
var r = v
while (r*2<= n) r *=2while (r*2+1<= n) r = r*2+1 diff[v]++if (r+1<= n) diff[r+1]-- }
var ans = 0; var cur = 0for (i in1..n) {
cur += diff[i]
if (cur % 2==1) ans++ }
return ans
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
classSolution:
defnumberOfNodes(self, n: int, queries: list[int]) -> int:
diff = [0]*(n+2)
for v in queries:
r = v
while r*2<= n: r = r*2while r*2+1<= n: r = r*2+1 diff[v] +=1if r+1<= n: diff[r+1] -=1 ans = cur =0for i in range(1, n+1):
cur += diff[i]
if cur %2==1: ans +=1return ans
impl Solution {
pubfnnumber_of_nodes(n: i32, queries: Vec<i32>) -> i32 {
let n = n asusize;
letmut diff =vec![0; n+2];
for&v in&queries {
letmut r = v asusize;
while r*2<= n { r *=2; }
while r*2+1<= n { r = r*2+1; }
diff[v asusize] +=1;
if r+1<= n { diff[r+1] -=1; }
}
letmut ans =0;
letmut cur =0;
for i in1..=n {
cur += diff[i];
if cur %2==1 { ans +=1; }
}
ans
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
classSolution {
numberOfNodes(n: number, queries: number[]):number {
constdiff= Array(n+2).fill(0);
for (letvofqueries) {
letr=v;
while (r*2<=n) r=r*2;
while (r*2+1<=n) r=r*2+1;
diff[v]++;
if (r+1<=n) diff[r+1]--;
}
letans=0, cur=0;
for (leti=1; i<=n; ++i) {
cur+=diff[i];
if (cur%2===1) ans++;
}
returnans;
}
}