Algorithms and Hardness for Multidimensional Range Updates and Queries
AAlgorithms and Hardness for MultidimensionalRange Updates and Queries
Joshua Lau
Sydney, [email protected]
Angus Ritossa
UNSW Sydney, [email protected]
Abstract
Traditional orthogonal range problems allow queries over a static set of points, each with somevalue. Dynamic variants allow points to be added or removed, one at a time. To support morepowerful updates, we introduce the
Grid Range class of data structure problems over arbitrarilylarge integer arrays in one or more dimensions. These problems allow range updates (such as fillingall points in a range with a constant) and queries (such as finding the sum or maximum of values ina range). In this work, we consider these operations along with updates that replace each point in arange with the minimum, maximum, or sum of its existing value, and a constant. In one dimension,it is known that segment trees can be leveraged to facilitate any n of these operations in ˜ O ( n ) timeoverall. Other than a few specific cases, until now, higher dimensional variants have been largelyunexplored.Despite their tightly-knit complexity in one dimension, we show that variants induced by subsets of these operations exhibit polynomial separation in two dimensions. In particular, no trulysubquadratic time algorithm can support certain pairs of these updates simultaneously withoutfalsifying several popular conjectures. On the positive side, we show that truly subquadraticalgorithms can be obtained for variants induced by other subsets.We provide two general approaches to designing such algorithms that can be generalised toonline and higher dimensional settings. First, we give almost-tight ˜ O ( n / ) time algorithms forsingle-update variants where the update and query operations meet a set of natural conditions.Second, for other variants, we provide a general framework for reducing to instances with a specialgeometry. Using this, we show that O ( m / − (cid:15) ) time algorithms for counting paths and walks oflength 2 and 3 between vertex pairs in sparse graphs imply truly subquadratic data structures forcertain variants; to this end, we give an ˜ O ( m (4 ω − / (2 ω +1) ) = O ( m . ) time algorithm for countingsimple 3-paths between vertex pairs. Theory of computation → Data structures design and analysis;Mathematics of computing → Graph algorithms
Keywords and phrases
Orthogonal range, Range updates, Online and Dynamic Data Structures,Fine-grained complexity, Cycle counting
Acknowledgements
We thank Tunan Shi, for suggesting the reduction from to Static Trellised 2D Grid Range ( + , set ) . We also thank Ray Li and anonymousreviewers for helpful suggestions and comments. Orthogonal range query problems are ubiquitous across various fields of Computer Science.In the simplest of these problems, a data set is modelled as a set of points in Z d , and the taskis to design a data structure that can efficiently answer queries which ask: how many pointslie within the (axis-aligned) orthogonal range [ l , r ] × . . . × [ l d , r d ]? One can extend thisdefinition by assigning a value to each point in the input, and having queries ask instead forsome aggregate (e.g. the maximum value) of the values of the points within the query range. a r X i v : . [ c s . D S ] J a n Algorithms and Hardness for Multidimensional Range Updates and Queries
This has been studied extensively [8, 16, 5, 13, 19, 22, 30, 12, 2, 29], along with modelswhich ask to report all points in the range [3, 4, 29].
Dynamic models, where a single pointmay be inserted or removed in a single operation, have also been studied [34, 16, 32, 15, 14],corresponding to the addition or deletion of a single record. Queries may then be interspersedbetween these update operations, providing insight into the data set as it changes over time.Modelling data sets in this way has proven useful for Online Analytical Processing (OLAP)[21, 32] of databases.In practice, however, one may wish to employ more powerful updates. In this work, weexamine data structures which support updating the values of all points that fall within arange in addition to range queries. This models updates to the records in a database tablewhose numerical field values fall within designated ranges. For instance, this could be givingall employees who have been employed between 5 to 10 years, and have KPIs between 80and 90 an “A” rating. We formalise this as follows. (cid:73)
Definition 1.1.
Let d be a positive integer constant, ( Z , q ) be a commutative semigroup and U = { u j } be a set of integer functions. The ( d D) Range ( q, U ) problem gives as inputa set P = { x , . . . , x p } of p points in Z d . A corresponding integer value v i is also given foreach point x i , each initially 0. It requests a series of operations , each taking one of thefollowing forms:update j (( l , r ) , . . . , ( l d , r d )) : for each x i ∈ [ l , r ] × . . . × [ l d , r d ] , set v i := u j ( v i ) query (( l , r ) , . . . , ( l d , r d )) : compute and return q ( P ) , where P is the multiset { v i : x i ∈ [ l , r ] × . . . × [ l d , r d ] } .Importantly, the operations may include updates of different types, and operations may occurin any order. All operations are given online and no information is known about them beforethe preceding operations are performed. There are n = n u + n q operations in all, with n u and n q of the first and second forms, respectively. We are most interested in the case where update functions take the form ∗ c ( x ) = x ∗ c , where ∗ is a binary operation over the integers, and c is an operation-specific constant. Througha slight abuse of notation, we also use ∗ to denote the set of all functions of the form ∗ c .We write ∗ as a member of U as shorthand for ∗ c being a member of U , for all c . Forexample, Range (+ , { + , max } ) allows updates of the form + c (increasing values by c ), max c (replacing values less than c with c ), and queries which ask for the sum of values in a range.For a single update function or binary operation u , we write Range ( q, u ) for short.The Grid Range variants are those whose point set is P = [ s ] d ; P is not given explicitly,but rather, is described in the input by the positive integer s . Hence, the size of input (andthus the running time) can be measured as a function of the number n of operations whichoccur, rather than the size of P . Grid Range problems will form our primary focus, butwe first describe the context surrounding
Range problems as a whole.
In one dimension,
Range problems can be viewed as operating over an array. In this case,balanced binary trees (such as binary search trees or segment trees [9]) over the array havebeen effective tools in solving
Range problems. Such trees allow any range of the array tobe canonically expressed as the disjoint union of O (log p ) subtrees of the tree. A semigroup ( S, +) is a set S equipped with an associative binary operator + : S × S → S . A semigroupis commutative if x + y = y + x for all x, y ∈ S . . Lau and A. Ritossa 3 When the functions in U are closed under composition and each distributes over q ,the folklore technique of lazy propagation over a binary tree solves
1D Range ( q , U ) in O (log p ) time in the worst case, per operation. Lazy propagation can be extended to supportseveral types of updates: it can be applied to solve
1D Range ( max , { + , min , set , max } ) inworst-case O (log p ) time per operation, where set is the binary operation that returns itssecond operand. These techniques are described in more detail in Section 2. Ji [25] consideredthe
1D Range ( + , max ) problem, where the update operation does not distribute over thequery operation, by introducing a technique known as a Ji-Driver Tree (informally “SegmentTree Beats”), generalising lazy propagation. Using this technique, he showed that
1D Range( + , { + , min , set , max } ) can be solved in amortised O (log p ) time per operation.Let B be the set of Range ( q , U ) problems with q ∈ { + , max } and U ⊆ { + , min , set , max } . Motivated by the one dimensional results for problems in B , we seek generaltechniques addressing Range problems in two or more dimensions. This has been asked asan open question in the competitive programming community [26].The
Range problem on a p element array can be generalised to multiple dimensions intwo natural ways: either a set of p points in Z d is provided explicitly, or the point set isconsidered to be [ s ] d . In the former case, one dimensional techniques can be generalisedto higher dimensions with the aid of an orthogonal space partition tree (hereafter simply partition tree ), such as a k d-tree [8]. (cid:73) Lemma 1.2. If
1D Range ( q , U ) on p points can be solved in time T ( p ) per operation and q is computable in O (1) time, then d D Range ( q , U ) can be solved in time O ( p − /d T ( p )) per operation. We prove this result and provide almost-tight Ω( p / − o (1) ) time per-operation lower boundsconditioned on the Online-Matrix Vector (OMv) Conjecture of Henzinger et al. [23], for all Range problems in B when d = 2, in Section 7.The latter case corresponds to the Grid Range class of problems, which is the subjectof the remainder of this work. The same technique does not apply in these cases, as thenumber of points is p = s d . Prior work on
Grid Range problems has been limited to a few specific cases.Since balanced binary trees have been rather effective in solving problems in one dimension,it is natural to ask whether they can be easily generalised to higher dimensions. Lueker [28]and Chazelle [16] generalised binary search trees and segment trees to higher dimensions toanswer various d -dimensional range query problems (without updates) on sets of n pointsin O (log d n ) time per query. This technique can be used to solve Grid Range problemsin the special cases where all update ranges affect a single point, or when all query rangescontain a single point and the update functions are commutative. The latter can be seen as ageneralisation of
Rectangle Stabbing [14] (which accepts a series of d -dimensional boxesand supports queries for the number of boxes covering a given point), a dual of traditionalrange queries. The same structure was used with scaling by Ibtehaz et al. [24] to solve GridRange ( + , + ) in worst-case O (log d s ) time per operation; without scaling, a straightforwardsolution for Grid Range ( max , max ) can be obtained in the same time. These upperbounds contrast with the abovementioned Ω( p / − o (1) ) time conditional lower bounds for An integer function u distributes over q if u ( q ( a, b )) = q ( u ( a ) , u ( b )) Algorithms and Hardness for Multidimensional Range Updates and Queries the corresponding
2D Range problems. For other problems in B , the lazy propagationtechnique used in one dimension cannot be directly applied over this structure, as it requiresa partition tree of the coordinate space.In the Klee’s Measure problem, n rectangles are given in d dimensions, and one isasked to find the volume of their union. In Weighted Depth , the rectangles each havea weight, and one is asked to find the maximum sum of weights of rectangles that cover asingle point. In
Dynamic versions of these problems, a single rectangle can be added orremoved in a single operation, and the new answer must be returned after each one. Thesecan be seen as special cases of
Grid Range problems.When only additions are supported,
Dynamic Klee’s Measure is a special case of
GridRange ( + , set ) or Grid Range ( + , max ) . Overmars and Yap [31] gave a O ( n ( d +1) / log n )time solution over n updates when the rectangles’ coordinates are known during preprocessing .Chan [10] gave a sublogarithmic time improvement over this method and showed that this isnearly tight in two dimensions, giving an Ω( √ n ) time per update worst-case lower bound byreducing from Dynamic Matrix-Vector Multiplication . Dynamic Weighted Depth is the special case of
Grid Range ( max , + ) , wherethe maximum value in the entire grid is queried after each update. It is closely related to Dynamic Klee’s Measure , in that every known algorithm for one has been adaptable tothe other, with running times differing only by a sublogarithmic factor. Hence, with a slightmodification of the result of Overmars and Yap [31], an ˜ O ( n ( d +1) / ) time algorithm for GridRange ( max , + ) can be obtained. Chan [10] showed that Dynamic Weighted Depth is solvable in time O ( n ( d +1) / log d/ log n ), with sublogarithmic improvements specific toentire-grid queries.When there are no updates, Klee’s Measure and
Weighted Depth can be reduced to O ( n ) updates on a d − Dynamic instance with a sweepline, however Chan [11]gave faster O ( n d/ − o (1) ) time algorithms for these. Reductions by Chan [10] and Backurset al. [7] showed that solving static variants of Klee’s Measure or Weighted Depth in time o ( n dω/ ) or O ( n d/ − (cid:15) ) for some (cid:15) >
0, respectively, would improve longstandingupper bounds for
Clique or Max Clique , respectively. It follows that these problemsare W[1]-complete for parameter d . The same reductions can be appropriated for d = 3 toshow that an O ( n / − (cid:15) ) time algorithm for
3D Weighted Depth implies an O ( n − (cid:15) ) timealgorithm for Negative Triangle . A truly subcubic algorithm for
Negative Triangle exists if and only if one exists for
APSP [33], so an O ( n / − (cid:15) ) time algorithm for
2D GridRange ( max , + ) for some (cid:15) > Our main contributions are conditional lower bounds, which serve to elucidate and categorisethe expressiveness of these data structures, and upper bounds in the form of general algorithmsand frameworks for solving
Grid Range problems (such as those in B ) in two or moredimensions. We do so on a Word RAM with Ω(log n )-bit words. In the sequel, we use B todenote B without the problems Grid Range ( + , + ) and Grid Range ( max , max ) , forwhich per-operation polylogarithmic time upper bounds are already known. Lower bounds.
First, we consider algorithms whose per-operation time complexity is afunction of s , the side length of the grid. In two dimensions, we show that there is noalgorithm running in time O ( s − (cid:15) ) per-operation for any (cid:15) >
0, for any problem in B , unlessthe OMv Conjecture is false. Further, for Grid Range ( + , { + , max } ) we obtain identicallower bounds, conditioned on the “extremely popular conjecture” of Abboud et al. [1] that at . Lau and A. Ritossa 5 Table 1
Lower and upper bounds for
Grid Range problems in B , exhibiting polynomialseparation. All results are in two dimensions where d is unspecified, and in d dimensions otherwise,where d is a constant. All lower bounds hold offline, except those for OMv, and all upper boundshold in fully online settings. q U Lower bounds Upper bound max max O ( n log d s ) (extension of segment trees)+ + O ( n log d s ) [24]max + Ω( n / − o (1) ) APSP [7] or OMvΩ( n ( d +1) / − o (1) ) MaxClique [7] ˜ O ( n / ) and O ( n log n ) space˜ O ( n ( d +1) / ) [10, 11]max min Ω( n / − o (1) ) OMv ˜ O ( n ( d +1) / )max setmax { min , set , max } ˜ O ( n ( d +2 d − / d )+ set ˜ O ( n / ω/ ( ω +1) ) = O ( n . )+ { + , set } ˜ O ( n / ω − / (4 ω +2) ) = O ( n . )max { + , min } Ω( n d − o (1) ) d -OV ˜ O ( n d )max { + , min , set , max } + { + , max } Ω( n − o (1) ) 3SUMΩ( n d − o (1) ) d -OV+ { + , min , set , max } least one of the APSP, 3SUM and 2-OV (Orthogonal Vectors) Conjectures are true (the latterof which is implied by the Strong Exponential Time Hypothesis [35]). Hence, we cannotsolve this variant in two dimensions in O ( s − (cid:15) ) time per operation for any (cid:15) > Grid Range ( max , { + , min } ) and Grid Range ( + , { + , max } ) , we generalise our resultsto d dimensions under the d -OV Conjecture to obtain an Ω( s ( d − − o (1) ) time per-operationlower bound. All these lower bounds are almost-tight, as ˜ O ( s d − ) time per-operation upperbounds can be obtained by maintaining s d − one dimensional instances.These results, however, do not preclude the existence of efficient and practical algorithmsfor Grid Range problems whose overall complexity is a function of the number of operations, n – the true size of the input – rather than s . Our aforementioned lower bounds underthe OMv and APSP Conjectures translate to conditional Ω( n / − o (1) ) lower bounds for allproblems in B , but our lower bound under the 3SUM Conjecture translates to conditionallower bounds of Ω( n − o (1) ) for
2D Grid Range ( + , { + , max } ) . In d dimensions, our lowerbounds under the d -OV Conjecture translate to Ω( n d − o (1) ) time conditional lower boundsfor Grid Range ( max , { + , min } ) and Grid Range ( + , { + , max } ) . Our lower bounds aresummarised in Table 1 and proven in Section 3. Upper bounds.
By reducing to algorithms in one dimension, ˜ O ( n d ) time algorithms canbe found for all these problems. Hence, we aim to determine which problems in B can besolved more efficiently, by seeking truly subquadratic time algorithms in two dimensions. Tothis end, we provide two general frameworks for developing such algorithms, both based onthe approach of Overmars and Yap [31] for Dynamic Klee’s Measure . Their algorithm
Algorithms and Hardness for Multidimensional Range Updates and Queries ˜ O ( n )˜ O ( n ) + , { + } max, { max } max, { + , min , set , max } +, { + , min , set , max } +, { + , max } Ω( n − o (1) ) max, { + , min } max, { + } max, { set } max, { min } +, { set } Ω( n . − o (1) ) OMv/APSP ?˜ O ( n . ) O ( n . ) +, { + , set } O ( n . ) ?max, { min , set , max } ˜ O ( n . ) ? + , { max } max , { + , set } ?? ? Figure 1
Our results for
2D Grid Range . Questions left open that ask whether certain problemsbelong to certain complexity classes are marked with a ‘?’. See Section 9 for more details on openproblems. constructs a partition tree of the grid such that the rectangles intersecting each leaf regionform a “trellis” pattern. This requires the coordinates of all rectangles to be known duringprecomputation.First, we provide a fully-online generalisation of this approach, that does not requirecoordinates to be known ahead of time. (cid:73)
Theorem 1.3.
Suppose q and u are associative, commutative binary operations, computablein O (1) time, such that u distributes over q , and 0 is an identity of u . Then Grid Range( q , u ) can be solved in ˜ O ( n ( d +1) / ) time. This algorithm does not apply to problems such as the
Range ( + , set ) problem describedin the abstract, as set does not distribute over +. Motivated by this, in Section 5 we showthat in two dimensions, efficient solutions to static Grid Range ( q , U ) instances wherethe update ranges form the same “trellis” pattern can be used to give fully-online, trulysubquadratic time solutions to many Grid Range ( q , U ) problems. We also extend theseresults to multiple dimensions.As an application, we use this approach to give a truly subquadratic time algorithm for
2D Grid Range ( max , { min , set , max } ) . We do the same for
2D Grid Range ( + , set ) and
2D Grid Range ( + , { + , set } ) in Section 6, by drawing an equivalence and a reductionbetween the respective “ Static Trellised ” instances and counting the number of 2- and3-edge paths between vertex pairs, respectively. To this end, we prove the following result. (cid:73)
Theorem 1.4.
Let G be a graph with m edges and O ( m ) vertices. The number of 3-edgewalks between each of q vertex pairs in G can be found in O ( m ω/ (2 ω +1) ( m + q ) (2 ω − / (2 ω +1) ) time. In this way, queries on static graphs yield efficient, fully-online dynamic
Grid Range . Lau and A. Ritossa 7 data structures. We find it somewhat surprising that, though all of the problems in B can besolved in ˜ O ( n ) time in one dimension, our upper and lower bounds imply likely polynomialseparation in two or more dimensions (see Table 1).Lastly, we provide a fully-online algorithm for
2D Grid Range ( max , + ) that uses O ( n log n ) space, and runs in a time comparable to that of existing algorithms. (cid:73) Theorem 1.5.
2D Grid Range ( max , + ) can be solved in ˜ O ( n / ) time, and O ( n log n ) space. This is proven in Section 8. While our complexity is slower than existing results by Chan[10] by a polylogarithmic factor, those require O ( n / o (1) ) space and are not fully-online.Overmars and Yap [31] also gave an O ( n ) space algorithm for static Klee’s Measure in d dimensions using a sweepline, but this does not apply in the dynamic case. Model of computation.
All results are described are for a Word RAM over l -bit words,with l = Ω(log n ). We further assume that any coordinates or values given in the inputs canbe represented in a constant number of words, and that basic arithmetic and the (binary)operations used in range problems can be performed on a constant number of words inconstant time. In particular, s = O ( n c ), for some constant c . Notation.
We use the notation ˜ O ( f ( n )) = O ( f ( n )poly log n ) to hide polylogarithmic factors.Note that log c s = log c n for any constant c . Where our algorithms and proofs use positiveor negative ∞ as a value, this can be replaced with a suitably large value, for a given inputinstance.Where x ≤ y are real numbers, we denote by [ x, y ] the set of all integers between x and y , inclusive. When y ≥
1, we write [ y ] as shorthand for [1 , y ].The binary operation set is the operation whose value is its second operand. That is,set( a, b ) = b .2 ≤ ω < . n × n integer matrices. We alsowrite ω ( a, b, c ) for the time taken to multiply an a × b matrix by a b × c matrix. Ancillary problems and variants. In Range problems, we say that the i th operation(update or query) occurs at time i . In Offline variants, all operations are provided togetherwith the initial input, and in
Static variants, it is guaranteed that all updates precede allqueries.We formalise and appropriate the “trellis” pattern observed by Overmars and Yap [31]for our use, as follows. Call a
Grid Range instance
Trellised if for each update, there isa dimension d ∗ such that [ l d , r d ] = ( −∞ , ∞ ) for all d ∈ [ d ] \ { d ∗ } . When d = 2, updatesmust either cover all points in a range of rows, or all points in a range of columns, which wecall row updates and column updates , respectively. Segment trees and lazy propagation.
Let s be a power of two. A segment tree over anarray A containing s elements is a complete rooted binary tree of ranges over [ s ]. The rootis [1 , s ], and each node [ a, b ] has two children: [ a, h ], [ h + 1 , b ], where h = ( a + b − / O ( s ) nodes in the tree, with a depth of log s . Given an integer interval I = [ l, r ](1 ≤ l ≤ r ≤ s ), we can write I as a canonical disjoint union of a set base( I ) of O (log s ) nodes. These are defined as the nodes closest to the root that are fully contained in Algorithms and Hardness for Multidimensional Range Updates and Queries I , and can be found recursively. We say that an integer interval [ l , l ] decomposes to I if I ∈ base([ l , l ]).Segment trees can be used to prove the following folklore proposition. (cid:73) Proposition 2.1 (Lazy propagation) . Suppose U is a set of update functions, and q is aquery function, computable in O (1) time. If there is a set ¯ U such that: U ⊆ ¯ U are sets of functions that can be represented and composed in ˜ O (1) space and time,such that the composition of any series of at most n (possibly non-distinct) functions of U results in a function in ¯ U ; and For each u ∈ ¯ U , u distributes over q then
1D Grid Range ( q , U ) is solvable in ˜ O ( n ) time. Proof.
Without loss of generality, increase s so it is a power of two, and build a segmenttree T over [ s ]. In each segment tree node a , we will store an element u a of ¯ U . We also storea “ q -value” at each node q a , each initially the value of q over an array of | a | a haschildren b and c , we keep the invariant q a = u a ( q ( q b , q c )). Whenever we need to access node a , for each ancestor a from the root, we update u b := u a ◦ u b , q b := u a ( q b ) for each child b of a , then reset u a := 0. This “lazily propagates” the updates from a parent to a child.To perform an update on an interval I , we perform an update on each node a in base( I )separately. For each node, we first propagate as above, then modify u a and q a , as above. Wethen consider the ancestors of a from a ’s parent back to the root, and update their q -valuesto maintain the invariant. To perform query, we perform a similar propagation on each nodein base( I ). The result can then be found by combining the q -values of these nodes.We note that the segment tree need not be constructed explicitly: we only create andstore values for nodes which are required. Using binary exponentiation, we can find the q -value of an untouched node in O (log s ) = O (log n ) time. (cid:74) Hardness conjectures.
We base hardness on the following popular conjectures. The first isa conjecture of Henzinger et al. [23]. (cid:73)
Conjecture 2.2 (OMv Conjecture) . No (randomized) algorithm can process a given m × m boolean matrix M , and then in an online way compute the ( ∨ , ∧ ) -product M v i for any m boolean vectors v , . . . , v m in total time O ( m − (cid:15) ) , for any (cid:15) > . In the OuMv problem, the same matrix M is given during preprocessing, and m pairsof boolean query vectors ( u , v ) , . . . , ( u m , v m ) are given online. For each, the value of theproduct u Ti M v i is requested. Note that the answer to each of these queries is a single bit.Henzinger et al. showed that if OuMv can be solved in O ( m − (cid:15) ) time, then OMv can besolved in O ( m − (cid:15)/ ) time, so these problems are subcubic equivalent.We refer the reader to the survey by Vassilevska Williams [36] for more details on theremaining conjectures. (cid:73) Conjecture 2.3 (APSP Conjecture) . No (randomized) algorithm can solve
All-PairsShortest Paths (APSP) in O ( | V | − (cid:15) ) time for (cid:15) > , on graphs with vertex set | V | , edgeweights in {− v c , . . . , v c } and no negative cycles, for large enough c . (cid:73) Definition 2.4 ( k -OV problem) . Let k ≥ be a constant, and z = ω (log n ) . Given k sets A , . . . , A k ⊆ { , } z with each | A i | = m , determine if there exist a ∈ A , . . . , a k ∈ A k suchthat a · . . . · a k = 0 , where a · . . . · a k := P zi =1 Q kj =1 a ji . (cid:73) Conjecture 2.5 ( k -OV Conjecture) . No (randomized) algorithm can solve k -OV in m k − (cid:15) poly( z ) time, for any (cid:15) > . . Lau and A. Ritossa 9 (cid:73) Conjecture 2.6 (3SUM Conjecture) . Any algorithm requires m − o (1) time in expectation todetermine whether a set S ⊂ {− m , . . . , m } of m integers contains three distinct elements a, b, c ∈ S with a + b = c . In this section, we establish conditional hardness for problems in B under popular conjectures.We do so by considering per-operation time complexity in terms of s (the side length of thegrid), and overall complexity in terms of n (the number of operations).Backurs et al. [7] gave a reduction from Max k -Clique to k D Weighted Depth .When k = 3, Max k -Clique is equivalent to Negative Triangle , which is subcubicequivalent to
APSP [33]. Adapting this reduction with a sweepline implies conditional lowerbounds for
2D Grid Range ( max , + ) . (cid:73) Proposition 3.1 (Slightly generalised from [7]) . If Offline 2D Grid Range ( max , + ) canbe solved in amortised O ( s − (cid:15) ) time per update and O ( s − (cid:15) ) time per query, or in O ( n / − (cid:15) ) time overall, for any (cid:15) > , then the APSP Conjecture is false. Proof.
We reduce from
Negative Triangle to the equivalent
2D Grid Range ( min , + ) problem by modifying the reductions of Chan [10] and Backurs et al. [7]. Let G = ( V, E ) bea graph without self-loops, and with weighted edges: for each ij E , we let w ij = ∞ .We start with an | V | × | V | grid C containing entirely zeros. Using O ( | V | ) additions, wecan initialise this grid to match the adjacency matrix of G . That is, the point ( i, j ) has value w ij . Then, for each vertex i , we add ∞ to point ( i, i ).Next, we will iterate through each vertex j , in order, to test if j is contained in a negativetriangle. We iterate through each edge ij in non-increasing order of w ij . For each, we usetwo updates to add w ij to all points in row i and column i of H . At this stage, the point( i, k ) is equal to w ik + w ij + w jk if i , j and k are distinct vertices, and ∞ otherwise. Hence,in a single min query, we are able to determine if there is a negative triangle containing j .We then undo these row and column updates by subtracting these amounts, in the sameorder we added them. We then proceed to vertex j + 1.In all, we perform O ( | V | ) updates, and O ( | V | ) queries, completing the proof. (cid:74) We now establish more general linear per-operation lower bounds for
2D Grid Range problems in terms of s , based on the OMv Conjecture. (cid:73) Lemma 3.2.
Suppose ( Z , + , is a monoid (resp. group ), ( Z , · ) is a commutativesemigroup such that r = r for all r ∈ Z and that there exists x ∈ Z such that ∈ { xz, ( x + x ) z, ( x + x + x ) z } if and only if z = 0 . Then,
2D Grid Range ( · , + ) cannotbe solved in worst-case (resp. amortised) O ( s − (cid:15) ) time per update and O ( s − (cid:15) ) time perquery, for any (cid:15) > , unless the OMv Conjecture is false. If ( Z , + , is a group,
2D GridRange ( · , + ) also cannot be solved in O ( n / − (cid:15) ) time overall, for any (cid:15) > , unless theOMv Conjecture is false. Proof.
We reduce OuMv to an instance of
2D Grid Range ( · , + ) with s = m . Let A ( i,j ) denote the value of the point ( i, j ). Initially, each A ( i,j ) = 0. In preprocessing, for each M ij = 0, add x to A ( i,j ) . We now say the data structure is in its ready state . ( Z , + ,
0) is a monoid if ( Z , +) is a semigroup, and the identity of + is 0. ( Z , + ,
0) is a group if it is a monoid and + is invertible.
Let ( u, v ) denote a pair of input vectors. For each u i = 0, add x to the value of all pointsin row i , and for each v j = 0, add x to the value of all points in column j . Every point nowhas a value in { , x, x + x, x + x + x } . Now some point has value 0 if and only if the answerto the OuMv query is 1, so we establish this with a single range query. We then restorethe data structure to its ready state, either by keeping a journal of updates (semigroup) orby updating with additive inverses (group). The reduction uses O ( m ) updates and O ( m )queries, implying the stated conditional lower bounds. (cid:74) This gives a Ω( n / − o (1) ) time conditional lower bound on
2D Grid Range ( max , + ) , matching that of Proposition 3.1. Through different reductions, we are able to obtainmatching lower bounds for the other problems in B , under the same conjecture. (cid:73) Lemma 3.3.
If any of
2D Grid Range ( + , max ) ,
2D Grid Range ( + , min ) or max , min ) can be solved in amortised O ( s − (cid:15) ) time per update and O ( s − (cid:15) ) time per query, or in O ( n / − (cid:15) ) time overall, for some (cid:15) > , then the OMv Conjecture isfalse. Proof.
We will prove this for
2D Grid Range ( + , min ) and
2D Grid Range ( max , min ) .The result for
2D Grid Range ( + , max ) directly follows from
2D Grid Range ( + , min ) .We reduce OuMv to an instance of
2D Grid Range ( q , min ) , with q ∈ { + , max } and s = m . Let A ( i,j ) denote the value of the point ( i, j ). Initially, each A ( i,j ) = 0. Inpreprocessing, for each M ij = 0, we perform an update to set the value of A ( i,j ) to − m .Before handling the k -th (1-indexed) pair of query vectors ( u, v ), we maintain that A ( i,j ) = − m if M ij = 0, and − k + 1 otherwise. For each u i = 0, we perform a min updatewith value − k to all points in the i -th row. We perform similar updates to the j -th columnif v j = 0. The answer to the query is 1 if and only if there is a point in A with value − k + 1.This can be established with a single max query, or with a + query if we precompute thenumber of points where M ij = 0. We finish the query by performing a single min updatewith value − k to all points in A .Over m pairs of query vectors, our reduction performs O ( m ) updates and O ( m ) queries,implying the stated conditional lower bounds. (cid:74)(cid:73) Lemma 3.4. If
2D Grid Range ( + , set ) or
2D Grid Range ( max , set ) can be solvedin O ( n / − (cid:15) ) time overall, for some (cid:15) > , then the OMv Conjecture is false. Proof.
We reduce OuMv to an instance of
2D Grid Range ( q , set ) , with q ∈ { + , max } and s = m . We only utilise a m × m area of the grid, and we restrict our updates andqueries to this area. Let A ( i,j ) denote the value of the point ( i, j ). In preprocessing, weperform m updates so that all points in the j -th column have a value of ( j − m ) + 1.Each M ij is represented in A by a 1 × m section of points with values [1 , , , ..., m ]. Foreach M ij = 0, we perform an additional update to set its section of points to 0.For the k -th (1-indexed) query, consider the columns that were assigned a value of m − k +1during preprocessing. We refer to the j -th such column as the j -th relevant column . Wemaintain that at the beginning of each query, all points in relevant columns from previousqueries have been set to 0. Hence, at the start of the k -th query, A ( i,j ) ≤ m − k + 1 holds forall points ( i, j ), and A ( i,j ) = m − k + 1 holds only for points in relevant columns. Let ( u, v )denote a pair of input vectors. For each v j = 0, we perform an update to set all values in the j -th relevant column to 0. The answer to the query is 1 if and only if there is a point withvalue m − k + 1 in any row i where u i = 1. This can be established by performing a max or+ query in each such row. After performing these queries, we set the value of the points inall relevant columns to 0. . Lau and A. Ritossa 11 Over m pairs of query vectors, our reduction performs O ( m ) updates and queries,implying the stated conditional lower bound. (cid:74) Together, these give conditional lower bounds for each of the single-update variants in B . (cid:73) Corollary 3.5. If q ∈ { + , max } and u ∈ { + , set , min , max } and q = u , then
2D GridRange ( q , u ) cannot be solved in worst-case O ( s − (cid:15) ) time per update and O ( s − (cid:15) ) time perquery, or in O ( n / − (cid:15) ) time overall, for some (cid:15) > , unless the OMv Conjecture is false. If u = set , then the lower bounds are amortised rather than worst-case. When measuring complexity in terms of s , it appears difficult to improve upon the naivesolution which maintains a 1D instance for each column of the grid, for these problems.However, when we measure complexity in terms of n , there is a polynomial gap betweenthe Ω( n / − o (1) ) time lower bound, and the ˜ O ( n ) time naive algorithm. Indeed, Chan [10]gave a ˜ O ( n / ) time solution for Grid Range ( max , + ) . This might lead one to ask ifthere exists a general mechanism to adapt ˜ O ( n ) time algorithms in one dimension to ˜ O ( n / )time algorithms in two dimensions, as there is for Range problems on a set of n explicitlyprovided points. Alas, when we consider variants with two simultaneous types of updates,we can obtain stronger reductions from the d -OV and 3SUM Conjectures, suggesting that itis unlikely that such a mechanism exists. (cid:73) Lemma 3.6.
Let d ≥ be a constant. If Offline Static Trellised d D Grid Range( + , { + , max } ) or Offline Static Trellised d D Grid Range ( max , { + , min } ) can besolved in amortised O ( s ( d − − (cid:15) ) time per update and amortised O ( s d − (cid:15) ) time per query, orin O ( n d − (cid:15) ) time overall, for any (cid:15) > , then the d -OV Conjecture is false. Proof.
We first prove this for
Grid Range ( + , { + , max } ) . We reduce from d -OV with z = log n to an instance of d D Grid Range ( + , { + , max } ) with s = m and n = ˜ O ( m ),over the points [ m ] d . Consider the first entry in each vector. Let v ji be the i th vector in A j .For each j ∈ [ d ] and each vector v ji ∈ A j , using the data structure, add ( v ji ) to all pointswith x j = i . Now a point x = ( x , . . . , x d ) ∈ [ m ] d has value d if and only if ( v jx j ) = 1 forevery j ∈ [ d ]. We then undo these updates by repeating the operations with the negations ofthe added values, to restore every point to 0. We repeat this procedure for each of the z entries in the vectors.Now observe that v x · . . . · v dx d = 0 if and only if C x never attained a value of d throughout this process. Using a trick of Ji [25] for storing the “historic maximum”, wecan use the data structure to represent an array D , such that at all times, D x stores thedifference between C x and the maximum value attained by C x thus far. We can do this bytranslating each + update to a range in C to a + and max update in D : whenever c is addedto C x , we update D x := max( D x − c, C have been performed, C isentirely zeros, so D x stores the maximum value attained by C x . Thus, we need to check if D contains any value less than d . Since every value is at most d , it suffices to check whetherthe sum of all points’ values is dm d . We note that this reduction can be done offline, uses O ( mdz ) = ˜ O ( m ) updates (each of the form x j = i ) and a single query spanning the wholegrid, giving the required lower bounds. Grid Range ( max , { + , min } ) is equivalent to Grid Range ( min , { + , max } ) bynegating all inputs and outputs, so D can be constructed similarly. It then suffices to checkif the minimum value in D is less than d , which can be done in a single query. (cid:74)(cid:73) Lemma 3.7. If Offline Trellised 2D Grid Range ( + , { + , max } ) can be solved withamortised O ( s − (cid:15) ) time per update and query, or in O ( n − (cid:15) ) time overall, and any (cid:15) > ,then the 3SUM Conjecture is false. Proof.
We reduce from an instance of 3SUM on a sequence x , . . . , x m of m integers to aninstance of
2D Grid Range ( + , { + , max } ) with s = m .Construct an m × m array A , such that A ij = x i + x j , by using the data structure toadd x i to each row i , and add x j to each column j . It suffices to check if x i is present in A , excluding row i and column i , for any i ∈ [ m ]. This can be done for the whole of A bycounting the number of points in A with value at least x i , and value at least x i + 1. We cancount and exclude the number of points in row or column i with value x i , which is doublethe number of values in the array equal to 0, subtracting 1 for point ( i, i ) if x i = 0.Suppose we would like to count the number of points A ≥ c in A with value at least c .We can do so using a constant number of updates and queries to the data structure. First,request an update to replace each value A ij with max( A ij , c − ≥ c − of A . We can repeat this to find Σ ≥ c of A . All points with original value at most c − A ≥ c = m − (Σ ≥ c − Σ ≥ c − ).This process reduces the problem to a series of max updates to A , each immediatelyfollowed by a sum query over A . To ensure these updates do not interfere with each other,we perform these update-query pairs in non-decreasing order of update value. Noting thatthis reduction uses O ( m ) updates and queries completes the proof. (cid:74) A trivial extension of Proposition 3.1, together with the results above imply strongconditional hardness for
Offline 2D Grid Range ( + , { + , max } ) , when complexity ismeasured per-operation. (cid:73) Corollary 3.8. If Offline 2D Grid Range ( + , { + , max } ) can be solved in amortised O ( s − (cid:15) ) time per update and query, or in O ( n / − (cid:15) ) overall, for any (cid:15) > , then the APSP,2-OV and 3SUM Conjectures are all false. Our lower bounds show that a general approach adapting almost-linear one dimensionalalgorithms for
Grid Range problems to truly subquadratic solutions for two dimensionalinstances is unlikely to exist. However, these results do not preclude the existence of efficientand practical algorithms for specific problems, so we would like to classify which
2D GridRange problems can (and can’t) be solved in truly subquadratic time. To this end, we nowdescribe truly subquadratic algorithms to some of the as-of-yet unclassified problems in B ,and generalise these to specific subclasses of
2D Grid Range problems.
Grid Range problems with a Dynamic Partition
In this section, we describe an extension of the partition of Overmars and Yap [31], andprovide some applications. Notably, our data structure is fully-online in that it does notrequire the coordinates in the input to be known during preprocessing.
First, we introduce some terminology to reason about orthogonal objects in d dimensions.A box is a d -dimensional (orthogonal) range. If B = [ l , r ] × . . . × [ l d , r d ] is a box and i ∈ [ d ] is a dimension, the i -boundaries of B are[ l , r ] × . . . × [ l i − , r i − ] × { l i } × [ l i +1 , r i +1 ] × . . . × [ l d , r d ] and[ l , r ] × . . . × [ l i − , r i − ] × { r i } × [ l i +1 , r i +1 ] × . . . × [ l d , r d ] . . Lau and A. Ritossa 13 The coordinate of an i -boundary is its only coordinate in the i th dimension. Hence, thecoordinates of the 1-boundaries of B are l i and r i . If V is a set of boxes, the i -boundaries of V are the i -boundaries of the boxes in V .For two boxes R = Q di =1 [ l i , r i ] and R = Q di =1 [ l i , r i ], we say that R is an i - pile withrespect to R if [ l j , r j ] ⊆ [ l j , r j ] for all dimensions j ∈ [ d ] \ { i } . Separately, we say that R partially covers R if ∅ (cid:40) R ∩ R (cid:40) R . Similarly, R completely covers R if R ⊆ R .An i - slab ( i ∈ [ d ]) is a box of the form [ l , r ] × . . . × [ l i , r i ] × Z d − i . We define Z d to be a0-slab. A partition tree is a rooted tree where All nodes are orthogonal ranges of Z d The root is Z d Every non-leaf node is the disjoint union of its immediate children.A partition tree is a level partition tree if it has depth d , and the nodes at depth i ( i ∈ [0 , d ])are i -slabs. Hence, a node at depth i in a level partition tree is cut at a series of coordinatesin dimension i + 1 to form its children.We now describe a data structure that maintains a list of boxes, corresponding to updatesfor a Grid Range problem. It will maintain a level partition tree of the grid so that the onlyboxes intersecting each leaf node are piles with respect to that leaf. We will construct this insuch a way that the number of children of each node, and number of boxes intersecting eachleaf is not too large. To this end, we describe how to form our level partition tree. We startwith a recursive function which constructs a level partition tree over a given i -slab. Algorithm 1
Partitioning an i -slab function partitionSlab( i , I × . . . × I i ) s ← I × . . . × I i × Z d − i if i = d thenreturn s Let V be the set of boxes in V which have a j -boundary intersecting s for some j ≤ i Let { x , . . . , x b } be the set of coordinates of ( i + 1)-boundaries in V , sorted sothat x < . . . < x b Let x = −∞ and x b +1 = ∞ . for j = 0 to b do c ← partitionSlab( i + 1 , I × . . . × I i × [ x j , x j +1 ] ) Append c as a child of s return s Suppose that v is a box in V and that s is a node returned by partitionSlab at level i . Then, there is at most one dimension j ≤ i where v has j -boundaries intersecting s . Inparticular, if s is a leaf, then if v intersects s , v is a pile with respect to s . Inserting a box.
Suppose T is a level partition tree over a set V of boxes. We will describehow to add a box b to T , while maintaining this condition.First, find the (at most) 2 columns which contain the 1-boundaries of b . If either ofthese columns C contains 2 √ n V ∪ { b } , we delete it, and partition it intotwo disjoint columns C and C , each containing at most √ n V ∪ { b } . Weconstruct subtrees by calling partitionSlab on C and C , which replace the subtrees of C as children of the root. Clearly, only O ( √ n ) columns are created over the course of thealgorithm.Next, for each i ∈ [1 , d − S i be the set of slabs s i in T , such that a j -boundaryof b intersects s i for some j ≤ i . To maintain the condition, we need to cut each s i at the coordinates of the ( i + 1)-boundaries of b . This splits (up to) two children of s i into twonew slabs at depth i + 1: we once again call partitionSlab on each of these new slabs andreplace the subtree rooted at s i with the returned trees. Balancing boxes.
The final ingredient in our algorithm is a mechanism for bounding themaximum number of children of a node. To do so, we strategically insert additional boxes,called balancing boxes . Let C be a column of T , i be a dimension and x be a coordinate indimension i . A ( C, i, x )-balancing box has the following properties:The box has both of its 1-boundaries inside the column C .The i -boundaries of the box both have coordinate x ;The remaining j -boundaries ( j
6∈ { , i } ) are chosen arbitrarily.For each dimension i , we keep a list X i of the coordinates of i -boundaries of V in non-decreasing order. For each column C , we maintain the invariant that for each consecutiverange X i [ j, j + √ n −
1] of √ n i -boundaries in X i , there is at least one ( C, i, x )-balancingbox with x ∈ [ X i [ j ] , X i [ j + √ n − x being the i -coordinate in thecentre of the range. Hence, O ( d √ n ) balancing boxes are added for each column, and so O ( n )balancing boxes are added overall, since d is a constant.This concludes the description of the algorithm; we now analyse its time complexity.First, we make an observation about the impact of balancing boxes. (cid:73) Lemma 4.1.
When balancing boxes are used, the set V described in partitionSlab willbe of size O ( √ n ) for all nodes in the partition tree. Proof.
Let B C be the set of balancing boxes with a column C . By definition, all b ∈ B C have a 1-boundary in C . Consider all slabs s i in the subtree of C at a depth i < d . Sincea cut is made at the ( i + 1)-boundaries of b ∈ B C , the children of s i each contain at most O ( √ n ) ( i + 1)-boundaries, and so by induction, this is true for all nodes in the subtree rootedat s i . (cid:74) We now obtain the following generalisation of the result of Overmars and Yap. (cid:73)
Theorem 4.2.
There is a data structure that maintains a set V of boxes, and supports n fully-online box insertions to V . Throughout, it can maintain a level partition tree of Z d whose leaves partition Z d into a set of O ( n d/ ) axis-aligned regions (colloquially “t-regions”,short for “trellised-regions”) such that: Every box in V does not intersect, completely covers or is a pile with respect to eacht-region; Each box partially covers O ( n ( d − / ) t-regions; Each t-region is partially covered by at most O ( √ n ) boxes; Any line parallel to a coordinate axis intersects at most O ( n ( d − / ) t-regions; and A list of the boxes that partially cover each t-region is maintained.This all can be done in amortised ˜ O ( n ( d − / ) time per insertion. Proof.
It follows from Lemma 4.1 that in each execution of partitionSlab , | V | = O ( d √ n )= O ( √ n ). Hence, each recursive call in partitionSlab creates O ( √ n ) children.We will now prove the five conditions of Theorem 4.2 are met. The first condition is truedue to the method used to partition slabs. In particular, performing cuts for all boxes in V which have a j -boundary intersecting s for some j ≤ i (that is, the set V ) ensures that onlyone j -boundary of a given box can intersect each t-region. . Lau and A. Ritossa 15 For the second condition, consider some box b . For b to partially cover a t-region, it musthave an i -boundary in the region for some dimension i . Consider all nodes s in the tree witha depth i −
1. There are O ( n ( i − / ) such nodes s , since the degree of all nodes in the tree isbounded by O ( √ n ). At most 2 children of s contain an i -boundary of b . There are O ( n ( d − i ) / )leaves in the subtrees of these children. Hence, at most O ( n ( i − / × n ( d − i ) / ) = O ( n ( d − / )t-regions are partially covered by b for each dimension i .The third condition follows from Lemma 4.1.The fourth condition can be proved in a similar way to the second. Consider a lineparallel to the i -axis. For each node at a depth i − partitionSlab , wecreate a list of the boxes which partially cover it. Additionally, each time a new box b isinserted we append it to the lists of all pre-existing t-regions that it partially covers. Notethat by the second condition, b is added to O ( n ( d − / ) lists. This can be done in O ( n ( d − / )time by iterating over all nodes at a depth of d − b .We will now consider the time complexity of partitionSlab . Since the returned treehas depth d − i , there are at most O ( n ( d − i ) / ) leaves in the partition. In each call to partitionSlab , we can find the set V in ˜ O ( | V | ) = ˜ O ( √ n ) time by keeping a sorted list of i -boundaries for each dimension i . We can create the list of boxes which cover a t-region inthe same way. Hence, partitionSlab on an i -slab has a time complexity of O ( n ( d − i +1) / ).Now consider the complexity of producing the partition. Across all n insertions, O ( √ n )columns are created, each in ˜ O ( n d/ ) time. This takes ˜ O ( n ( d +1) / ) time overall and henceamortised ˜ O ( n ( d − / ) time per insertion.When a box is inserted, up to two columns are split. Additionally, let S be the union ofthe sets of slabs S i described in the inserting a box paragraph. We will now consider thecontribution of splits made to slabs in S . If a slab is not in S , at most 2 of its children arein S . Hence, for each dimension i , there are at most O ( n ( i − / ) slabs that are split. Theoverall cost of calling partitionSlab on these slabs is O ( n ( d − / ). (cid:74) Using this structure, we can reduce
Grid Range problems to
Trellised Grid Range problems when the update operation distributes over the query operation. (cid:73)
Theorem 4.3.
Suppose q and u are associative operations, both computable in O (1) time,such that q is commutative, u distributes over q , and 0 is an identity of u . If Trellised d DGrid Range ( q , u ) can be solved in ˜ O ( n ) time, then Grid Range ( q , u ) can be solved in ˜ O ( n ( d +1) / ) time. Proof.
We use the structure given by a dynamic level partition tree J from Theorem 4.2.Recall that there are O ( n ( d − / ) ( d − J , and each is a parent of O ( √ n ) t-regions,which are leaves of J . For each ( d − d -coordinate, with each array entry containing a Trellised instance for the corresponding t-region. We support operations whose ranges completelycover multiple children by using a modified version of the
1D Grid Range ( q , u ) structure(see Proposition 2.1), treating the q -value of entire regions as the array values. Operationsaffecting only part of a child are handled by the corresponding Trellised instance. Inparticular, when a box corresponding to an update (query) is inserted into the list of at-region, we perform an update (query) on that t-region.
Operations are then performed by iterating over all ( d − O ( n d/ ) Trellised instances, each facilitating O ( √ n ) operations, giving the required time complexity. (cid:74) We apply Theorem 4.3 to give ˜ O ( n ( d +1) / ) time algorithms for particular problems.First, we show that Trellised variants can be solved as separate one dimensionalinstances, when the conditions of Theorem 4.3 are met and u is also commutative . (cid:73) Lemma 4.4.
Suppose q and u are associative, commutative binary operations, computablein O (1) time, such that u distributes over q , and 0 is an identity of u . Then TrellisedGrid Range ( q , u ) is solvable in ˜ O ( n ) time. Proof.
For notational convenience we provide a proof for the case where q is max and u is+: other operations are proven identically. By the definition of Trellised , we can associateevery update with a dimension i such that the update range is an i -pile with respect to Z d ;we call this an i -update for short. At any given point in time, let U i ( x ) be the sum (withrespect to u ) of all i -updates with coordinate x in dimension i .Now consider a query over the range R = [ l , r ] × . . . × [ l d , r d ]. The answer to the querycan be written asmax ( x ,...,x d ) ∈ R X i ∈ [ d ] U i ( x i ) = X i ∈ [ d ] max ( x ,...,x d ) ∈ R U i ( x i ) = X i ∈ [ d ] max x i ∈ [ l i ,r i ] U i ( x i )by distributivity. Hence, we can reduce to d instances of
1D Range ( q , u ) , which each canbe solved in ˜ O ( n ) time, by Proposition 2.1. (cid:74) This proves Theorem 1.3, giving efficient fully-online algorithms in these cases. (cid:73)
Corollary 4.5.
Grid Range ( max , + ) and Grid Range ( max , min ) can be solved in ˜ O ( n ( d +1) / ) time. There also exists some instances where u is not commutative for which an ˜ O ( n ) solution to Trellised Grid Range ( q , u ) exists, giving us algorithms with the same time complexity. (cid:73) Lemma 4.6.
Trellised Grid Range ( max , set ) can be solved in ˜ O ( n ) in d dimensions. Proof.
Consider only i -updates for some dimension i . For each i -coordinate, we will storethe latest set update which has been applied to it. We compress this by storing a structure R i of non-intersecting segments, where each segment corresponds to contiguous i -coordinateswhich have had the same latest update applied to them. We can maintain this as new updatesoccur in ˜ O ( n ) across all updates.Consider a query on a box b . For each dimension i , there will be a range of segments in R i which lie within or intersect b . Let t i be the earliest update time of all such segments.Let t by the maximum among all t i .Consider a segment in R i (for any dimension i ) which lies within or intersects b . Itsupdate is visible at some point in b if and only if its update occurred at or after time t . Theanswer to the query is the maximum value among all such segments. This can be computedin ˜ O (1) using 2D segment trees that support point updates and range maximum queries. (cid:74)(cid:73) Corollary 4.7.
Grid Range ( max , set ) can be solved in ˜ O ( n ( d +1) / ) time. . Lau and A. Ritossa 17 Static Trellised instances
The technique from the previous section does not work on all variants. For instance, considerthe
2D Grid Range ( max , { min , max } ) problem. While the update operations (min andmax) are individually associative, commutative and distribute over the query operation(max), they do not commute with each other. Given that the order of operations mattersgreatly, it is difficult to decompose this into separate one dimensional problems. As ameans for solving such problems, in this section we present a general framework for reducingmultidimensional range problems to Static Trellised instances.
We will first give a high-level overview of our reduction framework, using
2D Grid Range( max , { min , max } ) as an example. For simplicity, we assume these instances are Offline and in two dimensions. We formalise this framework in Subsection 5.2, also generalising ourresults to
Online and higher dimensional settings, accompanied by full details and proofs.
General approach.
Our algorithm operates by partitioning operations into chronologicallycontiguous batches of at most k operations, for some function k of n . Each batch may containboth updates and queries. Let B be a particular batch, and let G B be the state of the gridat the start of B . We will show how to answer the queries within B .The ¯ k = O ( k ) coordinates of B ’s operations partition the grid into a ¯ k × ¯ k overlay grid of overlay regions . Any update or query will concern all points that fall within a 2D rangeof whole overlay regions. Since the update operations min c and max c are monotonicallyincreasing functions for any constant c , it suffices to know the maximum value within eachoverlay region according to G B , to answer the queries of B . We use these maximums asinitial values for a
2D Grid Range ( max , { min , max } ) instance over the overlay grid, whichwe solve by keeping ¯ k
1D Range instances, one for each column, and facilitating operationsin ˜ O ( k ) time, by iterating over each one.It remains to find the maximum value within each overlay region, according to G B . Todo so, consider an alternate partition of the grid into t-regions according to Theorem 4.2. Ineach t-region Y , we will form an instance of Static Trellised 2D Grid Range ( max , { min , max } ) with O ( √ n ) updates; we describe how to do so below. Then, to find themaximum value within an overlay region O , we issue queries to the instances correspondingto the t-regions intersecting O . Forming
Static Trellised instances.
By construction, each t-region Y is affected by upto n whole updates which completely cover Y , and up to √ n partial updates which cover allpoints in a range of rows or range of columns of Y . We can afford to include each partialupdate in our instance, but need to find a succinct way to represent whole updates.To do so, we construct a segment tree T over time. Both min and max operations canbe written as functions of the form f a,b ( x ) = min( a, max( b, x )), which are closed undercomposition. For each range of time [ t , t ] in the tree, we will build a data structure which,given a t-region Y , returns values a and b such that the composition of whole updates to Y occurring during [ t , t ] is f a,b . Such a data structure can be built by performing lazyupdates over the t-region tree. Each update affects O ( √ n ) nodes in the t-region tree, andeach whole update is present in O (log n ) ranges of T . T can then return an f a,b for arbitraryranges of time and t-regions. Each t-region Y has √ n + 1 ranges of time before and after each of its partial updates.For each of these, we query T and translate the returned f a,b into a min and max updatespanning all of Y . Hence, we have formed an instance of Static Trellised 2D GridRange ( max , { min , max } ) with O ( √ n ) updates, as required. This concludes the descriptionof our approach. We now turn our attention to analysing its time complexity. (cid:73) Lemma 5.1.
If there an algorithm for
Static Trellised 2D Grid Range ( max , { min , max } ) running in ˜ O ( n c − γ u ( n u + n q ) γ ) time for some c ≥ and γ ∈ [0 , , then max , { min , max } ) can be solved in ˜ O ( n / c/ ) time. Proof.
We process each of the batches separately, and consider the time taken to answer thequeries within a particular batch B .Using the methods above, in ˜ O ( n / ) time we form an instance of Static Trellised 2DGrid Range ( max , { min , max } ) with n u = O ( √ n ) updates in each of O ( n ) t-regions. Next,we bound the number of queries made to such instances within B . Consider the partition P of the grid produced by refining each t-region by the overlay grid. A query to a StaticTrellised instance is made for each region in P , and the number of these regions and thus,queries, is O (( k + √ n ) ) = O ( k + n ): this is the number of intersections found when theoverlay grid is laid atop the t-regions.For a given t-region Y , let the number of updates and queries made to the instance in Y be n u Y and n q Y , respectively. First, consider the regions y where n q y < √ n . There are O ( n )regions in total, so we spend ˜ O ( n c/ ) time answering queries for these regions.Now consider the regions Υ where n q Υ ≥ √ n . Since n q Υ = Ω( n u ), the running time ofΥ’s Trellised instance is ˜ O ( n c − γ u Υ n γ q Υ ), which is subadditive with respect to n q Υ . Thus, thetotal running time for these regions is maximised when the queries are distributed evenlyamong as many regions as possible. Subject to the constraint on these regions, this maximumis achieved, within a constant multiplicative factor, when there are at most O (( k + n ) / √ n )regions, each with √ n queries. Hence, in this case, the total running time is bounded by˜ O (( k + n ) n ( c − / ). We thus spend time ˜ O ( n / + n c/ + k n ( c − / ) for each of n/k batches. For balance, we choose k = n / , giving a running time of ˜ O ( n / c/ ) overall. (cid:74) It remains to show that we can solve
Static Trellised Grid Range ( max , { min , max } ) efficiently. To do so, we first describe the following ancillary data structure. (cid:73) Lemma 5.2. If S is a (multi)set of integers, let L ( S ) denote the value in S with largestabsolute value.Let S , . . . , S n be multisets of integers. Then, there exists a data structure that canfacilitate any m of the operations:add ( l, r, x ) : for each i ∈ [ l, r ] , add a single copy of x to S i remove ( l, r, x ) : for each i ∈ [ l, r ] , remove a single copy of x from S i ; it is guaranteed thata previous add ( l, r, x ) call has been made, and that a corresponding remove call has notyet been madequery ( l, r, x ) : return max i ∈ [ l,r ] L ( S i ) in O (log n log m ) time for add and remove, and O (log n ) time for query, in the worst case. Proof.
Construct a segment tree, and keep an ordered set S u (e.g. a self-balancing binarysearch tree) at each node u . We maintain a value L ∗ v at each node v , such that at all times, L ∗ v = L ( { L ( S v ) , max( L ∗ ( S a ) , L ∗ ( S b )) } ), where a and b are the immediate children of v .Recall (from Section 2) that any range [ l, r ] can be written as the disjoint union of a setbase([ l, r ]) of O (log n ) segment tree ranges. For the add (remove) operation, add (remove) x from the sets corresponding to the nodes in base([ l, r ]). Each addition or removal from a set . Lau and A. Ritossa 19 takes O (log m ) time. Next, update the L ∗ values of these nodes, and their ancestors. Toanswer a query, it suffices to consider the L ∗ values of the nodes in base([ l, r ]), together withthe L values of their ancestors and use a recurrence identical to what is used to update thetree. (cid:74)(cid:73) Lemma 5.3.
Static Trellised 2D Grid Range ( max , { min , max } ) can be solved in ˜ O ( u ) time. Proof.
We describe how to preprocess a data structure that can answer queries of the form:“Is there a value in a given range [ l , r ] × [ l , r ], greater than or equal to a given value x ?”,where the range and x vary per query. Each query can then be answered with binary search.Observe that for any fixed x , we need only to consider two types of updates: min updateswith value less than x , and max updates with value at least x . If an update occurred at time t , we assign it a label of − t if it is of the first kind, and + t if it is of the second. We say thata label is later than another if its absolute value is larger.Consider a row (column) labelled by the labels of all row (column) updates covering it.Let L r ( L c ) be the latest label of row r (column c ). The answer to the query is then “Yes” ifand only if there is a point ( r, c ) ∈ [ l , r ] × [ l , r ] such that either L r > | L r | > | L c | ,or L c > | L c | > | L r | . However, this is precisely when L r + L c >
0. As r and c can bechosen independently, it suffices to find max r ∈ [ l ,r ] L r and max c ∈ [ l ,r ] L c , and check if theirsum is greater than 0.We show how to find the former value: the same can then be done for the latter. First,create an instance of the Lemma 5.2 data structure with a multiset for each row. We startwith x = −∞ , adding all max updates into the data structure, with their positive labels.Next, we sort all row updates (both min and max) into non-decreasing order by update value,and iterate through this sorted list. When we encounter a min update, we add its label to itscorresponding range of rows, and conversely, when we encounter a max update, we removeits label from its corresponding range of rows. In this way, each update corresponds to anincrease in x . By making this data structure persistent [17], we can facilitate online queriesfor any x with only constant multiplicative overhead in running time. (cid:74) By observing that set z = min z ◦ max z , we obtain the following result. (cid:73) Corollary 5.4.
2D Grid Range ( max , { min , set , max } ) can be solved in ˜ O ( n / ) time. Most of the steps in the approach of the previous subsection are not specific to the updateand query operations in our example. To generalise this approach to fully-online andmultidimensional settings, we introduce the following “partial information” variant of . (cid:73) Definition 5.5 (
1D Partitioned Range ( q, U ) ) . Let A be an integer array of length s and let a < · · · < a ρ = s be a sequence of indices. Let Q be a corresponding sequence of ρ integers, denoting the values q ( A [ a + 1 , a ]) , . . . , q ( A [ a ρ − + 1 , a ρ ]) . Initially, ρ = 1 .Let J be a list of range updates applicable to A in chronological order, initially empty.We write A J for the result of applying the updates of J to A , in order.Given an integer s , and the value of q ( A [1 , s ]) , support the following operations:split ( i, a, q l , q r ) : given i ∈ [0 , ρ − and a i < a < a i +1 , add a to the sequence of indices,and update Q with the knowledge that q ( A [ a i + 1 , a ]) = q l and q ( A [ a + 1 , a i +1 ]) = q r update j ( a l , a r ) : append to J , the update: “for each i ∈ [ a l + 1 , a r ] , set A [ i ] := u j ( A [ i ]) ” query ( a l , a r ) : return q ( A J [ a l + 1 , a r ]) It is guaranteed that every a l or a r provided as input will already be in the sequence. Note that this problem is not solvable for all choices of q and U : one may need to knowthe individual values of A , and not just the result of q over some ranges, to facilitate queriesafter certain types of updates. To see this, consider the case when q = + and U = { max } ,and suppose that we know that q ( A [1 , s ]) = A [1] + . . . + A [ s ] = 3. After we perform max over the entire array, we do not have enough information to facilitate a query for the sum ofthe new array: for instance, we would need to know the number of elements with value atmost 5, and the sum of elements greater than 5.We use the
1D Partitioned Range problem to describe a more general result. (cid:73)
Theorem 5.6.
Suppose U is a set of update functions, and q is a query function, computablein O (1) time. If there is a set ¯ U such that: U ⊆ ¯ U are sets of functions that can be represented and composed in ˜ O (1) space and time,such that the composition of any series of at most n (possibly non-distinct) functions of U results in a function in ¯ U ; There is an algorithm for
1D Partitioned Range ( q, U ) that performs both updatesand queries in ˜ O (1) time; There is an algorithm for
Static Trellised d D Grid Range ( q , ¯ U ) that runs in ˜ O ( n c − γ u ( n u + n q ) γ ) time for some γ ∈ [0 , then Grid Range ( q , U ) can be solved in ˜ O ( n c + d +12 − d ) time. When d = 2 , this is ˜ O ( n / c/ ) time. We dedicate the remainder of this section to proving this result, which will generaliseCorollary 5.4.To prove this, we adopt a batching approach once again, and precompute data structuresat the start of every batch which allow us to facilitate the operations within the batch inand online manner. Throughout the batch, we partition the grid according to the overlaygrid , whose construction is dependent on the operations in the batch. We formalise thisas follows. At any given point in time, we maintain, for each each dimension i , a sortedsequence C i of distinct coordinates in dimension i , with C i, = 0 < C i, < . . . < C i, | C i | = s .This partitions the coordinates of dimension i into disjoint ranges R i = { [ C i,j + 1 , C i,j +1 ] : j ∈ [0 , | C i | − } . In turn, these R i ’s partition the grid into a set of disjoint overlay regions O = { R × . . . × R d : ( R , . . . , R d ) ∈ R × . . . × R d } . We refer to this structure as an overlay grid . At the beginning of a batch, we have each C i = { s } , each R i = { [1 , s ] } and so O = { [1 , s ] d } .When we receive an update or query for a range [ l , r ] × . . . × [ l d , r d ], for each dimension i , we add l i − r i to C i . There are thus at most ¯ k = 2 k + 1 ranges in each dimensionper batch, and each added coordinate splits a range in R i thereby refining the partition O according to a d − initial value of an overlay region R be q ( R ) at the start of the batch. Ouralgorithm consists of two parts: first, maintaining the initial value of each overlay region as O changes; and using these values to support updates and queries within this batch. Maintaining the initial value of overlay regions.
We perform the following precomputationat the start of each batch. Let V be the set of update ranges appearing in prior batches.Separate to the overlay grid, we use the following result of Overmars and Yap [31] to construct . Lau and A. Ritossa 21 a separate partition Y of the grid into t-regions according to V . This theorem is a staticversion of Theorem 4.2, so we also call these t-regions , since they bear the same properties. (cid:73) Lemma 5.7 ([31]) . A partition of the grid into t-regions can be constructed from a collection V of n d -dimensional ranges in O ( n ( d +1) / ) time, with the following properties: There are O ( n d/ ) t-regions; Any hyperplane orthogonal to a coordinate axis intersects at most O ( n ( d − / ) t-regions.Hence, each range of V partially covers at most O ( n ( d − / ) points; Each t-region only contains piles (with respect to itself) in its interior; and Each t-region has at most O ( √ n ) ranges of V partially (but not completely) covering it. Within each t-region Y , we will construct an instance of Static Trellised d D Range( q , U ) , with O ( √ n ) updates. We do so in the same way as in Subsection 5.1: each pileintersecting Y will translate to a trellised update. Updates completely covering Y can besummarised as O ( √ n ) updates by building a segment tree over time. In each segment treerange, we will perform lazy propagation over the t-region tree. This takes ˜ O ( n ( d +1) / ) timein total.Next, define the set of fragments to be F = { Y ∩ O : Y ∈ Y and O ∈ O} . F can beseen as a refinement of our partitions Y and O , and so is itself a partition of the grid intodisjoint ranges. At the beginning of the batch, the set of initial fragments is Y . When acoordinate is added to C , some fragments are split, with each split creating two (smaller)new fragments. We will maintain the initial value of each fragment throughout the batch.Whenever a fragment is created, we issue a query to the Static Trellised instance inits t-region, to determine its value. We associate the initial value of each fragment to anarbitrary point within the fragment, and store these values in a data structure (which wecall the fragment data structure ) that supports updates which add or remove a point in Z d and range queries which ask for the value of q evaluated over points in a range. Such a datastructure can be constructed with a d -dimensional segment tree. After we have determinedthe value of the new fragments formed by a split, we remove the point corresponding to thefragment being split from the fragment data structure, then add to it new points locatedarbitrarily within the newly created fragments, with values corresponding to the respectivefragments. After the fragment data structure is updated, we can issue a range query overit to find the value of each newly created overlay region. Each point update and rangequery over the data structure takes O (log d s ) = O (log d n ) time, since d is a constant and s isbounded by a polynomial in n .This concludes the description of the first part of the algorithm. Before proceeding, let uspause to consider the time complexity of the procedure thus far, for any given batch. Thereare initially O ( n d/ ) t-regions, and thus, initial fragments. Whenever a new coordinate isadded to C , all fragments intersecting the hyperplane H corresponding to this coordinateare split in two. This creates a number of new fragments proportional to the number of suchintersections, which in turn is proportional to the number of intersections between fragmentboundaries and H . This intersection can be either at a boundary of an initial fragment (atmost O ( n ( d − / ) of these, by the second property of Lemma 5.7), or a boundary of an overlayregion (at most O (¯ k d − ) of these, since H cannot intersect a boundary parallel to H ). Hence,the total number of fragments created over the batch is f = O ( n d/ + ¯ k (¯ k d − + n ( d − / )).Using an identical argument to that in the proof of Lemma 5.1, the time complexity offinding the initial value of these fragments is O ( n ( c + d ) / + f n ( c − / ). Supporting updates and queries.
We now use the initial values of overlay regions to supportupdates and queries. Let O = { R × . . . × R d − : ( R , . . . , R d − ) ∈ R × . . . × R d − } be the overlay regions with respect to the first d − O , we willmaintain an instance of
1D Partitioned Range ( q , U ) , with C d as the sequence of indices.Recall that we add the coordinates of each update or query range to C , so we may assumeeach operation affects precisely a range of overlay regions. To answer a query, we iterate overeach region in O in the query range, and query its corresponding Partitioned instance,combining results along the way.When performing an update, we assume that the initial values of each of the currentoverlay regions have already been computed by the first part of the algorithm. Each coordinateadded to C through C d − splits O (¯ k d − ) regions in O into two new regions. For each,we create a new instance of
1D Partitioned Range ( q , U ) so after each, we have aninstance for each region in the new O . For each such region O , we iterate through theupdates, in order, in the batch so far (excluding the present update). For each such update,its range is necessarily either disjoint from O × Z , or its intersection with O is of theform O × R d for some range R d of the d -th dimension. In the latter case, we apply theupdate to R d . Next, for each coordinate x d that has been added to dimension d , we iteratethrough all O (¯ k d − ) Partitioned instances, splitting each with x d . As a result, every rangein every Partitioned instance once again corresponds one-to-one with an overlay region.For each newly created overlay region, we use its value to update the corresponding rangeon the corresponding
Partitioned instance. Finally, we apply the present update to all
Partitioned instances.To complete our proof of Theorem 5.6, it remains to analyse the overall time complexityof our algorithm. Recall that the number of dimensions, d ≥
1, is a constant. We create O (¯ k d − ) overlay regions overall. Each is affected by O ( k ) queries, updates, and splits, so weexpend ˜ O (¯ k d ) time on this data structure over all queries and updates in a batch. Hence, theoverall running time of the algorithm is dominated by the time taken to find the initial valueof each fragment. For balance, we set ¯ k = n ( d +1) / d , so k = (¯ k − / f = O ( n ( d +1) / ),since d ≥
1. This gives an overall running time of ˜ O ( n c + d +12 − d ) over the n/k batches, asrequired. This completes the proof of Theorem 5.6.By extending the Static Trellised algorithm of Lemma 5.3 to multiple dimensions, wecan apply this result to generalise our algorithm for
2D Grid Range ( max , { min , set , max } ) from Corollary 5.4 to higher dimensions. (cid:73) Theorem 5.8.
Grid Range ( max , { min , set , max } ) can be solved in ˜ O ( n ( d +2 d − / d ) time. Proof.
The first condition of Theorem 5.6 is met with ¯ U = { f a,b : | a | , | b | ≤ g } , where g isthe largest absolute value in the input, and f a,b ( x ) = min( a, max( b, x )) as described earlier,which is closed under composition. The second condition is met using a
1D Range ( max , { min , set , max } ) data structure. For each range of A , the data structure maintains a pointplaced arbitrarily within it. By using an ancillary lazy update data structure (as describedin Subsection 5.1), we can determine the composition of all updates to a given point. Whengiven the maximum value v of a new range of A , we are able to set its corresponding point’svalue to the value obtained after all updates have been applied to v . Updates can thenproceed as usual. Finally, it remains to solve d D Static Trellised Grid Range ( max , { min , set , max } ) efficiently.To do so, we extend the Static Trellised algorithm of Lemma 5.3 to multiple dimensions.Let L i,j be the latest label (as defined in Lemma 5.3) covering coordinate j in dimension i .We seek a point ( x , . . . , x d ) for which there is a dimension j such that L j,x j + L j ,x j > j = j . Hence, it suffices to find the latest label L ∗ i in every dimension i and check ifthe sum of the latest and earliest among these is positive. This can be done in ˜ O (1) time per . Lau and A. Ritossa 23 update and ˜ O ( d ) = ˜ O (1) time per query, by keeping a Lemma 5.2 data structure for eachdimension. Hence, for constant d , Static Trellised Range ( max , { min , set , max } ) canbe solved in ˜ O ( n ) time, and the result follows. (cid:74) In the next section, we give two additional applications of this theorem in two dimensions,which establish relationships between counting paths on graphs and
Static Trellised problems with set updates and sum queries. set updates and + queries by counting paths In this section, we apply Theorem 5.6 to give truly subquadratic algorithms for
2D GridRange ( + , set ) and
2D Grid Range ( + , { + , set } ) .
2D Grid Range ( + , set ) by counting inversions The first condition of Theorem 5.6 is met for U = ¯ U = { set } , since set a ◦ set b = set a for any a and b . The second condition can be met solving
1D Partitioned Range (+ , set) with a datastructure for
1D Grid Range (+ , set) over an array of size n . To initialise the structure, foreach a i , we use an update to set the value at a i to q ([ a i , a i +1 − A [ a i ] + . . . + A [ a i +1 − O (log n ) = ˜ O (1) time. Operations each occurin O (log n ) = ˜ O (1) time.Finally, we address the third condition by drawing an equivalence between StaticTrellised 2D Grid Range ( + , set ) and a class of range query problems over arrays. The RangeEqPairsQuery accepts an array of size n as input, and asks for the number of pairsof equal elements within each of q given ranges. Duraj et al. [18] defined this weightedanalogue for counting inversions between pairs of ranges. (cid:73) Definition 6.1 ( Weighted 2RangeInversionsQuery) . Given an integer array A , an in-teger array of weights W , both of length n , and a sequence of q pairs of non-overlapping ranges ([ l , r ] , [ l , r ]) , . . . , ([ l q , r q ] , [ l q , r q ]) , with r i < l i , compute for each pair ([ l , r ] , [ l , r ]) thequantity X i ∈ [ l ,r ] X j ∈ [ l ,r ] A [ i ] >A [ j ] · W [ i ] · W [ j ] . is the problem with the added restriction that every weight is 1. They showed that
RangeEqPairsQuery is equivalent, up to polylogarithmic factors, to , even when the time complexity is expressed as a function ofboth n and q . We extend this equivalence to Static Trellised 2D Grid Range ( + , set ) . (cid:73) Lemma 6.2.
Static Trellised 2D Grid Range ( + , set ) , Weighted 2RangeIn-versionsQuery and all have the same complexity, up to poly-logarithmic factors. This holds even when the queries are presented online, and with thecomplexity measured as a function of two variables, n and q . Proof. ( Static Trellised 2D Grid Range ( + , set ) → Weighted 2RangeInversion-sQuery ). By adding a dummy update with value 0 covering the whole grid at time −
1, wemay assume – without loss of generality – that every row (column) is covered by at leastone row (column) update. Now observe that the value of a given point (after all updates) isequal to the value of the later of the latest row update and the latest column update covering this point’s row and column, respectively. Thus, for each row (column), it suffices to keepthe latest row (column) update covering it. With a sort and sweep, we can process the rowupdates into O ( n ) disjoint row updates which preserve this property. Hence, without lossof generality, we may assume that the row updates are pairwise disjoint, as are the columnupdates. This preprocessing takes O ( n log n ) time.Consider a query over the points [ l , r ] × [ l , r ]. We will show how to compute thecontribution (sum of values) of those points whose value is determined by a row update: wecan treat the contribution from column updates identically, and the sum of these contributionsis the answer to the query. To do so, we form an instance of Weighted 2RangeInver-sionsQuery as follows. Create an array A , whose values are equal to the update time ofeach row update in order, from top to bottom, concatenated with the update time of eachcolumn update in order, from left to right. For the weights of our instance, let the weightcorresponding to the row update with value v over rows [ l , r ] be v × ( r − l + 1), and theweight corresponding to a column update over columns [ l , r ] be ( r − l + 1), irrespectiveof its value. This describes our instance (see Figure 2). We will now describe the queriesmade to this instance.Since row (column) updates are disjoint, there is a contiguous range of row (column)updates in this array which lie entirely inside [ l , r ] ([ l , r ]), whose indices can be foundwith binary search. The contribution of row updates to the points from these ranges is theresult of a query on our instance over the corresponding ranges. O (1) parts of the queryrange may fall within part of a row or column update: the contribution from these can befound by scaling the result of a similarly constructed query.( Weighted 2RangeInversionsQuery → ). We apply thesame trick as Duraj et al. used to reduce multiedge instances of Triangle Counting tosingle edge instances, with a multiplicative factor of O (log n ). This is achieved by convertingeach weight into a binary string of log n bits, and considering each possible pair of bitpositions of the weights corresponding to the inverted elements. Formally, for an integer b in[0 , log n ], let A b be the array A containing only those A [ i ] such that W [ i ] has its b -th bit set.Then for each ( b i , b j ) in [0 , log n ] , we create an instance with A b i concatenated with A b j .For each pair of query ranges [ l , r ] and [ l , r ], we query for [ l , r ] in the first part of thearray, and for [ l , r ] in the second part of the array, adjusting the indices to account for theelements removed from the array.( → Static Trellised 2D Grid Range ( + , set ) ). Weproduce an instance over an n × n grid, and perform (trellised) updates to the grid as follows.For each value v appearing in A , in non-decreasing order: first, for each index i such that A [ i ] = v , set the values in row i to 1, then for each index j such that A [ j ] = v , set thevalues in column j to 0. The order guarantees that after these updates have been performed, A [ i ][ j ] = 1 if and only if A [ i ] > A [ j ]. Each pair of query ranges [ l , r ] and [ l , r ] correspondsto a query over the range [ l , r ] × [ l , r ] in our instance. (cid:74) Duraj et al. [18] gave an ˜ O ( n (2 ω − / ( ω +1) ( n + q ) / ( ω +1) ) time algorithm for RangeEqPair-sQuery , so we obtain the following truly subquadratic time algorithm for
2D Grid Range( + , set ) . (cid:73) Theorem 6.3.
2D Grid Range ( + , set ) can be solved in ˜ O ( n / ω/ ( ω +1) ) = O ( n . ) time. The multivariate running time given in [18] is slightly better than this when q ≤ n , but this simplifiedform suffices our purposes. . Lau and A. Ritossa 25 set at t set at t set at t set at t Rows
Columns A W Columns
Rows A W + Figure 2
Reducing
Static Trellised 2D Grid Range ( + , set ) to Weighted 2RangeIn-versionsQuery . Updates occur at times t through t . Separate instances for row and columncontributions.
2D Grid Range ( + , { + , set } ) by counting 3-paths We will once again employ Theorem 5.6 to give a truly subquadratic time algorithm for + , { + , set } ) . The first condition is met with ¯ U = { + c : | c | ≤ ng } ˙ ∪{ set c : | c | ≤ ng } , where g is the largest value in the input, since + a ◦ set b = set a + b . These canbe represented in O (1) words. The second condition can be met with a
1D Grid Range( + , set ) data structure, constructed similarly as in the previous section. Fulfilling the thirdcondition is the subject of the remainder of this section.We begin by defining the following graph problems. (cid:73) Definition 6.4.
The k -WalkQuery (resp. Simple k -PathQuery ) problem gives, asinput, a simple graph with m edges and O ( m ) vertices, and poses q online queries, eachasking for the number of k -edge walks (resp. simple k -edge paths) between a given pair ofvertices. Duraj et al. [18] proved an equivalence between
RangeEqPairsQuery when n = q , and EdgeTriangleCounting , which gives as input an n -edge, O ( n )-vertex graph, and asks tocount the number of triangles each edge is contained in. When the restriction n = q is relaxed,an equivalence can be drawn with instead. In the same vein, we reduce Static Trellised 2D Grid Range ( + , { + , set } ) to via a generalisation of RangeEqPairsQuery .First, we define a class of problems that generalises and . (cid:73) Definition 6.5.
Let A and W A be an integer arrays of length n , and suppose L , R and W are arrays of length n , such that ≤ L [ i ] ≤ R [ i ] ≤ n for all i . The Weighted- (0 , -IndexedEqPairsQuery problem poses q queries, each accepting a pair of ranges [ l, r ] and [ l , r ] and asking for the sum X i ∈ [ l,r ] X k ∈ [ l ,r ] X j ∈ [ L [ k ] ,R [ k ]] A [ i ]= A [ j ] · W A [ i ] · W A [ j ] · W [ k ] . The
Weighted- (0 , -IndexedInversionsQuery problem poses q queries, each accepting a pair of ranges [ l, r ] and [ l , r ] and asking for the sum X i ∈ [ l,r ] X k ∈ [ l ,r ] X j ∈ [ L [ k ] ,R [ k ]] A [ i ] >A [ j ] · W A [ i ] · W A [ j ] · W [ k ] . The (0 , -IndexedEqPairsQuery and (0 , -IndexedInversionsQuery are the problemswith the added restriction that every entry in W A and W is 1. These problems are so named as the first range in each query [ l, r ] directly indexesthe array A , whereas the second range [ l , r ] indexes an array of ranges (defined by L and R ). Hence, the parameters undergo 0 and 1 levels of indirection, respectively. and are thus (0 , -IndexedEqPairsQuery and (0 , -IndexedInversionsQuery , respectively.Through reductions similar to Duraj et al. [18] and those in the previous subsection,these are equivalent, up to polylogarithmic factors. (cid:73) Lemma 6.6. (0 , -IndexedEqPairsQuery , Weighted- (0 , -IndexedEqPairsQuery , (0 , -IndexedEqPairsQuery and Weighted- (0 , -IndexedEqPairsQuery have thesame running time, up to polylogarithmic factors, even when the running time is expressedas a function of n and q . Proof. ((0 , -IndexedEqPairsQuery → (0 , -IndexedInversionsQuery ) We can easilycompute the number of pairs of entries using prefix sums: this takes time linear in the input,and any algorithm for (0 , -IndexedInversionsQuery takes at least as long. For eachquery, count the number of inversions between the pair of query ranges. By maintaining acopy of A with all entries negated, we can also count the number of inversions in the oppositedirection. Subtracting the answers to these two queries from the number of pairs in eachquery gives the answer.((0 , -IndexedInversionsQuery → (0 , -IndexedEqPairsQuery ) Suppose each ofthe entries of A are z -bit integers. If a > b , then the (padded) binary representations of a and b share some prefix, with a ’s next bit being 1, and b ’s next bit being 0. We reduce to O (log n ) instances of (0 , -IndexedEqPairsQuery : one for each prefix. Let v i ( x ) be the i th bit of x , from most significant, and let p i ( x ) = b x/ z − i c be the i most significant bits of x . For each i = 1 , . . . , z we create an array A i such that: A i [ j ] = p i − ( A [ j ]) if v i ( A [ j ]) = 1 and − ∞ otherwise A i [ n + j ] = p i − ( A [ j ]) if v i ( A [ j ]) = 0 and ∞ otherwiseThe answer to a query over A is the sum of the answers to the queries over each A i . (cid:74) Next, we reduce from
Static Trellised 2D Grid Range ( + , { + , set } ) to theseproblems. (cid:73) Lemma 6.7. If (0 , -WeightedIndexedInversionsQuery can be solved in T ( n, q ) time, then Static Trellised 2D Grid Range ( + , { + , set } ) can be solved in ˜ O ( T ( n, q )) time. Proof.
Observe that the value of any point is equal to its value at the time t of the latestset update covering it, plus the sum of all + updates covering it that occur after t . Hence,we can consider the contributions of set and + updates separately. The contributions ofset updates can be computed in the same way as for Static Trellised 2D Grid Range( + , set ) : namely, by reducing to which is the special case of(0 , -IndexedInversionsQuery with all L [ i ] = R [ i ] = i . . Lau and A. Ritossa 27 It remains to determine the contribution of the + updates. We will show how to determinethe contributions of row + updates only: column + updates can be determined separately inthe same way. We construct an instance of
Weighted (0 , -IndexedInversionsQuery ,over an array A = A A . By compressing the coordinates of the grid according to thecoordinates present in + and set updates, we assume there are n ≤ n + 1 compressed rowsand columns, each with a certain width. Every update covers precisely a range of compressedrows and compressed columns.Let t R [ i ] be the time of the latest set row update covering compressed row i , and let t C [ j ]be the time of the latest set column update covering compressed column j . A will havelength n , with A [ i ] = t C [ i ], and the weight of the i th entry will be the width of compressedcolumn i . Next, form a segment tree over compressed rows. For each range [ l, r ], store a listof all row + updates that decompose to [ l, r ] (as defined in Section 2), sorted in decreasingorder of time. Formally, the updates in this list are precisely those of the form “apply + c to[ l , r ] × Z ”, where [ l, r ] ∈ base([ l , r ]). Build an array of size O ( n log n ) by concatenatingthese lists in any order, while preserving the order within each list, with the value of eachentry equal to the time of the entry. The weight of each entry is equal to the value of the +update. This forms A .To build L and R , we build two smaller lists, L i and R i , for each compressed row i , andconcatenate them in order. For compressed row i , for each segment tree range i is in, binarysearch to find the largest prefix of entries in that range that occur after t R [ i ]. We appendentries in L i and R i corresponding to the range of A that these entries lie in. The weight ofeach entry is equal to the width of compressed row i .Each query corresponds to a range of rows and a range of columns. We decompose therange of rows into a range of whole compressed rows, and at most two fractions of compressedrows, and do the same for columns. Thus, we can write the query answer as the sum ofqueries over ranges of entire compressed rows and columns, scaling the result of a query overa fraction of a compressed row and/or column. For each of these ranges, we form a query bysetting [ l, r ] to correspond to the range of compressed columns in A , and set [ l , r ] to bethe range of entries in L and R corresponding to the range of compressed rows. (cid:74) As a means to solve (0 , -IndexedEqPairsQuery , we prove an equivalence between itand . Queries to must be answered in an online fashion togive an online data structure for our Static Trellised problem. (cid:73)
Lemma 6.8. (0 , -IndexedEqPairsQuery is equivalent to , up to poly-logarithmic factors, even when the running time is expressed as a function of both n and q . Proof. ((0 , -IndexedEqPairsQuery → ) We construct a graph whosevertex set is S ˙ ∪ T ˙ ∪ U ˙ ∪ T , and define these four sets as follows. S is the set of nodes ina segment tree with n leaves, denoting the indices of L and R . T and T are the set ofnodes in a segment tree with n leaves, denoting the n indices of A . U is the set of distinctvalues in A . For the node s representing range [ l, r ] in S , for each i ∈ [ l, r ] and for each I ∈ base([ L [ i ] , R [ i ]]), add an edge from s to the node in T corresponding to I . There are O ( n log n ) such ( s, i ) pairs, and O (log n ) elements in base([ L [ i ] , R [ i ]]) interval, for O ( n log n )edges added.Next, for the node t representing range [ l, r ] in T , for each i ∈ [ l, r ] add an edge from t to the node in U representing the value A [ i ]. Do likewise for T .In all, the graph contains O ( n ) vertices and O ( n log n ) edges. Every 3-edge walk from S to T passes through T and U . To answer a query [ l, r ] and [ l , r ], for each pair of ranges ( I, I ) ∈ base([ l, r ]) × base([ l , r ]), add the number of 3-edge walks between the vertexcorresponding to I in S and the vertex corresponding to I in T .( → (0 , -IndexedEqPairsQuery ) Let A be the concatenation of theadjacency lists of each vertex, in order. Let L [ i ] and R [ i ] correspond to the range in A wherethe i th vertex’s adjacency list is located. For each pair of query vertices s and t , we add aquery for the ranges [ L [ s ] , R [ s ]] and [ L [ t ] , R [ t ]]. The equal elements correspond to verticesthat can be reached in 1 edge from s , and in 2 edges from t . (cid:74) The structure of (0 , -IndexedEqPairsQuery and (0 , -IndexedInversionsQuery can be generalised by adding additional layers of “indexing” arrays L and R to both thefirst and second ranges in each query. Informally, we remark that if there are p layers ofindexing applied to the first range, and q layers to the second range, then we obtain problemsequivalent to ( p + q + 2) -WalkQuery , up to polylogarithmic factors of order p + q + O (1). (cid:73) Theorem 6.9. can be solved in O ( m ω/ (2 ω +1) ( m + q ) (2 ω − / (2 ω +1) ) time. Proof.
Let ∆ > H , M , and L depending if their degree is greater than ∆, greater than √ ∆ but at most ∆, and at most √ ∆,respectively. Next, enumerate all 2-walks abc such that b ∈ M ∪ L , and store a count C ( a, c )of the number of such walks for each ( a, c ) pair (note that possibly a = c ). There are atmost m ∆ of these walks. From this, we can construct matrices C H,H , C H,M,M and C M,L,H ,which are the counts with the restrictions a, c ∈ H ; a ∈ H and b, c ∈ M ; and a ∈ M, b ∈ L and c ∈ H , respectively. This takes time O ( m ∆ + | H || M | ). We also compute the | H | × | M | matrix D = G [ H ] × G [ H, M ] in time O ( ω (2 m/ ∆ , m/ ∆ , m/ √ ∆)), where – through a slightabuse of notation – we write G [ H ] for the adjacency matrix of the subgraph of G induced bythe vertex set H , and write G [ H, M ] for the adjacency matrix of the bipartite graph inducedby the disjoint vertex sets H and M in G .We consider disjoint cases according to which set each of the four vertices in the 3-walkfalls into. For ease of description, we describe them as if they formed a closed 4-walk, byimagining an edge joining the two endpoints (see Figure 3). For each, we describe anynecessary precomputation, and how to answer each query. For each query between vertices u and v , we consider the sets u and v are in, and sum the number of 3-walks in each case toobtain the result. Case 1: All vertices in H . We directly precompute the number of 3-walks between each pairof vertices in G [ H ], using matrix multiplication. This takes time O ( | H | ω ) = O ((2 m/ ∆) ω ). Case 2: Two opposite vertices in M ∪ L . Consider a pair of query vertices u and v , withat least one in M ∪ L . Without loss of generality, if u ∈ M ∪ L but v M ∪ L then, theanswer is P ( u, v ) = P ua ∈ E C ( a, v ). Otherwise, if both u and v are in M ∪ L , the answeris P ( u, v ) + P ( v, u ) − P ua ∈ E : a ∈ M ∪ L C ( a, v ). Each of these sums can be computed in O (∆)time, since d ( u ) ≤ ∆ whenever u ∈ M ∪ L . Hence, this case takes O (( m + q )∆) over allqueries. Case 3: Three vertices in H . We can compute the number of walks of the form H → M ∪ L → H → H by multiplying C H,H by the adjacency matrix of G [ H ] to give the matrix P , taking time O ((2 m/ ∆) ω + m ∆). Let h i be the i th H vertex. If u and v are both in H ,the answer is P uv + P vu .Otherwise, without loss of generality, assume u ∈ H and v ∈ M ∪ L . We can compute thenumber of 2-walks between pairs of vertices in H by squaring the adjacency matrix of G [ H ]. . Lau and A. Ritossa 29 We can then iterate over each vertex h in H adjacent to v , and add the number of 2-walksbetween u and h . Hence, this case takes O (( m/ ∆) ω + ( m + q )∆) time over all queries. Case 4a: Two adjacent vertices in H , two adjacent vertices in M . We multiply C H,M,M by the adjacency matrix of G [ M, H ] to compute the number of walks of the form H → M → M → H between all pairs of vertices in H . This takes time O ( ω (2 m/ ∆ , m/ √ ∆ , m/ ∆)).If u, v ∈ H , we can read off the answer from the matrix.Otherwise, at least one of u and v is in M : suppose, without loss of generality, that u is. If v ∈ H , then the answer can be computed as P um ∈ E : m ∈ M D vm . If v ∈ M , we can findthe answer as P uh ∈ E : h ∈ H D hv . Both cases can be computed in time O ( q ∆) over all queries,since d ( u ) ≤ ∆. Case 4b: Two adjacent vertices in H , one vertex in M , one vertex in L . In precompu-tation, multiply G [ H, M ] by C M,L,H in time O ( ω (2 m/ ∆ , m/ √ ∆ , m/ ∆)) to compute thenumber of walks of the form H → M → L → H between all pairs of vertices in H . If thisforms a matrix F , the answer when u, v ∈ H is F uv + F vu . We also precompute for the casewhen u ∈ M and v ∈ H by multiplying C M,L,H by the adjacency matrix of G [ H ]. In thiscase, the answer can be read off directly.Otherwise, at least one of u and v is in L : suppose, without loss of generality, that u is.If v ∈ H , we can compute the answer as P um ∈ E : m ∈ M D vm . Otherwise, v ∈ M and we cancompute the answer as P uh ∈ E : h ∈ H D hv . This runs in time O ( q √ ∆) over all queries, since d ( u ) ≤ √ ∆. Case 4c: Two adjacent vertices in H , two adjacent vertices in L . First, we precomputethe answer for the case where u, v ∈ H . For each u, v ∈ H , initialise a count Q uv to 0. Foreach edge ab ∈ L , we iterate over each pair of edges ua and bv , such that u, v ∈ H , andincrement Q uv . This takes time O ( m ∆) overall, since each d ( a ) , d ( b ) ≤ √ ∆. We can thenread the answer for each query off Q .Otherwise, at least one of u and v is in L : suppose, without loss of generality, that u is.If v ∈ H , then we can compute the answer as P ul ∈ E : l ∈ L |{ lh ∈ E : h ∈ H, hv ∈ E }| in time O (∆) per query, since d ( u ) , d ( l ) ≤ √ ∆. Otherwise, v ∈ L , and the answer can be computedas P ua ∈ E : a ∈ H |{ bv ∈ E : b ∈ H, ab ∈ E }| in time O (∆) per query, since d ( u ) , d ( v ) ≤ √ ∆.Hence, both cases can be computed in time O ( q ∆) overall.This concludes the description of our algorithm; we now analyse it. The time bottlenecks in ouralgorithm are the O (( m + q )∆) steps, and the rectangular matrix multiplication steps. Let ∆ = m α , for some α ∈ (0 , ω ( n a , n b , n c ) = O ( n a + b + c − (3 − ω ) min { a,b,c } ),the latter steps run in time O ( m − α/ − (3 − ω )(1 − α ) ) = O ( m α/ ω (1 − α ) ). Hence, we obtainan overall time complexity of O (( m + q ) m α + m α/ ω (1 − α ) ).When q ≤ m , we set α = 2( ω − / (2 ω + 1) for a complexity of O ( m (4 ω − / (2 ω +1) ) asobtained by Yuster and Zwick [37]. Otherwise, let β = log q log m , and set α = ω − β )2 ω +1 for balance.We may assume β ≤ ≤ ω if we cache the results to each query, since there are only O ( m )distinct queries. This gives a running time of O ( m ω/ (2 ω +1) q (2 ω − / (2 ω +1) ) when q > m , andthus an O ( m ω/ (2 ω +1) ( m + q ) (2 ω − / (2 ω +1) ) time algorithm overall.Finally, if the value of q is not known during precomputation, we run the algorithm withan initial guess of q = 1, and double our guess when more than q queries have been received.This increases the runtime by at most a constant multiplicative factor. (cid:74) H HHH M ∪ LH ∪ M ∪ LM ∪ LH M ∪ LH ∪ M ∪ LM ∪ LM ∪ L H M ∪ LHH H HHM ∪ L H MMH M HMH M HHM H MLH M LHH L MHH L HHM H LLH L LHH L HHL
Figure 3
The cases in proving Theorem 6.9, in order of consideration. In each case, the topvertex is u , and the left vertex is v , with the vertex pair ( u, v ) given in the query connected by adashed line in the diagram, irrespective of whether they are joined by an edge in the input graph.We omit cases which are identical up to reflection. . Lau and A. Ritossa 31 This gives us an algorithm with the same running time (up to polylogarithmic factors)for
Static Trellised 2D Grid Range ( + , { + , set }) , and thus the unrestricted problem,by Theorem 5.6. (cid:73) Theorem 6.10.
2D Grid Range ( + , { + , set } ) can be solved in time ˜ O ( n / ω − / (4 ω +2) )= O ( n . ) . To conclude this section, we also remark that our algorithm for can beused to solve the analogous problem for simple 3-edge paths, and thus count the number of4-cycles each edge of a graph is included in. (cid:73)
Theorem 6.11. is equivalent to
Simple3PathQuery , and both can besolved in O ( m ω/ (2 ω +1) ( m + q ) (2 ω − / (2 ω +1) ) time. Hence, Edge4CycleCounting (whichasks to count the number of simple 4-cycles that each edge in an m -edge graph is containedin) can be solved in O ( m (4 ω − / (2 ω +1) ) = O ( m . ) time. Proof.
Let u and v be a pair of distinct query vertices, and denote by P ( u, v ) and W ( u, v )the number of simple 3-edge paths, and the number of 3-edge walks between u and v . Suppose uabv is a non-simple 3-edge walk. Since a simple graph contains no self-loops, a = u , a = b and b = v . But since uabv is non-simple, some vertex must be repeated, so either a = v , b = u , or both. Hence, if the edge uv exists in the graph, W ( u, v ) = P ( u, v )+ d ( u )+ d ( v ) − d ( u ) is the degree of u . Otherwise, every 3-walk from u to v is also a simple 3-path, so W ( u, v ) = P ( u, v ).Since the edge uv , if it exists, cannot be part of any simple 3-path from u to v it followsthat Edge4CycleCounting can be solved by counting the number of simple 3-pathsbetween the endpoints of each edge. (cid:74)
Simple- k PathQuery is W [1]-hard for parameter k [20], meaning it is unlikely that an f ( k ) n O (1) time algorithm exists for it. However, k WalkQuery is solvable in O ( n ω log k )on n -vertex graphs by exponentiating the adjacency matrix, so it is unlikely that such anequivalence holds between the two problems for arbitrary k . Range problems over an explicit point set (cid:73)
Lemma 1.2. If
1D Range ( q , U ) on p points can be solved in time T ( p ) per operation and q is computable in O (1) time, then d D Range ( q , U ) can be solved in time O ( p − /d T ( p )) per operation. Proof.
We construct a k d-tree [8] T over the points in P . There is a single point of P ineach of the leaves of T : we assign these labels from 1 to p , according to the order of theirappearance in a preorder traversal of T .We can represent the points in any orthogonal range as those in the disjoint union of O ( p − /d ) subtrees of T [27]. The labels of points within these subtrees correspond to disjointranges of [ p ]. Hence, we can keep an instance of
1D Range ( q , U ) , and perform updatesand queries on the corresponding ranges of labels using this data structure. (cid:74)(cid:73) Theorem 7.1.
If either
2D Range ( + , + ) or
2D Range ( max , max ) can be solved inamortised O ( p / − (cid:15) ) time per update or query, for any (cid:15) > , then the OMv Conjecture isfalse. Proof.
We reduce from OuMv. For each M ij = 1, create a point ( i, j ) in our instance of thedata structure. (
2D Range ( + , + ) ) We maintain that at the beginning of each query, the value of eachpoint is set to 0. We perform the following for each pair of query vectors u and v . For each u i = 1, add 1 to all points in row i in the data structure. After doing this, use the datastructure to query the sum of all points in all columns j where v j = 1. We observe that u T M v = 1 if and only if at least one of these queries was nonzero. After performing thesequeries, we revert all of the updates by subtracting 1 from each row that we updated.(
2D Range ( max , max ) ) We maintain that at the beginning of the i -th query, the valueof each point is at most i −
1. We proceed similarly to
2D Range ( + , + ) , performingupdates with value i to rows and querying to check for the existence of i in columns.In both cases, this uses O ( m ) updates and queries to our data structure in each OuMvquery, and O ( m ) overall. Noticing that we create O ( m ) points overall completes theproof. (cid:74)(cid:73) Theorem 7.2.
If any of
2D Range ( + , set ) ,
2D Range ( max , set ) ,
2D Range( max , + ) ,
2D Range ( + , max ) or
2D Range ( max , min ) can be solved in amortised O ( p / − (cid:15) ) time per update and amortised O ( p − (cid:15) ) time per query, for any (cid:15) > , then theOMv Conjecture is false. Proof.
We reduce from OuMv. For each M ij = 1, create a point ( i, j ) in our instance of thedata structure.(
2D Range ( + , set ) and
2D Range ( max , set ) ) We maintain that at the beginning ofeach query, the value of each point is set to 0. We perform the following for each pair ofquery vectors u and v . For each u i = 0, set all points in row i of the data structure to − j where v j = 0. We observe that u T M v = 1 if and only if atleast one point in the data structure has a value of 0. We can do a single sum or max queryto determine if this is the case. After performing the query, do a single update to reset allthe points’ values to 0 before receiving the next pair of input vectors.(
2D Range ( max , + ) ) We proceed similarly to
2D Range ( max , set ) update, adding − i where u i = 0 and columns j where v j = 0. In a single query we can determineif a 0 exists in the data structure. After performing the query, undo all the updates by adding1 to the points in each row and column which had an update applied to them. (We alsonote that this proof is similar to that of Henzinger et al. [23] for the hardness of Erickson’sproblem, which gives an m × m matrix, and asks, in each operation, to permanently incrementall entries in a specified row or column, and report the maximum value in the new matrix).(
2D Range ( + , max ) ) We maintain that before processing the t th pair of input vectors,the value of each point is at most t −
1. To handle this pair, we perform an update withvalue t to all rows i where u i = 0 and columns j where v j = 0. We observe that u T M v = 1if and only if at least one point in the data structure has a value less than t . We can do asingle sum query to determine if this is the case.(
2D Range ( max , min ) ) We maintain that at the beginning of each query, the value ofeach point is at least − t + 1. We perform an update with value − t to all rows i where u i = 0and columns j where v j = 0. We observe that u T M v = 1 if and only if at least one point inthe data structure has a value greater than − t . We can do a single max query to determineif this is the case.In all cases, our reduction uses O ( m ) updates and O (1) queries to our data structure foreach OuMv query, and thus O ( m ) updates and O ( m ) queries overall. Finally, noticing thatwe create O ( m ) points overall gives the desired conditional lower bounds. (cid:74) . Lau and A. Ritossa 33
2D Grid Range ( max , + ) in ˜ O ( n / ) time, and ˜ O ( n ) space In this section, we provide a fully-online algorithm for
2D Grid Range ( max , + ) that uses O ( n log n ) space. While our complexity is slower than existing results by Chan [10] by apolylogarithmic factor, those results require O ( n / o (1) ) space.We first prove some useful results which are instrumental in the construction of ouralgorithm. (cid:73) Lemma 8.1.
Suppose there is an integer array A of length n , containing some initialarbitrary values, and an oracle that supports range maximum queries over the initial A in O ( t ) time per query. There is a data structure which, with access to such an oracle, andwith no preprocessing, supports online + and max updates to A , and max queries over A , inworst case O ( t log n ) time. Proof.
Define the class of functions f a,b ( x ) = max( x + a, b ), which are each monotonicallyincreasing in x , and are closed under functional composition. As both types of updatescan be written in this form, it suffices to only consider updates of this form. We use lazypropagation over a segment tree over the ranges of A , and at each node, store constants a and b , representing f a,b which is the sequential composition of a consecutive range of updatesthat apply to the range represented by this node. At the “deepest” nodes we have created,we make calls to the oracle to determine the maximum value in the range, and use this topopulate our internal segment tree values. (cid:74) Recall that in
Static (Grid) Range problems, all updates occur before all queries. (cid:73)
Lemma 8.2.
There exists a data structure which solves
Static 2D Grid Range ( max , + ) using O ( n u log n u ) time in preprocessing and O (log n u ) time per query and O ( n u log n u ) space. Proof.
By applying coordinate compression, it suffices to consider only O ( n u ) interesting x and y coordinates.We will build a kind of 2D segment tree, which is a segment tree over x coordinates.Suppose there is a node in the tree representing the range [ x , x ]. For this node, we willbuild a data structure capable of answering queries of the form: given a range of y coordinates[ y , y ], what is the maximum value in A [ x , x ][ y , y ] (after all updates have been applied)?We will build these structures simultaneously, maintaining a sweepline over x , representinga column of A . Break down each range + update into two events: one which adds and anotherwhich subtracts a value from a range of y coordinates in the sweepline. Using Lemma 8.1,the supporting of updates in the sweepline can be done in O (log n u ) time, and range maxqueries at any point in the sweepline can be supported in O (log n u ) time: the range maxvalue of the initial array is always 0, so the oracle can always return in O (1) time.We construct the data structure for the range [ x , x ] when the sweepline has processed allevents ending at x −
1, but before processing any events starting at x . Our data structurewill represent an array B such that B [ y ] = max( A [ x , x ][ y, y ]), and support range queriesover B . Equivalently, we can define B by starting with an array equal to the value of thesweepline after x = x −
1, applying all the events in the [ x , x ] range, in order, and have B [ y ] be the historical maximum value of the sweepline in row y , at any point in this range.This can be computed with a sweepline over the events in the range, and using Lemma 8.1together the idea of [25], where the oracle is the sweepline at this point in time. If there are n u events in [ x , x ], this takes O ( n u log n u log n u ) time. Each event appears in O (log n u )ranges [ x , x ], so these data structures take O ( n u log n u ) time to construct, overall. Queries are answered in the usual 2D segment tree manner, which takes O (log n u ) time,for O (log n u ) time overall. (cid:74) We are now ready to prove our result. (cid:73)
Theorem 1.5.
2D Grid Range ( max , + ) can be solved in ˜ O ( n / ) time, and O ( n log n ) space. Proof.
Let k be a positive integer, and split the updates and queries into n/k batcheschronologically, such that each contains at most k operations. At the beginning of eachbatch, we build a data structure D according to Lemma 8.2 from all the updates appearingin previous batches.As in Subsection 5.1, we split the grid into compressed-columns, based on interestingcoordinates in the input so far. For each compressed-column F , spanning [ x , x ], we maintaina data structure which, throughout the operations in this batch, supports queries for themaximum value of a point in F , among a given range of rows. Such a data structure can beconstructed by applying Lemma 8.1 over the rows of F , using D as the oracle. When wereceive new coordinates, we split compressed-columns in the same way as in Subsection 5.2,reapplying updates. Each such query takes ˜ O (1) time, so together, queries take ˜ O ( k ) timeover the batch.Factoring in the construction time of D , the overall time complexity of this algorithm is˜ O (( n/k )( n + k )). Setting k = √ n gives the desired complexity. (cid:74) We have shown that
2D Grid Range ( max , { min , set , max } ) and
2D Grid Range ( max , + ) can both be solved in truly subquadratic time, and found Ω( n − o (1) ) time conditionallower bounds for
2D Grid Range ( max , { + , min } ) . We also observe that
2D Grid Range( max , { + , set } ) reduces to
2D Grid Range ( max , { + , max } ) , since set c = max c ◦ + −∞ .Hence, the remaining maximum query variants in B are each at least as hard as
2D GridRange ( max , { + , set } ) . (cid:73) Open Problem 9.1.
Can (Offline) 2D Grid Range ( max , { + , set } ) be solved in trulysubquadratic time? Among variants supporting sum queries, we gave Ω( n − o (1) ) time conditional lower boundsfor
2D Grid Range ( + , { + , max } ) . Using the identity set c = max c ◦ + −∞ once again, onecan see that this is at least as hard as
2D Grid Range ( + , { + , set } ) , which we solvedin O ( n . ) time, using Theorem 5.6. Another problem easier than
2D Grid Range ( + , { + , max } ) is simply
2D Grid Range ( + , max ) , which does not support + updates. Thisis also the easiest among the remaining sum query variants in B .We make several comments regarding the hardness of
2D Grid Range ( + , max ) . First,we observe that it is also at least as hard as its set “counterpart”, since any instance of GridRange ( + , set ) can be simulated with two instances of Grid Range ( + , max ) . (cid:73) Lemma 9.2.
Grid Range ( + , set ) can be solved in the same time as Grid Range ( + , max ) . We solved
2D Grid Range ( + , set ) in O ( n . ) time using Theorem 5.6. However,it is easy to see that there is no solution to
1D Partitioned Range (+ , max), which isrequired as a precondition of Theorem 5.6: it is simply not enough to know the sum of arange of points. One might instead determine for each overlay region O , query range R and . Lau and A. Ritossa 35 max c update: the number of points in O ∩ R with value at most c and the sum of points in O ∩ R with value greater than c . This requires O ( k ) values returned per batch, limitingprecomputation to O ( n / − (cid:15) ) time, for some (cid:15) >
0, if a truly subquadratic time algorithmoverall is desired. This cannot be achieved with a direct application of Theorem 5.6, sincethere are O ( n / ) updates across the t-regions. (cid:73) Open Problem 9.3.
Can (Offline) 2D Grid Range ( + , max ) be solved in trulysubquadratic time? Finally, our Ω( n / − o (1) ) conditional lower bounds do not match the upper bounds wegave for
2D Grid Range ( + , { set , + } ) ,
2D Grid Range ( + , set ) and
2D Grid Range( max , { min , set , max } ) . We ask if the gap can be closed for these problems to see if thereexists an in-between complexity class of
2D Grid Range problems. In particular, this wouldbe resolved in the affirmative if Theorem 5.6 is a tight reduction for any of these problems. (cid:73)
Open Problem 9.4.
Are there any
2D Grid Range problems solvable in O ( n − (cid:15) ) time,for some (cid:15) > , but require Ω( n / − o (1) ) time? We have studied just a small subset of
Range and
Grid Range problems in thiswork. Additional update or query operations, such as addition modulo a prime, can alsobe considered. Many existing variants of range searching (see [5]) can also be adapted tothese problem classes. In particular, we have not investigated problems which deal withdata points that have a “colour” or “category”, and ask for the number of distinct coloursin a range. These may be of particular interest, as set updates could be used to facilitatechanging the colour of several data points at the same time.
References Amir Abboud, Virginia Vassilevska Williams, and Huacheng Yu. Matching triangles andbasing hardness on an extremely popular conjecture. In Rocco A. Servedio and RonittRubinfeld, editors,
Proceedings of the Forty-Seventh Annual ACM on Symposium on Theoryof Computing, STOC 2015, Portland, OR, USA, June 14-17, 2015 , pages 41–50. ACM, 2015. doi:10.1145/2746539.2746594 . Peyman Afshani. A new lower bound for semigroup orthogonal range searching. In GillBarequet and Yusu Wang, editors, , volume 129 of
LIPIcs , pages 3:1–3:14.Schloss Dagstuhl - Leibniz-Zentrum für Informatik, 2019. doi:10.4230/LIPIcs.SoCG.2019.3 . Peyman Afshani, Lars Arge, and Kasper Dalgaard Larsen. Orthogonal range reporting inthree and higher dimensions. In , pages 149–158. IEEEComputer Society, 2009. doi:10.1109/FOCS.2009.58 . Peyman Afshani, Lars Arge, and Kasper Dalgaard Larsen. Orthogonal range reporting: querylower bounds, optimal structures in 3-d, and higher-dimensional improvements. In David G.Kirkpatrick and Joseph S. B. Mitchell, editors,
Proceedings of the 26th ACM Symposium onComputational Geometry, Snowbird, Utah, USA, June 13-16, 2010 , pages 240–246. ACM,2010. doi:10.1145/1810959.1811001 . Pankaj K. Agarwal. Range searching. In Jacob E. Goodman and Joseph O’Rourke, editors,
Handbook of Discrete and Computational Geometry, Second Edition , pages 809–837. Chapmanand Hall/CRC, 2004. doi:10.1201/9781420035315.ch36 . Josh Alman and Virginia Vassilevska Williams. A refined laser method and faster matrixmultiplication.
CoRR , abs/2010.05846, 2020. URL: https://arxiv.org/abs/2010.05846 , arXiv:2010.05846 . Arturs Backurs, Nishanth Dikkala, and Christos Tzamos. Tight hardness results for maximumweight rectangles. In Ioannis Chatzigiannakis, Michael Mitzenmacher, Yuval Rabani, andDavide Sangiorgi, editors, , volume 55 of
LIPIcs , pages 81:1–81:13. Schloss Dagstuhl - Leibniz-Zentrum für Informatik, 2016. doi:10.4230/LIPIcs.ICALP.2016.81 . Jon Louis Bentley. Multidimensional binary search trees used for associative searching.
Commun. ACM , 18(9):509–517, 1975. doi:10.1145/361002.361007 . Jon Louis Bentley. Solutions to klee’s rectangle problems.
Unpublished manuscript , pages282–300, 1977. Timothy M. Chan. A (slightly) faster algorithm for klee’s measure problem. In MoniqueTeillaud, editor,
Proceedings of the 24th ACM Symposium on Computational Geometry, CollegePark, MD, USA, June 9-11, 2008 , pages 94–100. ACM, 2008. doi:10.1145/1377676.1377693 . Timothy M. Chan. Klee’s measure problem made easy. In ,pages 410–419. IEEE Computer Society, 2013. doi:10.1109/FOCS.2013.51 . Timothy M. Chan. Orthogonal range searching in moderate dimensions: k-d trees andrange trees strike back. In Boris Aronov and Matthew J. Katz, editors, ,volume 77 of
LIPIcs , pages 27:1–27:15. Schloss Dagstuhl - Leibniz-Zentrum für Informatik,2017. doi:10.4230/LIPIcs.SoCG.2017.27 . Timothy M. Chan, Kasper Green Larsen, and Mihai Pˇatraşcu. Orthogonal range searching onthe ram, revisited. In Ferran Hurtado and Marc J. van Kreveld, editors,
Proceedings of the27th ACM Symposium on Computational Geometry, Paris, France, June 13-15, 2011 , pages1–10. ACM, 2011. doi:10.1145/1998196.1998198 . Timothy M. Chan, Yakov Nekrich, and Michiel H. M. Smid. Orthogonal range reportingand rectangle stabbing for fat rectangles. In Zachary Friggstad, Jörg-Rüdiger Sack, andMohammad R. Salavatipour, editors,
Algorithms and Data Structures - 16th InternationalSymposium, WADS 2019, Edmonton, AB, Canada, August 5-7, 2019, Proceedings , volume11646 of
Lecture Notes in Computer Science , pages 283–295. Springer, 2019. URL: https://doi.org/10.1007/978-3-030-24766-9_21 , doi:10.1007/978-3-030-24766-9\_21 . Timothy M. Chan and Konstantinos Tsakalidis. Dynamic orthogonal range searching onthe ram, revisited. In Boris Aronov and Matthew J. Katz, editors, ,volume 77 of
LIPIcs , pages 28:1–28:13. Schloss Dagstuhl - Leibniz-Zentrum für Informatik,2017. doi:10.4230/LIPIcs.SoCG.2017.28 . Bernard Chazelle. A functional approach to data structures and its use in multidimensionalsearching.
SIAM J. Comput. , 17(3):427–462, 1988. doi:10.1137/0217026 . James R. Driscoll, Neil Sarnak, Daniel Dominic Sleator, and Robert Endre Tarjan. Makingdata structures persistent. In Juris Hartmanis, editor,
Proceedings of the 18th Annual ACMSymposium on Theory of Computing, May 28-30, 1986, Berkeley, California, USA , pages109–121. ACM, 1986. doi:10.1145/12130.12142 . Lech Duraj, Krzysztof Kleiner, Adam Polak, and Virginia Vassilevska Williams. Equivalencesbetween triangle and range query problems. In Shuchi Chawla, editor,
Proceedings of the2020 ACM-SIAM Symposium on Discrete Algorithms, SODA 2020, Salt Lake City, UT, USA,January 5-8, 2020 , pages 30–47. SIAM, 2020. doi:10.1137/1.9781611975994.3 . Arash Farzan, J. Ian Munro, and Rajeev Raman. Succinct indices for range queries withapplications to orthogonal range maxima. In Artur Czumaj, Kurt Mehlhorn, Andrew M. Pitts,and Roger Wattenhofer, editors,
Automata, Languages, and Programming - 39th InternationalColloquium, ICALP 2012, Warwick, UK, July 9-13, 2012, Proceedings, Part I , volume 7391 of
Lecture Notes in Computer Science , pages 327–338. Springer, 2012. URL: https://doi.org/10.1007/978-3-642-31594-7_28 , doi:10.1007/978-3-642-31594-7\_28 . . Lau and A. Ritossa 37 Jörg Flum and Martin Grohe. The parameterized complexity of counting problems.
SIAM J.Comput. , 33(4):892–922, 2004. doi:10.1137/S0097539703427203 . Jim Gray, Adam Bosworth, Andrew Layman, and Hamid Pirahesh. Data cube: A relationalaggregation operator generalizing group-by, cross-tab, and sub-total. In Stanley Y. W. Su,editor,
Proceedings of the Twelfth International Conference on Data Engineering, February 26- March 1, 1996, New Orleans, Louisiana, USA , pages 152–159. IEEE Computer Society, 1996. doi:10.1109/ICDE.1996.492099 . Meng He and J. Ian Munro. Space efficient data structures for dynamic orthogonal rangecounting.
Comput. Geom. , 47(2):268–281, 2014. doi:10.1016/j.comgeo.2013.08.007 . Monika Henzinger, Sebastian Krinninger, Danupon Nanongkai, and Thatchaphol Saranurak.Unifying and strengthening hardness for dynamic problems via the online matrix-vectormultiplication conjecture. In Rocco A. Servedio and Ronitt Rubinfeld, editors,
Proceedings ofthe Forty-Seventh Annual ACM on Symposium on Theory of Computing, STOC 2015, Portland,OR, USA, June 14-17, 2015 , pages 21–30. ACM, 2015. doi:10.1145/2746539.2746609 . Nabil Ibtehaz, M. Kaykobad, and M. Sohel Rahman. Multidimensional segment trees cando range queries and updates in logarithmic time.
CoRR , abs/1811.01226, 2018. URL: http://arxiv.org/abs/1811.01226 , arXiv:1811.01226 . Ruyi Ji. Interval maximum value operation and historical maximum value problem.
Inform-atics Olympiad China National Team Candidates Essay Collection , 2016. Article written inChinese. The author (Ji) has written a blog post in English, outlining the main techniques:https://codeforces.com/blog/entry/57319. Tuukka Korhonen. On multidimensional range queries. Technical report, University of Helsinki,2019. D. T. Lee and C. K. Wong. Worst-case analysis for region and partial region searches inmultidimensional binary search trees and balanced quad trees.
Acta Informatica , 9:23–29,1977. doi:10.1007/BF00263763 . George S. Lueker. A data structure for orthogonal range queries. In , pages28–34. IEEE Computer Society, 1978. doi:10.1109/SFCS.1978.1 . Yakov Nekrich. New data structures for orthogonal range reporting and range minimaqueries.
CoRR , abs/2007.11094, 2020. URL: https://arxiv.org/abs/2007.11094 , arXiv:2007.11094 . Yuzuru Okajima and Kouichi Maruyama. Faster linear-space orthogonal range searchingin arbitrary dimensions. In Ulrik Brandes and David Eppstein, editors,
Proceedings of theSeventeenth Workshop on Algorithm Engineering and Experiments, ALENEX 2015, San Diego,CA, USA, January 5, 2015 , pages 82–93. SIAM, 2015. doi:10.1137/1.9781611973754.8 . Mark H. Overmars and Chee-Keng Yap. New upper bounds in klee’s measure problem.
SIAMJ. Comput. , 20(6):1034–1045, 1991. doi:10.1137/0220065 . Chung Keung Poon. Dynamic orthogonal range queries in OLAP.
Theor. Comput. Sci. ,296(3):487–510, 2003. doi:10.1016/S0304-3975(02)00741-7 . Virginia Vassilevska Williams and Ryan Williams. Subcubic equivalences between path, matrixand triangle problems. In , pages 645–654. IEEE ComputerSociety, 2010. doi:10.1109/FOCS.2010.67 . Dan E. Willard and George S. Lueker. Adding range restriction capability to dynamic datastructures.
J. ACM , 32(3):597–617, 1985. doi:10.1145/3828.3839 . Ryan Williams. A new algorithm for optimal 2-constraint satisfaction and its implications.
Theor. Comput. Sci. , 348(2-3):357–365, 2005. doi:10.1016/j.tcs.2005.09.023 . Virginia Vassilevska Williams. Hardness of easy problems: Basing hardness on popularconjectures such as the strong exponential time hypothesis (invited talk). In Thore Husfeldt andIyad A. Kanj, editors,
IPEC 2015, September 16-18, 2015, Patras, Greece , volume 43 of
LIPIcs , pages 17–29. SchlossDagstuhl - Leibniz-Zentrum für Informatik, 2015. doi:10.4230/LIPIcs.IPEC.2015.17 . Raphael Yuster and Uri Zwick. Detecting short directed cycles using rectangular matrixmultiplication and dynamic programming. In J. Ian Munro, editor,
Proceedings of the FifteenthAnnual ACM-SIAM Symposium on Discrete Algorithms, SODA 2004, New Orleans, Louisiana,USA, January 11-14, 2004 , pages 254–260. SIAM, 2004. URL: http://dl.acm.org/citation.cfm?id=982792.982828http://dl.acm.org/citation.cfm?id=982792.982828