T4: Reverse Order
A [I] is the prefix and
Derivation (A [J]-A [I])/(J-I)> = m
A [J]-A [I]> = m (J-I)
A [J]-M * j> = A [I]-M * I
Set B [] = A []-M *();
B [J]> = B [I]
That is, reverse order;
There are two methods for reverse order query:
Merge and sort;
Tree array;
The following two methods are available:
1. Previously, only single-point modification and interval summation were used for tree arrays.
I always think lowbit is a magic thing (so far I have not understood the principle)
I searched the internet and used a tree array to find the reverse order method. I found that there was a great guy who wrote it very well... I understood it very well (Thank you ~~)
By the way, we learned the discretization, that is, the following:
for
(i=1;i<=n;i++)
{
scanf
(
"%d"
,&in[i].v);
in[i].order=i;
}
sort(in+1,in+n+1,cmp);
for
(i=1;i<=n;i++) aa[in[i].order]=i;
It is a bit like a pointer operation. (This is my initial feeling about discretization)
Then, the reverse order is obtained based on the principle of the tree array.
In fact, I have never been very clear about the principle of tree arrays. I have a little understanding after I flip the books.
1) First of all, it is the understanding of the C array in update. The process explained in the data link above will mistakenly assume that C is counting a certain number of data.
If you read the white book carefully, you will find that this is not the case. The C array is used to maintain the range and
The getsum operation adds the intervals indicated by the bars along the way, which saves time (note that you can understand the graph)
2). Specifically, C in the process is used to record how many numbers are smaller than I in a certain range.
3) to understand the lowbit process, you also need to carefully read the explanations in the white book.
Whether I = T-N; I + = lowbit (I) or I-= lowbit (I) is a process of finding a father.
4). To put it bluntly, what is the use of lowbit? In fact, it saves time and makes it fast and easy.
Well... in this case, I understand the principle of the tree array and feel like Qaq. What should I do if I forget this kind of thing?
Give me some advice later ~ Read the white book first, understand the picture, read your own sentiment, and finally read the link;
2. Another method is the traditional use: Merge sort to find reverse order Pairs
This algorithm is easy to understand in reverse order. Simply put, it is simply based on the fast sorting idea + statistics.
Main Program: p version... don't care about these details...
Procedure mergesort (L, R: longint); var mid, I, J, K: longint; begin if l = r then exit; Mid: = (L + r) Div 2; mergesort (L, mid); mergesort (Mid + 1, R); I: = L; J: = Mid + 1; K: = L; while (I <= mid) and (j <= r) do if V [I] <V [J] Then begin push (K, I); ans: = ans + R-J + 1; // In L .. mid and Mid + 1 .. in R, J is in [Mid + 1, R] And this segment is in ascending order. If V [J]> V [I] Then V [J .. r] will be greater than // v [I]. This is the same as reverse order. End else push (K, J); while I <= mid do push (K, i); While j <= r do push (K, J); for I: = L to r do V [I]: = temp [I]; end;
Create an equation based on the derivation, and then output ans.
I won't say much about it...
Good night Minna
Day2: T4 Reverse Order (tree array + merge sort)