Skip to the code
cf.
+25 XP

#688 Range Bonus, Point Lookup

medium

A game's leaderboard gives bonus points to ranges of players: "everyone ranked 3 to 9 gets 50 points". Players often check their own bonus total. Write a class BonusBoard:

  • new BonusBoard(n) creates players 0 to n - 1 with 0 bonus.
  • bonus(left, right, amount) adds amount to every player from left to right, inclusive.
  • score(index) returns player index's bonus total.

Both methods should be O(log n).

Each example lists the calls in order, then each call's arguments. The first call creates the object with new BonusBoard(…); the output lists what each call returns (null for the constructor and for methods that return nothing).

Example 1

Input: ["BonusBoard","bonus","bonus","score","score","score"]
[[6],[1,3,10],[2,5,5],[0],[2],[5]]
Output: [null,null,null,0,15,5]

Player 0 got nothing, player 2 got both bonuses (10 + 5), and player 5 got only the second.

Example 2

Input: ["BonusBoard","score"]
[[1],[0]]
Output: [null,0]

No bonus yet.

Constraints

  • 1 <= n <= 10^5
  • 0 <= left <= right < n
  • -1000 <= amount <= 1000
  • At most 10^5 calls.
Saved in this browser

Run tries your code on the examples. Submit runs every test, including hidden edge cases, and solves the problem when they all pass.

Ctrl + Enter runs, Ctrl + Shift + Enter submits (⌘ on a Mac).