Segment Tree Implementation for Range Queries and Updates
The segment tree is constructed recursively. Each node tracks its segment boundaries [left, right]. Leaf nodes correspond to individual array elements, while enternal nodes store the sum of their children.
struct SegmentTree {
int left[MAX_N * 4], right[MAX_N * 4];
long long value[MAX_N * 4], lazy[MAX_N * 4];
void build(int l, int ...
Posted on Thu, 18 Jun 2026 17:26:51 +0000 by hkothari
Heavy-Light Decomposition for Tree Operations
Heavy-Light Decomposition (HLD) is a powerful technique that partitions a rooted tree into a set of disjoint paths (chains). This transformation allows for efficient range-based operations (like updates and queries) on tree structures by mapping the nodes into a linear array using a Segment Tree.
Core Definitions
Heavy Edge: An edge connecting ...
Posted on Sun, 07 Jun 2026 17:42:18 +0000 by asunsha
NOIP Simulation Contest Solutions: flandre, meirin, sakuya, scarlet
flandre
The optimal selected sequence must be a contiguous suffix in the sorted array of fireworks by their "real effect" values. This is because any gap in the selection can be filled to increase the total "perceived effect".
After sorting the fireworks by real effect, we compute for each position i the contribution b[i] as ...
Posted on Fri, 05 Jun 2026 18:27:24 +0000 by osnewbie2004
Algorithm Solutions for Competitive Programming Problems
Grid Pattern Filling Algorithm
Problem Statement
Given an n×n grid containing '.' and '#' characters, determine if all '.' positions can be filled with cross-shaped patterns.
Solution Approach
Iterate through each cell and identify positions where cross patterns can be placed without overlapping.
#include <iostream>
#include <vector> ...
Posted on Mon, 01 Jun 2026 16:30:22 +0000 by aashcool198
Segment Tree with Lazy Propagation in Java
A segment tree supports efficient range updates and queries over an array. The following implementation uses an explicit tree structure with lazy propagation.
Node Representation
Each node stores its interval boundaries, the aggregated sum, and a pending lazy value that needs to be propagated to its childran before any further traversal.
static ...
Posted on Sat, 30 May 2026 00:47:09 +0000 by bran
Advanced Data Structures for Competitive Programming Challenges
Problem 1: Subtree Color Dominance
This problem requires finding the most frequent color in each subtree. A naive approach uses Mo's algorithm on the Euler tour of the tree combined with a segment tree tracking color frequencies. The Euler tour flattens the tree into an array where each subtree corresponds to a contiguous range.
However, the op ...
Posted on Fri, 29 May 2026 20:36:42 +0000 by rahish
Optimizing Array Pair Products for Maximum Sum
Problem Analysis and Solution
Given two arrays, the goal is to pair elements from each array to maximize the sum of their products. Since positive multiplied by positive yields positive, and negative multiplied by negative also yields positive, we can seperate both arrays into positive and negative components.
The optimal strategy is to pair la ...
Posted on Fri, 15 May 2026 16:12:07 +0000 by iBuddy
Introduction to Persistent Segment Trees
A common question arises: what distinguishes a Chairman Tree from a Persistent Segment Tree? The answer lies in their definitions—a Chairman Tree is specifically a persistent weighted segment tree, which is a specialized application of the persistent segment tree structure.
Definition
A persistent segment tree is a data structure that preserves ...
Posted on Fri, 15 May 2026 12:19:01 +0000 by Ind007
Algorithmic Problem Solutions with Data Structures
Fountain Construction Optimization
Three construction strategies exist: using only coins, only diamonds, or a combination. Iterate through coin-based fountains while tracking maximum aesthetic value achievable with remaining coins via a segment tree. Similarly process diamond-based options and update combined solutions during iteration.
#includ ...
Posted on Thu, 14 May 2026 04:23:42 +0000 by adamwhiles
Segment Tree with Lazy Propagation for Range Updates
Consider an array of (n) integers (a_1, a_2, \cdots, a_n). Two types of operations are supported:
Add a value (d) to all elements from index (l) to (r).
Query the maximum element within the range ([l, r]).
To efficiently handle these operations, we use a segment tree enhanced with lazy propagation. This technique introduces a lazy tag to defe ...
Posted on Fri, 08 May 2026 08:30:24 +0000 by Zaxnyd