Under construction

Still being built. Things may change

Jump Game

Time limit3sMemory limit1024 MB

Summary
Given N platforms whose values come from Q range-increment operations, choose jump positions at least K apart to maximize the sum of their values.
Level

Medium7 of 10

Topics
Dynamic programming, Segment tree, Intervals
Solved
No attempts yet

Problem

KOI Corp's jump game asks the player to cross NN platforms arranged in a row, using several jumps. The NN platforms sit on a vertical line and are numbered 0,1,…,N−10, 1, \dots, N-1 from left to right. At the start, the player stands on platform 0, the leftmost one, with a score of 0.

For every 0≤i≤N−10 \le i \le N-1, the player chooses either walk or jump on platform ii. Walking moves the player to platform i+1i+1 and leaves the score unchanged. Jumping moves the player to platform i+Ki+K and increases the score by A[i]A[i], where KK is a fixed number. The game ends successfully once the player moves past platform N−1N-1. Platforms numbered N,N+1,…N, N+1, \dots do not exist, but reaching any of them counts as moving past platform N−1N-1. The goal is to steer the player so that the final score is as large as possible and the game ends.

The KOI developers could not design so many platforms by hand, so they built them with a simple method. They set every A[i]A[i] to 0, then perform QQ operations. For each jj (0≤j≤Q−10 \le j \le Q-1), they choose an interval 0≤L[j]≤R[j]≤N−10 \le L[j] \le R[j] \le N-1 and increase each of A[L[j]],A[L[j]+1],…,A[R[j]]A[L[j]], A[L[j]+1], \dots, A[R[j]] by 1. After all operations, the array AA gives the score earned by jumping from each platform.

You are making a video about computer science that explains how to finish the KOI jump game with the maximum score. The game has up to N=1012N = 10^{12} platforms, so you need an efficient algorithm.

Constraints

  • 1≤N≤10121 \le N \le 10^{12}
  • 1≤Q≤250 0001 \le Q \le 250\,000
  • 1≤K≤N1 \le K \le N
  • For all 0≤j≤Q−10 \le j \le Q-1, 0≤L[j]≤R[j]≤N−10 \le L[j] \le R[j] \le N-1

Examples1

  1. Example 1

    Input
    1 1 1
    0 0
    
    Expected output
    1