YS - Vertex Set Path Composite
Author: Andrew Cheng
Time Complexity: from HLD
Main Idea: Different from a classic HLD problem, the nesting of functions does not follow the commutative property. In other words, the answer of the query is different from the answer of the query .
To solve this problem, we can break down the path from node to node into two paths: from node to and from to node . The path from node to will decrease in depth, and the path from to node will increase in depth.
We can then maintain two segment trees.
The first segment tree will calculate the equivalent function that results from moving from node to . And the second segment tree will calculate the equivalent function that results from moving from to node . After this we can plug in the given into the two functions to get the answer of the query.
It should be mentioned that the function at node should only be calculated once. To avoid calculating the node twice, we can intentionally avoid during exactly one of the two path queries.
The update queries can be implemented using segment tree point updates on both segment trees.
C++
#include <bits/stdc++.h>#define fi first#define se second#define pb push_backusing namespace std;using ll = long long;using pi = pair<ll, ll>;const int maxN = 2e5 + 5;const int height = 25;
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!