Preprocessing takes for DFS to compute sizes, depths, and parents. HLD decomposition takes . Building the segment tree takes . Total preprocessing: . Each path query walks chains and queries segment tree nodes per chain, giving per query.
Each update is . With queries, total time is , which handles easily within the second time limit.
Space complexity is for the data structures used.