Competitive Programming Template Collection
Tarjan's Algorithm for Strongly Connnected Components
namespace TarjanSCC {
int dfn[N], low[N], index, colorCount;
int comp[N], size[N], value[N];
bool inStack[N];
stack<int> stk;
vector<int> graph[N];
void tarjan(int u) {
dfn[u] = low[u] = ++index;
stk.push(u);
inStack[u] = true;
...
Posted on Tue, 02 Jun 2026 18:01:50 +0000 by justgrafx
Solving Codeforces Division 3 Round: Algorithmic Approaches and Implementations
Problem A: Minimum Steps to Visit All Points Given a array of distinct integers x₁, x₂, ..., xₙ and a starting position s on the number line. You can move left or right by one unit each step. Find the minimum number of steps required to visit all positions in the array, starting from position s. The optimal solution involves visiting the endpoi ...
Posted on Sun, 31 May 2026 23:51:47 +0000 by phant0m
Algebraic Structure of Left Monoid Actions on Information Monoids
Consider a labeled monoid (T) acting on an information monoid (S), forming the algebraic structure ((T, \times, 1_T, S, +, 0_S, \circ)).
The information monoid ((S, +, 0_S)) satisfies:
Closure: (\forall x, y \in S), (x + y \in S).
Associativity: (\forall x, y, z \in S), ((x + y) + z = x + (y + z)).
Idantity element: (\exists 0_S \in S) such th ...
Posted on Mon, 25 May 2026 23:11:11 +0000 by tearrek
Competitive Programming Contest Solutions: Segment Trees and Combinatorial Optimization
Problem 1: Maximum Goals and Assists
Problem Overview
Given two arrays representing goals and assists for multiple players, process queries that ask for the maximum total balls needed under different matching scenarios.
Key Observations
The problem essentially asks for the maximum value among three distinct scenarios:
Scenario 1: Each assist ca ...
Posted on Sun, 24 May 2026 16:31:07 +0000 by KingIsulgard
Introduction to Segment Trees
What is a Segment Tree?
A segment tree is a binary tree-based advanced data structure that supports flexible range operations. Unlike a Fenwick Tree (Binary Indexed Tree) which is limited to simple point update/range query use cases, segment trees can handle range updates with point queries, and even full range updates with range queries effici ...
Posted on Wed, 20 May 2026 07:33:16 +0000 by Stryks
Heavy-Light Decomposition Template for Tree Path Queries
The following C++ implementation demonstrates a complete Heavy-Light Decomposition (HLD) framework integrated with a lazy propagation segment tree to support efficient path updates and queries on trees. It passes the standard template problem on Luogu.
#include <bits/stdc++.h>
using namespace std;
using i64 = long long;
int MOD;
struct ...
Posted on Mon, 18 May 2026 00:02:42 +0000 by swizzer
Introduction to Segment Trees and Range Queries
Range Extremum Queries and Algorithmic ChoicesRange Maximum/Minimum Query (RMQ) problems involve processing an array of size n to handle multiple range queries and bulk modifications. Different data structures offer varying trade-offs:Brute Force: Simple implementation suitable for small datasets, but query performance is poor.Binary Indexed Tr ...
Posted on Wed, 13 May 2026 21:56:16 +0000 by Niruth
Segment Tree Historical Values and Advanced Tagging Techniques
Maintaining Range Minimum and Historical MaximumWhen a segment tree needs to support range addition, range minimum assignment, range sum, range maximum, and range historical maximum, a standard approach involves tracking the maximum value, strict second maximum value, and the count of maximum values within each node. Operations affecting the mi ...
Posted on Tue, 12 May 2026 19:45:03 +0000 by Pazuzu156
Finding the Leftmost Meeting Point in a Sequence of Buildings
This problem asks us to identify the earliest possible building index where two individuals, starting from distinct locations, can rendezvous. We are provided with an array representing building heights, let's call it buildingElevations, and a series of queries. Each query specifies two initial building indices, startA and startB.
The rule for ...
Posted on Mon, 11 May 2026 11:46:07 +0000 by Tryfan
Segment Tree Divide and Conquer with Rollback Data Structures
Introduction to Time-Based Divide and Conquer
Segment Tree Divide and Conquer is an advanced offline algorithmic technique typicalyl used to solve problems involving dynamic modifications that persist over specific time intervals. The core idea is to map the time dimension onto a segment tree, allowing us to decompose the lifespan of operations ...
Posted on Mon, 11 May 2026 09:35:33 +0000 by minc