MỤC LỤC Data structures (7) • Sqrtdecomposition TeX • Fenwick tree • The system of disjoint sets TeX • Segment tree TeX • Treap (treap, deramida) • Modification of the stack and queue for finding the minimum in O (1) • Randomized heap TeX Sqrtdecomposition Sqrtdecomposition is a method or a data structure that allows some typical operations (sum of subarray elements, to find the minimum maximum, etc.) for , significantly faster than for the trivial algorithm. First, we describe a data structure for one of the simplest applications of this idea, then show how to generalize it to solve some other problems, and, finally, look at some other use of this idea: the partition of the input requests sqrtblocks. The data structure on the basis of decomposition sqrt We pose the problem . Given an array . Required to implement such a data structure that will be able to find the sum of the elements for arbitrary and for operations. Description The basic idea sqrtdecomposition is what to do next predposchёt : divide the array into blocks of length approximately , and in each block in advance predposchitaem sum of elements in it. We can assume that the length of one unit and the number of units equal to one and the same number the root of , rounded up: then the array is divided into blocks like this: Although the latter unit may comprise less than , the elements (if not divisible ) is not essential. Thus, for each block , we know the amount on it : So, let the values previously calculated (that is, obviously, operations). That they can give the calculation of the response to another request ? Note that if the interval is long, then it will contain several blocks as a whole, and these blocks, we can find out the amount on them in a single operation. As a result, the total length will be only two blocks falling into it is only partially, and these pieces we have to sum trivial algorithm. Illustration (here designated block number, wherein the lie , and by the number of the block in which lies ): This figure shows that in order to calculate the amount of the segment , it is necessary to sum the elements of only two tails: and , and sum the values in all the blocks, starting from and ending with : (Note: this is incorrect formula where : in this case, some elements are added together twice and in this case it is necessary to sum the elements simply by ) Thus we ekononim significant number of operations. Indeed, the size of each of the tails clearly does not exceed the block length and the number of blocks not exceed . Since we have chosen , the total amount for the calculation of the interval , we need only operations. Implementation We give first a simple realization: входные данные int n; vector a (n); предпосчёт int len = (int) sqrt (n + .0) + 1; и размер блока, и количество блоков vector b (len); for (int i=0; i