Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3313564
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T22:07:48+00:00 2026-05-17T22:07:48+00:00

I am working with some spreadsheet data and I have a set of cell

  • 0

I am working with some spreadsheet data and I have a set of cell regions that are of arbitrary bounds. Given any cell, what is the fastest way to determine the subset of regions which contain the cell?

Currently, the best I have is to sort the regions with the primary sort field being the region’s starting row index, followed by its ending row index, starting column index, and then ending column index. When I want to search based on a given cell, I binary search to the first region whose starting row index is after the cell’s row index and then I check all regions before that one to see if they contain the cell, but this is too slow.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-17T22:07:48+00:00Added an answer on May 17, 2026 at 10:07 pm

    Based on some Googling, this is an example of the two dimensional point enclosure searching problem, or the “stabbing problem”. See:

    http://www.cs.nthu.edu.tw/~wkhon/ds/ds10/tutorial/tutorial6.pdf

    of here (starting at p.21/52):

    http://www.cs.brown.edu/courses/cs252/misc/slides/orthsearch.pdf

    The key data structure involved is the segment tree:

    http://en.wikipedia.org/wiki/Segment_tree

    For the 2-D case, it looks like you can build a segment tree containing segment trees and get O(log^2(n)) query complexity. (I think your current solution is O(n) since on average you’ll just exclude half of your regions with your binary search.)

    However, you said “spreadsheet”, which means you’ve probably got a relatively small area to work with. More importantly, you’ve got integer coordinates. And you said “fastest”, which means you’re probably willing to trade space and setup time for a faster query.

    You didn’t say which spreadsheet, but the code below is a wildly-inefficient, but dirt-simple, brute-force Excel/VBA implementation of a 2-D lookup table that, once set up, has O(1) query complexity:

    Public Sub brutishButShort()
        Dim posns(1 To 65536, 1 To 256) As Collection
    
        Dim regions As Collection
        Set regions = New Collection
    
        Call regions.Add([q42:z99])
        Call regions.Add([a1:s100])
        Call regions.Add([r45])
    
        Dim rng As Range
        Dim cell As Range
        Dim r As Long
        Dim c As Long
    
        For Each rng In regions
            For Each cell In rng
                r = cell.Row
                c = cell.Column
    
                If posns(r, c) Is Nothing Then
                    Set posns(r, c) = New Collection
                End If
    
                Call posns(r, c).Add(rng)
            Next cell
        Next rng
    
        Dim query As Range
        Set query = [r45]
    
        If Not posns(query.Row, query.Column) Is Nothing Then
            Dim result As Range
            For Each result In posns(query.Row, query.Column)
                Debug.Print result.address
            Next result
        End If
    End Sub
    

    If you have a larger grid to worry about or regions that are large relative to the grid, you can save a ton of space and setup time by using two 1-D lookup tables instead. However, then you have two lookups, plus a need to take the intersection of the two resulting sets.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have been working on some legacy C++ code that uses variable length structures
I have some data in CSV format that I want to pull into an
I have an excel spreadsheet that contains some formulas to calculate ROI for an
I'm working on some code that generates an Excel spreadsheet server-side and then downloads
I've been working intermittently on an Excel spreadsheet for a customer that does some
I have some working code that does amplitude modulation and plots it. However I'm
I have some working code that I altered but I can't seem to get
Working on some tweaks for a build script, I noticed that the output from
I am working with some CSS that is poorly written to say the least.
I'm working on some code that uses the System.Diagnostics.Trace class and I'm wondering how

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.