description
stringlengths
171
4k
code
stringlengths
94
3.98k
normalized_code
stringlengths
57
4.99k
Given a Binary Search Tree (BST) and a range l-h(inclusive), count the number of nodes in the BST that lie in the given range. The values smaller than root go to the left side The values greater and equal to the root go to the right side Example 1: Input: 10 / \ 5 50 / / \ 1 40 100 l = 5, h = 45 Output: 3 Explanation: 5 10 40 are the node in the range Example 2: Input: 5 / \ 4 6 / \ 3 7 l = 2, h = 8 Output: 5 Explanation: All the nodes are in the given range. Your Task: This is a function problem. You don't have to take input. You are required to complete the function getCountOfNode() that takes root, l ,h as parameters and returns the count. Expected Time Complexity: O(N) Expected Auxiliary Space: O(Height of the BST). Constraints: 1 <= Number of nodes <= 100 1 <= l < h < 10^{3}
class Solution: def getCount(self, root, low, high): def helper(node): if not node: return 0 if node.data < low: return helper(node.right) if node.data > high: return helper(node.left) return 1 + helper(node.left) + helper(node.right) return helper(root)
CLASS_DEF FUNC_DEF FUNC_DEF IF VAR RETURN NUMBER IF VAR VAR RETURN FUNC_CALL VAR VAR IF VAR VAR RETURN FUNC_CALL VAR VAR RETURN BIN_OP BIN_OP NUMBER FUNC_CALL VAR VAR FUNC_CALL VAR VAR RETURN FUNC_CALL VAR VAR
Given a Binary Search Tree (BST) and a range l-h(inclusive), count the number of nodes in the BST that lie in the given range. The values smaller than root go to the left side The values greater and equal to the root go to the right side Example 1: Input: 10 / \ 5 50 / / \ 1 40 100 l = 5, h = 45 Output: 3 Explanation: 5 10 40 are the node in the range Example 2: Input: 5 / \ 4 6 / \ 3 7 l = 2, h = 8 Output: 5 Explanation: All the nodes are in the given range. Your Task: This is a function problem. You don't have to take input. You are required to complete the function getCountOfNode() that takes root, l ,h as parameters and returns the count. Expected Time Complexity: O(N) Expected Auxiliary Space: O(Height of the BST). Constraints: 1 <= Number of nodes <= 100 1 <= l < h < 10^{3}
class Solution: def getCount(self, root, low, high): count = [0] def countnodes(root, low, high): if root == None: return if high < root.data: countnodes(root.left, low, high) return if low <= root.data and root.data <= high: count[0] += 1 if l >= root.data: countnodes(root.right, low, high) return countnodes(root.left, low, root.data - 1) countnodes(root.right, root.data, high) return countnodes(root, low, high) return count[0]
CLASS_DEF FUNC_DEF ASSIGN VAR LIST NUMBER FUNC_DEF IF VAR NONE RETURN IF VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR RETURN IF VAR VAR VAR VAR VAR NUMBER NUMBER IF VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR RETURN EXPR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR RETURN EXPR FUNC_CALL VAR VAR VAR VAR RETURN VAR NUMBER
Given a Binary Search Tree (BST) and a range l-h(inclusive), count the number of nodes in the BST that lie in the given range. The values smaller than root go to the left side The values greater and equal to the root go to the right side Example 1: Input: 10 / \ 5 50 / / \ 1 40 100 l = 5, h = 45 Output: 3 Explanation: 5 10 40 are the node in the range Example 2: Input: 5 / \ 4 6 / \ 3 7 l = 2, h = 8 Output: 5 Explanation: All the nodes are in the given range. Your Task: This is a function problem. You don't have to take input. You are required to complete the function getCountOfNode() that takes root, l ,h as parameters and returns the count. Expected Time Complexity: O(N) Expected Auxiliary Space: O(Height of the BST). Constraints: 1 <= Number of nodes <= 100 1 <= l < h < 10^{3}
class Solution: def getCount(self, root, low, high): count = 0 if root.data >= low and root.left is not None: count += self.getCount(root.left, low, high) if root.data <= high and root.right is not None: count += self.getCount(root.right, low, high) return count + 1 if root.data >= low and root.data <= high else count
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER IF VAR VAR VAR NONE VAR FUNC_CALL VAR VAR VAR VAR IF VAR VAR VAR NONE VAR FUNC_CALL VAR VAR VAR VAR RETURN VAR VAR VAR VAR BIN_OP VAR NUMBER VAR
Given a Binary Search Tree (BST) and a range l-h(inclusive), count the number of nodes in the BST that lie in the given range. The values smaller than root go to the left side The values greater and equal to the root go to the right side Example 1: Input: 10 / \ 5 50 / / \ 1 40 100 l = 5, h = 45 Output: 3 Explanation: 5 10 40 are the node in the range Example 2: Input: 5 / \ 4 6 / \ 3 7 l = 2, h = 8 Output: 5 Explanation: All the nodes are in the given range. Your Task: This is a function problem. You don't have to take input. You are required to complete the function getCountOfNode() that takes root, l ,h as parameters and returns the count. Expected Time Complexity: O(N) Expected Auxiliary Space: O(Height of the BST). Constraints: 1 <= Number of nodes <= 100 1 <= l < h < 10^{3}
class Solution: def getCount(self, root, low, high): count = 0 def preorder(node, low, high): if not node: return nonlocal count if node.data in range(low, high + 1): count += 1 preorder(node.left, low, high) preorder(node.right, low, high) preorder(root, low, high) return count
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER FUNC_DEF IF VAR RETURN IF VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR RETURN VAR
Given a Binary Search Tree (BST) and a range l-h(inclusive), count the number of nodes in the BST that lie in the given range. The values smaller than root go to the left side The values greater and equal to the root go to the right side Example 1: Input: 10 / \ 5 50 / / \ 1 40 100 l = 5, h = 45 Output: 3 Explanation: 5 10 40 are the node in the range Example 2: Input: 5 / \ 4 6 / \ 3 7 l = 2, h = 8 Output: 5 Explanation: All the nodes are in the given range. Your Task: This is a function problem. You don't have to take input. You are required to complete the function getCountOfNode() that takes root, l ,h as parameters and returns the count. Expected Time Complexity: O(N) Expected Auxiliary Space: O(Height of the BST). Constraints: 1 <= Number of nodes <= 100 1 <= l < h < 10^{3}
class Solution: def inorder(self, root): v = [] if root: v = self.inorder(root.left) v.append(root.data) v += self.inorder(root.right) return v def getCount(self, root, low, high): if root is None: return ans = [] ans = self.inorder(root) count = 0 for i in range(len(ans)): if ans[i] >= low and ans[i] <= high: count += 1 return count
CLASS_DEF FUNC_DEF ASSIGN VAR LIST IF VAR ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR RETURN VAR FUNC_DEF IF VAR NONE RETURN ASSIGN VAR LIST ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR VAR VAR NUMBER RETURN VAR
Given a Binary Search Tree (BST) and a range l-h(inclusive), count the number of nodes in the BST that lie in the given range. The values smaller than root go to the left side The values greater and equal to the root go to the right side Example 1: Input: 10 / \ 5 50 / / \ 1 40 100 l = 5, h = 45 Output: 3 Explanation: 5 10 40 are the node in the range Example 2: Input: 5 / \ 4 6 / \ 3 7 l = 2, h = 8 Output: 5 Explanation: All the nodes are in the given range. Your Task: This is a function problem. You don't have to take input. You are required to complete the function getCountOfNode() that takes root, l ,h as parameters and returns the count. Expected Time Complexity: O(N) Expected Auxiliary Space: O(Height of the BST). Constraints: 1 <= Number of nodes <= 100 1 <= l < h < 10^{3}
class Solution: def __init__(self): self.count = 0 def getCount(self, root, low, high): if not root: return 0 if root.data < low: return self.getCount(root.right, low, high) elif root.data > high: return self.getCount(root.left, low, high) else: return ( 1 + self.getCount(root.left, low, high) + self.getCount(root.right, low, high) )
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER FUNC_DEF IF VAR RETURN NUMBER IF VAR VAR RETURN FUNC_CALL VAR VAR VAR VAR IF VAR VAR RETURN FUNC_CALL VAR VAR VAR VAR RETURN BIN_OP BIN_OP NUMBER FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR VAR VAR VAR
Given a Binary Search Tree (BST) and a range l-h(inclusive), count the number of nodes in the BST that lie in the given range. The values smaller than root go to the left side The values greater and equal to the root go to the right side Example 1: Input: 10 / \ 5 50 / / \ 1 40 100 l = 5, h = 45 Output: 3 Explanation: 5 10 40 are the node in the range Example 2: Input: 5 / \ 4 6 / \ 3 7 l = 2, h = 8 Output: 5 Explanation: All the nodes are in the given range. Your Task: This is a function problem. You don't have to take input. You are required to complete the function getCountOfNode() that takes root, l ,h as parameters and returns the count. Expected Time Complexity: O(N) Expected Auxiliary Space: O(Height of the BST). Constraints: 1 <= Number of nodes <= 100 1 <= l < h < 10^{3}
class Solution: def inorder(self, root, arr): if root is not None: self.inorder(root.left, arr) arr.append(root.data) self.inorder(root.right, arr) def getCount(self, root, low, high): arr = [] c = 0 self.inorder(root, arr) for ele in arr: if low <= ele and ele <= high: c = c + 1 return c
CLASS_DEF FUNC_DEF IF VAR NONE EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR FUNC_DEF ASSIGN VAR LIST ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR VAR FOR VAR VAR IF VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER RETURN VAR
Given a Binary Search Tree (BST) and a range l-h(inclusive), count the number of nodes in the BST that lie in the given range. The values smaller than root go to the left side The values greater and equal to the root go to the right side Example 1: Input: 10 / \ 5 50 / / \ 1 40 100 l = 5, h = 45 Output: 3 Explanation: 5 10 40 are the node in the range Example 2: Input: 5 / \ 4 6 / \ 3 7 l = 2, h = 8 Output: 5 Explanation: All the nodes are in the given range. Your Task: This is a function problem. You don't have to take input. You are required to complete the function getCountOfNode() that takes root, l ,h as parameters and returns the count. Expected Time Complexity: O(N) Expected Auxiliary Space: O(Height of the BST). Constraints: 1 <= Number of nodes <= 100 1 <= l < h < 10^{3}
class Solution: def getCount(self, root, low, high): s = [] def inorder(root): if root != None: inorder(root.left) if low <= root.data <= high: s.append(root.data) inorder(root.right) inorder(root) return len(s)
CLASS_DEF FUNC_DEF ASSIGN VAR LIST FUNC_DEF IF VAR NONE EXPR FUNC_CALL VAR VAR IF VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR RETURN FUNC_CALL VAR VAR
Given a Binary Search Tree (BST) and a range l-h(inclusive), count the number of nodes in the BST that lie in the given range. The values smaller than root go to the left side The values greater and equal to the root go to the right side Example 1: Input: 10 / \ 5 50 / / \ 1 40 100 l = 5, h = 45 Output: 3 Explanation: 5 10 40 are the node in the range Example 2: Input: 5 / \ 4 6 / \ 3 7 l = 2, h = 8 Output: 5 Explanation: All the nodes are in the given range. Your Task: This is a function problem. You don't have to take input. You are required to complete the function getCountOfNode() that takes root, l ,h as parameters and returns the count. Expected Time Complexity: O(N) Expected Auxiliary Space: O(Height of the BST). Constraints: 1 <= Number of nodes <= 100 1 <= l < h < 10^{3}
class Solution: def getCount(self, root, low, high): x = self.order(root, []) c = 0 for i in x: if low <= i and i <= high: c += 1 return c def order(self, node, a): if node: a.append(node.data) self.order(node.left, a) self.order(node.right, a) return a
CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR LIST ASSIGN VAR NUMBER FOR VAR VAR IF VAR VAR VAR VAR VAR NUMBER RETURN VAR FUNC_DEF IF VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR RETURN VAR
Given a Binary Search Tree (BST) and a range l-h(inclusive), count the number of nodes in the BST that lie in the given range. The values smaller than root go to the left side The values greater and equal to the root go to the right side Example 1: Input: 10 / \ 5 50 / / \ 1 40 100 l = 5, h = 45 Output: 3 Explanation: 5 10 40 are the node in the range Example 2: Input: 5 / \ 4 6 / \ 3 7 l = 2, h = 8 Output: 5 Explanation: All the nodes are in the given range. Your Task: This is a function problem. You don't have to take input. You are required to complete the function getCountOfNode() that takes root, l ,h as parameters and returns the count. Expected Time Complexity: O(N) Expected Auxiliary Space: O(Height of the BST). Constraints: 1 <= Number of nodes <= 100 1 <= l < h < 10^{3}
class Solution: def getCount(self, root, low, high): def helper(root): x = [] if not root: return x x += helper(root.left) x.append(root.data) x += helper(root.right) return x ans = 0 cur = helper(root) for i in cur: if i > high: break if i >= low: ans += 1 return ans
CLASS_DEF FUNC_DEF FUNC_DEF ASSIGN VAR LIST IF VAR RETURN VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR RETURN VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FOR VAR VAR IF VAR VAR IF VAR VAR VAR NUMBER RETURN VAR
Read problems statements in [Mandarin Chinese], [Russian], [Vietnamese] and [Bengali] as well. A permutation is an array consisting of $N$ distinct integers from $1$ to $N$ in arbitrary order. Chef has two permutation arrays $A$ and $P$ of length $N$. Chef performs the following three types of operations on the array $A$: $1$ : Permute the array $A$ according to the order defined by permutation $P$.That is, replace $A$ with the array $B$ where $B[P[i]]=A[i]$. $2 \ x \ y$ : Swap the elements at positions $x$ and $y$ in $A$. $3 \ x$ : Output the element at position $x$ in array $A$. For each operation of type $3$, Chef wants you to output the element at position $x$ in array $A$ after all preceding operations have been applied. ------ Input ------ The first line contains an integer $T$, the number of test cases. Then the test cases follow. The first line of each test case contains a single integer $N$. The second line contains $N$ integers denoting elements of array $A$. The third line contains $N$ integers denoting elements of array $P$. The fourth line contains a single integer $Q$, denoting the number of operations to be performed. $Q$ lines follow, each describing an operation of one of the three types above. ------ Output ------ For each test case, for each query of type $3$, output the answer in a separate line. ------ Constraints ------ $1 ≤ T ≤ 1000$ $1 ≤ N, Q ≤ 10^{5}$ $1 ≤ A[i],P[i], x, y ≤ N$ $A$ and $P$ are permutations. $x\ne y$ in type $2$ operations. The sum of $N$ and the sum of $Q$ over all test cases do not exceed $10^{5}$. ----- Sample Input 1 ------ 1 3 1 2 3 2 3 1 3 1 2 2 3 3 1 ----- Sample Output 1 ------ 3 ----- explanation 1 ------ Initially, array $A$ is $[1,2,3]$. It changes as follows: $[1,2,3]$ -> $[3,1,2]$ -> $[3,2,1]$. So, we output element at position $1$ in array $A$, which is $3$.
t = int(input()) for _ in range(t): n = int(input()) A = ["X"] + list(map(int, input().split())) p = ["X"] + list(map(int, input().split())) cycle = {} cycle_name_of = {} index_in_cycle = {} already = {} for i in range(1, n + 1): if already.get(i, True): already[i] = False cycle[i] = [[i], 1] cycle_name_of[i] = i index_in_cycle[i] = cycle[i][1] - 1 j = p[i] while j != i: cycle[i][0].append(j) cycle[i][1] += 1 cycle_name_of[j] = i index_in_cycle[j] = cycle[i][1] - 1 already[j] = False j = p[j] count = 0 def find_original(index, count): the_cycle_it_belongs_to = cycle_name_of[index] m = cycle[the_cycle_it_belongs_to][1] elements = cycle[the_cycle_it_belongs_to][0] return elements[int((index_in_cycle[index] - count % m + m) % m)] q = int(input()) for Q in range(q): l = list(map(int, input().split())) if l[0] == 1: count += 1 elif l[0] == 2: a = find_original(l[1], count) b = find_original(l[2], count) A[a], A[b] = A[b], A[a] elif l[0] == 3: print(A[find_original(l[1], count)])
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST STRING FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST STRING FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR DICT ASSIGN VAR DICT ASSIGN VAR DICT ASSIGN VAR DICT FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR LIST LIST VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR VAR VAR WHILE VAR VAR EXPR FUNC_CALL VAR VAR NUMBER VAR VAR VAR NUMBER NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR NUMBER FUNC_DEF ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER RETURN VAR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR NUMBER NUMBER VAR NUMBER IF VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR IF VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR FUNC_CALL VAR VAR NUMBER VAR
Read problems statements in [Mandarin Chinese], [Russian], [Vietnamese] and [Bengali] as well. A permutation is an array consisting of $N$ distinct integers from $1$ to $N$ in arbitrary order. Chef has two permutation arrays $A$ and $P$ of length $N$. Chef performs the following three types of operations on the array $A$: $1$ : Permute the array $A$ according to the order defined by permutation $P$.That is, replace $A$ with the array $B$ where $B[P[i]]=A[i]$. $2 \ x \ y$ : Swap the elements at positions $x$ and $y$ in $A$. $3 \ x$ : Output the element at position $x$ in array $A$. For each operation of type $3$, Chef wants you to output the element at position $x$ in array $A$ after all preceding operations have been applied. ------ Input ------ The first line contains an integer $T$, the number of test cases. Then the test cases follow. The first line of each test case contains a single integer $N$. The second line contains $N$ integers denoting elements of array $A$. The third line contains $N$ integers denoting elements of array $P$. The fourth line contains a single integer $Q$, denoting the number of operations to be performed. $Q$ lines follow, each describing an operation of one of the three types above. ------ Output ------ For each test case, for each query of type $3$, output the answer in a separate line. ------ Constraints ------ $1 ≤ T ≤ 1000$ $1 ≤ N, Q ≤ 10^{5}$ $1 ≤ A[i],P[i], x, y ≤ N$ $A$ and $P$ are permutations. $x\ne y$ in type $2$ operations. The sum of $N$ and the sum of $Q$ over all test cases do not exceed $10^{5}$. ----- Sample Input 1 ------ 1 3 1 2 3 2 3 1 3 1 2 2 3 3 1 ----- Sample Output 1 ------ 3 ----- explanation 1 ------ Initially, array $A$ is $[1,2,3]$. It changes as follows: $[1,2,3]$ -> $[3,1,2]$ -> $[3,2,1]$. So, we output element at position $1$ in array $A$, which is $3$.
def get_value(k, ind, cycles, cycle, index_in_cycle): m = len(cycles[cycle[ind]]) return cycles[cycle[ind]][index_in_cycle[ind] - k % m] - 1 def determine_cycles(p, n, cycles): visited = [False] * n i = 1 while i <= n: if not visited[i - 1]: visited[i - 1] = True cycles.append([i]) j = p[i - 1] while j != i: visited[j - 1] = True cycles[-1].append(j) j = p[j - 1] i = i + 1 T = int(input()) for _ in range(T): n = int(input()) a = list(map(int, input().split())) p = list(map(int, input().split())) cycles = [] cycle = [0] * n index_in_cycle = [0] * n determine_cycles(p, n, cycles) no_of_cycles = len(cycles) for i in range(no_of_cycles): cycle_i = cycles[i] for j in range(len(cycle_i)): c = cycles[i][j] cycle[c - 1] = i index_in_cycle[c - 1] = j no_of_permutations = 0 q = int(input()) for _ in range(q): query = list(map(int, input().split())) if query[0] == 1: no_of_permutations += 1 elif query[0] == 2: x = query[1] - 1 y = query[2] - 1 x1 = get_value(no_of_permutations, x, cycles, cycle, index_in_cycle) y1 = get_value(no_of_permutations, y, cycles, cycle, index_in_cycle) a[x1], a[y1] = a[y1], a[x1] else: ind = query[1] - 1 print(a[get_value(no_of_permutations, ind, cycles, cycle, index_in_cycle)])
FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR VAR VAR RETURN BIN_OP VAR VAR VAR BIN_OP VAR VAR BIN_OP VAR VAR NUMBER FUNC_DEF ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER WHILE VAR VAR IF VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR LIST VAR ASSIGN VAR VAR BIN_OP VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER VAR EXPR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP VAR NUMBER VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR NUMBER NUMBER VAR NUMBER IF VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR
Read problems statements in [Mandarin Chinese], [Russian], [Vietnamese] and [Bengali] as well. A permutation is an array consisting of $N$ distinct integers from $1$ to $N$ in arbitrary order. Chef has two permutation arrays $A$ and $P$ of length $N$. Chef performs the following three types of operations on the array $A$: $1$ : Permute the array $A$ according to the order defined by permutation $P$.That is, replace $A$ with the array $B$ where $B[P[i]]=A[i]$. $2 \ x \ y$ : Swap the elements at positions $x$ and $y$ in $A$. $3 \ x$ : Output the element at position $x$ in array $A$. For each operation of type $3$, Chef wants you to output the element at position $x$ in array $A$ after all preceding operations have been applied. ------ Input ------ The first line contains an integer $T$, the number of test cases. Then the test cases follow. The first line of each test case contains a single integer $N$. The second line contains $N$ integers denoting elements of array $A$. The third line contains $N$ integers denoting elements of array $P$. The fourth line contains a single integer $Q$, denoting the number of operations to be performed. $Q$ lines follow, each describing an operation of one of the three types above. ------ Output ------ For each test case, for each query of type $3$, output the answer in a separate line. ------ Constraints ------ $1 ≤ T ≤ 1000$ $1 ≤ N, Q ≤ 10^{5}$ $1 ≤ A[i],P[i], x, y ≤ N$ $A$ and $P$ are permutations. $x\ne y$ in type $2$ operations. The sum of $N$ and the sum of $Q$ over all test cases do not exceed $10^{5}$. ----- Sample Input 1 ------ 1 3 1 2 3 2 3 1 3 1 2 2 3 3 1 ----- Sample Output 1 ------ 3 ----- explanation 1 ------ Initially, array $A$ is $[1,2,3]$. It changes as follows: $[1,2,3]$ -> $[3,1,2]$ -> $[3,2,1]$. So, we output element at position $1$ in array $A$, which is $3$.
for _ in range(int(input())): n = int(input()) A = list(map(int, input().split())) P = list(map(int, input().split())) cur = [] vis = {} for c in range(n): if c in vis: continue cur.append([]) while True: if c in vis: break cur[-1].append(A[c]) vis[c] = len(cur) - 1, len(cur[-1]) - 1 c = P[c] - 1 o = 0 def g(cur, z): n = len(cur[z[0]]) return cur[z[0]][((z[1] - o) % n + n) % n] def g2(cur, z, v): n = len(cur[z[0]]) cur[z[0]][((z[1] - o) % n + n) % n] = v for _ in range(int(input())): s = list(map(int, input().split())) if len(s) == 1: o += 1 elif s[0] == 2: z1 = vis[s[1] - 1] z2 = vis[s[2] - 1] x = g(cur, z1) y = g(cur, z2) g2(cur, z1, y) g2(cur, z2, x) else: z1 = vis[s[1] - 1] print(g(cur, z1))
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR DICT FOR VAR FUNC_CALL VAR VAR IF VAR VAR EXPR FUNC_CALL VAR LIST WHILE NUMBER IF VAR VAR EXPR FUNC_CALL VAR NUMBER VAR VAR ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR VAR NUMBER ASSIGN VAR NUMBER FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER RETURN VAR VAR NUMBER BIN_OP BIN_OP BIN_OP BIN_OP VAR NUMBER VAR VAR VAR VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER BIN_OP BIN_OP BIN_OP BIN_OP VAR NUMBER VAR VAR VAR VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF FUNC_CALL VAR VAR NUMBER VAR NUMBER IF VAR NUMBER NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR
Read problems statements in [Hindi], [Mandarin Chinese], [Russian], [Vietnamese] and [Bengali] as well. You have $C = 100,000$ cakes, numbered $1$ through $C$. Each cake has an integer height; initially, the height of each cake is $0$. There are $N$ operations. In each operation, you are given two integers $L$ and $R$, and you should increase by $1$ the height of each of the cakes $L, L+1, \ldots, R$. One of these $N$ operations should be removed and the remaining $N-1$ operations are then performed. Chef wants to remove one operation in such a way that after the remaining $N-1$ operations are performed, the number of cakes with height exactly $K$ is maximum possible. Since Chef is a bit busy these days, he has asked for your help. You need to find the maximum number of cakes with height exactly $K$ that can be achieved by removing one operation. ------ Input ------ The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows. The first line of each test case contains two space-separated integers $N$ and $K$. Each of the next $N$ lines contains two space-separated integers $L$ and $R$ describing one operation. ------ Output ------ For each test case, print a single line containing one integer — the maximum possible number of cakes with height $K$. ------ Constraints ------ $1 ≤ T ≤ 100$ $2 ≤ N ≤ 10^{5}$ $1 ≤ K ≤ N$ $1 ≤ L ≤ R ≤ 10^{5}$ the sum of $N$ over all test cases does not exceed $10^{6}$ ----- Sample Input 1 ------ 1 3 2 2 6 4 9 1 4 ----- Sample Output 1 ------ 3 ----- explanation 1 ------ Example case 1: Let's look at what happens after an operation is removed. - Removing operation $1$: The heights of cakes $4$ through $9$ increase by $1$. Then, the heights of cakes $1$ through $4$ increase by $1$. The resulting sequence of heights is $[1, 1, 1, 2, 1, 1, 1, 1, 1]$ (for cakes $1$ through $9$; the other cakes have heights $0$). The number of cakes with height $2$ is $1$. - Removing operation $2$: The resulting sequence of heights of cakes $1$ through $9$ is $[1, 2, 2, 2, 1, 1, 0, 0, 0]$. The number of cakes with height $2$ is $3$. - Removing operation $3$: The resulting sequence of heights of cakes $1$ through $9$ is $[0, 1, 1, 2, 2, 2, 1, 1, 1]$. The number of cakes with height $2$ is $3$. The maximum number of cakes with height $2$ is $3$.
for _ in range(int(input())): n, k = map(int, input().split()) d = {(0): 0} arr = [] for i in range(n): l, r = map(int, input().split()) arr.append([l, r]) d[l] = d.get(l, 0) + 1 d[r + 1] = d.get(r + 1, 0) - 1 b = sorted(d) s = 0 for i in b: d[i] = d[i] + s s = d[i] d1 = {} s = 0 s1 = 0 ans = 0 for i in range(len(b) - 1): if d[b[i]] == k: ans += b[i + 1] - b[i] d1[0] = [0, 0] for i in range(len(b) - 1): if d[b[i]] == k: d1[b[i + 1]] = [d1[b[i]][0] + b[i + 1] - b[i], d1[b[i]][1]] else: d1[b[i + 1]] = [d1[b[i]][0], d1[b[i]][1]] if d[b[i]] == k + 1: d1[b[i + 1]] = [d1[b[i + 1]][0], d1[b[i]][1] + b[i + 1] - b[i]] else: d1[b[i + 1]] = [d1[b[i + 1]][0], d1[b[i]][1]] m = -1 for i in arr: l = i[0] r = i[1] m = max(m, ans + d1[r + 1][1] - d1[l][1] - d1[r + 1][0] + d1[l][0]) print(m)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR DICT NUMBER NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR LIST VAR VAR ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR DICT ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER IF VAR VAR VAR VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR NUMBER LIST NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER IF VAR VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER LIST BIN_OP BIN_OP VAR VAR VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER LIST VAR VAR VAR NUMBER VAR VAR VAR NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER LIST VAR VAR BIN_OP VAR NUMBER NUMBER BIN_OP BIN_OP VAR VAR VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER LIST VAR VAR BIN_OP VAR NUMBER NUMBER VAR VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR BIN_OP VAR NUMBER NUMBER VAR VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
Read problems statements in [Hindi], [Mandarin Chinese], [Russian], [Vietnamese] and [Bengali] as well. You have $C = 100,000$ cakes, numbered $1$ through $C$. Each cake has an integer height; initially, the height of each cake is $0$. There are $N$ operations. In each operation, you are given two integers $L$ and $R$, and you should increase by $1$ the height of each of the cakes $L, L+1, \ldots, R$. One of these $N$ operations should be removed and the remaining $N-1$ operations are then performed. Chef wants to remove one operation in such a way that after the remaining $N-1$ operations are performed, the number of cakes with height exactly $K$ is maximum possible. Since Chef is a bit busy these days, he has asked for your help. You need to find the maximum number of cakes with height exactly $K$ that can be achieved by removing one operation. ------ Input ------ The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows. The first line of each test case contains two space-separated integers $N$ and $K$. Each of the next $N$ lines contains two space-separated integers $L$ and $R$ describing one operation. ------ Output ------ For each test case, print a single line containing one integer — the maximum possible number of cakes with height $K$. ------ Constraints ------ $1 ≤ T ≤ 100$ $2 ≤ N ≤ 10^{5}$ $1 ≤ K ≤ N$ $1 ≤ L ≤ R ≤ 10^{5}$ the sum of $N$ over all test cases does not exceed $10^{6}$ ----- Sample Input 1 ------ 1 3 2 2 6 4 9 1 4 ----- Sample Output 1 ------ 3 ----- explanation 1 ------ Example case 1: Let's look at what happens after an operation is removed. - Removing operation $1$: The heights of cakes $4$ through $9$ increase by $1$. Then, the heights of cakes $1$ through $4$ increase by $1$. The resulting sequence of heights is $[1, 1, 1, 2, 1, 1, 1, 1, 1]$ (for cakes $1$ through $9$; the other cakes have heights $0$). The number of cakes with height $2$ is $1$. - Removing operation $2$: The resulting sequence of heights of cakes $1$ through $9$ is $[1, 2, 2, 2, 1, 1, 0, 0, 0]$. The number of cakes with height $2$ is $3$. - Removing operation $3$: The resulting sequence of heights of cakes $1$ through $9$ is $[0, 1, 1, 2, 2, 2, 1, 1, 1]$. The number of cakes with height $2$ is $3$. The maximum number of cakes with height $2$ is $3$.
t = int(input()) for _ in range(t): n, k = map(int, input().split()) a = [] maxr = -1 c = [0] * 100001 for _ in range(n): l, r = map(int, input().split()) maxr = max(r, maxr) c[l] += 1 c[r + 1] -= 1 a.append((l, r)) count = 0 pre = [0] * (maxr + 1) pre2 = [0] * (maxr + 1) for i in range(1, maxr + 1): c[i] += c[i - 1] if c[i] == k: pre[i] = 1 if c[i] == k + 1: pre2[i] = 1 pre[i] += pre[i - 1] pre2[i] += pre2[i - 1] totk = pre[-1] ans = 0 for i in range(n): ck = pre[a[i][1]] - pre[a[i][0] - 1] ck1 = pre2[a[i][1]] - pre2[a[i][0] - 1] count = totk - ck + ck1 ans = max(ans, count) print(ans)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER IF VAR VAR VAR ASSIGN VAR VAR NUMBER IF VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR NUMBER VAR BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR VAR VAR NUMBER VAR BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR
Read problems statements in [Hindi], [Mandarin Chinese], [Russian], [Vietnamese] and [Bengali] as well. You have $C = 100,000$ cakes, numbered $1$ through $C$. Each cake has an integer height; initially, the height of each cake is $0$. There are $N$ operations. In each operation, you are given two integers $L$ and $R$, and you should increase by $1$ the height of each of the cakes $L, L+1, \ldots, R$. One of these $N$ operations should be removed and the remaining $N-1$ operations are then performed. Chef wants to remove one operation in such a way that after the remaining $N-1$ operations are performed, the number of cakes with height exactly $K$ is maximum possible. Since Chef is a bit busy these days, he has asked for your help. You need to find the maximum number of cakes with height exactly $K$ that can be achieved by removing one operation. ------ Input ------ The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows. The first line of each test case contains two space-separated integers $N$ and $K$. Each of the next $N$ lines contains two space-separated integers $L$ and $R$ describing one operation. ------ Output ------ For each test case, print a single line containing one integer — the maximum possible number of cakes with height $K$. ------ Constraints ------ $1 ≤ T ≤ 100$ $2 ≤ N ≤ 10^{5}$ $1 ≤ K ≤ N$ $1 ≤ L ≤ R ≤ 10^{5}$ the sum of $N$ over all test cases does not exceed $10^{6}$ ----- Sample Input 1 ------ 1 3 2 2 6 4 9 1 4 ----- Sample Output 1 ------ 3 ----- explanation 1 ------ Example case 1: Let's look at what happens after an operation is removed. - Removing operation $1$: The heights of cakes $4$ through $9$ increase by $1$. Then, the heights of cakes $1$ through $4$ increase by $1$. The resulting sequence of heights is $[1, 1, 1, 2, 1, 1, 1, 1, 1]$ (for cakes $1$ through $9$; the other cakes have heights $0$). The number of cakes with height $2$ is $1$. - Removing operation $2$: The resulting sequence of heights of cakes $1$ through $9$ is $[1, 2, 2, 2, 1, 1, 0, 0, 0]$. The number of cakes with height $2$ is $3$. - Removing operation $3$: The resulting sequence of heights of cakes $1$ through $9$ is $[0, 1, 1, 2, 2, 2, 1, 1, 1]$. The number of cakes with height $2$ is $3$. The maximum number of cakes with height $2$ is $3$.
t = int(input()) while t > 0: t = t - 1 n, k = map(int, input().split()) h = [] le = [] r = [] mr = 0 for i in range(0, n): l, ri = map(int, input().split()) le.append(l) r.append(ri) if ri > mr: mr = ri for i in range(0, mr + 2): h.append(0) for i in range(0, n): h[le[i]] += 1 h[r[i] + 1] += -1 for i in range(1, mr + 1): h[i] = h[i] + h[i - 1] he = [] hu = [] if h[0] == k: he.append(1) y = 1 else: he.append(0) y = 0 if h[0] == k + 1: hu.append(1) else: hu.append(0) for i in range(1, mr + 1): if h[i] == k: he.append(he[i - 1] + 1) y = y + 1 else: he.append(he[i - 1]) if h[i] == k + 1: hu.append(hu[i - 1] + 1) else: hu.append(hu[i - 1]) check = [] for i in range(0, n): check.append(he[r[i]] - he[le[i] - 1] - (hu[r[i]] - hu[le[i] - 1])) max = 0 for i in range(0, n): x = y - check[i] if x > max: max = x print(max)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR VAR VAR VAR NUMBER VAR BIN_OP VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR LIST ASSIGN VAR LIST IF VAR NUMBER VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER IF VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR VAR VAR BIN_OP VAR VAR NUMBER BIN_OP VAR VAR VAR VAR BIN_OP VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR BIN_OP VAR VAR VAR IF VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR
Read problems statements in [Hindi], [Mandarin Chinese], [Russian], [Vietnamese] and [Bengali] as well. You have $C = 100,000$ cakes, numbered $1$ through $C$. Each cake has an integer height; initially, the height of each cake is $0$. There are $N$ operations. In each operation, you are given two integers $L$ and $R$, and you should increase by $1$ the height of each of the cakes $L, L+1, \ldots, R$. One of these $N$ operations should be removed and the remaining $N-1$ operations are then performed. Chef wants to remove one operation in such a way that after the remaining $N-1$ operations are performed, the number of cakes with height exactly $K$ is maximum possible. Since Chef is a bit busy these days, he has asked for your help. You need to find the maximum number of cakes with height exactly $K$ that can be achieved by removing one operation. ------ Input ------ The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows. The first line of each test case contains two space-separated integers $N$ and $K$. Each of the next $N$ lines contains two space-separated integers $L$ and $R$ describing one operation. ------ Output ------ For each test case, print a single line containing one integer — the maximum possible number of cakes with height $K$. ------ Constraints ------ $1 ≤ T ≤ 100$ $2 ≤ N ≤ 10^{5}$ $1 ≤ K ≤ N$ $1 ≤ L ≤ R ≤ 10^{5}$ the sum of $N$ over all test cases does not exceed $10^{6}$ ----- Sample Input 1 ------ 1 3 2 2 6 4 9 1 4 ----- Sample Output 1 ------ 3 ----- explanation 1 ------ Example case 1: Let's look at what happens after an operation is removed. - Removing operation $1$: The heights of cakes $4$ through $9$ increase by $1$. Then, the heights of cakes $1$ through $4$ increase by $1$. The resulting sequence of heights is $[1, 1, 1, 2, 1, 1, 1, 1, 1]$ (for cakes $1$ through $9$; the other cakes have heights $0$). The number of cakes with height $2$ is $1$. - Removing operation $2$: The resulting sequence of heights of cakes $1$ through $9$ is $[1, 2, 2, 2, 1, 1, 0, 0, 0]$. The number of cakes with height $2$ is $3$. - Removing operation $3$: The resulting sequence of heights of cakes $1$ through $9$ is $[0, 1, 1, 2, 2, 2, 1, 1, 1]$. The number of cakes with height $2$ is $3$. The maximum number of cakes with height $2$ is $3$.
t = int(input()) for i in range(t): ma = 0 P = [] n, k = map(int, input().split()) for j in range(n): a, b = map(int, input().split()) if b > ma: ma = b P.append([a, b]) Arr = [0] * (ma + 5) for j in P: Arr[j[0]] += 1 Arr[j[1] + 1] -= 1 for j in range(1, len(Arr)): Arr[j] = Arr[j - 1] + Arr[j] Arr2 = [0] * (ma + 5) Arr3 = [0] * (ma + 5) for j in range(len(Arr)): if Arr[j] == k: Arr2[j] = 1 if Arr[j] == k + 1: Arr3[j] = 1 for j in range(1, len(Arr)): Arr2[j] = Arr2[j - 1] + Arr2[j] Arr3[j] = Arr3[j - 1] + Arr3[j] ma2 = 0 for j in P: tot = 0 her = Arr2[j[1]] - Arr2[j[0] - 1] tot += Arr2[ma] - her her2 = Arr3[j[1]] - Arr3[j[0] - 1] tot += her2 if ma2 < tot: ma2 = tot print(ma2)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR LIST ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR LIST VAR VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FOR VAR VAR VAR VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR ASSIGN VAR VAR NUMBER IF VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR NUMBER FOR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER VAR VAR IF VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR
Read problems statements in [Hindi], [Mandarin Chinese], [Russian], [Vietnamese] and [Bengali] as well. You have $C = 100,000$ cakes, numbered $1$ through $C$. Each cake has an integer height; initially, the height of each cake is $0$. There are $N$ operations. In each operation, you are given two integers $L$ and $R$, and you should increase by $1$ the height of each of the cakes $L, L+1, \ldots, R$. One of these $N$ operations should be removed and the remaining $N-1$ operations are then performed. Chef wants to remove one operation in such a way that after the remaining $N-1$ operations are performed, the number of cakes with height exactly $K$ is maximum possible. Since Chef is a bit busy these days, he has asked for your help. You need to find the maximum number of cakes with height exactly $K$ that can be achieved by removing one operation. ------ Input ------ The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows. The first line of each test case contains two space-separated integers $N$ and $K$. Each of the next $N$ lines contains two space-separated integers $L$ and $R$ describing one operation. ------ Output ------ For each test case, print a single line containing one integer — the maximum possible number of cakes with height $K$. ------ Constraints ------ $1 ≤ T ≤ 100$ $2 ≤ N ≤ 10^{5}$ $1 ≤ K ≤ N$ $1 ≤ L ≤ R ≤ 10^{5}$ the sum of $N$ over all test cases does not exceed $10^{6}$ ----- Sample Input 1 ------ 1 3 2 2 6 4 9 1 4 ----- Sample Output 1 ------ 3 ----- explanation 1 ------ Example case 1: Let's look at what happens after an operation is removed. - Removing operation $1$: The heights of cakes $4$ through $9$ increase by $1$. Then, the heights of cakes $1$ through $4$ increase by $1$. The resulting sequence of heights is $[1, 1, 1, 2, 1, 1, 1, 1, 1]$ (for cakes $1$ through $9$; the other cakes have heights $0$). The number of cakes with height $2$ is $1$. - Removing operation $2$: The resulting sequence of heights of cakes $1$ through $9$ is $[1, 2, 2, 2, 1, 1, 0, 0, 0]$. The number of cakes with height $2$ is $3$. - Removing operation $3$: The resulting sequence of heights of cakes $1$ through $9$ is $[0, 1, 1, 2, 2, 2, 1, 1, 1]$. The number of cakes with height $2$ is $3$. The maximum number of cakes with height $2$ is $3$.
for _ in range(int(input())): query = [] n, k = [int(i) for i in input().split()] mx = 0 for _ in range(n): l, r = [int(i) for i in input().split()] query.append((l, r)) mx = max(mx, r) arr = [0] * (mx + 2) arrk = [0] * (mx + 2) arrk1 = [0] * (mx + 2) for l, r in query: arr[l] += 1 arr[r + 1] -= 1 for i in range(mx + 1): arr[i + 1] += arr[i] if arr[i + 1] == k: arrk[i + 1] += 1 if arr[i + 1] == k + 1: arrk1[i + 1] += 1 for i in range(mx + 1): arrk[i + 1] += arrk[i] arrk1[i + 1] += arrk1[i] total = 0 for l, r in query: sub_total = arrk[-1] - (arrk[r] - arrk[l - 1]) + (arrk1[r] - arrk1[l - 1]) total = max(total, sub_total) print(total)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FOR VAR VAR VAR VAR VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR IF VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER NUMBER IF VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR NUMBER FOR VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR NUMBER BIN_OP VAR VAR VAR BIN_OP VAR NUMBER BIN_OP VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR
Read problems statements in [Hindi], [Mandarin Chinese], [Russian], [Vietnamese] and [Bengali] as well. You have $C = 100,000$ cakes, numbered $1$ through $C$. Each cake has an integer height; initially, the height of each cake is $0$. There are $N$ operations. In each operation, you are given two integers $L$ and $R$, and you should increase by $1$ the height of each of the cakes $L, L+1, \ldots, R$. One of these $N$ operations should be removed and the remaining $N-1$ operations are then performed. Chef wants to remove one operation in such a way that after the remaining $N-1$ operations are performed, the number of cakes with height exactly $K$ is maximum possible. Since Chef is a bit busy these days, he has asked for your help. You need to find the maximum number of cakes with height exactly $K$ that can be achieved by removing one operation. ------ Input ------ The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows. The first line of each test case contains two space-separated integers $N$ and $K$. Each of the next $N$ lines contains two space-separated integers $L$ and $R$ describing one operation. ------ Output ------ For each test case, print a single line containing one integer — the maximum possible number of cakes with height $K$. ------ Constraints ------ $1 ≤ T ≤ 100$ $2 ≤ N ≤ 10^{5}$ $1 ≤ K ≤ N$ $1 ≤ L ≤ R ≤ 10^{5}$ the sum of $N$ over all test cases does not exceed $10^{6}$ ----- Sample Input 1 ------ 1 3 2 2 6 4 9 1 4 ----- Sample Output 1 ------ 3 ----- explanation 1 ------ Example case 1: Let's look at what happens after an operation is removed. - Removing operation $1$: The heights of cakes $4$ through $9$ increase by $1$. Then, the heights of cakes $1$ through $4$ increase by $1$. The resulting sequence of heights is $[1, 1, 1, 2, 1, 1, 1, 1, 1]$ (for cakes $1$ through $9$; the other cakes have heights $0$). The number of cakes with height $2$ is $1$. - Removing operation $2$: The resulting sequence of heights of cakes $1$ through $9$ is $[1, 2, 2, 2, 1, 1, 0, 0, 0]$. The number of cakes with height $2$ is $3$. - Removing operation $3$: The resulting sequence of heights of cakes $1$ through $9$ is $[0, 1, 1, 2, 2, 2, 1, 1, 1]$. The number of cakes with height $2$ is $3$. The maximum number of cakes with height $2$ is $3$.
t = int(input()) for i in range(t): n, k = map(int, input().split(" ")) queries = [] p = [0] * 100007 max_r = -1 for j in range(n): l, r = map(int, input().split(" ")) max_r = max(max_r, r) queries.append([l, r]) p[l] += 1 p[r + 1] -= 1 for j in range(1, max_r + 1): p[j] += p[j - 1] results = [0] * (max_r + 2) dp = [0] * (max_r + 2) count = countk = 0 for j in range(1, max_r + 1): countk = 0 count = 0 if p[j] == k + 1: countk = 1 if p[j] == k: count = 1 results[j] = results[j - 1] + count dp[j] = dp[j - 1] + countk values = 0 for j in range(len(queries)): temp = ( results[-2] + dp[queries[j][1]] - dp[queries[j][0] - 1] - results[queries[j][1]] + results[queries[j][0] - 1] ) values = max(values, temp) print(values) del dp, p, queries, results
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR LIST ASSIGN VAR BIN_OP LIST NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR LIST VAR VAR VAR VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF VAR VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER IF VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR NUMBER VAR VAR VAR NUMBER VAR BIN_OP VAR VAR NUMBER NUMBER VAR VAR VAR NUMBER VAR BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR VAR
Read problems statements in [Hindi], [Mandarin Chinese], [Russian], [Vietnamese] and [Bengali] as well. You have $C = 100,000$ cakes, numbered $1$ through $C$. Each cake has an integer height; initially, the height of each cake is $0$. There are $N$ operations. In each operation, you are given two integers $L$ and $R$, and you should increase by $1$ the height of each of the cakes $L, L+1, \ldots, R$. One of these $N$ operations should be removed and the remaining $N-1$ operations are then performed. Chef wants to remove one operation in such a way that after the remaining $N-1$ operations are performed, the number of cakes with height exactly $K$ is maximum possible. Since Chef is a bit busy these days, he has asked for your help. You need to find the maximum number of cakes with height exactly $K$ that can be achieved by removing one operation. ------ Input ------ The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows. The first line of each test case contains two space-separated integers $N$ and $K$. Each of the next $N$ lines contains two space-separated integers $L$ and $R$ describing one operation. ------ Output ------ For each test case, print a single line containing one integer — the maximum possible number of cakes with height $K$. ------ Constraints ------ $1 ≤ T ≤ 100$ $2 ≤ N ≤ 10^{5}$ $1 ≤ K ≤ N$ $1 ≤ L ≤ R ≤ 10^{5}$ the sum of $N$ over all test cases does not exceed $10^{6}$ ----- Sample Input 1 ------ 1 3 2 2 6 4 9 1 4 ----- Sample Output 1 ------ 3 ----- explanation 1 ------ Example case 1: Let's look at what happens after an operation is removed. - Removing operation $1$: The heights of cakes $4$ through $9$ increase by $1$. Then, the heights of cakes $1$ through $4$ increase by $1$. The resulting sequence of heights is $[1, 1, 1, 2, 1, 1, 1, 1, 1]$ (for cakes $1$ through $9$; the other cakes have heights $0$). The number of cakes with height $2$ is $1$. - Removing operation $2$: The resulting sequence of heights of cakes $1$ through $9$ is $[1, 2, 2, 2, 1, 1, 0, 0, 0]$. The number of cakes with height $2$ is $3$. - Removing operation $3$: The resulting sequence of heights of cakes $1$ through $9$ is $[0, 1, 1, 2, 2, 2, 1, 1, 1]$. The number of cakes with height $2$ is $3$. The maximum number of cakes with height $2$ is $3$.
t = int(input()) for _ in range(int(t)): n, k = map(int, input().split()) q = [] sz = 0 for i in range(n): l, r = map(int, input().split()) q.append((l, r)) sz = max(sz, r) arr = [0] * (sz + 2) k1 = [0] * (sz + 2) k2 = [0] * (sz + 2) for l, r in q: arr[l] += 1 arr[r + 1] -= 1 res = [0] * (sz + 2) res[0] = arr[0] for i in range(1, sz + 2): res[i] = arr[i] + res[i - 1] k1[i] = k1[i - 1] k2[i] = k2[i - 1] if res[i] == k: k1[i] += 1 if res[i] == k + 1: k2[i] += 1 ans = -1 for l, r in q: kval = k1[r] - k1[l - 1] k1val = k2[r] - k2[l - 1] ans = max(ans, k1[-1] - kval + k1val, ans) print(ans)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FOR VAR VAR VAR VAR VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER IF VAR VAR VAR VAR VAR NUMBER IF VAR VAR BIN_OP VAR NUMBER VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR NUMBER VAR VAR VAR EXPR FUNC_CALL VAR VAR
Read problems statements in [Hindi], [Mandarin Chinese], [Russian], [Vietnamese] and [Bengali] as well. You have $C = 100,000$ cakes, numbered $1$ through $C$. Each cake has an integer height; initially, the height of each cake is $0$. There are $N$ operations. In each operation, you are given two integers $L$ and $R$, and you should increase by $1$ the height of each of the cakes $L, L+1, \ldots, R$. One of these $N$ operations should be removed and the remaining $N-1$ operations are then performed. Chef wants to remove one operation in such a way that after the remaining $N-1$ operations are performed, the number of cakes with height exactly $K$ is maximum possible. Since Chef is a bit busy these days, he has asked for your help. You need to find the maximum number of cakes with height exactly $K$ that can be achieved by removing one operation. ------ Input ------ The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows. The first line of each test case contains two space-separated integers $N$ and $K$. Each of the next $N$ lines contains two space-separated integers $L$ and $R$ describing one operation. ------ Output ------ For each test case, print a single line containing one integer — the maximum possible number of cakes with height $K$. ------ Constraints ------ $1 ≤ T ≤ 100$ $2 ≤ N ≤ 10^{5}$ $1 ≤ K ≤ N$ $1 ≤ L ≤ R ≤ 10^{5}$ the sum of $N$ over all test cases does not exceed $10^{6}$ ----- Sample Input 1 ------ 1 3 2 2 6 4 9 1 4 ----- Sample Output 1 ------ 3 ----- explanation 1 ------ Example case 1: Let's look at what happens after an operation is removed. - Removing operation $1$: The heights of cakes $4$ through $9$ increase by $1$. Then, the heights of cakes $1$ through $4$ increase by $1$. The resulting sequence of heights is $[1, 1, 1, 2, 1, 1, 1, 1, 1]$ (for cakes $1$ through $9$; the other cakes have heights $0$). The number of cakes with height $2$ is $1$. - Removing operation $2$: The resulting sequence of heights of cakes $1$ through $9$ is $[1, 2, 2, 2, 1, 1, 0, 0, 0]$. The number of cakes with height $2$ is $3$. - Removing operation $3$: The resulting sequence of heights of cakes $1$ through $9$ is $[0, 1, 1, 2, 2, 2, 1, 1, 1]$. The number of cakes with height $2$ is $3$. The maximum number of cakes with height $2$ is $3$.
for _ in range(int(input())): n, k = map(int, input().split()) lt = [] freq = [0] * 100003 maxi = -1 for _ in range(n): l, r = map(int, input().split()) lt.append((l, r)) freq[l] += 1 freq[r + 1] -= 1 maxi = max(maxi, r) for i in range(1, maxi + 1): freq[i] = freq[i - 1] + freq[i] prefix_k = [0] * (maxi + 1) prefix_k_plus = [0] * (maxi + 1) for i in range(1, maxi + 1): prefix_k[i] = prefix_k[i - 1] if freq[i] == k: prefix_k[i] += 1 for i in range(1, maxi + 1): prefix_k_plus[i] = prefix_k_plus[i - 1] if freq[i] == k + 1: prefix_k_plus[i] += 1 idx = 0 curr_k = 0 for i in range(n): l = lt[i][0] r = lt[i][1] local_k = ( prefix_k[-1] - prefix_k[r] + prefix_k[l - 1] + prefix_k_plus[r] - prefix_k_plus[l - 1] ) curr_k = max(local_k, curr_k) print(curr_k)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR BIN_OP LIST NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER IF VAR VAR VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER IF VAR VAR BIN_OP VAR NUMBER VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR
Read problems statements in [Hindi], [Mandarin Chinese], [Russian], [Vietnamese] and [Bengali] as well. You have $C = 100,000$ cakes, numbered $1$ through $C$. Each cake has an integer height; initially, the height of each cake is $0$. There are $N$ operations. In each operation, you are given two integers $L$ and $R$, and you should increase by $1$ the height of each of the cakes $L, L+1, \ldots, R$. One of these $N$ operations should be removed and the remaining $N-1$ operations are then performed. Chef wants to remove one operation in such a way that after the remaining $N-1$ operations are performed, the number of cakes with height exactly $K$ is maximum possible. Since Chef is a bit busy these days, he has asked for your help. You need to find the maximum number of cakes with height exactly $K$ that can be achieved by removing one operation. ------ Input ------ The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows. The first line of each test case contains two space-separated integers $N$ and $K$. Each of the next $N$ lines contains two space-separated integers $L$ and $R$ describing one operation. ------ Output ------ For each test case, print a single line containing one integer — the maximum possible number of cakes with height $K$. ------ Constraints ------ $1 ≤ T ≤ 100$ $2 ≤ N ≤ 10^{5}$ $1 ≤ K ≤ N$ $1 ≤ L ≤ R ≤ 10^{5}$ the sum of $N$ over all test cases does not exceed $10^{6}$ ----- Sample Input 1 ------ 1 3 2 2 6 4 9 1 4 ----- Sample Output 1 ------ 3 ----- explanation 1 ------ Example case 1: Let's look at what happens after an operation is removed. - Removing operation $1$: The heights of cakes $4$ through $9$ increase by $1$. Then, the heights of cakes $1$ through $4$ increase by $1$. The resulting sequence of heights is $[1, 1, 1, 2, 1, 1, 1, 1, 1]$ (for cakes $1$ through $9$; the other cakes have heights $0$). The number of cakes with height $2$ is $1$. - Removing operation $2$: The resulting sequence of heights of cakes $1$ through $9$ is $[1, 2, 2, 2, 1, 1, 0, 0, 0]$. The number of cakes with height $2$ is $3$. - Removing operation $3$: The resulting sequence of heights of cakes $1$ through $9$ is $[0, 1, 1, 2, 2, 2, 1, 1, 1]$. The number of cakes with height $2$ is $3$. The maximum number of cakes with height $2$ is $3$.
for _ in range(int(input())): n, k = map(int, input().split()) a = [] maxlen = 0 for _ in range(n): l, r = map(int, input().split()) a.append((l, r)) maxlen = max(maxlen, r) cakes = [0] * (maxlen + 2) for i in a: l, r = i[0], i[1] cakes[l - 1] += 1 cakes[r] -= 1 for i in range(1, len(cakes)): cakes[i] += cakes[i - 1] b = [] d = [] c = 0 f = 0 result = 0 for i in range(len(cakes)): if cakes[i] == k: c += 1 if cakes[i] == k + 1: f += 1 b.append(c) d.append(f) for i in range(n): l, r = a[i][0], a[i][1] nk = b[r - 1] - (0 if l == 1 else b[l - 2]) nk1 = d[r - 1] - (0 if l == 1 else d[l - 2]) tnk = nk1 - nk result = max(result, c + tnk) print(result)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FOR VAR VAR ASSIGN VAR VAR VAR NUMBER VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR NUMBER IF VAR VAR BIN_OP VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR
Read problems statements in [Hindi], [Mandarin Chinese], [Russian], [Vietnamese] and [Bengali] as well. You have $C = 100,000$ cakes, numbered $1$ through $C$. Each cake has an integer height; initially, the height of each cake is $0$. There are $N$ operations. In each operation, you are given two integers $L$ and $R$, and you should increase by $1$ the height of each of the cakes $L, L+1, \ldots, R$. One of these $N$ operations should be removed and the remaining $N-1$ operations are then performed. Chef wants to remove one operation in such a way that after the remaining $N-1$ operations are performed, the number of cakes with height exactly $K$ is maximum possible. Since Chef is a bit busy these days, he has asked for your help. You need to find the maximum number of cakes with height exactly $K$ that can be achieved by removing one operation. ------ Input ------ The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows. The first line of each test case contains two space-separated integers $N$ and $K$. Each of the next $N$ lines contains two space-separated integers $L$ and $R$ describing one operation. ------ Output ------ For each test case, print a single line containing one integer — the maximum possible number of cakes with height $K$. ------ Constraints ------ $1 ≤ T ≤ 100$ $2 ≤ N ≤ 10^{5}$ $1 ≤ K ≤ N$ $1 ≤ L ≤ R ≤ 10^{5}$ the sum of $N$ over all test cases does not exceed $10^{6}$ ----- Sample Input 1 ------ 1 3 2 2 6 4 9 1 4 ----- Sample Output 1 ------ 3 ----- explanation 1 ------ Example case 1: Let's look at what happens after an operation is removed. - Removing operation $1$: The heights of cakes $4$ through $9$ increase by $1$. Then, the heights of cakes $1$ through $4$ increase by $1$. The resulting sequence of heights is $[1, 1, 1, 2, 1, 1, 1, 1, 1]$ (for cakes $1$ through $9$; the other cakes have heights $0$). The number of cakes with height $2$ is $1$. - Removing operation $2$: The resulting sequence of heights of cakes $1$ through $9$ is $[1, 2, 2, 2, 1, 1, 0, 0, 0]$. The number of cakes with height $2$ is $3$. - Removing operation $3$: The resulting sequence of heights of cakes $1$ through $9$ is $[0, 1, 1, 2, 2, 2, 1, 1, 1]$. The number of cakes with height $2$ is $3$. The maximum number of cakes with height $2$ is $3$.
c = 100000 t = int(input()) for _ in range(t): n, k = map(int, input().split()) dif = [0] * (c + 1) ans = [0] * c rmax = 0 queries = [] for i in range(n): l, r = map(int, input().split()) queries.append([l, r]) rmax = max(rmax, r) dif[l - 1] += 1 dif[r] -= 1 ans[0] = dif[0] s = [0] * (rmax + 1) m = [0] * (rmax + 1) for i in range(rmax): ans[i] = ans[i - 1] + dif[i] s[i + 1] = s[i] m[i + 1] = m[i] if ans[i] == k + 1: s[i + 1] += 1 if ans[i] == k: m[i + 1] += 1 result = 0 total = m[-1] for l, r in queries: nk1 = s[r] - s[l - 1] nk = m[r] - m[l - 1] result = max(result, total + nk1 - nk) print(result)
ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR LIST VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER NUMBER VAR VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR BIN_OP VAR NUMBER VAR VAR IF VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER IF VAR VAR VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR VAR
Read problems statements in [Hindi], [Mandarin Chinese], [Russian], [Vietnamese] and [Bengali] as well. You have $C = 100,000$ cakes, numbered $1$ through $C$. Each cake has an integer height; initially, the height of each cake is $0$. There are $N$ operations. In each operation, you are given two integers $L$ and $R$, and you should increase by $1$ the height of each of the cakes $L, L+1, \ldots, R$. One of these $N$ operations should be removed and the remaining $N-1$ operations are then performed. Chef wants to remove one operation in such a way that after the remaining $N-1$ operations are performed, the number of cakes with height exactly $K$ is maximum possible. Since Chef is a bit busy these days, he has asked for your help. You need to find the maximum number of cakes with height exactly $K$ that can be achieved by removing one operation. ------ Input ------ The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows. The first line of each test case contains two space-separated integers $N$ and $K$. Each of the next $N$ lines contains two space-separated integers $L$ and $R$ describing one operation. ------ Output ------ For each test case, print a single line containing one integer — the maximum possible number of cakes with height $K$. ------ Constraints ------ $1 ≤ T ≤ 100$ $2 ≤ N ≤ 10^{5}$ $1 ≤ K ≤ N$ $1 ≤ L ≤ R ≤ 10^{5}$ the sum of $N$ over all test cases does not exceed $10^{6}$ ----- Sample Input 1 ------ 1 3 2 2 6 4 9 1 4 ----- Sample Output 1 ------ 3 ----- explanation 1 ------ Example case 1: Let's look at what happens after an operation is removed. - Removing operation $1$: The heights of cakes $4$ through $9$ increase by $1$. Then, the heights of cakes $1$ through $4$ increase by $1$. The resulting sequence of heights is $[1, 1, 1, 2, 1, 1, 1, 1, 1]$ (for cakes $1$ through $9$; the other cakes have heights $0$). The number of cakes with height $2$ is $1$. - Removing operation $2$: The resulting sequence of heights of cakes $1$ through $9$ is $[1, 2, 2, 2, 1, 1, 0, 0, 0]$. The number of cakes with height $2$ is $3$. - Removing operation $3$: The resulting sequence of heights of cakes $1$ through $9$ is $[0, 1, 1, 2, 2, 2, 1, 1, 1]$. The number of cakes with height $2$ is $3$. The maximum number of cakes with height $2$ is $3$.
for _ in range(int(input())): ma = 100009 n, k = map(int, input().split()) mat = [] mind = 0 for i in range(n): l, r = map(int, input().split()) mat.append([l, r]) mind = max(mind, r) ma = mind + 5 couk = [(0) for i in range(ma)] for i in range(n): l, r = mat[i][0], mat[i][1] couk[l] += 1 couk[r + 1] -= 1 pre = [(0) for i in range(ma)] pre2 = [(0) for i in range(ma)] for i in range(1, ma): couk[i] = couk[i - 1] + couk[i] if couk[i] == k: pre[i] = 1 if couk[i] == k + 1: pre2[i] = 1 pre[i] += pre[i - 1] pre2[i] += pre2[i - 1] total_k = pre[ma - 1] ans = 0 for i in range(n): ck = pre[mat[i][1]] - pre[mat[i][0] - 1] ck1 = pre2[mat[i][1]] - pre2[mat[i][0] - 1] temp = ck1 - ck + total_k ans = max(ans, temp) print(ans)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR LIST VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR NUMBER VAR VAR NUMBER VAR VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR IF VAR VAR VAR ASSIGN VAR VAR NUMBER IF VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR NUMBER VAR BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR VAR VAR NUMBER VAR BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR
Read problems statements in [Hindi], [Mandarin Chinese], [Russian], [Vietnamese] and [Bengali] as well. You have $C = 100,000$ cakes, numbered $1$ through $C$. Each cake has an integer height; initially, the height of each cake is $0$. There are $N$ operations. In each operation, you are given two integers $L$ and $R$, and you should increase by $1$ the height of each of the cakes $L, L+1, \ldots, R$. One of these $N$ operations should be removed and the remaining $N-1$ operations are then performed. Chef wants to remove one operation in such a way that after the remaining $N-1$ operations are performed, the number of cakes with height exactly $K$ is maximum possible. Since Chef is a bit busy these days, he has asked for your help. You need to find the maximum number of cakes with height exactly $K$ that can be achieved by removing one operation. ------ Input ------ The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows. The first line of each test case contains two space-separated integers $N$ and $K$. Each of the next $N$ lines contains two space-separated integers $L$ and $R$ describing one operation. ------ Output ------ For each test case, print a single line containing one integer — the maximum possible number of cakes with height $K$. ------ Constraints ------ $1 ≤ T ≤ 100$ $2 ≤ N ≤ 10^{5}$ $1 ≤ K ≤ N$ $1 ≤ L ≤ R ≤ 10^{5}$ the sum of $N$ over all test cases does not exceed $10^{6}$ ----- Sample Input 1 ------ 1 3 2 2 6 4 9 1 4 ----- Sample Output 1 ------ 3 ----- explanation 1 ------ Example case 1: Let's look at what happens after an operation is removed. - Removing operation $1$: The heights of cakes $4$ through $9$ increase by $1$. Then, the heights of cakes $1$ through $4$ increase by $1$. The resulting sequence of heights is $[1, 1, 1, 2, 1, 1, 1, 1, 1]$ (for cakes $1$ through $9$; the other cakes have heights $0$). The number of cakes with height $2$ is $1$. - Removing operation $2$: The resulting sequence of heights of cakes $1$ through $9$ is $[1, 2, 2, 2, 1, 1, 0, 0, 0]$. The number of cakes with height $2$ is $3$. - Removing operation $3$: The resulting sequence of heights of cakes $1$ through $9$ is $[0, 1, 1, 2, 2, 2, 1, 1, 1]$. The number of cakes with height $2$ is $3$. The maximum number of cakes with height $2$ is $3$.
t = int(input().strip()) for _ in range(t): n, k = map(int, input().split()) a = [] m = 0 for _ in range(n): l, r = map(int, input().split()) a.append([l, r]) m = max(m, r) a1 = [0] * (m + 2) a2 = [0] * (m + 2) a3 = [0] * (m + 2) for j in range(n): a1[a[j][0]] += 1 a1[a[j][1] + 1] -= 1 for j in range(1, m + 2, 1): a1[j] = a1[j - 1] + a1[j] for j in range(m + 2): if a1[j] == k: a2[j] = 1 if a1[j] == k + 1: a3[j] = 1 for j in range(1, m + 2, 1): a2[j] = a2[j - 1] + a2[j] a3[j] = a3[j - 1] + a3[j] maxi = 0 for i in range(n): tt = 0 t1 = a2[a[i][1]] - a2[a[i][0] - 1] tt += a2[m] - t1 t1 = a3[a[i][1]] - a3[a[i][0] - 1] tt += t1 maxi = max(maxi, tt) print(maxi)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR LIST VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR VAR NUMBER NUMBER VAR BIN_OP VAR VAR NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR VAR VAR ASSIGN VAR VAR NUMBER IF VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR NUMBER VAR BIN_OP VAR VAR NUMBER NUMBER VAR BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR NUMBER VAR BIN_OP VAR VAR NUMBER NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR
Read problems statements in [Hindi], [Mandarin Chinese], [Russian], [Vietnamese] and [Bengali] as well. You have $C = 100,000$ cakes, numbered $1$ through $C$. Each cake has an integer height; initially, the height of each cake is $0$. There are $N$ operations. In each operation, you are given two integers $L$ and $R$, and you should increase by $1$ the height of each of the cakes $L, L+1, \ldots, R$. One of these $N$ operations should be removed and the remaining $N-1$ operations are then performed. Chef wants to remove one operation in such a way that after the remaining $N-1$ operations are performed, the number of cakes with height exactly $K$ is maximum possible. Since Chef is a bit busy these days, he has asked for your help. You need to find the maximum number of cakes with height exactly $K$ that can be achieved by removing one operation. ------ Input ------ The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows. The first line of each test case contains two space-separated integers $N$ and $K$. Each of the next $N$ lines contains two space-separated integers $L$ and $R$ describing one operation. ------ Output ------ For each test case, print a single line containing one integer — the maximum possible number of cakes with height $K$. ------ Constraints ------ $1 ≤ T ≤ 100$ $2 ≤ N ≤ 10^{5}$ $1 ≤ K ≤ N$ $1 ≤ L ≤ R ≤ 10^{5}$ the sum of $N$ over all test cases does not exceed $10^{6}$ ----- Sample Input 1 ------ 1 3 2 2 6 4 9 1 4 ----- Sample Output 1 ------ 3 ----- explanation 1 ------ Example case 1: Let's look at what happens after an operation is removed. - Removing operation $1$: The heights of cakes $4$ through $9$ increase by $1$. Then, the heights of cakes $1$ through $4$ increase by $1$. The resulting sequence of heights is $[1, 1, 1, 2, 1, 1, 1, 1, 1]$ (for cakes $1$ through $9$; the other cakes have heights $0$). The number of cakes with height $2$ is $1$. - Removing operation $2$: The resulting sequence of heights of cakes $1$ through $9$ is $[1, 2, 2, 2, 1, 1, 0, 0, 0]$. The number of cakes with height $2$ is $3$. - Removing operation $3$: The resulting sequence of heights of cakes $1$ through $9$ is $[0, 1, 1, 2, 2, 2, 1, 1, 1]$. The number of cakes with height $2$ is $3$. The maximum number of cakes with height $2$ is $3$.
for t in range(int(input())): n, k = map(int, input().split()) l = [0] * n r = [0] * n for i in range(n): a, b = map(int, input().split()) l[i] = a r[i] = b MAX = max(r) + 2 psum = [0] * MAX for i in range(n): psum[l[i]] += 1 psum[r[i] + 1] -= 1 for i in range(1, MAX): psum[i] = psum[i] + psum[i - 1] psk = [0] * MAX pskp = [0] * MAX for i in range(1, MAX): if psum[i] == k: psk[i] = psk[i - 1] + 1 else: psk[i] = psk[i - 1] if psum[i] == k + 1: pskp[i] = pskp[i - 1] + 1 else: pskp[i] = pskp[i - 1] mk = -1000000 for i in range(n): nk = psk[r[i]] - psk[l[i] - 1] nkp = pskp[r[i]] - pskp[l[i] - 1] mk = max(mk, -nk + nkp) print(psk[MAX - 1] + mk)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR VAR VAR VAR VAR NUMBER VAR BIN_OP VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER IF VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR VAR BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR VAR BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER VAR
Read problems statements in [Hindi], [Mandarin Chinese], [Russian], [Vietnamese] and [Bengali] as well. You have $C = 100,000$ cakes, numbered $1$ through $C$. Each cake has an integer height; initially, the height of each cake is $0$. There are $N$ operations. In each operation, you are given two integers $L$ and $R$, and you should increase by $1$ the height of each of the cakes $L, L+1, \ldots, R$. One of these $N$ operations should be removed and the remaining $N-1$ operations are then performed. Chef wants to remove one operation in such a way that after the remaining $N-1$ operations are performed, the number of cakes with height exactly $K$ is maximum possible. Since Chef is a bit busy these days, he has asked for your help. You need to find the maximum number of cakes with height exactly $K$ that can be achieved by removing one operation. ------ Input ------ The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows. The first line of each test case contains two space-separated integers $N$ and $K$. Each of the next $N$ lines contains two space-separated integers $L$ and $R$ describing one operation. ------ Output ------ For each test case, print a single line containing one integer — the maximum possible number of cakes with height $K$. ------ Constraints ------ $1 ≤ T ≤ 100$ $2 ≤ N ≤ 10^{5}$ $1 ≤ K ≤ N$ $1 ≤ L ≤ R ≤ 10^{5}$ the sum of $N$ over all test cases does not exceed $10^{6}$ ----- Sample Input 1 ------ 1 3 2 2 6 4 9 1 4 ----- Sample Output 1 ------ 3 ----- explanation 1 ------ Example case 1: Let's look at what happens after an operation is removed. - Removing operation $1$: The heights of cakes $4$ through $9$ increase by $1$. Then, the heights of cakes $1$ through $4$ increase by $1$. The resulting sequence of heights is $[1, 1, 1, 2, 1, 1, 1, 1, 1]$ (for cakes $1$ through $9$; the other cakes have heights $0$). The number of cakes with height $2$ is $1$. - Removing operation $2$: The resulting sequence of heights of cakes $1$ through $9$ is $[1, 2, 2, 2, 1, 1, 0, 0, 0]$. The number of cakes with height $2$ is $3$. - Removing operation $3$: The resulting sequence of heights of cakes $1$ through $9$ is $[0, 1, 1, 2, 2, 2, 1, 1, 1]$. The number of cakes with height $2$ is $3$. The maximum number of cakes with height $2$ is $3$.
for _ in range(int(input())): n, k = map(int, input().split()) l = [0] * (10**5 + 1) op = [] ans = 0 maxi = 0 for i in range(n): a, b = map(int, input().split()) maxi = max(b, maxi) op.append([a, b]) l[a] += 1 l[b + 1] -= 1 arr = [0] * (maxi + 1) for i in range(1, maxi + 1): arr[i] = arr[i - 1] + l[i] count = 0 freq = [0] * (maxi + 1) freq1 = [0] * (maxi + 1) for i in range(1, maxi + 1): freq[i] = freq[i - 1] freq1[i] = freq1[i - 1] if arr[i] == k: freq[i] += 1 elif arr[i] == k + 1: freq1[i] += 1 val = arr.count(k) for i in range(n): cou = freq[op[i][1]] - freq[op[i][0] - 1] cou1 = freq1[op[i][1]] - freq1[op[i][0] - 1] count = val - cou + cou1 if count > ans: ans = count print(ans)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP BIN_OP NUMBER NUMBER NUMBER ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR LIST VAR VAR VAR VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER IF VAR VAR VAR VAR VAR NUMBER IF VAR VAR BIN_OP VAR NUMBER VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR NUMBER VAR BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR VAR VAR NUMBER VAR BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR IF VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR
Read problems statements in [Hindi], [Mandarin Chinese], [Russian], [Vietnamese] and [Bengali] as well. You have $C = 100,000$ cakes, numbered $1$ through $C$. Each cake has an integer height; initially, the height of each cake is $0$. There are $N$ operations. In each operation, you are given two integers $L$ and $R$, and you should increase by $1$ the height of each of the cakes $L, L+1, \ldots, R$. One of these $N$ operations should be removed and the remaining $N-1$ operations are then performed. Chef wants to remove one operation in such a way that after the remaining $N-1$ operations are performed, the number of cakes with height exactly $K$ is maximum possible. Since Chef is a bit busy these days, he has asked for your help. You need to find the maximum number of cakes with height exactly $K$ that can be achieved by removing one operation. ------ Input ------ The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows. The first line of each test case contains two space-separated integers $N$ and $K$. Each of the next $N$ lines contains two space-separated integers $L$ and $R$ describing one operation. ------ Output ------ For each test case, print a single line containing one integer — the maximum possible number of cakes with height $K$. ------ Constraints ------ $1 ≤ T ≤ 100$ $2 ≤ N ≤ 10^{5}$ $1 ≤ K ≤ N$ $1 ≤ L ≤ R ≤ 10^{5}$ the sum of $N$ over all test cases does not exceed $10^{6}$ ----- Sample Input 1 ------ 1 3 2 2 6 4 9 1 4 ----- Sample Output 1 ------ 3 ----- explanation 1 ------ Example case 1: Let's look at what happens after an operation is removed. - Removing operation $1$: The heights of cakes $4$ through $9$ increase by $1$. Then, the heights of cakes $1$ through $4$ increase by $1$. The resulting sequence of heights is $[1, 1, 1, 2, 1, 1, 1, 1, 1]$ (for cakes $1$ through $9$; the other cakes have heights $0$). The number of cakes with height $2$ is $1$. - Removing operation $2$: The resulting sequence of heights of cakes $1$ through $9$ is $[1, 2, 2, 2, 1, 1, 0, 0, 0]$. The number of cakes with height $2$ is $3$. - Removing operation $3$: The resulting sequence of heights of cakes $1$ through $9$ is $[0, 1, 1, 2, 2, 2, 1, 1, 1]$. The number of cakes with height $2$ is $3$. The maximum number of cakes with height $2$ is $3$.
def max_range(n, k, l): max = 0 h = 0 x = 0 y = 0 p = 0 q = 0 f = l[0][0] g = l[0][1] cake = [0] * 100000 for node in range(n): cake[l[node][0] - 1] += 1 cake[l[node][1]] -= 1 if l[node][0] < f: f = l[node][0] if l[node][1] > g: g = l[node][1] c = [0] * (g - f + 2) for i in range(f - 1, g + 1): if h + cake[i] == k + 1: c[i - (f - 1)] = q + 1 q += 1 else: c[i - (f - 1)] = q if h + cake[i] == k: x += 1 h = h + cake[i] cake[i] = p + 1 p += 1 else: h = h + cake[i] cake[i] = p for node in range(n): count = 0 if l[node][0] == 1: count = c[l[node][1] - 1 - (f - 1)] + x - cake[l[node][1] - 1] else: count = ( c[l[node][1] - 1 - (f - 1)] - c[l[node][0] - 2 - (f - 1)] + x - (cake[l[node][1] - 1] - cake[l[node][0] - 2]) ) if count > max: max = count return max t = int(input()) for x in range(t): s = input().split(" ") N = int(s[0]) K = int(s[1]) l = [] for i in range(N): l1 = [] s = input().split(" ") l1.append(int(s[0])) l1.append(int(s[1])) l.append(l1) result = max_range(N, K, l) print(result)
FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR BIN_OP LIST NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR NUMBER NUMBER NUMBER VAR VAR VAR NUMBER NUMBER IF VAR VAR NUMBER VAR ASSIGN VAR VAR VAR NUMBER IF VAR VAR NUMBER VAR ASSIGN VAR VAR VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP BIN_OP VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER IF BIN_OP VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER VAR IF BIN_OP VAR VAR VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR ASSIGN VAR VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER IF VAR VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP BIN_OP VAR VAR NUMBER NUMBER BIN_OP VAR NUMBER VAR VAR BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR BIN_OP BIN_OP VAR VAR NUMBER NUMBER BIN_OP VAR NUMBER VAR BIN_OP BIN_OP VAR VAR NUMBER NUMBER BIN_OP VAR NUMBER VAR BIN_OP VAR BIN_OP VAR VAR NUMBER NUMBER VAR BIN_OP VAR VAR NUMBER NUMBER IF VAR VAR ASSIGN VAR VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR LIST ASSIGN VAR FUNC_CALL FUNC_CALL VAR STRING EXPR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR
Read problems statements in [Hindi], [Mandarin Chinese], [Russian], [Vietnamese] and [Bengali] as well. You have $C = 100,000$ cakes, numbered $1$ through $C$. Each cake has an integer height; initially, the height of each cake is $0$. There are $N$ operations. In each operation, you are given two integers $L$ and $R$, and you should increase by $1$ the height of each of the cakes $L, L+1, \ldots, R$. One of these $N$ operations should be removed and the remaining $N-1$ operations are then performed. Chef wants to remove one operation in such a way that after the remaining $N-1$ operations are performed, the number of cakes with height exactly $K$ is maximum possible. Since Chef is a bit busy these days, he has asked for your help. You need to find the maximum number of cakes with height exactly $K$ that can be achieved by removing one operation. ------ Input ------ The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows. The first line of each test case contains two space-separated integers $N$ and $K$. Each of the next $N$ lines contains two space-separated integers $L$ and $R$ describing one operation. ------ Output ------ For each test case, print a single line containing one integer — the maximum possible number of cakes with height $K$. ------ Constraints ------ $1 ≤ T ≤ 100$ $2 ≤ N ≤ 10^{5}$ $1 ≤ K ≤ N$ $1 ≤ L ≤ R ≤ 10^{5}$ the sum of $N$ over all test cases does not exceed $10^{6}$ ----- Sample Input 1 ------ 1 3 2 2 6 4 9 1 4 ----- Sample Output 1 ------ 3 ----- explanation 1 ------ Example case 1: Let's look at what happens after an operation is removed. - Removing operation $1$: The heights of cakes $4$ through $9$ increase by $1$. Then, the heights of cakes $1$ through $4$ increase by $1$. The resulting sequence of heights is $[1, 1, 1, 2, 1, 1, 1, 1, 1]$ (for cakes $1$ through $9$; the other cakes have heights $0$). The number of cakes with height $2$ is $1$. - Removing operation $2$: The resulting sequence of heights of cakes $1$ through $9$ is $[1, 2, 2, 2, 1, 1, 0, 0, 0]$. The number of cakes with height $2$ is $3$. - Removing operation $3$: The resulting sequence of heights of cakes $1$ through $9$ is $[0, 1, 1, 2, 2, 2, 1, 1, 1]$. The number of cakes with height $2$ is $3$. The maximum number of cakes with height $2$ is $3$.
for _ in range(int(input())): n, k = [int(x) for x in input().split()] a = [] max_r = 0 for i in range(n): l, r = [int(x) for x in input().split()] max_r = max(max_r, r) a.append([l, r]) max_r += 2 change = [0] * max_r for i in a: change[i[0]] += 1 change[i[1] + 1] -= 1 for i in range(1, len(change)): change[i] += change[i - 1] mxk = 0 kar = [0] * max_r kpar = [0] * max_r for i in range(len(change)): if change[i] == k: kar[i] += 1 if change[i] == k + 1: kpar[i] += 1 for i in range(1, len(change)): kar[i] += kar[i - 1] kpar[i] += kpar[i - 1] for i in a: l, r = i[0], i[1] sayavar = kar[-1] - (kar[r] - kar[l - 1]) + (kpar[r] - kpar[l - 1]) mxk = max(mxk, sayavar) print(mxk)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR LIST VAR VAR VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR VAR VAR VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR NUMBER IF VAR VAR BIN_OP VAR NUMBER VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER FOR VAR VAR ASSIGN VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER BIN_OP VAR VAR VAR BIN_OP VAR NUMBER BIN_OP VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR
Read problems statements in [Hindi], [Mandarin Chinese], [Russian], [Vietnamese] and [Bengali] as well. You have $C = 100,000$ cakes, numbered $1$ through $C$. Each cake has an integer height; initially, the height of each cake is $0$. There are $N$ operations. In each operation, you are given two integers $L$ and $R$, and you should increase by $1$ the height of each of the cakes $L, L+1, \ldots, R$. One of these $N$ operations should be removed and the remaining $N-1$ operations are then performed. Chef wants to remove one operation in such a way that after the remaining $N-1$ operations are performed, the number of cakes with height exactly $K$ is maximum possible. Since Chef is a bit busy these days, he has asked for your help. You need to find the maximum number of cakes with height exactly $K$ that can be achieved by removing one operation. ------ Input ------ The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows. The first line of each test case contains two space-separated integers $N$ and $K$. Each of the next $N$ lines contains two space-separated integers $L$ and $R$ describing one operation. ------ Output ------ For each test case, print a single line containing one integer — the maximum possible number of cakes with height $K$. ------ Constraints ------ $1 ≤ T ≤ 100$ $2 ≤ N ≤ 10^{5}$ $1 ≤ K ≤ N$ $1 ≤ L ≤ R ≤ 10^{5}$ the sum of $N$ over all test cases does not exceed $10^{6}$ ----- Sample Input 1 ------ 1 3 2 2 6 4 9 1 4 ----- Sample Output 1 ------ 3 ----- explanation 1 ------ Example case 1: Let's look at what happens after an operation is removed. - Removing operation $1$: The heights of cakes $4$ through $9$ increase by $1$. Then, the heights of cakes $1$ through $4$ increase by $1$. The resulting sequence of heights is $[1, 1, 1, 2, 1, 1, 1, 1, 1]$ (for cakes $1$ through $9$; the other cakes have heights $0$). The number of cakes with height $2$ is $1$. - Removing operation $2$: The resulting sequence of heights of cakes $1$ through $9$ is $[1, 2, 2, 2, 1, 1, 0, 0, 0]$. The number of cakes with height $2$ is $3$. - Removing operation $3$: The resulting sequence of heights of cakes $1$ through $9$ is $[0, 1, 1, 2, 2, 2, 1, 1, 1]$. The number of cakes with height $2$ is $3$. The maximum number of cakes with height $2$ is $3$.
def sol(): n, k = map(int, input().split()) a = [] mx = 0 for _ in range(n): l, r = map(int, input().split()) mx = max(mx, r) a.append([l, r]) ans = [0] * (mx + 2) prek = [0] * (mx + 2) prek1 = [0] * (mx + 2) for l, r in a: ans[l] += 1 ans[r + 1] -= 1 for i in range(1, mx + 1): ans[i] += ans[i - 1] for i in range(1, mx + 1): prek[i] = 1 if ans[i] == k else 0 prek1[i] = 1 if ans[i] == k + 1 else 0 for i in range(1, mx + 1): prek[i] += prek[i - 1] prek1[i] += prek1[i - 1] res = 0 for l, r in a: res = max(res, prek[l - 1] + prek[mx] - prek[r] + prek1[r] - prek1[l - 1]) print(res) def main(): for _ in range(int(input())): sol() main()
FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR LIST VAR VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FOR VAR VAR VAR VAR VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR VAR VAR VAR NUMBER NUMBER ASSIGN VAR VAR VAR VAR BIN_OP VAR NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR VAR VAR VAR VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR FUNC_DEF FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR
Read problems statements in [Hindi], [Mandarin Chinese], [Russian], [Vietnamese] and [Bengali] as well. You have $C = 100,000$ cakes, numbered $1$ through $C$. Each cake has an integer height; initially, the height of each cake is $0$. There are $N$ operations. In each operation, you are given two integers $L$ and $R$, and you should increase by $1$ the height of each of the cakes $L, L+1, \ldots, R$. One of these $N$ operations should be removed and the remaining $N-1$ operations are then performed. Chef wants to remove one operation in such a way that after the remaining $N-1$ operations are performed, the number of cakes with height exactly $K$ is maximum possible. Since Chef is a bit busy these days, he has asked for your help. You need to find the maximum number of cakes with height exactly $K$ that can be achieved by removing one operation. ------ Input ------ The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows. The first line of each test case contains two space-separated integers $N$ and $K$. Each of the next $N$ lines contains two space-separated integers $L$ and $R$ describing one operation. ------ Output ------ For each test case, print a single line containing one integer — the maximum possible number of cakes with height $K$. ------ Constraints ------ $1 ≤ T ≤ 100$ $2 ≤ N ≤ 10^{5}$ $1 ≤ K ≤ N$ $1 ≤ L ≤ R ≤ 10^{5}$ the sum of $N$ over all test cases does not exceed $10^{6}$ ----- Sample Input 1 ------ 1 3 2 2 6 4 9 1 4 ----- Sample Output 1 ------ 3 ----- explanation 1 ------ Example case 1: Let's look at what happens after an operation is removed. - Removing operation $1$: The heights of cakes $4$ through $9$ increase by $1$. Then, the heights of cakes $1$ through $4$ increase by $1$. The resulting sequence of heights is $[1, 1, 1, 2, 1, 1, 1, 1, 1]$ (for cakes $1$ through $9$; the other cakes have heights $0$). The number of cakes with height $2$ is $1$. - Removing operation $2$: The resulting sequence of heights of cakes $1$ through $9$ is $[1, 2, 2, 2, 1, 1, 0, 0, 0]$. The number of cakes with height $2$ is $3$. - Removing operation $3$: The resulting sequence of heights of cakes $1$ through $9$ is $[0, 1, 1, 2, 2, 2, 1, 1, 1]$. The number of cakes with height $2$ is $3$. The maximum number of cakes with height $2$ is $3$.
t = int(input().strip()) for _ in range(t): n, k = map(int, input().split()) l = [] sb = [] mx = 0 for i in range(n): x, y = map(int, input().split()) l.append((x, y)) mx = max(mx, y) a = [] a = [(0) for i in range(mx + 1)] for t in l: a[t[0] - 1] += 1 a[t[1]] -= 1 for i in range(1, mx): a[i] += a[i - 1] kPlusOneArray = [0] * mx kArray = [0] * mx for i in range(mx): if a[i] == k: kArray[i] = 1 if a[i] == k + 1: kPlusOneArray[i] = 1 for i in range(1, mx): kArray[i] = kArray[i] + kArray[i - 1] kPlusOneArray[i] = kPlusOneArray[i] + kPlusOneArray[i - 1] ans = 0 for tuple in l: if tuple[0] > 1: kPlusOneAns = kPlusOneArray[tuple[1] - 1] - kPlusOneArray[tuple[0] - 2] kAns = kArray[tuple[1] - 1] - kArray[tuple[0] - 2] else: kPlusOneAns = kPlusOneArray[tuple[1] - 1] kAns = kArray[tuple[1] - 1] if ans < kArray[mx - 1] + kPlusOneAns - kAns: ans = kArray[mx - 1] + kPlusOneAns - kAns print(ans)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR LIST ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR VAR VAR BIN_OP VAR NUMBER NUMBER NUMBER VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR ASSIGN VAR VAR NUMBER IF VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER NUMBER IF VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR
Read problems statements in [Hindi], [Mandarin Chinese], [Russian], [Vietnamese] and [Bengali] as well. You have $C = 100,000$ cakes, numbered $1$ through $C$. Each cake has an integer height; initially, the height of each cake is $0$. There are $N$ operations. In each operation, you are given two integers $L$ and $R$, and you should increase by $1$ the height of each of the cakes $L, L+1, \ldots, R$. One of these $N$ operations should be removed and the remaining $N-1$ operations are then performed. Chef wants to remove one operation in such a way that after the remaining $N-1$ operations are performed, the number of cakes with height exactly $K$ is maximum possible. Since Chef is a bit busy these days, he has asked for your help. You need to find the maximum number of cakes with height exactly $K$ that can be achieved by removing one operation. ------ Input ------ The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows. The first line of each test case contains two space-separated integers $N$ and $K$. Each of the next $N$ lines contains two space-separated integers $L$ and $R$ describing one operation. ------ Output ------ For each test case, print a single line containing one integer — the maximum possible number of cakes with height $K$. ------ Constraints ------ $1 ≤ T ≤ 100$ $2 ≤ N ≤ 10^{5}$ $1 ≤ K ≤ N$ $1 ≤ L ≤ R ≤ 10^{5}$ the sum of $N$ over all test cases does not exceed $10^{6}$ ----- Sample Input 1 ------ 1 3 2 2 6 4 9 1 4 ----- Sample Output 1 ------ 3 ----- explanation 1 ------ Example case 1: Let's look at what happens after an operation is removed. - Removing operation $1$: The heights of cakes $4$ through $9$ increase by $1$. Then, the heights of cakes $1$ through $4$ increase by $1$. The resulting sequence of heights is $[1, 1, 1, 2, 1, 1, 1, 1, 1]$ (for cakes $1$ through $9$; the other cakes have heights $0$). The number of cakes with height $2$ is $1$. - Removing operation $2$: The resulting sequence of heights of cakes $1$ through $9$ is $[1, 2, 2, 2, 1, 1, 0, 0, 0]$. The number of cakes with height $2$ is $3$. - Removing operation $3$: The resulting sequence of heights of cakes $1$ through $9$ is $[0, 1, 1, 2, 2, 2, 1, 1, 1]$. The number of cakes with height $2$ is $3$. The maximum number of cakes with height $2$ is $3$.
sz = 100001 t = int(input()) for test in range(t): n, k = map(int, input().split()) end, start = [0] * n, [0] * n sfx, pfx, dp = [0] * sz, [0] * sz, [0] * sz ul = 0 for i in range(n): l, r = map(int, input().split()) ul = max(ul, r) dp[l - 1] += 1 dp[r] -= 1 start[i] = l - 1 end[i] = r - 1 a = [0] * (ul + 1) a[0] = dp[0] ans = 0 if a[0] == k: pfx[0] = 1 ans += 1 elif a[0] == k + 1: sfx[0] = 1 for i in range(1, ul): dp[i] += dp[i - 1] a[i] += dp[i] if a[i] == k: pfx[i] = pfx[i - 1] + 1 ans += 1 else: pfx[i] = pfx[i - 1] if a[i] == k + 1: sfx[i] = sfx[i - 1] + 1 else: sfx[i] = sfx[i - 1] mx = -100001 for i in range(n): l = start[i] r = end[i] tot = sfx[r] - sfx[l - 1] tot = tot - (pfx[r] - pfx[l - 1]) mx = max(mx, tot) print(ans + mx)
ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR BIN_OP LIST NUMBER VAR BIN_OP LIST NUMBER VAR ASSIGN VAR VAR VAR BIN_OP LIST NUMBER VAR BIN_OP LIST NUMBER VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER NUMBER VAR VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER VAR ASSIGN VAR NUMBER NUMBER VAR NUMBER IF VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR VAR IF VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER IF VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR
Read problems statements in [Hindi], [Mandarin Chinese], [Russian], [Vietnamese] and [Bengali] as well. You have $C = 100,000$ cakes, numbered $1$ through $C$. Each cake has an integer height; initially, the height of each cake is $0$. There are $N$ operations. In each operation, you are given two integers $L$ and $R$, and you should increase by $1$ the height of each of the cakes $L, L+1, \ldots, R$. One of these $N$ operations should be removed and the remaining $N-1$ operations are then performed. Chef wants to remove one operation in such a way that after the remaining $N-1$ operations are performed, the number of cakes with height exactly $K$ is maximum possible. Since Chef is a bit busy these days, he has asked for your help. You need to find the maximum number of cakes with height exactly $K$ that can be achieved by removing one operation. ------ Input ------ The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows. The first line of each test case contains two space-separated integers $N$ and $K$. Each of the next $N$ lines contains two space-separated integers $L$ and $R$ describing one operation. ------ Output ------ For each test case, print a single line containing one integer — the maximum possible number of cakes with height $K$. ------ Constraints ------ $1 ≤ T ≤ 100$ $2 ≤ N ≤ 10^{5}$ $1 ≤ K ≤ N$ $1 ≤ L ≤ R ≤ 10^{5}$ the sum of $N$ over all test cases does not exceed $10^{6}$ ----- Sample Input 1 ------ 1 3 2 2 6 4 9 1 4 ----- Sample Output 1 ------ 3 ----- explanation 1 ------ Example case 1: Let's look at what happens after an operation is removed. - Removing operation $1$: The heights of cakes $4$ through $9$ increase by $1$. Then, the heights of cakes $1$ through $4$ increase by $1$. The resulting sequence of heights is $[1, 1, 1, 2, 1, 1, 1, 1, 1]$ (for cakes $1$ through $9$; the other cakes have heights $0$). The number of cakes with height $2$ is $1$. - Removing operation $2$: The resulting sequence of heights of cakes $1$ through $9$ is $[1, 2, 2, 2, 1, 1, 0, 0, 0]$. The number of cakes with height $2$ is $3$. - Removing operation $3$: The resulting sequence of heights of cakes $1$ through $9$ is $[0, 1, 1, 2, 2, 2, 1, 1, 1]$. The number of cakes with height $2$ is $3$. The maximum number of cakes with height $2$ is $3$.
for _ in range(int(input())): n, k = map(int, input().split()) l = [] x = [0] * 100001 maxi_r = 0 for _ in range(n): L, R = map(int, input().split()) if R > maxi_r: maxi_r = R l.append((L, R)) x[L] += 1 x[R + 1] -= 1 maxi_r += 2 x = x[:maxi_r] yk = [0] * maxi_r zkk = [0] * maxi_r for i in range(1, maxi_r): x[i] = x[i - 1] + x[i] for i in range(1, maxi_r): if x[i] == k: yk[i] = yk[i - 1] + 1 else: yk[i] = yk[i - 1] if x[i] == k + 1: zkk[i] = zkk[i - 1] + 1 else: zkk[i] = zkk[i - 1] m = 0 for i, j in l: r1 = yk[j] - yk[i - 1] r2 = zkk[j] - zkk[i - 1] r = r2 - r1 n = yk[-1] + r if n > m: m = n print(m)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR BIN_OP LIST NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER IF VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR NUMBER VAR IF VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR
Read problems statements in [Hindi], [Mandarin Chinese], [Russian], [Vietnamese] and [Bengali] as well. You have $C = 100,000$ cakes, numbered $1$ through $C$. Each cake has an integer height; initially, the height of each cake is $0$. There are $N$ operations. In each operation, you are given two integers $L$ and $R$, and you should increase by $1$ the height of each of the cakes $L, L+1, \ldots, R$. One of these $N$ operations should be removed and the remaining $N-1$ operations are then performed. Chef wants to remove one operation in such a way that after the remaining $N-1$ operations are performed, the number of cakes with height exactly $K$ is maximum possible. Since Chef is a bit busy these days, he has asked for your help. You need to find the maximum number of cakes with height exactly $K$ that can be achieved by removing one operation. ------ Input ------ The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows. The first line of each test case contains two space-separated integers $N$ and $K$. Each of the next $N$ lines contains two space-separated integers $L$ and $R$ describing one operation. ------ Output ------ For each test case, print a single line containing one integer — the maximum possible number of cakes with height $K$. ------ Constraints ------ $1 ≤ T ≤ 100$ $2 ≤ N ≤ 10^{5}$ $1 ≤ K ≤ N$ $1 ≤ L ≤ R ≤ 10^{5}$ the sum of $N$ over all test cases does not exceed $10^{6}$ ----- Sample Input 1 ------ 1 3 2 2 6 4 9 1 4 ----- Sample Output 1 ------ 3 ----- explanation 1 ------ Example case 1: Let's look at what happens after an operation is removed. - Removing operation $1$: The heights of cakes $4$ through $9$ increase by $1$. Then, the heights of cakes $1$ through $4$ increase by $1$. The resulting sequence of heights is $[1, 1, 1, 2, 1, 1, 1, 1, 1]$ (for cakes $1$ through $9$; the other cakes have heights $0$). The number of cakes with height $2$ is $1$. - Removing operation $2$: The resulting sequence of heights of cakes $1$ through $9$ is $[1, 2, 2, 2, 1, 1, 0, 0, 0]$. The number of cakes with height $2$ is $3$. - Removing operation $3$: The resulting sequence of heights of cakes $1$ through $9$ is $[0, 1, 1, 2, 2, 2, 1, 1, 1]$. The number of cakes with height $2$ is $3$. The maximum number of cakes with height $2$ is $3$.
t = int(input()) for _ in range(t): n, k = map(int, input().split()) m = 0 options = [] for i in range(n): l, r = map(int, input().split()) options.append([l, r]) m = max(m, l, r) m += 2 c = [0] * m for i in options: c[i[0]] += 1 c[i[1] + 1] -= 1 x = 0 w = [] for i in range(m): x += c[i] w.append(x) c1 = [0] * m c2 = [0] * m for i in range(1, m): if w[i] == k: c1[i] = 1 + c1[i - 1] else: c1[i] = c1[i - 1] d1 = [0] * m for i in range(1, m): if w[i] == k + 1: d1[i] = 1 + d1[i - 1] else: d1[i] = d1[i - 1] for i in range(m - 2, 0, -1): if w[i] == k: c2[i] = 1 + c2[i + 1] else: c2[i] = c2[i + 1] mymax = 0 for i in options: mymax = max(mymax, c1[i[0] - 1] + c2[i[1] + 1] + d1[i[1]] - d1[i[0] - 1]) print(mymax)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR LIST VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR VAR VAR VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR VAR ASSIGN VAR VAR BIN_OP NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR VAR VAR ASSIGN VAR VAR BIN_OP NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER NUMBER VAR VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR
Read problems statements in [Hindi], [Mandarin Chinese], [Russian], [Vietnamese] and [Bengali] as well. You have $C = 100,000$ cakes, numbered $1$ through $C$. Each cake has an integer height; initially, the height of each cake is $0$. There are $N$ operations. In each operation, you are given two integers $L$ and $R$, and you should increase by $1$ the height of each of the cakes $L, L+1, \ldots, R$. One of these $N$ operations should be removed and the remaining $N-1$ operations are then performed. Chef wants to remove one operation in such a way that after the remaining $N-1$ operations are performed, the number of cakes with height exactly $K$ is maximum possible. Since Chef is a bit busy these days, he has asked for your help. You need to find the maximum number of cakes with height exactly $K$ that can be achieved by removing one operation. ------ Input ------ The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows. The first line of each test case contains two space-separated integers $N$ and $K$. Each of the next $N$ lines contains two space-separated integers $L$ and $R$ describing one operation. ------ Output ------ For each test case, print a single line containing one integer — the maximum possible number of cakes with height $K$. ------ Constraints ------ $1 ≤ T ≤ 100$ $2 ≤ N ≤ 10^{5}$ $1 ≤ K ≤ N$ $1 ≤ L ≤ R ≤ 10^{5}$ the sum of $N$ over all test cases does not exceed $10^{6}$ ----- Sample Input 1 ------ 1 3 2 2 6 4 9 1 4 ----- Sample Output 1 ------ 3 ----- explanation 1 ------ Example case 1: Let's look at what happens after an operation is removed. - Removing operation $1$: The heights of cakes $4$ through $9$ increase by $1$. Then, the heights of cakes $1$ through $4$ increase by $1$. The resulting sequence of heights is $[1, 1, 1, 2, 1, 1, 1, 1, 1]$ (for cakes $1$ through $9$; the other cakes have heights $0$). The number of cakes with height $2$ is $1$. - Removing operation $2$: The resulting sequence of heights of cakes $1$ through $9$ is $[1, 2, 2, 2, 1, 1, 0, 0, 0]$. The number of cakes with height $2$ is $3$. - Removing operation $3$: The resulting sequence of heights of cakes $1$ through $9$ is $[0, 1, 1, 2, 2, 2, 1, 1, 1]$. The number of cakes with height $2$ is $3$. The maximum number of cakes with height $2$ is $3$.
cpc = [(0) for i in range(100001)] cqc = [(0) for i in range(100001)] for _ in range(int(input())): n, k = map(int, input().split()) arr = [(0) for i in range(100001)] check = [] lr = 0 for i in range(n): l, r = map(int, input().split()) check.append((l, r)) lr = max(r, lr) arr[l] += 1 r += 1 if r <= 100000: arr[r] -= 1 ans = 0 cpc[1] = 0 cqc[1] = 0 if arr[1] == k: cpc[1] += 1 ans += 1 elif arr[1] == k + 1: cqc[1] += 1 for i in range(2, lr + 1): arr[i] += arr[i - 1] cpc[i] = 0 cpc[i] += cpc[i - 1] cqc[i] = 0 cqc[i] += cqc[i - 1] if arr[i] == k: ans += 1 cpc[i] += 1 elif arr[i] == k + 1: cqc[i] += 1 m = 0 for i in range(n): l, r = check[i][0], check[i][1] m = max(m, ans - (cpc[r] - cpc[l - 1]) + (cqc[r] - cqc[l - 1])) print(m)
ASSIGN VAR NUMBER VAR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR NUMBER ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR NUMBER VAR NUMBER IF VAR NUMBER VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER NUMBER ASSIGN VAR NUMBER NUMBER IF VAR NUMBER VAR VAR NUMBER NUMBER VAR NUMBER IF VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER IF VAR VAR VAR VAR NUMBER VAR VAR NUMBER IF VAR VAR BIN_OP VAR NUMBER VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER BIN_OP VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR
Read problems statements in [Hindi], [Mandarin Chinese], [Russian], [Vietnamese] and [Bengali] as well. You have $C = 100,000$ cakes, numbered $1$ through $C$. Each cake has an integer height; initially, the height of each cake is $0$. There are $N$ operations. In each operation, you are given two integers $L$ and $R$, and you should increase by $1$ the height of each of the cakes $L, L+1, \ldots, R$. One of these $N$ operations should be removed and the remaining $N-1$ operations are then performed. Chef wants to remove one operation in such a way that after the remaining $N-1$ operations are performed, the number of cakes with height exactly $K$ is maximum possible. Since Chef is a bit busy these days, he has asked for your help. You need to find the maximum number of cakes with height exactly $K$ that can be achieved by removing one operation. ------ Input ------ The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows. The first line of each test case contains two space-separated integers $N$ and $K$. Each of the next $N$ lines contains two space-separated integers $L$ and $R$ describing one operation. ------ Output ------ For each test case, print a single line containing one integer — the maximum possible number of cakes with height $K$. ------ Constraints ------ $1 ≤ T ≤ 100$ $2 ≤ N ≤ 10^{5}$ $1 ≤ K ≤ N$ $1 ≤ L ≤ R ≤ 10^{5}$ the sum of $N$ over all test cases does not exceed $10^{6}$ ----- Sample Input 1 ------ 1 3 2 2 6 4 9 1 4 ----- Sample Output 1 ------ 3 ----- explanation 1 ------ Example case 1: Let's look at what happens after an operation is removed. - Removing operation $1$: The heights of cakes $4$ through $9$ increase by $1$. Then, the heights of cakes $1$ through $4$ increase by $1$. The resulting sequence of heights is $[1, 1, 1, 2, 1, 1, 1, 1, 1]$ (for cakes $1$ through $9$; the other cakes have heights $0$). The number of cakes with height $2$ is $1$. - Removing operation $2$: The resulting sequence of heights of cakes $1$ through $9$ is $[1, 2, 2, 2, 1, 1, 0, 0, 0]$. The number of cakes with height $2$ is $3$. - Removing operation $3$: The resulting sequence of heights of cakes $1$ through $9$ is $[0, 1, 1, 2, 2, 2, 1, 1, 1]$. The number of cakes with height $2$ is $3$. The maximum number of cakes with height $2$ is $3$.
for t in range(int(input())): n, k = map(int, input().split(" ")) left = [] right = [] mx = 0 for i in range(n): l, r = map(int, input().split(" ")) left.append(l) right.append(r) mx = max(mx, r) total = [0] * (mx + 2) s = [0] * (mx + 2) po = [0] * (mx + 2) q = [0] * (mx + 2) for i in range(n): total[left[i]] += 1 total[right[i] + 1] += -1 for i in range(1, mx + 2): total[i] = total[i] + total[i - 1] q[i] = (total[i] == k + 1) + q[i - 1] s[i] = (total[i] == k) + s[i - 1] ac = 0 for i in reversed(range(1, mx + 1)): po[i] = po[i + 1] + (total[i] == k) for i in range(n): x = left[i] y = right[i] cnt = q[y] - q[x] + (total[x] == k + 1) cnt += s[x - 1] + po[y + 1] ac = max(ac, cnt) print(ac)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR STRING EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR VAR NUMBER VAR BIN_OP VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR
Old Farmer Ranjan works on the farm of landlord Mittal Sahab. On the occasion of Diwali, Mittal Sahab decided to give some bonus to the hardworking farmer Ranjan. Mittal Sahab promised him to offer all the dragon fruits that Ranjan collects in one go from the right side of the farm returning back to the same side, such that he can take exactly 2 left turns. Ranjan farmer can start plowing from any cell $A[i][N]$ where 1 ≤$ i$ ≤ N. The farm is represented as a 2D matrix, each cell consisting of some units of dragon fruit. Ranjan farmer wants to collect as much amount of dragon fruits as possible, but he is illiterate so he gave this job to his son, the great coder Rishi. Please refer to the image below for more clarification on how to traverse the farm. ------ Input ------ The first line contains single integer $T$, denoting the number of test cases. The next line consists of 2 space separated integers $N$ and $M$, denoting the dimension of the farm. The next $N$ lines consist of $M$ space separated integers each denoting the amount of dragon fruit in the cell. ------ Output: ------ Output a single integer denoting the maximum amount of vegetable Ranjan farmer could collect. ------ Constraints ------ $1 ≤ T ≤ 5$ $2 ≤ N,M ≤ 10^{3}$ $2 ≤ A[i][j] ≤ 10^{9}$ for $1 ≤ i,j ≤ N,M respectively$ ------ Sample Input: ------ 2 3 3 1 2 3 3 3 1 4 1 6 3 3 3 7 4 1 9 6 1 7 7 ------ Sample Output: ------ 20 34
T = 0 try: T = int(input()) except: pass while T > 0: n, m = map(int, input().split()) contiguous_sum = [0] * m max_sum = 0 while n > 0: row_vector = list(reversed(list(map(int, input().split())))) row_sum = 0 for i, k in enumerate(row_vector): row_sum += k max_sum = max(max_sum, row_sum + contiguous_sum[i]) contiguous_sum[i] = max(row_sum, k + contiguous_sum[i]) n = n - 1 print(max_sum) T = T - 1
ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER WHILE VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER
Old Farmer Ranjan works on the farm of landlord Mittal Sahab. On the occasion of Diwali, Mittal Sahab decided to give some bonus to the hardworking farmer Ranjan. Mittal Sahab promised him to offer all the dragon fruits that Ranjan collects in one go from the right side of the farm returning back to the same side, such that he can take exactly 2 left turns. Ranjan farmer can start plowing from any cell $A[i][N]$ where 1 ≤$ i$ ≤ N. The farm is represented as a 2D matrix, each cell consisting of some units of dragon fruit. Ranjan farmer wants to collect as much amount of dragon fruits as possible, but he is illiterate so he gave this job to his son, the great coder Rishi. Please refer to the image below for more clarification on how to traverse the farm. ------ Input ------ The first line contains single integer $T$, denoting the number of test cases. The next line consists of 2 space separated integers $N$ and $M$, denoting the dimension of the farm. The next $N$ lines consist of $M$ space separated integers each denoting the amount of dragon fruit in the cell. ------ Output: ------ Output a single integer denoting the maximum amount of vegetable Ranjan farmer could collect. ------ Constraints ------ $1 ≤ T ≤ 5$ $2 ≤ N,M ≤ 10^{3}$ $2 ≤ A[i][j] ≤ 10^{9}$ for $1 ≤ i,j ≤ N,M respectively$ ------ Sample Input: ------ 2 3 3 1 2 3 3 3 1 4 1 6 3 3 3 7 4 1 9 6 1 7 7 ------ Sample Output: ------ 20 34
for _ in range(int(input())): r, c = list(map(int, input().split())) a, c1, msum = [], [0] * c, 0 for i in range(r): a = list(reversed(list(map(int, input().split())))) s = 0 for j, k in enumerate(a): s += k msum = max(msum, s + c1[j]) c1[j] = max(s, k + c1[j]) print(msum)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR VAR LIST BIN_OP LIST NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR VAR
Old Farmer Ranjan works on the farm of landlord Mittal Sahab. On the occasion of Diwali, Mittal Sahab decided to give some bonus to the hardworking farmer Ranjan. Mittal Sahab promised him to offer all the dragon fruits that Ranjan collects in one go from the right side of the farm returning back to the same side, such that he can take exactly 2 left turns. Ranjan farmer can start plowing from any cell $A[i][N]$ where 1 ≤$ i$ ≤ N. The farm is represented as a 2D matrix, each cell consisting of some units of dragon fruit. Ranjan farmer wants to collect as much amount of dragon fruits as possible, but he is illiterate so he gave this job to his son, the great coder Rishi. Please refer to the image below for more clarification on how to traverse the farm. ------ Input ------ The first line contains single integer $T$, denoting the number of test cases. The next line consists of 2 space separated integers $N$ and $M$, denoting the dimension of the farm. The next $N$ lines consist of $M$ space separated integers each denoting the amount of dragon fruit in the cell. ------ Output: ------ Output a single integer denoting the maximum amount of vegetable Ranjan farmer could collect. ------ Constraints ------ $1 ≤ T ≤ 5$ $2 ≤ N,M ≤ 10^{3}$ $2 ≤ A[i][j] ≤ 10^{9}$ for $1 ≤ i,j ≤ N,M respectively$ ------ Sample Input: ------ 2 3 3 1 2 3 3 3 1 4 1 6 3 3 3 7 4 1 9 6 1 7 7 ------ Sample Output: ------ 20 34
for i in range(int(input())): n, m = map(int, input().split()) f1l = [[]] * n f2l = [[]] * n f3l = [[]] * n mat = [None] * n for i in range(n): f1l[i] = [0] * m f2l[i] = [0] * m f3l[i] = [0] * m mat[i] = list(map(int, input().split())) def f3(a, b): if a < n and a >= 0 and b < m and b >= 0: if f3l[a][b] == 0: f3l[a][b] = mat[a][b] + f3(a, b + 1) return f3l[a][b] return 0 def f2(a, b): if a < n and a >= 0 and b < m and b >= 0: if f2l[a][b] == 0: f2l[a][b] = max(f2(a + 1, b) + mat[a][b], f3(a, b)) return f2l[a][b] return 0 def f1(a, b): if a < n and a >= 0 and b < m and b >= 0: if f1l[a][b] == 0: f1l[a][b] = max(f1(a, b - 1) + mat[a][b], f2(a + 1, b) + mat[a][b]) return f1l[a][b] return 0 for i in range(n): x = f3(i, 0) for i in range(m): x = f2(0, i) x, mx = 0, 0 for i in range(n): x = f1(i, m - 1) if x > mx: mx = x print(mx)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST LIST VAR ASSIGN VAR BIN_OP LIST LIST VAR ASSIGN VAR BIN_OP LIST LIST VAR ASSIGN VAR BIN_OP LIST NONE VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP LIST NUMBER VAR ASSIGN VAR VAR BIN_OP LIST NUMBER VAR ASSIGN VAR VAR BIN_OP LIST NUMBER VAR ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF IF VAR VAR VAR NUMBER VAR VAR VAR NUMBER IF VAR VAR VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER RETURN VAR VAR VAR RETURN NUMBER FUNC_DEF IF VAR VAR VAR NUMBER VAR VAR VAR NUMBER IF VAR VAR VAR NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR VAR VAR FUNC_CALL VAR VAR VAR RETURN VAR VAR VAR RETURN NUMBER FUNC_DEF IF VAR VAR VAR NUMBER VAR VAR VAR NUMBER IF VAR VAR VAR NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR VAR VAR RETURN VAR VAR VAR RETURN NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER IF VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR
Old Farmer Ranjan works on the farm of landlord Mittal Sahab. On the occasion of Diwali, Mittal Sahab decided to give some bonus to the hardworking farmer Ranjan. Mittal Sahab promised him to offer all the dragon fruits that Ranjan collects in one go from the right side of the farm returning back to the same side, such that he can take exactly 2 left turns. Ranjan farmer can start plowing from any cell $A[i][N]$ where 1 ≤$ i$ ≤ N. The farm is represented as a 2D matrix, each cell consisting of some units of dragon fruit. Ranjan farmer wants to collect as much amount of dragon fruits as possible, but he is illiterate so he gave this job to his son, the great coder Rishi. Please refer to the image below for more clarification on how to traverse the farm. ------ Input ------ The first line contains single integer $T$, denoting the number of test cases. The next line consists of 2 space separated integers $N$ and $M$, denoting the dimension of the farm. The next $N$ lines consist of $M$ space separated integers each denoting the amount of dragon fruit in the cell. ------ Output: ------ Output a single integer denoting the maximum amount of vegetable Ranjan farmer could collect. ------ Constraints ------ $1 ≤ T ≤ 5$ $2 ≤ N,M ≤ 10^{3}$ $2 ≤ A[i][j] ≤ 10^{9}$ for $1 ≤ i,j ≤ N,M respectively$ ------ Sample Input: ------ 2 3 3 1 2 3 3 3 1 4 1 6 3 3 3 7 4 1 9 6 1 7 7 ------ Sample Output: ------ 20 34
def T(x, y, d, n): try: f = lut[x, y, d] except KeyError: g = 0 else: return f if d == 2: if y == n - 1: return a[x][y] else: k = a[x][y] + T(x, y + 1, 2, n) lut[x, y, d] = k return k elif d == 1: if x == 0: k = a[x][y] + T(x, y + 1, 2, n) lut[x, y, d] = k return k else: k = a[x][y] + max(T(x, y + 1, 2, n), T(x - 1, y, 1, n)) lut[x, y, d] = k return k elif y == n - 1: k = a[x][y] + T(x, y - 1, 0, n) lut[x, y, d] = k return k elif y == 0: k = a[x][y] + T(x - 1, y, 1, n) lut[x, y, d] = k return k else: k = a[x][y] + max(T(x - 1, y, 1, n), T(x, y - 1, 0, n)) lut[x, y, d] = k return k for _ in range(int(input())): lut = {} N, M = [int(x) for x in input().split()] a = [] for i in range(N): a += [[int(x) for x in input().split()]] ans = 0 for i in range(1, N): ans = max(ans, T(i, M - 1, 0, M)) print(ans)
FUNC_DEF ASSIGN VAR VAR VAR VAR VAR VAR ASSIGN VAR NUMBER RETURN VAR IF VAR NUMBER IF VAR BIN_OP VAR NUMBER RETURN VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER VAR ASSIGN VAR VAR VAR VAR VAR RETURN VAR IF VAR NUMBER IF VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER VAR ASSIGN VAR VAR VAR VAR VAR RETURN VAR ASSIGN VAR BIN_OP VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER VAR ASSIGN VAR VAR VAR VAR VAR RETURN VAR IF VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER VAR ASSIGN VAR VAR VAR VAR VAR RETURN VAR IF VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER VAR ASSIGN VAR VAR VAR VAR VAR RETURN VAR ASSIGN VAR BIN_OP VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER VAR ASSIGN VAR VAR VAR VAR VAR RETURN VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR DICT ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR VAR LIST FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER VAR EXPR FUNC_CALL VAR VAR
Old Farmer Ranjan works on the farm of landlord Mittal Sahab. On the occasion of Diwali, Mittal Sahab decided to give some bonus to the hardworking farmer Ranjan. Mittal Sahab promised him to offer all the dragon fruits that Ranjan collects in one go from the right side of the farm returning back to the same side, such that he can take exactly 2 left turns. Ranjan farmer can start plowing from any cell $A[i][N]$ where 1 ≤$ i$ ≤ N. The farm is represented as a 2D matrix, each cell consisting of some units of dragon fruit. Ranjan farmer wants to collect as much amount of dragon fruits as possible, but he is illiterate so he gave this job to his son, the great coder Rishi. Please refer to the image below for more clarification on how to traverse the farm. ------ Input ------ The first line contains single integer $T$, denoting the number of test cases. The next line consists of 2 space separated integers $N$ and $M$, denoting the dimension of the farm. The next $N$ lines consist of $M$ space separated integers each denoting the amount of dragon fruit in the cell. ------ Output: ------ Output a single integer denoting the maximum amount of vegetable Ranjan farmer could collect. ------ Constraints ------ $1 ≤ T ≤ 5$ $2 ≤ N,M ≤ 10^{3}$ $2 ≤ A[i][j] ≤ 10^{9}$ for $1 ≤ i,j ≤ N,M respectively$ ------ Sample Input: ------ 2 3 3 1 2 3 3 3 1 4 1 6 3 3 3 7 4 1 9 6 1 7 7 ------ Sample Output: ------ 20 34
a = int(input()) for i in range(a): cc = list(map(int, input().split())) prr = [] for i in range(cc[0]): prr.append(list(map(int, input().split()))) lrr = [[[0, 0] for i in range(cc[1])] for j in range(cc[0])] for i in range(cc[0]): trr = 0 for j in range(cc[1] - 1, -1, -1): trr += prr[i][j] if i == 0: lrr[i][j][0] = trr lrr[i][j][1] = 0 elif trr < lrr[i - 1][j][0] + prr[i][j]: lrr[i][j][0] = lrr[i - 1][j][0] + prr[i][j] lrr[i][j][1] = 0 else: lrr[i][j][0] = trr lrr[i][j][1] = 1 maxs = 0 for i in range(1, cc[0]): trr = 0 for j in range(cc[1] - 1, -1, -1): if lrr[i][j][1] == 0: if maxs < trr + lrr[i][j][0]: maxs = lrr[i][j][0] + trr elif maxs < trr + lrr[i - 1][j][0]: maxs = trr + lrr[i - 1][j][0] + prr[i][j] trr += prr[i][j] print(maxs)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST NUMBER NUMBER VAR FUNC_CALL VAR VAR NUMBER VAR FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER NUMBER VAR VAR VAR VAR IF VAR NUMBER ASSIGN VAR VAR VAR NUMBER VAR ASSIGN VAR VAR VAR NUMBER NUMBER IF VAR BIN_OP VAR BIN_OP VAR NUMBER VAR NUMBER VAR VAR VAR ASSIGN VAR VAR VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER VAR NUMBER VAR VAR VAR ASSIGN VAR VAR VAR NUMBER NUMBER ASSIGN VAR VAR VAR NUMBER VAR ASSIGN VAR VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER NUMBER IF VAR VAR VAR NUMBER NUMBER IF VAR BIN_OP VAR VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR NUMBER VAR IF VAR BIN_OP VAR VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR NUMBER VAR NUMBER VAR VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR
Given an array arr[] of N nodes representing preorder traversal of some BST. You have to build the exact PostOrder from it's given preorder traversal. In Pre-Order traversal, the root node is visited before the left child and right child nodes. Example 1: Input: N = 5 arr[] = {40,30,35,80,100} Output: 35 30 100 80 40 Explanation: PreOrder: 40 30 35 80 100 Therefore, the BST will be: 40 / \ 30 80 \ \ 35 100 Hence, the postOrder traversal will be: 35 30 100 80 40 Example 2: Input: N = 8 arr[] = {40,30,32,35,80,90,100,120} Output: 35 32 30 120 100 90 80 40 Your Task: You need to complete the given function and return the root of the tree. The driver code will then use this root to print the post order traversal. Expected Time Complexity: O(N). Expected Auxiliary Space: O(N). Constraints: 1 <= N <= 10^{3} 1 <= arr[i] <= 10^{4}
class Node: def __init__(self, data=0): self.data = data self.left = None self.right = None def post_order(pre, size) -> Node: def insert(root, val): if not root: return Node(val) if val < root.data: root.left = insert(root.left, val) else: root.right = insert(root.right, val) return root root = None for i in pre: root = insert(root, i) return root
CLASS_DEF FUNC_DEF NUMBER ASSIGN VAR VAR ASSIGN VAR NONE ASSIGN VAR NONE FUNC_DEF FUNC_DEF IF VAR RETURN FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN VAR ASSIGN VAR NONE FOR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN VAR VAR
Given an array arr[] of N nodes representing preorder traversal of some BST. You have to build the exact PostOrder from it's given preorder traversal. In Pre-Order traversal, the root node is visited before the left child and right child nodes. Example 1: Input: N = 5 arr[] = {40,30,35,80,100} Output: 35 30 100 80 40 Explanation: PreOrder: 40 30 35 80 100 Therefore, the BST will be: 40 / \ 30 80 \ \ 35 100 Hence, the postOrder traversal will be: 35 30 100 80 40 Example 2: Input: N = 8 arr[] = {40,30,32,35,80,90,100,120} Output: 35 32 30 120 100 90 80 40 Your Task: You need to complete the given function and return the root of the tree. The driver code will then use this root to print the post order traversal. Expected Time Complexity: O(N). Expected Auxiliary Space: O(N). Constraints: 1 <= N <= 10^{3} 1 <= arr[i] <= 10^{4}
class Node: def __init__(self, data=0): self.data = data self.left = None self.right = None def post_order(pre, size) -> Node: preind = [0] def findpostorder(minv, maxv): if preind[0] >= len(pre): return None if pre[preind[0]] < minv or pre[preind[0]] > maxv: return None val = pre[preind[0]] root = Node(val) preind[0] += 1 root.left = findpostorder(minv, val) root.right = findpostorder(val, maxv) return root return findpostorder(-999999999, 999999999)
CLASS_DEF FUNC_DEF NUMBER ASSIGN VAR VAR ASSIGN VAR NONE ASSIGN VAR NONE FUNC_DEF ASSIGN VAR LIST NUMBER FUNC_DEF IF VAR NUMBER FUNC_CALL VAR VAR RETURN NONE IF VAR VAR NUMBER VAR VAR VAR NUMBER VAR RETURN NONE ASSIGN VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN VAR RETURN FUNC_CALL VAR NUMBER NUMBER VAR
Given an array arr[] of N nodes representing preorder traversal of some BST. You have to build the exact PostOrder from it's given preorder traversal. In Pre-Order traversal, the root node is visited before the left child and right child nodes. Example 1: Input: N = 5 arr[] = {40,30,35,80,100} Output: 35 30 100 80 40 Explanation: PreOrder: 40 30 35 80 100 Therefore, the BST will be: 40 / \ 30 80 \ \ 35 100 Hence, the postOrder traversal will be: 35 30 100 80 40 Example 2: Input: N = 8 arr[] = {40,30,32,35,80,90,100,120} Output: 35 32 30 120 100 90 80 40 Your Task: You need to complete the given function and return the root of the tree. The driver code will then use this root to print the post order traversal. Expected Time Complexity: O(N). Expected Auxiliary Space: O(N). Constraints: 1 <= N <= 10^{3} 1 <= arr[i] <= 10^{4}
import sys class Node: def __init__(self, data=0): self.data = data self.left = None self.right = None def post_order(arr, size) -> Node: import sys sol = [] def post(root): if root == None: return post(root.left) post(root.right) sol.append(root.data) def inorder(root): if root == None: return inorder(root.left) print(root.data, end=" ") inorder(root.right) def construct(arr, lb, rb): if arr == []: return None if lb < arr[0] < rb: nn = Node(arr.pop(0)) nn.left = construct(arr, lb, nn.data) nn.right = construct(arr, nn.data, rb) else: return None return nn postOrd(construct(arr, -sys.maxsize, sys.maxsize))
IMPORT CLASS_DEF FUNC_DEF NUMBER ASSIGN VAR VAR ASSIGN VAR NONE ASSIGN VAR NONE FUNC_DEF IMPORT ASSIGN VAR LIST FUNC_DEF IF VAR NONE RETURN EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR FUNC_DEF IF VAR NONE RETURN EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR STRING EXPR FUNC_CALL VAR VAR FUNC_DEF IF VAR LIST RETURN NONE IF VAR VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR RETURN NONE RETURN VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR
Given an array arr[] of N nodes representing preorder traversal of some BST. You have to build the exact PostOrder from it's given preorder traversal. In Pre-Order traversal, the root node is visited before the left child and right child nodes. Example 1: Input: N = 5 arr[] = {40,30,35,80,100} Output: 35 30 100 80 40 Explanation: PreOrder: 40 30 35 80 100 Therefore, the BST will be: 40 / \ 30 80 \ \ 35 100 Hence, the postOrder traversal will be: 35 30 100 80 40 Example 2: Input: N = 8 arr[] = {40,30,32,35,80,90,100,120} Output: 35 32 30 120 100 90 80 40 Your Task: You need to complete the given function and return the root of the tree. The driver code will then use this root to print the post order traversal. Expected Time Complexity: O(N). Expected Auxiliary Space: O(N). Constraints: 1 <= N <= 10^{3} 1 <= arr[i] <= 10^{4}
class Node: def __init__(self, data=0): self.data = data self.left = None self.right = None def post_order(pre, size) -> Node: i = [0] def make_bst(pre, ls, rl): root = Node(pre[i[0]]) i[0] = i[0] + 1 if i[0] < size and pre[i[0]] < root.data and pre[i[0]] > ls: root.left = make_bst(pre, ls, root.data) else: root.left = None if i[0] < size and pre[i[0]] > root.data and pre[i[0]] < rl: root.right = make_bst(pre, root.data, rl) else: root.right = None return root r = make_bst(pre, -1, 1000000) return r
CLASS_DEF FUNC_DEF NUMBER ASSIGN VAR VAR ASSIGN VAR NONE ASSIGN VAR NONE FUNC_DEF ASSIGN VAR LIST NUMBER FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR NUMBER BIN_OP VAR NUMBER NUMBER IF VAR NUMBER VAR VAR VAR NUMBER VAR VAR VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR NONE IF VAR NUMBER VAR VAR VAR NUMBER VAR VAR VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR NONE RETURN VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER NUMBER RETURN VAR VAR
Given an array arr[] of N nodes representing preorder traversal of some BST. You have to build the exact PostOrder from it's given preorder traversal. In Pre-Order traversal, the root node is visited before the left child and right child nodes. Example 1: Input: N = 5 arr[] = {40,30,35,80,100} Output: 35 30 100 80 40 Explanation: PreOrder: 40 30 35 80 100 Therefore, the BST will be: 40 / \ 30 80 \ \ 35 100 Hence, the postOrder traversal will be: 35 30 100 80 40 Example 2: Input: N = 8 arr[] = {40,30,32,35,80,90,100,120} Output: 35 32 30 120 100 90 80 40 Your Task: You need to complete the given function and return the root of the tree. The driver code will then use this root to print the post order traversal. Expected Time Complexity: O(N). Expected Auxiliary Space: O(N). Constraints: 1 <= N <= 10^{3} 1 <= arr[i] <= 10^{4}
class Node: def __init__(self, data=0): self.data = data self.left = None self.right = None def post_order(pre, size) -> Node: root = Node(pre[0]) for i in range(1, size): root1 = post_to_tree(pre[i], root) return root1 def post_to_tree(val, root): if not root: return Node(val) if val < root.data: root.left = post_to_tree(val, root.left) elif val >= root.data: root.right = post_to_tree(val, root.right) return root
CLASS_DEF FUNC_DEF NUMBER ASSIGN VAR VAR ASSIGN VAR NONE ASSIGN VAR NONE FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR RETURN VAR VAR FUNC_DEF IF VAR RETURN FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN VAR
Given an array arr[] of N nodes representing preorder traversal of some BST. You have to build the exact PostOrder from it's given preorder traversal. In Pre-Order traversal, the root node is visited before the left child and right child nodes. Example 1: Input: N = 5 arr[] = {40,30,35,80,100} Output: 35 30 100 80 40 Explanation: PreOrder: 40 30 35 80 100 Therefore, the BST will be: 40 / \ 30 80 \ \ 35 100 Hence, the postOrder traversal will be: 35 30 100 80 40 Example 2: Input: N = 8 arr[] = {40,30,32,35,80,90,100,120} Output: 35 32 30 120 100 90 80 40 Your Task: You need to complete the given function and return the root of the tree. The driver code will then use this root to print the post order traversal. Expected Time Complexity: O(N). Expected Auxiliary Space: O(N). Constraints: 1 <= N <= 10^{3} 1 <= arr[i] <= 10^{4}
class Node: def __init__(self, data=0): self.data = data self.left = None self.right = None def post_order(pre, size) -> Node: global preindex preindex = 0 inorder = [] for i in pre: inorder.append(i) inorder.sort() root = cons_BST(pre, inorder, 0, size - 1) return root def cons_BST(pre, inorder, ist, ied): global preindex if ist > ied or preindex >= len(pre): return None root = Node(pre[preindex]) index = inorder.index(pre[preindex]) preindex += 1 root.left = cons_BST(pre, inorder, ist, index - 1) root.right = cons_BST(pre, inorder, index + 1, ied) return root
CLASS_DEF FUNC_DEF NUMBER ASSIGN VAR VAR ASSIGN VAR NONE ASSIGN VAR NONE FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR LIST FOR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER BIN_OP VAR NUMBER RETURN VAR VAR FUNC_DEF IF VAR VAR VAR FUNC_CALL VAR VAR RETURN NONE ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR RETURN VAR
Given an array arr[] of N nodes representing preorder traversal of some BST. You have to build the exact PostOrder from it's given preorder traversal. In Pre-Order traversal, the root node is visited before the left child and right child nodes. Example 1: Input: N = 5 arr[] = {40,30,35,80,100} Output: 35 30 100 80 40 Explanation: PreOrder: 40 30 35 80 100 Therefore, the BST will be: 40 / \ 30 80 \ \ 35 100 Hence, the postOrder traversal will be: 35 30 100 80 40 Example 2: Input: N = 8 arr[] = {40,30,32,35,80,90,100,120} Output: 35 32 30 120 100 90 80 40 Your Task: You need to complete the given function and return the root of the tree. The driver code will then use this root to print the post order traversal. Expected Time Complexity: O(N). Expected Auxiliary Space: O(N). Constraints: 1 <= N <= 10^{3} 1 <= arr[i] <= 10^{4}
class Node: def __init__(self, data=0): self.data = data self.left = None self.right = None def ins(root, ele): if root.left == None and ele < root.data: root.left = Node(ele) return if root.right == None and ele > root.data: root.right = Node(ele) return if ele < root.data: ins(root.left, ele) elif ele > root.data: ins(root.right, ele) def post_order(pre, size) -> Node: root = Node(pre[0]) for i in range(1, size): ins(root, pre[i]) return root
CLASS_DEF FUNC_DEF NUMBER ASSIGN VAR VAR ASSIGN VAR NONE ASSIGN VAR NONE FUNC_DEF IF VAR NONE VAR VAR ASSIGN VAR FUNC_CALL VAR VAR RETURN IF VAR NONE VAR VAR ASSIGN VAR FUNC_CALL VAR VAR RETURN IF VAR VAR EXPR FUNC_CALL VAR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR EXPR FUNC_CALL VAR VAR VAR VAR RETURN VAR VAR
Given an array arr[] of N nodes representing preorder traversal of some BST. You have to build the exact PostOrder from it's given preorder traversal. In Pre-Order traversal, the root node is visited before the left child and right child nodes. Example 1: Input: N = 5 arr[] = {40,30,35,80,100} Output: 35 30 100 80 40 Explanation: PreOrder: 40 30 35 80 100 Therefore, the BST will be: 40 / \ 30 80 \ \ 35 100 Hence, the postOrder traversal will be: 35 30 100 80 40 Example 2: Input: N = 8 arr[] = {40,30,32,35,80,90,100,120} Output: 35 32 30 120 100 90 80 40 Your Task: You need to complete the given function and return the root of the tree. The driver code will then use this root to print the post order traversal. Expected Time Complexity: O(N). Expected Auxiliary Space: O(N). Constraints: 1 <= N <= 10^{3} 1 <= arr[i] <= 10^{4}
class Node: def __init__(self, data=0): self.data = data self.left = None self.right = None def f(pre, si, ei): if si > ei: return None if si == ei: return Node(pre[si]) root = Node(pre[si]) i = si while i <= ei: if root.data < pre[i]: break i += 1 if i == ei + 1: root.left = f(pre, si + 1, i - 1) root.right = None else: root.left = f(pre, si + 1, i - 1) root.right = f(pre, i, ei) return root def post_order(pre, size) -> Node: return f(pre, 0, len(pre) - 1)
CLASS_DEF FUNC_DEF NUMBER ASSIGN VAR VAR ASSIGN VAR NONE ASSIGN VAR NONE FUNC_DEF IF VAR VAR RETURN NONE IF VAR VAR RETURN FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR WHILE VAR VAR IF VAR VAR VAR VAR NUMBER IF VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR NONE ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR RETURN VAR FUNC_DEF RETURN FUNC_CALL VAR VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER VAR
Given an array arr[] of N nodes representing preorder traversal of some BST. You have to build the exact PostOrder from it's given preorder traversal. In Pre-Order traversal, the root node is visited before the left child and right child nodes. Example 1: Input: N = 5 arr[] = {40,30,35,80,100} Output: 35 30 100 80 40 Explanation: PreOrder: 40 30 35 80 100 Therefore, the BST will be: 40 / \ 30 80 \ \ 35 100 Hence, the postOrder traversal will be: 35 30 100 80 40 Example 2: Input: N = 8 arr[] = {40,30,32,35,80,90,100,120} Output: 35 32 30 120 100 90 80 40 Your Task: You need to complete the given function and return the root of the tree. The driver code will then use this root to print the post order traversal. Expected Time Complexity: O(N). Expected Auxiliary Space: O(N). Constraints: 1 <= N <= 10^{3} 1 <= arr[i] <= 10^{4}
class Node: def __init__(self, data=0): self.data = data self.left = None self.right = None def post_order(pre, size) -> Node: root = Node(pre[0]) for i in range(1, size): bst(root, pre[i]) return root def postorder(root, res): if root: postorder(root.left, res) postorder(root.right, res) res.append(root.data) return res def bst(root, val): if not root: return Node(val) if root.data < val: root.right = bst(root.right, val) elif root.data > val: root.left = bst(root.left, val) return root
CLASS_DEF FUNC_DEF NUMBER ASSIGN VAR VAR ASSIGN VAR NONE ASSIGN VAR NONE FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR EXPR FUNC_CALL VAR VAR VAR VAR RETURN VAR VAR FUNC_DEF IF VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR RETURN VAR FUNC_DEF IF VAR RETURN FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN VAR
Given an array arr[] of N nodes representing preorder traversal of some BST. You have to build the exact PostOrder from it's given preorder traversal. In Pre-Order traversal, the root node is visited before the left child and right child nodes. Example 1: Input: N = 5 arr[] = {40,30,35,80,100} Output: 35 30 100 80 40 Explanation: PreOrder: 40 30 35 80 100 Therefore, the BST will be: 40 / \ 30 80 \ \ 35 100 Hence, the postOrder traversal will be: 35 30 100 80 40 Example 2: Input: N = 8 arr[] = {40,30,32,35,80,90,100,120} Output: 35 32 30 120 100 90 80 40 Your Task: You need to complete the given function and return the root of the tree. The driver code will then use this root to print the post order traversal. Expected Time Complexity: O(N). Expected Auxiliary Space: O(N). Constraints: 1 <= N <= 10^{3} 1 <= arr[i] <= 10^{4}
class Node: def __init__(self, data=0): self.data = data self.left = None self.right = None def binary_search(arr, ele): low, high = 0, len(arr) while low < high: mid = (low + high) // 2 if arr[mid] < ele: low = mid + 1 else: high = mid return low def construct_tree(preorder, inorder): if len(preorder) == 0: return rootval = preorder[0] root = Node(rootval) rootpos = binary_search(inorder, rootval) root.left = construct_tree(preorder[1 : 1 + rootpos], inorder[:rootpos]) root.right = construct_tree(preorder[1 + rootpos :], inorder[rootpos + 1 :]) return root def post_order(preorder, size) -> Node: inorder = sorted(preorder) root = construct_tree(preorder, inorder) return root
CLASS_DEF FUNC_DEF NUMBER ASSIGN VAR VAR ASSIGN VAR NONE ASSIGN VAR NONE FUNC_DEF ASSIGN VAR VAR NUMBER FUNC_CALL VAR VAR WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR RETURN VAR FUNC_DEF IF FUNC_CALL VAR VAR NUMBER RETURN ASSIGN VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER BIN_OP NUMBER VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP NUMBER VAR VAR BIN_OP VAR NUMBER RETURN VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN VAR VAR
Given an array arr[] of N nodes representing preorder traversal of some BST. You have to build the exact PostOrder from it's given preorder traversal. In Pre-Order traversal, the root node is visited before the left child and right child nodes. Example 1: Input: N = 5 arr[] = {40,30,35,80,100} Output: 35 30 100 80 40 Explanation: PreOrder: 40 30 35 80 100 Therefore, the BST will be: 40 / \ 30 80 \ \ 35 100 Hence, the postOrder traversal will be: 35 30 100 80 40 Example 2: Input: N = 8 arr[] = {40,30,32,35,80,90,100,120} Output: 35 32 30 120 100 90 80 40 Your Task: You need to complete the given function and return the root of the tree. The driver code will then use this root to print the post order traversal. Expected Time Complexity: O(N). Expected Auxiliary Space: O(N). Constraints: 1 <= N <= 10^{3} 1 <= arr[i] <= 10^{4}
import sys class Node: def __init__(self, data=0): self.data = data self.left = None self.right = None def f(pre, size, i, mini, maxi): if i[0] >= size: return None root = None if mini <= pre[i[0]] <= maxi: root = Node(pre[i[0]]) i[0] += 1 if i[0] < size: root.left = f(pre, size, i, mini, root.data) if i[0] < size: root.right = f(pre, size, i, root.data, maxi) return root def post_order(pre, size) -> Node: mini = -sys.maxsize maxi = sys.maxsize i = [0] return f(pre, size, i, mini, maxi)
IMPORT CLASS_DEF FUNC_DEF NUMBER ASSIGN VAR VAR ASSIGN VAR NONE ASSIGN VAR NONE FUNC_DEF IF VAR NUMBER VAR RETURN NONE ASSIGN VAR NONE IF VAR VAR VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER VAR NUMBER NUMBER IF VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR VAR IF VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR LIST NUMBER RETURN FUNC_CALL VAR VAR VAR VAR VAR VAR VAR
Given an array arr[] of N nodes representing preorder traversal of some BST. You have to build the exact PostOrder from it's given preorder traversal. In Pre-Order traversal, the root node is visited before the left child and right child nodes. Example 1: Input: N = 5 arr[] = {40,30,35,80,100} Output: 35 30 100 80 40 Explanation: PreOrder: 40 30 35 80 100 Therefore, the BST will be: 40 / \ 30 80 \ \ 35 100 Hence, the postOrder traversal will be: 35 30 100 80 40 Example 2: Input: N = 8 arr[] = {40,30,32,35,80,90,100,120} Output: 35 32 30 120 100 90 80 40 Your Task: You need to complete the given function and return the root of the tree. The driver code will then use this root to print the post order traversal. Expected Time Complexity: O(N). Expected Auxiliary Space: O(N). Constraints: 1 <= N <= 10^{3} 1 <= arr[i] <= 10^{4}
class Node: def __init__(self, data=0): self.data = data self.left = None self.right = None def post_order(pre, size) -> Node: i = [0] def create(top, bottom): if i[0] >= len(pre): return None node = Node(pre[i[0]]) if node.data > top or node.data < bottom: return None i[0] += 1 node.left = create(node.data, bottom) node.right = create(top, node.data) return node return create(float("inf"), -float("inf"))
CLASS_DEF FUNC_DEF NUMBER ASSIGN VAR VAR ASSIGN VAR NONE ASSIGN VAR NONE FUNC_DEF ASSIGN VAR LIST NUMBER FUNC_DEF IF VAR NUMBER FUNC_CALL VAR VAR RETURN NONE ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER IF VAR VAR VAR VAR RETURN NONE VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN VAR RETURN FUNC_CALL VAR FUNC_CALL VAR STRING FUNC_CALL VAR STRING VAR
Given an array arr[] of N nodes representing preorder traversal of some BST. You have to build the exact PostOrder from it's given preorder traversal. In Pre-Order traversal, the root node is visited before the left child and right child nodes. Example 1: Input: N = 5 arr[] = {40,30,35,80,100} Output: 35 30 100 80 40 Explanation: PreOrder: 40 30 35 80 100 Therefore, the BST will be: 40 / \ 30 80 \ \ 35 100 Hence, the postOrder traversal will be: 35 30 100 80 40 Example 2: Input: N = 8 arr[] = {40,30,32,35,80,90,100,120} Output: 35 32 30 120 100 90 80 40 Your Task: You need to complete the given function and return the root of the tree. The driver code will then use this root to print the post order traversal. Expected Time Complexity: O(N). Expected Auxiliary Space: O(N). Constraints: 1 <= N <= 10^{3} 1 <= arr[i] <= 10^{4}
class Node: def __init__(self, data=0): self.data = data self.left = None self.right = None def util(pre): if len(pre) == 0: return None rootdata = pre[0] pre.pop(0) count = 0 for i in range(len(pre)): if rootdata > pre[i]: count += 1 else: break root = Node(rootdata) root.left = util(pre[:count]) root.right = util(pre[count:]) return root def post_order(pre, size) -> Node: return util(pre)
CLASS_DEF FUNC_DEF NUMBER ASSIGN VAR VAR ASSIGN VAR NONE ASSIGN VAR NONE FUNC_DEF IF FUNC_CALL VAR VAR NUMBER RETURN NONE ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN VAR FUNC_DEF RETURN FUNC_CALL VAR VAR VAR
Given an array arr[] of N nodes representing preorder traversal of some BST. You have to build the exact PostOrder from it's given preorder traversal. In Pre-Order traversal, the root node is visited before the left child and right child nodes. Example 1: Input: N = 5 arr[] = {40,30,35,80,100} Output: 35 30 100 80 40 Explanation: PreOrder: 40 30 35 80 100 Therefore, the BST will be: 40 / \ 30 80 \ \ 35 100 Hence, the postOrder traversal will be: 35 30 100 80 40 Example 2: Input: N = 8 arr[] = {40,30,32,35,80,90,100,120} Output: 35 32 30 120 100 90 80 40 Your Task: You need to complete the given function and return the root of the tree. The driver code will then use this root to print the post order traversal. Expected Time Complexity: O(N). Expected Auxiliary Space: O(N). Constraints: 1 <= N <= 10^{3} 1 <= arr[i] <= 10^{4}
class Node: def __init__(self, data=0): self.data = data self.left = None self.right = None INT_MIN = -(2**31) INT_MAX = 2**31 def findPostOrderUtil(pre, n, minval, maxval, preIndex): if preIndex[0] == n: return if pre[preIndex[0]] < minval or pre[preIndex[0]] > maxval: return val = pre[preIndex[0]] preIndex[0] += 1 findPostOrderUtil(pre, n, minval, val, preIndex) findPostOrderUtil(pre, n, val, maxval, preIndex) print(val, end=" ") def post_order(pre, size) -> Node: preIndex = [0] findPostOrderUtil(pre, size, INT_MIN, INT_MAX, preIndex)
CLASS_DEF FUNC_DEF NUMBER ASSIGN VAR VAR ASSIGN VAR NONE ASSIGN VAR NONE ASSIGN VAR BIN_OP NUMBER NUMBER ASSIGN VAR BIN_OP NUMBER NUMBER FUNC_DEF IF VAR NUMBER VAR RETURN IF VAR VAR NUMBER VAR VAR VAR NUMBER VAR RETURN ASSIGN VAR VAR VAR NUMBER VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR STRING FUNC_DEF ASSIGN VAR LIST NUMBER EXPR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR
Given an array arr[] of N nodes representing preorder traversal of some BST. You have to build the exact PostOrder from it's given preorder traversal. In Pre-Order traversal, the root node is visited before the left child and right child nodes. Example 1: Input: N = 5 arr[] = {40,30,35,80,100} Output: 35 30 100 80 40 Explanation: PreOrder: 40 30 35 80 100 Therefore, the BST will be: 40 / \ 30 80 \ \ 35 100 Hence, the postOrder traversal will be: 35 30 100 80 40 Example 2: Input: N = 8 arr[] = {40,30,32,35,80,90,100,120} Output: 35 32 30 120 100 90 80 40 Your Task: You need to complete the given function and return the root of the tree. The driver code will then use this root to print the post order traversal. Expected Time Complexity: O(N). Expected Auxiliary Space: O(N). Constraints: 1 <= N <= 10^{3} 1 <= arr[i] <= 10^{4}
class Node: def __init__(self, data=0): self.data = data self.left = None self.right = None def func(a): if not a: return root = Node(a[0]) i = 1 while i < len(a) and a[i] < root.data: i += 1 root.left = func(a[1:i]) root.right = func(a[i:]) return root def fun(root, ans): if root is None: return fun(root.left, ans) fun(root.right, ans) ans.append(root.data) def post_order(pre, size) -> Node: root = func(pre) return root
CLASS_DEF FUNC_DEF NUMBER ASSIGN VAR VAR ASSIGN VAR NONE ASSIGN VAR NONE FUNC_DEF IF VAR RETURN ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN VAR FUNC_DEF IF VAR NONE RETURN EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR RETURN VAR VAR
Given an array arr[] of N nodes representing preorder traversal of some BST. You have to build the exact PostOrder from it's given preorder traversal. In Pre-Order traversal, the root node is visited before the left child and right child nodes. Example 1: Input: N = 5 arr[] = {40,30,35,80,100} Output: 35 30 100 80 40 Explanation: PreOrder: 40 30 35 80 100 Therefore, the BST will be: 40 / \ 30 80 \ \ 35 100 Hence, the postOrder traversal will be: 35 30 100 80 40 Example 2: Input: N = 8 arr[] = {40,30,32,35,80,90,100,120} Output: 35 32 30 120 100 90 80 40 Your Task: You need to complete the given function and return the root of the tree. The driver code will then use this root to print the post order traversal. Expected Time Complexity: O(N). Expected Auxiliary Space: O(N). Constraints: 1 <= N <= 10^{3} 1 <= arr[i] <= 10^{4}
class Node: def __init__(self, data=0): self.data = data self.left = None self.right = None def add(self, data): if self.data == data: return if data < self.data: if self.left: self.left.add(data) else: self.left = Node(data) elif self.right: self.right.add(data) else: self.right = Node(data) def add(root, data): if root.data == data: return if data < root.data: if root.left: add(root.left, data) else: root.left = Node(data) elif root.right: add(root.right, data) else: root.right = Node(data) def post_order(pre, size) -> Node: root = Node(pre[0]) for i in range(1, size): add(root, pre[i]) return root
CLASS_DEF FUNC_DEF NUMBER ASSIGN VAR VAR ASSIGN VAR NONE ASSIGN VAR NONE FUNC_DEF IF VAR VAR RETURN IF VAR VAR IF VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_DEF IF VAR VAR RETURN IF VAR VAR IF VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR EXPR FUNC_CALL VAR VAR VAR VAR RETURN VAR VAR
Given an array arr[] of N nodes representing preorder traversal of some BST. You have to build the exact PostOrder from it's given preorder traversal. In Pre-Order traversal, the root node is visited before the left child and right child nodes. Example 1: Input: N = 5 arr[] = {40,30,35,80,100} Output: 35 30 100 80 40 Explanation: PreOrder: 40 30 35 80 100 Therefore, the BST will be: 40 / \ 30 80 \ \ 35 100 Hence, the postOrder traversal will be: 35 30 100 80 40 Example 2: Input: N = 8 arr[] = {40,30,32,35,80,90,100,120} Output: 35 32 30 120 100 90 80 40 Your Task: You need to complete the given function and return the root of the tree. The driver code will then use this root to print the post order traversal. Expected Time Complexity: O(N). Expected Auxiliary Space: O(N). Constraints: 1 <= N <= 10^{3} 1 <= arr[i] <= 10^{4}
class Node: def __init__(self, data=0): self.data = data self.left = None self.right = None def post_order(pre, size) -> Node: i = 0 def solve(arr, minn, maxx): global i if i >= len(arr): return None elif arr[i] < minn or arr[i] > maxx: return None root = Node(arr[i]) i += 1 root.left = solve(arr, minn, root.data) root.right = solve(arr, root.data, maxx) return root def post_order(pre, size): global i i = 0 return solve(pre, -9999999, 9999999) return post_order(pre, size)
CLASS_DEF FUNC_DEF NUMBER ASSIGN VAR VAR ASSIGN VAR NONE ASSIGN VAR NONE FUNC_DEF ASSIGN VAR NUMBER FUNC_DEF IF VAR FUNC_CALL VAR VAR RETURN NONE IF VAR VAR VAR VAR VAR VAR RETURN NONE ASSIGN VAR FUNC_CALL VAR VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR NUMBER RETURN FUNC_CALL VAR VAR NUMBER NUMBER RETURN FUNC_CALL VAR VAR VAR VAR
Given an array arr[] of N nodes representing preorder traversal of some BST. You have to build the exact PostOrder from it's given preorder traversal. In Pre-Order traversal, the root node is visited before the left child and right child nodes. Example 1: Input: N = 5 arr[] = {40,30,35,80,100} Output: 35 30 100 80 40 Explanation: PreOrder: 40 30 35 80 100 Therefore, the BST will be: 40 / \ 30 80 \ \ 35 100 Hence, the postOrder traversal will be: 35 30 100 80 40 Example 2: Input: N = 8 arr[] = {40,30,32,35,80,90,100,120} Output: 35 32 30 120 100 90 80 40 Your Task: You need to complete the given function and return the root of the tree. The driver code will then use this root to print the post order traversal. Expected Time Complexity: O(N). Expected Auxiliary Space: O(N). Constraints: 1 <= N <= 10^{3} 1 <= arr[i] <= 10^{4}
class Node: def __init__(self, data=0): self.data = data self.left = None self.right = None def post_order(pre, size) -> Node: def fgt(arr): for j in range(len(arr)): if arr[j] > arr[0]: return j return len(arr) def solve(arr): if not arr: return None root = Node(arr[0]) indx = fgt(arr) root.left = solve(arr[1:indx]) root.right = solve(arr[indx:]) return root return solve(pre)
CLASS_DEF FUNC_DEF NUMBER ASSIGN VAR VAR ASSIGN VAR NONE ASSIGN VAR NONE FUNC_DEF FUNC_DEF FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR NUMBER RETURN VAR RETURN FUNC_CALL VAR VAR FUNC_DEF IF VAR RETURN NONE ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN VAR RETURN FUNC_CALL VAR VAR VAR
Given an array arr[] of N nodes representing preorder traversal of some BST. You have to build the exact PostOrder from it's given preorder traversal. In Pre-Order traversal, the root node is visited before the left child and right child nodes. Example 1: Input: N = 5 arr[] = {40,30,35,80,100} Output: 35 30 100 80 40 Explanation: PreOrder: 40 30 35 80 100 Therefore, the BST will be: 40 / \ 30 80 \ \ 35 100 Hence, the postOrder traversal will be: 35 30 100 80 40 Example 2: Input: N = 8 arr[] = {40,30,32,35,80,90,100,120} Output: 35 32 30 120 100 90 80 40 Your Task: You need to complete the given function and return the root of the tree. The driver code will then use this root to print the post order traversal. Expected Time Complexity: O(N). Expected Auxiliary Space: O(N). Constraints: 1 <= N <= 10^{3} 1 <= arr[i] <= 10^{4}
class Node: def __init__(self, data=0): self.data = data self.left = None self.right = None def post_order(pre, size) -> Node: if len(pre) == 0: return root = Node(pre[0]) i = 1 while i < len(pre) and pre[i] < root.data: i += 1 root.left = post_order(pre[1:i], size) root.right = post_order(pre[i:], size) return root
CLASS_DEF FUNC_DEF NUMBER ASSIGN VAR VAR ASSIGN VAR NONE ASSIGN VAR NONE FUNC_DEF IF FUNC_CALL VAR VAR NUMBER RETURN ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR RETURN VAR VAR
Given an array arr[] of N nodes representing preorder traversal of some BST. You have to build the exact PostOrder from it's given preorder traversal. In Pre-Order traversal, the root node is visited before the left child and right child nodes. Example 1: Input: N = 5 arr[] = {40,30,35,80,100} Output: 35 30 100 80 40 Explanation: PreOrder: 40 30 35 80 100 Therefore, the BST will be: 40 / \ 30 80 \ \ 35 100 Hence, the postOrder traversal will be: 35 30 100 80 40 Example 2: Input: N = 8 arr[] = {40,30,32,35,80,90,100,120} Output: 35 32 30 120 100 90 80 40 Your Task: You need to complete the given function and return the root of the tree. The driver code will then use this root to print the post order traversal. Expected Time Complexity: O(N). Expected Auxiliary Space: O(N). Constraints: 1 <= N <= 10^{3} 1 <= arr[i] <= 10^{4}
class Node: def __init__(self, data=0): self.data = data self.left = None self.right = None INT_MAX = 2**63 - 1 INT_MIN = -(2**63) - 1 def PreorderToBST(pre, indx, n, minimum, maximum): if indx[0] >= n: return None if pre[indx[0]] > maximum or pre[indx[0]] < minimum: return None root = Node(pre[indx[0]]) indx[0] += 1 root.left = PreorderToBST(pre, indx, n, minimum, root.data) root.right = PreorderToBST(pre, indx, n, root.data, maximum) return root def post_order(pre, size) -> Node: indx = [0] return PreorderToBST(pre, indx, size, INT_MIN, INT_MAX)
CLASS_DEF FUNC_DEF NUMBER ASSIGN VAR VAR ASSIGN VAR NONE ASSIGN VAR NONE ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER FUNC_DEF IF VAR NUMBER VAR RETURN NONE IF VAR VAR NUMBER VAR VAR VAR NUMBER VAR RETURN NONE ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR LIST NUMBER RETURN FUNC_CALL VAR VAR VAR VAR VAR VAR VAR
Given an array arr[] of N nodes representing preorder traversal of some BST. You have to build the exact PostOrder from it's given preorder traversal. In Pre-Order traversal, the root node is visited before the left child and right child nodes. Example 1: Input: N = 5 arr[] = {40,30,35,80,100} Output: 35 30 100 80 40 Explanation: PreOrder: 40 30 35 80 100 Therefore, the BST will be: 40 / \ 30 80 \ \ 35 100 Hence, the postOrder traversal will be: 35 30 100 80 40 Example 2: Input: N = 8 arr[] = {40,30,32,35,80,90,100,120} Output: 35 32 30 120 100 90 80 40 Your Task: You need to complete the given function and return the root of the tree. The driver code will then use this root to print the post order traversal. Expected Time Complexity: O(N). Expected Auxiliary Space: O(N). Constraints: 1 <= N <= 10^{3} 1 <= arr[i] <= 10^{4}
class Node: def __init__(self, data=0): self.data = data self.left = None self.right = None def post_order(pre, size) -> Node: preorder = pre inorder = list(sorted(pre)) def fun(ino, pre): if not ino or not pre: return None root = Node(pre[0]) mid = ino.index(pre[0]) root.left = fun(ino[:mid], pre[1 : mid + 1]) root.right = fun(ino[mid + 1 :], pre[mid + 1 :]) return root return fun(inorder, preorder)
CLASS_DEF FUNC_DEF NUMBER ASSIGN VAR VAR ASSIGN VAR NONE ASSIGN VAR NONE FUNC_DEF ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_DEF IF VAR VAR RETURN NONE ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER RETURN VAR RETURN FUNC_CALL VAR VAR VAR VAR
Given an array arr[] of N nodes representing preorder traversal of some BST. You have to build the exact PostOrder from it's given preorder traversal. In Pre-Order traversal, the root node is visited before the left child and right child nodes. Example 1: Input: N = 5 arr[] = {40,30,35,80,100} Output: 35 30 100 80 40 Explanation: PreOrder: 40 30 35 80 100 Therefore, the BST will be: 40 / \ 30 80 \ \ 35 100 Hence, the postOrder traversal will be: 35 30 100 80 40 Example 2: Input: N = 8 arr[] = {40,30,32,35,80,90,100,120} Output: 35 32 30 120 100 90 80 40 Your Task: You need to complete the given function and return the root of the tree. The driver code will then use this root to print the post order traversal. Expected Time Complexity: O(N). Expected Auxiliary Space: O(N). Constraints: 1 <= N <= 10^{3} 1 <= arr[i] <= 10^{4}
class Node: def __init__(self, data=0): self.data = data self.left = None self.right = None def construct(pre, s, e): if s > e: return None if s == e: return Node(pre[s]) j = e + 1 for i in range(s + 1, e + 1): if pre[i] > pre[s]: j = i break node = Node(pre[s]) node.left = construct(pre, s + 1, j - 1) node.right = construct(pre, j, e) return node def post_order(pre, size) -> Node: return construct(pre, 0, size - 1)
CLASS_DEF FUNC_DEF NUMBER ASSIGN VAR VAR ASSIGN VAR NONE ASSIGN VAR NONE FUNC_DEF IF VAR VAR RETURN NONE IF VAR VAR RETURN FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR RETURN VAR FUNC_DEF RETURN FUNC_CALL VAR VAR NUMBER BIN_OP VAR NUMBER VAR
Given an array arr[] of N nodes representing preorder traversal of some BST. You have to build the exact PostOrder from it's given preorder traversal. In Pre-Order traversal, the root node is visited before the left child and right child nodes. Example 1: Input: N = 5 arr[] = {40,30,35,80,100} Output: 35 30 100 80 40 Explanation: PreOrder: 40 30 35 80 100 Therefore, the BST will be: 40 / \ 30 80 \ \ 35 100 Hence, the postOrder traversal will be: 35 30 100 80 40 Example 2: Input: N = 8 arr[] = {40,30,32,35,80,90,100,120} Output: 35 32 30 120 100 90 80 40 Your Task: You need to complete the given function and return the root of the tree. The driver code will then use this root to print the post order traversal. Expected Time Complexity: O(N). Expected Auxiliary Space: O(N). Constraints: 1 <= N <= 10^{3} 1 <= arr[i] <= 10^{4}
class Node: def __init__(self, data=0): self.data = data self.left = None self.right = None def util(pre, n): if n == 0: return None if n == 1: return Node(pre[0]) rootele = pre[0] pre = pre[1:] index = 0 for i in range(n - 1): if rootele < pre[i]: index = i break root = Node(rootele) root.left = util(pre[:index], len(pre[:index])) root.right = util(pre[index:], len(pre[index:])) return root def post_order(pre, size) -> Node: return util(pre, size)
CLASS_DEF FUNC_DEF NUMBER ASSIGN VAR VAR ASSIGN VAR NONE ASSIGN VAR NONE FUNC_DEF IF VAR NUMBER RETURN NONE IF VAR NUMBER RETURN FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR RETURN VAR FUNC_DEF RETURN FUNC_CALL VAR VAR VAR VAR
Given an array arr[] of N nodes representing preorder traversal of some BST. You have to build the exact PostOrder from it's given preorder traversal. In Pre-Order traversal, the root node is visited before the left child and right child nodes. Example 1: Input: N = 5 arr[] = {40,30,35,80,100} Output: 35 30 100 80 40 Explanation: PreOrder: 40 30 35 80 100 Therefore, the BST will be: 40 / \ 30 80 \ \ 35 100 Hence, the postOrder traversal will be: 35 30 100 80 40 Example 2: Input: N = 8 arr[] = {40,30,32,35,80,90,100,120} Output: 35 32 30 120 100 90 80 40 Your Task: You need to complete the given function and return the root of the tree. The driver code will then use this root to print the post order traversal. Expected Time Complexity: O(N). Expected Auxiliary Space: O(N). Constraints: 1 <= N <= 10^{3} 1 <= arr[i] <= 10^{4}
class Node: def __init__(self, data=0): self.data = data self.left = None self.right = None def post_order(pre_order, size) -> Node: if len(pre_order) <= 0: return None root = Node(pre_order[0]) index_for_right = check_first_greater_element(pre_order, root.data) if index_for_right == -1: root.right = None root.left = post_order(pre_order[1:], size) else: root.right = post_order(pre_order[index_for_right:], size) root.left = post_order(pre_order[1:index_for_right], size) return root def check_first_greater_element(input_arr: [], key: int): n = len(input_arr) for i in range(0, n): if key < input_arr[i]: return i return -1
CLASS_DEF FUNC_DEF NUMBER ASSIGN VAR VAR ASSIGN VAR NONE ASSIGN VAR NONE FUNC_DEF IF FUNC_CALL VAR VAR NUMBER RETURN NONE ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR NUMBER ASSIGN VAR NONE ASSIGN VAR FUNC_CALL VAR VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER VAR VAR RETURN VAR VAR FUNC_DEF LIST VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR VAR RETURN VAR RETURN NUMBER
Read problem statements in [Hindi], [Bengali], [Mandarin Chinese], [Russian], and [Vietnamese] as well. Chef wants to give a gift to Chefina to celebrate their anniversary. Of course, he has a sequence $a_{1}, a_{2}, \ldots, a_{N}$ ready for this occasion. Since the half-heart necklace is kind of cliche, he decided to cut his sequence into two pieces and give her a piece instead. Formally, he wants to choose an integer $l$ ($1 ≤ l < N$) and split the sequence into its prefix with length $l$ and its suffix with length $N-l$. Chef wants his gift to be *cute*; he thinks that it will be cute if the product of the elements in Chefina's piece is coprime with the product of the elements in his piece. Can you tell him where to cut the sequence? Find the smallest valid $l$ such that Chef's gift would be cute. ------ Input ------ The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows. The first line of each test case contains the integer $N$. The second line contains $N$ space-separated integers $a_{1}, a_{2}, \ldots, a_{N}$. ------ Output ------ For each test case, print a single line containing one integer $l$ where Chef should cut the sequence. It is guaranteed that a solution exists for the given test data. ------ Constraints ------ $1 ≤ T ≤ 20$ $2 ≤ N ≤ 10^{5}$ $2 ≤ a_{i} ≤ 10^{5}$ for each valid $i$ the sum of $N$ over all test cases does not exceed $3 \cdot 10^{5}$ ------ Subtasks ------ Subtask #1 (25 points): $N ≤ 200$ the sum of $N$ over all test cases does not exceed $600$ Subtask #2 (40 points): $N ≤ 2,000$ the sum of $N$ over all test cases does not exceed $6,000$ Subtask #3 (35 points): original constraints ----- Sample Input 1 ------ 1 4 2 3 4 5 ----- Sample Output 1 ------ 3
maxn = 100001 prime = [x for x in range(maxn)] i = 2 while i * i < maxn: if prime[i] == i: j = i while i * j < maxn: if prime[i * j] == i * j: prime[i * j] = i j += 1 i += 1 for _ in range(int(input())): n = int(input()) a = list(map(int, input().split())) d = {} l = 0 for i in range(n): x = a[i] while x > 1: p = prime[x] if p in d and d[p] <= l: l = i d[p] = i x //= p print(l + 1)
ASSIGN VAR NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER WHILE BIN_OP VAR VAR VAR IF VAR VAR VAR ASSIGN VAR VAR WHILE BIN_OP VAR VAR VAR IF VAR BIN_OP VAR VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR DICT ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR WHILE VAR NUMBER ASSIGN VAR VAR VAR IF VAR VAR VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER
Read problem statements in [Hindi], [Bengali], [Mandarin Chinese], [Russian], and [Vietnamese] as well. Chef wants to give a gift to Chefina to celebrate their anniversary. Of course, he has a sequence $a_{1}, a_{2}, \ldots, a_{N}$ ready for this occasion. Since the half-heart necklace is kind of cliche, he decided to cut his sequence into two pieces and give her a piece instead. Formally, he wants to choose an integer $l$ ($1 ≤ l < N$) and split the sequence into its prefix with length $l$ and its suffix with length $N-l$. Chef wants his gift to be *cute*; he thinks that it will be cute if the product of the elements in Chefina's piece is coprime with the product of the elements in his piece. Can you tell him where to cut the sequence? Find the smallest valid $l$ such that Chef's gift would be cute. ------ Input ------ The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows. The first line of each test case contains the integer $N$. The second line contains $N$ space-separated integers $a_{1}, a_{2}, \ldots, a_{N}$. ------ Output ------ For each test case, print a single line containing one integer $l$ where Chef should cut the sequence. It is guaranteed that a solution exists for the given test data. ------ Constraints ------ $1 ≤ T ≤ 20$ $2 ≤ N ≤ 10^{5}$ $2 ≤ a_{i} ≤ 10^{5}$ for each valid $i$ the sum of $N$ over all test cases does not exceed $3 \cdot 10^{5}$ ------ Subtasks ------ Subtask #1 (25 points): $N ≤ 200$ the sum of $N$ over all test cases does not exceed $600$ Subtask #2 (40 points): $N ≤ 2,000$ the sum of $N$ over all test cases does not exceed $6,000$ Subtask #3 (35 points): original constraints ----- Sample Input 1 ------ 1 4 2 3 4 5 ----- Sample Output 1 ------ 3
n = 10**5 + 5 seive = [(0) for i in range(n)] for i in range(2, n): if seive[i] == 0: seive[i] = i for j in range(i * i, n, i): seive[j] = i t = int(input()) for you in range(t): n = int(input()) l = input().split() li = [int(i) for i in l] hashi = dict() for i in li: z = i while z > 1: if seive[z] in hashi: hashi[seive[z]] += 1 else: hashi[seive[z]] = 1 z = z // seive[z] start = dict() k = len(hashi) for i in range(n): z = li[i] while z > 1: hashi[seive[z]] -= 1 start[seive[z]] = 1 if hashi[seive[z]] == 0: del hashi[seive[z]] z = z // seive[z] if len(hashi) + len(start) == k: ans = i break print(ans + 1)
ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR NUMBER ASSIGN VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FOR VAR VAR ASSIGN VAR VAR WHILE VAR NUMBER IF VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR WHILE VAR NUMBER VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER IF VAR VAR VAR NUMBER VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR IF BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER
Read problem statements in [Hindi], [Bengali], [Mandarin Chinese], [Russian], and [Vietnamese] as well. Chef wants to give a gift to Chefina to celebrate their anniversary. Of course, he has a sequence $a_{1}, a_{2}, \ldots, a_{N}$ ready for this occasion. Since the half-heart necklace is kind of cliche, he decided to cut his sequence into two pieces and give her a piece instead. Formally, he wants to choose an integer $l$ ($1 ≤ l < N$) and split the sequence into its prefix with length $l$ and its suffix with length $N-l$. Chef wants his gift to be *cute*; he thinks that it will be cute if the product of the elements in Chefina's piece is coprime with the product of the elements in his piece. Can you tell him where to cut the sequence? Find the smallest valid $l$ such that Chef's gift would be cute. ------ Input ------ The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows. The first line of each test case contains the integer $N$. The second line contains $N$ space-separated integers $a_{1}, a_{2}, \ldots, a_{N}$. ------ Output ------ For each test case, print a single line containing one integer $l$ where Chef should cut the sequence. It is guaranteed that a solution exists for the given test data. ------ Constraints ------ $1 ≤ T ≤ 20$ $2 ≤ N ≤ 10^{5}$ $2 ≤ a_{i} ≤ 10^{5}$ for each valid $i$ the sum of $N$ over all test cases does not exceed $3 \cdot 10^{5}$ ------ Subtasks ------ Subtask #1 (25 points): $N ≤ 200$ the sum of $N$ over all test cases does not exceed $600$ Subtask #2 (40 points): $N ≤ 2,000$ the sum of $N$ over all test cases does not exceed $6,000$ Subtask #3 (35 points): original constraints ----- Sample Input 1 ------ 1 4 2 3 4 5 ----- Sample Output 1 ------ 3
from sys import maxsize, stdin, stdout from _collections import defaultdict tup = lambda: map(int, stdin.readline().split()) I = lambda: int(stdin.readline()) lint = lambda: [int(x) for x in stdin.readline().split()] stpr = lambda x: stdout.write(f"{x}" + "\n") star = lambda x: print(" ".join(map(str, x))) def count(n): d = defaultdict(int) while n % 2 == 0: d[2] += 1 n //= 2 for i in range(3, int(n**0.5) + 1, 2): while n % i == 0: n //= i d[i] += 1 if n > 2: d[n] += 1 return d for _ in range(I()): n = I() ls = lint() l, r = set(count(ls[0])), set() ans = 1 for i in range(1, n): p = count(ls[i]) r.update(p) if len(l.intersection(p)) > 0: l.update(r) r.clear() ans = i + 1 print(ans)
ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR STRING ASSIGN VAR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR WHILE BIN_OP VAR NUMBER NUMBER VAR NUMBER NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER WHILE BIN_OP VAR VAR NUMBER VAR VAR VAR VAR NUMBER IF VAR NUMBER VAR VAR NUMBER RETURN VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR IF FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR
Read problem statements in [Hindi], [Bengali], [Mandarin Chinese], [Russian], and [Vietnamese] as well. Chef wants to give a gift to Chefina to celebrate their anniversary. Of course, he has a sequence $a_{1}, a_{2}, \ldots, a_{N}$ ready for this occasion. Since the half-heart necklace is kind of cliche, he decided to cut his sequence into two pieces and give her a piece instead. Formally, he wants to choose an integer $l$ ($1 ≤ l < N$) and split the sequence into its prefix with length $l$ and its suffix with length $N-l$. Chef wants his gift to be *cute*; he thinks that it will be cute if the product of the elements in Chefina's piece is coprime with the product of the elements in his piece. Can you tell him where to cut the sequence? Find the smallest valid $l$ such that Chef's gift would be cute. ------ Input ------ The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows. The first line of each test case contains the integer $N$. The second line contains $N$ space-separated integers $a_{1}, a_{2}, \ldots, a_{N}$. ------ Output ------ For each test case, print a single line containing one integer $l$ where Chef should cut the sequence. It is guaranteed that a solution exists for the given test data. ------ Constraints ------ $1 ≤ T ≤ 20$ $2 ≤ N ≤ 10^{5}$ $2 ≤ a_{i} ≤ 10^{5}$ for each valid $i$ the sum of $N$ over all test cases does not exceed $3 \cdot 10^{5}$ ------ Subtasks ------ Subtask #1 (25 points): $N ≤ 200$ the sum of $N$ over all test cases does not exceed $600$ Subtask #2 (40 points): $N ≤ 2,000$ the sum of $N$ over all test cases does not exceed $6,000$ Subtask #3 (35 points): original constraints ----- Sample Input 1 ------ 1 4 2 3 4 5 ----- Sample Output 1 ------ 3
minPrime = [0] * 100001 minPrime[1], minPrime[2] = 1, 2 i = 4 while i < 100001: minPrime[i] = 2 i += 2 for i in range(3, 100001, 2): if minPrime[i] == 0: minPrime[i] = i j = i + i while j < 100001: minPrime[j] = i j += i t = int(input()) for _ in range(t): n = int(input()) l = list(map(int, input().split())) dl = {} dr = {} for i in range(n - 1, -1, -1): j = l[i] while j > 1: try: dr[minPrime[j]] += 1 except: dr[minPrime[j]] = 1 j //= minPrime[j] for i in range(n): j = l[i] while j > 1: dr[minPrime[j]] -= 1 try: dl[minPrime[j]] += 1 except: dl[minPrime[j]] = 1 j //= minPrime[j] f = True for k in dl: if dr[k]: f = False break if f: print(i + 1) break
ASSIGN VAR BIN_OP LIST NUMBER NUMBER ASSIGN VAR NUMBER VAR NUMBER NUMBER NUMBER ASSIGN VAR NUMBER WHILE VAR NUMBER ASSIGN VAR VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER NUMBER IF VAR VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR WHILE VAR NUMBER ASSIGN VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR DICT ASSIGN VAR DICT FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR VAR WHILE VAR NUMBER VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER VAR VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR WHILE VAR NUMBER VAR VAR VAR NUMBER VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER VAR VAR VAR ASSIGN VAR NUMBER FOR VAR VAR IF VAR VAR ASSIGN VAR NUMBER IF VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER
Read problem statements in [Hindi], [Bengali], [Mandarin Chinese], [Russian], and [Vietnamese] as well. Chef wants to give a gift to Chefina to celebrate their anniversary. Of course, he has a sequence $a_{1}, a_{2}, \ldots, a_{N}$ ready for this occasion. Since the half-heart necklace is kind of cliche, he decided to cut his sequence into two pieces and give her a piece instead. Formally, he wants to choose an integer $l$ ($1 ≤ l < N$) and split the sequence into its prefix with length $l$ and its suffix with length $N-l$. Chef wants his gift to be *cute*; he thinks that it will be cute if the product of the elements in Chefina's piece is coprime with the product of the elements in his piece. Can you tell him where to cut the sequence? Find the smallest valid $l$ such that Chef's gift would be cute. ------ Input ------ The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows. The first line of each test case contains the integer $N$. The second line contains $N$ space-separated integers $a_{1}, a_{2}, \ldots, a_{N}$. ------ Output ------ For each test case, print a single line containing one integer $l$ where Chef should cut the sequence. It is guaranteed that a solution exists for the given test data. ------ Constraints ------ $1 ≤ T ≤ 20$ $2 ≤ N ≤ 10^{5}$ $2 ≤ a_{i} ≤ 10^{5}$ for each valid $i$ the sum of $N$ over all test cases does not exceed $3 \cdot 10^{5}$ ------ Subtasks ------ Subtask #1 (25 points): $N ≤ 200$ the sum of $N$ over all test cases does not exceed $600$ Subtask #2 (40 points): $N ≤ 2,000$ the sum of $N$ over all test cases does not exceed $6,000$ Subtask #3 (35 points): original constraints ----- Sample Input 1 ------ 1 4 2 3 4 5 ----- Sample Output 1 ------ 3
mpr = [(0) for x in range(100001)] for i in range(2, 100001): if mpr[i] == 0: k = i for j in range(k, 100001, i): mpr[j] = i for _ in range(int(input())): n = int(input()) a = list(map(int, input().split())) d1 = {} d2 = {} for i in range(n - 1, -1, -1): k = a[i] while k > 1: try: d2[mpr[k]] += 1 except: d2[mpr[k]] = 1 k = k // mpr[k] for i in range(n): k = a[i] while k > 1: d2[mpr[k]] -= 1 try: d1[mpr[k]] += 1 except: d1[mpr[k]] = 1 k = k // mpr[k] stat = 1 for k in d1: if d2[k] > 0: stat = 0 break if stat == 1: print(i + 1) break
ASSIGN VAR NUMBER VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER IF VAR VAR NUMBER ASSIGN VAR VAR FOR VAR FUNC_CALL VAR VAR NUMBER VAR ASSIGN VAR VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR DICT ASSIGN VAR DICT FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR VAR WHILE VAR NUMBER VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR WHILE VAR NUMBER VAR VAR VAR NUMBER VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR ASSIGN VAR NUMBER FOR VAR VAR IF VAR VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER
Given an array arr of size N with all initial values as 0, the task is to perform the following M range increment operations as shown below: Increment(a_{i}, b_{i}, k_{i}) : Increment values from index 'a_{i}' to 'b_{i}' by 'k_{i}'. After M operations, calculate the maximum value in the array arr[]. Example 1: Input: N = 5, M = 3, a[] = {0, 1, 2} b[] = {1, 4, 3}, k[] = {100, 100, 100} Output: 200 Explanation: Initially array = {0, 0, 0, 0, 0} After first operation : {100, 100, 0, 0, 0} After second operation: {100, 200, 100, 100, 100} After third operation: {100, 200, 200, 200, 100} Maximum element after m operations is 200. Example 2: Input: N = 4, M = 3, a[] = {1, 0, 3} b[] = {2, 0, 3}, k[] = {603, 286, 882} Output: 882 Explanation: Initially array = {0, 0, 0, 0} After first operation: {0, 603, 603, 0} After second operation: {286, 603, 603, 0} After third operation: {286, 603, 603, 882} Maximum element after m operations is 882. Your Task: You don't need to read input or print anything. You just need to complete the function findMax() that takes arrays a[], b[], k[] and integers N, M as parameters and returns the desired output. Expected Time Complexity: O(M+N). Expected Auxiliary Space: O(N). Constraints: 1 ≤ N ≤ 10^{6} 0 ≤ a_{i } ≤ b_{i} ≤ N-1 1 ≤ M ≤ 10^{6} 0 ≤ k_{i} ≤ 10^{6}
class Solution: def findMax(self, n, m, a, b, c): d = [0] * n maxEle = 0 for i in range(m): d[a[i]] += c[i] if b[i] + 1 < n: d[b[i] + 1] -= c[i] sumSoFar = 0 for i in range(n): sumSoFar += d[i] d[i] = sumSoFar maxEle = 0 for i in d: if i > maxEle: maxEle = i return maxEle
CLASS_DEF FUNC_DEF ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR IF BIN_OP VAR VAR NUMBER VAR VAR BIN_OP VAR VAR NUMBER VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR NUMBER FOR VAR VAR IF VAR VAR ASSIGN VAR VAR RETURN VAR
Given an array arr of size N with all initial values as 0, the task is to perform the following M range increment operations as shown below: Increment(a_{i}, b_{i}, k_{i}) : Increment values from index 'a_{i}' to 'b_{i}' by 'k_{i}'. After M operations, calculate the maximum value in the array arr[]. Example 1: Input: N = 5, M = 3, a[] = {0, 1, 2} b[] = {1, 4, 3}, k[] = {100, 100, 100} Output: 200 Explanation: Initially array = {0, 0, 0, 0, 0} After first operation : {100, 100, 0, 0, 0} After second operation: {100, 200, 100, 100, 100} After third operation: {100, 200, 200, 200, 100} Maximum element after m operations is 200. Example 2: Input: N = 4, M = 3, a[] = {1, 0, 3} b[] = {2, 0, 3}, k[] = {603, 286, 882} Output: 882 Explanation: Initially array = {0, 0, 0, 0} After first operation: {0, 603, 603, 0} After second operation: {286, 603, 603, 0} After third operation: {286, 603, 603, 882} Maximum element after m operations is 882. Your Task: You don't need to read input or print anything. You just need to complete the function findMax() that takes arrays a[], b[], k[] and integers N, M as parameters and returns the desired output. Expected Time Complexity: O(M+N). Expected Auxiliary Space: O(N). Constraints: 1 ≤ N ≤ 10^{6} 0 ≤ a_{i } ≤ b_{i} ≤ N-1 1 ≤ M ≤ 10^{6} 0 ≤ k_{i} ≤ 10^{6}
class Solution: def findMax(self, n, m, a, b, c): arr = [0] * n for i in range(m): x = a[i] y = b[i] arr[x] += c[i] if y + 1 != n: arr[y + 1] += -c[i] pre = 0 for i in range(n): pre += arr[i] arr[i] = pre return max(arr)
CLASS_DEF FUNC_DEF ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR IF BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR RETURN FUNC_CALL VAR VAR
Given an array arr of size N with all initial values as 0, the task is to perform the following M range increment operations as shown below: Increment(a_{i}, b_{i}, k_{i}) : Increment values from index 'a_{i}' to 'b_{i}' by 'k_{i}'. After M operations, calculate the maximum value in the array arr[]. Example 1: Input: N = 5, M = 3, a[] = {0, 1, 2} b[] = {1, 4, 3}, k[] = {100, 100, 100} Output: 200 Explanation: Initially array = {0, 0, 0, 0, 0} After first operation : {100, 100, 0, 0, 0} After second operation: {100, 200, 100, 100, 100} After third operation: {100, 200, 200, 200, 100} Maximum element after m operations is 200. Example 2: Input: N = 4, M = 3, a[] = {1, 0, 3} b[] = {2, 0, 3}, k[] = {603, 286, 882} Output: 882 Explanation: Initially array = {0, 0, 0, 0} After first operation: {0, 603, 603, 0} After second operation: {286, 603, 603, 0} After third operation: {286, 603, 603, 882} Maximum element after m operations is 882. Your Task: You don't need to read input or print anything. You just need to complete the function findMax() that takes arrays a[], b[], k[] and integers N, M as parameters and returns the desired output. Expected Time Complexity: O(M+N). Expected Auxiliary Space: O(N). Constraints: 1 ≤ N ≤ 10^{6} 0 ≤ a_{i } ≤ b_{i} ≤ N-1 1 ≤ M ≤ 10^{6} 0 ≤ k_{i} ≤ 10^{6}
class Solution: def findMax(self, n, m, a, b, c): arr = [(0) for i in range(n)] for i in range(m): l, r, val = a[i], b[i], c[i] arr[l] += val if r + 1 < n: arr[r + 1] -= val for i in range(1, n): arr[i] += arr[i - 1] return max(arr)
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR IF BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER VAR VAR VAR VAR BIN_OP VAR NUMBER RETURN FUNC_CALL VAR VAR
Given an array arr of size N with all initial values as 0, the task is to perform the following M range increment operations as shown below: Increment(a_{i}, b_{i}, k_{i}) : Increment values from index 'a_{i}' to 'b_{i}' by 'k_{i}'. After M operations, calculate the maximum value in the array arr[]. Example 1: Input: N = 5, M = 3, a[] = {0, 1, 2} b[] = {1, 4, 3}, k[] = {100, 100, 100} Output: 200 Explanation: Initially array = {0, 0, 0, 0, 0} After first operation : {100, 100, 0, 0, 0} After second operation: {100, 200, 100, 100, 100} After third operation: {100, 200, 200, 200, 100} Maximum element after m operations is 200. Example 2: Input: N = 4, M = 3, a[] = {1, 0, 3} b[] = {2, 0, 3}, k[] = {603, 286, 882} Output: 882 Explanation: Initially array = {0, 0, 0, 0} After first operation: {0, 603, 603, 0} After second operation: {286, 603, 603, 0} After third operation: {286, 603, 603, 882} Maximum element after m operations is 882. Your Task: You don't need to read input or print anything. You just need to complete the function findMax() that takes arrays a[], b[], k[] and integers N, M as parameters and returns the desired output. Expected Time Complexity: O(M+N). Expected Auxiliary Space: O(N). Constraints: 1 ≤ N ≤ 10^{6} 0 ≤ a_{i } ≤ b_{i} ≤ N-1 1 ≤ M ≤ 10^{6} 0 ≤ k_{i} ≤ 10^{6}
class Solution: def findMax(self, n, m, a, b, k): l = [0] * (n + 1) for i in range(m): l[a[i]] += k[i] l[b[i] + 1] -= k[i] for i in range(1, len(l)): l[i] = l[i] + l[i - 1] return max(l)
CLASS_DEF FUNC_DEF ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR VAR BIN_OP VAR VAR NUMBER VAR VAR FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER RETURN FUNC_CALL VAR VAR
Given an array arr of size N with all initial values as 0, the task is to perform the following M range increment operations as shown below: Increment(a_{i}, b_{i}, k_{i}) : Increment values from index 'a_{i}' to 'b_{i}' by 'k_{i}'. After M operations, calculate the maximum value in the array arr[]. Example 1: Input: N = 5, M = 3, a[] = {0, 1, 2} b[] = {1, 4, 3}, k[] = {100, 100, 100} Output: 200 Explanation: Initially array = {0, 0, 0, 0, 0} After first operation : {100, 100, 0, 0, 0} After second operation: {100, 200, 100, 100, 100} After third operation: {100, 200, 200, 200, 100} Maximum element after m operations is 200. Example 2: Input: N = 4, M = 3, a[] = {1, 0, 3} b[] = {2, 0, 3}, k[] = {603, 286, 882} Output: 882 Explanation: Initially array = {0, 0, 0, 0} After first operation: {0, 603, 603, 0} After second operation: {286, 603, 603, 0} After third operation: {286, 603, 603, 882} Maximum element after m operations is 882. Your Task: You don't need to read input or print anything. You just need to complete the function findMax() that takes arrays a[], b[], k[] and integers N, M as parameters and returns the desired output. Expected Time Complexity: O(M+N). Expected Auxiliary Space: O(N). Constraints: 1 ≤ N ≤ 10^{6} 0 ≤ a_{i } ≤ b_{i} ≤ N-1 1 ≤ M ≤ 10^{6} 0 ≤ k_{i} ≤ 10^{6}
class Solution: def findMax(self, n, m, a, b, c): arr = [0] * (n + 2) for i in range(m): arr[a[i]] += c[i] arr[b[i] + 1] -= c[i] mx = arr[0] for i in range(1, n): arr[i] += arr[i - 1] mx = max(mx, arr[i]) return mx
CLASS_DEF FUNC_DEF ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR VAR BIN_OP VAR VAR NUMBER VAR VAR ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR RETURN VAR
Given an array arr of size N with all initial values as 0, the task is to perform the following M range increment operations as shown below: Increment(a_{i}, b_{i}, k_{i}) : Increment values from index 'a_{i}' to 'b_{i}' by 'k_{i}'. After M operations, calculate the maximum value in the array arr[]. Example 1: Input: N = 5, M = 3, a[] = {0, 1, 2} b[] = {1, 4, 3}, k[] = {100, 100, 100} Output: 200 Explanation: Initially array = {0, 0, 0, 0, 0} After first operation : {100, 100, 0, 0, 0} After second operation: {100, 200, 100, 100, 100} After third operation: {100, 200, 200, 200, 100} Maximum element after m operations is 200. Example 2: Input: N = 4, M = 3, a[] = {1, 0, 3} b[] = {2, 0, 3}, k[] = {603, 286, 882} Output: 882 Explanation: Initially array = {0, 0, 0, 0} After first operation: {0, 603, 603, 0} After second operation: {286, 603, 603, 0} After third operation: {286, 603, 603, 882} Maximum element after m operations is 882. Your Task: You don't need to read input or print anything. You just need to complete the function findMax() that takes arrays a[], b[], k[] and integers N, M as parameters and returns the desired output. Expected Time Complexity: O(M+N). Expected Auxiliary Space: O(N). Constraints: 1 ≤ N ≤ 10^{6} 0 ≤ a_{i } ≤ b_{i} ≤ N-1 1 ≤ M ≤ 10^{6} 0 ≤ k_{i} ≤ 10^{6}
import sys class Solution: def findMax(self, n, m, a, b, k): arr = [(0) for i in range(n + 1)] for i in range(m): lowerbound = a[i] upperbound = b[i] arr[lowerbound] += k[i] arr[upperbound + 1] -= k[i] sum = 0 res = -1 - sys.maxsize for i in range(n): sum += arr[i] res = max(res, sum) return res
IMPORT CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP NUMBER VAR FOR VAR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN VAR
Given an array arr of size N with all initial values as 0, the task is to perform the following M range increment operations as shown below: Increment(a_{i}, b_{i}, k_{i}) : Increment values from index 'a_{i}' to 'b_{i}' by 'k_{i}'. After M operations, calculate the maximum value in the array arr[]. Example 1: Input: N = 5, M = 3, a[] = {0, 1, 2} b[] = {1, 4, 3}, k[] = {100, 100, 100} Output: 200 Explanation: Initially array = {0, 0, 0, 0, 0} After first operation : {100, 100, 0, 0, 0} After second operation: {100, 200, 100, 100, 100} After third operation: {100, 200, 200, 200, 100} Maximum element after m operations is 200. Example 2: Input: N = 4, M = 3, a[] = {1, 0, 3} b[] = {2, 0, 3}, k[] = {603, 286, 882} Output: 882 Explanation: Initially array = {0, 0, 0, 0} After first operation: {0, 603, 603, 0} After second operation: {286, 603, 603, 0} After third operation: {286, 603, 603, 882} Maximum element after m operations is 882. Your Task: You don't need to read input or print anything. You just need to complete the function findMax() that takes arrays a[], b[], k[] and integers N, M as parameters and returns the desired output. Expected Time Complexity: O(M+N). Expected Auxiliary Space: O(N). Constraints: 1 ≤ N ≤ 10^{6} 0 ≤ a_{i } ≤ b_{i} ≤ N-1 1 ≤ M ≤ 10^{6} 0 ≤ k_{i} ≤ 10^{6}
class Solution: def findMax(self, n, m, a, b, c): ans = [0] * n for i in range(m): ans[a[i]] += c[i] if b[i] + 1 < n: ans[b[i] + 1] -= c[i] m = ans[0] for i in range(1, n): ans[i] = ans[i] + ans[i - 1] return max(ans) if __name__ == "__main__": t = int(input()) for _ in range(0, t): l = list(map(int, input().split())) n = l[0] m = l[1] a = [] b = [] c = [] for j in range(0, m): v = list(map(int, input().split())) a.append(v[0]) b.append(v[1]) c.append(v[2]) ob = Solution() print(ob.findMax(n, m, a, b, c))
CLASS_DEF FUNC_DEF ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR IF BIN_OP VAR VAR NUMBER VAR VAR BIN_OP VAR VAR NUMBER VAR VAR ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER RETURN FUNC_CALL VAR VAR IF VAR STRING ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR VAR
Given an array arr of size N with all initial values as 0, the task is to perform the following M range increment operations as shown below: Increment(a_{i}, b_{i}, k_{i}) : Increment values from index 'a_{i}' to 'b_{i}' by 'k_{i}'. After M operations, calculate the maximum value in the array arr[]. Example 1: Input: N = 5, M = 3, a[] = {0, 1, 2} b[] = {1, 4, 3}, k[] = {100, 100, 100} Output: 200 Explanation: Initially array = {0, 0, 0, 0, 0} After first operation : {100, 100, 0, 0, 0} After second operation: {100, 200, 100, 100, 100} After third operation: {100, 200, 200, 200, 100} Maximum element after m operations is 200. Example 2: Input: N = 4, M = 3, a[] = {1, 0, 3} b[] = {2, 0, 3}, k[] = {603, 286, 882} Output: 882 Explanation: Initially array = {0, 0, 0, 0} After first operation: {0, 603, 603, 0} After second operation: {286, 603, 603, 0} After third operation: {286, 603, 603, 882} Maximum element after m operations is 882. Your Task: You don't need to read input or print anything. You just need to complete the function findMax() that takes arrays a[], b[], k[] and integers N, M as parameters and returns the desired output. Expected Time Complexity: O(M+N). Expected Auxiliary Space: O(N). Constraints: 1 ≤ N ≤ 10^{6} 0 ≤ a_{i } ≤ b_{i} ≤ N-1 1 ≤ M ≤ 10^{6} 0 ≤ k_{i} ≤ 10^{6}
class Solution: def findMax(self, n, m, a, b, c): ans = [(0) for i in range(n + 1)] for i in range(m): lowerbound = a[i] upperbound = b[i] ans[lowerbound] += c[i] ans[upperbound + 1] -= c[i] maxsum = ans[0] for i in range(1, n): ans[i] += ans[i - 1] if maxsum < ans[i]: maxsum = ans[i] return maxsum
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR VAR VAR VAR BIN_OP VAR NUMBER IF VAR VAR VAR ASSIGN VAR VAR VAR RETURN VAR
Given an array arr of size N with all initial values as 0, the task is to perform the following M range increment operations as shown below: Increment(a_{i}, b_{i}, k_{i}) : Increment values from index 'a_{i}' to 'b_{i}' by 'k_{i}'. After M operations, calculate the maximum value in the array arr[]. Example 1: Input: N = 5, M = 3, a[] = {0, 1, 2} b[] = {1, 4, 3}, k[] = {100, 100, 100} Output: 200 Explanation: Initially array = {0, 0, 0, 0, 0} After first operation : {100, 100, 0, 0, 0} After second operation: {100, 200, 100, 100, 100} After third operation: {100, 200, 200, 200, 100} Maximum element after m operations is 200. Example 2: Input: N = 4, M = 3, a[] = {1, 0, 3} b[] = {2, 0, 3}, k[] = {603, 286, 882} Output: 882 Explanation: Initially array = {0, 0, 0, 0} After first operation: {0, 603, 603, 0} After second operation: {286, 603, 603, 0} After third operation: {286, 603, 603, 882} Maximum element after m operations is 882. Your Task: You don't need to read input or print anything. You just need to complete the function findMax() that takes arrays a[], b[], k[] and integers N, M as parameters and returns the desired output. Expected Time Complexity: O(M+N). Expected Auxiliary Space: O(N). Constraints: 1 ≤ N ≤ 10^{6} 0 ≤ a_{i } ≤ b_{i} ≤ N-1 1 ≤ M ≤ 10^{6} 0 ≤ k_{i} ≤ 10^{6}
class Solution: def findMax(self, n, m, a, b, c): arr = [[] for i in range(n)] li = [(0) for i in range(n)] for i in range(m): arr[a[i]].append([c[i], 0]) arr[b[i]].append([c[i], 1]) x = 0 for i in range(n): s = e = 0 for k, l in arr[i]: if l == 0: s += k else: e += k li[i] = x + s x += s - e return max(li)
CLASS_DEF FUNC_DEF ASSIGN VAR LIST VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR LIST VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR LIST VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER FOR VAR VAR VAR VAR IF VAR NUMBER VAR VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR RETURN FUNC_CALL VAR VAR
Given an array arr of size N with all initial values as 0, the task is to perform the following M range increment operations as shown below: Increment(a_{i}, b_{i}, k_{i}) : Increment values from index 'a_{i}' to 'b_{i}' by 'k_{i}'. After M operations, calculate the maximum value in the array arr[]. Example 1: Input: N = 5, M = 3, a[] = {0, 1, 2} b[] = {1, 4, 3}, k[] = {100, 100, 100} Output: 200 Explanation: Initially array = {0, 0, 0, 0, 0} After first operation : {100, 100, 0, 0, 0} After second operation: {100, 200, 100, 100, 100} After third operation: {100, 200, 200, 200, 100} Maximum element after m operations is 200. Example 2: Input: N = 4, M = 3, a[] = {1, 0, 3} b[] = {2, 0, 3}, k[] = {603, 286, 882} Output: 882 Explanation: Initially array = {0, 0, 0, 0} After first operation: {0, 603, 603, 0} After second operation: {286, 603, 603, 0} After third operation: {286, 603, 603, 882} Maximum element after m operations is 882. Your Task: You don't need to read input or print anything. You just need to complete the function findMax() that takes arrays a[], b[], k[] and integers N, M as parameters and returns the desired output. Expected Time Complexity: O(M+N). Expected Auxiliary Space: O(N). Constraints: 1 ≤ N ≤ 10^{6} 0 ≤ a_{i } ≤ b_{i} ≤ N-1 1 ≤ M ≤ 10^{6} 0 ≤ k_{i} ≤ 10^{6}
class Solution: def findMax(self, n, m, a, b, c): d = dict() for i in range(n + 1): d[i] = 0 for i in range(m): d[a[i]] = d[a[i]] + c[i] d[b[i] + 1] = d[b[i] + 1] - c[i] m = 0 s = 0 for i in range(n): if d.get(i) != None: s = s + d[i] m = max(m, s) return m
CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR NUMBER BIN_OP VAR BIN_OP VAR VAR NUMBER VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR NONE ASSIGN VAR BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN VAR
Given an array arr of size N with all initial values as 0, the task is to perform the following M range increment operations as shown below: Increment(a_{i}, b_{i}, k_{i}) : Increment values from index 'a_{i}' to 'b_{i}' by 'k_{i}'. After M operations, calculate the maximum value in the array arr[]. Example 1: Input: N = 5, M = 3, a[] = {0, 1, 2} b[] = {1, 4, 3}, k[] = {100, 100, 100} Output: 200 Explanation: Initially array = {0, 0, 0, 0, 0} After first operation : {100, 100, 0, 0, 0} After second operation: {100, 200, 100, 100, 100} After third operation: {100, 200, 200, 200, 100} Maximum element after m operations is 200. Example 2: Input: N = 4, M = 3, a[] = {1, 0, 3} b[] = {2, 0, 3}, k[] = {603, 286, 882} Output: 882 Explanation: Initially array = {0, 0, 0, 0} After first operation: {0, 603, 603, 0} After second operation: {286, 603, 603, 0} After third operation: {286, 603, 603, 882} Maximum element after m operations is 882. Your Task: You don't need to read input or print anything. You just need to complete the function findMax() that takes arrays a[], b[], k[] and integers N, M as parameters and returns the desired output. Expected Time Complexity: O(M+N). Expected Auxiliary Space: O(N). Constraints: 1 ≤ N ≤ 10^{6} 0 ≤ a_{i } ≤ b_{i} ≤ N-1 1 ≤ M ≤ 10^{6} 0 ≤ k_{i} ≤ 10^{6}
class Solution: def findMax(self, n, m, a, b, c): ques = [(0) for i in range(n)] for i in range(m): l, r, cost = a[i], b[i], c[i] ques[l] += cost if r + 1 < n: ques[r + 1] -= cost res = [ques[0]] ans = ques[0] for i in range(1, n): ans = max(ans, res[-1] + ques[i]) res.append(res[-1] + ques[i]) return ans
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR IF BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR LIST VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR RETURN VAR
Given an array arr of size N with all initial values as 0, the task is to perform the following M range increment operations as shown below: Increment(a_{i}, b_{i}, k_{i}) : Increment values from index 'a_{i}' to 'b_{i}' by 'k_{i}'. After M operations, calculate the maximum value in the array arr[]. Example 1: Input: N = 5, M = 3, a[] = {0, 1, 2} b[] = {1, 4, 3}, k[] = {100, 100, 100} Output: 200 Explanation: Initially array = {0, 0, 0, 0, 0} After first operation : {100, 100, 0, 0, 0} After second operation: {100, 200, 100, 100, 100} After third operation: {100, 200, 200, 200, 100} Maximum element after m operations is 200. Example 2: Input: N = 4, M = 3, a[] = {1, 0, 3} b[] = {2, 0, 3}, k[] = {603, 286, 882} Output: 882 Explanation: Initially array = {0, 0, 0, 0} After first operation: {0, 603, 603, 0} After second operation: {286, 603, 603, 0} After third operation: {286, 603, 603, 882} Maximum element after m operations is 882. Your Task: You don't need to read input or print anything. You just need to complete the function findMax() that takes arrays a[], b[], k[] and integers N, M as parameters and returns the desired output. Expected Time Complexity: O(M+N). Expected Auxiliary Space: O(N). Constraints: 1 ≤ N ≤ 10^{6} 0 ≤ a_{i } ≤ b_{i} ≤ N-1 1 ≤ M ≤ 10^{6} 0 ≤ k_{i} ≤ 10^{6}
class Solution: def findMax(self, n, m, a, b, c): ls = [0] * n for i in range(m): ls[a[i]] += c[i] if b[i] + 1 < len(ls): ls[b[i] + 1] -= c[i] sumi = ls[0] for i in range(1, len(ls)): sumi += ls[i] ls[i] = sumi return max(ls)
CLASS_DEF FUNC_DEF ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR IF BIN_OP VAR VAR NUMBER FUNC_CALL VAR VAR VAR BIN_OP VAR VAR NUMBER VAR VAR ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR RETURN FUNC_CALL VAR VAR
Given an array arr of size N with all initial values as 0, the task is to perform the following M range increment operations as shown below: Increment(a_{i}, b_{i}, k_{i}) : Increment values from index 'a_{i}' to 'b_{i}' by 'k_{i}'. After M operations, calculate the maximum value in the array arr[]. Example 1: Input: N = 5, M = 3, a[] = {0, 1, 2} b[] = {1, 4, 3}, k[] = {100, 100, 100} Output: 200 Explanation: Initially array = {0, 0, 0, 0, 0} After first operation : {100, 100, 0, 0, 0} After second operation: {100, 200, 100, 100, 100} After third operation: {100, 200, 200, 200, 100} Maximum element after m operations is 200. Example 2: Input: N = 4, M = 3, a[] = {1, 0, 3} b[] = {2, 0, 3}, k[] = {603, 286, 882} Output: 882 Explanation: Initially array = {0, 0, 0, 0} After first operation: {0, 603, 603, 0} After second operation: {286, 603, 603, 0} After third operation: {286, 603, 603, 882} Maximum element after m operations is 882. Your Task: You don't need to read input or print anything. You just need to complete the function findMax() that takes arrays a[], b[], k[] and integers N, M as parameters and returns the desired output. Expected Time Complexity: O(M+N). Expected Auxiliary Space: O(N). Constraints: 1 ≤ N ≤ 10^{6} 0 ≤ a_{i } ≤ b_{i} ≤ N-1 1 ≤ M ≤ 10^{6} 0 ≤ k_{i} ≤ 10^{6}
class Solution: def findMax(self, n, m, a, b, c): rec = [0] * n for i in range(m): rec[b[i]] += c[i] if a[i] > 0: rec[a[i] - 1] -= c[i] ac = 0 for i in range(n - 1, -1, -1): ac += rec[i] rec[i] = ac return max(rec)
CLASS_DEF FUNC_DEF ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR IF VAR VAR NUMBER VAR BIN_OP VAR VAR NUMBER VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER VAR VAR VAR ASSIGN VAR VAR VAR RETURN FUNC_CALL VAR VAR
Given an array arr of size N with all initial values as 0, the task is to perform the following M range increment operations as shown below: Increment(a_{i}, b_{i}, k_{i}) : Increment values from index 'a_{i}' to 'b_{i}' by 'k_{i}'. After M operations, calculate the maximum value in the array arr[]. Example 1: Input: N = 5, M = 3, a[] = {0, 1, 2} b[] = {1, 4, 3}, k[] = {100, 100, 100} Output: 200 Explanation: Initially array = {0, 0, 0, 0, 0} After first operation : {100, 100, 0, 0, 0} After second operation: {100, 200, 100, 100, 100} After third operation: {100, 200, 200, 200, 100} Maximum element after m operations is 200. Example 2: Input: N = 4, M = 3, a[] = {1, 0, 3} b[] = {2, 0, 3}, k[] = {603, 286, 882} Output: 882 Explanation: Initially array = {0, 0, 0, 0} After first operation: {0, 603, 603, 0} After second operation: {286, 603, 603, 0} After third operation: {286, 603, 603, 882} Maximum element after m operations is 882. Your Task: You don't need to read input or print anything. You just need to complete the function findMax() that takes arrays a[], b[], k[] and integers N, M as parameters and returns the desired output. Expected Time Complexity: O(M+N). Expected Auxiliary Space: O(N). Constraints: 1 ≤ N ≤ 10^{6} 0 ≤ a_{i } ≤ b_{i} ≤ N-1 1 ≤ M ≤ 10^{6} 0 ≤ k_{i} ≤ 10^{6}
class Solution: def findMax(self, n, m, a, b, c): arr = [0] * n for i in range(m): arr[a[i]] += c[i] if b[i] < len(arr) - 1: arr[b[i] + 1] -= c[i] maxa = 0 suma = 0 for i in range(n): suma += arr[i] maxa = max(suma, maxa) return maxa
CLASS_DEF FUNC_DEF ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR IF VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR BIN_OP VAR VAR NUMBER VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN VAR
Given an array arr of size N with all initial values as 0, the task is to perform the following M range increment operations as shown below: Increment(a_{i}, b_{i}, k_{i}) : Increment values from index 'a_{i}' to 'b_{i}' by 'k_{i}'. After M operations, calculate the maximum value in the array arr[]. Example 1: Input: N = 5, M = 3, a[] = {0, 1, 2} b[] = {1, 4, 3}, k[] = {100, 100, 100} Output: 200 Explanation: Initially array = {0, 0, 0, 0, 0} After first operation : {100, 100, 0, 0, 0} After second operation: {100, 200, 100, 100, 100} After third operation: {100, 200, 200, 200, 100} Maximum element after m operations is 200. Example 2: Input: N = 4, M = 3, a[] = {1, 0, 3} b[] = {2, 0, 3}, k[] = {603, 286, 882} Output: 882 Explanation: Initially array = {0, 0, 0, 0} After first operation: {0, 603, 603, 0} After second operation: {286, 603, 603, 0} After third operation: {286, 603, 603, 882} Maximum element after m operations is 882. Your Task: You don't need to read input or print anything. You just need to complete the function findMax() that takes arrays a[], b[], k[] and integers N, M as parameters and returns the desired output. Expected Time Complexity: O(M+N). Expected Auxiliary Space: O(N). Constraints: 1 ≤ N ≤ 10^{6} 0 ≤ a_{i } ≤ b_{i} ≤ N-1 1 ≤ M ≤ 10^{6} 0 ≤ k_{i} ≤ 10^{6}
class Solution: def findMax(self, n, m, a, b, c): db = {} de = {} for i in range(m): if a[i] not in db.keys(): db[a[i]] = c[i] else: db[a[i]] += c[i] if b[i] not in de.keys(): de[b[i]] = c[i] else: de[b[i]] += c[i] ans = [] x = 0 for i in range(n): if i in db.keys(): x += db[i] ans.append(x) if i in de.keys(): x -= de[i] return max(ans)
CLASS_DEF FUNC_DEF ASSIGN VAR DICT ASSIGN VAR DICT FOR VAR FUNC_CALL VAR VAR IF VAR VAR FUNC_CALL VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR IF VAR VAR FUNC_CALL VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR IF VAR FUNC_CALL VAR VAR VAR VAR RETURN FUNC_CALL VAR VAR
We are given a list of (axis-aligned) rectangles.  Each rectangle[i] = [x1, y1, x2, y2] , where (x1, y1) are the coordinates of the bottom-left corner, and (x2, y2) are the coordinates of the top-right corner of the ith rectangle. Find the total area covered by all rectangles in the plane.  Since the answer may be too large, return it modulo 10^9 + 7. Example 1: Input: [[0,0,2,2],[1,0,2,3],[1,0,3,1]] Output: 6 Explanation: As illustrated in the picture. Example 2: Input: [[0,0,1000000000,1000000000]] Output: 49 Explanation: The answer is 10^18 modulo (10^9 + 7), which is (10^9)^2 = (-7)^2 = 49. Note: 1 <= rectangles.length <= 200 rectanges[i].length = 4 0 <= rectangles[i][j] <= 10^9 The total area covered by all rectangles will never exceed 2^63 - 1 and thus will fit in a 64-bit signed integer.
class Solution: def rectangleArea(self, rectangles: List[List[int]]) -> int: Xs = [x for x1, _, x2, _ in rectangles for x in [x1, x2]] Xs.sort() X_idx_lookup = {x: idx for idx, x in enumerate(Xs)} res = 0 prev_y = 0 X_span = 0 Ys = [ e for x1, y1, x2, y2 in rectangles for e in [[y1, x1, x2, 1], [y2, x1, x2, -1]] ] Ys.sort() overlap_count = [0] * len(Xs) for y, xl, xr, inout in Ys: res += (y - prev_y) * X_span prev_y = y start_idx, end_idx = X_idx_lookup[xl], X_idx_lookup[xr] for i in range(start_idx, end_idx): overlap_count[i] += inout X_span = sum( x2 - x1 if c > 0 else 0 for x1, x2, c in zip(Xs, Xs[1:], overlap_count) ) return res % 1000000007 def rectangleArea(self, rectangles: List[List[int]]) -> int: Xs = [x for x1, _, x2, _ in rectangles for x in [x1, x2]] Xs.sort() covered = [0] * len(Xs) res = 0 keypoints = [ e for x1, y1, x2, y2 in rectangles for e in [[y1, x1, x2, 1], [y2, x1, x2, -1]] ] keypoints.sort() prev_y = 0 width_span = 0 for y, x1, x2, inout in keypoints: res += (y - prev_y) * width_span prev_y = y for i in range(len(covered) - 1): a, b = Xs[i], Xs[i + 1] if x1 <= a and b <= x2: covered[i] += inout width_span = sum( Xs[i + 1] - Xs[i] if covered[i] > 0 else 0 for i in range(len(covered) - 1) ) return res % 1000000007
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR LIST VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR VAR VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR LIST LIST VAR VAR VAR NUMBER LIST VAR VAR VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR VAR FOR VAR VAR VAR VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR FOR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER BIN_OP VAR VAR NUMBER VAR VAR VAR FUNC_CALL VAR VAR VAR NUMBER VAR RETURN BIN_OP VAR NUMBER VAR FUNC_DEF VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR LIST VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR LIST LIST VAR VAR VAR NUMBER LIST VAR VAR VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR VAR VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR ASSIGN VAR VAR FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR VAR VAR VAR BIN_OP VAR NUMBER IF VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER VAR VAR NUMBER VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER RETURN BIN_OP VAR NUMBER VAR
We are given a list of (axis-aligned) rectangles.  Each rectangle[i] = [x1, y1, x2, y2] , where (x1, y1) are the coordinates of the bottom-left corner, and (x2, y2) are the coordinates of the top-right corner of the ith rectangle. Find the total area covered by all rectangles in the plane.  Since the answer may be too large, return it modulo 10^9 + 7. Example 1: Input: [[0,0,2,2],[1,0,2,3],[1,0,3,1]] Output: 6 Explanation: As illustrated in the picture. Example 2: Input: [[0,0,1000000000,1000000000]] Output: 49 Explanation: The answer is 10^18 modulo (10^9 + 7), which is (10^9)^2 = (-7)^2 = 49. Note: 1 <= rectangles.length <= 200 rectanges[i].length = 4 0 <= rectangles[i][j] <= 10^9 The total area covered by all rectangles will never exceed 2^63 - 1 and thus will fit in a 64-bit signed integer.
MOD = 10**9 + 7 class Solution: def rectangleArea(self, rectangles: List[List[int]]) -> int: allY = [] for x1, y1, x2, y2 in rectangles: allY.append((y1, 0, x1, x2)) allY.append((y2, 1, x1, x2)) allY.sort() allX, ans = [], 0 curHeight = allY[0][0] for y, t, x1, x2 in allY: ans += self.getX(allX) * (y - curHeight) ans %= MOD if t == 0: bisect.insort(allX, (x1, x2)) else: idx = bisect.bisect_left(allX, (x1, x2)) allX.pop(idx) curHeight = y return ans def getX(self, allX): ans = 0 cur = -1 for x1, x2 in allX: cur = max(cur, x1) ans += max(0, x2 - cur) cur = max(cur, x2) return ans
ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR LIST FOR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR NUMBER VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR VAR LIST NUMBER ASSIGN VAR VAR NUMBER NUMBER FOR VAR VAR VAR VAR VAR VAR BIN_OP FUNC_CALL VAR VAR BIN_OP VAR VAR VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR RETURN VAR VAR FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR NUMBER BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN VAR
We are given a list of (axis-aligned) rectangles.  Each rectangle[i] = [x1, y1, x2, y2] , where (x1, y1) are the coordinates of the bottom-left corner, and (x2, y2) are the coordinates of the top-right corner of the ith rectangle. Find the total area covered by all rectangles in the plane.  Since the answer may be too large, return it modulo 10^9 + 7. Example 1: Input: [[0,0,2,2],[1,0,2,3],[1,0,3,1]] Output: 6 Explanation: As illustrated in the picture. Example 2: Input: [[0,0,1000000000,1000000000]] Output: 49 Explanation: The answer is 10^18 modulo (10^9 + 7), which is (10^9)^2 = (-7)^2 = 49. Note: 1 <= rectangles.length <= 200 rectanges[i].length = 4 0 <= rectangles[i][j] <= 10^9 The total area covered by all rectangles will never exceed 2^63 - 1 and thus will fit in a 64-bit signed integer.
class Solution: def rectangleArea(self, rectangles: List[List[int]]) -> int: all_rectangles = [] for rectangle in rectangles: self.add_rectangle(all_rectangles, rectangle, 0) ans = 0 mod = pow(10, 9) + 7 for rect in all_rectangles: x1, y1, x2, y2 = rect ans += (x2 - x1) * (y2 - y1) % mod return ans % mod def add_rectangle(self, all_rectangles, cur, start): if start >= len(all_rectangles): all_rectangles.append(cur) return x1, y1, x2, y2 = cur rx1, ry1, rx2, ry2 = all_rectangles[start] if x2 <= rx1 or x1 >= rx2 or y2 <= ry1 or y1 >= ry2: self.add_rectangle(all_rectangles, cur, start + 1) return if x1 < rx1: self.add_rectangle(all_rectangles, [x1, y1, rx1, y2], start + 1) if x2 > rx2: self.add_rectangle(all_rectangles, [rx2, y1, x2, y2], start + 1) if y1 < ry1: self.add_rectangle( all_rectangles, [max(x1, rx1), y1, min(x2, rx2), ry1], start + 1 ) if y2 > ry2: self.add_rectangle( all_rectangles, [max(x1, rx1), ry2, min(x2, rx2), y2], start + 1 )
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR LIST FOR VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR NUMBER NUMBER NUMBER FOR VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR VAR RETURN BIN_OP VAR VAR VAR FUNC_DEF IF VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR RETURN ASSIGN VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR IF VAR VAR VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER RETURN IF VAR VAR EXPR FUNC_CALL VAR VAR LIST VAR VAR VAR VAR BIN_OP VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR VAR LIST VAR VAR VAR VAR BIN_OP VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR VAR LIST FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR VAR LIST FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER
We are given a list of (axis-aligned) rectangles.  Each rectangle[i] = [x1, y1, x2, y2] , where (x1, y1) are the coordinates of the bottom-left corner, and (x2, y2) are the coordinates of the top-right corner of the ith rectangle. Find the total area covered by all rectangles in the plane.  Since the answer may be too large, return it modulo 10^9 + 7. Example 1: Input: [[0,0,2,2],[1,0,2,3],[1,0,3,1]] Output: 6 Explanation: As illustrated in the picture. Example 2: Input: [[0,0,1000000000,1000000000]] Output: 49 Explanation: The answer is 10^18 modulo (10^9 + 7), which is (10^9)^2 = (-7)^2 = 49. Note: 1 <= rectangles.length <= 200 rectanges[i].length = 4 0 <= rectangles[i][j] <= 10^9 The total area covered by all rectangles will never exceed 2^63 - 1 and thus will fit in a 64-bit signed integer.
class Solution: def rectangleArea(self, rectangles: List[List[int]]) -> int: mod = 10**9 + 7 events = [] for x1, y1, x2, y2 in rectangles: events.append([x1, 0, y1, y2]) events.append([x2, 1, y1, y2]) events.sort(key=lambda x: (x[0], -x[1])) def getArea(m): area = 0 prev = float("-inf") for l, r in heights: prev = max(prev, l) area += max(0, r - prev) * m prev = max(prev, r) return area area = 0 prev = 0 heights = [] for event in events: cur, close, y1, y2 = event area += getArea(cur - prev) if close: heights.remove((y1, y2)) else: heights.append((y1, y2)) heights.sort() prev = cur return area % mod
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER ASSIGN VAR LIST FOR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR LIST VAR NUMBER VAR VAR EXPR FUNC_CALL VAR LIST VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR NUMBER VAR NUMBER FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR STRING FOR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR BIN_OP FUNC_CALL VAR NUMBER BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST FOR VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR FUNC_CALL VAR BIN_OP VAR VAR IF VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR VAR RETURN BIN_OP VAR VAR VAR
We are given a list of (axis-aligned) rectangles.  Each rectangle[i] = [x1, y1, x2, y2] , where (x1, y1) are the coordinates of the bottom-left corner, and (x2, y2) are the coordinates of the top-right corner of the ith rectangle. Find the total area covered by all rectangles in the plane.  Since the answer may be too large, return it modulo 10^9 + 7. Example 1: Input: [[0,0,2,2],[1,0,2,3],[1,0,3,1]] Output: 6 Explanation: As illustrated in the picture. Example 2: Input: [[0,0,1000000000,1000000000]] Output: 49 Explanation: The answer is 10^18 modulo (10^9 + 7), which is (10^9)^2 = (-7)^2 = 49. Note: 1 <= rectangles.length <= 200 rectanges[i].length = 4 0 <= rectangles[i][j] <= 10^9 The total area covered by all rectangles will never exceed 2^63 - 1 and thus will fit in a 64-bit signed integer.
class Solution: def rectangleArea(self, rectangles: List[List[int]]) -> int: xs = sorted({x for x1, _, x2, _ in rectangles for x in [x1, x2]}) index = {v: i for i, v in enumerate(xs)} cnt = [0] * len(index) events = [] for x1, y1, x2, y2 in rectangles: events.append([y1, x1, x2, 1]) events.append([y2, x1, x2, -1]) events.sort() curr_y = curr_x_sum = area = 0 for y, x1, x2, sign in events: area += (y - curr_y) * curr_x_sum curr_y = y for i in range(index[x1], index[x2]): cnt[i] += sign curr_x_sum = sum(x2 - x1 if c else 0 for x1, x2, c in zip(xs, xs[1:], cnt)) return area % (10**9 + 7)
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR VAR LIST VAR VAR ASSIGN VAR VAR VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR VAR ASSIGN VAR LIST FOR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR LIST VAR VAR VAR NUMBER EXPR FUNC_CALL VAR LIST VAR VAR VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR VAR VAR NUMBER FOR VAR VAR VAR VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR ASSIGN VAR VAR FOR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR NUMBER VAR VAR VAR FUNC_CALL VAR VAR VAR NUMBER VAR RETURN BIN_OP VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER VAR
We are given a list of (axis-aligned) rectangles.  Each rectangle[i] = [x1, y1, x2, y2] , where (x1, y1) are the coordinates of the bottom-left corner, and (x2, y2) are the coordinates of the top-right corner of the ith rectangle. Find the total area covered by all rectangles in the plane.  Since the answer may be too large, return it modulo 10^9 + 7. Example 1: Input: [[0,0,2,2],[1,0,2,3],[1,0,3,1]] Output: 6 Explanation: As illustrated in the picture. Example 2: Input: [[0,0,1000000000,1000000000]] Output: 49 Explanation: The answer is 10^18 modulo (10^9 + 7), which is (10^9)^2 = (-7)^2 = 49. Note: 1 <= rectangles.length <= 200 rectanges[i].length = 4 0 <= rectangles[i][j] <= 10^9 The total area covered by all rectangles will never exceed 2^63 - 1 and thus will fit in a 64-bit signed integer.
class Solution: def rectangleArea(self, rectangles): OPEN, CLOSE = 1, -1 events = [] nonlocal X X = set() for x1, y1, x2, y2 in rectangles: events.append((y1, OPEN, x1, x2)) events.append((y2, CLOSE, x1, x2)) X.add(x1) X.add(x2) events.sort() X = sorted(X) Xi = {x: i for i, x in enumerate(X)} active = Node(0, len(X) - 1) ans = 0 cur_x_sum = 0 cur_y = events[0][0] for y, typ, x1, x2 in events: ans += cur_x_sum * (y - cur_y) cur_x_sum = active.update(Xi[x1], Xi[x2], typ) cur_y = y return ans % (10**9 + 7) class Node(object): def __init__(self, start, end): self.start, self.end = start, end self.total = self.count = 0 self._left = self._right = None @property def mid(self): return (self.start + self.end) // 2 @property def left(self): self._left = self._left or Node(self.start, self.mid) return self._left @property def right(self): self._right = self._right or Node(self.mid, self.end) return self._right def update(self, i, j, val): if i >= j: return 0 if self.start == i and self.end == j: self.count += val else: self.left.update(i, min(self.mid, j), val) self.right.update(max(self.mid, i), j, val) if self.count > 0: self.total = X[self.end] - X[self.start] else: self.total = self.left.total + self.right.total return self.total
CLASS_DEF FUNC_DEF ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR LIST ASSIGN VAR FUNC_CALL VAR FOR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER NUMBER FOR VAR VAR VAR VAR VAR VAR BIN_OP VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR RETURN BIN_OP VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER CLASS_DEF VAR FUNC_DEF ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NONE FUNC_DEF RETURN BIN_OP BIN_OP VAR VAR NUMBER VAR FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR VAR VAR RETURN VAR VAR FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR VAR VAR RETURN VAR VAR FUNC_DEF IF VAR VAR RETURN NUMBER IF VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR IF VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR RETURN VAR
We are given a list of (axis-aligned) rectangles.  Each rectangle[i] = [x1, y1, x2, y2] , where (x1, y1) are the coordinates of the bottom-left corner, and (x2, y2) are the coordinates of the top-right corner of the ith rectangle. Find the total area covered by all rectangles in the plane.  Since the answer may be too large, return it modulo 10^9 + 7. Example 1: Input: [[0,0,2,2],[1,0,2,3],[1,0,3,1]] Output: 6 Explanation: As illustrated in the picture. Example 2: Input: [[0,0,1000000000,1000000000]] Output: 49 Explanation: The answer is 10^18 modulo (10^9 + 7), which is (10^9)^2 = (-7)^2 = 49. Note: 1 <= rectangles.length <= 200 rectanges[i].length = 4 0 <= rectangles[i][j] <= 10^9 The total area covered by all rectangles will never exceed 2^63 - 1 and thus will fit in a 64-bit signed integer.
class Solution: def rectangleArea(self, rectangles: List[List[int]]) -> int: def getArea(width): res = 0 prev_low = 0 for low, high in intervals: low = max(prev_low, low) if high > low: res += (high - low) * width prev_low = high return res MOD = 10**9 + 7 events = [] for x1, y1, x2, y2 in rectangles: events.append((x1, 0, y1, y2)) events.append((x2, 1, y1, y2)) events.sort(key=lambda x: (x[0], x[1])) intervals = [] area = 0 prev_x = 0 for event in events: cur_x, type, low, high = event area += getArea(cur_x - prev_x) if type == 1: intervals.remove((low, high)) else: intervals.append((low, high)) intervals.sort() prev_x = cur_x return area % MOD
CLASS_DEF FUNC_DEF VAR VAR VAR FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR ASSIGN VAR VAR RETURN VAR ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER ASSIGN VAR LIST FOR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR NUMBER VAR NUMBER ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR FUNC_CALL VAR BIN_OP VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR VAR RETURN BIN_OP VAR VAR VAR
We are given a list of (axis-aligned) rectangles.  Each rectangle[i] = [x1, y1, x2, y2] , where (x1, y1) are the coordinates of the bottom-left corner, and (x2, y2) are the coordinates of the top-right corner of the ith rectangle. Find the total area covered by all rectangles in the plane.  Since the answer may be too large, return it modulo 10^9 + 7. Example 1: Input: [[0,0,2,2],[1,0,2,3],[1,0,3,1]] Output: 6 Explanation: As illustrated in the picture. Example 2: Input: [[0,0,1000000000,1000000000]] Output: 49 Explanation: The answer is 10^18 modulo (10^9 + 7), which is (10^9)^2 = (-7)^2 = 49. Note: 1 <= rectangles.length <= 200 rectanges[i].length = 4 0 <= rectangles[i][j] <= 10^9 The total area covered by all rectangles will never exceed 2^63 - 1 and thus will fit in a 64-bit signed integer.
class Solution: def rectangleArea(self, rectangles: List[List[int]]) -> int: all_x, all_y = set(), set() for x1, y1, x2, y2 in rectangles: all_x.add(x1) all_x.add(x2) all_y.add(y1) all_y.add(y2) all_x = list(all_x) all_x.sort() all_y = list(all_y) all_y.sort() x_map = {val: i for i, val in enumerate(all_x)} y_map = {val: i for i, val in enumerate(all_y)} area = [[(0) for _ in range(len(y_map))] for _ in range(len(x_map))] for x1, y1, x2, y2 in rectangles: for x in range(x_map[x1], x_map[x2]): for y in range(y_map[y1], y_map[y2]): area[x][y] = 1 ans = 0 for x in range(len(x_map)): for y in range(len(y_map)): if area[x][y]: ans += (all_x[x + 1] - all_x[x]) * (all_y[y + 1] - all_y[y]) if ans > 1000000007: ans %= 1000000007 return ans
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR VAR VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR VAR VAR VAR VAR FOR VAR FUNC_CALL VAR VAR VAR VAR VAR FOR VAR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR IF VAR NUMBER VAR NUMBER RETURN VAR VAR
We are given a list of (axis-aligned) rectangles.  Each rectangle[i] = [x1, y1, x2, y2] , where (x1, y1) are the coordinates of the bottom-left corner, and (x2, y2) are the coordinates of the top-right corner of the ith rectangle. Find the total area covered by all rectangles in the plane.  Since the answer may be too large, return it modulo 10^9 + 7. Example 1: Input: [[0,0,2,2],[1,0,2,3],[1,0,3,1]] Output: 6 Explanation: As illustrated in the picture. Example 2: Input: [[0,0,1000000000,1000000000]] Output: 49 Explanation: The answer is 10^18 modulo (10^9 + 7), which is (10^9)^2 = (-7)^2 = 49. Note: 1 <= rectangles.length <= 200 rectanges[i].length = 4 0 <= rectangles[i][j] <= 10^9 The total area covered by all rectangles will never exceed 2^63 - 1 and thus will fit in a 64-bit signed integer.
class Solution: def rectangleArea(self, rectangles: List[List[int]]) -> int: N = len(rectangles) Xvals, Yvals = set(), set() for x1, y1, x2, y2 in rectangles: Xvals.add(x1) Xvals.add(x2) Yvals.add(y1) Yvals.add(y2) imapx = sorted(Xvals) imapy = sorted(Yvals) mapx = {x: i for i, x in enumerate(imapx)} mapy = {y: i for i, y in enumerate(imapy)} grid = [([0] * len(imapy)) for _ in imapx] for x1, y1, x2, y2 in rectangles: for x in range(mapx[x1], mapx[x2]): for y in range(mapy[y1], mapy[y2]): grid[x][y] = 1 ans = 0 for x, row in enumerate(grid): for y, val in enumerate(row): if val: ans += (imapx[x + 1] - imapx[x]) * (imapy[y + 1] - imapy[y]) return ans % (10**9 + 7)
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR VAR VAR VAR FOR VAR VAR VAR VAR VAR FOR VAR FUNC_CALL VAR VAR VAR VAR VAR FOR VAR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR FOR VAR VAR FUNC_CALL VAR VAR IF VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR RETURN BIN_OP VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER VAR
We are given a list of (axis-aligned) rectangles.  Each rectangle[i] = [x1, y1, x2, y2] , where (x1, y1) are the coordinates of the bottom-left corner, and (x2, y2) are the coordinates of the top-right corner of the ith rectangle. Find the total area covered by all rectangles in the plane.  Since the answer may be too large, return it modulo 10^9 + 7. Example 1: Input: [[0,0,2,2],[1,0,2,3],[1,0,3,1]] Output: 6 Explanation: As illustrated in the picture. Example 2: Input: [[0,0,1000000000,1000000000]] Output: 49 Explanation: The answer is 10^18 modulo (10^9 + 7), which is (10^9)^2 = (-7)^2 = 49. Note: 1 <= rectangles.length <= 200 rectanges[i].length = 4 0 <= rectangles[i][j] <= 10^9 The total area covered by all rectangles will never exceed 2^63 - 1 and thus will fit in a 64-bit signed integer.
class Solution: def rectangleArea(self, rectangles: List[List[int]]) -> int: rects = [] for x1, y1, x2, y2 in rectangles: self.helper(rects, 0, x1, y1, x2, y2) ans = 0 mod = pow(10, 9) + 7 for x1, y1, x2, y2 in rects: ans += (x2 - x1) * (y2 - y1) % mod return ans % mod def helper(self, rects, index, x1, y1, x2, y2): if index == len(rects): rects.append([x1, y1, x2, y2]) return i1, j1, i2, j2 = rects[index] if i1 >= x2 or i2 <= x1 or j1 >= y2 or j2 <= y1: self.helper(rects, index + 1, x1, y1, x2, y2) return if x1 < i1: self.helper(rects, index + 1, x1, y1, min(i1, x2), y2) if x2 > i2: self.helper(rects, index + 1, max(i2, x1), y1, x2, y2) if y1 < j1: self.helper(rects, index + 1, max(x1, i1), y1, min(x2, i2), j1) if y2 > j2: self.helper(rects, index + 1, max(x1, i1), j2, min(x2, i2), y2)
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR LIST FOR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR NUMBER VAR VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR NUMBER NUMBER NUMBER FOR VAR VAR VAR VAR VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR VAR RETURN BIN_OP VAR VAR VAR FUNC_DEF IF VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR LIST VAR VAR VAR VAR RETURN ASSIGN VAR VAR VAR VAR VAR VAR IF VAR VAR VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR VAR VAR RETURN IF VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR FUNC_CALL VAR VAR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR VAR VAR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR VAR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR VAR VAR VAR
We are given a list of (axis-aligned) rectangles.  Each rectangle[i] = [x1, y1, x2, y2] , where (x1, y1) are the coordinates of the bottom-left corner, and (x2, y2) are the coordinates of the top-right corner of the ith rectangle. Find the total area covered by all rectangles in the plane.  Since the answer may be too large, return it modulo 10^9 + 7. Example 1: Input: [[0,0,2,2],[1,0,2,3],[1,0,3,1]] Output: 6 Explanation: As illustrated in the picture. Example 2: Input: [[0,0,1000000000,1000000000]] Output: 49 Explanation: The answer is 10^18 modulo (10^9 + 7), which is (10^9)^2 = (-7)^2 = 49. Note: 1 <= rectangles.length <= 200 rectanges[i].length = 4 0 <= rectangles[i][j] <= 10^9 The total area covered by all rectangles will never exceed 2^63 - 1 and thus will fit in a 64-bit signed integer.
class Solution: def rectangleArea(self, rectangles: List[List[int]]) -> int: xs = sorted(list(set(x for x1, y1, x2, y2 in rectangles for x in [x1, x2]))) xs_i = {x: i for i, x in enumerate(xs)} rects = [] for x1, y1, x2, y2 in rectangles: rects.append((y1, x1, x2, 1)) rects.append((y2, x1, x2, -1)) rects = sorted(rects) counts = [0] * len(xs_i) curr_y = 0 area = 0 L = 0 for y, x1, x2, sig in rects: area += (y - curr_y) * L curr_y = y for x in range(xs_i[x1], xs_i[x2]): counts[x] += sig L = sum(x2 - x1 for x1, x2, s1 in zip(xs, xs[1:], counts) if s1 > 0) return area % (10**9 + 7)
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR VAR LIST VAR VAR ASSIGN VAR VAR VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR LIST FOR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR VAR VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR ASSIGN VAR VAR FOR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR VAR FUNC_CALL VAR VAR VAR NUMBER VAR VAR NUMBER RETURN BIN_OP VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER VAR
We are given a list of (axis-aligned) rectangles.  Each rectangle[i] = [x1, y1, x2, y2] , where (x1, y1) are the coordinates of the bottom-left corner, and (x2, y2) are the coordinates of the top-right corner of the ith rectangle. Find the total area covered by all rectangles in the plane.  Since the answer may be too large, return it modulo 10^9 + 7. Example 1: Input: [[0,0,2,2],[1,0,2,3],[1,0,3,1]] Output: 6 Explanation: As illustrated in the picture. Example 2: Input: [[0,0,1000000000,1000000000]] Output: 49 Explanation: The answer is 10^18 modulo (10^9 + 7), which is (10^9)^2 = (-7)^2 = 49. Note: 1 <= rectangles.length <= 200 rectanges[i].length = 4 0 <= rectangles[i][j] <= 10^9 The total area covered by all rectangles will never exceed 2^63 - 1 and thus will fit in a 64-bit signed integer.
class Tree: def __init__(self, l, r): self.total = 0 self.count = 0 self.l = l self.r = r self.m = int((self.l + self.r) / 2) self.isLeaf = True if self.r - self.l == 1 else False self.left = None if self.isLeaf else Tree(self.l, self.m) self.right = None if self.isLeaf else Tree(self.m, self.r) def update(self, l, r, count): if l >= self.r or r <= self.l: return if self.isLeaf: self.count += count self.total = nums[self.r] - nums[self.l] if self.count else 0 else: self.left.update(l, r, count) self.right.update(l, r, count) self.total = self.left.total + self.right.total class Solution(object): def rectangleArea(self, rectangles): M = 10**9 + 7 events = [] nonlocal nums nums = set() for x1, y1, x2, y2 in rectangles: events.append((x1, y1, y2, 1)) events.append((x2, y1, y2, -1)) nums.add(y1) nums.add(y2) nums = list(nums) nums.sort() nToI = dict([(n, i) for i, n in enumerate(nums)]) iTree = Tree(0, len(nums) - 1) events.sort(key=lambda x: x[0]) res = 0 prev = events[0][0] for event in events: if event[0] != prev: res += iTree.total * (event[0] - prev) res = res % M prev = event[0] iTree.update(nToI[event[1]], nToI[event[2]], event[3]) return res
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR NONE FUNC_CALL VAR VAR VAR ASSIGN VAR VAR NONE FUNC_CALL VAR VAR VAR FUNC_DEF IF VAR VAR VAR VAR RETURN IF VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR CLASS_DEF VAR FUNC_DEF ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER ASSIGN VAR LIST ASSIGN VAR FUNC_CALL VAR FOR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER NUMBER FOR VAR VAR IF VAR NUMBER VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER VAR VAR NUMBER VAR NUMBER RETURN VAR
We are given a list of (axis-aligned) rectangles.  Each rectangle[i] = [x1, y1, x2, y2] , where (x1, y1) are the coordinates of the bottom-left corner, and (x2, y2) are the coordinates of the top-right corner of the ith rectangle. Find the total area covered by all rectangles in the plane.  Since the answer may be too large, return it modulo 10^9 + 7. Example 1: Input: [[0,0,2,2],[1,0,2,3],[1,0,3,1]] Output: 6 Explanation: As illustrated in the picture. Example 2: Input: [[0,0,1000000000,1000000000]] Output: 49 Explanation: The answer is 10^18 modulo (10^9 + 7), which is (10^9)^2 = (-7)^2 = 49. Note: 1 <= rectangles.length <= 200 rectanges[i].length = 4 0 <= rectangles[i][j] <= 10^9 The total area covered by all rectangles will never exceed 2^63 - 1 and thus will fit in a 64-bit signed integer.
class Solution: def rectangleArea(self, rectangles: List[List[int]]) -> int: xTicks = set() yTicks = set() for x1, y1, x2, y2 in rectangles: xTicks.add(x1) xTicks.add(x2) yTicks.add(y1) yTicks.add(y2) xTicksList = sorted(list(xTicks)) yTicksList = sorted(list(yTicks)) xTicksDict = {xLable: xi for xi, xLable in enumerate(xTicksList)} yTicksDict = {yLable: yi for yi, yLable in enumerate(yTicksList)} iMax = len(xTicksList) jMax = len(yTicksList) grid = [[(0) for j in range(jMax)] for i in range(iMax)] ans = 0 for x1, y1, x2, y2 in rectangles: for i in range(xTicksDict[x1], xTicksDict[x2]): xSide = xTicksList[i + 1] - xTicksList[i] for j in range(yTicksDict[y1], yTicksDict[y2]): if grid[i][j] == 0: ans += xSide * (yTicksList[j + 1] - yTicksList[j]) grid[i][j] = 1 return ans % (10**9 + 7)
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FOR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR VAR VAR VAR VAR FOR VAR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR FOR VAR FUNC_CALL VAR VAR VAR VAR VAR IF VAR VAR VAR NUMBER VAR BIN_OP VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR VAR VAR NUMBER RETURN BIN_OP VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER VAR
We are given a list of (axis-aligned) rectangles.  Each rectangle[i] = [x1, y1, x2, y2] , where (x1, y1) are the coordinates of the bottom-left corner, and (x2, y2) are the coordinates of the top-right corner of the ith rectangle. Find the total area covered by all rectangles in the plane.  Since the answer may be too large, return it modulo 10^9 + 7. Example 1: Input: [[0,0,2,2],[1,0,2,3],[1,0,3,1]] Output: 6 Explanation: As illustrated in the picture. Example 2: Input: [[0,0,1000000000,1000000000]] Output: 49 Explanation: The answer is 10^18 modulo (10^9 + 7), which is (10^9)^2 = (-7)^2 = 49. Note: 1 <= rectangles.length <= 200 rectanges[i].length = 4 0 <= rectangles[i][j] <= 10^9 The total area covered by all rectangles will never exceed 2^63 - 1 and thus will fit in a 64-bit signed integer.
class Solution: def rectangleArea(self, rectangles: List[List[int]]) -> int: START = 1 END = 0 MOD = 10**9 + 7 xaxis = [] for x1, y1, x2, y2 in rectangles: xaxis.append((x1, START, y1, y2)) xaxis.append((x2, END, y1, y2)) xaxis.sort() prev = 0 area = 0 yaxis = [] for i in range(len(xaxis)): x, status, y1, y2 = xaxis[i] if i > 0: area += self.get_length(yaxis) * (x - prev) area %= MOD if status == START: yaxis.append((y1, y2)) yaxis.sort() else: yaxis.remove((y1, y2)) prev = x return area def get_length(self, yaxis): length = 0 i = 0 prev = float("-inf"), float("-inf") for i in range(len(yaxis)): if not self.has_overlap(prev, yaxis[i]): length += yaxis[i][1] - yaxis[i][0] else: if prev[1] >= yaxis[i][1]: continue length += yaxis[i][1] - prev[1] prev = yaxis[i] return length def has_overlap(self, prev, cur): if prev[1] < cur[0] or cur[1] < prev[0]: return False return True def get_overlap_length(self, prev, cur): return min(prev[1], cur[1]) - max(prev[0], cur[0])
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER ASSIGN VAR LIST FOR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR IF VAR NUMBER VAR BIN_OP FUNC_CALL VAR VAR BIN_OP VAR VAR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR RETURN VAR VAR FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR STRING FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR VAR VAR VAR BIN_OP VAR VAR NUMBER VAR VAR NUMBER IF VAR NUMBER VAR VAR NUMBER VAR BIN_OP VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR RETURN VAR FUNC_DEF IF VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER RETURN NUMBER RETURN NUMBER FUNC_DEF RETURN BIN_OP FUNC_CALL VAR VAR NUMBER VAR NUMBER FUNC_CALL VAR VAR NUMBER VAR NUMBER
We are given a list of (axis-aligned) rectangles.  Each rectangle[i] = [x1, y1, x2, y2] , where (x1, y1) are the coordinates of the bottom-left corner, and (x2, y2) are the coordinates of the top-right corner of the ith rectangle. Find the total area covered by all rectangles in the plane.  Since the answer may be too large, return it modulo 10^9 + 7. Example 1: Input: [[0,0,2,2],[1,0,2,3],[1,0,3,1]] Output: 6 Explanation: As illustrated in the picture. Example 2: Input: [[0,0,1000000000,1000000000]] Output: 49 Explanation: The answer is 10^18 modulo (10^9 + 7), which is (10^9)^2 = (-7)^2 = 49. Note: 1 <= rectangles.length <= 200 rectanges[i].length = 4 0 <= rectangles[i][j] <= 10^9 The total area covered by all rectangles will never exceed 2^63 - 1 and thus will fit in a 64-bit signed integer.
class Solution(object): def rectangleArea(self, rectangles): OPEN, CLOSE = 0, 1 events = [] for x1, y1, x2, y2 in rectangles: events.append((y1, OPEN, x1, x2)) events.append((y2, CLOSE, x1, x2)) events.sort() def query(): ans = 0 cur = -1 for x1, x2 in active: cur = max(cur, x1) ans += max(0, x2 - cur) cur = max(cur, x2) return ans active = [] cur_y = events[0][0] ans = 0 for y, typ, x1, x2 in events: ans += query() * (y - cur_y) if typ is OPEN: active.append((x1, x2)) active.sort() else: active.remove((x1, x2)) cur_y = y return ans % (10**9 + 7)
CLASS_DEF VAR FUNC_DEF ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR LIST FOR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR NUMBER BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN VAR ASSIGN VAR LIST ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR VAR VAR VAR VAR VAR BIN_OP FUNC_CALL VAR BIN_OP VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR RETURN BIN_OP VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER