USACO Gold 2019 February - Cow Land
Author: Albert Ye
Time Complexity:
Main Idea: We use Euler tour technique to initialize a binary indexed tree, and use the binary indexed tree to run range XOR queries.
Check the official solution for another, more complicated, solution with HLD.
Let be the enjoyment values for each of the attractions, and be the lowest common ancestor between nodes and .
Euler Tour Technique
Let and be the in and out times for the tree, which we find through DFS with the Euler tour technique mentioned in this section.
Binary Indexed Tree
We create a binary indexed tree which supports range updates and XOR queries. Note that the inverse of XOR is XOR itself.
Store at indices and for all in the BIT. Note that the prefix XOR to is now the path XOR from the root node to each node at the tree. See the solution for Path Queries for a comprehensive explanation.
Let the prefix XOR at be .
The difference between the explanation for Path Queries and this problem is that we have range XOR queries instead of range sum queries.
This precomputation takes .
Responding to Queries
We now need to respond to path queries and updates. To update node , we remove the current values at and and replace these indices with the new value. For a path query, we just output (where represents the XOR operation). The and are the XOR queries for the paths from to and to , respectively. The path from to is counted twice and thus negates itself. Thus, we need to also XOR to account for the full path XOR from to .
LCA precomputation is known to take with sparse table. Additionally, updates and queries both take each, the complexity of the LCA and BIT queries. As there are updates and queries in total, the total complexity for the querying step is . Hence, the total complexity is .
Join the USACO Forum!
Stuck on a problem, or don't understand a module? Join the USACO Forum and get help from other competitive programmers!