How to use segment tree?
Build tree and query
You can read this aritical to learn some basic operations of segment tree.
Update and "Lazy Tag"
Lazy Tag
"Lazy Tag" is the most important part in updation. This method can reduce the time complexity to $$$O(\log(n))$$$ for each updation. It's obvious that we can't update the whole tree, or the time complexity will be $$$O(n)$$$ like build tree. To reduce the time complexity, if a segment is not queried now, we don't have to update it at present. We usually give it a tag to record how we update it. For example, if we want to update an array and query the sum of an interval, for each "add" operation, we do not add numbers to each segment, we only have to give a "add tag" to the some segments.
For example, if we want to add $$$2$$$ from $$$2$$$ to $$$5$$$. We give segment number $$$1$$$ and segment number $$$2$$$ an "add tag", to note we should add it up when we query the segment.
Note that segment number $$$1$$$ and number $$$2$$$ contain the
void apply_add_tag(int o, int l, int r, ll v)
{
sum[o] += (ll)(r - l + 1) * v;
add[o] += v;
}
Push Down
When we query the segment which has a tag