Skip to main content

Dynamic segment tree with Python bindings

Project description

Dynamic Segment Tree

A high-performance dynamic segment tree implemented in C++ with Python bindings using pybind11.
Supports efficient range queries and updates on large arrays without reserving memory upfront.

About Dynamic Segment Tree

This package implements a dynamic segment tree data structure for efficient use in Python programs.

What is a dynamic segment tree?

  • A segment tree is a powerful data structure used to perform range queries and updates efficiently on an array.
  • The dynamic variant does not pre-allocate memory for the entire array size, but instead creates nodes only when needed, saving memory.
  • This makes it practical for very large arrays (e.g., length 10 million or more) where many elements are initially zero or default.

What problem does this solve?

  • Suppose you have M categories and an array of length N.
  • At each position, exactly one category is assigned.
  • You want to:
    • Update the category at any index dynamically.
    • Query how many elements of a given category exist in any range [L, R].
  • This package supports exactly that efficiently, using minimal memory.

Main Functions

DynamicSegTree(N)

  • Creates a dynamic segment tree for an array of size N.
  • Initially, all positions are empty (or a default invalid category).

build(arr)

  • Builds the tree from a given list/array arr of categories.
  • This initializes the data structure with known values.

set(pos, category)

  • Updates the element at position pos to belong to category.
  • If the position already had a category, it updates it accordingly.

get(pos)

  • Returns the current category at position pos.
  • Returns -1 if the position is unset or empty.

query(l, r, category)

  • Returns the count of elements belonging to category in the range [l, r].
  • This is the main query function and works efficiently even for large N.

Why Use This Package?

  • Designed for very large arrays where a classical segment tree would be memory-heavy.
  • Provides a Python interface for easy integration into your projects.
  • Efficiently supports queries and updates with logarithmic time complexity.
  • Ideal for problems involving category counts over large ranges with sparse updates.

Installation

pip install dynamic-segment-tree

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

dynamic_segment_tree-0.1.0.tar.gz (5.0 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

dynamic_segment_tree-0.1.0-cp313-cp313-macosx_13_0_arm64.whl (94.7 kB view details)

Uploaded CPython 3.13macOS 13.0+ ARM64

File details

Details for the file dynamic_segment_tree-0.1.0.tar.gz.

File metadata

  • Download URL: dynamic_segment_tree-0.1.0.tar.gz
  • Upload date:
  • Size: 5.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.3

File hashes

Hashes for dynamic_segment_tree-0.1.0.tar.gz
Algorithm Hash digest
SHA256 d173e07a3504de9ed092c1eab6acb33b0baade060fff5990773a8cfb1bc77e47
MD5 d67e77a573ea5806b31fc009a6238d43
BLAKE2b-256 25437fbb3ad4952680c79b3a3ff3ca3e62af7fb58cb412da07f0cf0b0b810af0

See more details on using hashes here.

File details

Details for the file dynamic_segment_tree-0.1.0-cp313-cp313-macosx_13_0_arm64.whl.

File metadata

File hashes

Hashes for dynamic_segment_tree-0.1.0-cp313-cp313-macosx_13_0_arm64.whl
Algorithm Hash digest
SHA256 9470a99ddd4a0d30ee9a5241894c4e0b31adc671fbcced3b77ed04d6731e0472
MD5 f5b45a740fe571b92d1e2277778c5b6e
BLAKE2b-256 e0aca79b1eb1c693d5ce1e7d81d0309ca837c395dc9e6c91dc15337280468d5d

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page