Dynamic operations that mix path updates and queries on a tree defeat simple prefix‑sum or difference‑based preprocessing. Heavy‑light decomposition (HLD) assigns each vertex to a heavy path and gives every path, as well as every complete subtree, a contiguous interval in a preorder. By flattening t...
A - Increment or Smash Strategy: Direct simulation. Every element that is not the global maximum will undergo a reset to zero followed by an increment, contributing exactly two steps to the total. However, identical values only incur this penalty once. The global maximum avoids the reset step altoge...
Given an array (a_1, a_2, \ldots, a_n) and three variables (P, Q, R) initially set to zero, process each element sequentially. For each element (a_i), choose one of three operations: (P := P \oplus a_i) (Q := Q \oplus a_i) (R := R \oplus a_i) After each operation, at least two of (P, Q, R) must be e...
This article summarizes several challenging competitive programming problems, showcasing various algorithmic techniques from dynamic programming and data structures to number theory and tree algorithms. Each problem explores distinct optimization strategies and mathematical insights. Problem 1: Reso...
Greeting Code #include <iostream> int main() { std::cout << "Competition Day!" << '\n'; return 0; } Neighbor Sum Array #include <iostream> #include <vector> using namespace std; int main() { ios::sync_with_stdio(false); cin.tie(nullptr); int len; cin >>...
Core Concepts and Prerequisites To effectively implement merging operations on range-based data structures, understanding dynamic node allocation and order statistic trees is essential. Standard segment trees often consume excessive memory when built fully; dynamic initialization allowss resources t...
Given $n$ points $(x_i, y_i)$ and $m$ rectangles, determine the number of points inside each rectangle. Solution Approach The problem requires counting points satisfying $l_j \le x_i \le r_j$ and $d_j \le y_i \le u_j$. By applying inclusion-exclusion, the count for a rectangle can be expressed as: $...
Consider we have n points indexed from 1 to n, each storing a numeric value. We often need to perform updates or aggregate queries over the continuous subrange [L, R]. The segment tree enables both update and query operations in O(log n) time complexity. The core idea of a segment tree is to recursi...
Statistical Operations on Intervals Problem Description Given a sequence of up to 1e5 elements, support three operations on intervals [l, r]: Add a constant value to all elements in the interval Calculate the average of elements in the interval Calculate the variance of elements in the itnerval Vari...
Dynamic node creation is used when the value range is large (e.g., 1e9) to conserve memory and avoid waste. Nodes are allocated only when needed, and queries return immediately upon reaching a null node. void update(int& node, int left, int right, int pos, int delta) { if (!node) node = ++node_c...