Unexpected TLE with Segment tree
My solution to 1919F1 - Wine Factory (Easy Version) involved a few observations
- Since the capacity of pipes are infinite in the easy version, each tank will transfer all remaining water to the next tank, and we can imagine the last tank dumping water outside
- Now suppose we can also add water in from the left. How will the water that is dumped out from the right change?
- I realized that there is a base amount that will always be dumped. As I starting adding water from the left, there will be initially no change, but after a certain cutoff, the the amount of water dumped will increase.
- So a system of water tanks can be characterized by two values — base and cutoff
- Also two systems of water tanks can be composed. We can derive the base and cutoff of the larger system.
- Using a segment tree, we can exploit this to make updates fast
- The total amount of water and wine is conserved.
The logic seems right enough, since it ran till test 10, but encountered a TLE there. However the time complexity is O(n logn) for building the segment tree and O(q logn) for answering the queries. The constant factors also don't seem very large. I made some optimizations (replaced pair <int, int> with array) and tried again without success. What is the cause of this?
Links to my attempts