|
| 1 | +/* |
| 2 | +What this program does: |
| 3 | +
|
| 4 | +For each test case, two arrays A and B are given. |
| 5 | +At every index i, we may choose either A[i] or B[i] to form a new array C. |
| 6 | +
|
| 7 | +A subarray [L, R] is considered valid if it is possible to pick values |
| 8 | +from each position such that the chosen sequence is strictly increasing. |
| 9 | +
|
| 10 | +
|
| 11 | +How the solution works: |
| 12 | +
|
| 13 | +At every index i, there are two possible states: |
| 14 | +- ending at i by choosing A[i] |
| 15 | +- ending at i by choosing B[i] |
| 16 | +
|
| 17 | +For each state, we compute how far to the right the sequence can be |
| 18 | +extended while keeping it strictly increasing. |
| 19 | +
|
| 20 | +This is done using dynamic programming from right to left: |
| 21 | +- reachA[i] stores the farthest index reachable if we pick A[i] |
| 22 | +- reachB[i] stores the farthest index reachable if we pick B[i] |
| 23 | +
|
| 24 | +Transitions depend only on comparisons with the next position. |
| 25 | +
|
| 26 | +
|
| 27 | +Counting valid ranges: |
| 28 | +
|
| 29 | +For a fixed starting index i, the subarray can extend up to |
| 30 | +max(reachA[i], reachB[i]). |
| 31 | +
|
| 32 | +So the number of valid subarrays starting at i is: |
| 33 | +(maxReach - i + 1) |
| 34 | +
|
| 35 | +Summing this over all i gives the final answer. |
| 36 | +
|
| 37 | +
|
| 38 | +Complexity: |
| 39 | +
|
| 40 | +Time: O(N) per test case |
| 41 | +Space: O(N) |
| 42 | +
|
| 43 | +
|
| 44 | +//---------SUBMISSION LINK |
| 45 | +https://www.codechef.com/viewsolution/1223502836 |
| 46 | +*/ |
| 47 | + |
| 48 | +import java.io.*; |
| 49 | +import java.util.*; |
| 50 | + |
| 51 | +public class Solution2 { |
| 52 | + public static void main(String[] args) throws Exception { |
| 53 | + BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); |
| 54 | + StringBuilder out = new StringBuilder(); |
| 55 | + |
| 56 | + int t = Integer.parseInt(br.readLine().trim()); |
| 57 | + |
| 58 | + while (t-- > 0) { |
| 59 | + int n = Integer.parseInt(br.readLine().trim()); |
| 60 | + |
| 61 | + int[] a = new int[n]; |
| 62 | + int[] b = new int[n]; |
| 63 | + |
| 64 | + StringTokenizer st = new StringTokenizer(br.readLine()); |
| 65 | + for (int i = 0; i < n; i++) a[i] = Integer.parseInt(st.nextToken()); |
| 66 | + |
| 67 | + st = new StringTokenizer(br.readLine()); |
| 68 | + for (int i = 0; i < n; i++) b[i] = Integer.parseInt(st.nextToken()); |
| 69 | + |
| 70 | + int[] reachA = new int[n]; |
| 71 | + int[] reachB = new int[n]; |
| 72 | + |
| 73 | + for (int i = n - 1; i >= 0; i--) { |
| 74 | + reachA[i] = i; |
| 75 | + reachB[i] = i; |
| 76 | + |
| 77 | + if (i + 1 < n) { |
| 78 | + int ra = i; |
| 79 | + int rb = i; |
| 80 | + |
| 81 | + if (a[i + 1] > a[i]) ra = Math.max(ra, reachA[i + 1]); |
| 82 | + if (b[i + 1] > a[i]) ra = Math.max(ra, reachB[i + 1]); |
| 83 | + |
| 84 | + if (a[i + 1] > b[i]) rb = Math.max(rb, reachA[i + 1]); |
| 85 | + if (b[i + 1] > b[i]) rb = Math.max(rb, reachB[i + 1]); |
| 86 | + |
| 87 | + reachA[i] = ra; |
| 88 | + reachB[i] = rb; |
| 89 | + } |
| 90 | + } |
| 91 | + |
| 92 | + long ans = 0; |
| 93 | + for (int i = 0; i < n; i++) { |
| 94 | + ans += Math.max(reachA[i], reachB[i]) - i + 1; |
| 95 | + } |
| 96 | + |
| 97 | + out.append(ans).append('\n'); |
| 98 | + } |
| 99 | + |
| 100 | + System.out.print(out.toString()); |
| 101 | + } |
| 102 | +} |
0 commit comments