Block Array
Building a Block Array
A block array divides an array into several blocks, where information within each block is stored collectively. When querying, if incomplete blocks are encountered at the boundaries, brute-force queries are performed directly. Typically, the block size is \(O(\sqrt{n})\). For a detailed analysis, refer to Xu Mingkuan's Preliminary Exploration of Unconventional Sized Block Algorithms in the 2017 National Training Team thesis.
Below is a direct implementation of building a block array.
Implementation
1 2 3 4 5 6 7 8 9 10 | |
Here, st[i] and ed[i] represent the start and end indices of a block, and size[i] is the size of the block.
Storing and Modifying Block Information
Example 1: The Magic of the Cult Leader
Two types of operations:
- Add \(z\) to every number in the interval \([x,y]\);
- Query the count of numbers greater than or equal to \(z\) in the interval \([x,y]\).
We need to query the count of numbers greater than or equal to a given value within a block, so a t array is used to sort the block, while a retains the original (unsorted) array. For modifications on entire blocks, a method similar to permanent marking is used, with a delta array recording the value added to the entire block. Let \(q\) be the total number of query and modification operations, then the time complexity is \(O(q\sqrt{n}\log n)\).
The delta array records the overall addition value for each block.
Implementation
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 | |
Example 2: Cold Night Ark
Two types of operations:
- Set every number in the interval \([x,y]\) to \(z\);
- Query the count of numbers less than or equal to \(z\) in the interval \([x,y]\).
The delta array records the value to which the entire block is currently set. When a block is not entirely set, a special value (e.g., 0x3f3f3f3f3f3f3f3fll) is used to indicate this. For edge blocks, a pushdown operation is required before querying to propagate the block's stored information to each individual element. After assignment, remember to sort the block again. Other aspects are similar to the previous problem.
Implementation
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 | |
Exercises
- Single Point Modification, Interval Query
- Interval Modification, Interval Query
- Template: Segment Tree 2
- 「Ynoi2019 Mock Contest」Yuno loves sqrt technology III
- 「Violet」Dandelion
- Poetry
本页面最近更新:,更新历史
发现错误?想一起完善? 在 GitHub 上编辑此页!
本页面贡献者:OI-wiki
本页面的全部内容在 CC BY-SA 4.0 和 SATA 协议之条款下提供,附加条款亦可能应用