Possibly, someone called "MO" once used the trick in a local contest, and the name stuck and later got popular. I have never encountered the name "Mo's algorithm" outside of competitive programming.
The technique, or the term "Mo's Algorithm" ("莫隊算法" in Chinese) was originally thought of and popularized by 莫涛 (Mo Tao) and his teammates. It was first used to tackle a problem from 2009 China IOI Camp: 小Z的袜子(hose) — authored by 莫涛 himself. The problem was: given a sequence of integers, for any given interval, calculate the probability of choosing any 2 numbers and the 2 numbers are equal.
Now let's start. I have here compiled the tutorial, some articles written by me, and problems on Mo's algorithm.
Tutorials:
- Sqrt Decomposition
- Introduction to Mo's algorithm — By anudeep2011
- Introduction to Mo's algorithm(Video tutorial) — By kazama460
- MO's algorithm on Tree — By animeshf
Basic problems:
I have written some basic articles on Mo's algorithm which has been published on Geeksforgeeks.
- Queries for elements having values within the range A to B
- Queries for Count of divisors of the product of an Array in given range
- Number of elements less than or equal to a number in a subarray
- Count of odd and even parity elements in subarray using Mo’s algorithm
List of problems:
- DQUERY — SPOJ
- Count on a tree II — SPOJ
- Zero Query — SPOJ
- Powerful array — Codeforces
- Tree and Queries — Codeforces
- Jeff and Removing Periods — Codeforces
- XOR and Favorite Number
- Factor Tree — Codechef
- So Close Yet So Far — Codechef
- Chef and Graph Queries — Codechef
- Tree difference — Codechef
- Sherlock and Inversions — Codechef
- Estimating progress — Codechef
- Anup at Amrithapuri — Codechef
- Chefina and Beautiful Pairs — Codechef
- Traffic Count — Codechef
- Chef and cities — Codechef
- Kriti and her Birthday Gift — Hackerearth
- Sherlock and Inversions — Hackerearth
Edit:
Thanks for the upvotes! It means a lot to me








great research saurabhyadavz.
There must be a bookmark option to save such blogs .
Well said. These awesome blogs must be bookmarked.
Just as awesome as your editorials xD
There is a bookmark option. You can click star near upvote and downvote buttons and this blog will be added to your favourite blogs. You can watch them in your profile or using this link.
Mo is someone's name, writing "Mo's algorithm" makes more sense, no capital O. Where did that come from anyway?
Thanks for pointing out though. I think it comes from China.
The algorithm of course comes from China ;), but somehow I doubt the capital-O spelling comes from there.
It’s MO’s in order to capitalize on the big O runtime.
I have downvoted you for this.
I remember a time when I was stuck in a question in Codechef Long Challenge. This blog is really gonna help all.
Yes it was Factor Tree. Really challenging problem and one of the best Long Challenge I ever faced.
Hey dude, nice blog! Thanks a lot.
Just wondering what made you think the part you have written after edit!
For MO's Algo on trees, how to find the LCA of two nodes??
The page https://blog.anudeep2011.com/mos-algorithm/ doesn't load for me. Does this work for someone else?
Facing the same issue
One more problem based on Mo's Algorithm 221D - Little Elephant and Array
need an idea on how we can use this to find the distance between the first occurrence and last occurrence of any element in a given range for multiple queries? Any help appreciated!
Let's denote the given array as a. Solve for each value in the array separately. You can maintain an array of vectors ind[x] = {i | a[i] = x}. You could've solved it using binary search on ind, but you can also solve it using Mo's algorithm.
Oh this is interesting, i will try it.
Thank you!
With Mo you just keep an array of deques representing indices of the same value. Then when incrementing L you push_front, when incrementing R you push_back, when decrementing L you pop_front and when decrementing R you pop_back.
Will try this out, Thank you
I've never encountered Mo's algorithm outside of "We tried to cut solutions using Mo's algorithm, but unfortunately some ended up passing."
Great effort. It is nice to learn about this. I never knew this concept had a name or could be useful anywhere, but sometimes it works. Thank you for the blog and the work.
For anyone facing issues with anudeep2011’s blog not opening, you can try this workaround. It should work: Here you go!!
exactly, in Chinese mo's algorithm means "莫队算法" not "莫隊算法"
Never mind... Just a matter of two different fonts.