First of all, this technique isn't anything new, it's a fairly known one, but I couldn't find any resources on this topic other than this comment which is somewhat hard to find, so I decided to write a full blog.
With that being said, let's begin with the blog.
The problem
Lets consider the following problem: A graph is given with $$$N$$$ nodes and $$$Q$$$ queries. Each query is one of the following:
- Add edge $$$(u,v)$$$
- Remove edge $$$(u,v)$$$
- Query the number of connected components in the graph
The timeline
Lets imagine a timeline, from time $$$1$$$ to time $$$Q$$$, at each moment either some edge gets added, removed, or a query gets asked.
Now the main idea of this technique is instead of dealing with deletions, for each edge we determine some intervals during which it exists. Namely if edge $$$(u, v)$$$ gets added at time $$$A$$$ and then later removed at time $$$B$$$ then that edge will obviously exist in the time frame $$$[A, B)$$$.
The Segment Tree on time
Now we've turned the problem into a range update problem, which suggests using a segment tree. We build a segment tree over the timeline. For each node, we store a list of edges that are active throughout the segment represented by that node.
Each interval gets decomposed into $$$O( \log Q)$$$ segments in the tree, and for each of those segments, we simply add the corresponding edge to the node.
After building the tree, we will run a DFS on it while maintaining a DSU with rollbacks (this is a well-known structure that can handle operations in $$$O(\log N)$$$).
At each node, we add all the edges in the node to the DSU, recurse downwards, and then rollback.
At the leaves, if the time corresponds to a query we can easily answer it since our DSU is already built.
This way we will handle everything offline in $$$O(Q \log Q \log N)$$$.
How is this useful?
You may be wondering by this point how this is useful at all since Dynamic Connectivity can be solved both online and offline in a better time complexity.It turns out this technique can be generalized.
Notice that the only part specific to the problem that we used in our solution is the Rollback DSU. So this technique can be applied in any problem with addition and deletion of pretty much anything to any structure. The only condition is that we need to be able to handle addition, and most importantly, rollbacks in a good time complexity.
In return for writing this amazing blog I am asking you to link any problems that can be solved with this technique in the comments.
Big thanks to EntityPlantt for using his grammar police abilities for once to not ragebait me and instead help me fix my terrible grammar.








Auto comment: topic has been updated by VladiG (previous revision, new revision, compare).
As a grammar policeman, I am impressed by my fixing
?
Oh well, it turns out I am incapable of using the Internet.
You should probably just use a frontier model for web-search instead of a search engine at this point. I've found a lot of obscure educational stuff in this way that I couldn't have found on my own (or would have taken a lot of time to find).
I have solved this https://mirror.codeforces.com/contest/981/problem/E question with same technique. I suggest everyone to give it a try.
My solution: https://mirror.codeforces.com/contest/981/submission/272892239
OMG! Finally an educative, amazing, beautiful, excellent and fabulous blof on this platform!
There is this blog
Hi, great blog! Can you please recommend some cool hard problems that can be solved with this tech?
I've been truly blessed to find this blog! The idea of doing things on time instead of arrays and similar extends my smile to the ears. To thank you for reminding me of my favourite topic, I will publish some links of problems using this incredible solution.
1681F - Unique Occurrences
981E - Addition on Segments
601E - A Museum Robbery
1140F - Extending Set of Points
2104G - Modulo 3
I hope this brings you the maximum amount of happiness, for this blog did that to me!
https://mirror.codeforces.com/contest/2069/problem/F
https://mirror.codeforces.com/contest/1814/problem/F
https://cses.fi/problemset/task/2133 :)
How? To my knowledge, this is only possible under the additional constraint that the graph is always a forest. In the general case, the complexity is only $$$O(\log^2)$$$ per query, and you need a additional sophisticated algorithm on top of Link-Cut tree.
In the offline case, you can use a link-cut tree with edge weights and path min queries. And you can make the edge weights equal to the time of deletion. In this way you can easily keep some spanning tree of each component such that the spanning tree uses the edges which are deleted last.
But online dynamic connectivity with just link-cut tree indeed seems hard.
Turns out I overestimated what Link Cut Trees can do, I edited the blog.
Auto comment: topic has been updated by VladiG (previous revision, new revision, compare).