Mo learning notes team


For reprint, please bring this blog address:
And indicate the original author: @ blog Park: continue_ 1025, creation is not easy, please understand.

Ordinary Mo team

Introduce a small example

\(zl\)My sister has a string of numbers. Because students are too bald, now he wants to ask you\(m(m≤1e5)\)Times, of which\(L\)reach\(R\)Number of interval occurrences\(3\)How many times and above?


Segment tree

Inefficient and difficult to maintain.

\(p.s.\)\(lmpp\)Giant said if\(3\)If you can wait for one time, the line segment tree is more efficient. The giants can try it by themselves. The dishes are not demonstrated here)

So introduceMo team——An off-line algorithm for interval problems

0. Origin of algorithm name

Mo team algorithm, in which “Mo” refers to the national team Mo Tao giant, ccccorz.

1. Basic principles

Mo team isBeautiful violence

Let’s go back to the beginning and help\(zl\)Sister.

\(Continue\)He was a fool, so he made a violent attack;

for(int i=l;i<=r;i++)

If you call like this every time you ask, obviously,\(O(nm)\)Our algorithm is to make\(zl\)My sister is embarrassed. (\(zl\)Sister: are you serious?

Smart you picked up the fool\(Continue\)It’s a pity to throw away the violence.

So you begin to transform the violent result just now.

You think, now that we know\([L,R]\)As a result, then\([L-1,R]\)\([L+1,R]\)\([L,R-1]\)\([L,R+1]\)Can’t the results be easily obtained together?

stay\(O(1)\)In your time, now you have it in your hand\(5\)Two answers.

This is a good thing, so you extend this property to all inquiries.

In detail, for convenience, we might as well refer to the four answers obtained from the promotion asExtension interval, the original interval corresponding to the interval will be extended\([L,R]\)callOriginal interval。 As long as we knowOriginal intervalThe answer, then the requestExtension intervalThen you can ask.

So now the question is:“How to make the inquiry interval a promotion interval”。 Further, since we were unable to change the inquiry, the question became“How to make the extended interval match the “query interval”

Obviously, we can passConstantly modify the original intervalThe way, come onmatchingConsistent with the inquiry intervalExtension interval

Obviously, thisChanging rangeWe can operate through\(while\)Loop implementation.

But if every time\(while\), our code is still a fool’s code——meeting\(T\)It was terrible\(g2020\) \(lvt\) \(&&\) \(dlz\)(big man)

So the next step is the Mo team in real application:Block+\(sort\)

2. Basic Mo team

With some of the above inferences, now you realize that every time you askAdjust the size of the original interval according to the size of the query interval, and since the query interval is not the same (otherwise the question will be meaningless), soThis operation is inevitable

Under certain circumstances, we should make the operation as fast as possible, so as to achieve beautiful violence.

By analyzing the above process again, we find that the main time consumption of this operation comes fromThe process of locking the required intervalTherefore, we should try our bestReduce the difference between the generalization intervals required each time, in order toThe number of times of changing the range is reduced and the efficiency is improved.

The only way to achieve this is to sort the query intervals

This is the name of the beautiful Mo team\(sort\)part

As for the standard of ranking, it naturally depends onBlockCome on~

Since we require the two intervals to be similar as much as possible, we should meet the monotonicity, otherwise it will waste time.

As shown in the figure.



Note: the purple curve represents the length to be moved each time the section is locked.

Figure 1 shows the unsorted effect. We can see that the shadow part is moved repeatedly, which is a waste of time.

As long as it is sorted into Figure 2, the intervals to be moved will no longer overlap~

If the sorting task is determined, what about the sorting keyword?

The answer isBlock

The nodes are reasonably divided into different intervals by blocks, so that they can be compared quickly.

WeSort by the block where the left endpoint is located. If it is in the same block, compare the right endpoint。 In this way, we can scientifically and effectively reduce the time~

3. Code

I understand all the above. Next is a template code of ordinary Mo team. General questions can be changed into patterns. (of course, it can’t be regarded as the team with power and the team on the tree)

\(Problem\):HH Necklace

This question\(luogu\)It is the card of Mo team (but there is still a fairy giant card). The positive solution is a tree array.So it’s just a practice question here。 A6 points and T4 points are almost the same. Mainly thought, thought!


using namespace std;

const int maxn=2e6+10;
const int inf=1<<30;

inline int Read()
	int s=0;
	int f=1;
	char ch=getchar();
	return s*f;

int n,m;
struct Num
	int l,r; // Query interval
	int num; // Answers to questions
	int id; // Order of inquiry

int temp[maxn];

int bel[maxn]; //belong

bool cmp(Num x,Num y)
	return ((bel[x.l]==bel[y.l]) && (x.ry)
	int l=1,r=0;
	for(int i=1;i<=m;i++)
		int x=a[i].l;
		int y=a[i].r;
                //Process of locking interval

	sort(a+1,a+m+1,cmp2); // The offline algorithm outputs the answers in the original order
	for(int i=1;i<=m;i++)
	return 0;

stay\(zl\)Inspired by my sister’s grateful eyes, go further!

Power Mo team

Introduce a small example

\(zl\)My sister has a string of numbers. Because he is so cute, now I add an operation on the basis of the original: put the second\(k\)Number becomes\(num\)


Since the query operation is still reserved for the current problem, we still consider using Mo team to solve it.

Due to the operation of modifying the value, we need Mo team to store the data. Therefore, we introduce a new structure:Power Mo team

1. Basic principles

The basic principle of the weighted Mo team is the same as that of the ordinary Mo team.

A violent fool\(Continue\)I think so:
As long as the modification related to the current interval is entered every time, it will be modified immediately

Obviously, this will\(TLE\), because the interval may be uncertain.

It’s a lot like the problem we had when we first dealt with the basic Mo team. So this time, we use the same\(sort\)To solve it.

We add a keyword\(tim\) \((time)\), which recordsThe sequence number of the modification operation affected by the current query\(sort\)The time will be\(tim\)As the third keywordIn this way, it can not only ensure the correctness of the basic team’s handling of inquiry operations, but also deal with modification operations in time. Because the complexity of modification operation is low, it will not be changed\(TLE\)Yes.

The modification operation creates a structure separately,\(perfect\)

3. Code

\(Problem\)Number color

There is a giant in the solution of this problem. He used the idea of transformation when modifying the operation. My code is much worse, soPortalJust put it here. You can go and see it.

This problem is still a Kamo team, so if the number of blocks is set to a constant, there will be no card.

Many bigwigs are divided into blocks\(k\)Set to(ah, yes, this picture is copied), because it’s the fastest. Its specific proof stampThe big man’s solution, the food is better than me. I can’t come and understand it(



thank\(zl\)My little sister unconsciously provided me with spiritual support.

Thank myself for being a big dish.