Team Mo algorithm (offline)

What algorithm Mo team

Mo Mo Tao algorithm is a team captain invention, is a respect for him, so that this algorithm is called Mo team.


Scope

Algorithm for offline processing sequence of operations for a wide range, typically with a root of complexity.


Mo algorithms team thinking

Suppose the subject does not involve modification operations.

All offline operations, all operations sorted tuples, the first dimension is a left end point where the block number, the second dimension is the right point.

After sorting, the order of processing interrogation, maintaining a deque, while maintaining the answer within a queue interval, each from $ [L, R] $ answer extended to $ [L-1, R] $ $ [L, R-1] $ $ [L + 1, R] $ $ [L, R + 1] $ answer finally obtained when this answer interrogation interval.


Time complexity analysis

N-sequence length of $ $, $ m $ query number, length divided Kuaikuai $ D $, then the left end point at the same block, moving left point of not more than $ D $, does not move more than the right end in total $ n-$, clearly the $ \ Theta (m \ times d + \ frac {n ^ 2} {d}) $, so $ d = \ sqrt {n} $, $ n, m $ the same order, the time complexity to $ \ Theta (n \ sqrt {n}) $, Changshu great.


Mo nature of the algorithm

Mo algorithm is the essence of team, in the case of off-line, by adjusting the order of between inquiry and modify, extend from the known to the unknown answer answers to outstanding order of calculation in exchange for good time complexity.
Each interrogation $ [L, R] $ can be regarded as the whole point of $ (L, R) $ plane, and the other interrogation $ (l, r) is the Manhattan distance between $ transfer between two interrogation cost.
We have to do is adjust the order, minimizing the cost between inquiry and transfer.
The point is the optimal calculation procedure Manhattan minimum spanning tree, but to solve this thing is very troublesome, so we take this method is more direct sort of violence, to obtain balance and time complexity of code complexity.


When Mo algorithm team

When the operation is more complex requirements of the subject, it is more restrictive than with other segment tree data structure does not readily maintenance information, or the higher the complexity of information to maintain, and subject to less stringent requirements of time when offline, consider using Mo team.
Time complexity usually with root, with higher Mo modify teams complexity, constant and large, if you can directly block segment tree or get rid of the problem, try not to use Mo team, some problems running really slow (even if you calculate the time complexity is very good).

Conditions:

  Around the telescopic end $ [L, R] $ answer sequence, complexity is only $ \ Theta (1) $ a. Some $ \ Theta (\ log n) $ forcibly transferring answers with Mo team do even better than $ \ Theta O (n ^ 2) $ violent run fast.


note

  1 ++ and - are in the front or rear.

  2. Under normal circumstances it does not matter the order of four operations, but some questions are influential, for example: BZOJ4358: permu.


 

Code time

Ordinary Mo team:

struct rec
{
	int id;
	int l;
	int r;
	int pos;
}q[N];
int a[N];
int ans[N];
bool cmp(rec a,rec b){return a.pos==b.pos?a.r<b.r:a.pos<b.pos;}
int main()
{
	int n,m;
	scanf("%d%d",&n,&m);
	int t=sqrt(n);
	for(int i=1;i<=n;i++)
		scanf("%d",&a[i]);
	for(int i=1;i<=m;i++)
	{
		scanf("%d%d",&q[i].l,&q[i].r);
		q[i].id=i;
		q[i].pos=(q[i].l-1)/t+1;
	}
	sort(q+1,q+m+1,cmp);
	int l=1,r=0;
	for(int i=1;i<=m;i++)
	{
		while(l>q[i].l)upd(--l,1);
		while(r<q[i].r) upd (++ r, 1);
		while(l<q[i].l) upd (l ++, 0);
		while(r>q[i].r)upd(r--,0);
	}
	for(int i=1;i<=m;i++)
		printf("%d\n",ans[i]);
	return 0;
}

Parity Mo team:

bool cmp(rec a,rec b){return (a.pos)^(b.pos)?a.l<b.l:(((a.pos)&1)?a.r<b.r:a.r>b.r);}

rp ++

Guess you like

Origin www.cnblogs.com/wzc521/p/11240671.html