0% found this document useful (0 votes)
19 views37 pages

CP Workshop Day 2

The document discusses various algorithms and techniques for efficiently computing range sums and handling queries on arrays, including prefix sums, difference arrays, and sliding window techniques. It outlines multiple problems that can be solved using these methods, such as counting segments with specific properties and finding maximum sums within given constraints. Additionally, it covers advanced topics like 2D prefix sums and convolution operations.

Uploaded by

Aer As
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
19 views37 pages

CP Workshop Day 2

The document discusses various algorithms and techniques for efficiently computing range sums and handling queries on arrays, including prefix sums, difference arrays, and sliding window techniques. It outlines multiple problems that can be solved using these methods, such as counting segments with specific properties and finding maximum sums within given constraints. Additionally, it covers advanced topics like 2D prefix sums and convolution operations.

Uploaded by

Aer As
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 37

BRACU CP Workshop

Day 2

Shehran Rahman, Shafin Alam

BRAC University, Dhaka

13 April 2025
Prefix Sum

▶ Let’s say we have a one-indexed integer array arr of size N


and we want to compute the value of

arr[a] + arr[a + 1] + · · · + arr[b]


for Q different pairs (a, b) satisfying 1 ≤ a ≤ b ≤ N.

1
USACO Guide
Prefix Sum

▶ Let’s say we have a one-indexed integer array arr of size N


and we want to compute the value of

arr[a] + arr[a + 1] + · · · + arr[b]


for Q different pairs (a, b) satisfying 1 ≤ a ≤ b ≤ N.
▶ Naively, for every query, we can iterate through all entries
from index a to index b to add them up.

1
USACO Guide
Prefix Sum

▶ Let’s say we have a one-indexed integer array arr of size N


and we want to compute the value of

arr[a] + arr[a + 1] + · · · + arr[b]


for Q different pairs (a, b) satisfying 1 ≤ a ≤ b ≤ N.
▶ Naively, for every query, we can iterate through all entries
from index a to index b to add them up.
▶ Since we have Q queries and each query requires a maximum
of O(N) operations to calculate the sum, our total time
complexity is O(NQ). For most problems of this nature, the
constraints will be N, Q ≤ 105 , so NQ is on the order of 1010 .
This is not acceptable; it will almost certainly exceed the time
limit. 1

1
USACO Guide
Prefix Sum

▶ We designate a prefix sum array prefix. First, because we’re


1-indexing the array, set prefix[0] = 0, then for indices k
such that 1 ≤ k ≤ N, define the prefix sum array as follows:
k
X
prefix[k] = arr[i]
i=1

2
USACO Guide
Prefix Sum

▶ We designate a prefix sum array prefix. First, because we’re


1-indexing the array, set prefix[0] = 0, then for indices k
such that 1 ≤ k ≤ N, define the prefix sum array as follows:
k
X
prefix[k] = arr[i]
i=1
▶ Now, when we want to query for the sum of the elements of
arr between (1-indexed) indices a and b inclusive, we can use
the following formula:
R
X R
X L−1
X
arr[i] = arr[i] − arr[i]
i=L i=1 i=1
2

2
USACO Guide
Static Range Sum Query

▶ Using our definition of the elements in the prefix sum array,


we have
R
X
arr[i] = prefix[R] − prefix[L − 1]
i=L
3

3
USACO Guide
Problem 1

▶ Given:
▶ String s of length n (2 ≤ n ≤ 105 )
▶ Characters are either ’.’ or ’#’
▶ m queries (1 ≤ m ≤ 105 )
▶ Query:
▶ Two indices li , ri (1 ≤ li < ri ≤ n)
▶ Count indices i where s[i] = s[i + 1] in [li , ri )
▶ Output:
▶ Answer all queries in order
Difference Array Technique
Efficient Range Updates

▶ Purpose:
▶ Range Update: add x to all values in range [l, r ]
▶ Perform range updates in constant time
▶ Convert to final array in linear time
Difference Array Technique
Efficient Range Updates

▶ Purpose:
▶ Range Update: add x to all values in range [l, r ]
▶ Perform range updates in constant time
▶ Convert to final array in linear time
▶ Steps:
▶ For update [l, r ] + val:
▶ D[l] += val
▶ D[r + 1] −= val
▶ Compute final array using prefix sum technique
Problem 2

▶ Given:
▶ Array A of n elements (1 ≤ n ≤ 2 × 105 )
▶ q queries (1 ≤ q ≤ 2 × 105 )
▶ Query:
▶ Range [li , ri ]
▶ Returns sum A[li ] + A[li + 1] + · · · + A[ri ]
▶ Challenge:
▶ Reorder array elements to maximize total sum of all query
answers
Problem 3

▶ Given:
▶ Initial array A of size n (1 ≤ n ≤ 105 )
▶ m range operations (1 ≤ m ≤ 105 )
▶ k queries (1 ≤ k ≤ 105 )
▶ Operations:
▶ Operation i: Add di to A[li ..ri ]
▶ Queries:
▶ Query j: Apply operations xj to yj
▶ Output:
▶ Final array after all queries
2D Prefix Sum

▶ In two dimensional prefix sum array

a X
X b
prefix[a][b] = arr[i][j].
i=1 j=1
2D Prefix Sum

▶ In two dimensional prefix sum array

a X
X b
prefix[a][b] = arr[i][j].
i=1 j=1

▶ This can be calculated as follows for row index 1 ≤ i ≤ n and


column index 1 ≤ j ≤ m:

prefix[i][j] = prefix[i − 1][j] + prefix[i][j − 1]


− prefix[i − 1][j − 1] + arr[i][j]
2D Prefix Sum

▶ The submatrix sum between rows a and A and columns b and


B, can thus be expressed as follows:

A X
X B
arr[i][j] = prefix[A][B] − prefix[a − 1][B]
i=a j=b

− prefix[A][b − 1] + prefix[a − 1][b − 1]


Problem 4

▶ Given:
▶ Input matrix I of size n × m (1 ≤ n, m ≤ 103 )
▶ Kernel size k × l (1 ≤ k ≤ n, 1 ≤ l ≤ m)
▶ Kernel elements restricted to −1, 0, 1
▶ Operation:
▶ 2D convolution:
Pk Pl
O(p, q) = x=1 y =1 K (x, y ) × I (p + x − 1, q + y − 1)
▶ Output matrix size: (n − k + 1) × (m − l + 1)
▶ Challenge:
▶ Find the maximum sum of all elements in the output matrix O
among all possible matrix K
Problem 5

▶ Scenario:
▶ Street with n sights at positions 1, 2, . . . , n miles
▶ Sight at position i has beauty bi
▶ Task:
▶ Choose jog interval [l, r ] with at least 3 sights
▶ Maximize: sum(3 max beauties) − (r − l)
▶ Constraints:
▶ t ≤ 105 test cases
▶ 5
P≤ 10 per
n test case
▶ n ≤ 105
▶ 1 ≤ bi ≤ 108
Monotone stack

▶ Given an array, a, of N (1 ≤ N ≤ 105 ) integers, for every


index i, find the nearest index j to the left such that j < i and
aj < ai .
Monotone stack

▶ What is the time complexity of our solution?


Sliding Window

▶ A sliding window is a constant-size subarray that moves from


left to right through the array.
▶ At each window position, we want to calculate some
information about the elements inside the window.
Sliding Window Minimum
Problem 1

▶ Given an array A of size n. For each k-size Window, find the


smallest element inside the window.
Sliding Window Minimum
Problem 1

Step 0: 2 1 4 5 3 4 1 2
Step 1: 2 1 4 5 3 4 1 2
Step 2: 2 1 4 5 3 4 1 2
Step 3: 2 1 4 5 3 4 1 2
Step 4: 2 1 4 5 3 4 1 2
Step 5: 2 1 4 5 3 4 1 2
Sliding Window Minimum
Problem 1

▶ To find the sliding window minimum, we use a queue that


always keeps the smallest element at the front. All elements
in the queue are in non-decreasing order, so the front is
always the minimum in the current window.
Sliding Window Minimum
Problem 1

▶ To find the sliding window minimum, we use a queue that


always keeps the smallest element at the front. All elements
in the queue are in non-decreasing order, so the front is
always the minimum in the current window.

4
Antti Laaksonen, Competitive Programmer’s Handbook, Chapter 8.3, 2018.
Sliding Window Minimum
Problem 1

▶ To find the sliding window minimum, we use a queue that


always keeps the smallest element at the front. All elements
in the queue are in non-decreasing order, so the front is
always the minimum in the current window.
▶ After each window move, we remove elements from the end of
the queue until the last queue element is smaller than the new
window element, or the queue becomes empty. We also
remove the first queue element if it is not inside the window
anymore. Finally, we add the new window element to the end
of the queue. 4

4
Antti Laaksonen, Competitive Programmer’s Handbook, Chapter 8.3, 2018.
Time Complexity
Problem 1

▶ What is the time complexity of our solution


Time Complexity
Problem 1

▶ What is the time complexity of our solution


▶ The time complexity of our solution is O(n),
Maximum Subarray Sum II

▶ You are given an array of n integers, and two integers a and b


(1 ≤ a ≤ b ≤ n).
▶ Find the maximum sum of values in any contiguous subarray
whose length is between a and b (inclusive).
Sliding Window Median

▶ You are given an array, a, of n (1 ≤ n ≤ 105 ) integers, and an


integer k (1 ≤ k ≤ n).
▶ Your task is to calculate the median of each contiguous
subarray (window) of size k, from left to right.
Problem 1

▶ You are given two arrays, sorted in non-decreasing order.


Merge them into one sorted array.
Valera and Books

▶ Valera has n books and t free minutes.


▶ The i-th book takes ai minutes to read.
▶ He can choose a starting index i and read books i, i + 1, . . . in
order, stopping if he doesn’t have enough time to fully read
the next book.
▶ Find the maximum number of books he can read.
Count Good Segments

▶ You are given an array a of n integers and an integer k.


Count Good Segments

▶ You are given an array a of n integers and an integer k.


▶ A segment a[l..r ] (1 ≤ l ≤ r ≤ n) is good if it contains at
most k distinct elements.
Count Good Segments

▶ You are given an array a of n integers and an integer k.


▶ A segment a[l..r ] (1 ≤ l ≤ r ≤ n) is good if it contains at
most k distinct elements.
▶ Count the number of good segments.
Problem 2

▶ You are given an array, a, of n (1 ≤ n ≤ 105 ) integers, and an


integer x. Find two indices i and j such that i ̸= j and
ai + aj = x.
Count Triplets with Target Sum

▶ Given a sorted array, a, of n (1 ≤ n ≤ 105 ) integers, and a


target value x.
▶ Count the number of triplets (i, j, k) such that i < j < k and
ai + aj + ak = x.
Count Good Segments by Range

▶ Given an array of n integers ai , and an integer k.


▶ A segment a[l..r ] (1 ≤ l ≤ r ≤ n) is good if the difference
between the maximum and minimum elements in the segment
is at most k.
▶ Your task is to count the number of different good segments.

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy