description
stringlengths
171
4k
code
stringlengths
94
3.98k
normalized_code
stringlengths
57
4.99k
Given a Binary Search Tree with unique node values and a target value. You have to find the node whose data is equal to target and return the sum of all descendant (of target) node's data which are vertically below the target node. Initially, you are at the root node. Note: If target node is not present in bst then return -1. Example 1: Input: Target = 35 Output: 32 Explanation: Vertically below 35 is 32. Example 2: Input: Target = 25 Output: 52 Explanation: Vertically below 25 is 22, 30 and their sum is 52. Your Task: You don't need to read input or print anything. Your task is to complete the function verticallyDownBST() which takes BST(you are given root node of bst ) and target as input, and return an interger value as the sum of vertically down nodes from target. If target is not present in bst then return -1. Expected Time Complexity: O( n ), n is number of nodes. Expected Auxiliary Space: O(Height of the tree). Constraints: 1 <= n < 10^{6} 1 <= target <= n 1 <= node.data < 10^{6}
class Solution: def findTarget(self, root, target): if root is None: return None if root.data == target: return root if root.data > target: return self.findTarget(root.left, target) return self.findTarget(root.right, target) def traverseVertically(self, root, position): if root is None: return 0 left = self.traverseVertically(root.left, position + 1) right = self.traverseVertically(root.right, position - 1) if position == 0: result = root.data else: result = 0 return result + left + right def verticallyDownBST(self, root, target): result = self.findTarget(root, target) if result is None: return -1 return self.traverseVertically(result, 0) - result.data
CLASS_DEF FUNC_DEF IF VAR NONE RETURN NONE IF VAR VAR RETURN VAR IF VAR VAR RETURN FUNC_CALL VAR VAR VAR RETURN FUNC_CALL VAR VAR VAR FUNC_DEF IF VAR NONE RETURN NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR ASSIGN VAR NUMBER RETURN BIN_OP BIN_OP VAR VAR VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR NONE RETURN NUMBER RETURN BIN_OP FUNC_CALL VAR VAR NUMBER VAR
Given a Binary Search Tree with unique node values and a target value. You have to find the node whose data is equal to target and return the sum of all descendant (of target) node's data which are vertically below the target node. Initially, you are at the root node. Note: If target node is not present in bst then return -1. Example 1: Input: Target = 35 Output: 32 Explanation: Vertically below 35 is 32. Example 2: Input: Target = 25 Output: 52 Explanation: Vertically below 25 is 22, 30 and their sum is 52. Your Task: You don't need to read input or print anything. Your task is to complete the function verticallyDownBST() which takes BST(you are given root node of bst ) and target as input, and return an interger value as the sum of vertically down nodes from target. If target is not present in bst then return -1. Expected Time Complexity: O( n ), n is number of nodes. Expected Auxiliary Space: O(Height of the tree). Constraints: 1 <= n < 10^{6} 1 <= target <= n 1 <= node.data < 10^{6}
class Solution: def verticallyDownBST(self, root, target): def dfs(node, variance): if not node: return 0 if variance == 0: if not node.left and not node.right: return node.data if node.right and node.left: return dfs(node.right, 1) + dfs(node.left, -1) + node.data elif node.left: return dfs(node.left, -1) + node.data else: return dfs(node.right, 1) + node.data else: if not node.left and not node.right: return 0 if node.left and node.right: return dfs(node.left, variance - 1) + dfs(node.right, variance + 1) elif node.left: return dfs(node.left, variance - 1) else: return dfs(node.right, variance + 1) tar_node = root while tar_node and tar_node.data != target: if tar_node.data > target: tar_node = tar_node.left else: tar_node = tar_node.right if not tar_node: return -1 return dfs(tar_node, 0) - tar_node.data
CLASS_DEF FUNC_DEF FUNC_DEF IF VAR RETURN NUMBER IF VAR NUMBER IF VAR VAR RETURN VAR IF VAR VAR RETURN BIN_OP BIN_OP FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER VAR IF VAR RETURN BIN_OP FUNC_CALL VAR VAR NUMBER VAR RETURN BIN_OP FUNC_CALL VAR VAR NUMBER VAR IF VAR VAR RETURN NUMBER IF VAR VAR RETURN BIN_OP FUNC_CALL VAR VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR BIN_OP VAR NUMBER IF VAR RETURN FUNC_CALL VAR VAR BIN_OP VAR NUMBER RETURN FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR WHILE VAR VAR VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR IF VAR RETURN NUMBER RETURN BIN_OP FUNC_CALL VAR VAR NUMBER VAR
Given a Binary Search Tree with unique node values and a target value. You have to find the node whose data is equal to target and return the sum of all descendant (of target) node's data which are vertically below the target node. Initially, you are at the root node. Note: If target node is not present in bst then return -1. Example 1: Input: Target = 35 Output: 32 Explanation: Vertically below 35 is 32. Example 2: Input: Target = 25 Output: 52 Explanation: Vertically below 25 is 22, 30 and their sum is 52. Your Task: You don't need to read input or print anything. Your task is to complete the function verticallyDownBST() which takes BST(you are given root node of bst ) and target as input, and return an interger value as the sum of vertically down nodes from target. If target is not present in bst then return -1. Expected Time Complexity: O( n ), n is number of nodes. Expected Auxiliary Space: O(Height of the tree). Constraints: 1 <= n < 10^{6} 1 <= target <= n 1 <= node.data < 10^{6}
class Solution: def findTarget(self, root, target, roott): if root is None: return self.findTarget(root.left, target, roott) if root.data == target: roott.append(root) self.findTarget(root.right, target, roott) def traverseDown(self, root, level, target): if root is None: return if level == 0 and root.data != target: self.sum += root.data self.traverseDown(root.left, level - 1, target) self.traverseDown(root.right, level + 1, target) def verticallyDownBST(self, root, target): roott = [] level = 0 self.sum = 0 self.findTarget(root, target, roott) if roott: self.traverseDown(roott[0], level, target) return self.sum else: return -1
CLASS_DEF FUNC_DEF IF VAR NONE RETURN EXPR FUNC_CALL VAR VAR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR FUNC_DEF IF VAR NONE RETURN IF VAR NUMBER VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR FUNC_DEF ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR IF VAR EXPR FUNC_CALL VAR VAR NUMBER VAR VAR RETURN VAR RETURN NUMBER
Given a Binary Search Tree with unique node values and a target value. You have to find the node whose data is equal to target and return the sum of all descendant (of target) node's data which are vertically below the target node. Initially, you are at the root node. Note: If target node is not present in bst then return -1. Example 1: Input: Target = 35 Output: 32 Explanation: Vertically below 35 is 32. Example 2: Input: Target = 25 Output: 52 Explanation: Vertically below 25 is 22, 30 and their sum is 52. Your Task: You don't need to read input or print anything. Your task is to complete the function verticallyDownBST() which takes BST(you are given root node of bst ) and target as input, and return an interger value as the sum of vertically down nodes from target. If target is not present in bst then return -1. Expected Time Complexity: O( n ), n is number of nodes. Expected Auxiliary Space: O(Height of the tree). Constraints: 1 <= n < 10^{6} 1 <= target <= n 1 <= node.data < 10^{6}
class Solution: def get_target_node(self, root, target): while not root.data == target: if root.data > target: if root.left is None: return -1 root = root.left else: if root.right is None: return -1 root = root.right return root def get_vertical_sum(self, root, sum, displacement): if displacement == 0: sum += root.data if root.left is not None: sum += self.get_vertical_sum(root.left, 0, displacement - 1) if root.right is not None: sum += self.get_vertical_sum(root.right, 0, displacement + 1) return sum def verticallyDownBST(self, root, target): target_node = self.get_target_node(root, target) if target_node == -1: return -1 return self.get_vertical_sum(target_node, -target, 0)
CLASS_DEF FUNC_DEF WHILE VAR VAR IF VAR VAR IF VAR NONE RETURN NUMBER ASSIGN VAR VAR IF VAR NONE RETURN NUMBER ASSIGN VAR VAR RETURN VAR FUNC_DEF IF VAR NUMBER VAR VAR IF VAR NONE VAR FUNC_CALL VAR VAR NUMBER BIN_OP VAR NUMBER IF VAR NONE VAR FUNC_CALL VAR VAR NUMBER BIN_OP VAR NUMBER RETURN VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR NUMBER RETURN NUMBER RETURN FUNC_CALL VAR VAR VAR NUMBER
Given a Binary Search Tree with unique node values and a target value. You have to find the node whose data is equal to target and return the sum of all descendant (of target) node's data which are vertically below the target node. Initially, you are at the root node. Note: If target node is not present in bst then return -1. Example 1: Input: Target = 35 Output: 32 Explanation: Vertically below 35 is 32. Example 2: Input: Target = 25 Output: 52 Explanation: Vertically below 25 is 22, 30 and their sum is 52. Your Task: You don't need to read input or print anything. Your task is to complete the function verticallyDownBST() which takes BST(you are given root node of bst ) and target as input, and return an interger value as the sum of vertically down nodes from target. If target is not present in bst then return -1. Expected Time Complexity: O( n ), n is number of nodes. Expected Auxiliary Space: O(Height of the tree). Constraints: 1 <= n < 10^{6} 1 <= target <= n 1 <= node.data < 10^{6}
class Solution: def find(self, root, val, f): if root: if f and val == 0: self.ans += root.data if root.data == target: self.f = 1 self.find(root.left, -1, 1) self.find(root.right, 1, 1) else: self.find(root.left, val - 1, f) self.find(root.right, val + 1, f) def verticallyDownBST(self, root, target): self.ans = 0 self.f = 0 self.find(root, 0, 0) if not self.f: return -1 return self.ans
CLASS_DEF FUNC_DEF IF VAR IF VAR VAR NUMBER VAR VAR IF VAR VAR ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER NUMBER IF VAR RETURN NUMBER RETURN VAR
Given a Binary Search Tree with unique node values and a target value. You have to find the node whose data is equal to target and return the sum of all descendant (of target) node's data which are vertically below the target node. Initially, you are at the root node. Note: If target node is not present in bst then return -1. Example 1: Input: Target = 35 Output: 32 Explanation: Vertically below 35 is 32. Example 2: Input: Target = 25 Output: 52 Explanation: Vertically below 25 is 22, 30 and their sum is 52. Your Task: You don't need to read input or print anything. Your task is to complete the function verticallyDownBST() which takes BST(you are given root node of bst ) and target as input, and return an interger value as the sum of vertically down nodes from target. If target is not present in bst then return -1. Expected Time Complexity: O( n ), n is number of nodes. Expected Auxiliary Space: O(Height of the tree). Constraints: 1 <= n < 10^{6} 1 <= target <= n 1 <= node.data < 10^{6}
class Solution: def search(self, root, target): if root == None: return None if root.data == target: return root if root.data < target: return self.search(root.right, target) if root.data > target: return self.search(root.left, target) def descendant(self, root, pos): if root != None: if pos == 0: self.sum += root.data self.descendant(root.left, pos - 1) self.descendant(root.right, pos + 1) def verticallyDownBST(self, root, target): k = self.search(root, target) if k == None: return -1 left, right, sum, flag = k.left, k.right, 0, True self.sum = -1 * k.data self.descendant(k, 0) return self.sum
CLASS_DEF FUNC_DEF IF VAR NONE RETURN NONE IF VAR VAR RETURN VAR IF VAR VAR RETURN FUNC_CALL VAR VAR VAR IF VAR VAR RETURN FUNC_CALL VAR VAR VAR FUNC_DEF IF VAR NONE IF VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR NONE RETURN NUMBER ASSIGN VAR VAR VAR VAR VAR VAR NUMBER NUMBER ASSIGN VAR BIN_OP NUMBER VAR EXPR FUNC_CALL VAR VAR NUMBER RETURN VAR
Given a Binary Search Tree with unique node values and a target value. You have to find the node whose data is equal to target and return the sum of all descendant (of target) node's data which are vertically below the target node. Initially, you are at the root node. Note: If target node is not present in bst then return -1. Example 1: Input: Target = 35 Output: 32 Explanation: Vertically below 35 is 32. Example 2: Input: Target = 25 Output: 52 Explanation: Vertically below 25 is 22, 30 and their sum is 52. Your Task: You don't need to read input or print anything. Your task is to complete the function verticallyDownBST() which takes BST(you are given root node of bst ) and target as input, and return an interger value as the sum of vertically down nodes from target. If target is not present in bst then return -1. Expected Time Complexity: O( n ), n is number of nodes. Expected Auxiliary Space: O(Height of the tree). Constraints: 1 <= n < 10^{6} 1 <= target <= n 1 <= node.data < 10^{6}
class Solution: def verticallyDownBST(self, root, target): def helper(root, x): ans = 0 if root is None: return 0 if x == 0: ans = root.data ans += helper(root.left, x - 1) ans += helper(root.right, x + 1) return ans def find(root, target): if root is None: return -1 if root.data == target: return root if root.data > target: return find(root.left, target) return find(root.right, target) node = find(root, target) if node == -1: return -1 return helper(node, 0) - target
CLASS_DEF FUNC_DEF FUNC_DEF ASSIGN VAR NUMBER IF VAR NONE RETURN NUMBER IF VAR NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER RETURN VAR FUNC_DEF IF VAR NONE RETURN NUMBER IF VAR VAR RETURN VAR IF VAR VAR RETURN FUNC_CALL VAR VAR VAR RETURN FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR NUMBER RETURN NUMBER RETURN BIN_OP FUNC_CALL VAR VAR NUMBER VAR
Given a Binary Search Tree with unique node values and a target value. You have to find the node whose data is equal to target and return the sum of all descendant (of target) node's data which are vertically below the target node. Initially, you are at the root node. Note: If target node is not present in bst then return -1. Example 1: Input: Target = 35 Output: 32 Explanation: Vertically below 35 is 32. Example 2: Input: Target = 25 Output: 52 Explanation: Vertically below 25 is 22, 30 and their sum is 52. Your Task: You don't need to read input or print anything. Your task is to complete the function verticallyDownBST() which takes BST(you are given root node of bst ) and target as input, and return an interger value as the sum of vertically down nodes from target. If target is not present in bst then return -1. Expected Time Complexity: O( n ), n is number of nodes. Expected Auxiliary Space: O(Height of the tree). Constraints: 1 <= n < 10^{6} 1 <= target <= n 1 <= node.data < 10^{6}
class Solution: def verticallyDownBST(self, root, target): ans = [-1, -1] su = [0] def bfs(node, l=0, h=0): if node == None: return if ans[0] == l and ans[1] < h: su[0] += node.data bfs(node.left, l - 1, h + 1) bfs(node.right, l + 1, h + 1) return def bfs1(node, l=0, h=0): if node == None: return if node.data == target: ans[0] = l ans[1] = h bfs(node.left, l - 1, h + 1) bfs(node.right, l + 1, h + 1) return else: bfs1(node.left, l - 1, h + 1) bfs1(node.right, l + 1, h + 1) return bfs1(root) if ans[1] != -1: return su[0] return -1
CLASS_DEF FUNC_DEF ASSIGN VAR LIST NUMBER NUMBER ASSIGN VAR LIST NUMBER FUNC_DEF NUMBER NUMBER IF VAR NONE RETURN IF VAR NUMBER VAR VAR NUMBER VAR VAR NUMBER VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER RETURN FUNC_DEF NUMBER NUMBER IF VAR NONE RETURN IF VAR VAR ASSIGN VAR NUMBER VAR ASSIGN VAR NUMBER VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER RETURN EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER RETURN EXPR FUNC_CALL VAR VAR IF VAR NUMBER NUMBER RETURN VAR NUMBER RETURN NUMBER
Given a Binary Search Tree with unique node values and a target value. You have to find the node whose data is equal to target and return the sum of all descendant (of target) node's data which are vertically below the target node. Initially, you are at the root node. Note: If target node is not present in bst then return -1. Example 1: Input: Target = 35 Output: 32 Explanation: Vertically below 35 is 32. Example 2: Input: Target = 25 Output: 52 Explanation: Vertically below 25 is 22, 30 and their sum is 52. Your Task: You don't need to read input or print anything. Your task is to complete the function verticallyDownBST() which takes BST(you are given root node of bst ) and target as input, and return an interger value as the sum of vertically down nodes from target. If target is not present in bst then return -1. Expected Time Complexity: O( n ), n is number of nodes. Expected Auxiliary Space: O(Height of the tree). Constraints: 1 <= n < 10^{6} 1 <= target <= n 1 <= node.data < 10^{6}
class Solution: def findTarget(self, head, target): while head is not None: if head.data == target: return head if head.data < target: head = head.right else: head = head.left return None def rec(self, node, pos): sum = 0 if node is None: return sum if pos == 0: sum = node.data if node.left is not None: sum = sum + self.rec(node.left, pos - 1) if node.right is not None: sum = sum + self.rec(node.right, pos + 1) return sum def verticallyDownBST(self, root, target): pTarget = self.findTarget(root, target) if pTarget is None: return -1 else: return self.rec(pTarget, 0) - target
CLASS_DEF FUNC_DEF WHILE VAR NONE IF VAR VAR RETURN VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR RETURN NONE FUNC_DEF ASSIGN VAR NUMBER IF VAR NONE RETURN VAR IF VAR NUMBER ASSIGN VAR VAR IF VAR NONE ASSIGN VAR BIN_OP VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER IF VAR NONE ASSIGN VAR BIN_OP VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER RETURN VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR NONE RETURN NUMBER RETURN BIN_OP FUNC_CALL VAR VAR NUMBER VAR
Given a Binary Search Tree with unique node values and a target value. You have to find the node whose data is equal to target and return the sum of all descendant (of target) node's data which are vertically below the target node. Initially, you are at the root node. Note: If target node is not present in bst then return -1. Example 1: Input: Target = 35 Output: 32 Explanation: Vertically below 35 is 32. Example 2: Input: Target = 25 Output: 52 Explanation: Vertically below 25 is 22, 30 and their sum is 52. Your Task: You don't need to read input or print anything. Your task is to complete the function verticallyDownBST() which takes BST(you are given root node of bst ) and target as input, and return an interger value as the sum of vertically down nodes from target. If target is not present in bst then return -1. Expected Time Complexity: O( n ), n is number of nodes. Expected Auxiliary Space: O(Height of the tree). Constraints: 1 <= n < 10^{6} 1 <= target <= n 1 <= node.data < 10^{6}
class Solution: def verticallyDownBST(self, root, target): queue = [root] v = False l = {} k = 0 while len(queue) != 0: x = queue.pop(0) if x == None: continue elif x.data == target: queue = [] l[x] = 0 v = True if v == True: if x.left: l[x.left] = l[x] - 1 if x.right: l[x.right] = l[x] + 1 if x.data == target: pass elif l[x] == 0: k += x.data queue.append(x.left) queue.append(x.right) if v == False: return -1 return k
CLASS_DEF FUNC_DEF ASSIGN VAR LIST VAR ASSIGN VAR NUMBER ASSIGN VAR DICT ASSIGN VAR NUMBER WHILE FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR NUMBER IF VAR NONE IF VAR VAR ASSIGN VAR LIST ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER IF VAR ASSIGN VAR VAR BIN_OP VAR VAR NUMBER IF VAR ASSIGN VAR VAR BIN_OP VAR VAR NUMBER IF VAR VAR IF VAR VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR IF VAR NUMBER RETURN NUMBER RETURN VAR
Given a Binary Search Tree with unique node values and a target value. You have to find the node whose data is equal to target and return the sum of all descendant (of target) node's data which are vertically below the target node. Initially, you are at the root node. Note: If target node is not present in bst then return -1. Example 1: Input: Target = 35 Output: 32 Explanation: Vertically below 35 is 32. Example 2: Input: Target = 25 Output: 52 Explanation: Vertically below 25 is 22, 30 and their sum is 52. Your Task: You don't need to read input or print anything. Your task is to complete the function verticallyDownBST() which takes BST(you are given root node of bst ) and target as input, and return an interger value as the sum of vertically down nodes from target. If target is not present in bst then return -1. Expected Time Complexity: O( n ), n is number of nodes. Expected Auxiliary Space: O(Height of the tree). Constraints: 1 <= n < 10^{6} 1 <= target <= n 1 <= node.data < 10^{6}
class Solution: def verticallyDownBST(self, root, target): if root is None: return -1 if target < root.data: return self.verticallyDownBST(root.left, target) if target > root.data: return self.verticallyDownBST(root.right, target) return self.helper(root, 0) - root.data def helper(self, root, width): if root is None: return 0 output = 0 if width == 0: output += root.data output += self.helper(root.left, width - 1) output += self.helper(root.right, width + 1) return output
CLASS_DEF FUNC_DEF IF VAR NONE RETURN NUMBER IF VAR VAR RETURN FUNC_CALL VAR VAR VAR IF VAR VAR RETURN FUNC_CALL VAR VAR VAR RETURN BIN_OP FUNC_CALL VAR VAR NUMBER VAR FUNC_DEF IF VAR NONE RETURN NUMBER ASSIGN VAR NUMBER IF VAR NUMBER VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER RETURN VAR
Given a Binary Search Tree with unique node values and a target value. You have to find the node whose data is equal to target and return the sum of all descendant (of target) node's data which are vertically below the target node. Initially, you are at the root node. Note: If target node is not present in bst then return -1. Example 1: Input: Target = 35 Output: 32 Explanation: Vertically below 35 is 32. Example 2: Input: Target = 25 Output: 52 Explanation: Vertically below 25 is 22, 30 and their sum is 52. Your Task: You don't need to read input or print anything. Your task is to complete the function verticallyDownBST() which takes BST(you are given root node of bst ) and target as input, and return an interger value as the sum of vertically down nodes from target. If target is not present in bst then return -1. Expected Time Complexity: O( n ), n is number of nodes. Expected Auxiliary Space: O(Height of the tree). Constraints: 1 <= n < 10^{6} 1 <= target <= n 1 <= node.data < 10^{6}
class Solution: def verticallyDownBST(self, root, target): x = [] Solution.target(root, target, x) if len(x) == 0: return -1 sum = [] Solution.bst(x[0], 0, sum) s = 0 for i in sum: s += i return s - x[0].data def bst(root, st, sum): if root == None: return if st == 0: sum.append(root.data) Solution.bst(root.left, st - 1, sum) Solution.bst(root.right, st + 1, sum) def target(root, target, x): if root.data == target: x.append(root) return if root.left != None: Solution.target(root.left, target, x) if root.right != None: Solution.target(root.right, target, x)
CLASS_DEF FUNC_DEF ASSIGN VAR LIST EXPR FUNC_CALL VAR VAR VAR VAR IF FUNC_CALL VAR VAR NUMBER RETURN NUMBER ASSIGN VAR LIST EXPR FUNC_CALL VAR VAR NUMBER NUMBER VAR ASSIGN VAR NUMBER FOR VAR VAR VAR VAR RETURN BIN_OP VAR VAR NUMBER FUNC_DEF IF VAR NONE RETURN IF VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR FUNC_DEF IF VAR VAR EXPR FUNC_CALL VAR VAR RETURN IF VAR NONE EXPR FUNC_CALL VAR VAR VAR VAR IF VAR NONE EXPR FUNC_CALL VAR VAR VAR VAR
Read problems statements in [Hindi], [Mandarin Chinese], [Russian], [Vietnamese], and [Bengali] as well. Chef has a machine which he uses to rotate sequences. If he puts a sequence $S_{1}, S_{2}, \ldots, S_{N}$ into the machine, it produces the sequence $S_{2}, S_{3}, \ldots, S_{N}, S_{1}$, i.e. the first element of the sequence is moved to the end. Chef is definitely not a newbie ― he knows about trivial things like finding the maximum sum of a contiguous subsequence. Therefore, he now made a difficult problem to challenge himself: You are given a sequence $A_{1}, A_{2}, \ldots, A_{N}$. For each $k$ ($0 ≀ k ≀ N-1$), consider the sequence produced by inserting it into the machine repeatedly $k$ times (i.e. inserting $A$ into the machine and replacing $A$ by the sequence it produces, $k$ times); find the maximum sum of a non empty contiguous subsequence of this sequence. However, to solve this problem, Chef needs the help of a pro. Solve it for him. ------ 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 a single 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 $N$ space-separated integers. For each valid $i$, the $i$-th of these integers should denote the largest sum of a non empty contiguous subsequence of the sequence produced by the machine after $i-1$ repeated insertions. ------ Constraints ------ $1 ≀ T ≀ 100$ $1 ≀ N ≀ 5 \cdot 10^{5}$ $|A_{i}| ≀ 10^{9}$ for each valid $i$ the sum of $N$ over all test cases does not exceed $5 \cdot 10^{5}$ ----- Sample Input 1 ------ 1 4 -5 4 1 2 ----- Sample Output 1 ------ 7 7 4 5 ----- explanation 1 ------ Example case 1: - After zero insertions, $A = [-5, 4, 1, 2]$. The contiguous subsequence with the maximum sum is $A = [4, 1, 2]$. - After one insertion, $A = [4, 1, 2, -5]$. The contiguous subsequence with the maximum sum is $A = [4, 1, 2]$. - After two insertions, $A = [1, 2, -5, 4]$. The contiguous subsequence with the maximum sum is $A = [4]$. - After three insertions, $A = [2, -5, 4, 1]$. The contiguous subsequence with the maximum sum is $A = [4, 1]$.
t = int(input()) for _ in range(t): n = int(input()) lst = list(map(int, input().split())) lst.insert(0, 0) max_pre = [(0) for i in range(n + 2)] max_suf = [(0) for i in range(n + 2)] pre = [(0) for i in range(n + 2)] suf = [(0) for i in range(n + 2)] max_pre[0], max_suf[n + 1], pre[0], suf[n + 1] = ( -float("inf"), -float("inf"), -float("inf"), -float("inf"), ) total = 0 max_till_here = -float("inf") for i in range(1, n + 1): total += lst[i] max_till_here = max(max_till_here + lst[i], lst[i]) max_pre[i] = max(max_pre[i - 1], max_till_here) pre[i] = max(pre[i - 1], total) total = 0 max_till_here = -float("inf") for i in range(n, 0, -1): total += lst[i] max_till_here = max(max_till_here + lst[i], lst[i]) max_suf[i] = max(max_suf[i + 1], max_till_here) suf[i] = max(suf[i + 1], total) for i in range(0, n): f = max_suf[i + 1] s = max_pre[i] t = pre[i] + suf[i + 1] print(max(f, s, t), end=" ") print()
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 EXPR FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER VAR BIN_OP VAR NUMBER VAR NUMBER VAR BIN_OP VAR NUMBER FUNC_CALL VAR STRING FUNC_CALL VAR STRING FUNC_CALL VAR STRING FUNC_CALL VAR STRING ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER VAR VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR VAR NUMBER NUMBER VAR VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR STRING EXPR FUNC_CALL VAR
Read problems statements in [Hindi], [Mandarin Chinese], [Russian], [Vietnamese], and [Bengali] as well. Chef has a machine which he uses to rotate sequences. If he puts a sequence $S_{1}, S_{2}, \ldots, S_{N}$ into the machine, it produces the sequence $S_{2}, S_{3}, \ldots, S_{N}, S_{1}$, i.e. the first element of the sequence is moved to the end. Chef is definitely not a newbie ― he knows about trivial things like finding the maximum sum of a contiguous subsequence. Therefore, he now made a difficult problem to challenge himself: You are given a sequence $A_{1}, A_{2}, \ldots, A_{N}$. For each $k$ ($0 ≀ k ≀ N-1$), consider the sequence produced by inserting it into the machine repeatedly $k$ times (i.e. inserting $A$ into the machine and replacing $A$ by the sequence it produces, $k$ times); find the maximum sum of a non empty contiguous subsequence of this sequence. However, to solve this problem, Chef needs the help of a pro. Solve it for him. ------ 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 a single 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 $N$ space-separated integers. For each valid $i$, the $i$-th of these integers should denote the largest sum of a non empty contiguous subsequence of the sequence produced by the machine after $i-1$ repeated insertions. ------ Constraints ------ $1 ≀ T ≀ 100$ $1 ≀ N ≀ 5 \cdot 10^{5}$ $|A_{i}| ≀ 10^{9}$ for each valid $i$ the sum of $N$ over all test cases does not exceed $5 \cdot 10^{5}$ ----- Sample Input 1 ------ 1 4 -5 4 1 2 ----- Sample Output 1 ------ 7 7 4 5 ----- explanation 1 ------ Example case 1: - After zero insertions, $A = [-5, 4, 1, 2]$. The contiguous subsequence with the maximum sum is $A = [4, 1, 2]$. - After one insertion, $A = [4, 1, 2, -5]$. The contiguous subsequence with the maximum sum is $A = [4, 1, 2]$. - After two insertions, $A = [1, 2, -5, 4]$. The contiguous subsequence with the maximum sum is $A = [4]$. - After three insertions, $A = [2, -5, 4, 1]$. The contiguous subsequence with the maximum sum is $A = [4, 1]$.
t = int(input()) while t: t -= 1 n = int(input()) a = [int(i) for i in input().split()] before = [a[0]] ending = [a[0]] maxi = 0 curr = 0 for i in range(1, len(a)): ending.append(max(a[i], ending[i - 1] + a[i])) maxi = a[0] for i in range(1, len(a)): before.append(max(ending[i], maxi)) if before[i] > maxi: maxi = before[i] ending1 = [a[0]] b = a[:] b.reverse() after = [b[0]] starting = [b[0]] maxi = 0 curr = 0 for i in range(1, len(b)): starting.append(max(b[i], starting[i - 1] + b[i])) maxi = b[0] for i in range(1, len(b)): after.append(max(starting[i], maxi)) if after[i] > maxi: maxi = after[i] pre = [a[0]] ma = a[0] for i in range(1, len(a)): ma += a[i] pre.append(max(ma, pre[i - 1])) suff = [b[0]] ma = b[0] for i in range(1, len(a)): ma += b[i] suff.append(max(suff[i - 1], ma)) starting.reverse() after.reverse() ans = [after[0]] suff.reverse() for i in range(1, len(a)): ans.append(max(after[i], max(before[i - 1], pre[i - 1] + suff[i]))) print(*ans)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST VAR NUMBER ASSIGN VAR LIST VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR IF VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR LIST VAR NUMBER ASSIGN VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR LIST VAR NUMBER ASSIGN VAR LIST VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR IF VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR LIST VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR LIST VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR LIST VAR NUMBER EXPR FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER 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. Chef has a machine which he uses to rotate sequences. If he puts a sequence $S_{1}, S_{2}, \ldots, S_{N}$ into the machine, it produces the sequence $S_{2}, S_{3}, \ldots, S_{N}, S_{1}$, i.e. the first element of the sequence is moved to the end. Chef is definitely not a newbie ― he knows about trivial things like finding the maximum sum of a contiguous subsequence. Therefore, he now made a difficult problem to challenge himself: You are given a sequence $A_{1}, A_{2}, \ldots, A_{N}$. For each $k$ ($0 ≀ k ≀ N-1$), consider the sequence produced by inserting it into the machine repeatedly $k$ times (i.e. inserting $A$ into the machine and replacing $A$ by the sequence it produces, $k$ times); find the maximum sum of a non empty contiguous subsequence of this sequence. However, to solve this problem, Chef needs the help of a pro. Solve it for him. ------ 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 a single 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 $N$ space-separated integers. For each valid $i$, the $i$-th of these integers should denote the largest sum of a non empty contiguous subsequence of the sequence produced by the machine after $i-1$ repeated insertions. ------ Constraints ------ $1 ≀ T ≀ 100$ $1 ≀ N ≀ 5 \cdot 10^{5}$ $|A_{i}| ≀ 10^{9}$ for each valid $i$ the sum of $N$ over all test cases does not exceed $5 \cdot 10^{5}$ ----- Sample Input 1 ------ 1 4 -5 4 1 2 ----- Sample Output 1 ------ 7 7 4 5 ----- explanation 1 ------ Example case 1: - After zero insertions, $A = [-5, 4, 1, 2]$. The contiguous subsequence with the maximum sum is $A = [4, 1, 2]$. - After one insertion, $A = [4, 1, 2, -5]$. The contiguous subsequence with the maximum sum is $A = [4, 1, 2]$. - After two insertions, $A = [1, 2, -5, 4]$. The contiguous subsequence with the maximum sum is $A = [4]$. - After three insertions, $A = [2, -5, 4, 1]$. The contiguous subsequence with the maximum sum is $A = [4, 1]$.
for _ in range(int(input())): n = int(input()) arr = list(map(int, input().split())) b1 = [0] * (n + 1) b2 = [0] * (n + 1) c1 = [0] * (n + 1) c2 = [0] * (n + 1) arr1 = arr[::-1] b1[0] = -float("inf") b2[0] = -float("inf") c1[0] = -float("inf") c2[0] = -float("inf") s = 0 msp = 0 for i in range(n): s += arr[i] b1[i + 1] = max(b1[i], s - msp) c1[i + 1] = max(c1[i], s) msp = min(msp, s) s = 0 msp = 0 for i in range(n): s += arr1[i] b2[i + 1] = max(b2[i], s - msp) c2[i + 1] = max(c2[i], s) msp = min(msp, s) for i in range(n): print(max(b1[i], b2[n - i], c1[i] + c2[n - i]), end=" ") print()
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 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 ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER FUNC_CALL VAR STRING ASSIGN VAR NUMBER FUNC_CALL VAR STRING ASSIGN VAR NUMBER FUNC_CALL VAR STRING ASSIGN VAR NUMBER FUNC_CALL VAR STRING ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR STRING EXPR FUNC_CALL VAR
Read problems statements in [Hindi], [Mandarin Chinese], [Russian], [Vietnamese], and [Bengali] as well. Chef has a machine which he uses to rotate sequences. If he puts a sequence $S_{1}, S_{2}, \ldots, S_{N}$ into the machine, it produces the sequence $S_{2}, S_{3}, \ldots, S_{N}, S_{1}$, i.e. the first element of the sequence is moved to the end. Chef is definitely not a newbie ― he knows about trivial things like finding the maximum sum of a contiguous subsequence. Therefore, he now made a difficult problem to challenge himself: You are given a sequence $A_{1}, A_{2}, \ldots, A_{N}$. For each $k$ ($0 ≀ k ≀ N-1$), consider the sequence produced by inserting it into the machine repeatedly $k$ times (i.e. inserting $A$ into the machine and replacing $A$ by the sequence it produces, $k$ times); find the maximum sum of a non empty contiguous subsequence of this sequence. However, to solve this problem, Chef needs the help of a pro. Solve it for him. ------ 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 a single 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 $N$ space-separated integers. For each valid $i$, the $i$-th of these integers should denote the largest sum of a non empty contiguous subsequence of the sequence produced by the machine after $i-1$ repeated insertions. ------ Constraints ------ $1 ≀ T ≀ 100$ $1 ≀ N ≀ 5 \cdot 10^{5}$ $|A_{i}| ≀ 10^{9}$ for each valid $i$ the sum of $N$ over all test cases does not exceed $5 \cdot 10^{5}$ ----- Sample Input 1 ------ 1 4 -5 4 1 2 ----- Sample Output 1 ------ 7 7 4 5 ----- explanation 1 ------ Example case 1: - After zero insertions, $A = [-5, 4, 1, 2]$. The contiguous subsequence with the maximum sum is $A = [4, 1, 2]$. - After one insertion, $A = [4, 1, 2, -5]$. The contiguous subsequence with the maximum sum is $A = [4, 1, 2]$. - After two insertions, $A = [1, 2, -5, 4]$. The contiguous subsequence with the maximum sum is $A = [4]$. - After three insertions, $A = [2, -5, 4, 1]$. The contiguous subsequence with the maximum sum is $A = [4, 1]$.
ts = int(input()) while ts > 0: n = int(input()) a = list(map(int, input().split())) pre = [0] * n for i in range(n): if i == 0: pre[i] = a[i] else: pre[i] = max(pre[i - 1] + a[i], a[i]) suf = [0] * n for i in range(n - 1, -1, -1): if i == n - 1: suf[i] = a[i] else: suf[i] = max(suf[i + 1] + a[i], a[i]) mpre = [0] * n msuf = [0] * n mpre[0] = pre[0] for i in range(1, n): mpre[i] = max(mpre[i - 1], pre[i]) msuf[n - 1] = suf[n - 1] for i in range(n - 2, -1, -1): msuf[i] = max(msuf[i + 1], suf[i]) sum = 0 for i in range(n): sum += a[i] if i == 0: pre[i] = sum else: pre[i] = max(pre[i - 1], sum) sum = 0 for i in range(n - 1, -1, -1): sum += a[i] if i == n - 1: suf[i] = sum else: suf[i] = max(suf[i + 1], sum) for i in range(n): ans = -1 * 10**10 ans = max(ans, msuf[i]) if i - 1 >= 0: ans = max(ans, mpre[i - 1]) ans = max(ans, suf[i] + pre[i - 1]) print(ans, end=" ") ts -= 1
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR VAR IF VAR NUMBER ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR VAR IF VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER VAR VAR VAR IF VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP NUMBER BIN_OP NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR STRING VAR NUMBER
Read problems statements in [Hindi], [Mandarin Chinese], [Russian], [Vietnamese], and [Bengali] as well. Chef has a machine which he uses to rotate sequences. If he puts a sequence $S_{1}, S_{2}, \ldots, S_{N}$ into the machine, it produces the sequence $S_{2}, S_{3}, \ldots, S_{N}, S_{1}$, i.e. the first element of the sequence is moved to the end. Chef is definitely not a newbie ― he knows about trivial things like finding the maximum sum of a contiguous subsequence. Therefore, he now made a difficult problem to challenge himself: You are given a sequence $A_{1}, A_{2}, \ldots, A_{N}$. For each $k$ ($0 ≀ k ≀ N-1$), consider the sequence produced by inserting it into the machine repeatedly $k$ times (i.e. inserting $A$ into the machine and replacing $A$ by the sequence it produces, $k$ times); find the maximum sum of a non empty contiguous subsequence of this sequence. However, to solve this problem, Chef needs the help of a pro. Solve it for him. ------ 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 a single 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 $N$ space-separated integers. For each valid $i$, the $i$-th of these integers should denote the largest sum of a non empty contiguous subsequence of the sequence produced by the machine after $i-1$ repeated insertions. ------ Constraints ------ $1 ≀ T ≀ 100$ $1 ≀ N ≀ 5 \cdot 10^{5}$ $|A_{i}| ≀ 10^{9}$ for each valid $i$ the sum of $N$ over all test cases does not exceed $5 \cdot 10^{5}$ ----- Sample Input 1 ------ 1 4 -5 4 1 2 ----- Sample Output 1 ------ 7 7 4 5 ----- explanation 1 ------ Example case 1: - After zero insertions, $A = [-5, 4, 1, 2]$. The contiguous subsequence with the maximum sum is $A = [4, 1, 2]$. - After one insertion, $A = [4, 1, 2, -5]$. The contiguous subsequence with the maximum sum is $A = [4, 1, 2]$. - After two insertions, $A = [1, 2, -5, 4]$. The contiguous subsequence with the maximum sum is $A = [4]$. - After three insertions, $A = [2, -5, 4, 1]$. The contiguous subsequence with the maximum sum is $A = [4, 1]$.
def solve(arr, b, c, n): b[0] = c[0] = -float("inf") mps = 0 s = 0 for i in range(n): s += arr[i] b[i + 1] = max(b[i], s - mps) c[i + 1] = max(c[i], s) mps = min(mps, s) return b, c for tc in range(int(input())): n = int(input()) arr = list(map(int, input().split())) b1, c1 = solve(arr, [0] * (n + 1), [0] * (n + 1), n) arr.reverse() b2, c2 = solve(arr, [0] * (n + 1), [0] * (n + 1), n) for i in range(n): ans = max(b1[i], b2[n - i], c1[i] + c2[n - i]) print(ans, end=" ")
FUNC_DEF ASSIGN VAR NUMBER VAR NUMBER FUNC_CALL VAR STRING ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN 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 VAR FUNC_CALL VAR VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER BIN_OP LIST NUMBER BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER BIN_OP LIST NUMBER BIN_OP VAR NUMBER VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR STRING
Read problems statements in [Hindi], [Mandarin Chinese], [Russian], [Vietnamese], and [Bengali] as well. Chef has a machine which he uses to rotate sequences. If he puts a sequence $S_{1}, S_{2}, \ldots, S_{N}$ into the machine, it produces the sequence $S_{2}, S_{3}, \ldots, S_{N}, S_{1}$, i.e. the first element of the sequence is moved to the end. Chef is definitely not a newbie ― he knows about trivial things like finding the maximum sum of a contiguous subsequence. Therefore, he now made a difficult problem to challenge himself: You are given a sequence $A_{1}, A_{2}, \ldots, A_{N}$. For each $k$ ($0 ≀ k ≀ N-1$), consider the sequence produced by inserting it into the machine repeatedly $k$ times (i.e. inserting $A$ into the machine and replacing $A$ by the sequence it produces, $k$ times); find the maximum sum of a non empty contiguous subsequence of this sequence. However, to solve this problem, Chef needs the help of a pro. Solve it for him. ------ 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 a single 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 $N$ space-separated integers. For each valid $i$, the $i$-th of these integers should denote the largest sum of a non empty contiguous subsequence of the sequence produced by the machine after $i-1$ repeated insertions. ------ Constraints ------ $1 ≀ T ≀ 100$ $1 ≀ N ≀ 5 \cdot 10^{5}$ $|A_{i}| ≀ 10^{9}$ for each valid $i$ the sum of $N$ over all test cases does not exceed $5 \cdot 10^{5}$ ----- Sample Input 1 ------ 1 4 -5 4 1 2 ----- Sample Output 1 ------ 7 7 4 5 ----- explanation 1 ------ Example case 1: - After zero insertions, $A = [-5, 4, 1, 2]$. The contiguous subsequence with the maximum sum is $A = [4, 1, 2]$. - After one insertion, $A = [4, 1, 2, -5]$. The contiguous subsequence with the maximum sum is $A = [4, 1, 2]$. - After two insertions, $A = [1, 2, -5, 4]$. The contiguous subsequence with the maximum sum is $A = [4]$. - After three insertions, $A = [2, -5, 4, 1]$. The contiguous subsequence with the maximum sum is $A = [4, 1]$.
for T in range(int(input())): N = int(input()) A = list(map(int, input().split())) max1 = [None] * N max2 = [None] * N suffix = [None] * N prefix = [None] * N curr_max = A[N - 1] final_max = A[N - 1] max1[N - 1] = A[N - 1] for i in range(N - 2, -1, -1): curr_max = max(A[i], curr_max + A[i]) final_max = max(final_max, curr_max) max1[i] = final_max curr_max = A[0] final_max = A[0] max2[1] = A[0] for i in range(1, N - 1): curr_max = max(A[i], curr_max + A[i]) final_max = max(final_max, curr_max) max2[i + 1] = final_max curr_max = A[N - 1] final_max = A[N - 1] suffix[N - 1] = A[N - 1] for i in range(N - 2, -1, -1): curr_max += A[i] final_max = max(final_max, curr_max) suffix[i] = final_max curr_max = A[0] final_max = A[0] prefix[1] = A[0] for i in range(1, N - 1): curr_max += A[i] final_max = max(final_max, curr_max) prefix[i + 1] = final_max print(max1[0], end=" ") for i in range(1, N): fix = max(suffix[i], prefix[i]) if suffix[i] > 0 and prefix[i] > 0: fix = suffix[i] + prefix[i] print(max(max1[i], max2[i], fix), end=" ") print()
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 BIN_OP LIST NONE VAR ASSIGN VAR BIN_OP LIST NONE VAR ASSIGN VAR BIN_OP LIST NONE VAR ASSIGN VAR BIN_OP LIST NONE VAR ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR VAR NUMBER STRING FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR IF VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR VAR STRING EXPR FUNC_CALL VAR
Read problems statements in [Hindi], [Mandarin Chinese], [Russian], [Vietnamese], and [Bengali] as well. Chef has a machine which he uses to rotate sequences. If he puts a sequence $S_{1}, S_{2}, \ldots, S_{N}$ into the machine, it produces the sequence $S_{2}, S_{3}, \ldots, S_{N}, S_{1}$, i.e. the first element of the sequence is moved to the end. Chef is definitely not a newbie ― he knows about trivial things like finding the maximum sum of a contiguous subsequence. Therefore, he now made a difficult problem to challenge himself: You are given a sequence $A_{1}, A_{2}, \ldots, A_{N}$. For each $k$ ($0 ≀ k ≀ N-1$), consider the sequence produced by inserting it into the machine repeatedly $k$ times (i.e. inserting $A$ into the machine and replacing $A$ by the sequence it produces, $k$ times); find the maximum sum of a non empty contiguous subsequence of this sequence. However, to solve this problem, Chef needs the help of a pro. Solve it for him. ------ 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 a single 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 $N$ space-separated integers. For each valid $i$, the $i$-th of these integers should denote the largest sum of a non empty contiguous subsequence of the sequence produced by the machine after $i-1$ repeated insertions. ------ Constraints ------ $1 ≀ T ≀ 100$ $1 ≀ N ≀ 5 \cdot 10^{5}$ $|A_{i}| ≀ 10^{9}$ for each valid $i$ the sum of $N$ over all test cases does not exceed $5 \cdot 10^{5}$ ----- Sample Input 1 ------ 1 4 -5 4 1 2 ----- Sample Output 1 ------ 7 7 4 5 ----- explanation 1 ------ Example case 1: - After zero insertions, $A = [-5, 4, 1, 2]$. The contiguous subsequence with the maximum sum is $A = [4, 1, 2]$. - After one insertion, $A = [4, 1, 2, -5]$. The contiguous subsequence with the maximum sum is $A = [4, 1, 2]$. - After two insertions, $A = [1, 2, -5, 4]$. The contiguous subsequence with the maximum sum is $A = [4]$. - After three insertions, $A = [2, -5, 4, 1]$. The contiguous subsequence with the maximum sum is $A = [4, 1]$.
import sys def jaya(b, c, n, a): m = 0 s = 0 for i in range(n): s += a[i] b.append(max(b[i], s - m)) c.append(max(c[i], s)) m = min(m, s) return b, c for _ in range(int(input())): N = int(input()) A = list(map(int, input().split())) Maxsub = [-sys.maxsize - 1] MaxsuRev = [-sys.maxsize - 1] Maxsumpre = [-sys.maxsize - 1] MaxsumpreRev = [-sys.maxsize - 1] B = A.copy() B = B[::-1] Maxsub, Maxsumpre = jaya(Maxsub, Maxsumpre, N, A) MaxsuRev, MaxsumpreRev = jaya(MaxsuRev, MaxsumpreRev, N, B) for i in range(N): ans = max(MaxsuRev[N - i], Maxsub[i]) ans = max(ans, MaxsumpreRev[N - i] + Maxsumpre[i]) print(ans, end=" ")
IMPORT FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN 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 LIST BIN_OP VAR NUMBER ASSIGN VAR LIST BIN_OP VAR NUMBER ASSIGN VAR LIST BIN_OP VAR NUMBER ASSIGN VAR LIST BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR BIN_OP VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR STRING
Read problem statements in [Russian], [Mandarin Chinese], [Bengali], and [Vietnamese] as well. You are given an array A consisting of N integers and Q queries. Each query is described by two integers L and R. For each query, output the number of tuples (i, j, k) such that L≀ i< j< k≀ R and A_{i}+A_{j}+A_{k} is an even number. ------ Input Format ------ - 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 two integers N and Q. - The next line contains N integers A_{1},\ldots,A_{N}. - Then Q lines follow, each containing two integers L_{i} and R_{i}. ------ Output Format ------ For each query, output the number of tuples possible as mentioned in the problem statement. ------ Constraints ------ $1 ≀ T ≀ 10^{3}$ $1 ≀ N, Q ≀ 10^{5}$ $0 ≀ A_{i} ≀ 10^{6}$ $1 ≀ L_{i} ≀ R_{i} ≀ N$ - The sum of $N$ over all test cases does not exceed $10^{6}$. - The sum of $Q$ over all test cases does not exceed $10^{5}$ ----- Sample Input 1 ------ 1 6 3 1 2 3 4 5 6 1 3 2 5 1 6 ----- Sample Output 1 ------ 1 2 10 ----- explanation 1 ------ - For the first query, we can choose $(1, 2, 3)$ since $A_{1} + A_{2} + A_{3} = 6$ is an even number. - For the second query, we can choose $(2, 3, 5)$ since $A_{2} + A_{3} + A_{5} = 10$ is even, and $(3, 4, 5)$ since $A_{3} + A_{4} + A_{5} = 12$ is even.
def map_int_input(): return map(int, input().split()) def list_int_input(): return [int(x) for x in input().split()] def solve(): n, q = map_int_input() arr = list_int_input() even_num_count = [0] * (n + 1) for i in range(1, n + 1): even_num_count[i] = even_num_count[i - 1] + (1 if arr[i - 1] % 2 == 0 else 0) for _ in range(q): l, r = map_int_input() lr_even_count = even_num_count[r] - even_num_count[l - 1] lr_odd_count = r - l + 1 - lr_even_count first_case = lr_even_count * (lr_even_count - 1) * (lr_even_count - 2) / 6 second_case = lr_odd_count * (lr_odd_count - 1) / 2 * lr_even_count print(int(first_case + second_case)) for _ in range(int(input())): solve()
FUNC_DEF RETURN FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR 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 BIN_OP VAR BIN_OP VAR NUMBER NUMBER NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR ASSIGN VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR
Read problem statements in [Russian], [Mandarin Chinese], [Bengali], and [Vietnamese] as well. You are given an array A consisting of N integers and Q queries. Each query is described by two integers L and R. For each query, output the number of tuples (i, j, k) such that L≀ i< j< k≀ R and A_{i}+A_{j}+A_{k} is an even number. ------ Input Format ------ - 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 two integers N and Q. - The next line contains N integers A_{1},\ldots,A_{N}. - Then Q lines follow, each containing two integers L_{i} and R_{i}. ------ Output Format ------ For each query, output the number of tuples possible as mentioned in the problem statement. ------ Constraints ------ $1 ≀ T ≀ 10^{3}$ $1 ≀ N, Q ≀ 10^{5}$ $0 ≀ A_{i} ≀ 10^{6}$ $1 ≀ L_{i} ≀ R_{i} ≀ N$ - The sum of $N$ over all test cases does not exceed $10^{6}$. - The sum of $Q$ over all test cases does not exceed $10^{5}$ ----- Sample Input 1 ------ 1 6 3 1 2 3 4 5 6 1 3 2 5 1 6 ----- Sample Output 1 ------ 1 2 10 ----- explanation 1 ------ - For the first query, we can choose $(1, 2, 3)$ since $A_{1} + A_{2} + A_{3} = 6$ is an even number. - For the second query, we can choose $(2, 3, 5)$ since $A_{2} + A_{3} + A_{5} = 10$ is even, and $(3, 4, 5)$ since $A_{3} + A_{4} + A_{5} = 12$ is even.
t = int(input()) for i in range(t): n, q = [int(x) for x in input().split()] A = [int(x) for x in input().split()] p = [0] for i in A: if i % 2 != 0: p.append(p[-1] + 1) else: p.append(p[-1]) for i in range(q): l, r = [int(x) for x in input().split()] o = p[r] - p[l - 1] e = r - l + 1 - o if r - l + 1 < 2: print(0) else: print(o * (o - 1) // 2 * e + e * (e - 1) * (e - 2) // 6)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR 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 FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST NUMBER FOR VAR VAR IF BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER VAR IF BIN_OP BIN_OP VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP BIN_OP BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER
Read problem statements in [Russian], [Mandarin Chinese], [Bengali], and [Vietnamese] as well. You are given an array A consisting of N integers and Q queries. Each query is described by two integers L and R. For each query, output the number of tuples (i, j, k) such that L≀ i< j< k≀ R and A_{i}+A_{j}+A_{k} is an even number. ------ Input Format ------ - 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 two integers N and Q. - The next line contains N integers A_{1},\ldots,A_{N}. - Then Q lines follow, each containing two integers L_{i} and R_{i}. ------ Output Format ------ For each query, output the number of tuples possible as mentioned in the problem statement. ------ Constraints ------ $1 ≀ T ≀ 10^{3}$ $1 ≀ N, Q ≀ 10^{5}$ $0 ≀ A_{i} ≀ 10^{6}$ $1 ≀ L_{i} ≀ R_{i} ≀ N$ - The sum of $N$ over all test cases does not exceed $10^{6}$. - The sum of $Q$ over all test cases does not exceed $10^{5}$ ----- Sample Input 1 ------ 1 6 3 1 2 3 4 5 6 1 3 2 5 1 6 ----- Sample Output 1 ------ 1 2 10 ----- explanation 1 ------ - For the first query, we can choose $(1, 2, 3)$ since $A_{1} + A_{2} + A_{3} = 6$ is an even number. - For the second query, we can choose $(2, 3, 5)$ since $A_{2} + A_{3} + A_{5} = 10$ is even, and $(3, 4, 5)$ since $A_{3} + A_{4} + A_{5} = 12$ is even.
for i in range(int(input())): n, q = map(int, input().split()) dp = [0] * (n + 1) arr = list(map(int, input().split())) for i in range(len(arr)): if arr[i] % 2 == 0: dp[i + 1] = 1 + dp[i] else: dp[i + 1] = dp[i] for j in range(q): q1, q2 = list(map(int, input().split())) ev = dp[q2] - dp[q1 - 1] od = q2 - q1 + 1 - ev ev_com = ev * (ev - 1) * (ev - 2) // 6 od_com = od * (od - 1) // 2 * ev print(ev_com + od_com)
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 VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER BIN_OP NUMBER VAR VAR ASSIGN VAR BIN_OP VAR NUMBER VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR
Read problem statements in [Russian], [Mandarin Chinese], [Bengali], and [Vietnamese] as well. You are given an array A consisting of N integers and Q queries. Each query is described by two integers L and R. For each query, output the number of tuples (i, j, k) such that L≀ i< j< k≀ R and A_{i}+A_{j}+A_{k} is an even number. ------ Input Format ------ - 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 two integers N and Q. - The next line contains N integers A_{1},\ldots,A_{N}. - Then Q lines follow, each containing two integers L_{i} and R_{i}. ------ Output Format ------ For each query, output the number of tuples possible as mentioned in the problem statement. ------ Constraints ------ $1 ≀ T ≀ 10^{3}$ $1 ≀ N, Q ≀ 10^{5}$ $0 ≀ A_{i} ≀ 10^{6}$ $1 ≀ L_{i} ≀ R_{i} ≀ N$ - The sum of $N$ over all test cases does not exceed $10^{6}$. - The sum of $Q$ over all test cases does not exceed $10^{5}$ ----- Sample Input 1 ------ 1 6 3 1 2 3 4 5 6 1 3 2 5 1 6 ----- Sample Output 1 ------ 1 2 10 ----- explanation 1 ------ - For the first query, we can choose $(1, 2, 3)$ since $A_{1} + A_{2} + A_{3} = 6$ is an even number. - For the second query, we can choose $(2, 3, 5)$ since $A_{2} + A_{3} + A_{5} = 10$ is even, and $(3, 4, 5)$ since $A_{3} + A_{4} + A_{5} = 12$ is even.
for _ in range(int(input())): n, q = map(int, input().split()) x = list(map(int, input().split())) if x[0] % 2 == 0: xd = [1] else: xd = [0] for i in range(1, n): if x[i] % 2 == 0: xd += [1] else: xd += [0] xd[i] += xd[i - 1] for _ in range(q): l, r = map(int, input().split()) if r - l < 2: print(0) else: e = xd[r - 1] - xd[l - 1] if x[l - 1] % 2 == 0: e += 1 o = r - l + 1 - e ans = 0 if o >= 2 and e >= 1: ans += o * (o - 1) // 2 * e if e >= 3: ans += e * (e - 1) * (e - 2) // 6 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 FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR LIST NUMBER ASSIGN VAR LIST NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF BIN_OP VAR VAR NUMBER NUMBER VAR LIST NUMBER VAR LIST NUMBER VAR VAR VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER IF BIN_OP VAR BIN_OP VAR NUMBER NUMBER NUMBER VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER VAR ASSIGN VAR NUMBER IF VAR NUMBER VAR NUMBER VAR BIN_OP BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR IF VAR NUMBER VAR BIN_OP BIN_OP BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR
Read problem statements in [Russian], [Mandarin Chinese], [Bengali], and [Vietnamese] as well. You are given an array A consisting of N integers and Q queries. Each query is described by two integers L and R. For each query, output the number of tuples (i, j, k) such that L≀ i< j< k≀ R and A_{i}+A_{j}+A_{k} is an even number. ------ Input Format ------ - 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 two integers N and Q. - The next line contains N integers A_{1},\ldots,A_{N}. - Then Q lines follow, each containing two integers L_{i} and R_{i}. ------ Output Format ------ For each query, output the number of tuples possible as mentioned in the problem statement. ------ Constraints ------ $1 ≀ T ≀ 10^{3}$ $1 ≀ N, Q ≀ 10^{5}$ $0 ≀ A_{i} ≀ 10^{6}$ $1 ≀ L_{i} ≀ R_{i} ≀ N$ - The sum of $N$ over all test cases does not exceed $10^{6}$. - The sum of $Q$ over all test cases does not exceed $10^{5}$ ----- Sample Input 1 ------ 1 6 3 1 2 3 4 5 6 1 3 2 5 1 6 ----- Sample Output 1 ------ 1 2 10 ----- explanation 1 ------ - For the first query, we can choose $(1, 2, 3)$ since $A_{1} + A_{2} + A_{3} = 6$ is an even number. - For the second query, we can choose $(2, 3, 5)$ since $A_{2} + A_{3} + A_{5} = 10$ is even, and $(3, 4, 5)$ since $A_{3} + A_{4} + A_{5} = 12$ is even.
def fun(a): ans = [0] * (len(a) + 1) u = 1 for i in a: if i % 2 == 0: ans[u] = ans[u - 1] + 1 else: ans[u] = ans[u - 1] u += 1 return ans for _ in range(int(input())): n, q = map(int, input().split()) li = [int(x) for x in input().split()] ans_arr = fun(li) for i in range(q): l, r = map(int, input().split()) even = ans_arr[r] - ans_arr[l - 1] odd = r - l + 1 - even even_combo = even * (even - 1) * (even - 2) // 6 odd_combo = odd * (odd - 1) // 2 * even print(even_combo + odd_combo)
FUNC_DEF ASSIGN VAR BIN_OP LIST NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER RETURN VAR 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 FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR
Read problem statements in [Russian], [Mandarin Chinese], [Bengali], and [Vietnamese] as well. You are given an array A consisting of N integers and Q queries. Each query is described by two integers L and R. For each query, output the number of tuples (i, j, k) such that L≀ i< j< k≀ R and A_{i}+A_{j}+A_{k} is an even number. ------ Input Format ------ - 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 two integers N and Q. - The next line contains N integers A_{1},\ldots,A_{N}. - Then Q lines follow, each containing two integers L_{i} and R_{i}. ------ Output Format ------ For each query, output the number of tuples possible as mentioned in the problem statement. ------ Constraints ------ $1 ≀ T ≀ 10^{3}$ $1 ≀ N, Q ≀ 10^{5}$ $0 ≀ A_{i} ≀ 10^{6}$ $1 ≀ L_{i} ≀ R_{i} ≀ N$ - The sum of $N$ over all test cases does not exceed $10^{6}$. - The sum of $Q$ over all test cases does not exceed $10^{5}$ ----- Sample Input 1 ------ 1 6 3 1 2 3 4 5 6 1 3 2 5 1 6 ----- Sample Output 1 ------ 1 2 10 ----- explanation 1 ------ - For the first query, we can choose $(1, 2, 3)$ since $A_{1} + A_{2} + A_{3} = 6$ is an even number. - For the second query, we can choose $(2, 3, 5)$ since $A_{2} + A_{3} + A_{5} = 10$ is even, and $(3, 4, 5)$ since $A_{3} + A_{4} + A_{5} = 12$ is even.
T = int(input()) while T: n, q = map(int, input().split(" ")) n_list = [0] * (n + 1) i_l = list(map(int, input().split(" "))) n_list[1 : n + 1] = i_l for i in range(1, len(n_list)): if n_list[i] % 2 == 0: n_list[i] = n_list[i - 1] + 1 else: n_list[i] = n_list[i - 1] while q: l, r = map(int, input().split(" ")) even = n_list[r] - n_list[l - 1] odd = r - l + 1 - even ec3 = even * (even - 1) * (even - 2) // 6 oc2 = odd * (odd - 1) // 2 res = ec3 + oc2 * even print(res) q = q - 1 T = T - 1
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR NUMBER BIN_OP VAR NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER WHILE VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER
Read problem statements in [Russian], [Mandarin Chinese], [Bengali], and [Vietnamese] as well. You are given an array A consisting of N integers and Q queries. Each query is described by two integers L and R. For each query, output the number of tuples (i, j, k) such that L≀ i< j< k≀ R and A_{i}+A_{j}+A_{k} is an even number. ------ Input Format ------ - 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 two integers N and Q. - The next line contains N integers A_{1},\ldots,A_{N}. - Then Q lines follow, each containing two integers L_{i} and R_{i}. ------ Output Format ------ For each query, output the number of tuples possible as mentioned in the problem statement. ------ Constraints ------ $1 ≀ T ≀ 10^{3}$ $1 ≀ N, Q ≀ 10^{5}$ $0 ≀ A_{i} ≀ 10^{6}$ $1 ≀ L_{i} ≀ R_{i} ≀ N$ - The sum of $N$ over all test cases does not exceed $10^{6}$. - The sum of $Q$ over all test cases does not exceed $10^{5}$ ----- Sample Input 1 ------ 1 6 3 1 2 3 4 5 6 1 3 2 5 1 6 ----- Sample Output 1 ------ 1 2 10 ----- explanation 1 ------ - For the first query, we can choose $(1, 2, 3)$ since $A_{1} + A_{2} + A_{3} = 6$ is an even number. - For the second query, we can choose $(2, 3, 5)$ since $A_{2} + A_{3} + A_{5} = 10$ is even, and $(3, 4, 5)$ since $A_{3} + A_{4} + A_{5} = 12$ is even.
for testcase in range(int(input())): n, k = map(int, input().split()) l = list(map(int, input().split())) even = 0 final = [] for x in l: if x % 2 == 0: even += 1 final.append(even) for q in range(k): l, r = map(int, input().split()) if l != 1: total_even = final[r - 1] - final[l - 2] else: total_even = final[r - 1] total_odd = r - l - total_even + 1 print( total_even * (total_even - 1) * (total_even - 2) // 6 + total_odd * (total_odd - 1) // 2 * total_even )
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 FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR LIST FOR VAR VAR IF BIN_OP VAR NUMBER NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER BIN_OP BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR
Read problem statements in [Russian], [Mandarin Chinese], [Bengali], and [Vietnamese] as well. You are given an array A consisting of N integers and Q queries. Each query is described by two integers L and R. For each query, output the number of tuples (i, j, k) such that L≀ i< j< k≀ R and A_{i}+A_{j}+A_{k} is an even number. ------ Input Format ------ - 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 two integers N and Q. - The next line contains N integers A_{1},\ldots,A_{N}. - Then Q lines follow, each containing two integers L_{i} and R_{i}. ------ Output Format ------ For each query, output the number of tuples possible as mentioned in the problem statement. ------ Constraints ------ $1 ≀ T ≀ 10^{3}$ $1 ≀ N, Q ≀ 10^{5}$ $0 ≀ A_{i} ≀ 10^{6}$ $1 ≀ L_{i} ≀ R_{i} ≀ N$ - The sum of $N$ over all test cases does not exceed $10^{6}$. - The sum of $Q$ over all test cases does not exceed $10^{5}$ ----- Sample Input 1 ------ 1 6 3 1 2 3 4 5 6 1 3 2 5 1 6 ----- Sample Output 1 ------ 1 2 10 ----- explanation 1 ------ - For the first query, we can choose $(1, 2, 3)$ since $A_{1} + A_{2} + A_{3} = 6$ is an even number. - For the second query, we can choose $(2, 3, 5)$ since $A_{2} + A_{3} + A_{5} = 10$ is even, and $(3, 4, 5)$ since $A_{3} + A_{4} + A_{5} = 12$ is even.
for t in range(int(input())): inp = input().split() n = int(inp[0]) q = int(inp[1]) arr = input().split() eve = [0] for i in range(len(arr)): if int(arr[i]) % 2 == 0: eve.append(eve[-1] + 1) else: eve.append(eve[-1]) for i in range(q): lr = input().split() l = int(lr[0]) r = int(lr[1]) e = eve[r] - eve[l - 1] o = r - l + 1 - e count = int(e * o * (o - 1) / 2 + e * (e - 1) * (e - 2) / 6) print(count)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF BIN_OP FUNC_CALL VAR VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR BIN_OP VAR NUMBER NUMBER BIN_OP BIN_OP BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR
Read problem statements in [Russian], [Mandarin Chinese], [Bengali], and [Vietnamese] as well. You are given an array A consisting of N integers and Q queries. Each query is described by two integers L and R. For each query, output the number of tuples (i, j, k) such that L≀ i< j< k≀ R and A_{i}+A_{j}+A_{k} is an even number. ------ Input Format ------ - 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 two integers N and Q. - The next line contains N integers A_{1},\ldots,A_{N}. - Then Q lines follow, each containing two integers L_{i} and R_{i}. ------ Output Format ------ For each query, output the number of tuples possible as mentioned in the problem statement. ------ Constraints ------ $1 ≀ T ≀ 10^{3}$ $1 ≀ N, Q ≀ 10^{5}$ $0 ≀ A_{i} ≀ 10^{6}$ $1 ≀ L_{i} ≀ R_{i} ≀ N$ - The sum of $N$ over all test cases does not exceed $10^{6}$. - The sum of $Q$ over all test cases does not exceed $10^{5}$ ----- Sample Input 1 ------ 1 6 3 1 2 3 4 5 6 1 3 2 5 1 6 ----- Sample Output 1 ------ 1 2 10 ----- explanation 1 ------ - For the first query, we can choose $(1, 2, 3)$ since $A_{1} + A_{2} + A_{3} = 6$ is an even number. - For the second query, we can choose $(2, 3, 5)$ since $A_{2} + A_{3} + A_{5} = 10$ is even, and $(3, 4, 5)$ since $A_{3} + A_{4} + A_{5} = 12$ is even.
t = int(input()) while t > 0: n, q = map(int, input().split()) arr = list(map(int, input().strip().split())) even = [0] * n odd = [0] * n countOdd = 0 countEven = 0 for i in range(n): if arr[i] % 2 == 0: countEven += 1 else: countOdd += 1 even[i] = countEven odd[i] = countOdd for i in range(q): l, r = map(int, input().split()) e = even[r - 1] - even[l - 1] o = odd[r - 1] - odd[l - 1] if arr[l - 1] % 2 == 0: e += 1 else: o += 1 a = 0 b = 0 if e >= 3: a = e * (e - 1) * (e - 2) // 6 if e >= 1 and o >= 2: b = e * (o * (o - 1) // 2) ans = a + b print(ans) t = t - 1
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 FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR NUMBER NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER IF BIN_OP VAR BIN_OP VAR NUMBER NUMBER NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER IF VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER
Read problem statements in [Russian], [Mandarin Chinese], [Bengali], and [Vietnamese] as well. You are given an array A consisting of N integers and Q queries. Each query is described by two integers L and R. For each query, output the number of tuples (i, j, k) such that L≀ i< j< k≀ R and A_{i}+A_{j}+A_{k} is an even number. ------ Input Format ------ - 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 two integers N and Q. - The next line contains N integers A_{1},\ldots,A_{N}. - Then Q lines follow, each containing two integers L_{i} and R_{i}. ------ Output Format ------ For each query, output the number of tuples possible as mentioned in the problem statement. ------ Constraints ------ $1 ≀ T ≀ 10^{3}$ $1 ≀ N, Q ≀ 10^{5}$ $0 ≀ A_{i} ≀ 10^{6}$ $1 ≀ L_{i} ≀ R_{i} ≀ N$ - The sum of $N$ over all test cases does not exceed $10^{6}$. - The sum of $Q$ over all test cases does not exceed $10^{5}$ ----- Sample Input 1 ------ 1 6 3 1 2 3 4 5 6 1 3 2 5 1 6 ----- Sample Output 1 ------ 1 2 10 ----- explanation 1 ------ - For the first query, we can choose $(1, 2, 3)$ since $A_{1} + A_{2} + A_{3} = 6$ is an even number. - For the second query, we can choose $(2, 3, 5)$ since $A_{2} + A_{3} + A_{5} = 10$ is even, and $(3, 4, 5)$ since $A_{3} + A_{4} + A_{5} = 12$ is even.
def fact(a, b): if a < b: return 0 elif a == b: return 1 elif b == 1: return a elif b == 2: return a * (a - 1) / 2 else: return a * (a - 1) * (a - 2) / 6 for _ in range(int(input())): n, q = map(int, input().split()) l = list(map(int, input().split())) arr = [] eve = 0 odd = 0 for i in range(len(l)): k = [] if l[i] % 2 == 0: eve += 1 else: odd += 1 k.append(eve) k.append(odd) arr.append(k) for i in range(q): first, last = map(int, input().split()) first -= 2 last -= 1 if first == -1: f_eve = arr[last][0] f_odd = arr[last][1] else: f_eve = arr[last][0] - arr[first][0] f_odd = arr[last][1] - arr[first][1] if f_eve < 1: print(0) elif f_odd < 2 and f_eve < 3: print(0) else: z = fact(f_eve, 3) + fact(f_eve, 1) * fact(f_odd, 2) print(int(z))
FUNC_DEF IF VAR VAR RETURN NUMBER IF VAR VAR RETURN NUMBER IF VAR NUMBER RETURN VAR IF VAR NUMBER RETURN BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER RETURN BIN_OP BIN_OP BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER 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 FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR LIST IF BIN_OP VAR VAR NUMBER NUMBER VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR VAR NUMBER VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR NUMBER VAR VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER IF VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR
Read problem statements in [Russian], [Mandarin Chinese], [Bengali], and [Vietnamese] as well. You are given an array A consisting of N integers and Q queries. Each query is described by two integers L and R. For each query, output the number of tuples (i, j, k) such that L≀ i< j< k≀ R and A_{i}+A_{j}+A_{k} is an even number. ------ Input Format ------ - 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 two integers N and Q. - The next line contains N integers A_{1},\ldots,A_{N}. - Then Q lines follow, each containing two integers L_{i} and R_{i}. ------ Output Format ------ For each query, output the number of tuples possible as mentioned in the problem statement. ------ Constraints ------ $1 ≀ T ≀ 10^{3}$ $1 ≀ N, Q ≀ 10^{5}$ $0 ≀ A_{i} ≀ 10^{6}$ $1 ≀ L_{i} ≀ R_{i} ≀ N$ - The sum of $N$ over all test cases does not exceed $10^{6}$. - The sum of $Q$ over all test cases does not exceed $10^{5}$ ----- Sample Input 1 ------ 1 6 3 1 2 3 4 5 6 1 3 2 5 1 6 ----- Sample Output 1 ------ 1 2 10 ----- explanation 1 ------ - For the first query, we can choose $(1, 2, 3)$ since $A_{1} + A_{2} + A_{3} = 6$ is an even number. - For the second query, we can choose $(2, 3, 5)$ since $A_{2} + A_{3} + A_{5} = 10$ is even, and $(3, 4, 5)$ since $A_{3} + A_{4} + A_{5} = 12$ is even.
def A(): for _ in range(int(input())): R1, W1, C1 = [int(x) for x in input().split()] R2, W2, C2 = [int(x) for x in input().split()] A, B = 0, 0 if R1 > R2: A += 1 else: B += 1 if W1 > W2: A += 1 else: B += 1 if C1 > C2: A += 1 else: B += 1 print("A" if A > B else "B") def B(): for _ in range(int(input())): X, Y = [int(x) for x in input().split()] s = X + Y if s >= 6: print(0) else: print((6 - s) / 6) def C(): for _ in range(int(input())): N, M, L = [int(x) for x in input().split()] A = [[int(x) for x in input().split()] for i in range(M)] S = [int(x) for x in input().split()] colors = [0] * (N + 1) for i in range(M): for color in A[i][1:]: colors[color] = i segments = 1 for i in range(1, L): if colors[S[i - 1]] != colors[S[i]]: segments += 1 print(segments) def D(): def calc(odd, even): ans = even * (even - 1) * (even - 2) // 6 if even > 2 else 0 ans += even * odd * (odd - 1) // 2 if odd > 1 else 0 return ans for _ in range(int(input())): N, Q = [int(x) for x in input().split()] A = [int(x) for x in input().split()] LR = [[int(x) for x in input().split()] for i in range(Q)] even, odd = [0] * N, [0] * N if A[0] % 2 is 0: even[0] += 1 else: odd[0] += 1 for i, a in enumerate(A[1:], start=1): if a % 2 is 0: even[i] = even[i - 1] + 1 odd[i] = odd[i - 1] else: even[i] = even[i - 1] odd[i] = odd[i - 1] + 1 for i in range(Q): L, R = LR[i][0] - 1, LR[i][1] - 1 if L is 0: print(calc(odd[R], even[R])) else: print(calc(odd[R] - odd[L - 1], even[R] - even[L - 1])) D()
FUNC_DEF FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR NUMBER NUMBER IF VAR VAR VAR NUMBER VAR NUMBER IF VAR VAR VAR NUMBER VAR NUMBER IF VAR VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR VAR STRING STRING FUNC_DEF 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 BIN_OP VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP NUMBER VAR NUMBER FUNC_DEF FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR BIN_OP VAR NUMBER VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR FUNC_DEF FUNC_DEF ASSIGN VAR VAR NUMBER BIN_OP BIN_OP BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER NUMBER VAR VAR NUMBER BIN_OP BIN_OP BIN_OP VAR VAR BIN_OP VAR NUMBER NUMBER NUMBER RETURN VAR 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 FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP LIST NUMBER VAR BIN_OP LIST NUMBER VAR IF BIN_OP VAR NUMBER NUMBER NUMBER VAR NUMBER NUMBER VAR NUMBER NUMBER FOR VAR VAR FUNC_CALL VAR VAR NUMBER NUMBER IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR NUMBER NUMBER BIN_OP VAR VAR NUMBER NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER BIN_OP VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR
Read problem statements in [Russian], [Mandarin Chinese], [Bengali], and [Vietnamese] as well. You are given an array A consisting of N integers and Q queries. Each query is described by two integers L and R. For each query, output the number of tuples (i, j, k) such that L≀ i< j< k≀ R and A_{i}+A_{j}+A_{k} is an even number. ------ Input Format ------ - 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 two integers N and Q. - The next line contains N integers A_{1},\ldots,A_{N}. - Then Q lines follow, each containing two integers L_{i} and R_{i}. ------ Output Format ------ For each query, output the number of tuples possible as mentioned in the problem statement. ------ Constraints ------ $1 ≀ T ≀ 10^{3}$ $1 ≀ N, Q ≀ 10^{5}$ $0 ≀ A_{i} ≀ 10^{6}$ $1 ≀ L_{i} ≀ R_{i} ≀ N$ - The sum of $N$ over all test cases does not exceed $10^{6}$. - The sum of $Q$ over all test cases does not exceed $10^{5}$ ----- Sample Input 1 ------ 1 6 3 1 2 3 4 5 6 1 3 2 5 1 6 ----- Sample Output 1 ------ 1 2 10 ----- explanation 1 ------ - For the first query, we can choose $(1, 2, 3)$ since $A_{1} + A_{2} + A_{3} = 6$ is an even number. - For the second query, we can choose $(2, 3, 5)$ since $A_{2} + A_{3} + A_{5} = 10$ is even, and $(3, 4, 5)$ since $A_{3} + A_{4} + A_{5} = 12$ is even.
for _ in range(int(input())): n, q = map(int, input().split()) a = list(map(int, input().split())) ha = [0] * (len(a) + 1) for i in range(0, len(a)): if a[i] % 2 == 0: ha[i + 1] = ha[i] + 1 else: ha[i + 1] = ha[i] for x in range(q): l, r = map(int, input().split()) even = ha[r] - ha[l - 1] odd = r - l + 1 - even ans = even * (even - 1) * (even - 2) / 6 + odd * (odd - 1) / 2 * even print(int(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 FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER BIN_OP BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR
Read problem statements in [Russian], [Mandarin Chinese], [Bengali], and [Vietnamese] as well. You are given an array A consisting of N integers and Q queries. Each query is described by two integers L and R. For each query, output the number of tuples (i, j, k) such that L≀ i< j< k≀ R and A_{i}+A_{j}+A_{k} is an even number. ------ Input Format ------ - 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 two integers N and Q. - The next line contains N integers A_{1},\ldots,A_{N}. - Then Q lines follow, each containing two integers L_{i} and R_{i}. ------ Output Format ------ For each query, output the number of tuples possible as mentioned in the problem statement. ------ Constraints ------ $1 ≀ T ≀ 10^{3}$ $1 ≀ N, Q ≀ 10^{5}$ $0 ≀ A_{i} ≀ 10^{6}$ $1 ≀ L_{i} ≀ R_{i} ≀ N$ - The sum of $N$ over all test cases does not exceed $10^{6}$. - The sum of $Q$ over all test cases does not exceed $10^{5}$ ----- Sample Input 1 ------ 1 6 3 1 2 3 4 5 6 1 3 2 5 1 6 ----- Sample Output 1 ------ 1 2 10 ----- explanation 1 ------ - For the first query, we can choose $(1, 2, 3)$ since $A_{1} + A_{2} + A_{3} = 6$ is an even number. - For the second query, we can choose $(2, 3, 5)$ since $A_{2} + A_{3} + A_{5} = 10$ is even, and $(3, 4, 5)$ since $A_{3} + A_{4} + A_{5} = 12$ is even.
for t in range(int(input())): n, q = list(map(int, input().split())) lst = list(map(int, input().split())) even_odd_list = [(0) for i in range(n + 1)] for i in range(1, len(even_odd_list)): if lst[i - 1] % 2 == 0: even_odd_list[i] += 1 + even_odd_list[i - 1] else: even_odd_list[i] = even_odd_list[i - 1] for i in range(q): lr = list(map(int, input().split())) l = lr[0] r = lr[1] TE = even_odd_list[r] - even_odd_list[l - 1] TO = r - l + 1 - TE one = TE * (TE - 1) * (TE - 2) // 6 two = TO * (TO - 1) // 2 * TE print(one + two)
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 FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF BIN_OP VAR BIN_OP VAR NUMBER NUMBER NUMBER VAR VAR BIN_OP NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR 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 BIN_OP VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR
Read problem statements in [Russian], [Mandarin Chinese], [Bengali], and [Vietnamese] as well. You are given an array A consisting of N integers and Q queries. Each query is described by two integers L and R. For each query, output the number of tuples (i, j, k) such that L≀ i< j< k≀ R and A_{i}+A_{j}+A_{k} is an even number. ------ Input Format ------ - 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 two integers N and Q. - The next line contains N integers A_{1},\ldots,A_{N}. - Then Q lines follow, each containing two integers L_{i} and R_{i}. ------ Output Format ------ For each query, output the number of tuples possible as mentioned in the problem statement. ------ Constraints ------ $1 ≀ T ≀ 10^{3}$ $1 ≀ N, Q ≀ 10^{5}$ $0 ≀ A_{i} ≀ 10^{6}$ $1 ≀ L_{i} ≀ R_{i} ≀ N$ - The sum of $N$ over all test cases does not exceed $10^{6}$. - The sum of $Q$ over all test cases does not exceed $10^{5}$ ----- Sample Input 1 ------ 1 6 3 1 2 3 4 5 6 1 3 2 5 1 6 ----- Sample Output 1 ------ 1 2 10 ----- explanation 1 ------ - For the first query, we can choose $(1, 2, 3)$ since $A_{1} + A_{2} + A_{3} = 6$ is an even number. - For the second query, we can choose $(2, 3, 5)$ since $A_{2} + A_{3} + A_{5} = 10$ is even, and $(3, 4, 5)$ since $A_{3} + A_{4} + A_{5} = 12$ is even.
for _ in range(int(input())): n, q = [int(x) for x in input().split()] a = [int(x) for x in input().split()] pre = [0] * (n + 1) for i in range(n): pre[i + 1] = pre[i] if a[i] & 1: pre[i + 1] += 1 for _ in range(q): l, r = [int(x) for x in input().split()] odds = pre[r] - pre[l - 1] evens = r - l + 1 - odds if evens >= 3: even_answer = evens * (evens - 1) * (evens - 2) // 6 else: even_answer = 0 if odds >= 2: lal = odds - 1 odd_answer = lal * (lal + 1) // 2 * evens else: odd_answer = 0 print(even_answer + odd_answer)
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 FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER VAR VAR IF BIN_OP VAR VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER VAR IF VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER ASSIGN VAR NUMBER IF VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR ASSIGN VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR
Read problem statements in [Russian], [Mandarin Chinese], [Bengali], and [Vietnamese] as well. You are given an array A consisting of N integers and Q queries. Each query is described by two integers L and R. For each query, output the number of tuples (i, j, k) such that L≀ i< j< k≀ R and A_{i}+A_{j}+A_{k} is an even number. ------ Input Format ------ - 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 two integers N and Q. - The next line contains N integers A_{1},\ldots,A_{N}. - Then Q lines follow, each containing two integers L_{i} and R_{i}. ------ Output Format ------ For each query, output the number of tuples possible as mentioned in the problem statement. ------ Constraints ------ $1 ≀ T ≀ 10^{3}$ $1 ≀ N, Q ≀ 10^{5}$ $0 ≀ A_{i} ≀ 10^{6}$ $1 ≀ L_{i} ≀ R_{i} ≀ N$ - The sum of $N$ over all test cases does not exceed $10^{6}$. - The sum of $Q$ over all test cases does not exceed $10^{5}$ ----- Sample Input 1 ------ 1 6 3 1 2 3 4 5 6 1 3 2 5 1 6 ----- Sample Output 1 ------ 1 2 10 ----- explanation 1 ------ - For the first query, we can choose $(1, 2, 3)$ since $A_{1} + A_{2} + A_{3} = 6$ is an even number. - For the second query, we can choose $(2, 3, 5)$ since $A_{2} + A_{3} + A_{5} = 10$ is even, and $(3, 4, 5)$ since $A_{3} + A_{4} + A_{5} = 12$ is even.
t = int(input()) for i in range(t): N, Q = input().split() n = int(N) q = int(Q) arr = [int(i) for i in input().split()] l = [[0, 0]] for i in range(n): a = l[i] x = a[0] y = a[1] if arr[i] % 2 == 0: l.append([x + 1, y]) else: l.append([x, y + 1]) for i in range(q): A, B = input().split() a = int(A) b = int(B) p = l[a - 1][0] q = l[a - 1][1] r = l[b][0] s = l[b][1] even = abs(p - r) odd = abs(q - s) if even == 0: print(0) else: ans = int((odd - 1) * odd / 2) * even if even >= 3: ans += int(even * (even - 1) * (even - 2) / 6) print(ans)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST LIST NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER IF BIN_OP VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR LIST BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR LIST VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR BIN_OP BIN_OP BIN_OP VAR NUMBER VAR NUMBER VAR IF VAR NUMBER VAR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR
Read problem statements in [Russian], [Mandarin Chinese], [Bengali], and [Vietnamese] as well. You are given an array A consisting of N integers and Q queries. Each query is described by two integers L and R. For each query, output the number of tuples (i, j, k) such that L≀ i< j< k≀ R and A_{i}+A_{j}+A_{k} is an even number. ------ Input Format ------ - 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 two integers N and Q. - The next line contains N integers A_{1},\ldots,A_{N}. - Then Q lines follow, each containing two integers L_{i} and R_{i}. ------ Output Format ------ For each query, output the number of tuples possible as mentioned in the problem statement. ------ Constraints ------ $1 ≀ T ≀ 10^{3}$ $1 ≀ N, Q ≀ 10^{5}$ $0 ≀ A_{i} ≀ 10^{6}$ $1 ≀ L_{i} ≀ R_{i} ≀ N$ - The sum of $N$ over all test cases does not exceed $10^{6}$. - The sum of $Q$ over all test cases does not exceed $10^{5}$ ----- Sample Input 1 ------ 1 6 3 1 2 3 4 5 6 1 3 2 5 1 6 ----- Sample Output 1 ------ 1 2 10 ----- explanation 1 ------ - For the first query, we can choose $(1, 2, 3)$ since $A_{1} + A_{2} + A_{3} = 6$ is an even number. - For the second query, we can choose $(2, 3, 5)$ since $A_{2} + A_{3} + A_{5} = 10$ is even, and $(3, 4, 5)$ since $A_{3} + A_{4} + A_{5} = 12$ is even.
for t in range(int(input())): n, q = map(int, input().split()) odd = [0] * (n + 1) even = [0] * (n + 1) a = list(map(int, input().strip().split())) if a[0] % 2 == 0: even[1] = 1 else: odd[1] = 1 for i in range(1, n): if a[i] % 2 == 0: even[i + 1] += 1 else: odd[i + 1] += 1 even[i + 1] += even[i] odd[i + 1] += odd[i] for _ in range(q): l, r = map(int, input().split()) odds = odd[r] - odd[l - 1] evens = even[r] - even[l - 1] ans = 0 if odds >= 2: ans += odds * (odds - 1) // 2 * evens if evens > 2: ans += evens * (evens - 1) * (evens - 2) // 6 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 VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR IF BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR NUMBER NUMBER ASSIGN VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF BIN_OP VAR VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL 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 NUMBER IF VAR NUMBER VAR BIN_OP BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR IF VAR NUMBER VAR BIN_OP BIN_OP BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR
Read problem statements in [Russian], [Mandarin Chinese], [Bengali], and [Vietnamese] as well. You are given an array A consisting of N integers and Q queries. Each query is described by two integers L and R. For each query, output the number of tuples (i, j, k) such that L≀ i< j< k≀ R and A_{i}+A_{j}+A_{k} is an even number. ------ Input Format ------ - 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 two integers N and Q. - The next line contains N integers A_{1},\ldots,A_{N}. - Then Q lines follow, each containing two integers L_{i} and R_{i}. ------ Output Format ------ For each query, output the number of tuples possible as mentioned in the problem statement. ------ Constraints ------ $1 ≀ T ≀ 10^{3}$ $1 ≀ N, Q ≀ 10^{5}$ $0 ≀ A_{i} ≀ 10^{6}$ $1 ≀ L_{i} ≀ R_{i} ≀ N$ - The sum of $N$ over all test cases does not exceed $10^{6}$. - The sum of $Q$ over all test cases does not exceed $10^{5}$ ----- Sample Input 1 ------ 1 6 3 1 2 3 4 5 6 1 3 2 5 1 6 ----- Sample Output 1 ------ 1 2 10 ----- explanation 1 ------ - For the first query, we can choose $(1, 2, 3)$ since $A_{1} + A_{2} + A_{3} = 6$ is an even number. - For the second query, we can choose $(2, 3, 5)$ since $A_{2} + A_{3} + A_{5} = 10$ is even, and $(3, 4, 5)$ since $A_{3} + A_{4} + A_{5} = 12$ is even.
for _ in range(int(input())): n, q = map(int, input().split()) l = list(map(int, input().split())) even = [0] odd = [0] for i in l: if i % 2 == 0: even.append(even[-1] + 1) odd.append(odd[-1]) else: even.append(even[-1]) odd.append(odd[-1] + 1) for i in range(q): v, k = map(int, input().split()) e = even[k] - even[v - 1] o = odd[k] - odd[v - 1] a = o * (o - 1) * e // 2 b = e * (e - 1) * (e - 2) // 6 print(a + b)
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 FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST NUMBER ASSIGN VAR LIST NUMBER FOR VAR VAR IF BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL 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 BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR
Read problem statements in [Russian], [Mandarin Chinese], [Bengali], and [Vietnamese] as well. You are given an array A consisting of N integers and Q queries. Each query is described by two integers L and R. For each query, output the number of tuples (i, j, k) such that L≀ i< j< k≀ R and A_{i}+A_{j}+A_{k} is an even number. ------ Input Format ------ - 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 two integers N and Q. - The next line contains N integers A_{1},\ldots,A_{N}. - Then Q lines follow, each containing two integers L_{i} and R_{i}. ------ Output Format ------ For each query, output the number of tuples possible as mentioned in the problem statement. ------ Constraints ------ $1 ≀ T ≀ 10^{3}$ $1 ≀ N, Q ≀ 10^{5}$ $0 ≀ A_{i} ≀ 10^{6}$ $1 ≀ L_{i} ≀ R_{i} ≀ N$ - The sum of $N$ over all test cases does not exceed $10^{6}$. - The sum of $Q$ over all test cases does not exceed $10^{5}$ ----- Sample Input 1 ------ 1 6 3 1 2 3 4 5 6 1 3 2 5 1 6 ----- Sample Output 1 ------ 1 2 10 ----- explanation 1 ------ - For the first query, we can choose $(1, 2, 3)$ since $A_{1} + A_{2} + A_{3} = 6$ is an even number. - For the second query, we can choose $(2, 3, 5)$ since $A_{2} + A_{3} + A_{5} = 10$ is even, and $(3, 4, 5)$ since $A_{3} + A_{4} + A_{5} = 12$ is even.
t = int(input()) for i in range(t): ls1 = input().split(" ") N = int(ls1[0]) Q = int(ls1[1]) ls2 = [int(x) for x in input().split(" ")] evenlis = [] evenc = 0 oddlis = [] oddc = 0 for j in range(N): if ls2[j] % 2 == 0: evenc += 1 evenlis.append(evenc) oddlis.append(oddc) else: oddc += 1 evenlis.append(evenc) oddlis.append(oddc) for ii in range(Q): lls = input().split(" ") L = int(lls[0]) R = int(lls[1]) if L == 1: even_nos = evenlis[R - 1] odd_nos = oddlis[R - 1] else: even_nos = evenlis[R - 1] - evenlis[L - 2] odd_nos = oddlis[R - 1] - oddlis[L - 2] print( even_nos * (even_nos - 1) * (even_nos - 2) // 6 + odd_nos * (odd_nos - 1) // 2 * even_nos )
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 FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR NUMBER NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR 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 IF VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER BIN_OP BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR
Read problem statements in [Russian], [Mandarin Chinese], [Bengali], and [Vietnamese] as well. You are given an array A consisting of N integers and Q queries. Each query is described by two integers L and R. For each query, output the number of tuples (i, j, k) such that L≀ i< j< k≀ R and A_{i}+A_{j}+A_{k} is an even number. ------ Input Format ------ - 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 two integers N and Q. - The next line contains N integers A_{1},\ldots,A_{N}. - Then Q lines follow, each containing two integers L_{i} and R_{i}. ------ Output Format ------ For each query, output the number of tuples possible as mentioned in the problem statement. ------ Constraints ------ $1 ≀ T ≀ 10^{3}$ $1 ≀ N, Q ≀ 10^{5}$ $0 ≀ A_{i} ≀ 10^{6}$ $1 ≀ L_{i} ≀ R_{i} ≀ N$ - The sum of $N$ over all test cases does not exceed $10^{6}$. - The sum of $Q$ over all test cases does not exceed $10^{5}$ ----- Sample Input 1 ------ 1 6 3 1 2 3 4 5 6 1 3 2 5 1 6 ----- Sample Output 1 ------ 1 2 10 ----- explanation 1 ------ - For the first query, we can choose $(1, 2, 3)$ since $A_{1} + A_{2} + A_{3} = 6$ is an even number. - For the second query, we can choose $(2, 3, 5)$ since $A_{2} + A_{3} + A_{5} = 10$ is even, and $(3, 4, 5)$ since $A_{3} + A_{4} + A_{5} = 12$ is even.
for _ in range(int(input())): n, q = map(int, input().split()) arr = list(map(int, input().split())) res = [] ans = [] count = 0 for i in range(n): if arr[i] % 2 == 0: count += 1 res.append(count) ans.append(i + 1 - count) for _ in range(q): l, r = map(int, input().split()) if l > 1: e = res[r - 1] - res[l - 1] + (res[l - 1] - res[l - 2]) o = ans[r - 1] - ans[l - 1] + (ans[l - 1] - ans[l - 2]) else: e = res[r - 1] o = ans[r - 1] c = e * (e - 1) * (e - 2) // 6 + e * (o * (o - 1) // 2) print(c)
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 FUNC_CALL 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 IF BIN_OP VAR VAR NUMBER NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER BIN_OP VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR
Read problem statements in [Russian], [Mandarin Chinese], [Bengali], and [Vietnamese] as well. You are given an array A consisting of N integers and Q queries. Each query is described by two integers L and R. For each query, output the number of tuples (i, j, k) such that L≀ i< j< k≀ R and A_{i}+A_{j}+A_{k} is an even number. ------ Input Format ------ - 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 two integers N and Q. - The next line contains N integers A_{1},\ldots,A_{N}. - Then Q lines follow, each containing two integers L_{i} and R_{i}. ------ Output Format ------ For each query, output the number of tuples possible as mentioned in the problem statement. ------ Constraints ------ $1 ≀ T ≀ 10^{3}$ $1 ≀ N, Q ≀ 10^{5}$ $0 ≀ A_{i} ≀ 10^{6}$ $1 ≀ L_{i} ≀ R_{i} ≀ N$ - The sum of $N$ over all test cases does not exceed $10^{6}$. - The sum of $Q$ over all test cases does not exceed $10^{5}$ ----- Sample Input 1 ------ 1 6 3 1 2 3 4 5 6 1 3 2 5 1 6 ----- Sample Output 1 ------ 1 2 10 ----- explanation 1 ------ - For the first query, we can choose $(1, 2, 3)$ since $A_{1} + A_{2} + A_{3} = 6$ is an even number. - For the second query, we can choose $(2, 3, 5)$ since $A_{2} + A_{3} + A_{5} = 10$ is even, and $(3, 4, 5)$ since $A_{3} + A_{4} + A_{5} = 12$ is even.
def combination(n, r): numerator = 1 for i in range(n - r + 1, n + 1): numerator *= i denominator = 1 for j in range(2, r + 1): denominator *= j return numerator // denominator try: t = int(input()) i = 0 while i < t: arr = list(input().split(" ")) N = int(arr[0]) Q = int(arr[1]) A = list(input().split(" ")) for ind in range(0, len(A)): A[ind] = int(A[ind]) even = [0] for ind in range(0, N): if A[ind] % 2 == 0: even.append(even[ind] + 1) else: even.append(even[ind]) j = 0 while j < Q: query = list(input().split(" ")) L = int(query[0]) R = int(query[1]) even_count = even[R] - even[L - 1] odd_count = R - L + 1 - even_count tuples_count = ( combination(even_count, 3) + combination(odd_count, 2) * even_count ) print(tuples_count) Q -= 1 t -= 1 except IOError: print("IO Exception")
FUNC_DEF ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER BIN_OP VAR NUMBER VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER VAR VAR RETURN BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR LIST NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF BIN_OP VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER VAR EXPR FUNC_CALL VAR VAR VAR NUMBER VAR NUMBER VAR EXPR FUNC_CALL VAR STRING
Read problem statements in [Russian], [Mandarin Chinese], [Bengali], and [Vietnamese] as well. You are given an array A consisting of N integers and Q queries. Each query is described by two integers L and R. For each query, output the number of tuples (i, j, k) such that L≀ i< j< k≀ R and A_{i}+A_{j}+A_{k} is an even number. ------ Input Format ------ - 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 two integers N and Q. - The next line contains N integers A_{1},\ldots,A_{N}. - Then Q lines follow, each containing two integers L_{i} and R_{i}. ------ Output Format ------ For each query, output the number of tuples possible as mentioned in the problem statement. ------ Constraints ------ $1 ≀ T ≀ 10^{3}$ $1 ≀ N, Q ≀ 10^{5}$ $0 ≀ A_{i} ≀ 10^{6}$ $1 ≀ L_{i} ≀ R_{i} ≀ N$ - The sum of $N$ over all test cases does not exceed $10^{6}$. - The sum of $Q$ over all test cases does not exceed $10^{5}$ ----- Sample Input 1 ------ 1 6 3 1 2 3 4 5 6 1 3 2 5 1 6 ----- Sample Output 1 ------ 1 2 10 ----- explanation 1 ------ - For the first query, we can choose $(1, 2, 3)$ since $A_{1} + A_{2} + A_{3} = 6$ is an even number. - For the second query, we can choose $(2, 3, 5)$ since $A_{2} + A_{3} + A_{5} = 10$ is even, and $(3, 4, 5)$ since $A_{3} + A_{4} + A_{5} = 12$ is even.
d = int(input()) for p in range(0, d): n, q = list(map(int, input().split())) a = list(map(int, input().split())) c = 0 d = 0 x = [0] y = [0] for j in range(n): if a[j] % 2 == 0: c = c + 1 else: d = d + 1 y.append(d) x.append(c) for i in range(q): l, r = list(map(int, input().split())) if r - l + 1 <= 2: print(0) else: k = x[r] - x[l - 1] h = y[r] - y[l - 1] print(k * (h * (h - 1)) // 2 + k * (k - 1) * (k - 2) // 6)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR 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 NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST NUMBER ASSIGN VAR LIST NUMBER FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF BIN_OP BIN_OP VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER BIN_OP BIN_OP BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER
Read problem statements in [Russian], [Mandarin Chinese], [Bengali], and [Vietnamese] as well. You are given an array A consisting of N integers and Q queries. Each query is described by two integers L and R. For each query, output the number of tuples (i, j, k) such that L≀ i< j< k≀ R and A_{i}+A_{j}+A_{k} is an even number. ------ Input Format ------ - 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 two integers N and Q. - The next line contains N integers A_{1},\ldots,A_{N}. - Then Q lines follow, each containing two integers L_{i} and R_{i}. ------ Output Format ------ For each query, output the number of tuples possible as mentioned in the problem statement. ------ Constraints ------ $1 ≀ T ≀ 10^{3}$ $1 ≀ N, Q ≀ 10^{5}$ $0 ≀ A_{i} ≀ 10^{6}$ $1 ≀ L_{i} ≀ R_{i} ≀ N$ - The sum of $N$ over all test cases does not exceed $10^{6}$. - The sum of $Q$ over all test cases does not exceed $10^{5}$ ----- Sample Input 1 ------ 1 6 3 1 2 3 4 5 6 1 3 2 5 1 6 ----- Sample Output 1 ------ 1 2 10 ----- explanation 1 ------ - For the first query, we can choose $(1, 2, 3)$ since $A_{1} + A_{2} + A_{3} = 6$ is an even number. - For the second query, we can choose $(2, 3, 5)$ since $A_{2} + A_{3} + A_{5} = 10$ is even, and $(3, 4, 5)$ since $A_{3} + A_{4} + A_{5} = 12$ is even.
for _ in range(int(input())): n, q = map(int, input().split()) arr = list(map(int, input().split())) prefix = [0] * (n + 1) c = 0 for i in range(n): if arr[i] & 1: pass else: c += 1 prefix[i + 1] = c for i in range(q): l, r = map(int, input().split()) e = prefix[r] - prefix[l - 1] m = r - l + 1 o = m - e ans = 0 if e >= 3: ans += e * (e - 1) * (e - 2) // 6 if o >= 2 and e > 0: ans += e * (o * (o - 1) // 2) 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 FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR NUMBER IF VAR NUMBER VAR BIN_OP BIN_OP BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER IF VAR NUMBER VAR NUMBER VAR BIN_OP VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR
Read problem statements in [Russian], [Mandarin Chinese], [Bengali], and [Vietnamese] as well. You are given an array A consisting of N integers and Q queries. Each query is described by two integers L and R. For each query, output the number of tuples (i, j, k) such that L≀ i< j< k≀ R and A_{i}+A_{j}+A_{k} is an even number. ------ Input Format ------ - 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 two integers N and Q. - The next line contains N integers A_{1},\ldots,A_{N}. - Then Q lines follow, each containing two integers L_{i} and R_{i}. ------ Output Format ------ For each query, output the number of tuples possible as mentioned in the problem statement. ------ Constraints ------ $1 ≀ T ≀ 10^{3}$ $1 ≀ N, Q ≀ 10^{5}$ $0 ≀ A_{i} ≀ 10^{6}$ $1 ≀ L_{i} ≀ R_{i} ≀ N$ - The sum of $N$ over all test cases does not exceed $10^{6}$. - The sum of $Q$ over all test cases does not exceed $10^{5}$ ----- Sample Input 1 ------ 1 6 3 1 2 3 4 5 6 1 3 2 5 1 6 ----- Sample Output 1 ------ 1 2 10 ----- explanation 1 ------ - For the first query, we can choose $(1, 2, 3)$ since $A_{1} + A_{2} + A_{3} = 6$ is an even number. - For the second query, we can choose $(2, 3, 5)$ since $A_{2} + A_{3} + A_{5} = 10$ is even, and $(3, 4, 5)$ since $A_{3} + A_{4} + A_{5} = 12$ is even.
t = int(input()) while t: n, q = map(int, input().split()) a = [int(x) for x in input().split()] final = [x for x in range(len(a))] j = 0 for i in a: if j == 0: final[j] = i % 2 else: final[j] = final[j - 1] + i % 2 j += 1 for _ in range(q): l, r = map(int, input().split()) fir = l - 1 las = r - 1 even = odd = 0 if fir == 0: odd = final[las] else: odd = final[las] - final[fir - 1] even = las - fir + 1 - odd ans = even * (even - 1) * (even - 2) / 6 + odd * (odd - 1) / 2 * even print(int(ans)) t -= 1
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR VAR IF VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER BIN_OP BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR NUMBER
Read problem statements in [Russian], [Mandarin Chinese], [Bengali], and [Vietnamese] as well. You are given an array A consisting of N integers and Q queries. Each query is described by two integers L and R. For each query, output the number of tuples (i, j, k) such that L≀ i< j< k≀ R and A_{i}+A_{j}+A_{k} is an even number. ------ Input Format ------ - 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 two integers N and Q. - The next line contains N integers A_{1},\ldots,A_{N}. - Then Q lines follow, each containing two integers L_{i} and R_{i}. ------ Output Format ------ For each query, output the number of tuples possible as mentioned in the problem statement. ------ Constraints ------ $1 ≀ T ≀ 10^{3}$ $1 ≀ N, Q ≀ 10^{5}$ $0 ≀ A_{i} ≀ 10^{6}$ $1 ≀ L_{i} ≀ R_{i} ≀ N$ - The sum of $N$ over all test cases does not exceed $10^{6}$. - The sum of $Q$ over all test cases does not exceed $10^{5}$ ----- Sample Input 1 ------ 1 6 3 1 2 3 4 5 6 1 3 2 5 1 6 ----- Sample Output 1 ------ 1 2 10 ----- explanation 1 ------ - For the first query, we can choose $(1, 2, 3)$ since $A_{1} + A_{2} + A_{3} = 6$ is an even number. - For the second query, we can choose $(2, 3, 5)$ since $A_{2} + A_{3} + A_{5} = 10$ is even, and $(3, 4, 5)$ since $A_{3} + A_{4} + A_{5} = 12$ is even.
t = int(input()) def f(x): if int(x) % 2 == 0: return 0 else: return 1 for i in range(t): n, q = map(int, input().split()) arr = list(map(f, input().split())) cumul = None arr2 = [0] for j in range(len(arr)): temp = arr[j] if cumul is None: cumul = temp else: cumul = cumul + temp arr2.append(cumul) for j in range(q): l, r = map(int, input().split()) nodd = arr2[r] - arr2[l - 1] neven = r - l + 1 - nodd res = neven * (neven - 1) * (neven - 2) / 6 res += nodd * (nodd - 1) * neven / 2 res = int(res) if res > 0 else 0 print(res)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_DEF IF BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER RETURN NUMBER RETURN NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR 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 NONE ASSIGN VAR LIST NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR IF VAR NONE ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER VAR BIN_OP BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
Read problem statements in [Russian], [Mandarin Chinese], [Bengali], and [Vietnamese] as well. You are given an array A consisting of N integers and Q queries. Each query is described by two integers L and R. For each query, output the number of tuples (i, j, k) such that L≀ i< j< k≀ R and A_{i}+A_{j}+A_{k} is an even number. ------ Input Format ------ - 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 two integers N and Q. - The next line contains N integers A_{1},\ldots,A_{N}. - Then Q lines follow, each containing two integers L_{i} and R_{i}. ------ Output Format ------ For each query, output the number of tuples possible as mentioned in the problem statement. ------ Constraints ------ $1 ≀ T ≀ 10^{3}$ $1 ≀ N, Q ≀ 10^{5}$ $0 ≀ A_{i} ≀ 10^{6}$ $1 ≀ L_{i} ≀ R_{i} ≀ N$ - The sum of $N$ over all test cases does not exceed $10^{6}$. - The sum of $Q$ over all test cases does not exceed $10^{5}$ ----- Sample Input 1 ------ 1 6 3 1 2 3 4 5 6 1 3 2 5 1 6 ----- Sample Output 1 ------ 1 2 10 ----- explanation 1 ------ - For the first query, we can choose $(1, 2, 3)$ since $A_{1} + A_{2} + A_{3} = 6$ is an even number. - For the second query, we can choose $(2, 3, 5)$ since $A_{2} + A_{3} + A_{5} = 10$ is even, and $(3, 4, 5)$ since $A_{3} + A_{4} + A_{5} = 12$ is even.
t = int(input()) for i in range(t): n, q = tuple(map(int, input().split())) array = list(map(int, input().split())) if array[0] % 2 == 0: array[0] = 1 else: array[0] = 0 for w in range(1, n): if array[w] % 2 == 0: array[w] = array[w - 1] + 1 else: array[w] = array[w - 1] for j in range(q): l, r = tuple(map(int, input().split())) if l == 1: even = array[r - 1] else: even = array[r - 1] - array[l - 2] odd = r - l + 1 - even count = 0 count += even * (even - 1) * (even - 2) // 6 count += odd * (odd - 1) * even // 2 print(count)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR 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 IF BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR NUMBER NUMBER ASSIGN VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER VAR ASSIGN VAR NUMBER VAR BIN_OP BIN_OP BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER VAR BIN_OP BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR
Read problem statements in [Russian], [Mandarin Chinese], [Bengali], and [Vietnamese] as well. You are given an array A consisting of N integers and Q queries. Each query is described by two integers L and R. For each query, output the number of tuples (i, j, k) such that L≀ i< j< k≀ R and A_{i}+A_{j}+A_{k} is an even number. ------ Input Format ------ - 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 two integers N and Q. - The next line contains N integers A_{1},\ldots,A_{N}. - Then Q lines follow, each containing two integers L_{i} and R_{i}. ------ Output Format ------ For each query, output the number of tuples possible as mentioned in the problem statement. ------ Constraints ------ $1 ≀ T ≀ 10^{3}$ $1 ≀ N, Q ≀ 10^{5}$ $0 ≀ A_{i} ≀ 10^{6}$ $1 ≀ L_{i} ≀ R_{i} ≀ N$ - The sum of $N$ over all test cases does not exceed $10^{6}$. - The sum of $Q$ over all test cases does not exceed $10^{5}$ ----- Sample Input 1 ------ 1 6 3 1 2 3 4 5 6 1 3 2 5 1 6 ----- Sample Output 1 ------ 1 2 10 ----- explanation 1 ------ - For the first query, we can choose $(1, 2, 3)$ since $A_{1} + A_{2} + A_{3} = 6$ is an even number. - For the second query, we can choose $(2, 3, 5)$ since $A_{2} + A_{3} + A_{5} = 10$ is even, and $(3, 4, 5)$ since $A_{3} + A_{4} + A_{5} = 12$ is even.
ans = "" def nC3(n): return int(n * (n - 1) * (n - 2) / 6) def nC2(n): return int(n * (n - 1) / 2) for _ in range(int(input())): n, q = list(map(int, input().split())) a = list(map(int, input().split())) pre = [0] * n pre[0] = 1 if a[0] % 2 == 0 else 0 for i in range(1, n): if a[i] % 2 == 0: pre[i] += 1 pre[i] += pre[i - 1] for i in range(q): l, r = list(map(int, input().split())) even = pre[r - 1] - pre[l - 1] + (1 if a[l - 1] % 2 == 0 else 0) odd = r - l + 1 - even res = nC3(even) + nC2(odd) * even ans += str(res) + "\n" print(ans)
ASSIGN VAR STRING FUNC_DEF RETURN FUNC_CALL VAR BIN_OP BIN_OP BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER FUNC_DEF RETURN FUNC_CALL VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER 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 FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER BIN_OP VAR NUMBER NUMBER NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF BIN_OP VAR VAR NUMBER NUMBER VAR VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER NUMBER NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR VAR VAR VAR BIN_OP FUNC_CALL VAR VAR STRING EXPR FUNC_CALL VAR VAR
Read problem statements in [Russian], [Mandarin Chinese], [Bengali], and [Vietnamese] as well. You are given an array A consisting of N integers and Q queries. Each query is described by two integers L and R. For each query, output the number of tuples (i, j, k) such that L≀ i< j< k≀ R and A_{i}+A_{j}+A_{k} is an even number. ------ Input Format ------ - 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 two integers N and Q. - The next line contains N integers A_{1},\ldots,A_{N}. - Then Q lines follow, each containing two integers L_{i} and R_{i}. ------ Output Format ------ For each query, output the number of tuples possible as mentioned in the problem statement. ------ Constraints ------ $1 ≀ T ≀ 10^{3}$ $1 ≀ N, Q ≀ 10^{5}$ $0 ≀ A_{i} ≀ 10^{6}$ $1 ≀ L_{i} ≀ R_{i} ≀ N$ - The sum of $N$ over all test cases does not exceed $10^{6}$. - The sum of $Q$ over all test cases does not exceed $10^{5}$ ----- Sample Input 1 ------ 1 6 3 1 2 3 4 5 6 1 3 2 5 1 6 ----- Sample Output 1 ------ 1 2 10 ----- explanation 1 ------ - For the first query, we can choose $(1, 2, 3)$ since $A_{1} + A_{2} + A_{3} = 6$ is an even number. - For the second query, we can choose $(2, 3, 5)$ since $A_{2} + A_{3} + A_{5} = 10$ is even, and $(3, 4, 5)$ since $A_{3} + A_{4} + A_{5} = 12$ is even.
for _ in range(int(input())): N, Q = list(map(int, input().split())) A = list(map(int, input().split())) even = [0] * N odd = [0] * N for i in range(N): even[i] += even[max(i - 1, 0)] odd[i] += odd[max(i - 1, 0)] if i != N - 1: if A[i] % 2: odd[i + 1] += 1 else: even[i + 1] += 1 for i in range(Q): L, R = list(map(int, input().split())) L -= 1 R -= 1 num_evens = even[R] - even[L] + 1 * (not A[R] % 2) num_odds = odd[R] - odd[L] + 1 * (A[R] % 2) choose2 = num_odds * (num_odds - 1) // 2 * num_evens choose3 = num_evens * (num_evens - 1) * (num_evens - 2) // 6 print(choose2 + choose3)
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 FUNC_CALL 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 VAR VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER VAR VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER IF VAR BIN_OP VAR NUMBER IF BIN_OP VAR VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR VAR BIN_OP NUMBER BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR VAR BIN_OP NUMBER BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR
Read problem statements in [Russian], [Mandarin Chinese], [Bengali], and [Vietnamese] as well. You are given an array A consisting of N integers and Q queries. Each query is described by two integers L and R. For each query, output the number of tuples (i, j, k) such that L≀ i< j< k≀ R and A_{i}+A_{j}+A_{k} is an even number. ------ Input Format ------ - 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 two integers N and Q. - The next line contains N integers A_{1},\ldots,A_{N}. - Then Q lines follow, each containing two integers L_{i} and R_{i}. ------ Output Format ------ For each query, output the number of tuples possible as mentioned in the problem statement. ------ Constraints ------ $1 ≀ T ≀ 10^{3}$ $1 ≀ N, Q ≀ 10^{5}$ $0 ≀ A_{i} ≀ 10^{6}$ $1 ≀ L_{i} ≀ R_{i} ≀ N$ - The sum of $N$ over all test cases does not exceed $10^{6}$. - The sum of $Q$ over all test cases does not exceed $10^{5}$ ----- Sample Input 1 ------ 1 6 3 1 2 3 4 5 6 1 3 2 5 1 6 ----- Sample Output 1 ------ 1 2 10 ----- explanation 1 ------ - For the first query, we can choose $(1, 2, 3)$ since $A_{1} + A_{2} + A_{3} = 6$ is an even number. - For the second query, we can choose $(2, 3, 5)$ since $A_{2} + A_{3} + A_{5} = 10$ is even, and $(3, 4, 5)$ since $A_{3} + A_{4} + A_{5} = 12$ is even.
def binomialCoefficient(n, k): if k > n - k: k = n - k res = 1 for i in range(k): res = res * (n - i) res = res // (i + 1) return res t = int(input()) for _ in range(t): n, q = map(int, input().split()) arr = list(map(int, input().split())) queries = [] for _ in range(q): l, r = map(int, input().split()) queries.append([l, r]) even = [0] * len(arr) odd = [0] * len(arr) n = len(arr) for i in range(len(arr)): if arr[i] % 2 == 0: even[i] = 1 else: odd[i] = 1 prefixSum_odd = [(0) for i in range(n + 1)] prefixSum_odd[0] = odd[0] for i in range(1, n): prefixSum_odd[i] = prefixSum_odd[i - 1] + odd[i] prefixSum_even = [(0) for i in range(n + 1)] prefixSum_even[0] = even[0] for i in range(1, n): prefixSum_even[i] = prefixSum_even[i - 1] + even[i] for query in queries: a = prefixSum_even[query[1] - 1] - prefixSum_even[query[0] - 2] b = prefixSum_odd[query[1] - 1] - prefixSum_odd[query[0] - 2] term1 = 0 term2 = 0 term3 = 0 if a >= 3: term1 = binomialCoefficient(a, 3) if a >= 1: term2 = binomialCoefficient(a, 1) if b >= 2: term3 = binomialCoefficient(b, 2) result = term1 + term2 * term3 print(result)
FUNC_DEF IF VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER RETURN VAR 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 FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR 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 BIN_OP LIST NUMBER FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR FOR VAR VAR 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 NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR
Read problem statements in [Russian], [Mandarin Chinese], [Bengali], and [Vietnamese] as well. You are given an array A consisting of N integers and Q queries. Each query is described by two integers L and R. For each query, output the number of tuples (i, j, k) such that L≀ i< j< k≀ R and A_{i}+A_{j}+A_{k} is an even number. ------ Input Format ------ - 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 two integers N and Q. - The next line contains N integers A_{1},\ldots,A_{N}. - Then Q lines follow, each containing two integers L_{i} and R_{i}. ------ Output Format ------ For each query, output the number of tuples possible as mentioned in the problem statement. ------ Constraints ------ $1 ≀ T ≀ 10^{3}$ $1 ≀ N, Q ≀ 10^{5}$ $0 ≀ A_{i} ≀ 10^{6}$ $1 ≀ L_{i} ≀ R_{i} ≀ N$ - The sum of $N$ over all test cases does not exceed $10^{6}$. - The sum of $Q$ over all test cases does not exceed $10^{5}$ ----- Sample Input 1 ------ 1 6 3 1 2 3 4 5 6 1 3 2 5 1 6 ----- Sample Output 1 ------ 1 2 10 ----- explanation 1 ------ - For the first query, we can choose $(1, 2, 3)$ since $A_{1} + A_{2} + A_{3} = 6$ is an even number. - For the second query, we can choose $(2, 3, 5)$ since $A_{2} + A_{3} + A_{5} = 10$ is even, and $(3, 4, 5)$ since $A_{3} + A_{4} + A_{5} = 12$ is even.
def fact(n, m): if n == m: return 1 return n * fact(n - 1, m) T = int(input()) while T > 0: n, q = input().split() n = int(n) q = int(q) arr = input().split() final = [x for x in range(len(arr))] j = 0 for i in arr: i = int(i) if j == 0: final[j] = i % 2 else: final[j] = final[j - 1] + i % 2 j += 1 for i in range(q): l, r = input().split() l = int(l) - 1 r = int(r) - 1 if l == 0: odd = final[r] else: odd = final[r] - final[l - 1] even = r - l + 1 - odd ans1 = ans2 = ans = 0 if even > 2: ans1 = fact(even, even - 3) / 6 if even > 0 and odd > 1: ans2 = fact(odd, odd - 2) / 2 * even ans = ans1 + ans2 print(int(ans)) T -= 1
FUNC_DEF IF VAR VAR RETURN NUMBER RETURN BIN_OP VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR NUMBER ASSIGN VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER VAR ASSIGN VAR VAR VAR NUMBER IF VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER IF VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP BIN_OP FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER VAR ASSIGN VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR NUMBER
Read problem statements in [Russian], [Mandarin Chinese], [Bengali], and [Vietnamese] as well. You are given an array A consisting of N integers and Q queries. Each query is described by two integers L and R. For each query, output the number of tuples (i, j, k) such that L≀ i< j< k≀ R and A_{i}+A_{j}+A_{k} is an even number. ------ Input Format ------ - 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 two integers N and Q. - The next line contains N integers A_{1},\ldots,A_{N}. - Then Q lines follow, each containing two integers L_{i} and R_{i}. ------ Output Format ------ For each query, output the number of tuples possible as mentioned in the problem statement. ------ Constraints ------ $1 ≀ T ≀ 10^{3}$ $1 ≀ N, Q ≀ 10^{5}$ $0 ≀ A_{i} ≀ 10^{6}$ $1 ≀ L_{i} ≀ R_{i} ≀ N$ - The sum of $N$ over all test cases does not exceed $10^{6}$. - The sum of $Q$ over all test cases does not exceed $10^{5}$ ----- Sample Input 1 ------ 1 6 3 1 2 3 4 5 6 1 3 2 5 1 6 ----- Sample Output 1 ------ 1 2 10 ----- explanation 1 ------ - For the first query, we can choose $(1, 2, 3)$ since $A_{1} + A_{2} + A_{3} = 6$ is an even number. - For the second query, we can choose $(2, 3, 5)$ since $A_{2} + A_{3} + A_{5} = 10$ is even, and $(3, 4, 5)$ since $A_{3} + A_{4} + A_{5} = 12$ is even.
dp = [[(0) for j in range(4)] for i in range(50001)] for i in range(50001): for j in range(4): if i == j or j == 0: dp[i][j] = 1 else: dp[i][j] = dp[i - 1][j - 1] + dp[i - 1][j] for _ in range(int(input())): n, q = map(int, input().split()) a = [int(i) for i in input().split()] even = [0] * (n + 1) for i in range(1, n + 1): if a[i - 1] & 1: even[i] = even[i - 1] else: even[i] = even[i - 1] + 1 for i in range(q): l, r = map(int, input().split()) ecnt = even[r] - even[l - 1] ocnt = r - l + 1 - ecnt print(dp[ecnt][3] + dp[ecnt][1] * dp[ocnt][2])
ASSIGN VAR NUMBER VAR FUNC_CALL VAR NUMBER VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER IF VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR 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 FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF BIN_OP VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR NUMBER BIN_OP VAR VAR NUMBER VAR VAR NUMBER
Read problem statements in [Russian], [Mandarin Chinese], [Bengali], and [Vietnamese] as well. You are given an array A consisting of N integers and Q queries. Each query is described by two integers L and R. For each query, output the number of tuples (i, j, k) such that L≀ i< j< k≀ R and A_{i}+A_{j}+A_{k} is an even number. ------ Input Format ------ - 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 two integers N and Q. - The next line contains N integers A_{1},\ldots,A_{N}. - Then Q lines follow, each containing two integers L_{i} and R_{i}. ------ Output Format ------ For each query, output the number of tuples possible as mentioned in the problem statement. ------ Constraints ------ $1 ≀ T ≀ 10^{3}$ $1 ≀ N, Q ≀ 10^{5}$ $0 ≀ A_{i} ≀ 10^{6}$ $1 ≀ L_{i} ≀ R_{i} ≀ N$ - The sum of $N$ over all test cases does not exceed $10^{6}$. - The sum of $Q$ over all test cases does not exceed $10^{5}$ ----- Sample Input 1 ------ 1 6 3 1 2 3 4 5 6 1 3 2 5 1 6 ----- Sample Output 1 ------ 1 2 10 ----- explanation 1 ------ - For the first query, we can choose $(1, 2, 3)$ since $A_{1} + A_{2} + A_{3} = 6$ is an even number. - For the second query, we can choose $(2, 3, 5)$ since $A_{2} + A_{3} + A_{5} = 10$ is even, and $(3, 4, 5)$ since $A_{3} + A_{4} + A_{5} = 12$ is even.
t = int(input()) for _ in range(t): n, q = list(map(int, input().split())) arr = list(map(int, input().split())) arr = [(item & 1) for item in arr] cum = 0 for i in range(len(arr)): cum += arr[i] arr[i] = cum for i in range(q): l, r = list(map(int, input().split())) tot = r - l + 1 if l - 1 == 0: odd = arr[r - 1] else: odd = arr[r - 1] - arr[l - 2] even = tot - odd k1 = odd * (odd - 1) // 2 k2 = even * (even - 1) * (even - 2) // 6 print(even * k1 + k2)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR 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 BIN_OP VAR NUMBER VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR VAR
Read problem statements in [Russian], [Mandarin Chinese], [Bengali], and [Vietnamese] as well. You are given an array A consisting of N integers and Q queries. Each query is described by two integers L and R. For each query, output the number of tuples (i, j, k) such that L≀ i< j< k≀ R and A_{i}+A_{j}+A_{k} is an even number. ------ Input Format ------ - 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 two integers N and Q. - The next line contains N integers A_{1},\ldots,A_{N}. - Then Q lines follow, each containing two integers L_{i} and R_{i}. ------ Output Format ------ For each query, output the number of tuples possible as mentioned in the problem statement. ------ Constraints ------ $1 ≀ T ≀ 10^{3}$ $1 ≀ N, Q ≀ 10^{5}$ $0 ≀ A_{i} ≀ 10^{6}$ $1 ≀ L_{i} ≀ R_{i} ≀ N$ - The sum of $N$ over all test cases does not exceed $10^{6}$. - The sum of $Q$ over all test cases does not exceed $10^{5}$ ----- Sample Input 1 ------ 1 6 3 1 2 3 4 5 6 1 3 2 5 1 6 ----- Sample Output 1 ------ 1 2 10 ----- explanation 1 ------ - For the first query, we can choose $(1, 2, 3)$ since $A_{1} + A_{2} + A_{3} = 6$ is an even number. - For the second query, we can choose $(2, 3, 5)$ since $A_{2} + A_{3} + A_{5} = 10$ is even, and $(3, 4, 5)$ since $A_{3} + A_{4} + A_{5} = 12$ is even.
def NC3(x): if x < 3: return 0 elif x == 3: return 1 else: return x * (x - 1) * (x - 2) // 6 def NC2(x): if x < 2: return 0 elif x == 2: return 1 else: return x * (x - 1) // 2 for _ in range(int(input())): n, q = map(int, input().split()) arr = list(map(int, input().split())) even = [0] for i in range(n): if arr[i] % 2 == 0: even.append(even[i] + 1) else: even.append(even[i]) for i in range(q): q1, q2 = map(int, input().split()) e = even[q2] - even[q1 - 1] o = q2 - q1 + 1 - e print(NC3(e) + NC2(o) * e)
FUNC_DEF IF VAR NUMBER RETURN NUMBER IF VAR NUMBER RETURN NUMBER RETURN BIN_OP BIN_OP BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER FUNC_DEF IF VAR NUMBER RETURN NUMBER IF VAR NUMBER RETURN NUMBER RETURN BIN_OP BIN_OP VAR BIN_OP VAR NUMBER 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 FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST NUMBER FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER VAR EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR VAR VAR
Read problem statements in [Russian], [Mandarin Chinese], [Bengali], and [Vietnamese] as well. You are given an array A consisting of N integers and Q queries. Each query is described by two integers L and R. For each query, output the number of tuples (i, j, k) such that L≀ i< j< k≀ R and A_{i}+A_{j}+A_{k} is an even number. ------ Input Format ------ - 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 two integers N and Q. - The next line contains N integers A_{1},\ldots,A_{N}. - Then Q lines follow, each containing two integers L_{i} and R_{i}. ------ Output Format ------ For each query, output the number of tuples possible as mentioned in the problem statement. ------ Constraints ------ $1 ≀ T ≀ 10^{3}$ $1 ≀ N, Q ≀ 10^{5}$ $0 ≀ A_{i} ≀ 10^{6}$ $1 ≀ L_{i} ≀ R_{i} ≀ N$ - The sum of $N$ over all test cases does not exceed $10^{6}$. - The sum of $Q$ over all test cases does not exceed $10^{5}$ ----- Sample Input 1 ------ 1 6 3 1 2 3 4 5 6 1 3 2 5 1 6 ----- Sample Output 1 ------ 1 2 10 ----- explanation 1 ------ - For the first query, we can choose $(1, 2, 3)$ since $A_{1} + A_{2} + A_{3} = 6$ is an even number. - For the second query, we can choose $(2, 3, 5)$ since $A_{2} + A_{3} + A_{5} = 10$ is even, and $(3, 4, 5)$ since $A_{3} + A_{4} + A_{5} = 12$ is even.
def NC3(x): if x < 3: return 0 elif x == 3: return 1 else: return x * (x - 1) * (x - 2) // 6 def NC2(x): if x < 2: return 0 elif x == 2: return 1 else: return x * (x - 1) // 2 t = int(input()) for _ in range(t): n, q = [int(x) for x in input().split()] nums = [int(x) for x in input().split()] even_dp = [0] * len(nums) for i, n in enumerate(nums): if i == 0: even_dp[i] = 0 + (n % 2 == 0) else: even_dp[i] = even_dp[i - 1] + (n % 2 == 0) for _ in range(q): l, r = [(int(x) - 1) for x in input().split()] even = even_dp[r] - (0 if not l else even_dp[l - 1]) odd = r - l + 1 - even res = NC3(even) + NC2(odd) * even print(res)
FUNC_DEF IF VAR NUMBER RETURN NUMBER IF VAR NUMBER RETURN NUMBER RETURN BIN_OP BIN_OP BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER FUNC_DEF IF VAR NUMBER RETURN NUMBER IF VAR NUMBER RETURN NUMBER RETURN BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR 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 FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR VAR FOR VAR VAR FUNC_CALL VAR VAR IF VAR NUMBER ASSIGN VAR VAR BIN_OP NUMBER BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP VAR VAR VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR
Read problem statements in [Russian], [Mandarin Chinese], [Bengali], and [Vietnamese] as well. You are given an array A consisting of N integers and Q queries. Each query is described by two integers L and R. For each query, output the number of tuples (i, j, k) such that L≀ i< j< k≀ R and A_{i}+A_{j}+A_{k} is an even number. ------ Input Format ------ - 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 two integers N and Q. - The next line contains N integers A_{1},\ldots,A_{N}. - Then Q lines follow, each containing two integers L_{i} and R_{i}. ------ Output Format ------ For each query, output the number of tuples possible as mentioned in the problem statement. ------ Constraints ------ $1 ≀ T ≀ 10^{3}$ $1 ≀ N, Q ≀ 10^{5}$ $0 ≀ A_{i} ≀ 10^{6}$ $1 ≀ L_{i} ≀ R_{i} ≀ N$ - The sum of $N$ over all test cases does not exceed $10^{6}$. - The sum of $Q$ over all test cases does not exceed $10^{5}$ ----- Sample Input 1 ------ 1 6 3 1 2 3 4 5 6 1 3 2 5 1 6 ----- Sample Output 1 ------ 1 2 10 ----- explanation 1 ------ - For the first query, we can choose $(1, 2, 3)$ since $A_{1} + A_{2} + A_{3} = 6$ is an even number. - For the second query, we can choose $(2, 3, 5)$ since $A_{2} + A_{3} + A_{5} = 10$ is even, and $(3, 4, 5)$ since $A_{3} + A_{4} + A_{5} = 12$ is even.
for _ in range(int(input())): n, q = map(int, input().split()) ar = list(map(int, input().split())) op = ar[:] ep = ar[:] if ar[0] % 2 == 0: ep[0] = 1 op[0] = 0 else: op[0] = 1 ep[0] = 0 for i in range(1, n): if ar[i] % 2 == 0: ep[i] = ep[i - 1] + 1 op[i] = op[i - 1] else: op[i] = op[i - 1] + 1 ep[i] = ep[i - 1] ep.insert(0, 0) op.insert(0, 0) for i in range(q): l, r = map(int, input().split()) nop = op[r] - op[l - 1] noe = ep[r] - ep[l - 1] ctr = 0 if noe >= 3: ctr += noe * (noe - 1) * (noe - 2) // 6 if nop >= 2 and noe >= 1: ctr += noe * (nop * (nop - 1) // 2) print(ctr)
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 FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR ASSIGN VAR VAR IF BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR NUMBER NUMBER ASSIGN VAR NUMBER NUMBER ASSIGN VAR NUMBER NUMBER ASSIGN VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR 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 EXPR FUNC_CALL VAR NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL 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 NUMBER IF VAR NUMBER VAR BIN_OP BIN_OP BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER IF VAR NUMBER VAR NUMBER VAR BIN_OP VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR
Read problem statements in [Russian], [Mandarin Chinese], [Bengali], and [Vietnamese] as well. You are given an array A consisting of N integers and Q queries. Each query is described by two integers L and R. For each query, output the number of tuples (i, j, k) such that L≀ i< j< k≀ R and A_{i}+A_{j}+A_{k} is an even number. ------ Input Format ------ - 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 two integers N and Q. - The next line contains N integers A_{1},\ldots,A_{N}. - Then Q lines follow, each containing two integers L_{i} and R_{i}. ------ Output Format ------ For each query, output the number of tuples possible as mentioned in the problem statement. ------ Constraints ------ $1 ≀ T ≀ 10^{3}$ $1 ≀ N, Q ≀ 10^{5}$ $0 ≀ A_{i} ≀ 10^{6}$ $1 ≀ L_{i} ≀ R_{i} ≀ N$ - The sum of $N$ over all test cases does not exceed $10^{6}$. - The sum of $Q$ over all test cases does not exceed $10^{5}$ ----- Sample Input 1 ------ 1 6 3 1 2 3 4 5 6 1 3 2 5 1 6 ----- Sample Output 1 ------ 1 2 10 ----- explanation 1 ------ - For the first query, we can choose $(1, 2, 3)$ since $A_{1} + A_{2} + A_{3} = 6$ is an even number. - For the second query, we can choose $(2, 3, 5)$ since $A_{2} + A_{3} + A_{5} = 10$ is even, and $(3, 4, 5)$ since $A_{3} + A_{4} + A_{5} = 12$ is even.
def evenTuples(): for _ in range(int(input())): N, Q = [int(x) for x in input().split()] A = [int(x) for x in input().split()] odds, evens = [0], [0] for a in A: if a % 2 is 0: evens.append(evens[-1] + 1) odds.append(odds[-1]) else: odds.append(odds[-1] + 1) evens.append(evens[-1]) LR = [[int(x) for x in input().split()] for i in range(Q)] for L, R in LR: odd = odds[R] - odds[L - 1] even = evens[R] - evens[L - 1] ans = even * odd * (odd - 1) // 2 + even * (even - 1) * (even - 2) // 6 print(ans) evenTuples()
FUNC_DEF 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 FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR LIST NUMBER LIST NUMBER FOR VAR VAR IF BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR VAR FUNC_CALL VAR VAR 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 BIN_OP BIN_OP BIN_OP VAR VAR BIN_OP VAR NUMBER NUMBER BIN_OP BIN_OP BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR
Read problem statements in [Russian], [Mandarin Chinese], [Bengali], and [Vietnamese] as well. You are given an array A consisting of N integers and Q queries. Each query is described by two integers L and R. For each query, output the number of tuples (i, j, k) such that L≀ i< j< k≀ R and A_{i}+A_{j}+A_{k} is an even number. ------ Input Format ------ - 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 two integers N and Q. - The next line contains N integers A_{1},\ldots,A_{N}. - Then Q lines follow, each containing two integers L_{i} and R_{i}. ------ Output Format ------ For each query, output the number of tuples possible as mentioned in the problem statement. ------ Constraints ------ $1 ≀ T ≀ 10^{3}$ $1 ≀ N, Q ≀ 10^{5}$ $0 ≀ A_{i} ≀ 10^{6}$ $1 ≀ L_{i} ≀ R_{i} ≀ N$ - The sum of $N$ over all test cases does not exceed $10^{6}$. - The sum of $Q$ over all test cases does not exceed $10^{5}$ ----- Sample Input 1 ------ 1 6 3 1 2 3 4 5 6 1 3 2 5 1 6 ----- Sample Output 1 ------ 1 2 10 ----- explanation 1 ------ - For the first query, we can choose $(1, 2, 3)$ since $A_{1} + A_{2} + A_{3} = 6$ is an even number. - For the second query, we can choose $(2, 3, 5)$ since $A_{2} + A_{3} + A_{5} = 10$ is even, and $(3, 4, 5)$ since $A_{3} + A_{4} + A_{5} = 12$ is even.
def NC3(x): if x < 3: return 0 elif x == 3: return 1 else: return x * (x - 1) * (x - 2) // 6 def NC2(x): if x < 2: return 0 elif x == 2: return 1 else: return x * (x - 1) // 2 t = int(input()) while t: t -= 1 n, q = map(int, input().split()) a = list(map(int, input().split())) odds = [0] evens = [0] for i in a: if i & 1: odds.append(odds[-1] + 1) evens.append(evens[-1]) else: odds.append(odds[-1]) evens.append(evens[-1] + 1) for i in range(q): query = list(map(int, input().split())) even = evens[query[1]] - evens[query[0] - 1] odd = odds[query[1]] - odds[query[0] - 1] sol = NC3(even) + NC2(odd) * even print(sol)
FUNC_DEF IF VAR NUMBER RETURN NUMBER IF VAR NUMBER RETURN NUMBER RETURN BIN_OP BIN_OP BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER FUNC_DEF IF VAR NUMBER RETURN NUMBER IF VAR NUMBER RETURN NUMBER RETURN BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR VAR NUMBER ASSIGN VAR 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 NUMBER ASSIGN VAR LIST NUMBER FOR VAR VAR IF BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP VAR VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR
Read problem statements in [Russian], [Mandarin Chinese], [Bengali], and [Vietnamese] as well. You are given an array A consisting of N integers and Q queries. Each query is described by two integers L and R. For each query, output the number of tuples (i, j, k) such that L≀ i< j< k≀ R and A_{i}+A_{j}+A_{k} is an even number. ------ Input Format ------ - 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 two integers N and Q. - The next line contains N integers A_{1},\ldots,A_{N}. - Then Q lines follow, each containing two integers L_{i} and R_{i}. ------ Output Format ------ For each query, output the number of tuples possible as mentioned in the problem statement. ------ Constraints ------ $1 ≀ T ≀ 10^{3}$ $1 ≀ N, Q ≀ 10^{5}$ $0 ≀ A_{i} ≀ 10^{6}$ $1 ≀ L_{i} ≀ R_{i} ≀ N$ - The sum of $N$ over all test cases does not exceed $10^{6}$. - The sum of $Q$ over all test cases does not exceed $10^{5}$ ----- Sample Input 1 ------ 1 6 3 1 2 3 4 5 6 1 3 2 5 1 6 ----- Sample Output 1 ------ 1 2 10 ----- explanation 1 ------ - For the first query, we can choose $(1, 2, 3)$ since $A_{1} + A_{2} + A_{3} = 6$ is an even number. - For the second query, we can choose $(2, 3, 5)$ since $A_{2} + A_{3} + A_{5} = 10$ is even, and $(3, 4, 5)$ since $A_{3} + A_{4} + A_{5} = 12$ is even.
T = int(input()) for i in range(T): n, Q = [int(i) for i in input().split()] arr = [int(i) for i in input().split()] arrE = [] count = 0 arrE.append(count) for i in range(len(arr)): if arr[i] % 2 == 0: count = count + 1 arrE.append(count) else: arrE.append(count) for i in range(Q): L, R = [int(i) for i in input().split()] e = arrE[R] - arrE[L - 1] o = R - L + 1 - e eC = e * (e - 1) * (e - 2) // 6 oC = o * (o - 1) // 2 * e print(eC + oC)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR 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 FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR
Read problem statements in [Russian], [Mandarin Chinese], [Bengali], and [Vietnamese] as well. You are given an array A consisting of N integers and Q queries. Each query is described by two integers L and R. For each query, output the number of tuples (i, j, k) such that L≀ i< j< k≀ R and A_{i}+A_{j}+A_{k} is an even number. ------ Input Format ------ - 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 two integers N and Q. - The next line contains N integers A_{1},\ldots,A_{N}. - Then Q lines follow, each containing two integers L_{i} and R_{i}. ------ Output Format ------ For each query, output the number of tuples possible as mentioned in the problem statement. ------ Constraints ------ $1 ≀ T ≀ 10^{3}$ $1 ≀ N, Q ≀ 10^{5}$ $0 ≀ A_{i} ≀ 10^{6}$ $1 ≀ L_{i} ≀ R_{i} ≀ N$ - The sum of $N$ over all test cases does not exceed $10^{6}$. - The sum of $Q$ over all test cases does not exceed $10^{5}$ ----- Sample Input 1 ------ 1 6 3 1 2 3 4 5 6 1 3 2 5 1 6 ----- Sample Output 1 ------ 1 2 10 ----- explanation 1 ------ - For the first query, we can choose $(1, 2, 3)$ since $A_{1} + A_{2} + A_{3} = 6$ is an even number. - For the second query, we can choose $(2, 3, 5)$ since $A_{2} + A_{3} + A_{5} = 10$ is even, and $(3, 4, 5)$ since $A_{3} + A_{4} + A_{5} = 12$ is even.
from sys import stdin, stdout test = int(stdin.readline().rstrip()) while test > 0: n, q = map(int, stdin.readline().rstrip().split()) array = list(map(int, stdin.readline().rstrip().split())) lst = [] c = 0 for i in range(len(array)): if array[i] % 2 == 0: c += 1 lst.append(c) while q > 0: a, b = map(int, stdin.readline().rstrip().split()) c = lst[b - 1] - lst[a - 1] if array[a - 1] % 2 == 0: c += 1 o = b - a + 1 - c x = o * (o - 1) // 2 * c y = c * (c - 1) * (c - 2) // 6 print(x + y) q -= 1 test -= 1
ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR WHILE VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR NUMBER NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR WHILE VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER IF BIN_OP VAR BIN_OP VAR NUMBER NUMBER NUMBER VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR NUMBER VAR NUMBER
Read problem statements in [Russian], [Mandarin Chinese], [Bengali], and [Vietnamese] as well. You are given an array A consisting of N integers and Q queries. Each query is described by two integers L and R. For each query, output the number of tuples (i, j, k) such that L≀ i< j< k≀ R and A_{i}+A_{j}+A_{k} is an even number. ------ Input Format ------ - 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 two integers N and Q. - The next line contains N integers A_{1},\ldots,A_{N}. - Then Q lines follow, each containing two integers L_{i} and R_{i}. ------ Output Format ------ For each query, output the number of tuples possible as mentioned in the problem statement. ------ Constraints ------ $1 ≀ T ≀ 10^{3}$ $1 ≀ N, Q ≀ 10^{5}$ $0 ≀ A_{i} ≀ 10^{6}$ $1 ≀ L_{i} ≀ R_{i} ≀ N$ - The sum of $N$ over all test cases does not exceed $10^{6}$. - The sum of $Q$ over all test cases does not exceed $10^{5}$ ----- Sample Input 1 ------ 1 6 3 1 2 3 4 5 6 1 3 2 5 1 6 ----- Sample Output 1 ------ 1 2 10 ----- explanation 1 ------ - For the first query, we can choose $(1, 2, 3)$ since $A_{1} + A_{2} + A_{3} = 6$ is an even number. - For the second query, we can choose $(2, 3, 5)$ since $A_{2} + A_{3} + A_{5} = 10$ is even, and $(3, 4, 5)$ since $A_{3} + A_{4} + A_{5} = 12$ is even.
for _ in range(int(input())): n, q = map(int, input().split()) arr = list(map(int, input().split())) c1 = 0 temp = [] even = [] for i in arr: if i % 2 == 0: temp.append(1) else: temp.append(0) even.append(0) for i in temp: if i == 1: c1 += 1 even.append(c1) for _ in range(q): l, r = map(int, input().split()) noofevens = even[r] - even[l - 1] noofodds = r - l + 1 - noofevens print( noofevens * (noofevens - 1) * (noofevens - 2) // 6 + noofodds * (noofodds - 1) // 2 * noofevens )
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 FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR VAR IF BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER FOR VAR VAR IF VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER BIN_OP BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR
Read problem statements in [Russian], [Mandarin Chinese], [Bengali], and [Vietnamese] as well. You are given an array A consisting of N integers and Q queries. Each query is described by two integers L and R. For each query, output the number of tuples (i, j, k) such that L≀ i< j< k≀ R and A_{i}+A_{j}+A_{k} is an even number. ------ Input Format ------ - 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 two integers N and Q. - The next line contains N integers A_{1},\ldots,A_{N}. - Then Q lines follow, each containing two integers L_{i} and R_{i}. ------ Output Format ------ For each query, output the number of tuples possible as mentioned in the problem statement. ------ Constraints ------ $1 ≀ T ≀ 10^{3}$ $1 ≀ N, Q ≀ 10^{5}$ $0 ≀ A_{i} ≀ 10^{6}$ $1 ≀ L_{i} ≀ R_{i} ≀ N$ - The sum of $N$ over all test cases does not exceed $10^{6}$. - The sum of $Q$ over all test cases does not exceed $10^{5}$ ----- Sample Input 1 ------ 1 6 3 1 2 3 4 5 6 1 3 2 5 1 6 ----- Sample Output 1 ------ 1 2 10 ----- explanation 1 ------ - For the first query, we can choose $(1, 2, 3)$ since $A_{1} + A_{2} + A_{3} = 6$ is an even number. - For the second query, we can choose $(2, 3, 5)$ since $A_{2} + A_{3} + A_{5} = 10$ is even, and $(3, 4, 5)$ since $A_{3} + A_{4} + A_{5} = 12$ is even.
for _ in range(int(input())): a, b = map(int, input().split()) l = list(map(int, input().split())) mid = {(0): [0, 0]} count1, count2 = 0, 0 for i in range(a): if l[i] % 2 == 0: count1 += 1 else: count2 += 1 mid[i + 1] = [count1, count2] for i in range(b): x, y = map(int, input().split()) evens = mid[y][0] - mid[x - 1][0] odds = mid[y][1] - mid[x - 1][1] print(evens * (evens - 1) * (evens - 2) // 6 + odds * (odds - 1) * evens // 2)
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 FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR DICT NUMBER LIST NUMBER NUMBER ASSIGN VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR NUMBER NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER LIST VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP VAR VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER BIN_OP BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR NUMBER
Read problem statements in [Russian], [Mandarin Chinese], [Bengali], and [Vietnamese] as well. You are given an array A consisting of N integers and Q queries. Each query is described by two integers L and R. For each query, output the number of tuples (i, j, k) such that L≀ i< j< k≀ R and A_{i}+A_{j}+A_{k} is an even number. ------ Input Format ------ - 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 two integers N and Q. - The next line contains N integers A_{1},\ldots,A_{N}. - Then Q lines follow, each containing two integers L_{i} and R_{i}. ------ Output Format ------ For each query, output the number of tuples possible as mentioned in the problem statement. ------ Constraints ------ $1 ≀ T ≀ 10^{3}$ $1 ≀ N, Q ≀ 10^{5}$ $0 ≀ A_{i} ≀ 10^{6}$ $1 ≀ L_{i} ≀ R_{i} ≀ N$ - The sum of $N$ over all test cases does not exceed $10^{6}$. - The sum of $Q$ over all test cases does not exceed $10^{5}$ ----- Sample Input 1 ------ 1 6 3 1 2 3 4 5 6 1 3 2 5 1 6 ----- Sample Output 1 ------ 1 2 10 ----- explanation 1 ------ - For the first query, we can choose $(1, 2, 3)$ since $A_{1} + A_{2} + A_{3} = 6$ is an even number. - For the second query, we can choose $(2, 3, 5)$ since $A_{2} + A_{3} + A_{5} = 10$ is even, and $(3, 4, 5)$ since $A_{3} + A_{4} + A_{5} = 12$ is even.
def checkeven(n): if n % 2 == 0: return 1 else: return 0 t = int(input()) for i in range(t): ip = input() sn, sq = ip.split(" ") n, q = int(sn), int(sq) arr = [0] * (n + 1) istrip = input() strip = [] strip = istrip.split(" ") strip = list(map(int, strip)) strip.insert(0, 0) c = 0 c = checkeven(strip[1]) arr[1] = c for j in range(2, n + 1): c += checkeven(strip[j]) arr[j] = c for j in range(q): ip = input() sl, sr = ip.split(" ") l, r = int(sl), int(sr) even = arr[r] - arr[l - 1] odd = r - l + 1 - even ceven = int(even * (even - 1) * (even - 2) / 6) codd = int(odd * (odd - 1) / 2 * even) out = ceven + codd print(out)
FUNC_DEF IF BIN_OP VAR NUMBER NUMBER RETURN NUMBER RETURN NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR STRING ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR STRING ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR ASSIGN VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR
Read problem statements in [Russian], [Mandarin Chinese], [Bengali], and [Vietnamese] as well. You are given an array A consisting of N integers and Q queries. Each query is described by two integers L and R. For each query, output the number of tuples (i, j, k) such that L≀ i< j< k≀ R and A_{i}+A_{j}+A_{k} is an even number. ------ Input Format ------ - 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 two integers N and Q. - The next line contains N integers A_{1},\ldots,A_{N}. - Then Q lines follow, each containing two integers L_{i} and R_{i}. ------ Output Format ------ For each query, output the number of tuples possible as mentioned in the problem statement. ------ Constraints ------ $1 ≀ T ≀ 10^{3}$ $1 ≀ N, Q ≀ 10^{5}$ $0 ≀ A_{i} ≀ 10^{6}$ $1 ≀ L_{i} ≀ R_{i} ≀ N$ - The sum of $N$ over all test cases does not exceed $10^{6}$. - The sum of $Q$ over all test cases does not exceed $10^{5}$ ----- Sample Input 1 ------ 1 6 3 1 2 3 4 5 6 1 3 2 5 1 6 ----- Sample Output 1 ------ 1 2 10 ----- explanation 1 ------ - For the first query, we can choose $(1, 2, 3)$ since $A_{1} + A_{2} + A_{3} = 6$ is an even number. - For the second query, we can choose $(2, 3, 5)$ since $A_{2} + A_{3} + A_{5} = 10$ is even, and $(3, 4, 5)$ since $A_{3} + A_{4} + A_{5} = 12$ is even.
for i in range(int(input())): x, y = map(int, input().split()) a = list(map(int, input().split())) s = [] c = 0 for i in a: if i % 2 == 0: c += 1 s.append(c) else: s.append(c) for i in range(y): b, c = map(int, input().split()) e = s[c - 1] - s[b - 1] if a[b - 1] % 2 == 0: e += 1 o = c - b + 1 - e z = e * int(o * (o - 1) / 2) + int(e * (e - 1) * (e - 2) / 6) print(z)
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 FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR VAR IF BIN_OP VAR NUMBER NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER IF BIN_OP VAR BIN_OP VAR NUMBER NUMBER NUMBER VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER VAR ASSIGN VAR BIN_OP BIN_OP VAR FUNC_CALL VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER FUNC_CALL VAR BIN_OP BIN_OP BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR
Read problems statements in [Hindi], [Mandarin Chinese], [Vietnamese], and [Bengali] as well. You are given two sets of segments on a line, $A$ and $B$. Set $A$ contains $N$ segments (numbered $1$ through $N$); for each valid $i$, the $i$-th of these segments is $S_{A,i} = [L_{A,i}, R_{A,i}]$. Set $B$ contains $M$ segments (numbered $1$ through $M$); for each valid $i$, the $i$-th of these segments is $S_{B,i} = [L_{B,i}, R_{B,i}]$. Find $\sum_{i=1}^N \sum_{j=1}^M |S_{A,i} \cap S_{B,j}|$, where $|S_{A,i} \cap S_{B,j}|$ denotes the length of the intersection of the $i$-th segment from set $A$ and the $j$-th segment from set $B$. Note that the length of a segment $[l, r]$ is $r-l$. ------ 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 $M$. $N$ lines follow. For each valid $i$, the $i$-th of these lines contains two space-separated integers $L_{A,i}$ and $R_{A,i}$. $M$ more lines follow. For each valid $i$, the $i$-th of these lines contains two space-separated integers $L_{B,i}$ and $R_{B,i}$. ------ Output ------ For each test case, print a single line containing one integer ― the value of the sum. ------ Constraints ------ $1 ≀ T ≀ 10^{5}$ $1 ≀ N, M ≀ 10^{5}$ $1 ≀ L_{A,i} < R_{A,i} ≀ 10^{8}$ for each valid $i$ $1 ≀ L_{B,i} < R_{B,i} ≀ 10^{8}$ for each valid $i$ the sum of $N$ over all test cases does not exceed $2 \cdot 10^{5}$ the sum of $M$ over all test cases does not exceed $2 \cdot 10^{5}$ ------ Example Input ------ 3 2 2 1 2 3 4 1 3 2 4 1 1 1 2 3 4 2 1 5 6 6 8 5 8 ------ Example Output ------ 2 0 3 ------ Explanation ------ Example case 1: The intersection of $S_{A,1}$ and $S_{B,1}$ is the segment $[1, 2]$. The intersection of $S_{A,2}$ and $S_{B,2}$ is $[3, 4]$. Both remaining intersections have lengths $0$, so the sum is $1 + 1 = 2$. Example case 2: The only two segments have an empty intersection, which has length $0$. Example case 3: The segment $[5, 8]$ is covered by both sets of segments, so the sum is $3$.
import sys input = lambda: sys.stdin.readline().rstrip("\r\n") inp = lambda: list(map(int, sys.stdin.readline().rstrip("\r\n").split())) mod = 10**9 + 7 Mod = 998244353 INF = float("inf") tc = 1 (tc,) = inp() for _ in range(tc): n, m = inp() a = [inp() for i in range(n)] b = [inp() for i in range(m)] A = [i for i, j in a] + [j for i, j in a] B = [i for i, j in b] + [j for i, j in b] A.sort() B.sort() c = [0] N = len(A) M = len(B) i = j = 0 IND = {} while i < N and j < M: if A[i] <= B[j]: if c[-1] != A[i]: c.append(A[i]) i += 1 else: if c[-1] != B[j]: c.append(B[j]) j += 1 while i < N: if c[-1] != A[i]: c.append(A[i]) i += 1 while j < M: if c[-1] != B[j]: c.append(B[j]) j += 1 d = c.copy() for i, val in enumerate(c): IND[val] = i C = [0] * len(c) D = [0] * len(d) for i, j in a: C[IND[i]] += 1 C[IND[j]] -= 1 for i, j in b: D[IND[i]] += 1 D[IND[j]] -= 1 for i in range(1, len(c)): C[i] += C[i - 1] D[i] += D[i - 1] ans = 0 for i in range(len(c) - 1): ans += C[i] * D[i] * (c[i + 1] - c[i]) print(ans)
IMPORT ASSIGN VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR LIST NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR DICT WHILE VAR VAR VAR VAR IF VAR VAR VAR VAR IF VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR NUMBER IF VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR NUMBER WHILE VAR VAR IF VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR NUMBER WHILE VAR VAR IF VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FOR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR VAR FOR VAR VAR VAR VAR VAR VAR NUMBER VAR VAR VAR NUMBER FOR VAR VAR VAR VAR VAR VAR NUMBER VAR 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 ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR BIN_OP BIN_OP VAR VAR VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR
Limak is a little polar bear. He is playing a video game and he needs your help. There is a row with N cells, each either empty or occupied by a soldier, denoted by '0' and '1' respectively. The goal of the game is to move all soldiers to the right (they should occupy some number of rightmost cells). The only possible command is choosing a soldier and telling him to move to the right as far as possible. Choosing a soldier takes 1 second, and a soldier moves with the speed of a cell per second. The soldier stops immediately if he is in the last cell of the row or the next cell is already occupied. Limak isn't allowed to choose a soldier that can't move at all (the chosen soldier must move at least one cell to the right). Limak enjoys this game very much and wants to play as long as possible. In particular, he doesn't start a new command while the previously chosen soldier moves. Can you tell him, how many seconds he can play at most? -----Input----- The first line of the input contains an integer T denoting the number of test cases. The description of T test cases follows. The only line of each test case contains a string S describing the row with N cells. Each character is either '0' or '1', denoting an empty cell or a cell with a soldier respectively. -----Output----- For each test case, output a single line containing one integer β€” the maximum possible number of seconds Limak will play the game. -----Constraints----- - 1 ≀ T ≀ 5 - 1 ≀ N ≀ 105 (N denotes the length of the string S) -----Subtasks----- - Subtask #1 (25 points): 1 ≀ N ≀ 10 - Subtask #2 (25 points): 1 ≀ N ≀ 2000 - Subtask #3 (50 points): Original constraints. -----Example----- Input: 4 10100 1100001 000000000111 001110100011010 Output: 8 10 0 48 -----Explanation----- Test case 1. The initial string is 10100. There are N = 5 cells. There is one soldier in the first cell, and one soldier in the third cell. The following scenario maximizes the total time: - Limak chooses the soldier in the first cell. This soldier can move only one cell to the right. It takes 1 second to choose a soldier and 1 second for a soldier to move to the next cell (2 seconds in total). The string is 01100 now. - Limak has only one choice. He must choose the soldier in the third cell because the other soldier can't move at all (the soldier in the second cell can't move to the right because the next cell is already occupied). Choosing a soldier takes 1 second. The chosen soldier moves from the third cell to the fifth cell, which takes 2 seconds. This operation takes 1 + 2 = 3 seconds in total. The string is 01001 now. - Limak has only one choice again. Since the soldier in the last row can't move further to the right, the soldier in the second cell must be chosen. He will move 2 cells to the right. This operation takes 1 + 2 = 3 seconds in total. The string become 00011 and the game is over. The total time is 2 + 3 + 3 = 8. Test case 2. The initial string is 1100001. There is only one possible scenario: - 1100001 is changed to 1000011 in 5 seconds (1 second to choose a soldier and 4 seconds for the soldier to move 4 cells to the right). - 1000011 is changed to 0000111 in 5 seconds. The total time is 5 + 5 = 10 seconds. Test case 3. The game is over immediately because all soldiers occupy rightmost cells already. The answer is 0.
for _ in range(int(input())): s = input() c = 0 n = len(s) c = 0 m = 0 r = n - 1 curr = n - 1 while curr >= 0 and s[r] == "1": curr -= 1 r -= 1 while curr >= 0: if s[curr] == "1": if s[curr + 1] == "0": c += 1 m += c + (r - curr) r -= 1 curr -= 1 print(m)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR NUMBER VAR VAR STRING VAR NUMBER VAR NUMBER WHILE VAR NUMBER IF VAR VAR STRING IF VAR BIN_OP VAR NUMBER STRING VAR NUMBER VAR BIN_OP VAR BIN_OP VAR VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR
Limak is a little polar bear. He is playing a video game and he needs your help. There is a row with N cells, each either empty or occupied by a soldier, denoted by '0' and '1' respectively. The goal of the game is to move all soldiers to the right (they should occupy some number of rightmost cells). The only possible command is choosing a soldier and telling him to move to the right as far as possible. Choosing a soldier takes 1 second, and a soldier moves with the speed of a cell per second. The soldier stops immediately if he is in the last cell of the row or the next cell is already occupied. Limak isn't allowed to choose a soldier that can't move at all (the chosen soldier must move at least one cell to the right). Limak enjoys this game very much and wants to play as long as possible. In particular, he doesn't start a new command while the previously chosen soldier moves. Can you tell him, how many seconds he can play at most? -----Input----- The first line of the input contains an integer T denoting the number of test cases. The description of T test cases follows. The only line of each test case contains a string S describing the row with N cells. Each character is either '0' or '1', denoting an empty cell or a cell with a soldier respectively. -----Output----- For each test case, output a single line containing one integer β€” the maximum possible number of seconds Limak will play the game. -----Constraints----- - 1 ≀ T ≀ 5 - 1 ≀ N ≀ 105 (N denotes the length of the string S) -----Subtasks----- - Subtask #1 (25 points): 1 ≀ N ≀ 10 - Subtask #2 (25 points): 1 ≀ N ≀ 2000 - Subtask #3 (50 points): Original constraints. -----Example----- Input: 4 10100 1100001 000000000111 001110100011010 Output: 8 10 0 48 -----Explanation----- Test case 1. The initial string is 10100. There are N = 5 cells. There is one soldier in the first cell, and one soldier in the third cell. The following scenario maximizes the total time: - Limak chooses the soldier in the first cell. This soldier can move only one cell to the right. It takes 1 second to choose a soldier and 1 second for a soldier to move to the next cell (2 seconds in total). The string is 01100 now. - Limak has only one choice. He must choose the soldier in the third cell because the other soldier can't move at all (the soldier in the second cell can't move to the right because the next cell is already occupied). Choosing a soldier takes 1 second. The chosen soldier moves from the third cell to the fifth cell, which takes 2 seconds. This operation takes 1 + 2 = 3 seconds in total. The string is 01001 now. - Limak has only one choice again. Since the soldier in the last row can't move further to the right, the soldier in the second cell must be chosen. He will move 2 cells to the right. This operation takes 1 + 2 = 3 seconds in total. The string become 00011 and the game is over. The total time is 2 + 3 + 3 = 8. Test case 2. The initial string is 1100001. There is only one possible scenario: - 1100001 is changed to 1000011 in 5 seconds (1 second to choose a soldier and 4 seconds for the soldier to move 4 cells to the right). - 1000011 is changed to 0000111 in 5 seconds. The total time is 5 + 5 = 10 seconds. Test case 3. The game is over immediately because all soldiers occupy rightmost cells already. The answer is 0.
t = int(input()) for _ in range(t): time = 0 s = list(map(int, input().lstrip("0"))) ones = 0 zeroes = 0 i = 0 while i < len(s): if i < len(s): while s[i] == 1: i += 1 ones += 1 if i >= len(s): break if i < len(s): while s[i] == 0: i += 1 zeroes += 1 if i >= len(s): break time += ones * (zeroes + 1) zeroes = 0 if len(s) > 0: if s[-1] == 1: time -= s.count(1) print(time)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR IF VAR FUNC_CALL VAR VAR WHILE VAR VAR NUMBER VAR NUMBER VAR NUMBER IF VAR FUNC_CALL VAR VAR IF VAR FUNC_CALL VAR VAR WHILE VAR VAR NUMBER VAR NUMBER VAR NUMBER IF VAR FUNC_CALL VAR VAR VAR BIN_OP VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER IF FUNC_CALL VAR VAR NUMBER IF VAR NUMBER NUMBER VAR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR
Limak is a little polar bear. He is playing a video game and he needs your help. There is a row with N cells, each either empty or occupied by a soldier, denoted by '0' and '1' respectively. The goal of the game is to move all soldiers to the right (they should occupy some number of rightmost cells). The only possible command is choosing a soldier and telling him to move to the right as far as possible. Choosing a soldier takes 1 second, and a soldier moves with the speed of a cell per second. The soldier stops immediately if he is in the last cell of the row or the next cell is already occupied. Limak isn't allowed to choose a soldier that can't move at all (the chosen soldier must move at least one cell to the right). Limak enjoys this game very much and wants to play as long as possible. In particular, he doesn't start a new command while the previously chosen soldier moves. Can you tell him, how many seconds he can play at most? -----Input----- The first line of the input contains an integer T denoting the number of test cases. The description of T test cases follows. The only line of each test case contains a string S describing the row with N cells. Each character is either '0' or '1', denoting an empty cell or a cell with a soldier respectively. -----Output----- For each test case, output a single line containing one integer β€” the maximum possible number of seconds Limak will play the game. -----Constraints----- - 1 ≀ T ≀ 5 - 1 ≀ N ≀ 105 (N denotes the length of the string S) -----Subtasks----- - Subtask #1 (25 points): 1 ≀ N ≀ 10 - Subtask #2 (25 points): 1 ≀ N ≀ 2000 - Subtask #3 (50 points): Original constraints. -----Example----- Input: 4 10100 1100001 000000000111 001110100011010 Output: 8 10 0 48 -----Explanation----- Test case 1. The initial string is 10100. There are N = 5 cells. There is one soldier in the first cell, and one soldier in the third cell. The following scenario maximizes the total time: - Limak chooses the soldier in the first cell. This soldier can move only one cell to the right. It takes 1 second to choose a soldier and 1 second for a soldier to move to the next cell (2 seconds in total). The string is 01100 now. - Limak has only one choice. He must choose the soldier in the third cell because the other soldier can't move at all (the soldier in the second cell can't move to the right because the next cell is already occupied). Choosing a soldier takes 1 second. The chosen soldier moves from the third cell to the fifth cell, which takes 2 seconds. This operation takes 1 + 2 = 3 seconds in total. The string is 01001 now. - Limak has only one choice again. Since the soldier in the last row can't move further to the right, the soldier in the second cell must be chosen. He will move 2 cells to the right. This operation takes 1 + 2 = 3 seconds in total. The string become 00011 and the game is over. The total time is 2 + 3 + 3 = 8. Test case 2. The initial string is 1100001. There is only one possible scenario: - 1100001 is changed to 1000011 in 5 seconds (1 second to choose a soldier and 4 seconds for the soldier to move 4 cells to the right). - 1000011 is changed to 0000111 in 5 seconds. The total time is 5 + 5 = 10 seconds. Test case 3. The game is over immediately because all soldiers occupy rightmost cells already. The answer is 0.
def rowsold(s): t = 0 count0 = 0 count1 = 0 for j in range(len(s)): if s[j] == "1" or j == len(s) - 1: if s[j] == "0": count0 = count0 + 1 if count0 != 0: t = t + count1 + count1 * count0 count1 = count1 + 1 count0 = 0 elif s[j] == "0": count0 = count0 + 1 print(t) for i in range(int(input())): rowsold(input())
FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR STRING VAR BIN_OP FUNC_CALL VAR VAR NUMBER IF VAR VAR STRING ASSIGN VAR BIN_OP VAR NUMBER IF VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER IF VAR VAR STRING ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR
Limak is a little polar bear. He is playing a video game and he needs your help. There is a row with N cells, each either empty or occupied by a soldier, denoted by '0' and '1' respectively. The goal of the game is to move all soldiers to the right (they should occupy some number of rightmost cells). The only possible command is choosing a soldier and telling him to move to the right as far as possible. Choosing a soldier takes 1 second, and a soldier moves with the speed of a cell per second. The soldier stops immediately if he is in the last cell of the row or the next cell is already occupied. Limak isn't allowed to choose a soldier that can't move at all (the chosen soldier must move at least one cell to the right). Limak enjoys this game very much and wants to play as long as possible. In particular, he doesn't start a new command while the previously chosen soldier moves. Can you tell him, how many seconds he can play at most? -----Input----- The first line of the input contains an integer T denoting the number of test cases. The description of T test cases follows. The only line of each test case contains a string S describing the row with N cells. Each character is either '0' or '1', denoting an empty cell or a cell with a soldier respectively. -----Output----- For each test case, output a single line containing one integer β€” the maximum possible number of seconds Limak will play the game. -----Constraints----- - 1 ≀ T ≀ 5 - 1 ≀ N ≀ 105 (N denotes the length of the string S) -----Subtasks----- - Subtask #1 (25 points): 1 ≀ N ≀ 10 - Subtask #2 (25 points): 1 ≀ N ≀ 2000 - Subtask #3 (50 points): Original constraints. -----Example----- Input: 4 10100 1100001 000000000111 001110100011010 Output: 8 10 0 48 -----Explanation----- Test case 1. The initial string is 10100. There are N = 5 cells. There is one soldier in the first cell, and one soldier in the third cell. The following scenario maximizes the total time: - Limak chooses the soldier in the first cell. This soldier can move only one cell to the right. It takes 1 second to choose a soldier and 1 second for a soldier to move to the next cell (2 seconds in total). The string is 01100 now. - Limak has only one choice. He must choose the soldier in the third cell because the other soldier can't move at all (the soldier in the second cell can't move to the right because the next cell is already occupied). Choosing a soldier takes 1 second. The chosen soldier moves from the third cell to the fifth cell, which takes 2 seconds. This operation takes 1 + 2 = 3 seconds in total. The string is 01001 now. - Limak has only one choice again. Since the soldier in the last row can't move further to the right, the soldier in the second cell must be chosen. He will move 2 cells to the right. This operation takes 1 + 2 = 3 seconds in total. The string become 00011 and the game is over. The total time is 2 + 3 + 3 = 8. Test case 2. The initial string is 1100001. There is only one possible scenario: - 1100001 is changed to 1000011 in 5 seconds (1 second to choose a soldier and 4 seconds for the soldier to move 4 cells to the right). - 1000011 is changed to 0000111 in 5 seconds. The total time is 5 + 5 = 10 seconds. Test case 3. The game is over immediately because all soldiers occupy rightmost cells already. The answer is 0.
t = input() for i in range(int(t)): n = input() l = len(n) time = 0 count1 = 0 j = 0 while j < l: count0 = 0 while j < l and n[j] == "1": count1 += 1 j += 1 while j < l and n[j] == "0": count0 += 1 j += 1 if j < l or n[j - 1] == "0": time += count1 * (count0 + 1) print(time)
ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR NUMBER WHILE VAR VAR VAR VAR STRING VAR NUMBER VAR NUMBER WHILE VAR VAR VAR VAR STRING VAR NUMBER VAR NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER STRING VAR BIN_OP VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR
Limak is a little polar bear. He is playing a video game and he needs your help. There is a row with N cells, each either empty or occupied by a soldier, denoted by '0' and '1' respectively. The goal of the game is to move all soldiers to the right (they should occupy some number of rightmost cells). The only possible command is choosing a soldier and telling him to move to the right as far as possible. Choosing a soldier takes 1 second, and a soldier moves with the speed of a cell per second. The soldier stops immediately if he is in the last cell of the row or the next cell is already occupied. Limak isn't allowed to choose a soldier that can't move at all (the chosen soldier must move at least one cell to the right). Limak enjoys this game very much and wants to play as long as possible. In particular, he doesn't start a new command while the previously chosen soldier moves. Can you tell him, how many seconds he can play at most? -----Input----- The first line of the input contains an integer T denoting the number of test cases. The description of T test cases follows. The only line of each test case contains a string S describing the row with N cells. Each character is either '0' or '1', denoting an empty cell or a cell with a soldier respectively. -----Output----- For each test case, output a single line containing one integer β€” the maximum possible number of seconds Limak will play the game. -----Constraints----- - 1 ≀ T ≀ 5 - 1 ≀ N ≀ 105 (N denotes the length of the string S) -----Subtasks----- - Subtask #1 (25 points): 1 ≀ N ≀ 10 - Subtask #2 (25 points): 1 ≀ N ≀ 2000 - Subtask #3 (50 points): Original constraints. -----Example----- Input: 4 10100 1100001 000000000111 001110100011010 Output: 8 10 0 48 -----Explanation----- Test case 1. The initial string is 10100. There are N = 5 cells. There is one soldier in the first cell, and one soldier in the third cell. The following scenario maximizes the total time: - Limak chooses the soldier in the first cell. This soldier can move only one cell to the right. It takes 1 second to choose a soldier and 1 second for a soldier to move to the next cell (2 seconds in total). The string is 01100 now. - Limak has only one choice. He must choose the soldier in the third cell because the other soldier can't move at all (the soldier in the second cell can't move to the right because the next cell is already occupied). Choosing a soldier takes 1 second. The chosen soldier moves from the third cell to the fifth cell, which takes 2 seconds. This operation takes 1 + 2 = 3 seconds in total. The string is 01001 now. - Limak has only one choice again. Since the soldier in the last row can't move further to the right, the soldier in the second cell must be chosen. He will move 2 cells to the right. This operation takes 1 + 2 = 3 seconds in total. The string become 00011 and the game is over. The total time is 2 + 3 + 3 = 8. Test case 2. The initial string is 1100001. There is only one possible scenario: - 1100001 is changed to 1000011 in 5 seconds (1 second to choose a soldier and 4 seconds for the soldier to move 4 cells to the right). - 1000011 is changed to 0000111 in 5 seconds. The total time is 5 + 5 = 10 seconds. Test case 3. The game is over immediately because all soldiers occupy rightmost cells already. The answer is 0.
from sys import stdin def solve(): T = int(stdin.readline().strip()) for t in range(T): game = stdin.readline().strip() game = list(game) length = len(game) cnt = ans = 0 first = True start = end = -1 i = 0 while i < length: if game[i] == "1": cnt += 1 if first: start = i first = False else: end = i if ( start != -1 and end != -1 and start + cnt != length and start + cnt - 1 != end ): if cnt > 1: ans += (end - start - cnt + 1) * (cnt - 1) + (cnt - 1) else: ans += (end - start - cnt + 1) * cnt + (cnt - 1) start = end - cnt + 1 end = -1 first = False i += 1 if start + cnt != length: ans += (length - start - cnt + 1) * cnt print(ans) solve()
FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR IF VAR VAR STRING VAR NUMBER IF VAR ASSIGN VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR IF VAR NUMBER VAR NUMBER BIN_OP VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR IF VAR NUMBER VAR BIN_OP BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR BIN_OP BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER IF BIN_OP VAR VAR VAR VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR VAR NUMBER VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR
Limak is a little polar bear. He is playing a video game and he needs your help. There is a row with N cells, each either empty or occupied by a soldier, denoted by '0' and '1' respectively. The goal of the game is to move all soldiers to the right (they should occupy some number of rightmost cells). The only possible command is choosing a soldier and telling him to move to the right as far as possible. Choosing a soldier takes 1 second, and a soldier moves with the speed of a cell per second. The soldier stops immediately if he is in the last cell of the row or the next cell is already occupied. Limak isn't allowed to choose a soldier that can't move at all (the chosen soldier must move at least one cell to the right). Limak enjoys this game very much and wants to play as long as possible. In particular, he doesn't start a new command while the previously chosen soldier moves. Can you tell him, how many seconds he can play at most? -----Input----- The first line of the input contains an integer T denoting the number of test cases. The description of T test cases follows. The only line of each test case contains a string S describing the row with N cells. Each character is either '0' or '1', denoting an empty cell or a cell with a soldier respectively. -----Output----- For each test case, output a single line containing one integer β€” the maximum possible number of seconds Limak will play the game. -----Constraints----- - 1 ≀ T ≀ 5 - 1 ≀ N ≀ 105 (N denotes the length of the string S) -----Subtasks----- - Subtask #1 (25 points): 1 ≀ N ≀ 10 - Subtask #2 (25 points): 1 ≀ N ≀ 2000 - Subtask #3 (50 points): Original constraints. -----Example----- Input: 4 10100 1100001 000000000111 001110100011010 Output: 8 10 0 48 -----Explanation----- Test case 1. The initial string is 10100. There are N = 5 cells. There is one soldier in the first cell, and one soldier in the third cell. The following scenario maximizes the total time: - Limak chooses the soldier in the first cell. This soldier can move only one cell to the right. It takes 1 second to choose a soldier and 1 second for a soldier to move to the next cell (2 seconds in total). The string is 01100 now. - Limak has only one choice. He must choose the soldier in the third cell because the other soldier can't move at all (the soldier in the second cell can't move to the right because the next cell is already occupied). Choosing a soldier takes 1 second. The chosen soldier moves from the third cell to the fifth cell, which takes 2 seconds. This operation takes 1 + 2 = 3 seconds in total. The string is 01001 now. - Limak has only one choice again. Since the soldier in the last row can't move further to the right, the soldier in the second cell must be chosen. He will move 2 cells to the right. This operation takes 1 + 2 = 3 seconds in total. The string become 00011 and the game is over. The total time is 2 + 3 + 3 = 8. Test case 2. The initial string is 1100001. There is only one possible scenario: - 1100001 is changed to 1000011 in 5 seconds (1 second to choose a soldier and 4 seconds for the soldier to move 4 cells to the right). - 1000011 is changed to 0000111 in 5 seconds. The total time is 5 + 5 = 10 seconds. Test case 3. The game is over immediately because all soldiers occupy rightmost cells already. The answer is 0.
t = int(input()) for q in range(t): arr = input() arr = list(arr) arr_len = len(arr) count_time = 0 num_zeroes = 0 num_ones = 0 num_ones_total = 0 multiplier = 0 num_selections = 0 flag = 0 for i in arr: if i == "1": num_ones_total += 1 multiplier += 1 flag += 1 else: num_ones = num_ones_total num_zeroes += multiplier if flag != 0: num_selections += num_ones flag = 0 count_time = num_selections + num_zeroes print(count_time)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF VAR STRING VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR VAR IF VAR NUMBER VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR
Limak is a little polar bear. He is playing a video game and he needs your help. There is a row with N cells, each either empty or occupied by a soldier, denoted by '0' and '1' respectively. The goal of the game is to move all soldiers to the right (they should occupy some number of rightmost cells). The only possible command is choosing a soldier and telling him to move to the right as far as possible. Choosing a soldier takes 1 second, and a soldier moves with the speed of a cell per second. The soldier stops immediately if he is in the last cell of the row or the next cell is already occupied. Limak isn't allowed to choose a soldier that can't move at all (the chosen soldier must move at least one cell to the right). Limak enjoys this game very much and wants to play as long as possible. In particular, he doesn't start a new command while the previously chosen soldier moves. Can you tell him, how many seconds he can play at most? -----Input----- The first line of the input contains an integer T denoting the number of test cases. The description of T test cases follows. The only line of each test case contains a string S describing the row with N cells. Each character is either '0' or '1', denoting an empty cell or a cell with a soldier respectively. -----Output----- For each test case, output a single line containing one integer β€” the maximum possible number of seconds Limak will play the game. -----Constraints----- - 1 ≀ T ≀ 5 - 1 ≀ N ≀ 105 (N denotes the length of the string S) -----Subtasks----- - Subtask #1 (25 points): 1 ≀ N ≀ 10 - Subtask #2 (25 points): 1 ≀ N ≀ 2000 - Subtask #3 (50 points): Original constraints. -----Example----- Input: 4 10100 1100001 000000000111 001110100011010 Output: 8 10 0 48 -----Explanation----- Test case 1. The initial string is 10100. There are N = 5 cells. There is one soldier in the first cell, and one soldier in the third cell. The following scenario maximizes the total time: - Limak chooses the soldier in the first cell. This soldier can move only one cell to the right. It takes 1 second to choose a soldier and 1 second for a soldier to move to the next cell (2 seconds in total). The string is 01100 now. - Limak has only one choice. He must choose the soldier in the third cell because the other soldier can't move at all (the soldier in the second cell can't move to the right because the next cell is already occupied). Choosing a soldier takes 1 second. The chosen soldier moves from the third cell to the fifth cell, which takes 2 seconds. This operation takes 1 + 2 = 3 seconds in total. The string is 01001 now. - Limak has only one choice again. Since the soldier in the last row can't move further to the right, the soldier in the second cell must be chosen. He will move 2 cells to the right. This operation takes 1 + 2 = 3 seconds in total. The string become 00011 and the game is over. The total time is 2 + 3 + 3 = 8. Test case 2. The initial string is 1100001. There is only one possible scenario: - 1100001 is changed to 1000011 in 5 seconds (1 second to choose a soldier and 4 seconds for the soldier to move 4 cells to the right). - 1000011 is changed to 0000111 in 5 seconds. The total time is 5 + 5 = 10 seconds. Test case 3. The game is over immediately because all soldiers occupy rightmost cells already. The answer is 0.
n = int(input()) while n: n -= 1 str_val = input() final_place = len(str_val) - 1 size = len(str_val) jumps = 0 ans = 0 for i in range(size - 1, -1, -1): if str_val[i] == "1": if i != size - 1 and str_val[i + 1] == "0": jumps += 1 ans += jumps + (final_place - i) final_place -= 1 print(ans)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR VAR STRING IF VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER STRING VAR NUMBER VAR BIN_OP VAR BIN_OP VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
Limak is a little polar bear. He is playing a video game and he needs your help. There is a row with N cells, each either empty or occupied by a soldier, denoted by '0' and '1' respectively. The goal of the game is to move all soldiers to the right (they should occupy some number of rightmost cells). The only possible command is choosing a soldier and telling him to move to the right as far as possible. Choosing a soldier takes 1 second, and a soldier moves with the speed of a cell per second. The soldier stops immediately if he is in the last cell of the row or the next cell is already occupied. Limak isn't allowed to choose a soldier that can't move at all (the chosen soldier must move at least one cell to the right). Limak enjoys this game very much and wants to play as long as possible. In particular, he doesn't start a new command while the previously chosen soldier moves. Can you tell him, how many seconds he can play at most? -----Input----- The first line of the input contains an integer T denoting the number of test cases. The description of T test cases follows. The only line of each test case contains a string S describing the row with N cells. Each character is either '0' or '1', denoting an empty cell or a cell with a soldier respectively. -----Output----- For each test case, output a single line containing one integer β€” the maximum possible number of seconds Limak will play the game. -----Constraints----- - 1 ≀ T ≀ 5 - 1 ≀ N ≀ 105 (N denotes the length of the string S) -----Subtasks----- - Subtask #1 (25 points): 1 ≀ N ≀ 10 - Subtask #2 (25 points): 1 ≀ N ≀ 2000 - Subtask #3 (50 points): Original constraints. -----Example----- Input: 4 10100 1100001 000000000111 001110100011010 Output: 8 10 0 48 -----Explanation----- Test case 1. The initial string is 10100. There are N = 5 cells. There is one soldier in the first cell, and one soldier in the third cell. The following scenario maximizes the total time: - Limak chooses the soldier in the first cell. This soldier can move only one cell to the right. It takes 1 second to choose a soldier and 1 second for a soldier to move to the next cell (2 seconds in total). The string is 01100 now. - Limak has only one choice. He must choose the soldier in the third cell because the other soldier can't move at all (the soldier in the second cell can't move to the right because the next cell is already occupied). Choosing a soldier takes 1 second. The chosen soldier moves from the third cell to the fifth cell, which takes 2 seconds. This operation takes 1 + 2 = 3 seconds in total. The string is 01001 now. - Limak has only one choice again. Since the soldier in the last row can't move further to the right, the soldier in the second cell must be chosen. He will move 2 cells to the right. This operation takes 1 + 2 = 3 seconds in total. The string become 00011 and the game is over. The total time is 2 + 3 + 3 = 8. Test case 2. The initial string is 1100001. There is only one possible scenario: - 1100001 is changed to 1000011 in 5 seconds (1 second to choose a soldier and 4 seconds for the soldier to move 4 cells to the right). - 1000011 is changed to 0000111 in 5 seconds. The total time is 5 + 5 = 10 seconds. Test case 3. The game is over immediately because all soldiers occupy rightmost cells already. The answer is 0.
t = int(input()) for each in range(t): a = input() n = len(a) picks = [0] * (n + 1) picks[n - 1] = 1 if a[n - 1] == "0" else 0 i = n - 2 while i > -1: picks[i] = picks[i + 1] if a[i] == "0" and a[i + 1] == "1": picks[i] += 1 i -= 1 disp = [0] * (n + 1) soldiers = 0 i = n - 1 while i > -1: if a[i] == "1": disp[i] = n - soldiers - 1 - i soldiers += 1 i -= 1 ans = 0 for i in range(n): if a[i] == "1": ans += picks[i] + disp[i] print(ans)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER STRING NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER IF VAR VAR STRING VAR BIN_OP VAR NUMBER STRING VAR VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR NUMBER IF VAR VAR STRING ASSIGN VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER VAR VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR STRING VAR BIN_OP VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR
Limak is a little polar bear. He is playing a video game and he needs your help. There is a row with N cells, each either empty or occupied by a soldier, denoted by '0' and '1' respectively. The goal of the game is to move all soldiers to the right (they should occupy some number of rightmost cells). The only possible command is choosing a soldier and telling him to move to the right as far as possible. Choosing a soldier takes 1 second, and a soldier moves with the speed of a cell per second. The soldier stops immediately if he is in the last cell of the row or the next cell is already occupied. Limak isn't allowed to choose a soldier that can't move at all (the chosen soldier must move at least one cell to the right). Limak enjoys this game very much and wants to play as long as possible. In particular, he doesn't start a new command while the previously chosen soldier moves. Can you tell him, how many seconds he can play at most? -----Input----- The first line of the input contains an integer T denoting the number of test cases. The description of T test cases follows. The only line of each test case contains a string S describing the row with N cells. Each character is either '0' or '1', denoting an empty cell or a cell with a soldier respectively. -----Output----- For each test case, output a single line containing one integer β€” the maximum possible number of seconds Limak will play the game. -----Constraints----- - 1 ≀ T ≀ 5 - 1 ≀ N ≀ 105 (N denotes the length of the string S) -----Subtasks----- - Subtask #1 (25 points): 1 ≀ N ≀ 10 - Subtask #2 (25 points): 1 ≀ N ≀ 2000 - Subtask #3 (50 points): Original constraints. -----Example----- Input: 4 10100 1100001 000000000111 001110100011010 Output: 8 10 0 48 -----Explanation----- Test case 1. The initial string is 10100. There are N = 5 cells. There is one soldier in the first cell, and one soldier in the third cell. The following scenario maximizes the total time: - Limak chooses the soldier in the first cell. This soldier can move only one cell to the right. It takes 1 second to choose a soldier and 1 second for a soldier to move to the next cell (2 seconds in total). The string is 01100 now. - Limak has only one choice. He must choose the soldier in the third cell because the other soldier can't move at all (the soldier in the second cell can't move to the right because the next cell is already occupied). Choosing a soldier takes 1 second. The chosen soldier moves from the third cell to the fifth cell, which takes 2 seconds. This operation takes 1 + 2 = 3 seconds in total. The string is 01001 now. - Limak has only one choice again. Since the soldier in the last row can't move further to the right, the soldier in the second cell must be chosen. He will move 2 cells to the right. This operation takes 1 + 2 = 3 seconds in total. The string become 00011 and the game is over. The total time is 2 + 3 + 3 = 8. Test case 2. The initial string is 1100001. There is only one possible scenario: - 1100001 is changed to 1000011 in 5 seconds (1 second to choose a soldier and 4 seconds for the soldier to move 4 cells to the right). - 1000011 is changed to 0000111 in 5 seconds. The total time is 5 + 5 = 10 seconds. Test case 3. The game is over immediately because all soldiers occupy rightmost cells already. The answer is 0.
from itertools import groupby n = int(input()) p = [] for i in range(0, n): x = input() s1 = 0 a = [int(j) for j in x] from itertools import groupby s = [len(list(group)) for key, group in groupby(a)] if a[0] == 0: s = s[1:] j = 0 k = 0 while j < len(s): if j > 0: s[j] = s[j] + s[k] if j + 1 == len(s): break s1 = s[j] * (s[j + 1] + 1) + s1 k = j j = j + 2 p.append(s1) for i in p: print(i)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR VAR IF VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR IF VAR NUMBER ASSIGN VAR VAR BIN_OP VAR VAR VAR VAR IF BIN_OP VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR
Limak is a little polar bear. He is playing a video game and he needs your help. There is a row with N cells, each either empty or occupied by a soldier, denoted by '0' and '1' respectively. The goal of the game is to move all soldiers to the right (they should occupy some number of rightmost cells). The only possible command is choosing a soldier and telling him to move to the right as far as possible. Choosing a soldier takes 1 second, and a soldier moves with the speed of a cell per second. The soldier stops immediately if he is in the last cell of the row or the next cell is already occupied. Limak isn't allowed to choose a soldier that can't move at all (the chosen soldier must move at least one cell to the right). Limak enjoys this game very much and wants to play as long as possible. In particular, he doesn't start a new command while the previously chosen soldier moves. Can you tell him, how many seconds he can play at most? -----Input----- The first line of the input contains an integer T denoting the number of test cases. The description of T test cases follows. The only line of each test case contains a string S describing the row with N cells. Each character is either '0' or '1', denoting an empty cell or a cell with a soldier respectively. -----Output----- For each test case, output a single line containing one integer β€” the maximum possible number of seconds Limak will play the game. -----Constraints----- - 1 ≀ T ≀ 5 - 1 ≀ N ≀ 105 (N denotes the length of the string S) -----Subtasks----- - Subtask #1 (25 points): 1 ≀ N ≀ 10 - Subtask #2 (25 points): 1 ≀ N ≀ 2000 - Subtask #3 (50 points): Original constraints. -----Example----- Input: 4 10100 1100001 000000000111 001110100011010 Output: 8 10 0 48 -----Explanation----- Test case 1. The initial string is 10100. There are N = 5 cells. There is one soldier in the first cell, and one soldier in the third cell. The following scenario maximizes the total time: - Limak chooses the soldier in the first cell. This soldier can move only one cell to the right. It takes 1 second to choose a soldier and 1 second for a soldier to move to the next cell (2 seconds in total). The string is 01100 now. - Limak has only one choice. He must choose the soldier in the third cell because the other soldier can't move at all (the soldier in the second cell can't move to the right because the next cell is already occupied). Choosing a soldier takes 1 second. The chosen soldier moves from the third cell to the fifth cell, which takes 2 seconds. This operation takes 1 + 2 = 3 seconds in total. The string is 01001 now. - Limak has only one choice again. Since the soldier in the last row can't move further to the right, the soldier in the second cell must be chosen. He will move 2 cells to the right. This operation takes 1 + 2 = 3 seconds in total. The string become 00011 and the game is over. The total time is 2 + 3 + 3 = 8. Test case 2. The initial string is 1100001. There is only one possible scenario: - 1100001 is changed to 1000011 in 5 seconds (1 second to choose a soldier and 4 seconds for the soldier to move 4 cells to the right). - 1000011 is changed to 0000111 in 5 seconds. The total time is 5 + 5 = 10 seconds. Test case 3. The game is over immediately because all soldiers occupy rightmost cells already. The answer is 0.
def solve(): s = input() n = len(s) ans = 0 cnt = 0 selected = False for i in range(n): if s[i] == "0": ans += cnt if not selected: selected = True ans += cnt else: cnt += 1 selected = False print(ans) t = int(input()) for _ in range(t): solve()
FUNC_DEF ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR STRING VAR VAR IF VAR ASSIGN VAR NUMBER VAR VAR VAR NUMBER ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR
Limak is a little polar bear. He is playing a video game and he needs your help. There is a row with N cells, each either empty or occupied by a soldier, denoted by '0' and '1' respectively. The goal of the game is to move all soldiers to the right (they should occupy some number of rightmost cells). The only possible command is choosing a soldier and telling him to move to the right as far as possible. Choosing a soldier takes 1 second, and a soldier moves with the speed of a cell per second. The soldier stops immediately if he is in the last cell of the row or the next cell is already occupied. Limak isn't allowed to choose a soldier that can't move at all (the chosen soldier must move at least one cell to the right). Limak enjoys this game very much and wants to play as long as possible. In particular, he doesn't start a new command while the previously chosen soldier moves. Can you tell him, how many seconds he can play at most? -----Input----- The first line of the input contains an integer T denoting the number of test cases. The description of T test cases follows. The only line of each test case contains a string S describing the row with N cells. Each character is either '0' or '1', denoting an empty cell or a cell with a soldier respectively. -----Output----- For each test case, output a single line containing one integer β€” the maximum possible number of seconds Limak will play the game. -----Constraints----- - 1 ≀ T ≀ 5 - 1 ≀ N ≀ 105 (N denotes the length of the string S) -----Subtasks----- - Subtask #1 (25 points): 1 ≀ N ≀ 10 - Subtask #2 (25 points): 1 ≀ N ≀ 2000 - Subtask #3 (50 points): Original constraints. -----Example----- Input: 4 10100 1100001 000000000111 001110100011010 Output: 8 10 0 48 -----Explanation----- Test case 1. The initial string is 10100. There are N = 5 cells. There is one soldier in the first cell, and one soldier in the third cell. The following scenario maximizes the total time: - Limak chooses the soldier in the first cell. This soldier can move only one cell to the right. It takes 1 second to choose a soldier and 1 second for a soldier to move to the next cell (2 seconds in total). The string is 01100 now. - Limak has only one choice. He must choose the soldier in the third cell because the other soldier can't move at all (the soldier in the second cell can't move to the right because the next cell is already occupied). Choosing a soldier takes 1 second. The chosen soldier moves from the third cell to the fifth cell, which takes 2 seconds. This operation takes 1 + 2 = 3 seconds in total. The string is 01001 now. - Limak has only one choice again. Since the soldier in the last row can't move further to the right, the soldier in the second cell must be chosen. He will move 2 cells to the right. This operation takes 1 + 2 = 3 seconds in total. The string become 00011 and the game is over. The total time is 2 + 3 + 3 = 8. Test case 2. The initial string is 1100001. There is only one possible scenario: - 1100001 is changed to 1000011 in 5 seconds (1 second to choose a soldier and 4 seconds for the soldier to move 4 cells to the right). - 1000011 is changed to 0000111 in 5 seconds. The total time is 5 + 5 = 10 seconds. Test case 3. The game is over immediately because all soldiers occupy rightmost cells already. The answer is 0.
for _ in range(int(input())): arr = list(input()) n = len(arr) for i in reversed(range(n)): if arr[i] == "1": arr.pop() else: break n = len(arr) zero = prev = total = cnt = 0 for i in reversed(range(n)): if arr[i] == "0": zero += 1 continue if prev != zero: prev = zero cnt += 1 total += cnt + zero print(total)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR STRING EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR STRING VAR NUMBER IF VAR VAR ASSIGN VAR VAR VAR NUMBER VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR
Limak is a little polar bear. He is playing a video game and he needs your help. There is a row with N cells, each either empty or occupied by a soldier, denoted by '0' and '1' respectively. The goal of the game is to move all soldiers to the right (they should occupy some number of rightmost cells). The only possible command is choosing a soldier and telling him to move to the right as far as possible. Choosing a soldier takes 1 second, and a soldier moves with the speed of a cell per second. The soldier stops immediately if he is in the last cell of the row or the next cell is already occupied. Limak isn't allowed to choose a soldier that can't move at all (the chosen soldier must move at least one cell to the right). Limak enjoys this game very much and wants to play as long as possible. In particular, he doesn't start a new command while the previously chosen soldier moves. Can you tell him, how many seconds he can play at most? -----Input----- The first line of the input contains an integer T denoting the number of test cases. The description of T test cases follows. The only line of each test case contains a string S describing the row with N cells. Each character is either '0' or '1', denoting an empty cell or a cell with a soldier respectively. -----Output----- For each test case, output a single line containing one integer β€” the maximum possible number of seconds Limak will play the game. -----Constraints----- - 1 ≀ T ≀ 5 - 1 ≀ N ≀ 105 (N denotes the length of the string S) -----Subtasks----- - Subtask #1 (25 points): 1 ≀ N ≀ 10 - Subtask #2 (25 points): 1 ≀ N ≀ 2000 - Subtask #3 (50 points): Original constraints. -----Example----- Input: 4 10100 1100001 000000000111 001110100011010 Output: 8 10 0 48 -----Explanation----- Test case 1. The initial string is 10100. There are N = 5 cells. There is one soldier in the first cell, and one soldier in the third cell. The following scenario maximizes the total time: - Limak chooses the soldier in the first cell. This soldier can move only one cell to the right. It takes 1 second to choose a soldier and 1 second for a soldier to move to the next cell (2 seconds in total). The string is 01100 now. - Limak has only one choice. He must choose the soldier in the third cell because the other soldier can't move at all (the soldier in the second cell can't move to the right because the next cell is already occupied). Choosing a soldier takes 1 second. The chosen soldier moves from the third cell to the fifth cell, which takes 2 seconds. This operation takes 1 + 2 = 3 seconds in total. The string is 01001 now. - Limak has only one choice again. Since the soldier in the last row can't move further to the right, the soldier in the second cell must be chosen. He will move 2 cells to the right. This operation takes 1 + 2 = 3 seconds in total. The string become 00011 and the game is over. The total time is 2 + 3 + 3 = 8. Test case 2. The initial string is 1100001. There is only one possible scenario: - 1100001 is changed to 1000011 in 5 seconds (1 second to choose a soldier and 4 seconds for the soldier to move 4 cells to the right). - 1000011 is changed to 0000111 in 5 seconds. The total time is 5 + 5 = 10 seconds. Test case 3. The game is over immediately because all soldiers occupy rightmost cells already. The answer is 0.
x = int(input()) while x: s = input() if set(s) == set(["1"]): print(0) else: for i in range(len(s) - 1, 0, -1): if s[i] == "1": s = s[:-1] else: break a = [] for i in range(len(s)): if s[i] == "1": a.append(i + 1) c = 0 ans = 0 if len(a): space = 0 for i in range(len(a) - 1): c = c + 1 space = a[i + 1] - a[i] - 1 if space: ans += space * c + c c = c + 1 space = len(s) - a[len(a) - 1] ans += space * c + c print(ans) x = x - 1
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR ASSIGN VAR FUNC_CALL VAR IF FUNC_CALL VAR VAR FUNC_CALL VAR LIST STRING EXPR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER IF VAR VAR STRING ASSIGN VAR VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR STRING EXPR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR VAR NUMBER IF VAR VAR BIN_OP BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR BIN_OP BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER
Limak is a little polar bear. He is playing a video game and he needs your help. There is a row with N cells, each either empty or occupied by a soldier, denoted by '0' and '1' respectively. The goal of the game is to move all soldiers to the right (they should occupy some number of rightmost cells). The only possible command is choosing a soldier and telling him to move to the right as far as possible. Choosing a soldier takes 1 second, and a soldier moves with the speed of a cell per second. The soldier stops immediately if he is in the last cell of the row or the next cell is already occupied. Limak isn't allowed to choose a soldier that can't move at all (the chosen soldier must move at least one cell to the right). Limak enjoys this game very much and wants to play as long as possible. In particular, he doesn't start a new command while the previously chosen soldier moves. Can you tell him, how many seconds he can play at most? -----Input----- The first line of the input contains an integer T denoting the number of test cases. The description of T test cases follows. The only line of each test case contains a string S describing the row with N cells. Each character is either '0' or '1', denoting an empty cell or a cell with a soldier respectively. -----Output----- For each test case, output a single line containing one integer β€” the maximum possible number of seconds Limak will play the game. -----Constraints----- - 1 ≀ T ≀ 5 - 1 ≀ N ≀ 105 (N denotes the length of the string S) -----Subtasks----- - Subtask #1 (25 points): 1 ≀ N ≀ 10 - Subtask #2 (25 points): 1 ≀ N ≀ 2000 - Subtask #3 (50 points): Original constraints. -----Example----- Input: 4 10100 1100001 000000000111 001110100011010 Output: 8 10 0 48 -----Explanation----- Test case 1. The initial string is 10100. There are N = 5 cells. There is one soldier in the first cell, and one soldier in the third cell. The following scenario maximizes the total time: - Limak chooses the soldier in the first cell. This soldier can move only one cell to the right. It takes 1 second to choose a soldier and 1 second for a soldier to move to the next cell (2 seconds in total). The string is 01100 now. - Limak has only one choice. He must choose the soldier in the third cell because the other soldier can't move at all (the soldier in the second cell can't move to the right because the next cell is already occupied). Choosing a soldier takes 1 second. The chosen soldier moves from the third cell to the fifth cell, which takes 2 seconds. This operation takes 1 + 2 = 3 seconds in total. The string is 01001 now. - Limak has only one choice again. Since the soldier in the last row can't move further to the right, the soldier in the second cell must be chosen. He will move 2 cells to the right. This operation takes 1 + 2 = 3 seconds in total. The string become 00011 and the game is over. The total time is 2 + 3 + 3 = 8. Test case 2. The initial string is 1100001. There is only one possible scenario: - 1100001 is changed to 1000011 in 5 seconds (1 second to choose a soldier and 4 seconds for the soldier to move 4 cells to the right). - 1000011 is changed to 0000111 in 5 seconds. The total time is 5 + 5 = 10 seconds. Test case 3. The game is over immediately because all soldiers occupy rightmost cells already. The answer is 0.
t = int(input()) for i in range(t): one = 0 sec = 0 given = input() given = given[given.find("1") :] while True: if not "0" in given: break index0 = given.find("0") one += index0 given = given[index0:] if not "1" in given: zero = len(given) sec += one * (zero + 1) break index1 = given.find("1") zero = index1 sec += one * (zero + 1) given = given[index1:] print(sec)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR STRING WHILE NUMBER IF STRING VAR ASSIGN VAR FUNC_CALL VAR STRING VAR VAR ASSIGN VAR VAR VAR IF STRING VAR ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR VAR VAR BIN_OP VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR VAR
Limak is a little polar bear. He is playing a video game and he needs your help. There is a row with N cells, each either empty or occupied by a soldier, denoted by '0' and '1' respectively. The goal of the game is to move all soldiers to the right (they should occupy some number of rightmost cells). The only possible command is choosing a soldier and telling him to move to the right as far as possible. Choosing a soldier takes 1 second, and a soldier moves with the speed of a cell per second. The soldier stops immediately if he is in the last cell of the row or the next cell is already occupied. Limak isn't allowed to choose a soldier that can't move at all (the chosen soldier must move at least one cell to the right). Limak enjoys this game very much and wants to play as long as possible. In particular, he doesn't start a new command while the previously chosen soldier moves. Can you tell him, how many seconds he can play at most? -----Input----- The first line of the input contains an integer T denoting the number of test cases. The description of T test cases follows. The only line of each test case contains a string S describing the row with N cells. Each character is either '0' or '1', denoting an empty cell or a cell with a soldier respectively. -----Output----- For each test case, output a single line containing one integer β€” the maximum possible number of seconds Limak will play the game. -----Constraints----- - 1 ≀ T ≀ 5 - 1 ≀ N ≀ 105 (N denotes the length of the string S) -----Subtasks----- - Subtask #1 (25 points): 1 ≀ N ≀ 10 - Subtask #2 (25 points): 1 ≀ N ≀ 2000 - Subtask #3 (50 points): Original constraints. -----Example----- Input: 4 10100 1100001 000000000111 001110100011010 Output: 8 10 0 48 -----Explanation----- Test case 1. The initial string is 10100. There are N = 5 cells. There is one soldier in the first cell, and one soldier in the third cell. The following scenario maximizes the total time: - Limak chooses the soldier in the first cell. This soldier can move only one cell to the right. It takes 1 second to choose a soldier and 1 second for a soldier to move to the next cell (2 seconds in total). The string is 01100 now. - Limak has only one choice. He must choose the soldier in the third cell because the other soldier can't move at all (the soldier in the second cell can't move to the right because the next cell is already occupied). Choosing a soldier takes 1 second. The chosen soldier moves from the third cell to the fifth cell, which takes 2 seconds. This operation takes 1 + 2 = 3 seconds in total. The string is 01001 now. - Limak has only one choice again. Since the soldier in the last row can't move further to the right, the soldier in the second cell must be chosen. He will move 2 cells to the right. This operation takes 1 + 2 = 3 seconds in total. The string become 00011 and the game is over. The total time is 2 + 3 + 3 = 8. Test case 2. The initial string is 1100001. There is only one possible scenario: - 1100001 is changed to 1000011 in 5 seconds (1 second to choose a soldier and 4 seconds for the soldier to move 4 cells to the right). - 1000011 is changed to 0000111 in 5 seconds. The total time is 5 + 5 = 10 seconds. Test case 3. The game is over immediately because all soldiers occupy rightmost cells already. The answer is 0.
try: t = int(input()) while t > 0: t -= 1 s = input() intermediate = 0 count = 0 end = len(s) - 1 if s[len(s) - 1] == "1": while end >= 0: if s[end] != "1" or s[end] == "1" and end == 0: pos = end - 1 last = end break end -= 1 else: pos = end - 1 last = end while pos >= 0: if s[pos] == "1": count = count + (last - pos) + intermediate + 1 last -= 1 if pos - 1 >= 0 and s[pos - 1] != "1": intermediate += 1 pos -= 1 print(count) except EOFError: pass
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER IF VAR BIN_OP FUNC_CALL VAR VAR NUMBER STRING WHILE VAR NUMBER IF VAR VAR STRING VAR VAR STRING VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR WHILE VAR NUMBER IF VAR VAR STRING ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR BIN_OP VAR VAR VAR NUMBER VAR NUMBER IF BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER STRING VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR VAR
Limak is a little polar bear. He is playing a video game and he needs your help. There is a row with N cells, each either empty or occupied by a soldier, denoted by '0' and '1' respectively. The goal of the game is to move all soldiers to the right (they should occupy some number of rightmost cells). The only possible command is choosing a soldier and telling him to move to the right as far as possible. Choosing a soldier takes 1 second, and a soldier moves with the speed of a cell per second. The soldier stops immediately if he is in the last cell of the row or the next cell is already occupied. Limak isn't allowed to choose a soldier that can't move at all (the chosen soldier must move at least one cell to the right). Limak enjoys this game very much and wants to play as long as possible. In particular, he doesn't start a new command while the previously chosen soldier moves. Can you tell him, how many seconds he can play at most? -----Input----- The first line of the input contains an integer T denoting the number of test cases. The description of T test cases follows. The only line of each test case contains a string S describing the row with N cells. Each character is either '0' or '1', denoting an empty cell or a cell with a soldier respectively. -----Output----- For each test case, output a single line containing one integer β€” the maximum possible number of seconds Limak will play the game. -----Constraints----- - 1 ≀ T ≀ 5 - 1 ≀ N ≀ 105 (N denotes the length of the string S) -----Subtasks----- - Subtask #1 (25 points): 1 ≀ N ≀ 10 - Subtask #2 (25 points): 1 ≀ N ≀ 2000 - Subtask #3 (50 points): Original constraints. -----Example----- Input: 4 10100 1100001 000000000111 001110100011010 Output: 8 10 0 48 -----Explanation----- Test case 1. The initial string is 10100. There are N = 5 cells. There is one soldier in the first cell, and one soldier in the third cell. The following scenario maximizes the total time: - Limak chooses the soldier in the first cell. This soldier can move only one cell to the right. It takes 1 second to choose a soldier and 1 second for a soldier to move to the next cell (2 seconds in total). The string is 01100 now. - Limak has only one choice. He must choose the soldier in the third cell because the other soldier can't move at all (the soldier in the second cell can't move to the right because the next cell is already occupied). Choosing a soldier takes 1 second. The chosen soldier moves from the third cell to the fifth cell, which takes 2 seconds. This operation takes 1 + 2 = 3 seconds in total. The string is 01001 now. - Limak has only one choice again. Since the soldier in the last row can't move further to the right, the soldier in the second cell must be chosen. He will move 2 cells to the right. This operation takes 1 + 2 = 3 seconds in total. The string become 00011 and the game is over. The total time is 2 + 3 + 3 = 8. Test case 2. The initial string is 1100001. There is only one possible scenario: - 1100001 is changed to 1000011 in 5 seconds (1 second to choose a soldier and 4 seconds for the soldier to move 4 cells to the right). - 1000011 is changed to 0000111 in 5 seconds. The total time is 5 + 5 = 10 seconds. Test case 3. The game is over immediately because all soldiers occupy rightmost cells already. The answer is 0.
t = int(input()) for T in range(0, t): str = input() a = [] for i in range(0, len(str) - 1): if str[i] == "1": c = 0 j = i + 1 while str[j] == "0": c += 1 if j < len(str) - 1: j += 1 else: break a.append(c + 1) tot = 0 for i in range(0, len(a)): if a[i] != 1: tot += a[i] * (i + 1) print(tot)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER IF VAR VAR STRING ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR VAR STRING VAR NUMBER IF VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF VAR VAR NUMBER VAR BIN_OP VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR
Limak is a little polar bear. He is playing a video game and he needs your help. There is a row with N cells, each either empty or occupied by a soldier, denoted by '0' and '1' respectively. The goal of the game is to move all soldiers to the right (they should occupy some number of rightmost cells). The only possible command is choosing a soldier and telling him to move to the right as far as possible. Choosing a soldier takes 1 second, and a soldier moves with the speed of a cell per second. The soldier stops immediately if he is in the last cell of the row or the next cell is already occupied. Limak isn't allowed to choose a soldier that can't move at all (the chosen soldier must move at least one cell to the right). Limak enjoys this game very much and wants to play as long as possible. In particular, he doesn't start a new command while the previously chosen soldier moves. Can you tell him, how many seconds he can play at most? -----Input----- The first line of the input contains an integer T denoting the number of test cases. The description of T test cases follows. The only line of each test case contains a string S describing the row with N cells. Each character is either '0' or '1', denoting an empty cell or a cell with a soldier respectively. -----Output----- For each test case, output a single line containing one integer β€” the maximum possible number of seconds Limak will play the game. -----Constraints----- - 1 ≀ T ≀ 5 - 1 ≀ N ≀ 105 (N denotes the length of the string S) -----Subtasks----- - Subtask #1 (25 points): 1 ≀ N ≀ 10 - Subtask #2 (25 points): 1 ≀ N ≀ 2000 - Subtask #3 (50 points): Original constraints. -----Example----- Input: 4 10100 1100001 000000000111 001110100011010 Output: 8 10 0 48 -----Explanation----- Test case 1. The initial string is 10100. There are N = 5 cells. There is one soldier in the first cell, and one soldier in the third cell. The following scenario maximizes the total time: - Limak chooses the soldier in the first cell. This soldier can move only one cell to the right. It takes 1 second to choose a soldier and 1 second for a soldier to move to the next cell (2 seconds in total). The string is 01100 now. - Limak has only one choice. He must choose the soldier in the third cell because the other soldier can't move at all (the soldier in the second cell can't move to the right because the next cell is already occupied). Choosing a soldier takes 1 second. The chosen soldier moves from the third cell to the fifth cell, which takes 2 seconds. This operation takes 1 + 2 = 3 seconds in total. The string is 01001 now. - Limak has only one choice again. Since the soldier in the last row can't move further to the right, the soldier in the second cell must be chosen. He will move 2 cells to the right. This operation takes 1 + 2 = 3 seconds in total. The string become 00011 and the game is over. The total time is 2 + 3 + 3 = 8. Test case 2. The initial string is 1100001. There is only one possible scenario: - 1100001 is changed to 1000011 in 5 seconds (1 second to choose a soldier and 4 seconds for the soldier to move 4 cells to the right). - 1000011 is changed to 0000111 in 5 seconds. The total time is 5 + 5 = 10 seconds. Test case 3. The game is over immediately because all soldiers occupy rightmost cells already. The answer is 0.
t = int(input()) for i in range(t): str = input() ls = list(str) a = 0 r = 0 n = ls.count("1") for j in range(len(ls) - 1, -1, -1): if ls[j] == "1": if a == 0: n = n - 1 else: r = r + (a + 1) * n n = n - 1 a = 0 else: a = a + 1 print(r)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER IF VAR VAR STRING IF VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR
Limak is a little polar bear. He is playing a video game and he needs your help. There is a row with N cells, each either empty or occupied by a soldier, denoted by '0' and '1' respectively. The goal of the game is to move all soldiers to the right (they should occupy some number of rightmost cells). The only possible command is choosing a soldier and telling him to move to the right as far as possible. Choosing a soldier takes 1 second, and a soldier moves with the speed of a cell per second. The soldier stops immediately if he is in the last cell of the row or the next cell is already occupied. Limak isn't allowed to choose a soldier that can't move at all (the chosen soldier must move at least one cell to the right). Limak enjoys this game very much and wants to play as long as possible. In particular, he doesn't start a new command while the previously chosen soldier moves. Can you tell him, how many seconds he can play at most? -----Input----- The first line of the input contains an integer T denoting the number of test cases. The description of T test cases follows. The only line of each test case contains a string S describing the row with N cells. Each character is either '0' or '1', denoting an empty cell or a cell with a soldier respectively. -----Output----- For each test case, output a single line containing one integer β€” the maximum possible number of seconds Limak will play the game. -----Constraints----- - 1 ≀ T ≀ 5 - 1 ≀ N ≀ 105 (N denotes the length of the string S) -----Subtasks----- - Subtask #1 (25 points): 1 ≀ N ≀ 10 - Subtask #2 (25 points): 1 ≀ N ≀ 2000 - Subtask #3 (50 points): Original constraints. -----Example----- Input: 4 10100 1100001 000000000111 001110100011010 Output: 8 10 0 48 -----Explanation----- Test case 1. The initial string is 10100. There are N = 5 cells. There is one soldier in the first cell, and one soldier in the third cell. The following scenario maximizes the total time: - Limak chooses the soldier in the first cell. This soldier can move only one cell to the right. It takes 1 second to choose a soldier and 1 second for a soldier to move to the next cell (2 seconds in total). The string is 01100 now. - Limak has only one choice. He must choose the soldier in the third cell because the other soldier can't move at all (the soldier in the second cell can't move to the right because the next cell is already occupied). Choosing a soldier takes 1 second. The chosen soldier moves from the third cell to the fifth cell, which takes 2 seconds. This operation takes 1 + 2 = 3 seconds in total. The string is 01001 now. - Limak has only one choice again. Since the soldier in the last row can't move further to the right, the soldier in the second cell must be chosen. He will move 2 cells to the right. This operation takes 1 + 2 = 3 seconds in total. The string become 00011 and the game is over. The total time is 2 + 3 + 3 = 8. Test case 2. The initial string is 1100001. There is only one possible scenario: - 1100001 is changed to 1000011 in 5 seconds (1 second to choose a soldier and 4 seconds for the soldier to move 4 cells to the right). - 1000011 is changed to 0000111 in 5 seconds. The total time is 5 + 5 = 10 seconds. Test case 3. The game is over immediately because all soldiers occupy rightmost cells already. The answer is 0.
import itertools import sys def get_group_list(row): group_size = 0 grouped_row = [] for i in row: if i == 0: grouped_row.append(group_size) group_size = 0 grouped_row.append(i) elif i == 1: group_size += 1 return list(filter(lambda x: x > 0, grouped_row)) def get_distance_list(row): zeroes = 0 total = 0 for i in reversed(row): if i == 0: zeroes += 1 elif i == 1: total += zeroes return total def grouping_method(row): distance_sum = get_distance_list(row) group_list = get_group_list(row) total_group_sum = 0 for i, group in enumerate(reversed(group_list), start=1): total_group_sum += i * group return distance_sum + total_group_sum test_cases = int(sys.stdin.readline().strip()) for test in range(test_cases): row = [int(i) for i in sys.stdin.readline().strip()] seconds = grouping_method(row) print(seconds)
IMPORT IMPORT FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR LIST FOR VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR IF VAR NUMBER VAR NUMBER RETURN FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER VAR FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR NUMBER VAR NUMBER IF VAR NUMBER VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER VAR BIN_OP VAR VAR RETURN BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR
Limak is a little polar bear. He is playing a video game and he needs your help. There is a row with N cells, each either empty or occupied by a soldier, denoted by '0' and '1' respectively. The goal of the game is to move all soldiers to the right (they should occupy some number of rightmost cells). The only possible command is choosing a soldier and telling him to move to the right as far as possible. Choosing a soldier takes 1 second, and a soldier moves with the speed of a cell per second. The soldier stops immediately if he is in the last cell of the row or the next cell is already occupied. Limak isn't allowed to choose a soldier that can't move at all (the chosen soldier must move at least one cell to the right). Limak enjoys this game very much and wants to play as long as possible. In particular, he doesn't start a new command while the previously chosen soldier moves. Can you tell him, how many seconds he can play at most? -----Input----- The first line of the input contains an integer T denoting the number of test cases. The description of T test cases follows. The only line of each test case contains a string S describing the row with N cells. Each character is either '0' or '1', denoting an empty cell or a cell with a soldier respectively. -----Output----- For each test case, output a single line containing one integer β€” the maximum possible number of seconds Limak will play the game. -----Constraints----- - 1 ≀ T ≀ 5 - 1 ≀ N ≀ 105 (N denotes the length of the string S) -----Subtasks----- - Subtask #1 (25 points): 1 ≀ N ≀ 10 - Subtask #2 (25 points): 1 ≀ N ≀ 2000 - Subtask #3 (50 points): Original constraints. -----Example----- Input: 4 10100 1100001 000000000111 001110100011010 Output: 8 10 0 48 -----Explanation----- Test case 1. The initial string is 10100. There are N = 5 cells. There is one soldier in the first cell, and one soldier in the third cell. The following scenario maximizes the total time: - Limak chooses the soldier in the first cell. This soldier can move only one cell to the right. It takes 1 second to choose a soldier and 1 second for a soldier to move to the next cell (2 seconds in total). The string is 01100 now. - Limak has only one choice. He must choose the soldier in the third cell because the other soldier can't move at all (the soldier in the second cell can't move to the right because the next cell is already occupied). Choosing a soldier takes 1 second. The chosen soldier moves from the third cell to the fifth cell, which takes 2 seconds. This operation takes 1 + 2 = 3 seconds in total. The string is 01001 now. - Limak has only one choice again. Since the soldier in the last row can't move further to the right, the soldier in the second cell must be chosen. He will move 2 cells to the right. This operation takes 1 + 2 = 3 seconds in total. The string become 00011 and the game is over. The total time is 2 + 3 + 3 = 8. Test case 2. The initial string is 1100001. There is only one possible scenario: - 1100001 is changed to 1000011 in 5 seconds (1 second to choose a soldier and 4 seconds for the soldier to move 4 cells to the right). - 1000011 is changed to 0000111 in 5 seconds. The total time is 5 + 5 = 10 seconds. Test case 3. The game is over immediately because all soldiers occupy rightmost cells already. The answer is 0.
t = int(input()) while t > 0: t -= 1 count, st = 0, list(input()) m = {} tmpC = 0 for i in range(len(st)): if st[i] == "1": m[len(m)] = tmpC tmpC = 0 else: tmpC += 1 m[len(m)] = tmpC s = 0 for key, value in m.items(): tmp = key * value s += tmp if tmp != 0: s += key print(s)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR NUMBER VAR NUMBER ASSIGN VAR VAR NUMBER FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR DICT ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR STRING ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR ASSIGN VAR BIN_OP VAR VAR VAR VAR IF VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR
Limak is a little polar bear. He is playing a video game and he needs your help. There is a row with N cells, each either empty or occupied by a soldier, denoted by '0' and '1' respectively. The goal of the game is to move all soldiers to the right (they should occupy some number of rightmost cells). The only possible command is choosing a soldier and telling him to move to the right as far as possible. Choosing a soldier takes 1 second, and a soldier moves with the speed of a cell per second. The soldier stops immediately if he is in the last cell of the row or the next cell is already occupied. Limak isn't allowed to choose a soldier that can't move at all (the chosen soldier must move at least one cell to the right). Limak enjoys this game very much and wants to play as long as possible. In particular, he doesn't start a new command while the previously chosen soldier moves. Can you tell him, how many seconds he can play at most? -----Input----- The first line of the input contains an integer T denoting the number of test cases. The description of T test cases follows. The only line of each test case contains a string S describing the row with N cells. Each character is either '0' or '1', denoting an empty cell or a cell with a soldier respectively. -----Output----- For each test case, output a single line containing one integer β€” the maximum possible number of seconds Limak will play the game. -----Constraints----- - 1 ≀ T ≀ 5 - 1 ≀ N ≀ 105 (N denotes the length of the string S) -----Subtasks----- - Subtask #1 (25 points): 1 ≀ N ≀ 10 - Subtask #2 (25 points): 1 ≀ N ≀ 2000 - Subtask #3 (50 points): Original constraints. -----Example----- Input: 4 10100 1100001 000000000111 001110100011010 Output: 8 10 0 48 -----Explanation----- Test case 1. The initial string is 10100. There are N = 5 cells. There is one soldier in the first cell, and one soldier in the third cell. The following scenario maximizes the total time: - Limak chooses the soldier in the first cell. This soldier can move only one cell to the right. It takes 1 second to choose a soldier and 1 second for a soldier to move to the next cell (2 seconds in total). The string is 01100 now. - Limak has only one choice. He must choose the soldier in the third cell because the other soldier can't move at all (the soldier in the second cell can't move to the right because the next cell is already occupied). Choosing a soldier takes 1 second. The chosen soldier moves from the third cell to the fifth cell, which takes 2 seconds. This operation takes 1 + 2 = 3 seconds in total. The string is 01001 now. - Limak has only one choice again. Since the soldier in the last row can't move further to the right, the soldier in the second cell must be chosen. He will move 2 cells to the right. This operation takes 1 + 2 = 3 seconds in total. The string become 00011 and the game is over. The total time is 2 + 3 + 3 = 8. Test case 2. The initial string is 1100001. There is only one possible scenario: - 1100001 is changed to 1000011 in 5 seconds (1 second to choose a soldier and 4 seconds for the soldier to move 4 cells to the right). - 1000011 is changed to 0000111 in 5 seconds. The total time is 5 + 5 = 10 seconds. Test case 3. The game is over immediately because all soldiers occupy rightmost cells already. The answer is 0.
for _ in range(int(input())): animal = input() answer = 0 cer = 0 sqwerty = 0 po = 0 for lk in range(2): po += 1 for lk in range(2): po += 1 for de in range(2): if de != 0: po += 1 for i in range(len(animal)): if animal[i] == "1": answer += sqwerty * cer cer += 1 for lk in range(2): po += 1 for lk in range(2): po += 1 for de in range(2): if de != 0: po += 1 sqwerty = 0 elif cer != 0: if sqwerty == 0: sqwerty = 1 sqwerty += 1 for lk in range(2): po += 1 for lk in range(2): po += 1 for de in range(2): if de != 0: po += 1 if animal[len(animal) - 1] == "0": answer += sqwerty * cer print(answer)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER IF VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR STRING VAR BIN_OP VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER IF VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER IF VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER IF VAR NUMBER VAR NUMBER IF VAR BIN_OP FUNC_CALL VAR VAR NUMBER STRING VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR
Limak is a little polar bear. He is playing a video game and he needs your help. There is a row with N cells, each either empty or occupied by a soldier, denoted by '0' and '1' respectively. The goal of the game is to move all soldiers to the right (they should occupy some number of rightmost cells). The only possible command is choosing a soldier and telling him to move to the right as far as possible. Choosing a soldier takes 1 second, and a soldier moves with the speed of a cell per second. The soldier stops immediately if he is in the last cell of the row or the next cell is already occupied. Limak isn't allowed to choose a soldier that can't move at all (the chosen soldier must move at least one cell to the right). Limak enjoys this game very much and wants to play as long as possible. In particular, he doesn't start a new command while the previously chosen soldier moves. Can you tell him, how many seconds he can play at most? -----Input----- The first line of the input contains an integer T denoting the number of test cases. The description of T test cases follows. The only line of each test case contains a string S describing the row with N cells. Each character is either '0' or '1', denoting an empty cell or a cell with a soldier respectively. -----Output----- For each test case, output a single line containing one integer β€” the maximum possible number of seconds Limak will play the game. -----Constraints----- - 1 ≀ T ≀ 5 - 1 ≀ N ≀ 105 (N denotes the length of the string S) -----Subtasks----- - Subtask #1 (25 points): 1 ≀ N ≀ 10 - Subtask #2 (25 points): 1 ≀ N ≀ 2000 - Subtask #3 (50 points): Original constraints. -----Example----- Input: 4 10100 1100001 000000000111 001110100011010 Output: 8 10 0 48 -----Explanation----- Test case 1. The initial string is 10100. There are N = 5 cells. There is one soldier in the first cell, and one soldier in the third cell. The following scenario maximizes the total time: - Limak chooses the soldier in the first cell. This soldier can move only one cell to the right. It takes 1 second to choose a soldier and 1 second for a soldier to move to the next cell (2 seconds in total). The string is 01100 now. - Limak has only one choice. He must choose the soldier in the third cell because the other soldier can't move at all (the soldier in the second cell can't move to the right because the next cell is already occupied). Choosing a soldier takes 1 second. The chosen soldier moves from the third cell to the fifth cell, which takes 2 seconds. This operation takes 1 + 2 = 3 seconds in total. The string is 01001 now. - Limak has only one choice again. Since the soldier in the last row can't move further to the right, the soldier in the second cell must be chosen. He will move 2 cells to the right. This operation takes 1 + 2 = 3 seconds in total. The string become 00011 and the game is over. The total time is 2 + 3 + 3 = 8. Test case 2. The initial string is 1100001. There is only one possible scenario: - 1100001 is changed to 1000011 in 5 seconds (1 second to choose a soldier and 4 seconds for the soldier to move 4 cells to the right). - 1000011 is changed to 0000111 in 5 seconds. The total time is 5 + 5 = 10 seconds. Test case 3. The game is over immediately because all soldiers occupy rightmost cells already. The answer is 0.
t = int(input()) for e in range(t): s = input() ans = 0 a, lens = [], len(s) for i in range(lens): if s[i] == "1": a.append(i) n = len(a) for i in range(n): if i == n - 1: if a[i] != lens - 1: ans += (lens - a[i]) * (i + 1) else: if a[i + 1] == a[i] + 1: continue ans += (a[i + 1] - a[i]) * (i + 1) print(ans)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR VAR LIST FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR STRING EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR BIN_OP VAR NUMBER IF VAR VAR BIN_OP VAR NUMBER VAR BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR NUMBER IF VAR BIN_OP VAR NUMBER BIN_OP VAR VAR NUMBER VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR
Limak is a little polar bear. He is playing a video game and he needs your help. There is a row with N cells, each either empty or occupied by a soldier, denoted by '0' and '1' respectively. The goal of the game is to move all soldiers to the right (they should occupy some number of rightmost cells). The only possible command is choosing a soldier and telling him to move to the right as far as possible. Choosing a soldier takes 1 second, and a soldier moves with the speed of a cell per second. The soldier stops immediately if he is in the last cell of the row or the next cell is already occupied. Limak isn't allowed to choose a soldier that can't move at all (the chosen soldier must move at least one cell to the right). Limak enjoys this game very much and wants to play as long as possible. In particular, he doesn't start a new command while the previously chosen soldier moves. Can you tell him, how many seconds he can play at most? -----Input----- The first line of the input contains an integer T denoting the number of test cases. The description of T test cases follows. The only line of each test case contains a string S describing the row with N cells. Each character is either '0' or '1', denoting an empty cell or a cell with a soldier respectively. -----Output----- For each test case, output a single line containing one integer β€” the maximum possible number of seconds Limak will play the game. -----Constraints----- - 1 ≀ T ≀ 5 - 1 ≀ N ≀ 105 (N denotes the length of the string S) -----Subtasks----- - Subtask #1 (25 points): 1 ≀ N ≀ 10 - Subtask #2 (25 points): 1 ≀ N ≀ 2000 - Subtask #3 (50 points): Original constraints. -----Example----- Input: 4 10100 1100001 000000000111 001110100011010 Output: 8 10 0 48 -----Explanation----- Test case 1. The initial string is 10100. There are N = 5 cells. There is one soldier in the first cell, and one soldier in the third cell. The following scenario maximizes the total time: - Limak chooses the soldier in the first cell. This soldier can move only one cell to the right. It takes 1 second to choose a soldier and 1 second for a soldier to move to the next cell (2 seconds in total). The string is 01100 now. - Limak has only one choice. He must choose the soldier in the third cell because the other soldier can't move at all (the soldier in the second cell can't move to the right because the next cell is already occupied). Choosing a soldier takes 1 second. The chosen soldier moves from the third cell to the fifth cell, which takes 2 seconds. This operation takes 1 + 2 = 3 seconds in total. The string is 01001 now. - Limak has only one choice again. Since the soldier in the last row can't move further to the right, the soldier in the second cell must be chosen. He will move 2 cells to the right. This operation takes 1 + 2 = 3 seconds in total. The string become 00011 and the game is over. The total time is 2 + 3 + 3 = 8. Test case 2. The initial string is 1100001. There is only one possible scenario: - 1100001 is changed to 1000011 in 5 seconds (1 second to choose a soldier and 4 seconds for the soldier to move 4 cells to the right). - 1000011 is changed to 0000111 in 5 seconds. The total time is 5 + 5 = 10 seconds. Test case 3. The game is over immediately because all soldiers occupy rightmost cells already. The answer is 0.
def getChar(s, ch): for i in range(len(s)): if s[i] != ch: return s[i:], i return "", len(s) T = int(input()) for t in range(T): ozs = [] tot = 0 count = 0 moving = False s = input().strip() s = s[s.find("1") :] while len(s) > 0: ozs.append([0, 0]) s, ozs[count][0] = getChar(s, "1") if len(s) == 0: ozs.pop() break s, ozs[count][1] = getChar(s, "0") count += 1 sub_t = 0 for i in reversed(ozs): sub_t += i[1] + 1 tot += i[0] * sub_t print(tot)
FUNC_DEF FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR RETURN VAR VAR VAR RETURN STRING FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR STRING WHILE FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR LIST NUMBER NUMBER ASSIGN VAR VAR VAR NUMBER FUNC_CALL VAR VAR STRING IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR VAR VAR NUMBER FUNC_CALL VAR VAR STRING VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR VAR
Limak is a little polar bear. He is playing a video game and he needs your help. There is a row with N cells, each either empty or occupied by a soldier, denoted by '0' and '1' respectively. The goal of the game is to move all soldiers to the right (they should occupy some number of rightmost cells). The only possible command is choosing a soldier and telling him to move to the right as far as possible. Choosing a soldier takes 1 second, and a soldier moves with the speed of a cell per second. The soldier stops immediately if he is in the last cell of the row or the next cell is already occupied. Limak isn't allowed to choose a soldier that can't move at all (the chosen soldier must move at least one cell to the right). Limak enjoys this game very much and wants to play as long as possible. In particular, he doesn't start a new command while the previously chosen soldier moves. Can you tell him, how many seconds he can play at most? -----Input----- The first line of the input contains an integer T denoting the number of test cases. The description of T test cases follows. The only line of each test case contains a string S describing the row with N cells. Each character is either '0' or '1', denoting an empty cell or a cell with a soldier respectively. -----Output----- For each test case, output a single line containing one integer β€” the maximum possible number of seconds Limak will play the game. -----Constraints----- - 1 ≀ T ≀ 5 - 1 ≀ N ≀ 105 (N denotes the length of the string S) -----Subtasks----- - Subtask #1 (25 points): 1 ≀ N ≀ 10 - Subtask #2 (25 points): 1 ≀ N ≀ 2000 - Subtask #3 (50 points): Original constraints. -----Example----- Input: 4 10100 1100001 000000000111 001110100011010 Output: 8 10 0 48 -----Explanation----- Test case 1. The initial string is 10100. There are N = 5 cells. There is one soldier in the first cell, and one soldier in the third cell. The following scenario maximizes the total time: - Limak chooses the soldier in the first cell. This soldier can move only one cell to the right. It takes 1 second to choose a soldier and 1 second for a soldier to move to the next cell (2 seconds in total). The string is 01100 now. - Limak has only one choice. He must choose the soldier in the third cell because the other soldier can't move at all (the soldier in the second cell can't move to the right because the next cell is already occupied). Choosing a soldier takes 1 second. The chosen soldier moves from the third cell to the fifth cell, which takes 2 seconds. This operation takes 1 + 2 = 3 seconds in total. The string is 01001 now. - Limak has only one choice again. Since the soldier in the last row can't move further to the right, the soldier in the second cell must be chosen. He will move 2 cells to the right. This operation takes 1 + 2 = 3 seconds in total. The string become 00011 and the game is over. The total time is 2 + 3 + 3 = 8. Test case 2. The initial string is 1100001. There is only one possible scenario: - 1100001 is changed to 1000011 in 5 seconds (1 second to choose a soldier and 4 seconds for the soldier to move 4 cells to the right). - 1000011 is changed to 0000111 in 5 seconds. The total time is 5 + 5 = 10 seconds. Test case 3. The game is over immediately because all soldiers occupy rightmost cells already. The answer is 0.
repe = int(input()) casos = 0 res = [] while casos < repe: cadena = input() soldados = 0 espacios = 0 tiempo = 0 tt = 0 for c in cadena: if c == "1": if espacios > 0: tiempo = soldados + soldados * espacios tt += tiempo espacios = 0 soldados += 1 else: espacios += 1 if espacios > 0: tt += soldados + soldados * espacios res.append(tt) casos += 1 for c in res: print(str(c))
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR LIST WHILE VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF VAR STRING IF VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR NUMBER VAR NUMBER VAR NUMBER IF VAR NUMBER VAR BIN_OP VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER FOR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR
Limak is a little polar bear. He is playing a video game and he needs your help. There is a row with N cells, each either empty or occupied by a soldier, denoted by '0' and '1' respectively. The goal of the game is to move all soldiers to the right (they should occupy some number of rightmost cells). The only possible command is choosing a soldier and telling him to move to the right as far as possible. Choosing a soldier takes 1 second, and a soldier moves with the speed of a cell per second. The soldier stops immediately if he is in the last cell of the row or the next cell is already occupied. Limak isn't allowed to choose a soldier that can't move at all (the chosen soldier must move at least one cell to the right). Limak enjoys this game very much and wants to play as long as possible. In particular, he doesn't start a new command while the previously chosen soldier moves. Can you tell him, how many seconds he can play at most? -----Input----- The first line of the input contains an integer T denoting the number of test cases. The description of T test cases follows. The only line of each test case contains a string S describing the row with N cells. Each character is either '0' or '1', denoting an empty cell or a cell with a soldier respectively. -----Output----- For each test case, output a single line containing one integer β€” the maximum possible number of seconds Limak will play the game. -----Constraints----- - 1 ≀ T ≀ 5 - 1 ≀ N ≀ 105 (N denotes the length of the string S) -----Subtasks----- - Subtask #1 (25 points): 1 ≀ N ≀ 10 - Subtask #2 (25 points): 1 ≀ N ≀ 2000 - Subtask #3 (50 points): Original constraints. -----Example----- Input: 4 10100 1100001 000000000111 001110100011010 Output: 8 10 0 48 -----Explanation----- Test case 1. The initial string is 10100. There are N = 5 cells. There is one soldier in the first cell, and one soldier in the third cell. The following scenario maximizes the total time: - Limak chooses the soldier in the first cell. This soldier can move only one cell to the right. It takes 1 second to choose a soldier and 1 second for a soldier to move to the next cell (2 seconds in total). The string is 01100 now. - Limak has only one choice. He must choose the soldier in the third cell because the other soldier can't move at all (the soldier in the second cell can't move to the right because the next cell is already occupied). Choosing a soldier takes 1 second. The chosen soldier moves from the third cell to the fifth cell, which takes 2 seconds. This operation takes 1 + 2 = 3 seconds in total. The string is 01001 now. - Limak has only one choice again. Since the soldier in the last row can't move further to the right, the soldier in the second cell must be chosen. He will move 2 cells to the right. This operation takes 1 + 2 = 3 seconds in total. The string become 00011 and the game is over. The total time is 2 + 3 + 3 = 8. Test case 2. The initial string is 1100001. There is only one possible scenario: - 1100001 is changed to 1000011 in 5 seconds (1 second to choose a soldier and 4 seconds for the soldier to move 4 cells to the right). - 1000011 is changed to 0000111 in 5 seconds. The total time is 5 + 5 = 10 seconds. Test case 3. The game is over immediately because all soldiers occupy rightmost cells already. The answer is 0.
def calc(st): cnt = 0 ones = 0 enc = False zeros = 0 for i in range(len(st)): if st[i] == "1": if enc: cnt += (zeros + 1) * ones enc = False ones += 1 zeros = 0 elif st[i] == "0": if ones: if enc: zeros += 1 else: zeros = 1 enc = True if zeros: cnt += (zeros + 1) * ones return cnt t = int(input()) for _ in range(t): a = input() print(calc(a))
FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR STRING IF VAR VAR BIN_OP BIN_OP VAR NUMBER VAR ASSIGN VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER IF VAR VAR STRING IF VAR IF VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF VAR VAR BIN_OP BIN_OP VAR NUMBER VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR
Limak is a little polar bear. He is playing a video game and he needs your help. There is a row with N cells, each either empty or occupied by a soldier, denoted by '0' and '1' respectively. The goal of the game is to move all soldiers to the right (they should occupy some number of rightmost cells). The only possible command is choosing a soldier and telling him to move to the right as far as possible. Choosing a soldier takes 1 second, and a soldier moves with the speed of a cell per second. The soldier stops immediately if he is in the last cell of the row or the next cell is already occupied. Limak isn't allowed to choose a soldier that can't move at all (the chosen soldier must move at least one cell to the right). Limak enjoys this game very much and wants to play as long as possible. In particular, he doesn't start a new command while the previously chosen soldier moves. Can you tell him, how many seconds he can play at most? -----Input----- The first line of the input contains an integer T denoting the number of test cases. The description of T test cases follows. The only line of each test case contains a string S describing the row with N cells. Each character is either '0' or '1', denoting an empty cell or a cell with a soldier respectively. -----Output----- For each test case, output a single line containing one integer β€” the maximum possible number of seconds Limak will play the game. -----Constraints----- - 1 ≀ T ≀ 5 - 1 ≀ N ≀ 105 (N denotes the length of the string S) -----Subtasks----- - Subtask #1 (25 points): 1 ≀ N ≀ 10 - Subtask #2 (25 points): 1 ≀ N ≀ 2000 - Subtask #3 (50 points): Original constraints. -----Example----- Input: 4 10100 1100001 000000000111 001110100011010 Output: 8 10 0 48 -----Explanation----- Test case 1. The initial string is 10100. There are N = 5 cells. There is one soldier in the first cell, and one soldier in the third cell. The following scenario maximizes the total time: - Limak chooses the soldier in the first cell. This soldier can move only one cell to the right. It takes 1 second to choose a soldier and 1 second for a soldier to move to the next cell (2 seconds in total). The string is 01100 now. - Limak has only one choice. He must choose the soldier in the third cell because the other soldier can't move at all (the soldier in the second cell can't move to the right because the next cell is already occupied). Choosing a soldier takes 1 second. The chosen soldier moves from the third cell to the fifth cell, which takes 2 seconds. This operation takes 1 + 2 = 3 seconds in total. The string is 01001 now. - Limak has only one choice again. Since the soldier in the last row can't move further to the right, the soldier in the second cell must be chosen. He will move 2 cells to the right. This operation takes 1 + 2 = 3 seconds in total. The string become 00011 and the game is over. The total time is 2 + 3 + 3 = 8. Test case 2. The initial string is 1100001. There is only one possible scenario: - 1100001 is changed to 1000011 in 5 seconds (1 second to choose a soldier and 4 seconds for the soldier to move 4 cells to the right). - 1000011 is changed to 0000111 in 5 seconds. The total time is 5 + 5 = 10 seconds. Test case 3. The game is over immediately because all soldiers occupy rightmost cells already. The answer is 0.
t = int(input()) for _ in range(t): s = input() pre = [] n = len(s) j = 0 ss = {} for i in range(n): if s[i] == "1": pre.append(i) ss[i] = j j += 1 pre.append(n) suff = [(0) for i in range(n + 1)] tot = 0 duff = [(0) for i in range(n + 1)] for i in range(n - 1, -1, -1): if s[i] == "0": pass else: suff[i] = suff[pre[ss[i] + 1]] + max(tot, 0) duff[i] = max(tot, 0) tot -= 1 tot += 1 tot = 0 st = False dp = [0] * (n + 1) for i in range(n - 1, -1, -1): if s[i] == "0": st = True continue else: if st: tot += 1 st = False dp[i] = tot + dp[pre[ss[i] + 1]] print(suff[pre[0]] + dp[pre[0]])
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR DICT FOR VAR FUNC_CALL VAR VAR IF VAR VAR STRING EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR VAR STRING ASSIGN VAR VAR BIN_OP VAR VAR BIN_OP VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR VAR STRING ASSIGN VAR NUMBER IF VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR NUMBER VAR VAR NUMBER
Limak is a little polar bear. He is playing a video game and he needs your help. There is a row with N cells, each either empty or occupied by a soldier, denoted by '0' and '1' respectively. The goal of the game is to move all soldiers to the right (they should occupy some number of rightmost cells). The only possible command is choosing a soldier and telling him to move to the right as far as possible. Choosing a soldier takes 1 second, and a soldier moves with the speed of a cell per second. The soldier stops immediately if he is in the last cell of the row or the next cell is already occupied. Limak isn't allowed to choose a soldier that can't move at all (the chosen soldier must move at least one cell to the right). Limak enjoys this game very much and wants to play as long as possible. In particular, he doesn't start a new command while the previously chosen soldier moves. Can you tell him, how many seconds he can play at most? -----Input----- The first line of the input contains an integer T denoting the number of test cases. The description of T test cases follows. The only line of each test case contains a string S describing the row with N cells. Each character is either '0' or '1', denoting an empty cell or a cell with a soldier respectively. -----Output----- For each test case, output a single line containing one integer β€” the maximum possible number of seconds Limak will play the game. -----Constraints----- - 1 ≀ T ≀ 5 - 1 ≀ N ≀ 105 (N denotes the length of the string S) -----Subtasks----- - Subtask #1 (25 points): 1 ≀ N ≀ 10 - Subtask #2 (25 points): 1 ≀ N ≀ 2000 - Subtask #3 (50 points): Original constraints. -----Example----- Input: 4 10100 1100001 000000000111 001110100011010 Output: 8 10 0 48 -----Explanation----- Test case 1. The initial string is 10100. There are N = 5 cells. There is one soldier in the first cell, and one soldier in the third cell. The following scenario maximizes the total time: - Limak chooses the soldier in the first cell. This soldier can move only one cell to the right. It takes 1 second to choose a soldier and 1 second for a soldier to move to the next cell (2 seconds in total). The string is 01100 now. - Limak has only one choice. He must choose the soldier in the third cell because the other soldier can't move at all (the soldier in the second cell can't move to the right because the next cell is already occupied). Choosing a soldier takes 1 second. The chosen soldier moves from the third cell to the fifth cell, which takes 2 seconds. This operation takes 1 + 2 = 3 seconds in total. The string is 01001 now. - Limak has only one choice again. Since the soldier in the last row can't move further to the right, the soldier in the second cell must be chosen. He will move 2 cells to the right. This operation takes 1 + 2 = 3 seconds in total. The string become 00011 and the game is over. The total time is 2 + 3 + 3 = 8. Test case 2. The initial string is 1100001. There is only one possible scenario: - 1100001 is changed to 1000011 in 5 seconds (1 second to choose a soldier and 4 seconds for the soldier to move 4 cells to the right). - 1000011 is changed to 0000111 in 5 seconds. The total time is 5 + 5 = 10 seconds. Test case 3. The game is over immediately because all soldiers occupy rightmost cells already. The answer is 0.
t = int(input()) while t: s = input() n = len(s) if s[n - 1] == "1": n -= 1 ans = 0 count = 0 i = n - 1 while i >= 0: if s[i] == "0": count += 1 else: count += 1 if s[i + 1] == "1": count -= 1 ans += count i -= 1 print(ans) t -= 1
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR BIN_OP VAR NUMBER STRING VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR NUMBER IF VAR VAR STRING VAR NUMBER VAR NUMBER IF VAR BIN_OP VAR NUMBER STRING VAR NUMBER VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER
Limak is a little polar bear. He is playing a video game and he needs your help. There is a row with N cells, each either empty or occupied by a soldier, denoted by '0' and '1' respectively. The goal of the game is to move all soldiers to the right (they should occupy some number of rightmost cells). The only possible command is choosing a soldier and telling him to move to the right as far as possible. Choosing a soldier takes 1 second, and a soldier moves with the speed of a cell per second. The soldier stops immediately if he is in the last cell of the row or the next cell is already occupied. Limak isn't allowed to choose a soldier that can't move at all (the chosen soldier must move at least one cell to the right). Limak enjoys this game very much and wants to play as long as possible. In particular, he doesn't start a new command while the previously chosen soldier moves. Can you tell him, how many seconds he can play at most? -----Input----- The first line of the input contains an integer T denoting the number of test cases. The description of T test cases follows. The only line of each test case contains a string S describing the row with N cells. Each character is either '0' or '1', denoting an empty cell or a cell with a soldier respectively. -----Output----- For each test case, output a single line containing one integer β€” the maximum possible number of seconds Limak will play the game. -----Constraints----- - 1 ≀ T ≀ 5 - 1 ≀ N ≀ 105 (N denotes the length of the string S) -----Subtasks----- - Subtask #1 (25 points): 1 ≀ N ≀ 10 - Subtask #2 (25 points): 1 ≀ N ≀ 2000 - Subtask #3 (50 points): Original constraints. -----Example----- Input: 4 10100 1100001 000000000111 001110100011010 Output: 8 10 0 48 -----Explanation----- Test case 1. The initial string is 10100. There are N = 5 cells. There is one soldier in the first cell, and one soldier in the third cell. The following scenario maximizes the total time: - Limak chooses the soldier in the first cell. This soldier can move only one cell to the right. It takes 1 second to choose a soldier and 1 second for a soldier to move to the next cell (2 seconds in total). The string is 01100 now. - Limak has only one choice. He must choose the soldier in the third cell because the other soldier can't move at all (the soldier in the second cell can't move to the right because the next cell is already occupied). Choosing a soldier takes 1 second. The chosen soldier moves from the third cell to the fifth cell, which takes 2 seconds. This operation takes 1 + 2 = 3 seconds in total. The string is 01001 now. - Limak has only one choice again. Since the soldier in the last row can't move further to the right, the soldier in the second cell must be chosen. He will move 2 cells to the right. This operation takes 1 + 2 = 3 seconds in total. The string become 00011 and the game is over. The total time is 2 + 3 + 3 = 8. Test case 2. The initial string is 1100001. There is only one possible scenario: - 1100001 is changed to 1000011 in 5 seconds (1 second to choose a soldier and 4 seconds for the soldier to move 4 cells to the right). - 1000011 is changed to 0000111 in 5 seconds. The total time is 5 + 5 = 10 seconds. Test case 3. The game is over immediately because all soldiers occupy rightmost cells already. The answer is 0.
t = int(input()) for i in range(t): n = input() l = list(n) one = 0 sec = 0 zero = 0 k = 0 f = 0 while l[k] == "0": k = k + 1 if k == len(l): break for j in range(k, len(l)): if l[j] == "1": if f == 1: sec = sec + one + one * zero f = 0 zero = 0 one = one + 1 if l[j] == "0": zero = zero + 1 f = 1 if not zero == 0: sec = sec + one + one * zero print(sec)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR STRING ASSIGN VAR BIN_OP VAR NUMBER IF VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR IF VAR VAR STRING IF VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF VAR VAR STRING ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR
Limak is a little polar bear. He is playing a video game and he needs your help. There is a row with N cells, each either empty or occupied by a soldier, denoted by '0' and '1' respectively. The goal of the game is to move all soldiers to the right (they should occupy some number of rightmost cells). The only possible command is choosing a soldier and telling him to move to the right as far as possible. Choosing a soldier takes 1 second, and a soldier moves with the speed of a cell per second. The soldier stops immediately if he is in the last cell of the row or the next cell is already occupied. Limak isn't allowed to choose a soldier that can't move at all (the chosen soldier must move at least one cell to the right). Limak enjoys this game very much and wants to play as long as possible. In particular, he doesn't start a new command while the previously chosen soldier moves. Can you tell him, how many seconds he can play at most? -----Input----- The first line of the input contains an integer T denoting the number of test cases. The description of T test cases follows. The only line of each test case contains a string S describing the row with N cells. Each character is either '0' or '1', denoting an empty cell or a cell with a soldier respectively. -----Output----- For each test case, output a single line containing one integer β€” the maximum possible number of seconds Limak will play the game. -----Constraints----- - 1 ≀ T ≀ 5 - 1 ≀ N ≀ 105 (N denotes the length of the string S) -----Subtasks----- - Subtask #1 (25 points): 1 ≀ N ≀ 10 - Subtask #2 (25 points): 1 ≀ N ≀ 2000 - Subtask #3 (50 points): Original constraints. -----Example----- Input: 4 10100 1100001 000000000111 001110100011010 Output: 8 10 0 48 -----Explanation----- Test case 1. The initial string is 10100. There are N = 5 cells. There is one soldier in the first cell, and one soldier in the third cell. The following scenario maximizes the total time: - Limak chooses the soldier in the first cell. This soldier can move only one cell to the right. It takes 1 second to choose a soldier and 1 second for a soldier to move to the next cell (2 seconds in total). The string is 01100 now. - Limak has only one choice. He must choose the soldier in the third cell because the other soldier can't move at all (the soldier in the second cell can't move to the right because the next cell is already occupied). Choosing a soldier takes 1 second. The chosen soldier moves from the third cell to the fifth cell, which takes 2 seconds. This operation takes 1 + 2 = 3 seconds in total. The string is 01001 now. - Limak has only one choice again. Since the soldier in the last row can't move further to the right, the soldier in the second cell must be chosen. He will move 2 cells to the right. This operation takes 1 + 2 = 3 seconds in total. The string become 00011 and the game is over. The total time is 2 + 3 + 3 = 8. Test case 2. The initial string is 1100001. There is only one possible scenario: - 1100001 is changed to 1000011 in 5 seconds (1 second to choose a soldier and 4 seconds for the soldier to move 4 cells to the right). - 1000011 is changed to 0000111 in 5 seconds. The total time is 5 + 5 = 10 seconds. Test case 3. The game is over immediately because all soldiers occupy rightmost cells already. The answer is 0.
test = int(input()) while test: count = 1 ans = 0 n = input() leng = len(n) while n[leng - 1] == "1": leng -= 1 if leng == 0: break for i in range(leng): if n[leng - i - 1] == "0": count += 1 else: if n[leng - i] == "1": count -= 1 ans += count count += 1 print(ans) test -= 1
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR WHILE VAR BIN_OP VAR NUMBER STRING VAR NUMBER IF VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR BIN_OP BIN_OP VAR VAR NUMBER STRING VAR NUMBER IF VAR BIN_OP VAR VAR STRING VAR NUMBER VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER
Limak is a little polar bear. He is playing a video game and he needs your help. There is a row with N cells, each either empty or occupied by a soldier, denoted by '0' and '1' respectively. The goal of the game is to move all soldiers to the right (they should occupy some number of rightmost cells). The only possible command is choosing a soldier and telling him to move to the right as far as possible. Choosing a soldier takes 1 second, and a soldier moves with the speed of a cell per second. The soldier stops immediately if he is in the last cell of the row or the next cell is already occupied. Limak isn't allowed to choose a soldier that can't move at all (the chosen soldier must move at least one cell to the right). Limak enjoys this game very much and wants to play as long as possible. In particular, he doesn't start a new command while the previously chosen soldier moves. Can you tell him, how many seconds he can play at most? -----Input----- The first line of the input contains an integer T denoting the number of test cases. The description of T test cases follows. The only line of each test case contains a string S describing the row with N cells. Each character is either '0' or '1', denoting an empty cell or a cell with a soldier respectively. -----Output----- For each test case, output a single line containing one integer β€” the maximum possible number of seconds Limak will play the game. -----Constraints----- - 1 ≀ T ≀ 5 - 1 ≀ N ≀ 105 (N denotes the length of the string S) -----Subtasks----- - Subtask #1 (25 points): 1 ≀ N ≀ 10 - Subtask #2 (25 points): 1 ≀ N ≀ 2000 - Subtask #3 (50 points): Original constraints. -----Example----- Input: 4 10100 1100001 000000000111 001110100011010 Output: 8 10 0 48 -----Explanation----- Test case 1. The initial string is 10100. There are N = 5 cells. There is one soldier in the first cell, and one soldier in the third cell. The following scenario maximizes the total time: - Limak chooses the soldier in the first cell. This soldier can move only one cell to the right. It takes 1 second to choose a soldier and 1 second for a soldier to move to the next cell (2 seconds in total). The string is 01100 now. - Limak has only one choice. He must choose the soldier in the third cell because the other soldier can't move at all (the soldier in the second cell can't move to the right because the next cell is already occupied). Choosing a soldier takes 1 second. The chosen soldier moves from the third cell to the fifth cell, which takes 2 seconds. This operation takes 1 + 2 = 3 seconds in total. The string is 01001 now. - Limak has only one choice again. Since the soldier in the last row can't move further to the right, the soldier in the second cell must be chosen. He will move 2 cells to the right. This operation takes 1 + 2 = 3 seconds in total. The string become 00011 and the game is over. The total time is 2 + 3 + 3 = 8. Test case 2. The initial string is 1100001. There is only one possible scenario: - 1100001 is changed to 1000011 in 5 seconds (1 second to choose a soldier and 4 seconds for the soldier to move 4 cells to the right). - 1000011 is changed to 0000111 in 5 seconds. The total time is 5 + 5 = 10 seconds. Test case 3. The game is over immediately because all soldiers occupy rightmost cells already. The answer is 0.
def find_1seq(S, start): i = start while i < len(S) and S[i] == "0": i += 1 j = i + 1 while j < len(S) and S[j] == "1": j += 1 return j - i, j for _ in range(int(input())): S = input() time = 0 i = 0 prev_nbr_ones = 0 while True: nbr_ones, i = find_1seq(S, i) nbr_ones += prev_nbr_ones prev_nbr_ones = nbr_ones if i < len(S): time += nbr_ones while i < len(S) and S[i] == "0": time += nbr_ones i += 1 else: break print(time)
FUNC_DEF ASSIGN VAR VAR WHILE VAR FUNC_CALL VAR VAR VAR VAR STRING VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR FUNC_CALL VAR VAR VAR VAR STRING VAR NUMBER RETURN BIN_OP VAR VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR VAR IF VAR FUNC_CALL VAR VAR VAR VAR WHILE VAR FUNC_CALL VAR VAR VAR VAR STRING VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
Limak is a little polar bear. He is playing a video game and he needs your help. There is a row with N cells, each either empty or occupied by a soldier, denoted by '0' and '1' respectively. The goal of the game is to move all soldiers to the right (they should occupy some number of rightmost cells). The only possible command is choosing a soldier and telling him to move to the right as far as possible. Choosing a soldier takes 1 second, and a soldier moves with the speed of a cell per second. The soldier stops immediately if he is in the last cell of the row or the next cell is already occupied. Limak isn't allowed to choose a soldier that can't move at all (the chosen soldier must move at least one cell to the right). Limak enjoys this game very much and wants to play as long as possible. In particular, he doesn't start a new command while the previously chosen soldier moves. Can you tell him, how many seconds he can play at most? -----Input----- The first line of the input contains an integer T denoting the number of test cases. The description of T test cases follows. The only line of each test case contains a string S describing the row with N cells. Each character is either '0' or '1', denoting an empty cell or a cell with a soldier respectively. -----Output----- For each test case, output a single line containing one integer β€” the maximum possible number of seconds Limak will play the game. -----Constraints----- - 1 ≀ T ≀ 5 - 1 ≀ N ≀ 105 (N denotes the length of the string S) -----Subtasks----- - Subtask #1 (25 points): 1 ≀ N ≀ 10 - Subtask #2 (25 points): 1 ≀ N ≀ 2000 - Subtask #3 (50 points): Original constraints. -----Example----- Input: 4 10100 1100001 000000000111 001110100011010 Output: 8 10 0 48 -----Explanation----- Test case 1. The initial string is 10100. There are N = 5 cells. There is one soldier in the first cell, and one soldier in the third cell. The following scenario maximizes the total time: - Limak chooses the soldier in the first cell. This soldier can move only one cell to the right. It takes 1 second to choose a soldier and 1 second for a soldier to move to the next cell (2 seconds in total). The string is 01100 now. - Limak has only one choice. He must choose the soldier in the third cell because the other soldier can't move at all (the soldier in the second cell can't move to the right because the next cell is already occupied). Choosing a soldier takes 1 second. The chosen soldier moves from the third cell to the fifth cell, which takes 2 seconds. This operation takes 1 + 2 = 3 seconds in total. The string is 01001 now. - Limak has only one choice again. Since the soldier in the last row can't move further to the right, the soldier in the second cell must be chosen. He will move 2 cells to the right. This operation takes 1 + 2 = 3 seconds in total. The string become 00011 and the game is over. The total time is 2 + 3 + 3 = 8. Test case 2. The initial string is 1100001. There is only one possible scenario: - 1100001 is changed to 1000011 in 5 seconds (1 second to choose a soldier and 4 seconds for the soldier to move 4 cells to the right). - 1000011 is changed to 0000111 in 5 seconds. The total time is 5 + 5 = 10 seconds. Test case 3. The game is over immediately because all soldiers occupy rightmost cells already. The answer is 0.
t = int(input()) for _ in range(t): st = input() data = [] temp = 1 for i in range(1, len(st)): if st[i] == st[i - 1]: temp += 1 else: data.append((st[i - 1], temp)) temp = 1 data.append((st[len(st) - 1], temp)) ans = 0 ones = 0 for d in data: if d[0] == "1": ones += d[1] else: ans += ones * (d[1] + 1) print(ans)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF VAR NUMBER STRING VAR VAR NUMBER VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR
Limak is a little polar bear. He is playing a video game and he needs your help. There is a row with N cells, each either empty or occupied by a soldier, denoted by '0' and '1' respectively. The goal of the game is to move all soldiers to the right (they should occupy some number of rightmost cells). The only possible command is choosing a soldier and telling him to move to the right as far as possible. Choosing a soldier takes 1 second, and a soldier moves with the speed of a cell per second. The soldier stops immediately if he is in the last cell of the row or the next cell is already occupied. Limak isn't allowed to choose a soldier that can't move at all (the chosen soldier must move at least one cell to the right). Limak enjoys this game very much and wants to play as long as possible. In particular, he doesn't start a new command while the previously chosen soldier moves. Can you tell him, how many seconds he can play at most? -----Input----- The first line of the input contains an integer T denoting the number of test cases. The description of T test cases follows. The only line of each test case contains a string S describing the row with N cells. Each character is either '0' or '1', denoting an empty cell or a cell with a soldier respectively. -----Output----- For each test case, output a single line containing one integer β€” the maximum possible number of seconds Limak will play the game. -----Constraints----- - 1 ≀ T ≀ 5 - 1 ≀ N ≀ 105 (N denotes the length of the string S) -----Subtasks----- - Subtask #1 (25 points): 1 ≀ N ≀ 10 - Subtask #2 (25 points): 1 ≀ N ≀ 2000 - Subtask #3 (50 points): Original constraints. -----Example----- Input: 4 10100 1100001 000000000111 001110100011010 Output: 8 10 0 48 -----Explanation----- Test case 1. The initial string is 10100. There are N = 5 cells. There is one soldier in the first cell, and one soldier in the third cell. The following scenario maximizes the total time: - Limak chooses the soldier in the first cell. This soldier can move only one cell to the right. It takes 1 second to choose a soldier and 1 second for a soldier to move to the next cell (2 seconds in total). The string is 01100 now. - Limak has only one choice. He must choose the soldier in the third cell because the other soldier can't move at all (the soldier in the second cell can't move to the right because the next cell is already occupied). Choosing a soldier takes 1 second. The chosen soldier moves from the third cell to the fifth cell, which takes 2 seconds. This operation takes 1 + 2 = 3 seconds in total. The string is 01001 now. - Limak has only one choice again. Since the soldier in the last row can't move further to the right, the soldier in the second cell must be chosen. He will move 2 cells to the right. This operation takes 1 + 2 = 3 seconds in total. The string become 00011 and the game is over. The total time is 2 + 3 + 3 = 8. Test case 2. The initial string is 1100001. There is only one possible scenario: - 1100001 is changed to 1000011 in 5 seconds (1 second to choose a soldier and 4 seconds for the soldier to move 4 cells to the right). - 1000011 is changed to 0000111 in 5 seconds. The total time is 5 + 5 = 10 seconds. Test case 3. The game is over immediately because all soldiers occupy rightmost cells already. The answer is 0.
t = int(input()) while t > 0: a = input() zerocount = 0 onecount = 0 i = 0 sumi = 0 while i < len(a): if int(a[i]) == 1: onecount += 1 if int(a[i]) == 0: j = i while j < len(a) and int(a[j]) != 1: zerocount += 1 j += 1 i = j - 1 if zerocount: sumi = sumi + (zerocount + 1) * onecount zerocount = 0 i += 1 print(sumi) t -= 1
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR VAR NUMBER VAR NUMBER IF FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR VAR WHILE VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF VAR ASSIGN VAR BIN_OP VAR BIN_OP BIN_OP VAR NUMBER VAR ASSIGN VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER
Limak is a little polar bear. He is playing a video game and he needs your help. There is a row with N cells, each either empty or occupied by a soldier, denoted by '0' and '1' respectively. The goal of the game is to move all soldiers to the right (they should occupy some number of rightmost cells). The only possible command is choosing a soldier and telling him to move to the right as far as possible. Choosing a soldier takes 1 second, and a soldier moves with the speed of a cell per second. The soldier stops immediately if he is in the last cell of the row or the next cell is already occupied. Limak isn't allowed to choose a soldier that can't move at all (the chosen soldier must move at least one cell to the right). Limak enjoys this game very much and wants to play as long as possible. In particular, he doesn't start a new command while the previously chosen soldier moves. Can you tell him, how many seconds he can play at most? -----Input----- The first line of the input contains an integer T denoting the number of test cases. The description of T test cases follows. The only line of each test case contains a string S describing the row with N cells. Each character is either '0' or '1', denoting an empty cell or a cell with a soldier respectively. -----Output----- For each test case, output a single line containing one integer β€” the maximum possible number of seconds Limak will play the game. -----Constraints----- - 1 ≀ T ≀ 5 - 1 ≀ N ≀ 105 (N denotes the length of the string S) -----Subtasks----- - Subtask #1 (25 points): 1 ≀ N ≀ 10 - Subtask #2 (25 points): 1 ≀ N ≀ 2000 - Subtask #3 (50 points): Original constraints. -----Example----- Input: 4 10100 1100001 000000000111 001110100011010 Output: 8 10 0 48 -----Explanation----- Test case 1. The initial string is 10100. There are N = 5 cells. There is one soldier in the first cell, and one soldier in the third cell. The following scenario maximizes the total time: - Limak chooses the soldier in the first cell. This soldier can move only one cell to the right. It takes 1 second to choose a soldier and 1 second for a soldier to move to the next cell (2 seconds in total). The string is 01100 now. - Limak has only one choice. He must choose the soldier in the third cell because the other soldier can't move at all (the soldier in the second cell can't move to the right because the next cell is already occupied). Choosing a soldier takes 1 second. The chosen soldier moves from the third cell to the fifth cell, which takes 2 seconds. This operation takes 1 + 2 = 3 seconds in total. The string is 01001 now. - Limak has only one choice again. Since the soldier in the last row can't move further to the right, the soldier in the second cell must be chosen. He will move 2 cells to the right. This operation takes 1 + 2 = 3 seconds in total. The string become 00011 and the game is over. The total time is 2 + 3 + 3 = 8. Test case 2. The initial string is 1100001. There is only one possible scenario: - 1100001 is changed to 1000011 in 5 seconds (1 second to choose a soldier and 4 seconds for the soldier to move 4 cells to the right). - 1000011 is changed to 0000111 in 5 seconds. The total time is 5 + 5 = 10 seconds. Test case 3. The game is over immediately because all soldiers occupy rightmost cells already. The answer is 0.
t = int(input().strip()) for a0 in range(t): n = input().strip() pos_ones = [(len(n) - (i + 1)) for i in range(len(n)) if n[i] == "1"] len_one = len(pos_ones) pos_ones.reverse() i = 0 total_cnt = 0 while i < len_one: if pos_ones[i] == i: i += 1 else: if i == 0: total_cnt += (pos_ones[i] + 1) * (len_one - i) elif pos_ones[i] - pos_ones[i - 1] > 1: total_cnt += (pos_ones[i] - pos_ones[i - 1]) * (len_one - i) i += 1 print(total_cnt)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR STRING ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR IF VAR VAR VAR VAR NUMBER IF VAR NUMBER VAR BIN_OP BIN_OP VAR VAR NUMBER BIN_OP VAR VAR IF BIN_OP VAR VAR VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR NUMBER BIN_OP VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
Limak is a little polar bear. He is playing a video game and he needs your help. There is a row with N cells, each either empty or occupied by a soldier, denoted by '0' and '1' respectively. The goal of the game is to move all soldiers to the right (they should occupy some number of rightmost cells). The only possible command is choosing a soldier and telling him to move to the right as far as possible. Choosing a soldier takes 1 second, and a soldier moves with the speed of a cell per second. The soldier stops immediately if he is in the last cell of the row or the next cell is already occupied. Limak isn't allowed to choose a soldier that can't move at all (the chosen soldier must move at least one cell to the right). Limak enjoys this game very much and wants to play as long as possible. In particular, he doesn't start a new command while the previously chosen soldier moves. Can you tell him, how many seconds he can play at most? -----Input----- The first line of the input contains an integer T denoting the number of test cases. The description of T test cases follows. The only line of each test case contains a string S describing the row with N cells. Each character is either '0' or '1', denoting an empty cell or a cell with a soldier respectively. -----Output----- For each test case, output a single line containing one integer β€” the maximum possible number of seconds Limak will play the game. -----Constraints----- - 1 ≀ T ≀ 5 - 1 ≀ N ≀ 105 (N denotes the length of the string S) -----Subtasks----- - Subtask #1 (25 points): 1 ≀ N ≀ 10 - Subtask #2 (25 points): 1 ≀ N ≀ 2000 - Subtask #3 (50 points): Original constraints. -----Example----- Input: 4 10100 1100001 000000000111 001110100011010 Output: 8 10 0 48 -----Explanation----- Test case 1. The initial string is 10100. There are N = 5 cells. There is one soldier in the first cell, and one soldier in the third cell. The following scenario maximizes the total time: - Limak chooses the soldier in the first cell. This soldier can move only one cell to the right. It takes 1 second to choose a soldier and 1 second for a soldier to move to the next cell (2 seconds in total). The string is 01100 now. - Limak has only one choice. He must choose the soldier in the third cell because the other soldier can't move at all (the soldier in the second cell can't move to the right because the next cell is already occupied). Choosing a soldier takes 1 second. The chosen soldier moves from the third cell to the fifth cell, which takes 2 seconds. This operation takes 1 + 2 = 3 seconds in total. The string is 01001 now. - Limak has only one choice again. Since the soldier in the last row can't move further to the right, the soldier in the second cell must be chosen. He will move 2 cells to the right. This operation takes 1 + 2 = 3 seconds in total. The string become 00011 and the game is over. The total time is 2 + 3 + 3 = 8. Test case 2. The initial string is 1100001. There is only one possible scenario: - 1100001 is changed to 1000011 in 5 seconds (1 second to choose a soldier and 4 seconds for the soldier to move 4 cells to the right). - 1000011 is changed to 0000111 in 5 seconds. The total time is 5 + 5 = 10 seconds. Test case 3. The game is over immediately because all soldiers occupy rightmost cells already. The answer is 0.
import sys for _ in range(int(input())): s = str(input()) arr = [int(i) for i in s] n = len(arr) total = 0 cnt = 0 lstindx, curr = n - 1, n - 1 while arr[curr] == 1 and curr >= 0: curr -= 1 lstindx -= 1 while curr >= 0: if arr[curr] == 1: if arr[curr + 1] == 0: cnt += 1 total += cnt + (lstindx - curr) lstindx -= 1 curr -= 1 print(total)
IMPORT FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER WHILE VAR VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER WHILE VAR NUMBER IF VAR VAR NUMBER IF VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER VAR BIN_OP VAR BIN_OP VAR VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR
Limak is a little polar bear. He is playing a video game and he needs your help. There is a row with N cells, each either empty or occupied by a soldier, denoted by '0' and '1' respectively. The goal of the game is to move all soldiers to the right (they should occupy some number of rightmost cells). The only possible command is choosing a soldier and telling him to move to the right as far as possible. Choosing a soldier takes 1 second, and a soldier moves with the speed of a cell per second. The soldier stops immediately if he is in the last cell of the row or the next cell is already occupied. Limak isn't allowed to choose a soldier that can't move at all (the chosen soldier must move at least one cell to the right). Limak enjoys this game very much and wants to play as long as possible. In particular, he doesn't start a new command while the previously chosen soldier moves. Can you tell him, how many seconds he can play at most? -----Input----- The first line of the input contains an integer T denoting the number of test cases. The description of T test cases follows. The only line of each test case contains a string S describing the row with N cells. Each character is either '0' or '1', denoting an empty cell or a cell with a soldier respectively. -----Output----- For each test case, output a single line containing one integer β€” the maximum possible number of seconds Limak will play the game. -----Constraints----- - 1 ≀ T ≀ 5 - 1 ≀ N ≀ 105 (N denotes the length of the string S) -----Subtasks----- - Subtask #1 (25 points): 1 ≀ N ≀ 10 - Subtask #2 (25 points): 1 ≀ N ≀ 2000 - Subtask #3 (50 points): Original constraints. -----Example----- Input: 4 10100 1100001 000000000111 001110100011010 Output: 8 10 0 48 -----Explanation----- Test case 1. The initial string is 10100. There are N = 5 cells. There is one soldier in the first cell, and one soldier in the third cell. The following scenario maximizes the total time: - Limak chooses the soldier in the first cell. This soldier can move only one cell to the right. It takes 1 second to choose a soldier and 1 second for a soldier to move to the next cell (2 seconds in total). The string is 01100 now. - Limak has only one choice. He must choose the soldier in the third cell because the other soldier can't move at all (the soldier in the second cell can't move to the right because the next cell is already occupied). Choosing a soldier takes 1 second. The chosen soldier moves from the third cell to the fifth cell, which takes 2 seconds. This operation takes 1 + 2 = 3 seconds in total. The string is 01001 now. - Limak has only one choice again. Since the soldier in the last row can't move further to the right, the soldier in the second cell must be chosen. He will move 2 cells to the right. This operation takes 1 + 2 = 3 seconds in total. The string become 00011 and the game is over. The total time is 2 + 3 + 3 = 8. Test case 2. The initial string is 1100001. There is only one possible scenario: - 1100001 is changed to 1000011 in 5 seconds (1 second to choose a soldier and 4 seconds for the soldier to move 4 cells to the right). - 1000011 is changed to 0000111 in 5 seconds. The total time is 5 + 5 = 10 seconds. Test case 3. The game is over immediately because all soldiers occupy rightmost cells already. The answer is 0.
def soln(s): n = len(s) res = ones = i = 0 while i < n and s[i] != "1": i += 1 while i < n: while i < n and s[i] == "1": ones += 1 i += 1 zeros = 0 while i < n and s[i] == "0": zeros += 1 i += 1 if zeros == 0: continue res += ones * (zeros + 1) return res for _ in range(int(input())): s = input() print(soln(s))
FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR NUMBER WHILE VAR VAR VAR VAR STRING VAR NUMBER WHILE VAR VAR WHILE VAR VAR VAR VAR STRING VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR VAR VAR STRING VAR NUMBER VAR NUMBER IF VAR NUMBER VAR BIN_OP VAR BIN_OP VAR NUMBER RETURN VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR
Limak is a little polar bear. He is playing a video game and he needs your help. There is a row with N cells, each either empty or occupied by a soldier, denoted by '0' and '1' respectively. The goal of the game is to move all soldiers to the right (they should occupy some number of rightmost cells). The only possible command is choosing a soldier and telling him to move to the right as far as possible. Choosing a soldier takes 1 second, and a soldier moves with the speed of a cell per second. The soldier stops immediately if he is in the last cell of the row or the next cell is already occupied. Limak isn't allowed to choose a soldier that can't move at all (the chosen soldier must move at least one cell to the right). Limak enjoys this game very much and wants to play as long as possible. In particular, he doesn't start a new command while the previously chosen soldier moves. Can you tell him, how many seconds he can play at most? -----Input----- The first line of the input contains an integer T denoting the number of test cases. The description of T test cases follows. The only line of each test case contains a string S describing the row with N cells. Each character is either '0' or '1', denoting an empty cell or a cell with a soldier respectively. -----Output----- For each test case, output a single line containing one integer β€” the maximum possible number of seconds Limak will play the game. -----Constraints----- - 1 ≀ T ≀ 5 - 1 ≀ N ≀ 105 (N denotes the length of the string S) -----Subtasks----- - Subtask #1 (25 points): 1 ≀ N ≀ 10 - Subtask #2 (25 points): 1 ≀ N ≀ 2000 - Subtask #3 (50 points): Original constraints. -----Example----- Input: 4 10100 1100001 000000000111 001110100011010 Output: 8 10 0 48 -----Explanation----- Test case 1. The initial string is 10100. There are N = 5 cells. There is one soldier in the first cell, and one soldier in the third cell. The following scenario maximizes the total time: - Limak chooses the soldier in the first cell. This soldier can move only one cell to the right. It takes 1 second to choose a soldier and 1 second for a soldier to move to the next cell (2 seconds in total). The string is 01100 now. - Limak has only one choice. He must choose the soldier in the third cell because the other soldier can't move at all (the soldier in the second cell can't move to the right because the next cell is already occupied). Choosing a soldier takes 1 second. The chosen soldier moves from the third cell to the fifth cell, which takes 2 seconds. This operation takes 1 + 2 = 3 seconds in total. The string is 01001 now. - Limak has only one choice again. Since the soldier in the last row can't move further to the right, the soldier in the second cell must be chosen. He will move 2 cells to the right. This operation takes 1 + 2 = 3 seconds in total. The string become 00011 and the game is over. The total time is 2 + 3 + 3 = 8. Test case 2. The initial string is 1100001. There is only one possible scenario: - 1100001 is changed to 1000011 in 5 seconds (1 second to choose a soldier and 4 seconds for the soldier to move 4 cells to the right). - 1000011 is changed to 0000111 in 5 seconds. The total time is 5 + 5 = 10 seconds. Test case 3. The game is over immediately because all soldiers occupy rightmost cells already. The answer is 0.
casos = int(input()) ejecuciones = 0 respuestas = [] soldados = 0 pasos = 0 tiempoIteracion = 0 total = 0 while ejecuciones < casos: entrada = input() for soldado in entrada: if soldado == "0": pasos += 1 else: if pasos > 0: tiempoIteracion = soldados + soldados * pasos total += tiempoIteracion pasos = 0 soldados += 1 if pasos > 0: total += soldados + soldados * pasos respuestas.append(total) ejecuciones += 1 soldados = 0 pasos = 0 tiempoIteracion = 0 total = 0 for res in respuestas: print(str(res))
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR FUNC_CALL VAR FOR VAR VAR IF VAR STRING VAR NUMBER IF VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR NUMBER VAR NUMBER IF VAR NUMBER VAR BIN_OP VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR
Limak is a little polar bear. He is playing a video game and he needs your help. There is a row with N cells, each either empty or occupied by a soldier, denoted by '0' and '1' respectively. The goal of the game is to move all soldiers to the right (they should occupy some number of rightmost cells). The only possible command is choosing a soldier and telling him to move to the right as far as possible. Choosing a soldier takes 1 second, and a soldier moves with the speed of a cell per second. The soldier stops immediately if he is in the last cell of the row or the next cell is already occupied. Limak isn't allowed to choose a soldier that can't move at all (the chosen soldier must move at least one cell to the right). Limak enjoys this game very much and wants to play as long as possible. In particular, he doesn't start a new command while the previously chosen soldier moves. Can you tell him, how many seconds he can play at most? -----Input----- The first line of the input contains an integer T denoting the number of test cases. The description of T test cases follows. The only line of each test case contains a string S describing the row with N cells. Each character is either '0' or '1', denoting an empty cell or a cell with a soldier respectively. -----Output----- For each test case, output a single line containing one integer β€” the maximum possible number of seconds Limak will play the game. -----Constraints----- - 1 ≀ T ≀ 5 - 1 ≀ N ≀ 105 (N denotes the length of the string S) -----Subtasks----- - Subtask #1 (25 points): 1 ≀ N ≀ 10 - Subtask #2 (25 points): 1 ≀ N ≀ 2000 - Subtask #3 (50 points): Original constraints. -----Example----- Input: 4 10100 1100001 000000000111 001110100011010 Output: 8 10 0 48 -----Explanation----- Test case 1. The initial string is 10100. There are N = 5 cells. There is one soldier in the first cell, and one soldier in the third cell. The following scenario maximizes the total time: - Limak chooses the soldier in the first cell. This soldier can move only one cell to the right. It takes 1 second to choose a soldier and 1 second for a soldier to move to the next cell (2 seconds in total). The string is 01100 now. - Limak has only one choice. He must choose the soldier in the third cell because the other soldier can't move at all (the soldier in the second cell can't move to the right because the next cell is already occupied). Choosing a soldier takes 1 second. The chosen soldier moves from the third cell to the fifth cell, which takes 2 seconds. This operation takes 1 + 2 = 3 seconds in total. The string is 01001 now. - Limak has only one choice again. Since the soldier in the last row can't move further to the right, the soldier in the second cell must be chosen. He will move 2 cells to the right. This operation takes 1 + 2 = 3 seconds in total. The string become 00011 and the game is over. The total time is 2 + 3 + 3 = 8. Test case 2. The initial string is 1100001. There is only one possible scenario: - 1100001 is changed to 1000011 in 5 seconds (1 second to choose a soldier and 4 seconds for the soldier to move 4 cells to the right). - 1000011 is changed to 0000111 in 5 seconds. The total time is 5 + 5 = 10 seconds. Test case 3. The game is over immediately because all soldiers occupy rightmost cells already. The answer is 0.
for t in range(int(input())): s = input()[::-1].strip() last = 0 extra_stops = 0 try: for i in range(s.index("01") + 2, len(s)): if s[i] == "1": if s[i - 1] == "0": last += 1 extra_stops += last except ValueError: pass ans = 0 ones = 0 for i in range(len(s)): if s[i] == "1": ones += 1 this = i - ones + 2 if this > 1: ans += this print(ans + extra_stops)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR STRING NUMBER FUNC_CALL VAR VAR IF VAR VAR STRING IF VAR BIN_OP VAR NUMBER STRING VAR NUMBER VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR STRING VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR NUMBER VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR
Limak is a little polar bear. He is playing a video game and he needs your help. There is a row with N cells, each either empty or occupied by a soldier, denoted by '0' and '1' respectively. The goal of the game is to move all soldiers to the right (they should occupy some number of rightmost cells). The only possible command is choosing a soldier and telling him to move to the right as far as possible. Choosing a soldier takes 1 second, and a soldier moves with the speed of a cell per second. The soldier stops immediately if he is in the last cell of the row or the next cell is already occupied. Limak isn't allowed to choose a soldier that can't move at all (the chosen soldier must move at least one cell to the right). Limak enjoys this game very much and wants to play as long as possible. In particular, he doesn't start a new command while the previously chosen soldier moves. Can you tell him, how many seconds he can play at most? -----Input----- The first line of the input contains an integer T denoting the number of test cases. The description of T test cases follows. The only line of each test case contains a string S describing the row with N cells. Each character is either '0' or '1', denoting an empty cell or a cell with a soldier respectively. -----Output----- For each test case, output a single line containing one integer β€” the maximum possible number of seconds Limak will play the game. -----Constraints----- - 1 ≀ T ≀ 5 - 1 ≀ N ≀ 105 (N denotes the length of the string S) -----Subtasks----- - Subtask #1 (25 points): 1 ≀ N ≀ 10 - Subtask #2 (25 points): 1 ≀ N ≀ 2000 - Subtask #3 (50 points): Original constraints. -----Example----- Input: 4 10100 1100001 000000000111 001110100011010 Output: 8 10 0 48 -----Explanation----- Test case 1. The initial string is 10100. There are N = 5 cells. There is one soldier in the first cell, and one soldier in the third cell. The following scenario maximizes the total time: - Limak chooses the soldier in the first cell. This soldier can move only one cell to the right. It takes 1 second to choose a soldier and 1 second for a soldier to move to the next cell (2 seconds in total). The string is 01100 now. - Limak has only one choice. He must choose the soldier in the third cell because the other soldier can't move at all (the soldier in the second cell can't move to the right because the next cell is already occupied). Choosing a soldier takes 1 second. The chosen soldier moves from the third cell to the fifth cell, which takes 2 seconds. This operation takes 1 + 2 = 3 seconds in total. The string is 01001 now. - Limak has only one choice again. Since the soldier in the last row can't move further to the right, the soldier in the second cell must be chosen. He will move 2 cells to the right. This operation takes 1 + 2 = 3 seconds in total. The string become 00011 and the game is over. The total time is 2 + 3 + 3 = 8. Test case 2. The initial string is 1100001. There is only one possible scenario: - 1100001 is changed to 1000011 in 5 seconds (1 second to choose a soldier and 4 seconds for the soldier to move 4 cells to the right). - 1000011 is changed to 0000111 in 5 seconds. The total time is 5 + 5 = 10 seconds. Test case 3. The game is over immediately because all soldiers occupy rightmost cells already. The answer is 0.
T = int(input()) ans = [] for _ in range(T): S = input() N = len(S) ones = 0 count = 0 for i in range(N - 1, -1, -1): if S[i] == "1": count += N - i - 1 - ones ones += 1 t = S.split("0") group = 1 for i in range(len(t) - 1, -1, -1): if i != len(t) - 1 and "1" in t[i]: count += len(t[i]) * group group += 1 ans.append(count) for i in ans: print(i)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR VAR STRING VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER IF VAR BIN_OP FUNC_CALL VAR VAR NUMBER STRING VAR VAR VAR BIN_OP FUNC_CALL VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR
Limak is a little polar bear. He is playing a video game and he needs your help. There is a row with N cells, each either empty or occupied by a soldier, denoted by '0' and '1' respectively. The goal of the game is to move all soldiers to the right (they should occupy some number of rightmost cells). The only possible command is choosing a soldier and telling him to move to the right as far as possible. Choosing a soldier takes 1 second, and a soldier moves with the speed of a cell per second. The soldier stops immediately if he is in the last cell of the row or the next cell is already occupied. Limak isn't allowed to choose a soldier that can't move at all (the chosen soldier must move at least one cell to the right). Limak enjoys this game very much and wants to play as long as possible. In particular, he doesn't start a new command while the previously chosen soldier moves. Can you tell him, how many seconds he can play at most? -----Input----- The first line of the input contains an integer T denoting the number of test cases. The description of T test cases follows. The only line of each test case contains a string S describing the row with N cells. Each character is either '0' or '1', denoting an empty cell or a cell with a soldier respectively. -----Output----- For each test case, output a single line containing one integer β€” the maximum possible number of seconds Limak will play the game. -----Constraints----- - 1 ≀ T ≀ 5 - 1 ≀ N ≀ 105 (N denotes the length of the string S) -----Subtasks----- - Subtask #1 (25 points): 1 ≀ N ≀ 10 - Subtask #2 (25 points): 1 ≀ N ≀ 2000 - Subtask #3 (50 points): Original constraints. -----Example----- Input: 4 10100 1100001 000000000111 001110100011010 Output: 8 10 0 48 -----Explanation----- Test case 1. The initial string is 10100. There are N = 5 cells. There is one soldier in the first cell, and one soldier in the third cell. The following scenario maximizes the total time: - Limak chooses the soldier in the first cell. This soldier can move only one cell to the right. It takes 1 second to choose a soldier and 1 second for a soldier to move to the next cell (2 seconds in total). The string is 01100 now. - Limak has only one choice. He must choose the soldier in the third cell because the other soldier can't move at all (the soldier in the second cell can't move to the right because the next cell is already occupied). Choosing a soldier takes 1 second. The chosen soldier moves from the third cell to the fifth cell, which takes 2 seconds. This operation takes 1 + 2 = 3 seconds in total. The string is 01001 now. - Limak has only one choice again. Since the soldier in the last row can't move further to the right, the soldier in the second cell must be chosen. He will move 2 cells to the right. This operation takes 1 + 2 = 3 seconds in total. The string become 00011 and the game is over. The total time is 2 + 3 + 3 = 8. Test case 2. The initial string is 1100001. There is only one possible scenario: - 1100001 is changed to 1000011 in 5 seconds (1 second to choose a soldier and 4 seconds for the soldier to move 4 cells to the right). - 1000011 is changed to 0000111 in 5 seconds. The total time is 5 + 5 = 10 seconds. Test case 3. The game is over immediately because all soldiers occupy rightmost cells already. The answer is 0.
t = int(input()) for i in range(0, t): enc = 0 jump = -1 total = flag = 0 sel = 0 string = input() n = len(string) i = n - 1 while (i >= 0) & (string[i] == "1"): i -= 1 enc += 1 while i >= 0: if (string[i] == "1") & (flag == 0): jump += 1 flag = 1 sel = n - 1 - enc - i total += jump + sel + 1 enc += 1 elif string[i] == "0": flag = 0 sel = 1 else: sel = n - 1 - enc - i total += jump + sel + 1 enc += 1 i -= 1 print(total)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER WHILE BIN_OP VAR NUMBER VAR VAR STRING VAR NUMBER VAR NUMBER WHILE VAR NUMBER IF BIN_OP VAR VAR STRING VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR NUMBER VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR NUMBER IF VAR VAR STRING ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR NUMBER VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR
Limak is a little polar bear. He is playing a video game and he needs your help. There is a row with N cells, each either empty or occupied by a soldier, denoted by '0' and '1' respectively. The goal of the game is to move all soldiers to the right (they should occupy some number of rightmost cells). The only possible command is choosing a soldier and telling him to move to the right as far as possible. Choosing a soldier takes 1 second, and a soldier moves with the speed of a cell per second. The soldier stops immediately if he is in the last cell of the row or the next cell is already occupied. Limak isn't allowed to choose a soldier that can't move at all (the chosen soldier must move at least one cell to the right). Limak enjoys this game very much and wants to play as long as possible. In particular, he doesn't start a new command while the previously chosen soldier moves. Can you tell him, how many seconds he can play at most? -----Input----- The first line of the input contains an integer T denoting the number of test cases. The description of T test cases follows. The only line of each test case contains a string S describing the row with N cells. Each character is either '0' or '1', denoting an empty cell or a cell with a soldier respectively. -----Output----- For each test case, output a single line containing one integer β€” the maximum possible number of seconds Limak will play the game. -----Constraints----- - 1 ≀ T ≀ 5 - 1 ≀ N ≀ 105 (N denotes the length of the string S) -----Subtasks----- - Subtask #1 (25 points): 1 ≀ N ≀ 10 - Subtask #2 (25 points): 1 ≀ N ≀ 2000 - Subtask #3 (50 points): Original constraints. -----Example----- Input: 4 10100 1100001 000000000111 001110100011010 Output: 8 10 0 48 -----Explanation----- Test case 1. The initial string is 10100. There are N = 5 cells. There is one soldier in the first cell, and one soldier in the third cell. The following scenario maximizes the total time: - Limak chooses the soldier in the first cell. This soldier can move only one cell to the right. It takes 1 second to choose a soldier and 1 second for a soldier to move to the next cell (2 seconds in total). The string is 01100 now. - Limak has only one choice. He must choose the soldier in the third cell because the other soldier can't move at all (the soldier in the second cell can't move to the right because the next cell is already occupied). Choosing a soldier takes 1 second. The chosen soldier moves from the third cell to the fifth cell, which takes 2 seconds. This operation takes 1 + 2 = 3 seconds in total. The string is 01001 now. - Limak has only one choice again. Since the soldier in the last row can't move further to the right, the soldier in the second cell must be chosen. He will move 2 cells to the right. This operation takes 1 + 2 = 3 seconds in total. The string become 00011 and the game is over. The total time is 2 + 3 + 3 = 8. Test case 2. The initial string is 1100001. There is only one possible scenario: - 1100001 is changed to 1000011 in 5 seconds (1 second to choose a soldier and 4 seconds for the soldier to move 4 cells to the right). - 1000011 is changed to 0000111 in 5 seconds. The total time is 5 + 5 = 10 seconds. Test case 3. The game is over immediately because all soldiers occupy rightmost cells already. The answer is 0.
t = int(input()) for _ in range(t): s = input() n = len(s) i = n - 1 ones = s.count("1") c = 0 ans = 0 while i >= 0: if s[i] == "0": c += 1 i -= 1 else: if c: ans += ones * (c + 1) c = 0 while i >= 0 and s[i] == "1": ones -= 1 i -= 1 print(ans)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR NUMBER IF VAR VAR STRING VAR NUMBER VAR NUMBER IF VAR VAR BIN_OP VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER WHILE VAR NUMBER VAR VAR STRING VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR