Essential Algorithm Templates for Competitive Programming

Data Structures Segment Tree #include <iostream> #include <vector> #include <algorithm> using namespace std; typedef long long ll; const int MAXN = 100010; int n, m; vector<ll> arr; vector<ll> tree; vector<ll> lazy; inline ll read() { ll x = 0, f = 1; char ch = getchar(); while (ch < '0' || ...

Posted on Tue, 28 Jul 2026 16:49:08 +0000 by Daggeth

Chtholly Tree and Color Segment Amortization

Overview The Chtholly Tree, also known as ODT (Old Driver Tree), gained popularity through Codeforces problem 896C. It's crucial to understand that this approach is fundamentally based on color segment amortization for random data, rather than being a strict data structure. The operations described below represent specific implementations of th ...

Posted on Fri, 24 Jul 2026 16:28:49 +0000 by Renich

Persistent Segment Trees: Path Copying for Historical Range Queries

A persistent segment tree maintains a complete history of all structural modifications applied to the data structure. Unlike standard implementations that overwrite previous states, this variant preserves every version, enabling direct queries on historical configurations. The core technique relies on path copying, where only nodes along the mo ...

Posted on Tue, 21 Jul 2026 16:09:02 +0000 by philipolson

Solution: QOJ-6322 / The 1st Universal Cup. Stage 12: Ōokayama - F. Forestry

Introduction This is a challenging problem that combines segment tree merging with dynamic programming optimization. While it follows a relatively standard template, the overall difficulty level is high. Prerequisites: Dynamic programming, tree-based DP, segmant tree with dynamic node allocation, segment tree merging. Problem link: Click here D ...

Posted on Fri, 10 Jul 2026 16:49:46 +0000 by Masna

Segment Tree Variants and Categorization Techniques

Linear Operation Segment Trees The most basic form of segment tree handles linear operations that satisfy commutativity and associativity, such as addition. Since operations do not depend on each other, maintaining lazy propagation is straightforward. For single-point modifications, a Fenwick Tree (Binary Indexed Tree) is often a more efficient ...

Posted on Sun, 28 Jun 2026 16:55:59 +0000 by sycoj0ker

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&gt ...

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