what happens when you take the XOR of whole array with an element.
what to do in the case when the XOR of the array is zero.
Compute the XOR of the whole array, what happen when the XOR of the whole array is ZERO the ans is YES.
If the XOR is not zero then from which number we should XOR that it becames zero.
If XOR of the whole array is X if X^a[i]=0 then X=a[i] as removing the element for the array is same as XORing it again.
Then if X (XOR of the whole array) is present in the array then we can remove it and then make the XOR zero else the ans is NO.
675587B - The magical operation
If an element doesn't divide x now, will it ever ?
if it divides x now and get multiplied with y at max how many times it will get updated (Remember then the element will never exceed 1e18 limit).
what about the case in which y==1.
lets assume their is a magical x which is divisible by every number then how many times a single element will get updated if y>1 -- at max 60(if y=2 and $$$a[i]$$$ = 1then it can multiple 60 times till it becomes more then 1e18).
when y==1 then just skip the operation because it will not updated any number.
what about when y is not one and also and element is not updated because x%a[i]!=0.
If initially x%a[i]!=0 then it will never be divisible after any update.
lets assume two number X and Y if X%Y==0 then every factor of Y will also divides X,
but if a factor of Y doesn't divides X then will Y also not divide X.
If a is not a factor of X then a*b will also be not the factor of X.
For every distinct value $$$x$$$, we store all indices $$$i$$$ where $$$x$$$ is divisible by $$$a[i]$$$.
We remove an index $$$i$$$ from the set once $$$x$$$ is no longer divisible by $$$a[i]$$$.
The total complexity to remove indices across all operations is $$$O(n)$$$.
If $$$x$$$ is divisible by $$$a[i]$$$, the total complexity is $$$O(32 \cdot n)$$$ for $$$y \gt 1$$$ (since $$$x \le 2 \cdot 10^9$$$, an element can be divided at most 31 times).
If $$$y = 1$$$, we skip the operation entirely.Since the number of distinct values of $$$x$$$ is $$$\le 500$$$, this approach is efficient and stays within time limits.
675587C - Ashutosh and his dreams
think from the back.
now when you add x, can you know what value it will get converted to at the end.
The trick lies in processing the queries in reverse.Think about it: an element added at query $$$i$$$ is only affected by Type 2 operations that occur after query $$$i$$$. If we know what a value $$$v$$$ "eventually becomes" by the end of all queries, we can immediately determine the final value of any number we append.
Create an array (let's call it parent or f) where f[v] represents what the value $$$v$$$ currently transforms into. Initially, f[v] = v for all $$$v$$$ up to the maximum possible value ($$$3 \cdot 10^5$$$).
Start from the $$$q$$$-th query and work back to the 1st.
Process Type 2 ($$$2, x, y$$$): In reverse, this operation means that any $$$x$$$ existing before this point will eventually be treated as $$$x + y$$$.Update the mapping: f[x] = f[x + y].
Process Type 1 ($$$1, x$$$):When we "add" $$$x$$$ (in reverse, this is the moment the element was born), its final value is whatever f[x] is at that moment.Store f[x] in a result list.
Since we processed Type 1 queries from last to first, reverse your result list and print it.
think of invariance in single operation.
multiply the x and the y coordinate.
The problem asks us to find the initial active cell $$$(s, t)$$$ given the final state of a 2D grid after several "toggle" operations. Each operation affects four specific points. To solve this, we must find invariants — properties of the active cells that do not change when the operation is performed.
1. The Invariant for $$$s$$$
Let's consider the function $$$f(x, y) = x$$$. When we perform an operation at $$$(x, y)$$$, the XOR sum of the $$$x$$$-coordinates of the four toggled cells is:
Since every operation contributes $$$0$$$ to the total XOR sum, the XOR sum of $$$x$$$-coordinates of all active cells remains constant. Initially, only $$$(s, t)$$$ was active, so:
2. The Invariant for $$$t$$$
To find $$$t$$$, we use the function $$$g(x, y) = x \cdot y$$$. The four points toggled are:
- $$$P_1 = (x, y(x+1))$$$ which gives $$$g(P_1) = x \cdot y(x+1)$$$
- $$$P_2 = (x, (y+1)(x+1))$$$ which gives $$$g(P_2) = x \cdot (y+1)(x+1)$$$
- $$$P_3 = (x+1, yx)$$$ which gives $$$g(P_3) = (x+1) \cdot yx$$$
- $$$P_4 = (x+1, (y+1)x)$$$ which gives $$$g(P_4) = (x+1) \cdot (y+1)x$$$
Notice that $$$g(P_1) = g(P_3)$$$ and $$$g(P_2) = g(P_4)$$$. When we XOR these four values together:
This means the XOR sum of the products $$$(x_i \cdot y_i)$$$ is also an invariant!
3. Final Calculation
- Calculate the XOR sum of all $$$x_i$$$ to get $$$s$$$.
- Calculate the XOR sum of all $$$(x_i \cdot y_i)$$$ to get a value $$$V$$$.
- The initial $$$t$$$ is calculated as:
Complexity: $$$O(n)$$$ time and $$$O(1)$$$ extra space.
675587E1 - Holi Tree (Easy Version) 675587E2 - Holi Tree - (Hard version)
try to convert the tree in an array.
euler tour.
now you have a path in what range will the subtree of i lies?
Randomized Algorithm.
By performing a DFS Traversal, we can assign each vertex an entry time (tin) and exit time (tout).The subtree rooted at $$$i$$$ now corresponds to a contiguous range in the DFS-order array: [tin[i], tout[i]].The size of the subtree $$$sz$$$ is simply tout[i] — tin[i] + 1.
now the range of subtree of $$$i$$$ will lies in range pos[i] to pos[i]+subtree[i];
now we can apply a randomize search in this range .
if their exist a colour which is more then 1/3rd of the time if we randomly check the colour the probability that it will not be chosen is 2/3 .
The probability of not picking a vertex of this specific color in a single random check is at most $$$1 - 1/3 = 2/3$$$.
if we perform this operation 50 times then its very low that we may not choose a vertices whose colour appears more then 1/3 time
now for every query do 50+ times randomize approach then for the colour of random vertices see the freq. of that colour in the range which can be done by binary search.
you will store the tin tout time of the vertices for every colour then sort the vector .
then do
can you just remove the vertices for the current colour and put it in the new colour .
use ordered_set instead of vector .
read the solution for the easy version.
now for the 2nd type of query you can use ordered set instead of the vector and then can erase and insert the colour according to the update and to count the frequency use this ,
try to do it with Misra-Gries Algorithm.



