Dynamic DP
Practice
1.8 (5 votes)
Advanced data structures
Data structures
Medium
Segment trees
Problem
81% Success 340 Attempts 50 Points 1.5s Time Limit 256MB Memory 1024 KB Max Code

You are given the following:

  • An array \(A\) of length n that contains integers
  • \(DP\) of length \(n\) 
  • Arrays \(L\) and \(R\) of length \(n\) that contain integers

You are also provided with the following attributes:

  • If \(i=1\), then \(L_i=R_i=1\)

  • If \(2\leq i\leq n\), then \(1\leq L_i\leq R_i\lt i\)

  • \(DP_1=A_1\)

  • \(DP_i=min(DP[L_i,R_i])+A_i\)

The element \(A[x,y]\) is defined to be all the elements that are available in \([a,b]\) of array \(A\). For example, \(DP[2,3]\) indicates \(DP_2\) and \(DP_3\).

You are also provided with \(q\) queries that state the following:

  • You are given two integers \(x\) and \(y\).
  • You are required to replace \(DP[1,x]\) to \(y\) and then update the remaining part of \(DP\). Now, print \(DP_n\).

Note

  • You must repeat this task every time you are given these two integers.
  • The queries are independent of each other. Therefore, the current query cannot modify the next query.

Please login to use the editor

You need to be logged in to access the code editor

Loading...

Please wait while we load the editor

Loading...
Results
Custom Input
Run your code to see the output
Submissions
Please login to view your submissions
Similar Problems
Points:50
2 votes
Tags:
Segment TreesData StructuresCombinatoricsImplementationAdvanced Data StructuresGraphsSegment treeBasics of Implementation
Points:50
12 votes
Tags:
Advanced Data StructuresData StructuresSegment Trees
Points:50
3 votes
Tags:
Advanced Data StructuresSegment TreesBasics of ImplementationData StructuresImplementationSegment treeMath