Given 3 types of queries
- Insert element 'x' into multiset
- Delete element 'x' from multiset
- Find xor of all elements present in multiset which are less than 'k' (k is not fixed)
I required above solution in this problem. But I was unable to solve it this way. How to solve this?
Well i solved it during the contest. well you need to be versed with few concepts: 1. Tree linearisation- doing dfs to get in and out time and get a linear tree. 2. Offlining the queries. 3. Sorting queries based on 'k' 4. Building segment tree on the linearised tree.
My code is enough moduler to understand what every function does. In my segment tree i just make the values 0 fore those edges that are no longer required after sorting and using sorted k.
There are Persistent tree approaches also available.
I solved it offline too. Need an online approach. Thanks.
For online, you will definitely need Persistent tree i guess, or something similar. Well these two problems are more or less comparable to offline and online. KQUERY and KQUERYO on spoj... check them out. Well if you don't want to use Persistent tree then see the solution of KQUERYO.
The Solution uses the Mergesort tree approach on segment tree storing the whole sorted elements of the segment tree at each node for its range. Querying one node takes O(Logn) time. Hope this helps.
for each node v store the Xor of numbers lie on path between v and 2^i parent of v
for each query find the LCA(u, v), then find Xor path(u, LCA) let it's X, path(v, LCA) let it's Y, in log(n)
so the answer is (X xor Y)
k is not fixed and we don't need to find lca because xoring path from u to root and v to root will cancel out path from lca to root.
Theres a K factor in query too... How would you inculcate that in this sparse table approach?
You can mantain all the values in a binary balanced tree(BBT) [such as treap(aka. cartesian tree)]. Operations 1. and 2. are natural of BBT. For operation 3. you can store on each node of the tree the xor of all elements on the subtree, and to compute the xor of all elements less than a k you can perform binary search on the tree [notice that I mean traverse the tree which is almost the same as doing binary search] or a simpler [probably slower option] split the tree into two trees, all elements less than k in one tree A and the remaining elements in the other tree B, then the value you are looking for is the xor stored on the root of the tree A. Overall complexity is log(n) per query.
Note: Since the query operation is xor and a^a = 0 both operations 1. and 2. are the same operation: Change the parity of the frequency of the element x in the multiset. This is not too relevant though.
If x, k is in friendly range, you can just use Binary Indexed Tree.
1. Insert x:
for(int i = x; i <= maxX; i += i & -i) BIT[i] ^= x
2. Delete x: Same as Insert. As XOR-ing again is same as removing it.
3. Query k:
for(int i = k-1; i > 0; i -= i & -i) ans ^= BIT[i];
In the problem you linked, You can compress all x and k first, so they will be in range [1, n + q]. Then you can do
update(compressed[x], x)
andquery(compressed[k])
.In the problem XOR of path (u, v) is actually same as XOR of path (root, u) and (root, v). XOR of path (root, lca(u, v)) will be cancelled by XOR-ing twice.
Here is my submission — 14527601
Use something like a skiplist... As n,m<=10^5,a list like this can be also OK: ~~~~~ int prefix_xor[317]; struct node{ int data; list *next,*prev,*next317; } ~~~~~
Online approach using persistent segment tree suggested by Fazle.
This solution is for arrays and can be extended to your problem.
1. Make a sorted array of unique array elements.
2. Initialize segment tree to be all zeroes. This version of the segment tree is S0.
3. Take the first element of the sorted array. Let it be x. Update the segment tree S0 with value x at all indices where the array has value x. This version of segment is S1.
4. Similarly, form segment tree Si from Si - 1.
5. For query k, find the index i of sorted array element that is just smaller than k. Query on Si.