Competitive Programming Code Templates and Common Algorithms

Header File Templates C++ Template #include <bits/stdc++.h> #define fi first #define endl '\n' #define se second #define lowbit(x) ((x)&(-(x))) #define all(x) begin(x), end(x) #define lp(i, j, k) for(int i = int(j); i <= int(k); i++) #define rlp(i, j, k) for(int i = int(j); i >= int(k); i++) #define IO std::ios::sync_with_std ...

Posted on Mon, 08 Jun 2026 17:51:31 +0000 by atstein

Algorithms for Finding the Maximum Subarray Sum

Given a sequence of integers of length n, the objective is to identify a contiguous subarray that yields the maximum possible sum. This is a fundamental problem in computer science, solvable through several distinct algorithmic approaches. Dynamic Programming The optimal substructure for this problem can be defined by letting f(i) represent the ...

Posted on Sun, 17 May 2026 06:36:02 +0000 by Jiin

Bit Counting Using Divide and Conquer with 3-Bit Grouping

The following code deomnstrates a divide and conquer approach for bit counting using 3-bit grouping. This method efficiently computes the number of set bits in an integer by breaking the problem into smaller subproblems, solving them individually, and then combinnig the results. public static int bitsCount(int x) { int n; n = (x >&gt ...

Posted on Fri, 15 May 2026 14:53:35 +0000 by monloi

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