Мо онлайн

Правка en1, от 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.

История

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