Maintenance contrast the pros and cons of several ways interval

The so-called maintenance intervals, that is, for a range of some operations and queries, often need to use some of the data structure is completed, we will analyze the pros and cons of various methods of maintenance intervals.

Naive / violence

Generally, this method is somewhat better, easily out BUG. But the time complexity of a high, sub-section should be able to get, even if the water can get data \ (70% \) scores (such as Luo Valley Fenwick tree template 1).

Prefix and / Differential

Prefix and is may \ (\ mathcal {O} ( 1) \) derived interval \ ([l, r] \ ) of one of the information, the difference can be \ (\ mathcal {O} ( 1) \) modifying some sections \ ([l, r] \ ) values.
Prefix and the most significant difference and advantage is to write very intuitive, easy to understand, better than violence to write (this is true, the cycle of violence often need to modify / query, and prefix and / differential avoid these problems, only need to maintain a prefix and / differential arrays on it), and a single operation much faster than violence .
However, the prefix and the differential of the most criticized areas so that future generations can only be that they Interval operation. If you want to simultaneously modify the interval and range queries and then either the prefix or the difference can only \ (\ mathcal {O} ( 1) \) carrying out an operation, another operation is in any case takes a section length the cost of time, the last time complexity is cut down.
But if you are too lazy to write segment tree / Fenwick tree / ST table and the title does not involve two operations, the prefix and / differential has been more than sufficient.

ST table

ST table is widely used in the famous RMQ (range of most value problem) in.
I did not learn, to be filled sinkhole (funny life insurance)

Fenwick tree

If you need to modify, and query the same time interval, then Fenwick tree is a better choice. The core idea is to use an array of tree lowbit modify and query operations.

The greatest advantage of Fenwick tree is to make up for the shortcomings of the prefix and the differential, and time and space are suspended or beaten tree line.

But Fenwick tree is really difficult to understand.

I do not like Fenwick tree, a big reason is that the data structure is not too intuitive, I have no interest in this strange technical research binary 233, in fact, just remember two pieces of code. Where to \ (a_x \) plus a value is to let the current position \ (i \) continue to raise lowbit (x), then the query \ (x \) prefix and is constantly subtract lowbit (x).

void add(int x,int y)
{
    for(register int i=x;i<=n;i+=l(i))t[i]+=y;
}
//等价于 a[x]+=y 

int sum(int x)
{
    int ans=0;
    for(register int i=x;i;i-=l(i))ans+=t[i];
    return ans;
}
//得到第x个数的前缀和 

Anyway, most of which I do not like to use clever but useless binary. . .
And Fenwick tree is extremely easy to expand, so if the topic would not deliberately try to write cards often tree line (of course, if you areKind of like yjxFenwick tree fairy when I did. . )

Segment tree

Love tree line! (Lxl do not hit me, I will not do Ynoi ...)
segment tree lucid, fast time, the card can often be had,Code Description, Is the maintenance interval Bazhe also. By lxl
Then scalability segment tree is very strong, interval changes, a single point of modification, the query interval, a single point of inquiry are capable, and extended to the two-dimensional after more powerful.

But the constant segment tree is really great, too open space four times, basically a little bit of cancer a person on the topic and maintenance intervals like the title would go to card a card segment tree.

But ,,, but ,,, but there are solutions. For example, add a quick read something. . . Or something close io stream synchronization. . . I am not good at, but I know in general can quickly read + O2 card in the past.

I have an article that is specialized segment tree template put these issues cards with a card segment tree always had. . . here

It is generally recommended maintenance intervals topic to write tree line, tree line generally write the best to speed up the reading.

Block

The basic idea is to sequence into a few pieces, query / change time uphold the "overall maintenance, local simple" principle, it is an elegant violence.

I do not know. Sinkhole to be filled.


Probably so much strike.

Guess you like

Origin www.cnblogs.com/BlueInRed/p/12394640.html