Problem 1: Minimum Cost to Partition (Dynamic Programming + Convex
Hull Trick or Divide & Conquer Optimization)
Problem Statement:
You are given an array A of n integers. You want to partition it into k segments (1 ≤ k ≤ n), such
that the total cost is minimized. The cost of a segment [l, r] is defined as:
Cost(l, r) = (sum of elements from A[l] to A[r])²
Compute the minimum total cost to partition the array into exactly k segments.
Constraints:
● 1 ≤ n ≤ 10⁵
● 1 ≤ k ≤ 100
● 1 ≤ A[i] ≤ 1000
Hint:
● Use DP: dp[i][j] = min over p < j of (dp[i-1][p] + cost(p+1, j))
● The cost function is convex ⇒ optimize with Convex Hull Trick or Divide and Conquer
Optimization.
Problem 2: Range Sum with Point Updates (Segment Tree)
Problem Statement:
You are given an array A of size n. You have to perform two types of operations:
1. Update the value at index i to x.
2. Query the sum of elements in the range [l, r].
Input Format:
● First line: n and q (1 ≤ n, q ≤ 10⁵)
● Second line: n integers, the initial array A
● Next q lines:
○ 1 i x for update (0-based indexing)
○ 2 l r for range sum query
Output:
For each query of type 2, output the sum in a new line.
Hint:
Use a Segment Tree with O(log n) time per operation.
Problem 3: Connected Components in a Graph (Disjoint Set Union /
Union-Find)
Problem Statement:
You are given a graph with n nodes and m edges. The graph is initially empty. You will be given
a sequence of edges to add. After each edge is added, report the number of connected
components in the graph.
Input Format:
● First line: n and m (1 ≤ n ≤ 10⁵, 1 ≤ m ≤ 2×10⁵)
● Next m lines: two integers u and v (1 ≤ u, v ≤ n)
Output:
Output m lines. Each line should contain the number of connected components after adding
that edge.
Hint:
Use Disjoint Set Union (DSU) with path compression and union by rank for efficient merging.