Essential Data Structures and Algorithmic Templates
A classic strcuture for managing dynamic connectivity and equivalence classes.
Initialization
int parent[N];
void initUnionFind() {
for (int i = 1; i <= n; ++i) {
parent[i] = i;
}
}
Path Compression Find
int findRoot(int x) {
return parent[x] == x ? x : parent[x] = findRoot(parent[x]);
}
Union by Root
void unite(int a, ...
Posted on Wed, 12 Aug 2026 16:17:03 +0000 by dibyajyotig
Minimizing Inversion Pairs in Array Partitioning
Problem Description
Given a permutation of length n and an integer k, partition the array in to k contiguous segments such that the sum of inversion counts within each segment is minimized.
Constraints: n ≤ 25000, k ≤ 25
Solution Approach
This problem can be solved using dynamic programming with decision monotonicity and divide-and-conquer opti ...
Posted on Thu, 06 Aug 2026 16:57:59 +0000 by IWS
Optimizing Sequence Deletion for Maximum Fixed-Point Sum
Problem Statement
Given a sequence \(a_1, a_2, \dots, a_n\), select elements to delete such that the remaining subsequence maximizes the count of indices \(i\) where \(a_i = i\). The goal is to compute this maximum value.
Initial Solution and Limitations
A dynamic programming approach tracks the longest subsequence satisfying \(a_j = j\) after ...
Posted on Mon, 15 Jun 2026 18:24:25 +0000 by Htmlwiz
Fenwick Tree Mastery: Core Operations and Practical Applications
Core Functinos
1. Point Update Operation
void update(int idx, int delta) {
while (idx <= arrSize) {
tree[idx] += delta;
idx += idx & -idx; // Propagate to parent nodes
}
}
2. Prefix Sum Query
long long query(int pos) {
long long result = 0;
while (pos > 0) {
result += tree[pos];
pos -= pos & -pos; // Move ...
Posted on Sat, 13 Jun 2026 18:23:20 +0000 by rupam_jaiswal
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
Counting Array Inversions Efficiently
Problem Specification
Given a sequence of n integers, compute the total number of inversions contained within the array. An inversion is formally defined as a pair of indices (i, j) satisfying i < j and A[i] > A[j].
Constraints & Limits
Sequence length: 1 ≤ n ≤ 10<sup>5</sup>
Element values: 0 ≤ A[i] ≤ 10<sup>9</ ...
Posted on Sat, 23 May 2026 18:36:57 +0000 by garydt
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