Мо онлайн

Revision en1, by Noobish_Monk, 2025-07-17 02:18:54

Hello everyone! In contests, problems involving range queries appear periodically. Most problems provide queries offline, which allows for greater flexibility in solving them, but often they reduce to using a segment tree or Mo's algorithm. However, sometimes a problem that is easily solvable offline with Mo's algorithm becomes interesting in the online version. In such cases, some people try to maintain something like $$$500$$$ different states of Mo's algorithm—I never really understood what was meant by that.

Today, I managed to come up with an online version of Mo's algorithm that works in $$$O(n^{\frac{5}{3}})$$$ time and memory for standard problems. For asymptotic analysis, I'll assume $$$n = q$$$. I'm not the first to come up with this idea. As it turns out, here there was a discussion where this idea was proposed, but as in the previous blog, I want to explain it in more detail so that more people know about it.

In this blog, I want to break down how to solve a problem from a recent Educational Round using online Mo's algorithm.

2043G - Problem with Queries This problem has too much going on—update queries and even encoded inputs. Let's first try solving the offline version without update queries. I should clarify that instead of counting the number of indices $$$i \lt j$$$ where $$$a_i \neq a_j$$$, I'll count the opposite—the number of pairs where $$$a_i = a_j$$$. From this value, it's easy to derive the answer to the original problem, but counting pairs with equal values is slightly simpler to maintain.

The problem itself is a simple exercise on Mo's algorithm.

History

 
 
 
 
Revisions
 
 
  Rev. Lang. By When Δ Comment
ru26 Russian Noobish_Monk 2025-07-17 12:58:41 0 (опубликовано)
en15 English Noobish_Monk 2025-07-17 12:58:31 0 (published)
en14 English Noobish_Monk 2025-07-17 12:58:12 14 Tiny change: 'e remains unaffected. \n\nWit' -> 'e remains the same. \n\nWit'
en13 English Noobish_Monk 2025-07-17 12:56:17 55
en12 English Noobish_Monk 2025-07-17 12:55:39 17
ru25 Russian Noobish_Monk 2025-07-17 12:55:12 13
ru24 Russian Noobish_Monk 2025-07-17 12:53:22 134
en11 English Noobish_Monk 2025-07-17 12:53:18 1016
ru23 Russian Noobish_Monk 2025-07-17 12:26:33 242
en10 English Noobish_Monk 2025-07-17 12:00:00 0 (saved to drafts)
ru22 Russian Noobish_Monk 2025-07-17 11:59:41 597 (сохранено в черновиках)
en9 English Noobish_Monk 2025-07-17 02:34:42 16
ru21 Russian Noobish_Monk 2025-07-17 02:24:50 0 (опубликовано)
en8 English Noobish_Monk 2025-07-17 02:24:43 0 (published)
en7 English Noobish_Monk 2025-07-17 02:24:27 2
ru20 Russian Noobish_Monk 2025-07-17 02:24:22 4 Мелкая правка: 'о сделать del и add. Но нам в' -> 'о сделать *del* и *add*. Но нам в'
en6 English Noobish_Monk 2025-07-17 02:22:40 57
en5 English Noobish_Monk 2025-07-17 02:21:49 5
en4 English Noobish_Monk 2025-07-17 02:21:06 40
en3 English Noobish_Monk 2025-07-17 02:20:35 10
en2 English Noobish_Monk 2025-07-17 02:20:21 4812
en1 English Noobish_Monk 2025-07-17 02:18:54 1582 Initial revision for English translation (saved to drafts)
ru19 Russian Noobish_Monk 2025-07-17 02:16:04 125 Мелкая правка: ' Создадим \textit{состояние}, например' -> ' Создадим _состояние_, например'
ru18 Russian Noobish_Monk 2025-07-17 02:13:30 1 Мелкая правка: ' Создадим \textit{состояние}, например' -> ' Создадим _состояние_, например'
ru17 Russian Noobish_Monk 2025-07-17 02:13:02 11 Мелкая правка: ' Создадим \textit{состояние}, например' -> ' Создадим _состояние_, например'
ru16 Russian Noobish_Monk 2025-07-17 02:12:46 71
ru15 Russian Noobish_Monk 2025-07-17 02:11:35 35
ru14 Russian Noobish_Monk 2025-07-17 02:08:01 9 Мелкая правка: 'n[cut]\n\n\nPrerequisites: Мо и желательно 3D Мо\n\nВ этом' -> 'n[cut]\n\nВ этом'
ru13 Russian Noobish_Monk 2025-07-17 02:07:23 18 Мелкая правка: 'n[cut]\n\n\nPrerequisites: Мо и желательно 3D Мо\n\nВ этом' -> 'n[cut]\n\nВ этом'
ru12 Russian Noobish_Monk 2025-07-17 02:06:29 42 Мелкая правка: 'n[cut]\n\n\nPrerequisites: Мо и желательно 3D Мо\n\nВ этом' -> 'n[cut]\n\nВ этом'
ru11 Russian Noobish_Monk 2025-07-17 02:05:26 50
ru10 Russian Noobish_Monk 2025-07-17 02:05:01 2
ru9 Russian Noobish_Monk 2025-07-17 02:04:43 64
ru8 Russian Noobish_Monk 2025-07-17 02:03:25 23
ru7 Russian Noobish_Monk 2025-07-17 02:02:38 2500
ru6 Russian Noobish_Monk 2025-07-17 01:48:38 2209
ru5 Russian Noobish_Monk 2025-07-17 01:21:15 84
ru4 Russian Noobish_Monk 2025-07-16 22:48:03 93
ru3 Russian Noobish_Monk 2025-07-16 22:46:01 1061
ru2 Russian Noobish_Monk 2025-07-16 22:34:01 1016
ru1 Russian Noobish_Monk 2025-07-16 22:08:23 1025 Первая редакция (сохранено в черновиках)