We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
  • HackerRank Home

    HackerRank

  • |
  • Prepare
  • Certify
  • Compete
  • Apply
  • Hiring developers?
  1. Prepare
  2. Algorithms
  3. Graph Theory
  4. Quadrant Queries
  5. Discussions

Quadrant Queries

Problem
Submissions
Leaderboard
Discussions
Topics

    You are viewing a single comment's thread. Return to all comments →

  • kstauffer
    3 years ago+ 1 comment

    Interval trees are pretty complicated. I find arrays are easier, and a lot faster. If n is the number of points, consider a left-complete binary tree with n leaves. You can represent that in an array of size 2^(1 + ceil(log2(n))). Array element 1 is the root, and the children of element i are at 2*i and 2*i+1. At each element store the 4-long vector (for each quadrant) of counts of points in the subtree of that element. Each leaf node contains exactly one point. At every internal node, also store a "flip" parameter that says how that count needs to be permuted to reflect the X/Y operations that have been done on that subtree. When an X or Y query is made, traverse the tree and update the flip parameter of any node that is entirely contained in the query but whose parent is not contained.

    0|
    Permalink
  • Blog
  • Scoring
  • Environment
  • FAQ
  • About Us
  • Support
  • Careers
  • Terms Of Service
  • Privacy Policy