Skip to content
Back to libraryPart 2 · Core techniques

2.1Prefix sums

Before you start

It helps to have read 1.3 Arrays and strings and 1.2 Complexity and limits. Overall level: Core.

Practice: Prefix sums

0 of 4 solved

About these problems

Apply, Connect, Reason and Stretch describe difficulty within this topic, not an official contest rating. Open any problem in any order; reading the chapter first is optional.

  1. 1Range totalsYou are given a list of n integers a1, a2, ..., an, with positions numbered from 1; the list never changes. Answer q queries. Each query gives two positions l and r and asks for the sum al + al+1 + ... + ar, including both ends.ApplyNot started
  2. 2Best fixed blockYou are given a list of n integers a1, a2, ..., an; values may be negative. Print the largest sum of a block of exactly k consecutive elements, ai + ai+1 + ... + ai+k-1.ConnectNot started
  3. 3Range paint countsA list has n positions, numbered 1 to n, and every value starts at 0. Each of q instructions gives l, r and v, and adds v to every position from l to r, including both ends. Print the final value at each position after all instructions.ReasonNot started
  4. 4Rectangle sensorsA grid has n rows and m columns, and each cell contains 0 or 1. Rows are numbered 1 to n from top to bottom and columns 1 to m from left to right. Answer q queries. Each query gives a rectangle whose top-left cell is in row r1, column c1 and whose bottom-right cell is in row r2, column c2, both included, and asks how many cells inside it contain 1.StretchNot started

Read the lesson