Optimal Moves
Practice
4.6 (5 votes)
Dynamic programming
Segment trees
Advanced data structures
Segment tree
Data structures
Problem
86% Success 384 Attempts 50 Points 2s Time Limit 256MB Memory 1024 KB Max Code
There are \(N\) points placed on a number line at position \(1, 2,..., N.\)
Initially, you are at point \(1\) and want to find the minimum cost to reach each of the \(N\) points. You are allowed to move only towards +ve x-axis.
Let's say, after certain moves, you are at point \(i (<N)\) \(\)and want to make the next move. You are allowed to make a move only of the following two types:
- type \(1:\) by moving to the next point i.e point \(i+1\) which cost \(A_i.\)
- type \(2:\) by moving to point \(j\) if and only if \(i∈[ L_j, R_j ]\) which cost \(B_i.\) \(\)
Find the minimum cost to reach each of the \(N\) points.
Input Format
- The first line contains \(T\) denoting the number of test cases. The description of \(T \) test cases is as follows:
- Each test case consists of multiple lines of input.
- The first line of each test case contains one integer \(N\) — the number of points.
- The second line consists of \(N-1\) space-separated integers denoting the elements of array \(A\).
- The Third line consists of \(N\) space-separated integers denoting the elements of array \(B\).
- The Fourth line consists of \(N\) space-separated integers denoting the elements of array \(L\).
- The Fifth line consists of \(N\) space-separated integers denoting the elements of array \(R\).
Output Format
- For each test case, output on a new line, the minimum cost to reach each of the \(N\) points.
Constraints
- \(1 \leq T \leq 10^{5}\)
- \(2 \leq N \leq 2\cdot10^{5}\)
- \(1 \leq A_i, B_i \leq 10^{9}\)
- \(1 \leq L_i \leq R_i \leq i\)
- \(\text { It is guaranteed that sum of N over all test cases doesn't exceed }\ 2\cdot 10^{5}\)
Submissions
Please login to view your submissions
Similar Problems
Points:50
11 votes
Tags:
ApprovedData StructuresDivide-and-conquer algorithmHardOpenSegment TreesTrees
Points:50
26 votes
Tags:
Data StructuresHardOpenSegment TreesTrees
Points:50
7 votes
Tags:
Advanced Data StructuresBinary SearchNumber TheorySegment TreesData StructuresMath
Editorial