description stringlengths 171 4k | code stringlengths 94 3.98k | normalized_code stringlengths 57 4.99k |
|---|---|---|
Given a positive integer N, count all possible distinct binary strings of length N such that there are no consecutive 1’s. Output your answer modulo 10^9 + 7.
Example 1:
Input:
N = 3
Output: 5
Explanation: 5 strings are (000,
001, 010, 100, 101).
Example 2:
Input:
N = 2
Output: 3
Explanation: 3 strings are
(00,01,10).
Your Task:
Complete the function countStrings() which takes single integer n, as input parameters and returns an integer denoting the answer. You don't to print answer or take inputs.
Expected Time Complexity: O(N)
Expected Auxiliary Space: O(N)
Constraints:
1 ≤ N ≤ 10^{5} | class Solution:
def countStrings(self, n):
zeros = 1
ones = 1
for i in range(2, n + 1):
temp = zeros
zeros = zeros + ones
ones = temp
zeros = zeros % 1000000007
ones = ones % 1000000007
return (ones + zeros) % 1000000007 | CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER RETURN BIN_OP BIN_OP VAR VAR NUMBER |
Given a positive integer N, count all possible distinct binary strings of length N such that there are no consecutive 1’s. Output your answer modulo 10^9 + 7.
Example 1:
Input:
N = 3
Output: 5
Explanation: 5 strings are (000,
001, 010, 100, 101).
Example 2:
Input:
N = 2
Output: 3
Explanation: 3 strings are
(00,01,10).
Your Task:
Complete the function countStrings() which takes single integer n, as input parameters and returns an integer denoting the answer. You don't to print answer or take inputs.
Expected Time Complexity: O(N)
Expected Auxiliary Space: O(N)
Constraints:
1 ≤ N ≤ 10^{5} | class Solution:
def countStrings(self, n):
if n == 0:
return 0
elif n == 1:
return 2
elif n == 2:
return 3
else:
l = [0, 2, 3]
while len(l) != n + 1:
l.append(l[-1] + l[-2])
return l[-1] % (10**9 + 7) | CLASS_DEF FUNC_DEF IF VAR NUMBER RETURN NUMBER IF VAR NUMBER RETURN NUMBER IF VAR NUMBER RETURN NUMBER ASSIGN VAR LIST NUMBER NUMBER NUMBER WHILE FUNC_CALL VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER RETURN BIN_OP VAR NUMBER BIN_OP BIN_OP NUMBER NUMBER NUMBER |
You are a professional robber planning to rob houses along a street. Each house has a certain amount of money stashed. All houses at this place are arranged in a circle. That means the first house is the neighbor of the last one. Meanwhile, adjacent houses have security system connected and it will automatically contact the police if two adjacent houses were broken into on the same night.
Given a list of non-negative integers representing the amount of money of each house, determine the maximum amount of money you can rob tonight without alerting the police.
Example 1:
Input: [2,3,2]
Output: 3
Explanation: You cannot rob house 1 (money = 2) and then rob house 3 (money = 2),
because they are adjacent houses.
Example 2:
Input: [1,2,3,1]
Output: 4
Explanation: Rob house 1 (money = 1) and then rob house 3 (money = 3).
Total amount you can rob = 1 + 3 = 4. | class Solution:
def rob(self, nums):
l = len(nums)
if l == 0:
return 0
if l == 1:
return nums[0]
if l == 2:
return max(nums)
dp1 = [None] * l
dp2 = [None] * l
dp1[0] = nums[0]
dp1[1] = max(nums[0], nums[1])
for i in range(2, l - 1):
dp1[i] = max(dp1[i - 2] + nums[i], dp1[i - 1])
print(dp1)
dp2[1] = nums[1]
dp2[2] = max(nums[1], nums[2])
for i in range(3, l):
dp2[i] = max(dp2[i - 2] + nums[i], dp2[i - 1])
print(dp2)
return max(dp1[l - 2], dp2[l - 1]) | CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR IF VAR NUMBER RETURN NUMBER IF VAR NUMBER RETURN VAR NUMBER IF VAR NUMBER RETURN FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NONE VAR ASSIGN VAR BIN_OP LIST NONE VAR ASSIGN VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER FUNC_CALL VAR VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER FUNC_CALL VAR VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR RETURN FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER |
You are a professional robber planning to rob houses along a street. Each house has a certain amount of money stashed. All houses at this place are arranged in a circle. That means the first house is the neighbor of the last one. Meanwhile, adjacent houses have security system connected and it will automatically contact the police if two adjacent houses were broken into on the same night.
Given a list of non-negative integers representing the amount of money of each house, determine the maximum amount of money you can rob tonight without alerting the police.
Example 1:
Input: [2,3,2]
Output: 3
Explanation: You cannot rob house 1 (money = 2) and then rob house 3 (money = 2),
because they are adjacent houses.
Example 2:
Input: [1,2,3,1]
Output: 4
Explanation: Rob house 1 (money = 1) and then rob house 3 (money = 3).
Total amount you can rob = 1 + 3 = 4. | class Solution:
def rob(self, nums):
if not nums:
return 0
ymax = [nums[i] for i in range(len(nums))]
nmax = [nums[i] for i in range(len(nums))]
nmax[0] = 0
if len(nums) > 1:
ymax[1] = max(ymax[0], ymax[1])
for i in range(2, len(nums)):
if i == len(nums) - 1:
ymax[i] = ymax[i - 1]
else:
ymax[i] = max(ymax[i - 1], ymax[i - 2] + ymax[i])
nmax[i] = max(nmax[i - 1], nmax[i - 2] + nmax[i])
return max(nmax[-1], ymax[-1]) | CLASS_DEF FUNC_DEF IF VAR RETURN NUMBER ASSIGN VAR VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER NUMBER IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER FUNC_CALL VAR VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER VAR VAR RETURN FUNC_CALL VAR VAR NUMBER VAR NUMBER |
You are a professional robber planning to rob houses along a street. Each house has a certain amount of money stashed. All houses at this place are arranged in a circle. That means the first house is the neighbor of the last one. Meanwhile, adjacent houses have security system connected and it will automatically contact the police if two adjacent houses were broken into on the same night.
Given a list of non-negative integers representing the amount of money of each house, determine the maximum amount of money you can rob tonight without alerting the police.
Example 1:
Input: [2,3,2]
Output: 3
Explanation: You cannot rob house 1 (money = 2) and then rob house 3 (money = 2),
because they are adjacent houses.
Example 2:
Input: [1,2,3,1]
Output: 4
Explanation: Rob house 1 (money = 1) and then rob house 3 (money = 3).
Total amount you can rob = 1 + 3 = 4. | class Solution:
def rob(self, nums):
if not nums:
return 0
if len(nums) == 1:
return nums[0]
def helper(nums):
steal = 0
cool = 0
for num in nums:
steal, cool = cool, max(cool, steal + num)
return max(steal, cool)
return max(helper(nums[:-1]), helper(nums[1:])) | CLASS_DEF FUNC_DEF IF VAR RETURN NUMBER IF FUNC_CALL VAR VAR NUMBER RETURN VAR NUMBER FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR VAR RETURN FUNC_CALL VAR VAR VAR RETURN FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER |
You are a professional robber planning to rob houses along a street. Each house has a certain amount of money stashed. All houses at this place are arranged in a circle. That means the first house is the neighbor of the last one. Meanwhile, adjacent houses have security system connected and it will automatically contact the police if two adjacent houses were broken into on the same night.
Given a list of non-negative integers representing the amount of money of each house, determine the maximum amount of money you can rob tonight without alerting the police.
Example 1:
Input: [2,3,2]
Output: 3
Explanation: You cannot rob house 1 (money = 2) and then rob house 3 (money = 2),
because they are adjacent houses.
Example 2:
Input: [1,2,3,1]
Output: 4
Explanation: Rob house 1 (money = 1) and then rob house 3 (money = 3).
Total amount you can rob = 1 + 3 = 4. | class Solution:
def rob(self, nums):
def helper(nums):
if not nums:
return 0
dp = [0] * (len(nums) + 1)
dp[1] = nums[0]
for i in range(2, len(nums) + 1):
dp[i] = max(dp[i - 1], nums[i - 1] + dp[i - 2])
return dp[-1]
if not nums:
return 0
if len(nums) == 1:
return nums[0]
return max(
helper(nums[: len(nums) - 1]), nums[-1] + helper(nums[1 : len(nums) - 2])
) | CLASS_DEF FUNC_DEF FUNC_DEF IF VAR RETURN NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER RETURN VAR NUMBER IF VAR RETURN NUMBER IF FUNC_CALL VAR VAR NUMBER RETURN VAR NUMBER RETURN FUNC_CALL VAR FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER BIN_OP VAR NUMBER FUNC_CALL VAR VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER |
You are a professional robber planning to rob houses along a street. Each house has a certain amount of money stashed. All houses at this place are arranged in a circle. That means the first house is the neighbor of the last one. Meanwhile, adjacent houses have security system connected and it will automatically contact the police if two adjacent houses were broken into on the same night.
Given a list of non-negative integers representing the amount of money of each house, determine the maximum amount of money you can rob tonight without alerting the police.
Example 1:
Input: [2,3,2]
Output: 3
Explanation: You cannot rob house 1 (money = 2) and then rob house 3 (money = 2),
because they are adjacent houses.
Example 2:
Input: [1,2,3,1]
Output: 4
Explanation: Rob house 1 (money = 1) and then rob house 3 (money = 3).
Total amount you can rob = 1 + 3 = 4. | class Solution:
def rob_prev(self, nums):
n = len(nums)
if n == 0:
return 0
elif n < 3:
return max(nums)
mem = [(0) for x in range(n + 1)]
mem[0] = nums[0]
mem[1] = max(nums[1], nums[0])
for i in range(2, n):
mem[i] = max(mem[i - 2] + nums[i], mem[i - 1])
return mem[n - 1]
def rob(self, nums):
n = len(nums)
if n == 0:
return 0
elif n < 3:
return max(nums)
return max(self.rob_prev(nums[1:]), self.rob_prev(nums[: len(nums) - 1])) | CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR IF VAR NUMBER RETURN NUMBER IF VAR NUMBER RETURN FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER FUNC_CALL VAR VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER RETURN VAR BIN_OP VAR NUMBER FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR IF VAR NUMBER RETURN NUMBER IF VAR NUMBER RETURN FUNC_CALL VAR VAR RETURN FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER |
You are a professional robber planning to rob houses along a street. Each house has a certain amount of money stashed. All houses at this place are arranged in a circle. That means the first house is the neighbor of the last one. Meanwhile, adjacent houses have security system connected and it will automatically contact the police if two adjacent houses were broken into on the same night.
Given a list of non-negative integers representing the amount of money of each house, determine the maximum amount of money you can rob tonight without alerting the police.
Example 1:
Input: [2,3,2]
Output: 3
Explanation: You cannot rob house 1 (money = 2) and then rob house 3 (money = 2),
because they are adjacent houses.
Example 2:
Input: [1,2,3,1]
Output: 4
Explanation: Rob house 1 (money = 1) and then rob house 3 (money = 3).
Total amount you can rob = 1 + 3 = 4. | class Solution:
def rob(self, nums):
mem = {}
def max_money(start, rob_first=False):
if start >= len(nums):
return 0
if start == len(nums) - 1 and rob_first:
return 0
if (start, rob_first) in mem:
return mem[start, rob_first]
if start == 0:
mem[start, rob_first] = max(
max_money(start + 1, False),
nums[start] + max_money(start + 2, True),
)
else:
mem[start, rob_first] = max(
max_money(start + 1, rob_first),
nums[start] + max_money(start + 2, rob_first),
)
return mem[start, rob_first]
return max_money(0) | CLASS_DEF FUNC_DEF ASSIGN VAR DICT FUNC_DEF NUMBER IF VAR FUNC_CALL VAR VAR RETURN NUMBER IF VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR RETURN NUMBER IF VAR VAR VAR RETURN VAR VAR VAR IF VAR NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER BIN_OP VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR BIN_OP VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR RETURN VAR VAR VAR RETURN FUNC_CALL VAR NUMBER |
You are a professional robber planning to rob houses along a street. Each house has a certain amount of money stashed. All houses at this place are arranged in a circle. That means the first house is the neighbor of the last one. Meanwhile, adjacent houses have security system connected and it will automatically contact the police if two adjacent houses were broken into on the same night.
Given a list of non-negative integers representing the amount of money of each house, determine the maximum amount of money you can rob tonight without alerting the police.
Example 1:
Input: [2,3,2]
Output: 3
Explanation: You cannot rob house 1 (money = 2) and then rob house 3 (money = 2),
because they are adjacent houses.
Example 2:
Input: [1,2,3,1]
Output: 4
Explanation: Rob house 1 (money = 1) and then rob house 3 (money = 3).
Total amount you can rob = 1 + 3 = 4. | class Solution:
def rob(self, nums):
if not nums:
return 0
if len(nums) == 1:
return nums[0]
if len(nums) <= 3:
return max(nums)
def rob_line(lst):
last, now = 0, 0
for i in lst:
last, now = now, max(now, last + i)
return now
return max(rob_line(nums[:-1]), rob_line(nums[1:])) | CLASS_DEF FUNC_DEF IF VAR RETURN NUMBER IF FUNC_CALL VAR VAR NUMBER RETURN VAR NUMBER IF FUNC_CALL VAR VAR NUMBER RETURN FUNC_CALL VAR VAR FUNC_DEF ASSIGN VAR VAR NUMBER NUMBER FOR VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR VAR RETURN VAR RETURN FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER |
You are a professional robber planning to rob houses along a street. Each house has a certain amount of money stashed. All houses at this place are arranged in a circle. That means the first house is the neighbor of the last one. Meanwhile, adjacent houses have security system connected and it will automatically contact the police if two adjacent houses were broken into on the same night.
Given a list of non-negative integers representing the amount of money of each house, determine the maximum amount of money you can rob tonight without alerting the police.
Example 1:
Input: [2,3,2]
Output: 3
Explanation: You cannot rob house 1 (money = 2) and then rob house 3 (money = 2),
because they are adjacent houses.
Example 2:
Input: [1,2,3,1]
Output: 4
Explanation: Rob house 1 (money = 1) and then rob house 3 (money = 3).
Total amount you can rob = 1 + 3 = 4. | class Solution:
def helper(self, nums, cache={}):
if len(nums) == 0:
return 0
key = str(nums)
if key in cache:
return cache[key]
cache[key] = max(nums[0] + self.helper(nums[2:]), self.helper(nums[1:]))
return cache[key]
def rob(self, nums):
if len(nums) == 0:
return 0
return max(
nums[0] + self.helper(nums[2:-1]),
nums[-1] + self.helper(nums[1:-2]),
self.helper(nums[1:-1]),
) | CLASS_DEF FUNC_DEF DICT IF FUNC_CALL VAR VAR NUMBER RETURN NUMBER ASSIGN VAR FUNC_CALL VAR VAR IF VAR VAR RETURN VAR VAR ASSIGN VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER RETURN VAR VAR FUNC_DEF IF FUNC_CALL VAR VAR NUMBER RETURN NUMBER RETURN FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR NUMBER NUMBER BIN_OP VAR NUMBER FUNC_CALL VAR VAR NUMBER NUMBER FUNC_CALL VAR VAR NUMBER NUMBER |
You are a professional robber planning to rob houses along a street. Each house has a certain amount of money stashed. All houses at this place are arranged in a circle. That means the first house is the neighbor of the last one. Meanwhile, adjacent houses have security system connected and it will automatically contact the police if two adjacent houses were broken into on the same night.
Given a list of non-negative integers representing the amount of money of each house, determine the maximum amount of money you can rob tonight without alerting the police.
Example 1:
Input: [2,3,2]
Output: 3
Explanation: You cannot rob house 1 (money = 2) and then rob house 3 (money = 2),
because they are adjacent houses.
Example 2:
Input: [1,2,3,1]
Output: 4
Explanation: Rob house 1 (money = 1) and then rob house 3 (money = 3).
Total amount you can rob = 1 + 3 = 4. | class Solution:
def rob(self, nums):
if not nums:
return 0
mark = [(0) for i in range(len(nums))]
result = list(mark)
if len(nums) == 1:
return nums[0]
if len(nums) == 2:
return max(nums[0], nums[1])
mark[0] = 1
result[0] = nums[0]
if nums[0] > nums[1]:
mark[1] = 1
result[1] = nums[0]
else:
result[1] = nums[1]
for i in range(2, len(nums)):
result[i] = max(nums[i] + result[i - 2], result[i - 1])
if nums[i] + result[i - 2] > result[i - 1]:
mark[i] = mark[i - 2]
else:
mark[i] = mark[i - 1]
if mark[0] == 1 and mark[-1] == 1:
return max(self.solve(nums[1:]), self.solve(nums[:-1]))
return result[-1]
def solve(self, nums):
if not nums:
return 0
if len(nums) == 1:
return nums[0]
if len(nums) == 2:
return max(nums[0], nums[1])
result = [(0) for i in range(len(nums))]
result[0] = nums[0]
result[1] = max(nums[0], nums[1])
for i in range(2, len(nums)):
result[i] = max(nums[i] + result[i - 2], result[i - 1])
return result[-1] | CLASS_DEF FUNC_DEF IF VAR RETURN NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR NUMBER RETURN VAR NUMBER IF FUNC_CALL VAR VAR NUMBER RETURN FUNC_CALL VAR VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER NUMBER ASSIGN VAR NUMBER VAR NUMBER IF VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER NUMBER ASSIGN VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER IF BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER IF VAR NUMBER NUMBER VAR NUMBER NUMBER RETURN FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER RETURN VAR NUMBER FUNC_DEF IF VAR RETURN NUMBER IF FUNC_CALL VAR VAR NUMBER RETURN VAR NUMBER IF FUNC_CALL VAR VAR NUMBER RETURN FUNC_CALL VAR VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER FUNC_CALL VAR VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER RETURN VAR NUMBER |
You are a professional robber planning to rob houses along a street. Each house has a certain amount of money stashed. All houses at this place are arranged in a circle. That means the first house is the neighbor of the last one. Meanwhile, adjacent houses have security system connected and it will automatically contact the police if two adjacent houses were broken into on the same night.
Given a list of non-negative integers representing the amount of money of each house, determine the maximum amount of money you can rob tonight without alerting the police.
Example 1:
Input: [2,3,2]
Output: 3
Explanation: You cannot rob house 1 (money = 2) and then rob house 3 (money = 2),
because they are adjacent houses.
Example 2:
Input: [1,2,3,1]
Output: 4
Explanation: Rob house 1 (money = 1) and then rob house 3 (money = 3).
Total amount you can rob = 1 + 3 = 4. | class Solution:
def rob(self, nums):
if len(nums) == 1:
return nums[0]
last, now = 0, 0
for i in nums[:-1]:
last, now = now, max(last + i, now)
ret = now
last, now = 0, 0
for i in nums[1:]:
last, now = now, max(last + i, now)
return max(ret, now) | CLASS_DEF FUNC_DEF IF FUNC_CALL VAR VAR NUMBER RETURN VAR NUMBER ASSIGN VAR VAR NUMBER NUMBER FOR VAR VAR NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR NUMBER NUMBER FOR VAR VAR NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR RETURN FUNC_CALL VAR VAR VAR |
You are a professional robber planning to rob houses along a street. Each house has a certain amount of money stashed. All houses at this place are arranged in a circle. That means the first house is the neighbor of the last one. Meanwhile, adjacent houses have security system connected and it will automatically contact the police if two adjacent houses were broken into on the same night.
Given a list of non-negative integers representing the amount of money of each house, determine the maximum amount of money you can rob tonight without alerting the police.
Example 1:
Input: [2,3,2]
Output: 3
Explanation: You cannot rob house 1 (money = 2) and then rob house 3 (money = 2),
because they are adjacent houses.
Example 2:
Input: [1,2,3,1]
Output: 4
Explanation: Rob house 1 (money = 1) and then rob house 3 (money = 3).
Total amount you can rob = 1 + 3 = 4. | class Solution:
def rob(self, nums):
if not nums:
return 0
if len(nums) == 1:
return nums[0]
return max(self.helper(nums[1:]), self.helper(nums[:-1]))
def helper(self, nums):
now = prev = 0
for nxt in nums:
now, prev = max(nxt + prev, now), now
return now | CLASS_DEF FUNC_DEF IF VAR RETURN NUMBER IF FUNC_CALL VAR VAR NUMBER RETURN VAR NUMBER RETURN FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER FUNC_DEF ASSIGN VAR VAR NUMBER FOR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR RETURN VAR |
You are a professional robber planning to rob houses along a street. Each house has a certain amount of money stashed. All houses at this place are arranged in a circle. That means the first house is the neighbor of the last one. Meanwhile, adjacent houses have security system connected and it will automatically contact the police if two adjacent houses were broken into on the same night.
Given a list of non-negative integers representing the amount of money of each house, determine the maximum amount of money you can rob tonight without alerting the police.
Example 1:
Input: [2,3,2]
Output: 3
Explanation: You cannot rob house 1 (money = 2) and then rob house 3 (money = 2),
because they are adjacent houses.
Example 2:
Input: [1,2,3,1]
Output: 4
Explanation: Rob house 1 (money = 1) and then rob house 3 (money = 3).
Total amount you can rob = 1 + 3 = 4. | class Solution:
def rob(self, nums):
if len(nums) == 0:
return 0
if len(nums) <= 2:
return max(nums[0], nums[-1])
a = [(0) for _ in range(len(nums) - 1)]
b = a.copy()
c = b.copy()
a[0] = nums[0]
b[0] = nums[-1]
for i in range(1, len(nums) - 1):
a[i] = max(a[i - 1], a[i - 2] + nums[i])
b[i] = max(b[i - 1], b[i - 2] + nums[-i - 1])
return max(a[-1], b[-1]) | CLASS_DEF FUNC_DEF IF FUNC_CALL VAR VAR NUMBER RETURN NUMBER IF FUNC_CALL VAR VAR NUMBER RETURN FUNC_CALL VAR VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER RETURN FUNC_CALL VAR VAR NUMBER VAR NUMBER |
Given a string S, the task is to count number of subsequences of the form a^{i}b^{j}c^{k}, where i >= 1, j >=1 and k >= 1.
Note:
1. Two subsequences are considered different if the set of array indexes picked for the 2 subsequences are different.
2. For large test cases, the output value will be too large, return the answer MODULO 10^9+7
Example 1:
Input:
S = "abbc"
Output: 3
Explanation: Subsequences are abc, abc and abbc.
Example 2:
Input:
S = "abcabc"
Output: 7
Explanation: Subsequences are abc, abc,
abbc, aabc abcc, abc and abc.
Your Task:
You don't need to read input or print anything. Your task is to complete the function fun() which takes the string S as input parameter and returns the number of subsequences which follows given condition.
Expected Time Complexity: O(Length of String).
Expected Auxiliary Space: O(1) .
Constraints:
1 <= |S| <= 10^{5} | class Solution:
def fun(self, s):
a_cnt, b_cnt, c_cnt = 0, 0, 0
for char in s:
if char == "a":
a_cnt = 1 + 2 * a_cnt
if char == "b":
b_cnt = a_cnt + 2 * b_cnt
if char == "c":
c_cnt = b_cnt + 2 * c_cnt
if a_cnt > 0 and b_cnt > 0 and c_cnt > 0:
return c_cnt % (10**9 + 7)
return 0 | CLASS_DEF FUNC_DEF ASSIGN VAR VAR VAR NUMBER NUMBER NUMBER FOR VAR VAR IF VAR STRING ASSIGN VAR BIN_OP NUMBER BIN_OP NUMBER VAR IF VAR STRING ASSIGN VAR BIN_OP VAR BIN_OP NUMBER VAR IF VAR STRING ASSIGN VAR BIN_OP VAR BIN_OP NUMBER VAR IF VAR NUMBER VAR NUMBER VAR NUMBER RETURN BIN_OP VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER RETURN NUMBER |
Given a string S, the task is to count number of subsequences of the form a^{i}b^{j}c^{k}, where i >= 1, j >=1 and k >= 1.
Note:
1. Two subsequences are considered different if the set of array indexes picked for the 2 subsequences are different.
2. For large test cases, the output value will be too large, return the answer MODULO 10^9+7
Example 1:
Input:
S = "abbc"
Output: 3
Explanation: Subsequences are abc, abc and abbc.
Example 2:
Input:
S = "abcabc"
Output: 7
Explanation: Subsequences are abc, abc,
abbc, aabc abcc, abc and abc.
Your Task:
You don't need to read input or print anything. Your task is to complete the function fun() which takes the string S as input parameter and returns the number of subsequences which follows given condition.
Expected Time Complexity: O(Length of String).
Expected Auxiliary Space: O(1) .
Constraints:
1 <= |S| <= 10^{5} | class Solution:
def fun(self, s):
n = len(s)
dp = [([0] * (n + 1)) for _ in range(4)]
for i in range(n + 1):
dp[0][i] = 1
for i in range(1, 4):
for j in range(1, n + 1):
dp[i][j] = dp[i][j - 1]
if ord(s[j - 1]) - 96 == i:
dp[i][j] = (dp[i][j] + dp[i - 1][j - 1] + dp[i][j - 1]) % (
10**9 + 7
)
return dp[3][n] | CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR VAR VAR VAR BIN_OP VAR NUMBER IF BIN_OP FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER VAR ASSIGN VAR VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER BIN_OP BIN_OP NUMBER NUMBER NUMBER RETURN VAR NUMBER VAR |
Given a string S, the task is to count number of subsequences of the form a^{i}b^{j}c^{k}, where i >= 1, j >=1 and k >= 1.
Note:
1. Two subsequences are considered different if the set of array indexes picked for the 2 subsequences are different.
2. For large test cases, the output value will be too large, return the answer MODULO 10^9+7
Example 1:
Input:
S = "abbc"
Output: 3
Explanation: Subsequences are abc, abc and abbc.
Example 2:
Input:
S = "abcabc"
Output: 7
Explanation: Subsequences are abc, abc,
abbc, aabc abcc, abc and abc.
Your Task:
You don't need to read input or print anything. Your task is to complete the function fun() which takes the string S as input parameter and returns the number of subsequences which follows given condition.
Expected Time Complexity: O(Length of String).
Expected Auxiliary Space: O(1) .
Constraints:
1 <= |S| <= 10^{5} | class Solution:
def fun(self, s):
mod = 1000000007
a = 0
b = 0
c = 0
n = len(s)
for i in range(n):
if s[i] == "a":
a = (1 + 2 * a) % mod
if s[i] == "b":
b = (a + 2 * b) % mod
if s[i] == "c":
c = (b + 2 * c) % mod
return c | CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR STRING ASSIGN VAR BIN_OP BIN_OP NUMBER BIN_OP NUMBER VAR VAR IF VAR VAR STRING ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP NUMBER VAR VAR IF VAR VAR STRING ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP NUMBER VAR VAR RETURN VAR |
Given a string S, the task is to count number of subsequences of the form a^{i}b^{j}c^{k}, where i >= 1, j >=1 and k >= 1.
Note:
1. Two subsequences are considered different if the set of array indexes picked for the 2 subsequences are different.
2. For large test cases, the output value will be too large, return the answer MODULO 10^9+7
Example 1:
Input:
S = "abbc"
Output: 3
Explanation: Subsequences are abc, abc and abbc.
Example 2:
Input:
S = "abcabc"
Output: 7
Explanation: Subsequences are abc, abc,
abbc, aabc abcc, abc and abc.
Your Task:
You don't need to read input or print anything. Your task is to complete the function fun() which takes the string S as input parameter and returns the number of subsequences which follows given condition.
Expected Time Complexity: O(Length of String).
Expected Auxiliary Space: O(1) .
Constraints:
1 <= |S| <= 10^{5} | class Solution:
def fun(self, s):
_mod_ = 1000000007
a = ab = abc = 0
for c in s:
if c == "a":
a = (2 * a % _mod_ + 1) % _mod_
if c == "b":
ab = (2 * ab % _mod_ + a) % _mod_
if c == "c":
abc = (2 * abc % _mod_ + ab) % _mod_
return abc | CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR VAR VAR NUMBER FOR VAR VAR IF VAR STRING ASSIGN VAR BIN_OP BIN_OP BIN_OP BIN_OP NUMBER VAR VAR NUMBER VAR IF VAR STRING ASSIGN VAR BIN_OP BIN_OP BIN_OP BIN_OP NUMBER VAR VAR VAR VAR IF VAR STRING ASSIGN VAR BIN_OP BIN_OP BIN_OP BIN_OP NUMBER VAR VAR VAR VAR RETURN VAR |
Given a string S, the task is to count number of subsequences of the form a^{i}b^{j}c^{k}, where i >= 1, j >=1 and k >= 1.
Note:
1. Two subsequences are considered different if the set of array indexes picked for the 2 subsequences are different.
2. For large test cases, the output value will be too large, return the answer MODULO 10^9+7
Example 1:
Input:
S = "abbc"
Output: 3
Explanation: Subsequences are abc, abc and abbc.
Example 2:
Input:
S = "abcabc"
Output: 7
Explanation: Subsequences are abc, abc,
abbc, aabc abcc, abc and abc.
Your Task:
You don't need to read input or print anything. Your task is to complete the function fun() which takes the string S as input parameter and returns the number of subsequences which follows given condition.
Expected Time Complexity: O(Length of String).
Expected Auxiliary Space: O(1) .
Constraints:
1 <= |S| <= 10^{5} | class Solution:
def fun(self, string):
result = self.solve(string)
return result % 1000000007
def solve(self, string):
a = b = c = 0
for current in string:
if current == "a":
a = 2 * a + 1
elif current == "b":
b = 2 * b + a
elif current == "c":
c = 2 * c + b
return c | CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR RETURN BIN_OP VAR NUMBER FUNC_DEF ASSIGN VAR VAR VAR NUMBER FOR VAR VAR IF VAR STRING ASSIGN VAR BIN_OP BIN_OP NUMBER VAR NUMBER IF VAR STRING ASSIGN VAR BIN_OP BIN_OP NUMBER VAR VAR IF VAR STRING ASSIGN VAR BIN_OP BIN_OP NUMBER VAR VAR RETURN VAR |
Given a string S, the task is to count number of subsequences of the form a^{i}b^{j}c^{k}, where i >= 1, j >=1 and k >= 1.
Note:
1. Two subsequences are considered different if the set of array indexes picked for the 2 subsequences are different.
2. For large test cases, the output value will be too large, return the answer MODULO 10^9+7
Example 1:
Input:
S = "abbc"
Output: 3
Explanation: Subsequences are abc, abc and abbc.
Example 2:
Input:
S = "abcabc"
Output: 7
Explanation: Subsequences are abc, abc,
abbc, aabc abcc, abc and abc.
Your Task:
You don't need to read input or print anything. Your task is to complete the function fun() which takes the string S as input parameter and returns the number of subsequences which follows given condition.
Expected Time Complexity: O(Length of String).
Expected Auxiliary Space: O(1) .
Constraints:
1 <= |S| <= 10^{5} | class Solution:
def fun(self, s):
n = len(s)
mod = 10**9 + 7
aa = 0
bb = 0
cc = 0
for ele in s[::-1]:
if ele == "c":
cc += cc
cc += 1
cc = cc % mod
if ele == "b":
bb += bb
bb += cc
bb = bb % mod
if ele == "a":
aa += aa
aa += bb
aa = aa % mod
return aa | CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR NUMBER IF VAR STRING VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR IF VAR STRING VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR IF VAR STRING VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR RETURN VAR |
Given a string S, the task is to count number of subsequences of the form a^{i}b^{j}c^{k}, where i >= 1, j >=1 and k >= 1.
Note:
1. Two subsequences are considered different if the set of array indexes picked for the 2 subsequences are different.
2. For large test cases, the output value will be too large, return the answer MODULO 10^9+7
Example 1:
Input:
S = "abbc"
Output: 3
Explanation: Subsequences are abc, abc and abbc.
Example 2:
Input:
S = "abcabc"
Output: 7
Explanation: Subsequences are abc, abc,
abbc, aabc abcc, abc and abc.
Your Task:
You don't need to read input or print anything. Your task is to complete the function fun() which takes the string S as input parameter and returns the number of subsequences which follows given condition.
Expected Time Complexity: O(Length of String).
Expected Auxiliary Space: O(1) .
Constraints:
1 <= |S| <= 10^{5} | mod = 10**9 + 7
class Solution:
def fun(self, s):
a = 0
ab = 0
abc = 0
for i in s:
if i == "a":
a = (2 * a + 1) % mod
if i == "b":
ab = (2 * ab + a) % mod
if i == "c":
abc = (2 * abc + ab) % mod
return abc % mod | ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF VAR STRING ASSIGN VAR BIN_OP BIN_OP BIN_OP NUMBER VAR NUMBER VAR IF VAR STRING ASSIGN VAR BIN_OP BIN_OP BIN_OP NUMBER VAR VAR VAR IF VAR STRING ASSIGN VAR BIN_OP BIN_OP BIN_OP NUMBER VAR VAR VAR RETURN BIN_OP VAR VAR |
Given a string S, the task is to count number of subsequences of the form a^{i}b^{j}c^{k}, where i >= 1, j >=1 and k >= 1.
Note:
1. Two subsequences are considered different if the set of array indexes picked for the 2 subsequences are different.
2. For large test cases, the output value will be too large, return the answer MODULO 10^9+7
Example 1:
Input:
S = "abbc"
Output: 3
Explanation: Subsequences are abc, abc and abbc.
Example 2:
Input:
S = "abcabc"
Output: 7
Explanation: Subsequences are abc, abc,
abbc, aabc abcc, abc and abc.
Your Task:
You don't need to read input or print anything. Your task is to complete the function fun() which takes the string S as input parameter and returns the number of subsequences which follows given condition.
Expected Time Complexity: O(Length of String).
Expected Auxiliary Space: O(1) .
Constraints:
1 <= |S| <= 10^{5} | class Solution:
def fun(self, s):
ca = 0
c_ab = 0
c_abc = 0
for i in range(len(s)):
if s[i] == "a":
ca = 1 + 2 * ca
elif s[i] == "b":
c_ab = ca + 2 * c_ab
else:
c_abc = (c_ab + 2 * c_abc) % (10**9 + 7)
return c_abc | CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR STRING ASSIGN VAR BIN_OP NUMBER BIN_OP NUMBER VAR IF VAR VAR STRING ASSIGN VAR BIN_OP VAR BIN_OP NUMBER VAR ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP NUMBER VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER RETURN VAR |
Given a string S, the task is to count number of subsequences of the form a^{i}b^{j}c^{k}, where i >= 1, j >=1 and k >= 1.
Note:
1. Two subsequences are considered different if the set of array indexes picked for the 2 subsequences are different.
2. For large test cases, the output value will be too large, return the answer MODULO 10^9+7
Example 1:
Input:
S = "abbc"
Output: 3
Explanation: Subsequences are abc, abc and abbc.
Example 2:
Input:
S = "abcabc"
Output: 7
Explanation: Subsequences are abc, abc,
abbc, aabc abcc, abc and abc.
Your Task:
You don't need to read input or print anything. Your task is to complete the function fun() which takes the string S as input parameter and returns the number of subsequences which follows given condition.
Expected Time Complexity: O(Length of String).
Expected Auxiliary Space: O(1) .
Constraints:
1 <= |S| <= 10^{5} | class Solution:
def fun(self, s):
a = 0
b = 0
c = 0
for char in s:
if char == "a":
newa = 2 * a + 1
a = newa
if char == "b":
newb = 2 * b + a
b = newb
if char == "c":
newc = 2 * c + b
c = newc
return c % 1000000007 | CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF VAR STRING ASSIGN VAR BIN_OP BIN_OP NUMBER VAR NUMBER ASSIGN VAR VAR IF VAR STRING ASSIGN VAR BIN_OP BIN_OP NUMBER VAR VAR ASSIGN VAR VAR IF VAR STRING ASSIGN VAR BIN_OP BIN_OP NUMBER VAR VAR ASSIGN VAR VAR RETURN BIN_OP VAR NUMBER |
Given a string S, the task is to count number of subsequences of the form a^{i}b^{j}c^{k}, where i >= 1, j >=1 and k >= 1.
Note:
1. Two subsequences are considered different if the set of array indexes picked for the 2 subsequences are different.
2. For large test cases, the output value will be too large, return the answer MODULO 10^9+7
Example 1:
Input:
S = "abbc"
Output: 3
Explanation: Subsequences are abc, abc and abbc.
Example 2:
Input:
S = "abcabc"
Output: 7
Explanation: Subsequences are abc, abc,
abbc, aabc abcc, abc and abc.
Your Task:
You don't need to read input or print anything. Your task is to complete the function fun() which takes the string S as input parameter and returns the number of subsequences which follows given condition.
Expected Time Complexity: O(Length of String).
Expected Auxiliary Space: O(1) .
Constraints:
1 <= |S| <= 10^{5} | class Solution:
def fun(self, s):
ac, abc, abcc, mod = 0, 0, 0, 1000000007
for i in s:
if i == "a":
ac += ac + 1
ac %= mod
if i == "b":
abc += ac + abc
abc %= mod
if i == "c":
abcc += abc + abcc
abcc %= mod
return abcc | CLASS_DEF FUNC_DEF ASSIGN VAR VAR VAR VAR NUMBER NUMBER NUMBER NUMBER FOR VAR VAR IF VAR STRING VAR BIN_OP VAR NUMBER VAR VAR IF VAR STRING VAR BIN_OP VAR VAR VAR VAR IF VAR STRING VAR BIN_OP VAR VAR VAR VAR RETURN VAR |
Given a string S, the task is to count number of subsequences of the form a^{i}b^{j}c^{k}, where i >= 1, j >=1 and k >= 1.
Note:
1. Two subsequences are considered different if the set of array indexes picked for the 2 subsequences are different.
2. For large test cases, the output value will be too large, return the answer MODULO 10^9+7
Example 1:
Input:
S = "abbc"
Output: 3
Explanation: Subsequences are abc, abc and abbc.
Example 2:
Input:
S = "abcabc"
Output: 7
Explanation: Subsequences are abc, abc,
abbc, aabc abcc, abc and abc.
Your Task:
You don't need to read input or print anything. Your task is to complete the function fun() which takes the string S as input parameter and returns the number of subsequences which follows given condition.
Expected Time Complexity: O(Length of String).
Expected Auxiliary Space: O(1) .
Constraints:
1 <= |S| <= 10^{5} | class Solution:
def fun(self, s):
acount, bcount, ccount = 0, 0, 0
mod = 1000000007
for i in range(len(s)):
if s[i] == "a":
acount += acount + 1
acount = acount % mod
elif s[i] == "b":
bcount += bcount + acount
bcount = bcount % mod
elif s[i] == "c":
ccount += ccount + bcount
ccount = ccount % mod
return ccount | CLASS_DEF FUNC_DEF ASSIGN VAR VAR VAR NUMBER NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR STRING VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR IF VAR VAR STRING VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR IF VAR VAR STRING VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR RETURN VAR |
Given a string S, the task is to count number of subsequences of the form a^{i}b^{j}c^{k}, where i >= 1, j >=1 and k >= 1.
Note:
1. Two subsequences are considered different if the set of array indexes picked for the 2 subsequences are different.
2. For large test cases, the output value will be too large, return the answer MODULO 10^9+7
Example 1:
Input:
S = "abbc"
Output: 3
Explanation: Subsequences are abc, abc and abbc.
Example 2:
Input:
S = "abcabc"
Output: 7
Explanation: Subsequences are abc, abc,
abbc, aabc abcc, abc and abc.
Your Task:
You don't need to read input or print anything. Your task is to complete the function fun() which takes the string S as input parameter and returns the number of subsequences which follows given condition.
Expected Time Complexity: O(Length of String).
Expected Auxiliary Space: O(1) .
Constraints:
1 <= |S| <= 10^{5} | class Solution:
def fun(self, s):
sub_sequences = [0, 0, 0]
i = len(s) - 1
while i >= 0:
if s[i] is "c":
sub_sequences[2] = (2 * sub_sequences[2] + 1) % 1000000007
elif s[i] is "b":
sub_sequences[1] = (
2 * sub_sequences[1] + sub_sequences[2]
) % 1000000007
elif s[i] is "a":
sub_sequences[0] = (
2 * sub_sequences[0] + sub_sequences[1]
) % 1000000007
i = i - 1
return sub_sequences[0]
t = int(input())
for _ in range(t):
s = input()
print(Solution().fun(s)) | CLASS_DEF FUNC_DEF ASSIGN VAR LIST NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER WHILE VAR NUMBER IF VAR VAR STRING ASSIGN VAR NUMBER BIN_OP BIN_OP BIN_OP NUMBER VAR NUMBER NUMBER NUMBER IF VAR VAR STRING ASSIGN VAR NUMBER BIN_OP BIN_OP BIN_OP NUMBER VAR NUMBER VAR NUMBER NUMBER IF VAR VAR STRING ASSIGN VAR NUMBER BIN_OP BIN_OP BIN_OP NUMBER VAR NUMBER VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER RETURN VAR NUMBER 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 FUNC_CALL VAR VAR |
Given a string S, the task is to count number of subsequences of the form a^{i}b^{j}c^{k}, where i >= 1, j >=1 and k >= 1.
Note:
1. Two subsequences are considered different if the set of array indexes picked for the 2 subsequences are different.
2. For large test cases, the output value will be too large, return the answer MODULO 10^9+7
Example 1:
Input:
S = "abbc"
Output: 3
Explanation: Subsequences are abc, abc and abbc.
Example 2:
Input:
S = "abcabc"
Output: 7
Explanation: Subsequences are abc, abc,
abbc, aabc abcc, abc and abc.
Your Task:
You don't need to read input or print anything. Your task is to complete the function fun() which takes the string S as input parameter and returns the number of subsequences which follows given condition.
Expected Time Complexity: O(Length of String).
Expected Auxiliary Space: O(1) .
Constraints:
1 <= |S| <= 10^{5} | class Solution:
def fun(self, s):
acount, bcount, ccount = 0, 0, 0
for i in s:
if i == "a":
acount = 1 + 2 * acount
elif i == "b":
bcount = 2 * bcount + acount
else:
ccount = 2 * ccount + bcount
return ccount % (10**9 + 7) | CLASS_DEF FUNC_DEF ASSIGN VAR VAR VAR NUMBER NUMBER NUMBER FOR VAR VAR IF VAR STRING ASSIGN VAR BIN_OP NUMBER BIN_OP NUMBER VAR IF VAR STRING ASSIGN VAR BIN_OP BIN_OP NUMBER VAR VAR ASSIGN VAR BIN_OP BIN_OP NUMBER VAR VAR RETURN BIN_OP VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER |
Given a string S, the task is to count number of subsequences of the form a^{i}b^{j}c^{k}, where i >= 1, j >=1 and k >= 1.
Note:
1. Two subsequences are considered different if the set of array indexes picked for the 2 subsequences are different.
2. For large test cases, the output value will be too large, return the answer MODULO 10^9+7
Example 1:
Input:
S = "abbc"
Output: 3
Explanation: Subsequences are abc, abc and abbc.
Example 2:
Input:
S = "abcabc"
Output: 7
Explanation: Subsequences are abc, abc,
abbc, aabc abcc, abc and abc.
Your Task:
You don't need to read input or print anything. Your task is to complete the function fun() which takes the string S as input parameter and returns the number of subsequences which follows given condition.
Expected Time Complexity: O(Length of String).
Expected Auxiliary Space: O(1) .
Constraints:
1 <= |S| <= 10^{5} | class Solution:
def fun(self, s):
N, MOD = len(s), 10**9 + 7
acnt = sum(1 for c in s if c == "a")
ans, avar, bvar, cvar = 0, 0, 0, 0
for i in reversed(range(N)):
x = s[i]
if x == "c":
cvar = (2 * cvar + 1) % MOD
elif x == "b":
bvar = (2 * bvar + cvar) % MOD
else:
acnt -= 1
avar = 2**acnt % MOD
ans = (ans + avar * bvar) % MOD
return ans | CLASS_DEF FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR NUMBER VAR VAR VAR STRING ASSIGN VAR VAR VAR VAR NUMBER NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR IF VAR STRING ASSIGN VAR BIN_OP BIN_OP BIN_OP NUMBER VAR NUMBER VAR IF VAR STRING ASSIGN VAR BIN_OP BIN_OP BIN_OP NUMBER VAR VAR VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP NUMBER VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP VAR VAR VAR RETURN VAR |
Given a string S, the task is to count number of subsequences of the form a^{i}b^{j}c^{k}, where i >= 1, j >=1 and k >= 1.
Note:
1. Two subsequences are considered different if the set of array indexes picked for the 2 subsequences are different.
2. For large test cases, the output value will be too large, return the answer MODULO 10^9+7
Example 1:
Input:
S = "abbc"
Output: 3
Explanation: Subsequences are abc, abc and abbc.
Example 2:
Input:
S = "abcabc"
Output: 7
Explanation: Subsequences are abc, abc,
abbc, aabc abcc, abc and abc.
Your Task:
You don't need to read input or print anything. Your task is to complete the function fun() which takes the string S as input parameter and returns the number of subsequences which follows given condition.
Expected Time Complexity: O(Length of String).
Expected Auxiliary Space: O(1) .
Constraints:
1 <= |S| <= 10^{5} | class Solution:
def fun(self, string):
result = self.solve(string)
return result % 1000000007
def solve(self, string):
dp = [(0) for i in range(3)]
for current in string:
if current == "a":
dp[0] = 2 * dp[0] + 1
elif current == "b":
dp[1] = 2 * dp[1] + dp[0]
elif current == "c":
dp[2] = 2 * dp[2] + dp[1]
return dp[2] | CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR RETURN BIN_OP VAR NUMBER FUNC_DEF ASSIGN VAR NUMBER VAR FUNC_CALL VAR NUMBER FOR VAR VAR IF VAR STRING ASSIGN VAR NUMBER BIN_OP BIN_OP NUMBER VAR NUMBER NUMBER IF VAR STRING ASSIGN VAR NUMBER BIN_OP BIN_OP NUMBER VAR NUMBER VAR NUMBER IF VAR STRING ASSIGN VAR NUMBER BIN_OP BIN_OP NUMBER VAR NUMBER VAR NUMBER RETURN VAR NUMBER |
Given a string S, the task is to count number of subsequences of the form a^{i}b^{j}c^{k}, where i >= 1, j >=1 and k >= 1.
Note:
1. Two subsequences are considered different if the set of array indexes picked for the 2 subsequences are different.
2. For large test cases, the output value will be too large, return the answer MODULO 10^9+7
Example 1:
Input:
S = "abbc"
Output: 3
Explanation: Subsequences are abc, abc and abbc.
Example 2:
Input:
S = "abcabc"
Output: 7
Explanation: Subsequences are abc, abc,
abbc, aabc abcc, abc and abc.
Your Task:
You don't need to read input or print anything. Your task is to complete the function fun() which takes the string S as input parameter and returns the number of subsequences which follows given condition.
Expected Time Complexity: O(Length of String).
Expected Auxiliary Space: O(1) .
Constraints:
1 <= |S| <= 10^{5} | class Solution:
def fun(self, s):
a, b, c = 0, 0, 0
MOD = 10**9 + 7
for ch in s:
if ch == "a":
a = 2 * a + 1
elif ch == "b":
b = 2 * b + a
else:
c = 2 * c + b
return c % MOD
t = int(input())
for _ in range(t):
s = input()
print(Solution().fun(s)) | CLASS_DEF FUNC_DEF ASSIGN VAR VAR VAR NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER FOR VAR VAR IF VAR STRING ASSIGN VAR BIN_OP BIN_OP NUMBER VAR NUMBER IF VAR STRING ASSIGN VAR BIN_OP BIN_OP NUMBER VAR VAR ASSIGN VAR BIN_OP BIN_OP NUMBER VAR VAR RETURN BIN_OP VAR 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 FUNC_CALL VAR VAR |
Given a string S, the task is to count number of subsequences of the form a^{i}b^{j}c^{k}, where i >= 1, j >=1 and k >= 1.
Note:
1. Two subsequences are considered different if the set of array indexes picked for the 2 subsequences are different.
2. For large test cases, the output value will be too large, return the answer MODULO 10^9+7
Example 1:
Input:
S = "abbc"
Output: 3
Explanation: Subsequences are abc, abc and abbc.
Example 2:
Input:
S = "abcabc"
Output: 7
Explanation: Subsequences are abc, abc,
abbc, aabc abcc, abc and abc.
Your Task:
You don't need to read input or print anything. Your task is to complete the function fun() which takes the string S as input parameter and returns the number of subsequences which follows given condition.
Expected Time Complexity: O(Length of String).
Expected Auxiliary Space: O(1) .
Constraints:
1 <= |S| <= 10^{5} | class Solution:
def fun(self, s):
a = 0
ab = 0
abc = 0
ln = len(s)
for x in range(ln):
if s[x] == "a":
a = (2 * a % 1000000007 + 1) % 1000000007
elif s[x] == "b":
ab = (2 * ab % 1000000007 + a) % 1000000007
else:
abc = (2 * abc % 1000000007 + ab) % 1000000007
return abc | CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR STRING ASSIGN VAR BIN_OP BIN_OP BIN_OP BIN_OP NUMBER VAR NUMBER NUMBER NUMBER IF VAR VAR STRING ASSIGN VAR BIN_OP BIN_OP BIN_OP BIN_OP NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP BIN_OP NUMBER VAR NUMBER VAR NUMBER RETURN VAR |
Given a string S, the task is to count number of subsequences of the form a^{i}b^{j}c^{k}, where i >= 1, j >=1 and k >= 1.
Note:
1. Two subsequences are considered different if the set of array indexes picked for the 2 subsequences are different.
2. For large test cases, the output value will be too large, return the answer MODULO 10^9+7
Example 1:
Input:
S = "abbc"
Output: 3
Explanation: Subsequences are abc, abc and abbc.
Example 2:
Input:
S = "abcabc"
Output: 7
Explanation: Subsequences are abc, abc,
abbc, aabc abcc, abc and abc.
Your Task:
You don't need to read input or print anything. Your task is to complete the function fun() which takes the string S as input parameter and returns the number of subsequences which follows given condition.
Expected Time Complexity: O(Length of String).
Expected Auxiliary Space: O(1) .
Constraints:
1 <= |S| <= 10^{5} | import sys
class Solution:
def fun(self, s):
mod = 1000000000.0 + 7
acount = 0
bcount = 0
ccount = 0
for i in range(len(s)):
if s[i] == "a":
acount = (1 + 2 * acount % mod) % mod
elif s[i] == "b":
bcount = (acount % mod + 2 * bcount % mod) % mod
elif s[i] == "c":
ccount = (bcount % mod + 2 * ccount % mod) % mod
return int(ccount) | IMPORT CLASS_DEF FUNC_DEF ASSIGN VAR BIN_OP NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR STRING ASSIGN VAR BIN_OP BIN_OP NUMBER BIN_OP BIN_OP NUMBER VAR VAR VAR IF VAR VAR STRING ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR BIN_OP BIN_OP NUMBER VAR VAR VAR IF VAR VAR STRING ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR BIN_OP BIN_OP NUMBER VAR VAR VAR RETURN FUNC_CALL VAR VAR |
Given a string S, the task is to count number of subsequences of the form a^{i}b^{j}c^{k}, where i >= 1, j >=1 and k >= 1.
Note:
1. Two subsequences are considered different if the set of array indexes picked for the 2 subsequences are different.
2. For large test cases, the output value will be too large, return the answer MODULO 10^9+7
Example 1:
Input:
S = "abbc"
Output: 3
Explanation: Subsequences are abc, abc and abbc.
Example 2:
Input:
S = "abcabc"
Output: 7
Explanation: Subsequences are abc, abc,
abbc, aabc abcc, abc and abc.
Your Task:
You don't need to read input or print anything. Your task is to complete the function fun() which takes the string S as input parameter and returns the number of subsequences which follows given condition.
Expected Time Complexity: O(Length of String).
Expected Auxiliary Space: O(1) .
Constraints:
1 <= |S| <= 10^{5} | class Solution:
def fun(self, s):
arr = list(s)
a = ab = abc = 0
for i in range(len(arr)):
if arr[i] == "a":
a = 2 * a + 1
elif arr[i] == "b":
ab = 2 * ab + a
elif arr[i] == "c":
abc = 2 * abc + ab
return abc % 1000000007 | CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR STRING ASSIGN VAR BIN_OP BIN_OP NUMBER VAR NUMBER IF VAR VAR STRING ASSIGN VAR BIN_OP BIN_OP NUMBER VAR VAR IF VAR VAR STRING ASSIGN VAR BIN_OP BIN_OP NUMBER VAR VAR RETURN BIN_OP VAR NUMBER |
Given a string S, the task is to count number of subsequences of the form a^{i}b^{j}c^{k}, where i >= 1, j >=1 and k >= 1.
Note:
1. Two subsequences are considered different if the set of array indexes picked for the 2 subsequences are different.
2. For large test cases, the output value will be too large, return the answer MODULO 10^9+7
Example 1:
Input:
S = "abbc"
Output: 3
Explanation: Subsequences are abc, abc and abbc.
Example 2:
Input:
S = "abcabc"
Output: 7
Explanation: Subsequences are abc, abc,
abbc, aabc abcc, abc and abc.
Your Task:
You don't need to read input or print anything. Your task is to complete the function fun() which takes the string S as input parameter and returns the number of subsequences which follows given condition.
Expected Time Complexity: O(Length of String).
Expected Auxiliary Space: O(1) .
Constraints:
1 <= |S| <= 10^{5} | class Solution:
def fun(self, s):
m = 1000000007
mat = [[0], [0], [0]]
for i in range(0, len(s)):
if s[i] == "a":
mat[0].append((mat[0][-1] * 2 % m + 1) % m)
if s[i] == "b":
mat[1].append((mat[1][-1] * 2 % m + mat[0][-1]) % m)
if s[i] == "c":
mat[2].append((mat[2][-1] * 2 % m + mat[1][-1]) % m)
return mat[-1][-1] | CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR LIST LIST NUMBER LIST NUMBER LIST NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF VAR VAR STRING EXPR FUNC_CALL VAR NUMBER BIN_OP BIN_OP BIN_OP BIN_OP VAR NUMBER NUMBER NUMBER VAR NUMBER VAR IF VAR VAR STRING EXPR FUNC_CALL VAR NUMBER BIN_OP BIN_OP BIN_OP BIN_OP VAR NUMBER NUMBER NUMBER VAR VAR NUMBER NUMBER VAR IF VAR VAR STRING EXPR FUNC_CALL VAR NUMBER BIN_OP BIN_OP BIN_OP BIN_OP VAR NUMBER NUMBER NUMBER VAR VAR NUMBER NUMBER VAR RETURN VAR NUMBER NUMBER |
Given a string S, the task is to count number of subsequences of the form a^{i}b^{j}c^{k}, where i >= 1, j >=1 and k >= 1.
Note:
1. Two subsequences are considered different if the set of array indexes picked for the 2 subsequences are different.
2. For large test cases, the output value will be too large, return the answer MODULO 10^9+7
Example 1:
Input:
S = "abbc"
Output: 3
Explanation: Subsequences are abc, abc and abbc.
Example 2:
Input:
S = "abcabc"
Output: 7
Explanation: Subsequences are abc, abc,
abbc, aabc abcc, abc and abc.
Your Task:
You don't need to read input or print anything. Your task is to complete the function fun() which takes the string S as input parameter and returns the number of subsequences which follows given condition.
Expected Time Complexity: O(Length of String).
Expected Auxiliary Space: O(1) .
Constraints:
1 <= |S| <= 10^{5} | class Solution:
def fun(self, s):
c = 0
bc = 0
ans = 0
l = len(s)
for i in range(l - 1, -1, -1):
if s[i] == "c":
c = 2 * c + 1
elif s[i] == "b":
bc = 2 * bc + c
elif s[i] == "a":
ans = 2 * ans + bc
return ans % 1000000007 | CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR VAR STRING ASSIGN VAR BIN_OP BIN_OP NUMBER VAR NUMBER IF VAR VAR STRING ASSIGN VAR BIN_OP BIN_OP NUMBER VAR VAR IF VAR VAR STRING ASSIGN VAR BIN_OP BIN_OP NUMBER VAR VAR RETURN BIN_OP VAR NUMBER |
Given a string S, the task is to count number of subsequences of the form a^{i}b^{j}c^{k}, where i >= 1, j >=1 and k >= 1.
Note:
1. Two subsequences are considered different if the set of array indexes picked for the 2 subsequences are different.
2. For large test cases, the output value will be too large, return the answer MODULO 10^9+7
Example 1:
Input:
S = "abbc"
Output: 3
Explanation: Subsequences are abc, abc and abbc.
Example 2:
Input:
S = "abcabc"
Output: 7
Explanation: Subsequences are abc, abc,
abbc, aabc abcc, abc and abc.
Your Task:
You don't need to read input or print anything. Your task is to complete the function fun() which takes the string S as input parameter and returns the number of subsequences which follows given condition.
Expected Time Complexity: O(Length of String).
Expected Auxiliary Space: O(1) .
Constraints:
1 <= |S| <= 10^{5} | class Solution:
def fun(self, s):
if len(s) == 1:
return 0
a_count = 0
b_count = 0
c_count = 0
for i in range(len(s)):
if s[i] == "a":
a_count = 1 + 2 * a_count
elif s[i] == "b":
b_count = a_count + 2 * b_count
elif s[i] == "c":
c_count = b_count + 2 * c_count
return c_count % (10**9 + 7) | CLASS_DEF FUNC_DEF IF FUNC_CALL VAR VAR NUMBER RETURN NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR STRING ASSIGN VAR BIN_OP NUMBER BIN_OP NUMBER VAR IF VAR VAR STRING ASSIGN VAR BIN_OP VAR BIN_OP NUMBER VAR IF VAR VAR STRING ASSIGN VAR BIN_OP VAR BIN_OP NUMBER VAR RETURN BIN_OP VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER |
Given a string S, the task is to count number of subsequences of the form a^{i}b^{j}c^{k}, where i >= 1, j >=1 and k >= 1.
Note:
1. Two subsequences are considered different if the set of array indexes picked for the 2 subsequences are different.
2. For large test cases, the output value will be too large, return the answer MODULO 10^9+7
Example 1:
Input:
S = "abbc"
Output: 3
Explanation: Subsequences are abc, abc and abbc.
Example 2:
Input:
S = "abcabc"
Output: 7
Explanation: Subsequences are abc, abc,
abbc, aabc abcc, abc and abc.
Your Task:
You don't need to read input or print anything. Your task is to complete the function fun() which takes the string S as input parameter and returns the number of subsequences which follows given condition.
Expected Time Complexity: O(Length of String).
Expected Auxiliary Space: O(1) .
Constraints:
1 <= |S| <= 10^{5} | class Solution:
def fun(self, s):
a, ab, abc = 0, 0, 0
mod = 1000000007
for i in range(len(s)):
if s[i] == "a":
a = (2 * a + 1) % mod
elif s[i] == "b":
ab = (2 * ab + a) % mod
elif s[i] == "c":
abc = (2 * abc + ab) % mod
return abc
t = int(input())
for _ in range(t):
s = input()
print(Solution().fun(s)) | CLASS_DEF FUNC_DEF ASSIGN VAR VAR VAR NUMBER NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR STRING ASSIGN VAR BIN_OP BIN_OP BIN_OP NUMBER VAR NUMBER VAR IF VAR VAR STRING ASSIGN VAR BIN_OP BIN_OP BIN_OP NUMBER VAR VAR VAR IF VAR VAR STRING ASSIGN VAR BIN_OP BIN_OP BIN_OP NUMBER VAR VAR 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 FUNC_CALL VAR VAR |
Given a string S, the task is to count number of subsequences of the form a^{i}b^{j}c^{k}, where i >= 1, j >=1 and k >= 1.
Note:
1. Two subsequences are considered different if the set of array indexes picked for the 2 subsequences are different.
2. For large test cases, the output value will be too large, return the answer MODULO 10^9+7
Example 1:
Input:
S = "abbc"
Output: 3
Explanation: Subsequences are abc, abc and abbc.
Example 2:
Input:
S = "abcabc"
Output: 7
Explanation: Subsequences are abc, abc,
abbc, aabc abcc, abc and abc.
Your Task:
You don't need to read input or print anything. Your task is to complete the function fun() which takes the string S as input parameter and returns the number of subsequences which follows given condition.
Expected Time Complexity: O(Length of String).
Expected Auxiliary Space: O(1) .
Constraints:
1 <= |S| <= 10^{5} | MOD = int(1000000000.0 + 7)
class Solution:
def fun(self, s):
ss = [0, 0, 0]
n = len(s)
for i in range(n - 1, -1, -1):
if s[i] == "c":
ss[2] = 2 * ss[2] + 1
ss[2] %= MOD
elif s[i] == "b":
ss[1] = 2 * ss[1] + ss[2]
ss[1] %= MOD
elif s[i] == "a":
ss[0] = 2 * ss[0] + ss[1]
ss[0] %= MOD
return ss[0] | ASSIGN VAR FUNC_CALL VAR BIN_OP NUMBER NUMBER CLASS_DEF FUNC_DEF ASSIGN VAR LIST NUMBER NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR VAR STRING ASSIGN VAR NUMBER BIN_OP BIN_OP NUMBER VAR NUMBER NUMBER VAR NUMBER VAR IF VAR VAR STRING ASSIGN VAR NUMBER BIN_OP BIN_OP NUMBER VAR NUMBER VAR NUMBER VAR NUMBER VAR IF VAR VAR STRING ASSIGN VAR NUMBER BIN_OP BIN_OP NUMBER VAR NUMBER VAR NUMBER VAR NUMBER VAR RETURN VAR NUMBER |
Given a string S, the task is to count number of subsequences of the form a^{i}b^{j}c^{k}, where i >= 1, j >=1 and k >= 1.
Note:
1. Two subsequences are considered different if the set of array indexes picked for the 2 subsequences are different.
2. For large test cases, the output value will be too large, return the answer MODULO 10^9+7
Example 1:
Input:
S = "abbc"
Output: 3
Explanation: Subsequences are abc, abc and abbc.
Example 2:
Input:
S = "abcabc"
Output: 7
Explanation: Subsequences are abc, abc,
abbc, aabc abcc, abc and abc.
Your Task:
You don't need to read input or print anything. Your task is to complete the function fun() which takes the string S as input parameter and returns the number of subsequences which follows given condition.
Expected Time Complexity: O(Length of String).
Expected Auxiliary Space: O(1) .
Constraints:
1 <= |S| <= 10^{5} | class Solution:
def fun(self, s):
sub_sequences = [0, 0, 0]
i = len(s) - 1
while i >= 0:
if s[i] is "c":
sub_sequences[2] = (2 * sub_sequences[2] + 1) % 1000000007
elif s[i] is "b":
sub_sequences[1] = (
2 * sub_sequences[1] + sub_sequences[2]
) % 1000000007
elif s[i] is "a":
sub_sequences[0] = (
2 * sub_sequences[0] + sub_sequences[1]
) % 1000000007
i = i - 1
return sub_sequences[0] | CLASS_DEF FUNC_DEF ASSIGN VAR LIST NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER WHILE VAR NUMBER IF VAR VAR STRING ASSIGN VAR NUMBER BIN_OP BIN_OP BIN_OP NUMBER VAR NUMBER NUMBER NUMBER IF VAR VAR STRING ASSIGN VAR NUMBER BIN_OP BIN_OP BIN_OP NUMBER VAR NUMBER VAR NUMBER NUMBER IF VAR VAR STRING ASSIGN VAR NUMBER BIN_OP BIN_OP BIN_OP NUMBER VAR NUMBER VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER RETURN VAR NUMBER |
Given a string S, the task is to count number of subsequences of the form a^{i}b^{j}c^{k}, where i >= 1, j >=1 and k >= 1.
Note:
1. Two subsequences are considered different if the set of array indexes picked for the 2 subsequences are different.
2. For large test cases, the output value will be too large, return the answer MODULO 10^9+7
Example 1:
Input:
S = "abbc"
Output: 3
Explanation: Subsequences are abc, abc and abbc.
Example 2:
Input:
S = "abcabc"
Output: 7
Explanation: Subsequences are abc, abc,
abbc, aabc abcc, abc and abc.
Your Task:
You don't need to read input or print anything. Your task is to complete the function fun() which takes the string S as input parameter and returns the number of subsequences which follows given condition.
Expected Time Complexity: O(Length of String).
Expected Auxiliary Space: O(1) .
Constraints:
1 <= |S| <= 10^{5} | class Solution:
def fun(self, s):
a = [0, 0, 0]
for x in s[::-1]:
if x == "c":
a[2] = (2 * a[2] + 1) % (10**9 + 7)
elif x == "b":
a[1] = (2 * a[1] + a[2]) % (10**9 + 7)
else:
a[0] = (2 * a[0] + a[1]) % (10**9 + 7)
return a[0] | CLASS_DEF FUNC_DEF ASSIGN VAR LIST NUMBER NUMBER NUMBER FOR VAR VAR NUMBER IF VAR STRING ASSIGN VAR NUMBER BIN_OP BIN_OP BIN_OP NUMBER VAR NUMBER NUMBER BIN_OP BIN_OP NUMBER NUMBER NUMBER IF VAR STRING ASSIGN VAR NUMBER BIN_OP BIN_OP BIN_OP NUMBER VAR NUMBER VAR NUMBER BIN_OP BIN_OP NUMBER NUMBER NUMBER ASSIGN VAR NUMBER BIN_OP BIN_OP BIN_OP NUMBER VAR NUMBER VAR NUMBER BIN_OP BIN_OP NUMBER NUMBER NUMBER RETURN VAR NUMBER |
Given a string S, the task is to count number of subsequences of the form a^{i}b^{j}c^{k}, where i >= 1, j >=1 and k >= 1.
Note:
1. Two subsequences are considered different if the set of array indexes picked for the 2 subsequences are different.
2. For large test cases, the output value will be too large, return the answer MODULO 10^9+7
Example 1:
Input:
S = "abbc"
Output: 3
Explanation: Subsequences are abc, abc and abbc.
Example 2:
Input:
S = "abcabc"
Output: 7
Explanation: Subsequences are abc, abc,
abbc, aabc abcc, abc and abc.
Your Task:
You don't need to read input or print anything. Your task is to complete the function fun() which takes the string S as input parameter and returns the number of subsequences which follows given condition.
Expected Time Complexity: O(Length of String).
Expected Auxiliary Space: O(1) .
Constraints:
1 <= |S| <= 10^{5} | class Solution:
def fun(self, s):
n = len(s)
mod = 10**9 + 7
aCount = bCount = cCount = 0
for i in range(n):
if s[i] == "a":
aCount = (1 + 2 * aCount) % mod
elif s[i] == "b":
bCount = (aCount + 2 * bCount) % mod
else:
cCount = (bCount + 2 * cCount) % mod
return cCount | CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER ASSIGN VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR STRING ASSIGN VAR BIN_OP BIN_OP NUMBER BIN_OP NUMBER VAR VAR IF VAR VAR STRING ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP NUMBER VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP NUMBER VAR VAR RETURN VAR |
Given a string S, the task is to count number of subsequences of the form a^{i}b^{j}c^{k}, where i >= 1, j >=1 and k >= 1.
Note:
1. Two subsequences are considered different if the set of array indexes picked for the 2 subsequences are different.
2. For large test cases, the output value will be too large, return the answer MODULO 10^9+7
Example 1:
Input:
S = "abbc"
Output: 3
Explanation: Subsequences are abc, abc and abbc.
Example 2:
Input:
S = "abcabc"
Output: 7
Explanation: Subsequences are abc, abc,
abbc, aabc abcc, abc and abc.
Your Task:
You don't need to read input or print anything. Your task is to complete the function fun() which takes the string S as input parameter and returns the number of subsequences which follows given condition.
Expected Time Complexity: O(Length of String).
Expected Auxiliary Space: O(1) .
Constraints:
1 <= |S| <= 10^{5} | class Solution:
def fun(self, s):
a = 0
ab = 0
abc = 0
for i in range(len(s)):
ch = s[i]
if ch == "a":
a = 2 * a + 1
elif ch == "b":
ab = 2 * ab + a
else:
abc = 2 * abc + ab
return abc % (10**9 + 7)
t = int(input())
for _ in range(t):
s = input()
print(Solution().fun(s)) | CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR IF VAR STRING ASSIGN VAR BIN_OP BIN_OP NUMBER VAR NUMBER IF VAR STRING ASSIGN VAR BIN_OP BIN_OP NUMBER VAR VAR ASSIGN VAR BIN_OP BIN_OP NUMBER VAR VAR RETURN BIN_OP VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER 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 FUNC_CALL VAR VAR |
Given a string S, the task is to count number of subsequences of the form a^{i}b^{j}c^{k}, where i >= 1, j >=1 and k >= 1.
Note:
1. Two subsequences are considered different if the set of array indexes picked for the 2 subsequences are different.
2. For large test cases, the output value will be too large, return the answer MODULO 10^9+7
Example 1:
Input:
S = "abbc"
Output: 3
Explanation: Subsequences are abc, abc and abbc.
Example 2:
Input:
S = "abcabc"
Output: 7
Explanation: Subsequences are abc, abc,
abbc, aabc abcc, abc and abc.
Your Task:
You don't need to read input or print anything. Your task is to complete the function fun() which takes the string S as input parameter and returns the number of subsequences which follows given condition.
Expected Time Complexity: O(Length of String).
Expected Auxiliary Space: O(1) .
Constraints:
1 <= |S| <= 10^{5} | class Solution:
def fun(self, s):
a = 0
ab = 0
abc = 0
for i in s:
if i == "a":
a = a + a + 1
elif i == "b":
ab = ab + ab + a
else:
abc = abc + abc + ab
return abc % (10**9 + 7) | CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF VAR STRING ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR STRING ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR RETURN BIN_OP VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER |
Given a string S, the task is to count number of subsequences of the form a^{i}b^{j}c^{k}, where i >= 1, j >=1 and k >= 1.
Note:
1. Two subsequences are considered different if the set of array indexes picked for the 2 subsequences are different.
2. For large test cases, the output value will be too large, return the answer MODULO 10^9+7
Example 1:
Input:
S = "abbc"
Output: 3
Explanation: Subsequences are abc, abc and abbc.
Example 2:
Input:
S = "abcabc"
Output: 7
Explanation: Subsequences are abc, abc,
abbc, aabc abcc, abc and abc.
Your Task:
You don't need to read input or print anything. Your task is to complete the function fun() which takes the string S as input parameter and returns the number of subsequences which follows given condition.
Expected Time Complexity: O(Length of String).
Expected Auxiliary Space: O(1) .
Constraints:
1 <= |S| <= 10^{5} | class Solution:
def fun(self, s):
P = 10**9 + 7
ans = 0
acount = 0
bcount = 0
ccount = 0
for i in s:
if i == "a":
acount = 1 + 2 * acount
if i == "b":
bcount = acount + 2 * bcount
if i == "c":
ccount = bcount + 2 * ccount
ans = ccount
return ans % P | CLASS_DEF FUNC_DEF ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF VAR STRING ASSIGN VAR BIN_OP NUMBER BIN_OP NUMBER VAR IF VAR STRING ASSIGN VAR BIN_OP VAR BIN_OP NUMBER VAR IF VAR STRING ASSIGN VAR BIN_OP VAR BIN_OP NUMBER VAR ASSIGN VAR VAR RETURN BIN_OP VAR VAR |
Given a string S, the task is to count number of subsequences of the form a^{i}b^{j}c^{k}, where i >= 1, j >=1 and k >= 1.
Note:
1. Two subsequences are considered different if the set of array indexes picked for the 2 subsequences are different.
2. For large test cases, the output value will be too large, return the answer MODULO 10^9+7
Example 1:
Input:
S = "abbc"
Output: 3
Explanation: Subsequences are abc, abc and abbc.
Example 2:
Input:
S = "abcabc"
Output: 7
Explanation: Subsequences are abc, abc,
abbc, aabc abcc, abc and abc.
Your Task:
You don't need to read input or print anything. Your task is to complete the function fun() which takes the string S as input parameter and returns the number of subsequences which follows given condition.
Expected Time Complexity: O(Length of String).
Expected Auxiliary Space: O(1) .
Constraints:
1 <= |S| <= 10^{5} | class Solution:
def fun(self, s):
n = len(s)
dp = [[(0) for j in range(n + 1)] for i in range(4)]
seq = ["a", "b", "c"]
mod = 1000000007
for i in range(n + 1):
dp[0][i] = 1
for i in range(1, 4):
for j in range(1, n + 1):
if seq[i - 1] == s[j - 1]:
dp[i][j] = (dp[i - 1][j - 1] + 2 * dp[i][j - 1]) % mod
else:
dp[i][j] = dp[i][j - 1]
return dp[-1][-1] | CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR NUMBER ASSIGN VAR LIST STRING STRING STRING ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP NUMBER VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR VAR VAR VAR BIN_OP VAR NUMBER RETURN VAR NUMBER NUMBER |
Given a string S, the task is to count number of subsequences of the form a^{i}b^{j}c^{k}, where i >= 1, j >=1 and k >= 1.
Note:
1. Two subsequences are considered different if the set of array indexes picked for the 2 subsequences are different.
2. For large test cases, the output value will be too large, return the answer MODULO 10^9+7
Example 1:
Input:
S = "abbc"
Output: 3
Explanation: Subsequences are abc, abc and abbc.
Example 2:
Input:
S = "abcabc"
Output: 7
Explanation: Subsequences are abc, abc,
abbc, aabc abcc, abc and abc.
Your Task:
You don't need to read input or print anything. Your task is to complete the function fun() which takes the string S as input parameter and returns the number of subsequences which follows given condition.
Expected Time Complexity: O(Length of String).
Expected Auxiliary Space: O(1) .
Constraints:
1 <= |S| <= 10^{5} | class Solution:
def fun(self, S: str) -> int:
MOD = int(1000000000.0) + 7
aCount, bCount, cCount = 0, 0, 0
for ch in S:
if ch == "a":
aCount = 2 * aCount + 1
elif ch == "b":
bCount = 2 * bCount + aCount
elif ch == "c":
cCount = 2 * cCount + bCount
return cCount % MOD | CLASS_DEF FUNC_DEF VAR ASSIGN VAR BIN_OP FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR VAR VAR NUMBER NUMBER NUMBER FOR VAR VAR IF VAR STRING ASSIGN VAR BIN_OP BIN_OP NUMBER VAR NUMBER IF VAR STRING ASSIGN VAR BIN_OP BIN_OP NUMBER VAR VAR IF VAR STRING ASSIGN VAR BIN_OP BIN_OP NUMBER VAR VAR RETURN BIN_OP VAR VAR VAR |
Given a string S, the task is to count number of subsequences of the form a^{i}b^{j}c^{k}, where i >= 1, j >=1 and k >= 1.
Note:
1. Two subsequences are considered different if the set of array indexes picked for the 2 subsequences are different.
2. For large test cases, the output value will be too large, return the answer MODULO 10^9+7
Example 1:
Input:
S = "abbc"
Output: 3
Explanation: Subsequences are abc, abc and abbc.
Example 2:
Input:
S = "abcabc"
Output: 7
Explanation: Subsequences are abc, abc,
abbc, aabc abcc, abc and abc.
Your Task:
You don't need to read input or print anything. Your task is to complete the function fun() which takes the string S as input parameter and returns the number of subsequences which follows given condition.
Expected Time Complexity: O(Length of String).
Expected Auxiliary Space: O(1) .
Constraints:
1 <= |S| <= 10^{5} | class Solution:
def fun(self, s):
a, b, c = 0, 0, 0
M = 1000000007
for x in s:
if x == "a":
a = (2 * a + 1) % M
elif x == "b":
b = (2 * b + a) % M
else:
c = (2 * c + b) % M
return c | CLASS_DEF FUNC_DEF ASSIGN VAR VAR VAR NUMBER NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF VAR STRING ASSIGN VAR BIN_OP BIN_OP BIN_OP NUMBER VAR NUMBER VAR IF VAR STRING ASSIGN VAR BIN_OP BIN_OP BIN_OP NUMBER VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP NUMBER VAR VAR VAR RETURN VAR |
Given a string S, the task is to count number of subsequences of the form a^{i}b^{j}c^{k}, where i >= 1, j >=1 and k >= 1.
Note:
1. Two subsequences are considered different if the set of array indexes picked for the 2 subsequences are different.
2. For large test cases, the output value will be too large, return the answer MODULO 10^9+7
Example 1:
Input:
S = "abbc"
Output: 3
Explanation: Subsequences are abc, abc and abbc.
Example 2:
Input:
S = "abcabc"
Output: 7
Explanation: Subsequences are abc, abc,
abbc, aabc abcc, abc and abc.
Your Task:
You don't need to read input or print anything. Your task is to complete the function fun() which takes the string S as input parameter and returns the number of subsequences which follows given condition.
Expected Time Complexity: O(Length of String).
Expected Auxiliary Space: O(1) .
Constraints:
1 <= |S| <= 10^{5} | class Solution:
def fun(self, s):
sa = 0
sab = 0
sabc = 0
mod = 1000000000.0 + 7
for i in range(0, len(s)):
if s[i] == "a":
sa = 2 * sa + 1
elif s[i] == "b":
sab = sa + 2 * sab
elif s[i] == "c":
sabc = sab + 2 * sabc
sa = sa % mod
sab = sab % mod
sabc = sabc % mod
return int(sabc) | CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF VAR VAR STRING ASSIGN VAR BIN_OP BIN_OP NUMBER VAR NUMBER IF VAR VAR STRING ASSIGN VAR BIN_OP VAR BIN_OP NUMBER VAR IF VAR VAR STRING ASSIGN VAR BIN_OP VAR BIN_OP NUMBER VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR RETURN FUNC_CALL VAR VAR |
Given a string S, the task is to count number of subsequences of the form a^{i}b^{j}c^{k}, where i >= 1, j >=1 and k >= 1.
Note:
1. Two subsequences are considered different if the set of array indexes picked for the 2 subsequences are different.
2. For large test cases, the output value will be too large, return the answer MODULO 10^9+7
Example 1:
Input:
S = "abbc"
Output: 3
Explanation: Subsequences are abc, abc and abbc.
Example 2:
Input:
S = "abcabc"
Output: 7
Explanation: Subsequences are abc, abc,
abbc, aabc abcc, abc and abc.
Your Task:
You don't need to read input or print anything. Your task is to complete the function fun() which takes the string S as input parameter and returns the number of subsequences which follows given condition.
Expected Time Complexity: O(Length of String).
Expected Auxiliary Space: O(1) .
Constraints:
1 <= |S| <= 10^{5} | mod = 10**9 + 7
class Solution:
def __init__(self):
self.DP = []
def solve(self, li, n):
for i in range(0, n):
for ended in range(0, 3):
if i == 0:
s = 1 if li[i] == ended and ended == 0 else 0
self.DP[i][ended] = s
continue
if ended == 0:
c = 1 if li[i] == ended else 0
s = self.DP[i - 1][ended] + c * self.DP[i - 1][ended] + c
self.DP[i][ended] = s % mod
continue
partial = 0
if li[i] == ended:
partial = self.DP[i - 1][ended - 1] + self.DP[i - 1][ended]
s = partial + self.DP[i - 1][ended]
self.DP[i][ended] = s % mod
return self.DP[n - 1][2]
def fun(self, s):
self.DP = [[-1, -1, -1] for i in range(len(s) + 1)]
li = [(ord(i) - ord("a")) for i in s]
return self.solve(li, len(s)) | ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER CLASS_DEF FUNC_DEF ASSIGN VAR LIST FUNC_DEF FOR VAR FUNC_CALL VAR NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER NUMBER IF VAR NUMBER ASSIGN VAR VAR VAR VAR VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR VAR VAR IF VAR NUMBER ASSIGN VAR VAR VAR VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR VAR ASSIGN VAR NUMBER IF VAR VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR VAR BIN_OP VAR VAR RETURN VAR BIN_OP VAR NUMBER NUMBER FUNC_DEF ASSIGN VAR LIST NUMBER NUMBER NUMBER VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR STRING VAR VAR RETURN FUNC_CALL VAR VAR FUNC_CALL VAR VAR |
Given a string S, the task is to count number of subsequences of the form a^{i}b^{j}c^{k}, where i >= 1, j >=1 and k >= 1.
Note:
1. Two subsequences are considered different if the set of array indexes picked for the 2 subsequences are different.
2. For large test cases, the output value will be too large, return the answer MODULO 10^9+7
Example 1:
Input:
S = "abbc"
Output: 3
Explanation: Subsequences are abc, abc and abbc.
Example 2:
Input:
S = "abcabc"
Output: 7
Explanation: Subsequences are abc, abc,
abbc, aabc abcc, abc and abc.
Your Task:
You don't need to read input or print anything. Your task is to complete the function fun() which takes the string S as input parameter and returns the number of subsequences which follows given condition.
Expected Time Complexity: O(Length of String).
Expected Auxiliary Space: O(1) .
Constraints:
1 <= |S| <= 10^{5} | class Solution:
def fun(self, s):
dp = [[(0) for i in range(len(s) + 1)] for j in range(3)]
for i in range(1, len(dp[0])):
if s[i - 1] == "a":
dp[0][i] = dp[0][i - 1] * 2 + 1
dp[1][i] = dp[1][i - 1]
dp[2][i] = dp[2][i - 1]
elif s[i - 1] == "b":
dp[0][i] = dp[0][i - 1]
dp[1][i] = dp[0][i] + dp[1][i - 1] * 2
dp[2][i] = dp[2][i - 1]
else:
dp[0][i] = dp[0][i - 1]
dp[1][i] = dp[1][i - 1]
dp[2][i] = dp[2][i - 1] * 2 + dp[1][i]
return dp[-1][-1] % (10**9 + 7) | CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR NUMBER IF VAR BIN_OP VAR NUMBER STRING ASSIGN VAR NUMBER VAR BIN_OP BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR NUMBER VAR VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER VAR VAR NUMBER BIN_OP VAR NUMBER IF VAR BIN_OP VAR NUMBER STRING ASSIGN VAR NUMBER VAR VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER ASSIGN VAR NUMBER VAR VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER VAR VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER VAR VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER VAR BIN_OP BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER VAR NUMBER VAR RETURN BIN_OP VAR NUMBER NUMBER BIN_OP BIN_OP NUMBER NUMBER NUMBER |
Given a string S, the task is to count number of subsequences of the form a^{i}b^{j}c^{k}, where i >= 1, j >=1 and k >= 1.
Note:
1. Two subsequences are considered different if the set of array indexes picked for the 2 subsequences are different.
2. For large test cases, the output value will be too large, return the answer MODULO 10^9+7
Example 1:
Input:
S = "abbc"
Output: 3
Explanation: Subsequences are abc, abc and abbc.
Example 2:
Input:
S = "abcabc"
Output: 7
Explanation: Subsequences are abc, abc,
abbc, aabc abcc, abc and abc.
Your Task:
You don't need to read input or print anything. Your task is to complete the function fun() which takes the string S as input parameter and returns the number of subsequences which follows given condition.
Expected Time Complexity: O(Length of String).
Expected Auxiliary Space: O(1) .
Constraints:
1 <= |S| <= 10^{5} | import sys
sys.setrecursionlimit(10000)
class Solution:
def __init__(self):
self.memo = {}
self.next = {"a": "b", "b": "c", "c": "0"}
def helper(self, s, ch, start):
if ch == "0":
return 1
if start == len(s):
return 0
key = ch, start
if key in self.memo:
return self.memo[key]
if s[start] != ch:
self.memo[key] = self.helper(s, ch, start + 1)
else:
self.memo[key] = 2 * self.helper(s, ch, start + 1) + self.helper(
s, self.next[ch], start + 1
)
self.memo[key] %= int(10**9 + 7.1)
return self.memo[key]
def fun(self, s):
res = self.helper(s, "a", 0)
return res % int(10**9 + 7.1) | IMPORT EXPR FUNC_CALL VAR NUMBER CLASS_DEF FUNC_DEF ASSIGN VAR DICT ASSIGN VAR DICT STRING STRING STRING STRING STRING STRING FUNC_DEF IF VAR STRING RETURN NUMBER IF VAR FUNC_CALL VAR VAR RETURN NUMBER ASSIGN VAR VAR VAR IF VAR VAR RETURN VAR VAR IF VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP BIN_OP NUMBER FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER VAR VAR FUNC_CALL VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER RETURN VAR VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR STRING NUMBER RETURN BIN_OP VAR FUNC_CALL VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER |
Given a string S, the task is to count number of subsequences of the form a^{i}b^{j}c^{k}, where i >= 1, j >=1 and k >= 1.
Note:
1. Two subsequences are considered different if the set of array indexes picked for the 2 subsequences are different.
2. For large test cases, the output value will be too large, return the answer MODULO 10^9+7
Example 1:
Input:
S = "abbc"
Output: 3
Explanation: Subsequences are abc, abc and abbc.
Example 2:
Input:
S = "abcabc"
Output: 7
Explanation: Subsequences are abc, abc,
abbc, aabc abcc, abc and abc.
Your Task:
You don't need to read input or print anything. Your task is to complete the function fun() which takes the string S as input parameter and returns the number of subsequences which follows given condition.
Expected Time Complexity: O(Length of String).
Expected Auxiliary Space: O(1) .
Constraints:
1 <= |S| <= 10^{5} | class Solution:
def fun(self, s):
a, ab, abc = 0, 0, 0
mod = 10**9 + 7
for i in range(len(s)):
if s[i] == "a":
a = (2 * a + 1) % mod
elif s[i] == "b":
ab = (ab * 2 + a) % mod
else:
abc = (2 * abc + ab) % mod
return abc | CLASS_DEF FUNC_DEF ASSIGN VAR VAR VAR NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR STRING ASSIGN VAR BIN_OP BIN_OP BIN_OP NUMBER VAR NUMBER VAR IF VAR VAR STRING ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR NUMBER VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP NUMBER VAR VAR VAR RETURN VAR |
Given a string S, the task is to count number of subsequences of the form a^{i}b^{j}c^{k}, where i >= 1, j >=1 and k >= 1.
Note:
1. Two subsequences are considered different if the set of array indexes picked for the 2 subsequences are different.
2. For large test cases, the output value will be too large, return the answer MODULO 10^9+7
Example 1:
Input:
S = "abbc"
Output: 3
Explanation: Subsequences are abc, abc and abbc.
Example 2:
Input:
S = "abcabc"
Output: 7
Explanation: Subsequences are abc, abc,
abbc, aabc abcc, abc and abc.
Your Task:
You don't need to read input or print anything. Your task is to complete the function fun() which takes the string S as input parameter and returns the number of subsequences which follows given condition.
Expected Time Complexity: O(Length of String).
Expected Auxiliary Space: O(1) .
Constraints:
1 <= |S| <= 10^{5} | class Solution:
def fun(self, s):
mod = 1000000007
a, b, c = 0, 0, 0
for i in s:
if i == "a":
a = (1 + 2 * a) % mod
elif i == "b":
b = (a + 2 * b) % mod
elif i == "c":
c = (b + 2 * c) % mod
return c | CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR VAR VAR NUMBER NUMBER NUMBER FOR VAR VAR IF VAR STRING ASSIGN VAR BIN_OP BIN_OP NUMBER BIN_OP NUMBER VAR VAR IF VAR STRING ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP NUMBER VAR VAR IF VAR STRING ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP NUMBER VAR VAR RETURN VAR |
Given a string S, the task is to count number of subsequences of the form a^{i}b^{j}c^{k}, where i >= 1, j >=1 and k >= 1.
Note:
1. Two subsequences are considered different if the set of array indexes picked for the 2 subsequences are different.
2. For large test cases, the output value will be too large, return the answer MODULO 10^9+7
Example 1:
Input:
S = "abbc"
Output: 3
Explanation: Subsequences are abc, abc and abbc.
Example 2:
Input:
S = "abcabc"
Output: 7
Explanation: Subsequences are abc, abc,
abbc, aabc abcc, abc and abc.
Your Task:
You don't need to read input or print anything. Your task is to complete the function fun() which takes the string S as input parameter and returns the number of subsequences which follows given condition.
Expected Time Complexity: O(Length of String).
Expected Auxiliary Space: O(1) .
Constraints:
1 <= |S| <= 10^{5} | class Solution:
def fun(self, s):
m = 10**9 + 7
ac, bc, cc = 0, 0, 0
n = len(s)
for i in range(n):
if s[i] == "a":
ac = 1 + 2 * ac
elif s[i] == "b":
bc = ac + 2 * bc
elif s[i] == "c":
cc = bc + 2 * cc
return cc % m | CLASS_DEF FUNC_DEF ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER ASSIGN VAR VAR VAR NUMBER NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR STRING ASSIGN VAR BIN_OP NUMBER BIN_OP NUMBER VAR IF VAR VAR STRING ASSIGN VAR BIN_OP VAR BIN_OP NUMBER VAR IF VAR VAR STRING ASSIGN VAR BIN_OP VAR BIN_OP NUMBER VAR RETURN BIN_OP VAR VAR |
Given a string S, the task is to count number of subsequences of the form a^{i}b^{j}c^{k}, where i >= 1, j >=1 and k >= 1.
Note:
1. Two subsequences are considered different if the set of array indexes picked for the 2 subsequences are different.
2. For large test cases, the output value will be too large, return the answer MODULO 10^9+7
Example 1:
Input:
S = "abbc"
Output: 3
Explanation: Subsequences are abc, abc and abbc.
Example 2:
Input:
S = "abcabc"
Output: 7
Explanation: Subsequences are abc, abc,
abbc, aabc abcc, abc and abc.
Your Task:
You don't need to read input or print anything. Your task is to complete the function fun() which takes the string S as input parameter and returns the number of subsequences which follows given condition.
Expected Time Complexity: O(Length of String).
Expected Auxiliary Space: O(1) .
Constraints:
1 <= |S| <= 10^{5} | class Solution:
def fun(self, s):
a, b, c = 0, 0, 0
m = 1000000000.0 + 7
for i in range(len(s)):
if s[i] == "a":
a = int((2 * a + 1) % m)
if s[i] == "b":
b = int((2 * b + a) % m)
if s[i] == "c":
c = int((2 * c + b) % m)
return c | CLASS_DEF FUNC_DEF ASSIGN VAR VAR VAR NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP NUMBER NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR STRING ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP NUMBER VAR NUMBER VAR IF VAR VAR STRING ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP NUMBER VAR VAR VAR IF VAR VAR STRING ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP NUMBER VAR VAR VAR RETURN VAR |
Given a string S, the task is to count number of subsequences of the form a^{i}b^{j}c^{k}, where i >= 1, j >=1 and k >= 1.
Note:
1. Two subsequences are considered different if the set of array indexes picked for the 2 subsequences are different.
2. For large test cases, the output value will be too large, return the answer MODULO 10^9+7
Example 1:
Input:
S = "abbc"
Output: 3
Explanation: Subsequences are abc, abc and abbc.
Example 2:
Input:
S = "abcabc"
Output: 7
Explanation: Subsequences are abc, abc,
abbc, aabc abcc, abc and abc.
Your Task:
You don't need to read input or print anything. Your task is to complete the function fun() which takes the string S as input parameter and returns the number of subsequences which follows given condition.
Expected Time Complexity: O(Length of String).
Expected Auxiliary Space: O(1) .
Constraints:
1 <= |S| <= 10^{5} | class Solution:
def fun(self, s):
a = b = c = 0
for idx in range(len(s)):
alphabet = s[idx]
if alphabet == "a":
a = 2 * a + 1
elif alphabet == "b":
b = 2 * b + a
else:
c = 2 * c + b
return c % (10**9 + 7) | CLASS_DEF FUNC_DEF ASSIGN VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR IF VAR STRING ASSIGN VAR BIN_OP BIN_OP NUMBER VAR NUMBER IF VAR STRING ASSIGN VAR BIN_OP BIN_OP NUMBER VAR VAR ASSIGN VAR BIN_OP BIN_OP NUMBER VAR VAR RETURN BIN_OP VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER |
Given a string S, the task is to count number of subsequences of the form a^{i}b^{j}c^{k}, where i >= 1, j >=1 and k >= 1.
Note:
1. Two subsequences are considered different if the set of array indexes picked for the 2 subsequences are different.
2. For large test cases, the output value will be too large, return the answer MODULO 10^9+7
Example 1:
Input:
S = "abbc"
Output: 3
Explanation: Subsequences are abc, abc and abbc.
Example 2:
Input:
S = "abcabc"
Output: 7
Explanation: Subsequences are abc, abc,
abbc, aabc abcc, abc and abc.
Your Task:
You don't need to read input or print anything. Your task is to complete the function fun() which takes the string S as input parameter and returns the number of subsequences which follows given condition.
Expected Time Complexity: O(Length of String).
Expected Auxiliary Space: O(1) .
Constraints:
1 <= |S| <= 10^{5} | class Solution:
def fun(self, s):
a = 0
b = 0
c = 0
m = 10**9 + 7
n = len(s)
for i in range(n):
if s[i] == "a":
a, b, c = 2 * a + 1, b, c
elif s[i] == "b":
a, b, c = a, 2 * b + a, c
else:
a, b, c = a, b, 2 * c + b
a = a % m
b = b % m
c = c % m
return c
t = int(input())
for _ in range(t):
s = input()
print(Solution().fun(s)) | CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR STRING ASSIGN VAR VAR VAR BIN_OP BIN_OP NUMBER VAR NUMBER VAR VAR IF VAR VAR STRING ASSIGN VAR VAR VAR VAR BIN_OP BIN_OP NUMBER VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR BIN_OP BIN_OP NUMBER VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR 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 FUNC_CALL VAR VAR |
Given a string S, the task is to count number of subsequences of the form a^{i}b^{j}c^{k}, where i >= 1, j >=1 and k >= 1.
Note:
1. Two subsequences are considered different if the set of array indexes picked for the 2 subsequences are different.
2. For large test cases, the output value will be too large, return the answer MODULO 10^9+7
Example 1:
Input:
S = "abbc"
Output: 3
Explanation: Subsequences are abc, abc and abbc.
Example 2:
Input:
S = "abcabc"
Output: 7
Explanation: Subsequences are abc, abc,
abbc, aabc abcc, abc and abc.
Your Task:
You don't need to read input or print anything. Your task is to complete the function fun() which takes the string S as input parameter and returns the number of subsequences which follows given condition.
Expected Time Complexity: O(Length of String).
Expected Auxiliary Space: O(1) .
Constraints:
1 <= |S| <= 10^{5} | class Solution:
def fun(self, s):
n = len(s)
a_count = 0
ab_count = 0
abc_count = 0
for i in range(n):
if s[i] == "a":
a_count = 2 * a_count + 1
elif s[i] == "b":
ab_count = 2 * ab_count + a_count
elif s[i] == "c":
abc_count = 2 * abc_count + ab_count
return abc_count % (10**9 + 7) | CLASS_DEF FUNC_DEF 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 ASSIGN VAR BIN_OP BIN_OP NUMBER VAR NUMBER IF VAR VAR STRING ASSIGN VAR BIN_OP BIN_OP NUMBER VAR VAR IF VAR VAR STRING ASSIGN VAR BIN_OP BIN_OP NUMBER VAR VAR RETURN BIN_OP VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER |
Given a string S, the task is to count number of subsequences of the form a^{i}b^{j}c^{k}, where i >= 1, j >=1 and k >= 1.
Note:
1. Two subsequences are considered different if the set of array indexes picked for the 2 subsequences are different.
2. For large test cases, the output value will be too large, return the answer MODULO 10^9+7
Example 1:
Input:
S = "abbc"
Output: 3
Explanation: Subsequences are abc, abc and abbc.
Example 2:
Input:
S = "abcabc"
Output: 7
Explanation: Subsequences are abc, abc,
abbc, aabc abcc, abc and abc.
Your Task:
You don't need to read input or print anything. Your task is to complete the function fun() which takes the string S as input parameter and returns the number of subsequences which follows given condition.
Expected Time Complexity: O(Length of String).
Expected Auxiliary Space: O(1) .
Constraints:
1 <= |S| <= 10^{5} | class Solution:
def fun(self, s):
MOD = 10**9 + 7
n = len(s)
aCount = abCount = abcCount = 0
for i in range(n):
if s[i] == "a":
aCount = (1 + aCount * 2) % MOD
elif s[i] == "b":
abCount = (aCount + abCount * 2) % MOD
else:
abcCount = (abCount + abcCount * 2) % MOD
return abcCount | CLASS_DEF FUNC_DEF ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR STRING ASSIGN VAR BIN_OP BIN_OP NUMBER BIN_OP VAR NUMBER VAR IF VAR VAR STRING ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR RETURN VAR |
Given a string S, the task is to count number of subsequences of the form a^{i}b^{j}c^{k}, where i >= 1, j >=1 and k >= 1.
Note:
1. Two subsequences are considered different if the set of array indexes picked for the 2 subsequences are different.
2. For large test cases, the output value will be too large, return the answer MODULO 10^9+7
Example 1:
Input:
S = "abbc"
Output: 3
Explanation: Subsequences are abc, abc and abbc.
Example 2:
Input:
S = "abcabc"
Output: 7
Explanation: Subsequences are abc, abc,
abbc, aabc abcc, abc and abc.
Your Task:
You don't need to read input or print anything. Your task is to complete the function fun() which takes the string S as input parameter and returns the number of subsequences which follows given condition.
Expected Time Complexity: O(Length of String).
Expected Auxiliary Space: O(1) .
Constraints:
1 <= |S| <= 10^{5} | MOD = pow(10, 9) + 7
class Solution:
def fun(self, s):
a = ab = abc = 0
for char in s:
if char == "a":
a = (a + a + 1) % MOD
elif char == "b":
ab = (ab + ab + a) % MOD
elif char == "c":
abc = (abc + abc + ab) % MOD
return abc | ASSIGN VAR BIN_OP FUNC_CALL VAR NUMBER NUMBER NUMBER CLASS_DEF FUNC_DEF ASSIGN VAR VAR VAR NUMBER FOR VAR VAR IF VAR STRING ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER VAR IF VAR STRING ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR IF VAR STRING ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR RETURN VAR |
Given two integers N and M. Find the longest common contiguous subset in binary representation of both the numbers and print its decimal value.
Example 1:
Input: N = 10, M = 11
Output: 5
Explanation: 10 in Binary is "1010" and
11 in Binary is "1011". The longest common
contiguous subset is "101" which has a
decimal value of 5.
ââ¬â¹Example 2:
Input: N = 8, M = 16
Output: 8
Explanation: 8 in Binary is "1000" and
16 in Binary is "10000". The longest common
contiguous subset is "1000" which has a
decimal value of 8.
Your Task:
You don't need to read input or print anything. Your task is to complete the function longestCommon() which takes two integers N and M as inputs and returns the Decimal representation of the longest common contiguous subset in the Binary representation of N and M.
Note: If there's a tie in the length of the longest common contiguous subset, return the one with the highest decimal value among them.
Expected Time Complexity: O((log(max (N, M))^{3}).
Expected Auxiliary Space: O((log(max (N, M))^{2}).
Constraints:
1<=N,M<=10^{18} | class Solution:
def longestCommon(self, N, M):
d = []
e = []
while N > 0:
d.append(N % 2)
N //= 2
while M > 0:
e.append(M % 2)
M //= 2
a1 = {}
for i in range(len(d)):
c = 0
p = 1
s = ""
for j in range(i, len(d)):
s += str(d[j])
a1[s] = 1
res = 0
c2 = 0
for i in range(len(e)):
c = 0
p = 1
s = ""
c1 = 0
for j in range(i, len(e)):
c += p * e[j]
p *= 2
s += str(e[j])
c1 += 1
if s in a1:
if c1 > c2:
res = c
c2 = c1
elif c1 == c2:
res = max(res, c)
return res | CLASS_DEF FUNC_DEF ASSIGN VAR LIST ASSIGN VAR LIST WHILE VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER WHILE VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR DICT FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR STRING FOR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR STRING ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR VAR VAR NUMBER VAR FUNC_CALL VAR VAR VAR VAR NUMBER IF VAR VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN VAR |
Given two integers N and M. Find the longest common contiguous subset in binary representation of both the numbers and print its decimal value.
Example 1:
Input: N = 10, M = 11
Output: 5
Explanation: 10 in Binary is "1010" and
11 in Binary is "1011". The longest common
contiguous subset is "101" which has a
decimal value of 5.
ââ¬â¹Example 2:
Input: N = 8, M = 16
Output: 8
Explanation: 8 in Binary is "1000" and
16 in Binary is "10000". The longest common
contiguous subset is "1000" which has a
decimal value of 8.
Your Task:
You don't need to read input or print anything. Your task is to complete the function longestCommon() which takes two integers N and M as inputs and returns the Decimal representation of the longest common contiguous subset in the Binary representation of N and M.
Note: If there's a tie in the length of the longest common contiguous subset, return the one with the highest decimal value among them.
Expected Time Complexity: O((log(max (N, M))^{3}).
Expected Auxiliary Space: O((log(max (N, M))^{2}).
Constraints:
1<=N,M<=10^{18} | class Solution:
def longestCommon(self, N, M):
s1 = bin(N)[2:]
s2 = bin(M)[2:]
ans = 0
n, m = len(s1), len(s2)
dp = [[(0) for i in range(m + 1)] for j in range(n + 1)]
for i in range(1, n + 1):
for j in range(1, m + 1):
if s1[i - 1] == s2[j - 1]:
dp[i][j] = 1 + dp[i - 1][j - 1]
else:
dp[i][j] = 0
ans = max(ans, dp[i][j])
l = []
for i in range(n + 1):
for j in range(m + 1):
if dp[i][j] == ans:
l.append(i - 1)
res = 0
for i in range(len(l)):
s = ""
index = l[i]
s = s1[index - ans + 1 : index + 1]
res = max(res, int(s, 2))
return res | CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR BIN_OP NUMBER VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR STRING ASSIGN VAR VAR VAR ASSIGN VAR VAR BIN_OP BIN_OP VAR VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR NUMBER RETURN VAR |
Given two integers N and M. Find the longest common contiguous subset in binary representation of both the numbers and print its decimal value.
Example 1:
Input: N = 10, M = 11
Output: 5
Explanation: 10 in Binary is "1010" and
11 in Binary is "1011". The longest common
contiguous subset is "101" which has a
decimal value of 5.
ââ¬â¹Example 2:
Input: N = 8, M = 16
Output: 8
Explanation: 8 in Binary is "1000" and
16 in Binary is "10000". The longest common
contiguous subset is "1000" which has a
decimal value of 8.
Your Task:
You don't need to read input or print anything. Your task is to complete the function longestCommon() which takes two integers N and M as inputs and returns the Decimal representation of the longest common contiguous subset in the Binary representation of N and M.
Note: If there's a tie in the length of the longest common contiguous subset, return the one with the highest decimal value among them.
Expected Time Complexity: O((log(max (N, M))^{3}).
Expected Auxiliary Space: O((log(max (N, M))^{2}).
Constraints:
1<=N,M<=10^{18} | from itertools import combinations
class Solution:
def longestCommon(self, N, M):
a = bin(N)[2:]
b = bin(M)[2:]
x = [i for i in range(len(a) + 1)]
y = [i for i in range(len(b) + 1)]
ad = {}
for i, j in combinations(x, 2):
aad = a[i:j]
if aad not in ad:
ad[aad] = 1
bd = []
for i, j in combinations(y, 2):
bbd = b[i:j]
bd.append(bbd)
ans = []
for i in bd:
if i in ad:
ans.append(i)
l = 0
for i in ans:
ll = len(i)
if ll > l:
l = ll
z = []
for i in ans:
if len(i) == l:
z.append(i)
xyz = []
for i in z:
xyz.append(int(i, 2))
return max(xyz) | CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR DICT FOR VAR VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR VAR VAR IF VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR LIST FOR VAR VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR LIST FOR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR LIST FOR VAR VAR IF FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR LIST FOR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER RETURN FUNC_CALL VAR VAR |
Given two integers N and M. Find the longest common contiguous subset in binary representation of both the numbers and print its decimal value.
Example 1:
Input: N = 10, M = 11
Output: 5
Explanation: 10 in Binary is "1010" and
11 in Binary is "1011". The longest common
contiguous subset is "101" which has a
decimal value of 5.
ââ¬â¹Example 2:
Input: N = 8, M = 16
Output: 8
Explanation: 8 in Binary is "1000" and
16 in Binary is "10000". The longest common
contiguous subset is "1000" which has a
decimal value of 8.
Your Task:
You don't need to read input or print anything. Your task is to complete the function longestCommon() which takes two integers N and M as inputs and returns the Decimal representation of the longest common contiguous subset in the Binary representation of N and M.
Note: If there's a tie in the length of the longest common contiguous subset, return the one with the highest decimal value among them.
Expected Time Complexity: O((log(max (N, M))^{3}).
Expected Auxiliary Space: O((log(max (N, M))^{2}).
Constraints:
1<=N,M<=10^{18} | class Solution:
def longestCommon(self, N, M):
a = bin(N)[2:]
b = bin(M)[2:]
n = len(a)
m = len(b)
dp = []
for i in range(n + 1):
d = [0] * (m + 1)
dp.append(d)
sample = 0
lj = 0
for i in range(1, n + 1):
for j in range(1, m + 1):
if a[i - 1] == b[j - 1]:
dp[i][j] = 1 + dp[i - 1][j - 1]
ll = len(b[j - dp[i][j] : j])
if ll > lj:
sample = int(b[j - dp[i][j] : j], 2)
lj = ll
elif ll == lj:
sample = max(sample, int(b[j - dp[i][j] : j], 2))
lj = ll
else:
dp[i][j] = 0
return sample | CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR BIN_OP NUMBER VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR VAR VAR IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR VAR NUMBER RETURN VAR |
Given two integers N and M. Find the longest common contiguous subset in binary representation of both the numbers and print its decimal value.
Example 1:
Input: N = 10, M = 11
Output: 5
Explanation: 10 in Binary is "1010" and
11 in Binary is "1011". The longest common
contiguous subset is "101" which has a
decimal value of 5.
ââ¬â¹Example 2:
Input: N = 8, M = 16
Output: 8
Explanation: 8 in Binary is "1000" and
16 in Binary is "10000". The longest common
contiguous subset is "1000" which has a
decimal value of 8.
Your Task:
You don't need to read input or print anything. Your task is to complete the function longestCommon() which takes two integers N and M as inputs and returns the Decimal representation of the longest common contiguous subset in the Binary representation of N and M.
Note: If there's a tie in the length of the longest common contiguous subset, return the one with the highest decimal value among them.
Expected Time Complexity: O((log(max (N, M))^{3}).
Expected Auxiliary Space: O((log(max (N, M))^{2}).
Constraints:
1<=N,M<=10^{18} | class Solution:
def longestCommon(self, N, M):
n = bin(N)[2:]
m = bin(M)[2:]
dp = [([None] * (len(n) + 1)) for i in range(len(m) + 1)]
a = []
ans = 0
for i in range(len(m) + 1):
for j in range(len(n) + 1):
if i == 0 or j == 0:
dp[i][j] = 0
elif m[i - 1] == n[j - 1]:
dp[i][j] = 1 + dp[i - 1][j - 1]
if dp[i][j] > ans:
ans = dp[i][j]
a = [(i, j)]
elif dp[i][j] == ans:
a.append((i, j))
else:
dp[i][j] = 0
res = 0
for i, j in a:
s = ""
while dp[i][j] != 0:
s = m[i - 1] + s
i -= 1
j -= 1
res = max(res, int(s, 2))
return res | CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP LIST NONE BIN_OP FUNC_CALL VAR VAR NUMBER VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER IF VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR BIN_OP NUMBER VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR LIST VAR VAR IF VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR VAR ASSIGN VAR STRING WHILE VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR NUMBER RETURN VAR |
Given two integers N and M. Find the longest common contiguous subset in binary representation of both the numbers and print its decimal value.
Example 1:
Input: N = 10, M = 11
Output: 5
Explanation: 10 in Binary is "1010" and
11 in Binary is "1011". The longest common
contiguous subset is "101" which has a
decimal value of 5.
ââ¬â¹Example 2:
Input: N = 8, M = 16
Output: 8
Explanation: 8 in Binary is "1000" and
16 in Binary is "10000". The longest common
contiguous subset is "1000" which has a
decimal value of 8.
Your Task:
You don't need to read input or print anything. Your task is to complete the function longestCommon() which takes two integers N and M as inputs and returns the Decimal representation of the longest common contiguous subset in the Binary representation of N and M.
Note: If there's a tie in the length of the longest common contiguous subset, return the one with the highest decimal value among them.
Expected Time Complexity: O((log(max (N, M))^{3}).
Expected Auxiliary Space: O((log(max (N, M))^{2}).
Constraints:
1<=N,M<=10^{18} | class Solution:
def longestCommon(self, N, M):
a = bin(N)[2:]
b = bin(M)[2:]
n = len(a)
m = len(b)
dp = [([0] * (m + 1)) for i in range(n + 1)]
p = 0
for i in range(1, n + 1):
for j in range(1, m + 1):
if a[i - 1] == b[j - 1]:
dp[i][j] = 1 + dp[i - 1][j - 1]
if p < dp[i][j]:
p = dp[i][j]
x = [(i, j)]
elif p == dp[i][j]:
x.append((i, j))
s = 0
for i, j in x:
d = ""
while dp[i][j] != 0:
d = a[i - 1] + d
i -= 1
j -= 1
s = max(s, int(d, base=2))
return s
return s | CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR BIN_OP NUMBER VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR LIST VAR VAR IF VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER FOR VAR VAR VAR ASSIGN VAR STRING WHILE VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR NUMBER RETURN VAR RETURN VAR |
You are given a program that consists of $n$ instructions. Initially a single variable $x$ is assigned to $0$. Afterwards, the instructions are of two types:
increase $x$ by $1$;
decrease $x$ by $1$.
You are given $m$ queries of the following format:
query $l$ $r$ — how many distinct values is $x$ assigned to if all the instructions between the $l$-th one and the $r$-th one inclusive are ignored and the rest are executed without changing the order?
-----Input-----
The first line contains a single integer $t$ ($1 \le t \le 1000$) — the number of testcases.
Then the description of $t$ testcases follows.
The first line of each testcase contains two integers $n$ and $m$ ($1 \le n, m \le 2 \cdot 10^5$) — the number of instructions in the program and the number of queries.
The second line of each testcase contains a program — a string of $n$ characters: each character is either '+' or '-' — increment and decrement instruction, respectively.
Each of the next $m$ lines contains two integers $l$ and $r$ ($1 \le l \le r \le n$) — the description of the query.
The sum of $n$ over all testcases doesn't exceed $2 \cdot 10^5$. The sum of $m$ over all testcases doesn't exceed $2 \cdot 10^5$.
-----Output-----
For each testcase print $m$ integers — for each query $l$, $r$ print the number of distinct values variable $x$ is assigned to if all the instructions between the $l$-th one and the $r$-th one inclusive are ignored and the rest are executed without changing the order.
-----Examples-----
Input
2
8 4
-+--+--+
1 8
2 8
2 5
1 1
4 10
+-++
1 1
1 2
2 2
1 3
2 3
3 3
1 4
2 4
3 4
4 4
Output
1
2
4
4
3
3
4
2
3
2
1
2
2
2
-----Note-----
The instructions that remain for each query of the first testcase are:
empty program — $x$ was only equal to $0$;
"-" — $x$ had values $0$ and $-1$;
"---+" — $x$ had values $0$, $-1$, $-2$, $-3$, $-2$ — there are $4$ distinct values among them;
"+--+--+" — the distinct values are $1$, $0$, $-1$, $-2$. | import sys
stdin = list(list(sys.stdin))
t = int(stdin[0].strip())
j = 1
for i in range(t):
inp = stdin[j].rstrip().split(" ")
n = int(inp[0])
m = int(inp[1])
j += 1
a = stdin[j].rstrip()
j += 1
x = [0]
left = [[0, 0]]
for i in a:
if i == "-":
x.append(x[-1] - 1)
else:
x.append(x[-1] + 1)
left.append([left[-1][0], left[-1][1]])
if x[-1] > left[-1][1]:
left[-1][1] = x[-1]
if x[-1] < left[-1][0]:
left[-1][0] = x[-1]
right = [[x[-1], x[-1]]]
for v in x[::-1][1:]:
right.append([right[-1][0], right[-1][1]])
if v > right[-1][1]:
right[-1][1] = v
if v < right[-1][0]:
right[-1][0] = v
right = right[::-1]
for z in range(m):
inp = stdin[j].rstrip().split(" ")
l = int(inp[0])
r = int(inp[1])
o = min(left[l - 1][0], right[r][0] - x[r] + x[l - 1])
d = max(left[l - 1][1], right[r][1] - x[r] + x[l - 1])
print(d - o + 1)
j += 1 | IMPORT ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR VAR STRING ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR LIST NUMBER ASSIGN VAR LIST LIST NUMBER NUMBER FOR VAR VAR IF VAR STRING EXPR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR LIST VAR NUMBER NUMBER VAR NUMBER NUMBER IF VAR NUMBER VAR NUMBER NUMBER ASSIGN VAR NUMBER NUMBER VAR NUMBER IF VAR NUMBER VAR NUMBER NUMBER ASSIGN VAR NUMBER NUMBER VAR NUMBER ASSIGN VAR LIST LIST VAR NUMBER VAR NUMBER FOR VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR LIST VAR NUMBER NUMBER VAR NUMBER NUMBER IF VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER NUMBER VAR IF VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER NUMBER VAR ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR VAR STRING ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER BIN_OP BIN_OP VAR VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER BIN_OP BIN_OP VAR VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER VAR NUMBER |
You are given a program that consists of $n$ instructions. Initially a single variable $x$ is assigned to $0$. Afterwards, the instructions are of two types:
increase $x$ by $1$;
decrease $x$ by $1$.
You are given $m$ queries of the following format:
query $l$ $r$ — how many distinct values is $x$ assigned to if all the instructions between the $l$-th one and the $r$-th one inclusive are ignored and the rest are executed without changing the order?
-----Input-----
The first line contains a single integer $t$ ($1 \le t \le 1000$) — the number of testcases.
Then the description of $t$ testcases follows.
The first line of each testcase contains two integers $n$ and $m$ ($1 \le n, m \le 2 \cdot 10^5$) — the number of instructions in the program and the number of queries.
The second line of each testcase contains a program — a string of $n$ characters: each character is either '+' or '-' — increment and decrement instruction, respectively.
Each of the next $m$ lines contains two integers $l$ and $r$ ($1 \le l \le r \le n$) — the description of the query.
The sum of $n$ over all testcases doesn't exceed $2 \cdot 10^5$. The sum of $m$ over all testcases doesn't exceed $2 \cdot 10^5$.
-----Output-----
For each testcase print $m$ integers — for each query $l$, $r$ print the number of distinct values variable $x$ is assigned to if all the instructions between the $l$-th one and the $r$-th one inclusive are ignored and the rest are executed without changing the order.
-----Examples-----
Input
2
8 4
-+--+--+
1 8
2 8
2 5
1 1
4 10
+-++
1 1
1 2
2 2
1 3
2 3
3 3
1 4
2 4
3 4
4 4
Output
1
2
4
4
3
3
4
2
3
2
1
2
2
2
-----Note-----
The instructions that remain for each query of the first testcase are:
empty program — $x$ was only equal to $0$;
"-" — $x$ had values $0$ and $-1$;
"---+" — $x$ had values $0$, $-1$, $-2$, $-3$, $-2$ — there are $4$ distinct values among them;
"+--+--+" — the distinct values are $1$, $0$, $-1$, $-2$. | def find_answers(program, queries):
x = 0
min_x = 0
max_x = 0
min_values = [0]
max_values = [0]
x_values = [0]
for ch in program:
if ch == "+":
x += 1
else:
x -= 1
min_x = min(min_x, x)
max_x = max(max_x, x)
min_values.append(min_x)
max_values.append(max_x)
x_values.append(x)
x = 0
min_x = 0
max_x = 0
min_values_r = [0]
max_values_r = [0]
x_values_r = [0]
for ch in list(program)[::-1]:
if ch == "+":
x -= 1
else:
x += 1
min_x = min(min_x, x)
max_x = max(max_x, x)
min_values_r.append(min_x)
max_values_r.append(max_x)
x_values_r.append(x)
min_values_r = min_values_r[::-1]
max_values_r = max_values_r[::-1]
x_values_r = x_values_r[::-1]
result = []
for q in queries:
l, r = q
min_v = min(
min_values[l - 1], x_values[l - 1] + min_values_r[r] - x_values_r[r]
)
max_v = max(
max_values[l - 1], x_values[l - 1] + max_values_r[r] - x_values_r[r]
)
result.append(max_v - min_v + 1)
return result
n_samples = int(input())
for t in range(n_samples):
n, m = map(int, input().split(" "))
program = input()
assert len(program) == n
queries = []
for _ in range(m):
l, r = map(int, input().split(" "))
queries.append((l, r))
answers = find_answers(program, queries)
for a in answers:
print(a) | FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST NUMBER ASSIGN VAR LIST NUMBER ASSIGN VAR LIST NUMBER FOR VAR VAR IF VAR STRING VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST NUMBER ASSIGN VAR LIST NUMBER ASSIGN VAR LIST NUMBER FOR VAR FUNC_CALL VAR VAR NUMBER IF VAR STRING VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR LIST FOR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR 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 STRING ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR STRING EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR |
You are given a program that consists of $n$ instructions. Initially a single variable $x$ is assigned to $0$. Afterwards, the instructions are of two types:
increase $x$ by $1$;
decrease $x$ by $1$.
You are given $m$ queries of the following format:
query $l$ $r$ — how many distinct values is $x$ assigned to if all the instructions between the $l$-th one and the $r$-th one inclusive are ignored and the rest are executed without changing the order?
-----Input-----
The first line contains a single integer $t$ ($1 \le t \le 1000$) — the number of testcases.
Then the description of $t$ testcases follows.
The first line of each testcase contains two integers $n$ and $m$ ($1 \le n, m \le 2 \cdot 10^5$) — the number of instructions in the program and the number of queries.
The second line of each testcase contains a program — a string of $n$ characters: each character is either '+' or '-' — increment and decrement instruction, respectively.
Each of the next $m$ lines contains two integers $l$ and $r$ ($1 \le l \le r \le n$) — the description of the query.
The sum of $n$ over all testcases doesn't exceed $2 \cdot 10^5$. The sum of $m$ over all testcases doesn't exceed $2 \cdot 10^5$.
-----Output-----
For each testcase print $m$ integers — for each query $l$, $r$ print the number of distinct values variable $x$ is assigned to if all the instructions between the $l$-th one and the $r$-th one inclusive are ignored and the rest are executed without changing the order.
-----Examples-----
Input
2
8 4
-+--+--+
1 8
2 8
2 5
1 1
4 10
+-++
1 1
1 2
2 2
1 3
2 3
3 3
1 4
2 4
3 4
4 4
Output
1
2
4
4
3
3
4
2
3
2
1
2
2
2
-----Note-----
The instructions that remain for each query of the first testcase are:
empty program — $x$ was only equal to $0$;
"-" — $x$ had values $0$ and $-1$;
"---+" — $x$ had values $0$, $-1$, $-2$, $-3$, $-2$ — there are $4$ distinct values among them;
"+--+--+" — the distinct values are $1$, $0$, $-1$, $-2$. | import sys
input = sys.stdin.readline
for _ in range(int(input().strip())):
n, q = map(int, input().strip().split(" "))
s = input().strip()
alg = [(0, 0, 0)]
M = 0
m = 0
c = 0
for i in s:
if i == "+":
c += 1
else:
c -= 1
if c > M:
M = c
if c < m:
m = c
alg.append((M, m, c))
lop = [(0, 0, 0)]
M = 0
m = 0
c = 0
for i in reversed(s):
if i == "-":
c += 1
else:
c -= 1
if c > M:
M = c
if c < m:
m = c
lop.append((M, m, c))
for _ in range(q):
l, r = map(int, input().strip().split(" "))
st = alg[l - 1]
en = lop[n - r]
print(max(st[0], st[2] + en[0] - en[2]) - min(st[1], st[2] + en[1] - en[2]) + 1) | IMPORT ASSIGN VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST NUMBER NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF VAR STRING VAR NUMBER VAR NUMBER IF VAR VAR ASSIGN VAR VAR IF VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR LIST NUMBER NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR STRING VAR NUMBER VAR NUMBER IF VAR VAR ASSIGN VAR VAR IF VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP FUNC_CALL VAR VAR NUMBER BIN_OP BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER FUNC_CALL VAR VAR NUMBER BIN_OP BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER NUMBER |
You are given a program that consists of $n$ instructions. Initially a single variable $x$ is assigned to $0$. Afterwards, the instructions are of two types:
increase $x$ by $1$;
decrease $x$ by $1$.
You are given $m$ queries of the following format:
query $l$ $r$ — how many distinct values is $x$ assigned to if all the instructions between the $l$-th one and the $r$-th one inclusive are ignored and the rest are executed without changing the order?
-----Input-----
The first line contains a single integer $t$ ($1 \le t \le 1000$) — the number of testcases.
Then the description of $t$ testcases follows.
The first line of each testcase contains two integers $n$ and $m$ ($1 \le n, m \le 2 \cdot 10^5$) — the number of instructions in the program and the number of queries.
The second line of each testcase contains a program — a string of $n$ characters: each character is either '+' or '-' — increment and decrement instruction, respectively.
Each of the next $m$ lines contains two integers $l$ and $r$ ($1 \le l \le r \le n$) — the description of the query.
The sum of $n$ over all testcases doesn't exceed $2 \cdot 10^5$. The sum of $m$ over all testcases doesn't exceed $2 \cdot 10^5$.
-----Output-----
For each testcase print $m$ integers — for each query $l$, $r$ print the number of distinct values variable $x$ is assigned to if all the instructions between the $l$-th one and the $r$-th one inclusive are ignored and the rest are executed without changing the order.
-----Examples-----
Input
2
8 4
-+--+--+
1 8
2 8
2 5
1 1
4 10
+-++
1 1
1 2
2 2
1 3
2 3
3 3
1 4
2 4
3 4
4 4
Output
1
2
4
4
3
3
4
2
3
2
1
2
2
2
-----Note-----
The instructions that remain for each query of the first testcase are:
empty program — $x$ was only equal to $0$;
"-" — $x$ had values $0$ and $-1$;
"---+" — $x$ had values $0$, $-1$, $-2$, $-3$, $-2$ — there are $4$ distinct values among them;
"+--+--+" — the distinct values are $1$, $0$, $-1$, $-2$. | import sys
input = sys.stdin.readline
def check(x, x1, y2):
if x >= x1 and x <= y2:
return True
else:
return False
def mini(l, r):
global n, data1, data2
mi = float("inf")
l += n
r += n
while l < r:
if l % 2 != 0:
mi = min(mi, data1[l])
l += 1
if r % 2 != 0:
r -= 1
mi = min(mi, data1[r])
l //= 2
r //= 2
return mi
def maxi(l, r):
global n, data1, data2
l += n
r += n
mi = -float("inf")
while l < r:
if l % 2 != 0:
mi = max(mi, data2[l])
l += 1
if r % 2 != 0:
r -= 1
mi = max(mi, data2[r])
l //= 2
r //= 2
return mi
for _ in range(int(input())):
n, m = map(int, input().split())
ar = list(input())
ar = ar[:-1]
x = 0
main = []
mif = [0]
maf = [0]
for i in range(n):
if ar[i] == "+":
x += 1
else:
x -= 1
main.append(x)
mif.append(min(mif[-1], x))
maf.append(max(maf[-1], x))
data1 = [0] * n + main.copy()
data2 = [0] * n + main.copy()
for i in range(n - 1, 0, -1):
data1[i] = min(data1[i * 2], data1[i * 2 + 1])
data2[i] = max(data2[i * 2], data2[i * 2 + 1])
su = [0] + main.copy()
for i in range(m):
l, r = map(int, input().split())
x1 = mif[l - 1]
y1 = maf[l - 1]
if r != n:
xx = su[r] - su[l - 1]
x2 = mini(r, n) - xx
y2 = maxi(r, n) - xx
else:
x2 = 0
y2 = 0
if (
check(x1, x2, y2)
or check(y1, x2, y2)
or check(x2, x1, y1)
or check(y2, x1, y1)
):
mii = min(x1, x2)
maa = max(y1, y2)
print(maa - mii + 1)
else:
print(y2 + y1 - (x1 + x2) + 2) | IMPORT ASSIGN VAR VAR FUNC_DEF IF VAR VAR VAR VAR RETURN NUMBER RETURN NUMBER FUNC_DEF ASSIGN VAR FUNC_CALL VAR STRING VAR VAR VAR VAR WHILE VAR VAR IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR NUMBER IF BIN_OP VAR NUMBER NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR NUMBER VAR NUMBER RETURN VAR FUNC_DEF VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR STRING WHILE VAR VAR IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR NUMBER IF BIN_OP VAR NUMBER NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR 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 FUNC_CALL VAR ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST ASSIGN VAR LIST NUMBER ASSIGN VAR LIST NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR STRING VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER VAR ASSIGN VAR BIN_OP BIN_OP LIST NUMBER VAR FUNC_CALL VAR ASSIGN VAR BIN_OP BIN_OP LIST NUMBER VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR BIN_OP BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR BIN_OP BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER IF VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR NUMBER |
You are given a program that consists of $n$ instructions. Initially a single variable $x$ is assigned to $0$. Afterwards, the instructions are of two types:
increase $x$ by $1$;
decrease $x$ by $1$.
You are given $m$ queries of the following format:
query $l$ $r$ — how many distinct values is $x$ assigned to if all the instructions between the $l$-th one and the $r$-th one inclusive are ignored and the rest are executed without changing the order?
-----Input-----
The first line contains a single integer $t$ ($1 \le t \le 1000$) — the number of testcases.
Then the description of $t$ testcases follows.
The first line of each testcase contains two integers $n$ and $m$ ($1 \le n, m \le 2 \cdot 10^5$) — the number of instructions in the program and the number of queries.
The second line of each testcase contains a program — a string of $n$ characters: each character is either '+' or '-' — increment and decrement instruction, respectively.
Each of the next $m$ lines contains two integers $l$ and $r$ ($1 \le l \le r \le n$) — the description of the query.
The sum of $n$ over all testcases doesn't exceed $2 \cdot 10^5$. The sum of $m$ over all testcases doesn't exceed $2 \cdot 10^5$.
-----Output-----
For each testcase print $m$ integers — for each query $l$, $r$ print the number of distinct values variable $x$ is assigned to if all the instructions between the $l$-th one and the $r$-th one inclusive are ignored and the rest are executed without changing the order.
-----Examples-----
Input
2
8 4
-+--+--+
1 8
2 8
2 5
1 1
4 10
+-++
1 1
1 2
2 2
1 3
2 3
3 3
1 4
2 4
3 4
4 4
Output
1
2
4
4
3
3
4
2
3
2
1
2
2
2
-----Note-----
The instructions that remain for each query of the first testcase are:
empty program — $x$ was only equal to $0$;
"-" — $x$ had values $0$ and $-1$;
"---+" — $x$ had values $0$, $-1$, $-2$, $-3$, $-2$ — there are $4$ distinct values among them;
"+--+--+" — the distinct values are $1$, $0$, $-1$, $-2$. | import sys
input = sys.stdin.readline
t = int(input())
for _ in range(t):
m, n = (int(i) for i in input().split(" "))
program = input()
minn, maxx, x = 0, 0, 0
front = []
for i in program:
if i == "+":
x += 1
else:
x -= 1
minn = min(minn, x)
maxx = max(maxx, x)
front.append((minn, maxx, x))
minn, maxx, x = 0, 0, 0
reverse = [0] * m
for i in range(m - 1, -1, -1):
if program[i] == "+":
x += 1
else:
x -= 1
minn = min(minn, x)
maxx = max(maxx, x)
actual_max = -(minn - x)
actual_min = -(maxx - x)
reverse[i] = actual_min, actual_max
for _ in range(n):
l, r = (int(i) - 1 for i in input().split(" "))
if l == 0 and r == m - 1:
print("1")
elif l == 0:
print(reverse[r + 1][1] - reverse[r + 1][0] + 1)
elif r == m - 1:
print(front[l - 1][1] - front[l - 1][0] + 1)
else:
fmin = front[l - 1][0]
fmax = front[l - 1][1]
rmin = reverse[r + 1][0] + front[l - 1][2]
rmax = reverse[r + 1][1] + front[l - 1][2]
print(max(fmax, rmax) - min(fmin, rmin) + 1) | IMPORT ASSIGN VAR VAR 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 STRING ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR VAR NUMBER NUMBER NUMBER ASSIGN VAR LIST FOR VAR VAR IF VAR STRING VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR VAR NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR VAR STRING VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR VAR VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR FUNC_CALL FUNC_CALL VAR STRING IF VAR NUMBER VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR STRING IF VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR NUMBER |
You are given a program that consists of $n$ instructions. Initially a single variable $x$ is assigned to $0$. Afterwards, the instructions are of two types:
increase $x$ by $1$;
decrease $x$ by $1$.
You are given $m$ queries of the following format:
query $l$ $r$ — how many distinct values is $x$ assigned to if all the instructions between the $l$-th one and the $r$-th one inclusive are ignored and the rest are executed without changing the order?
-----Input-----
The first line contains a single integer $t$ ($1 \le t \le 1000$) — the number of testcases.
Then the description of $t$ testcases follows.
The first line of each testcase contains two integers $n$ and $m$ ($1 \le n, m \le 2 \cdot 10^5$) — the number of instructions in the program and the number of queries.
The second line of each testcase contains a program — a string of $n$ characters: each character is either '+' or '-' — increment and decrement instruction, respectively.
Each of the next $m$ lines contains two integers $l$ and $r$ ($1 \le l \le r \le n$) — the description of the query.
The sum of $n$ over all testcases doesn't exceed $2 \cdot 10^5$. The sum of $m$ over all testcases doesn't exceed $2 \cdot 10^5$.
-----Output-----
For each testcase print $m$ integers — for each query $l$, $r$ print the number of distinct values variable $x$ is assigned to if all the instructions between the $l$-th one and the $r$-th one inclusive are ignored and the rest are executed without changing the order.
-----Examples-----
Input
2
8 4
-+--+--+
1 8
2 8
2 5
1 1
4 10
+-++
1 1
1 2
2 2
1 3
2 3
3 3
1 4
2 4
3 4
4 4
Output
1
2
4
4
3
3
4
2
3
2
1
2
2
2
-----Note-----
The instructions that remain for each query of the first testcase are:
empty program — $x$ was only equal to $0$;
"-" — $x$ had values $0$ and $-1$;
"---+" — $x$ had values $0$, $-1$, $-2$, $-3$, $-2$ — there are $4$ distinct values among them;
"+--+--+" — the distinct values are $1$, $0$, $-1$, $-2$. | import sys
input = sys.stdin.readline
for _ in range(int(input())):
n, m = map(int, input().split())
p = "$" + input()
x = 0
preSum = [0] * (n + 2)
prefixMin = [0] * (n + 2)
prefixMax = [0] * (n + 2)
suffixMin = [0] * (n + 2)
suffixMax = [0] * (n + 2)
for i in range(1, n + 1):
preSum[i] = preSum[i - 1] + int(p[i] + "1")
prefixMin[i] = min(prefixMin[i - 1], preSum[i])
prefixMax[i] = max(prefixMax[i - 1], preSum[i])
for i in range(n, 0, -1):
suffixMin[i] = min(suffixMin[i], suffixMin[i + 1] + int(p[i] + "1"))
suffixMax[i] = max(suffixMax[i], suffixMax[i + 1] + int(p[i] + "1"))
for q in range(m):
l, r = map(int, input().split())
a = max(prefixMax[l - 1], suffixMax[r + 1] + preSum[l - 1])
b = min(prefixMin[l - 1], suffixMin[r + 1] + preSum[l - 1])
ans = a - b + 1
print(ans) | IMPORT ASSIGN VAR 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 BIN_OP STRING FUNC_CALL VAR ASSIGN 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 BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER FUNC_CALL VAR BIN_OP VAR VAR STRING ASSIGN VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR FOR VAR FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR BIN_OP VAR NUMBER FUNC_CALL VAR BIN_OP VAR VAR STRING ASSIGN VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR BIN_OP VAR NUMBER FUNC_CALL VAR BIN_OP VAR VAR STRING FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR |
You are given a program that consists of $n$ instructions. Initially a single variable $x$ is assigned to $0$. Afterwards, the instructions are of two types:
increase $x$ by $1$;
decrease $x$ by $1$.
You are given $m$ queries of the following format:
query $l$ $r$ — how many distinct values is $x$ assigned to if all the instructions between the $l$-th one and the $r$-th one inclusive are ignored and the rest are executed without changing the order?
-----Input-----
The first line contains a single integer $t$ ($1 \le t \le 1000$) — the number of testcases.
Then the description of $t$ testcases follows.
The first line of each testcase contains two integers $n$ and $m$ ($1 \le n, m \le 2 \cdot 10^5$) — the number of instructions in the program and the number of queries.
The second line of each testcase contains a program — a string of $n$ characters: each character is either '+' or '-' — increment and decrement instruction, respectively.
Each of the next $m$ lines contains two integers $l$ and $r$ ($1 \le l \le r \le n$) — the description of the query.
The sum of $n$ over all testcases doesn't exceed $2 \cdot 10^5$. The sum of $m$ over all testcases doesn't exceed $2 \cdot 10^5$.
-----Output-----
For each testcase print $m$ integers — for each query $l$, $r$ print the number of distinct values variable $x$ is assigned to if all the instructions between the $l$-th one and the $r$-th one inclusive are ignored and the rest are executed without changing the order.
-----Examples-----
Input
2
8 4
-+--+--+
1 8
2 8
2 5
1 1
4 10
+-++
1 1
1 2
2 2
1 3
2 3
3 3
1 4
2 4
3 4
4 4
Output
1
2
4
4
3
3
4
2
3
2
1
2
2
2
-----Note-----
The instructions that remain for each query of the first testcase are:
empty program — $x$ was only equal to $0$;
"-" — $x$ had values $0$ and $-1$;
"---+" — $x$ had values $0$, $-1$, $-2$, $-3$, $-2$ — there are $4$ distinct values among them;
"+--+--+" — the distinct values are $1$, $0$, $-1$, $-2$. | for _ in range(int(input())):
n, m = map(int, input().split())
s = list(map(int, input().replace("+", "1,").replace("-", "-1,").split(",")[:-1]))
interval = [list(map(int, input().split())) for _ in range(m)]
prefix_min, prefix_max, surffix_min, surffix_max = [0], [0], [0], [0]
value = [0]
for i in range(n):
value.append(s[i] + value[-1])
prefix_min.append(min(value[-1], prefix_min[-1]))
prefix_max.append(max(value[-1], prefix_max[-1]))
surffix_min.append(min(0, s[n - i - 1] + surffix_min[-1]))
surffix_max.append(max(0, s[n - i - 1] + surffix_max[-1]))
surffix_max = surffix_max[::-1]
surffix_min = surffix_min[::-1]
for l, r in interval:
l -= 1
l1 = prefix_min[l]
r1 = prefix_max[l]
l2 = surffix_min[r] + value[l]
r2 = surffix_max[r] + value[l]
print(max(r1, r2) - min(l1, l2) + 1) | 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 FUNC_CALL FUNC_CALL VAR STRING STRING STRING STRING STRING NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR LIST NUMBER LIST NUMBER LIST NUMBER LIST NUMBER ASSIGN VAR LIST NUMBER FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR NUMBER BIN_OP VAR BIN_OP BIN_OP VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR NUMBER BIN_OP VAR BIN_OP BIN_OP VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR NUMBER |
You are given a program that consists of $n$ instructions. Initially a single variable $x$ is assigned to $0$. Afterwards, the instructions are of two types:
increase $x$ by $1$;
decrease $x$ by $1$.
You are given $m$ queries of the following format:
query $l$ $r$ — how many distinct values is $x$ assigned to if all the instructions between the $l$-th one and the $r$-th one inclusive are ignored and the rest are executed without changing the order?
-----Input-----
The first line contains a single integer $t$ ($1 \le t \le 1000$) — the number of testcases.
Then the description of $t$ testcases follows.
The first line of each testcase contains two integers $n$ and $m$ ($1 \le n, m \le 2 \cdot 10^5$) — the number of instructions in the program and the number of queries.
The second line of each testcase contains a program — a string of $n$ characters: each character is either '+' or '-' — increment and decrement instruction, respectively.
Each of the next $m$ lines contains two integers $l$ and $r$ ($1 \le l \le r \le n$) — the description of the query.
The sum of $n$ over all testcases doesn't exceed $2 \cdot 10^5$. The sum of $m$ over all testcases doesn't exceed $2 \cdot 10^5$.
-----Output-----
For each testcase print $m$ integers — for each query $l$, $r$ print the number of distinct values variable $x$ is assigned to if all the instructions between the $l$-th one and the $r$-th one inclusive are ignored and the rest are executed without changing the order.
-----Examples-----
Input
2
8 4
-+--+--+
1 8
2 8
2 5
1 1
4 10
+-++
1 1
1 2
2 2
1 3
2 3
3 3
1 4
2 4
3 4
4 4
Output
1
2
4
4
3
3
4
2
3
2
1
2
2
2
-----Note-----
The instructions that remain for each query of the first testcase are:
empty program — $x$ was only equal to $0$;
"-" — $x$ had values $0$ and $-1$;
"---+" — $x$ had values $0$, $-1$, $-2$, $-3$, $-2$ — there are $4$ distinct values among them;
"+--+--+" — the distinct values are $1$, $0$, $-1$, $-2$. | from sys import stdin
t = int(stdin.readline())
for _ in range(t):
n, m = map(int, stdin.readline().split())
arr = [0]
arr2 = [[0, 0]]
so = 0
maxx = 0
minn = 0
strg = stdin.readline()
for i in strg:
if i == "+":
so += 1
maxx = max(so, maxx)
arr.append(so)
arr2.append([minn, maxx])
elif i == "-":
so -= 1
minn = min(so, minn)
arr.append(so)
arr2.append([minn, maxx])
arr3 = []
so2 = 0
minn2 = int(so)
maxx2 = int(so)
for i in range(-1, -n - 1, -1):
minn2 = min(minn2, arr[i])
maxx2 = max(maxx2, arr[i])
arr3.append([minn2 - arr[i], maxx2 - arr[i]])
for j in range(m):
quer1, quer2 = map(int, stdin.readline().split())
range1 = arr2[quer1 - 1]
range2 = arr3[-quer2]
print(
max(range1[1], arr[quer1 - 1] + range2[1])
- min(range1[0], arr[quer1 - 1] + range2[0])
+ 1
) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST NUMBER ASSIGN VAR LIST LIST NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR FOR VAR VAR IF VAR STRING VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR LIST VAR VAR IF VAR STRING VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR LIST VAR VAR ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR LIST BIN_OP VAR VAR VAR BIN_OP VAR VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP FUNC_CALL VAR VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER VAR NUMBER FUNC_CALL VAR VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER VAR NUMBER NUMBER |
You are given a program that consists of $n$ instructions. Initially a single variable $x$ is assigned to $0$. Afterwards, the instructions are of two types:
increase $x$ by $1$;
decrease $x$ by $1$.
You are given $m$ queries of the following format:
query $l$ $r$ — how many distinct values is $x$ assigned to if all the instructions between the $l$-th one and the $r$-th one inclusive are ignored and the rest are executed without changing the order?
-----Input-----
The first line contains a single integer $t$ ($1 \le t \le 1000$) — the number of testcases.
Then the description of $t$ testcases follows.
The first line of each testcase contains two integers $n$ and $m$ ($1 \le n, m \le 2 \cdot 10^5$) — the number of instructions in the program and the number of queries.
The second line of each testcase contains a program — a string of $n$ characters: each character is either '+' or '-' — increment and decrement instruction, respectively.
Each of the next $m$ lines contains two integers $l$ and $r$ ($1 \le l \le r \le n$) — the description of the query.
The sum of $n$ over all testcases doesn't exceed $2 \cdot 10^5$. The sum of $m$ over all testcases doesn't exceed $2 \cdot 10^5$.
-----Output-----
For each testcase print $m$ integers — for each query $l$, $r$ print the number of distinct values variable $x$ is assigned to if all the instructions between the $l$-th one and the $r$-th one inclusive are ignored and the rest are executed without changing the order.
-----Examples-----
Input
2
8 4
-+--+--+
1 8
2 8
2 5
1 1
4 10
+-++
1 1
1 2
2 2
1 3
2 3
3 3
1 4
2 4
3 4
4 4
Output
1
2
4
4
3
3
4
2
3
2
1
2
2
2
-----Note-----
The instructions that remain for each query of the first testcase are:
empty program — $x$ was only equal to $0$;
"-" — $x$ had values $0$ and $-1$;
"---+" — $x$ had values $0$, $-1$, $-2$, $-3$, $-2$ — there are $4$ distinct values among them;
"+--+--+" — the distinct values are $1$, $0$, $-1$, $-2$. | import sys
input = sys.stdin.readline
t = int(input())
for _ in range(t):
n, m = map(int, input().split())
s = input()
pr = [0]
prmin = [0]
prmax = [0]
for i in range(n):
if s[i] == "+":
d = 1
else:
d = -1
pr.append(pr[-1] + d)
prmin.append(min(pr[-1], prmin[-1]))
prmax.append(max(pr[-1], prmax[-1]))
sumin = [0]
sumax = [0]
for i in range(n - 1, -1, -1):
if s[i] == "+":
d = 1
else:
d = -1
sumin.append(min(0, sumin[-1] + d))
sumax.append(max(0, sumax[-1] + d))
sumin.reverse()
sumax.reverse()
for q in range(m):
l, r = map(int, input().split())
l -= 1
lmin, rmin, lmax, rmax = prmin[l], sumin[r] + pr[l], prmax[l], sumax[r] + pr[l]
print(max(lmax, rmax) - min(lmin, rmin) + 1) | IMPORT ASSIGN VAR 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 ASSIGN VAR LIST NUMBER ASSIGN VAR LIST NUMBER ASSIGN VAR LIST NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR STRING ASSIGN VAR NUMBER ASSIGN VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER VAR NUMBER ASSIGN VAR LIST NUMBER ASSIGN VAR LIST NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR VAR STRING ASSIGN VAR NUMBER ASSIGN VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR NUMBER |
You are given a program that consists of $n$ instructions. Initially a single variable $x$ is assigned to $0$. Afterwards, the instructions are of two types:
increase $x$ by $1$;
decrease $x$ by $1$.
You are given $m$ queries of the following format:
query $l$ $r$ — how many distinct values is $x$ assigned to if all the instructions between the $l$-th one and the $r$-th one inclusive are ignored and the rest are executed without changing the order?
-----Input-----
The first line contains a single integer $t$ ($1 \le t \le 1000$) — the number of testcases.
Then the description of $t$ testcases follows.
The first line of each testcase contains two integers $n$ and $m$ ($1 \le n, m \le 2 \cdot 10^5$) — the number of instructions in the program and the number of queries.
The second line of each testcase contains a program — a string of $n$ characters: each character is either '+' or '-' — increment and decrement instruction, respectively.
Each of the next $m$ lines contains two integers $l$ and $r$ ($1 \le l \le r \le n$) — the description of the query.
The sum of $n$ over all testcases doesn't exceed $2 \cdot 10^5$. The sum of $m$ over all testcases doesn't exceed $2 \cdot 10^5$.
-----Output-----
For each testcase print $m$ integers — for each query $l$, $r$ print the number of distinct values variable $x$ is assigned to if all the instructions between the $l$-th one and the $r$-th one inclusive are ignored and the rest are executed without changing the order.
-----Examples-----
Input
2
8 4
-+--+--+
1 8
2 8
2 5
1 1
4 10
+-++
1 1
1 2
2 2
1 3
2 3
3 3
1 4
2 4
3 4
4 4
Output
1
2
4
4
3
3
4
2
3
2
1
2
2
2
-----Note-----
The instructions that remain for each query of the first testcase are:
empty program — $x$ was only equal to $0$;
"-" — $x$ had values $0$ and $-1$;
"---+" — $x$ had values $0$, $-1$, $-2$, $-3$, $-2$ — there are $4$ distinct values among them;
"+--+--+" — the distinct values are $1$, $0$, $-1$, $-2$. | import sys
for iter in range(int(sys.stdin.readline())):
n, m = map(int, sys.stdin.readline().split())
s = input()
balance = [0] * (n + 1)
for i in range(n):
if s[i] == "+":
balance[i + 1] = balance[i] + 1
else:
balance[i + 1] = balance[i] - 1
max_prefix, min_prefix = [0] * (n + 1), [0] * (n + 1)
max_suffix, min_suffix = [balance[-1]] * (n + 1), [balance[-1]] * (n + 1)
for i in range(n):
max_prefix[i + 1] = max(max_prefix[i], balance[i + 1])
min_prefix[i + 1] = min(min_prefix[i], balance[i + 1])
for i in range(n - 1, -1, -1):
max_suffix[i] = max(balance[i], max_suffix[i + 1])
min_suffix[i] = min(balance[i], min_suffix[i + 1])
for i in range(m):
l, r = map(int, sys.stdin.readline().split())
max_avaliable = max(
max_prefix[l - 1], balance[l - 1] + max_suffix[r] - balance[r]
)
min_avaliable = min(
min_prefix[l - 1], balance[l - 1] + min_suffix[r] - balance[r]
)
sys.stdout.write(str(max_avaliable - min_avaliable + 1) + "\n") | IMPORT 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 ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR STRING ASSIGN VAR BIN_OP VAR NUMBER BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER BIN_OP VAR VAR NUMBER ASSIGN VAR VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP LIST VAR NUMBER BIN_OP VAR NUMBER BIN_OP LIST VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR 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 ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER STRING |
You are given a program that consists of $n$ instructions. Initially a single variable $x$ is assigned to $0$. Afterwards, the instructions are of two types:
increase $x$ by $1$;
decrease $x$ by $1$.
You are given $m$ queries of the following format:
query $l$ $r$ — how many distinct values is $x$ assigned to if all the instructions between the $l$-th one and the $r$-th one inclusive are ignored and the rest are executed without changing the order?
-----Input-----
The first line contains a single integer $t$ ($1 \le t \le 1000$) — the number of testcases.
Then the description of $t$ testcases follows.
The first line of each testcase contains two integers $n$ and $m$ ($1 \le n, m \le 2 \cdot 10^5$) — the number of instructions in the program and the number of queries.
The second line of each testcase contains a program — a string of $n$ characters: each character is either '+' or '-' — increment and decrement instruction, respectively.
Each of the next $m$ lines contains two integers $l$ and $r$ ($1 \le l \le r \le n$) — the description of the query.
The sum of $n$ over all testcases doesn't exceed $2 \cdot 10^5$. The sum of $m$ over all testcases doesn't exceed $2 \cdot 10^5$.
-----Output-----
For each testcase print $m$ integers — for each query $l$, $r$ print the number of distinct values variable $x$ is assigned to if all the instructions between the $l$-th one and the $r$-th one inclusive are ignored and the rest are executed without changing the order.
-----Examples-----
Input
2
8 4
-+--+--+
1 8
2 8
2 5
1 1
4 10
+-++
1 1
1 2
2 2
1 3
2 3
3 3
1 4
2 4
3 4
4 4
Output
1
2
4
4
3
3
4
2
3
2
1
2
2
2
-----Note-----
The instructions that remain for each query of the first testcase are:
empty program — $x$ was only equal to $0$;
"-" — $x$ had values $0$ and $-1$;
"---+" — $x$ had values $0$, $-1$, $-2$, $-3$, $-2$ — there are $4$ distinct values among them;
"+--+--+" — the distinct values are $1$, $0$, $-1$, $-2$. | import sys
input = sys.stdin.readline
def segfunc(x, y):
return min(x, y)
def segfunc2(x, y):
return max(x, y)
def segfunc3(x, y):
return x + y
ide_ele = 10000000000
ide_ele2 = -10000000000
ide_ele3 = 0
class SegTree:
def __init__(self, init_val, segfunc, ide_ele):
n = len(init_val)
self.segfunc = segfunc
self.ide_ele = ide_ele
self.num = 1 << (n - 1).bit_length()
self.tree = [ide_ele] * 2 * self.num
for i in range(n):
self.tree[self.num + i] = init_val[i]
for i in range(self.num - 1, 0, -1):
self.tree[i] = self.segfunc(self.tree[2 * i], self.tree[2 * i + 1])
def update(self, k, x):
k += self.num
self.tree[k] = x
while k > 1:
self.tree[k >> 1] = self.segfunc(self.tree[k], self.tree[k ^ 1])
k >>= 1
def query(self, l, r):
res = self.ide_ele
l += self.num
r += self.num
while l < r:
if l & 1:
res = self.segfunc(res, self.tree[l])
l += 1
if r & 1:
res = self.segfunc(res, self.tree[r - 1])
l >>= 1
r >>= 1
return res
t = int(input())
for f in range(t):
n, m = map(int, input().split())
s = input().rstrip()
query = []
for i in range(m):
q = list(map(int, input().split()))
q[1] += 1
query.append(q)
cum = [0]
check = [0]
for i in range(n):
if s[i] == "-":
cum.append(cum[-1] - 1)
check.append(-1)
else:
cum.append(cum[-1] + 1)
check.append(1)
seg_min = SegTree(cum, segfunc, ide_ele)
seg_max = SegTree(cum, segfunc2, ide_ele2)
seg_sum = SegTree(check, segfunc3, ide_ele3)
for i in query:
ans = 0
a = seg_min.query(0, i[0])
b = seg_max.query(0, i[0])
e = seg_sum.query(i[0], i[1])
if i[1] != n + 1:
c = seg_min.query(i[1], n + 1) - e
d = seg_max.query(i[1], n + 1) - e
ans += max(b, d) - min(a, c) + 1
else:
ans += abs(b - a) + 1
print(ans) | IMPORT ASSIGN VAR VAR FUNC_DEF RETURN FUNC_CALL VAR VAR VAR FUNC_DEF RETURN FUNC_CALL VAR VAR VAR FUNC_DEF RETURN BIN_OP VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP NUMBER FUNC_CALL BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP LIST VAR NUMBER VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR BIN_OP NUMBER VAR VAR BIN_OP BIN_OP NUMBER VAR NUMBER FUNC_DEF VAR VAR ASSIGN VAR VAR VAR WHILE VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER FUNC_DEF ASSIGN VAR VAR VAR VAR VAR VAR WHILE VAR VAR IF BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR NUMBER IF BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER 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 FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR LIST NUMBER ASSIGN VAR LIST NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR STRING EXPR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR FOR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER VAR NUMBER IF VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER BIN_OP VAR NUMBER VAR VAR BIN_OP BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR NUMBER VAR BIN_OP FUNC_CALL VAR BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR |
You are given a program that consists of $n$ instructions. Initially a single variable $x$ is assigned to $0$. Afterwards, the instructions are of two types:
increase $x$ by $1$;
decrease $x$ by $1$.
You are given $m$ queries of the following format:
query $l$ $r$ — how many distinct values is $x$ assigned to if all the instructions between the $l$-th one and the $r$-th one inclusive are ignored and the rest are executed without changing the order?
-----Input-----
The first line contains a single integer $t$ ($1 \le t \le 1000$) — the number of testcases.
Then the description of $t$ testcases follows.
The first line of each testcase contains two integers $n$ and $m$ ($1 \le n, m \le 2 \cdot 10^5$) — the number of instructions in the program and the number of queries.
The second line of each testcase contains a program — a string of $n$ characters: each character is either '+' or '-' — increment and decrement instruction, respectively.
Each of the next $m$ lines contains two integers $l$ and $r$ ($1 \le l \le r \le n$) — the description of the query.
The sum of $n$ over all testcases doesn't exceed $2 \cdot 10^5$. The sum of $m$ over all testcases doesn't exceed $2 \cdot 10^5$.
-----Output-----
For each testcase print $m$ integers — for each query $l$, $r$ print the number of distinct values variable $x$ is assigned to if all the instructions between the $l$-th one and the $r$-th one inclusive are ignored and the rest are executed without changing the order.
-----Examples-----
Input
2
8 4
-+--+--+
1 8
2 8
2 5
1 1
4 10
+-++
1 1
1 2
2 2
1 3
2 3
3 3
1 4
2 4
3 4
4 4
Output
1
2
4
4
3
3
4
2
3
2
1
2
2
2
-----Note-----
The instructions that remain for each query of the first testcase are:
empty program — $x$ was only equal to $0$;
"-" — $x$ had values $0$ and $-1$;
"---+" — $x$ had values $0$, $-1$, $-2$, $-3$, $-2$ — there are $4$ distinct values among them;
"+--+--+" — the distinct values are $1$, $0$, $-1$, $-2$. | import sys
def main():
t = int(input())
allAns = []
for _ in range(t):
n, m = readIntArr()
s = input()
prefixMin = [inf for __ in range(n)]
prefixMax = [(-inf) for __ in range(n)]
suffixMin = [inf for __ in range(n)]
suffixMax = [(-inf) for __ in range(n)]
valueAt = [(0) for __ in range(n)]
curr = 0
currMin = 0
currMax = 0
for i, c in enumerate(s):
if c == "+":
curr += 1
else:
curr -= 1
currMin = min(curr, currMin)
currMax = max(curr, currMax)
prefixMin[i] = currMin
prefixMax[i] = currMax
valueAt[i] = curr
curr = valueAt[n - 1]
currMin = curr
currMax = curr
for i in range(n - 1, -1, -1):
suffixMin[i] = currMin
suffixMax[i] = currMax
c = s[i]
if c == "+":
curr -= 1
else:
curr += 1
currMin = min(curr, currMin)
currMax = max(curr, currMax)
for __ in range(m):
l, r = readIntArr()
l -= 1
r -= 1
if l == 0:
preMin = 0
preMax = 0
valuePrev = 0
else:
preMin = prefixMin[l - 1]
preMax = prefixMax[l - 1]
valuePrev = valueAt[l - 1]
valueAfter = valueAt[r]
if r == n - 1:
sufMin = valueAt[n - 1]
sufMax = valueAt[n - 1]
else:
sufMin = suffixMin[r + 1]
sufMax = suffixMax[r + 1]
delta = valueAfter - valuePrev
minn = min(preMin, sufMin - delta)
maxx = max(preMax, sufMax - delta)
allAns.append(maxx - minn + 1)
multiLineArrayPrint(allAns)
return
input = lambda: sys.stdin.readline().rstrip("\r\n")
def oneLineArrayPrint(arr):
print(" ".join([str(x) for x in arr]))
def multiLineArrayPrint(arr):
print("\n".join([str(x) for x in arr]))
def multiLineArrayOfArraysPrint(arr):
print("\n".join([" ".join([str(x) for x in y]) for y in arr]))
def readIntArr():
return [int(x) for x in input().split()]
inf = float("inf")
MOD = 10**9 + 7
main() | IMPORT FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR IF VAR STRING VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR IF VAR STRING VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR NUMBER VAR NUMBER IF VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR IF VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR RETURN ASSIGN VAR FUNC_CALL FUNC_CALL VAR STRING FUNC_DEF EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR VAR FUNC_DEF EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR VAR FUNC_DEF EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL STRING FUNC_CALL VAR VAR VAR VAR VAR VAR FUNC_DEF RETURN FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR |
You are given a program that consists of $n$ instructions. Initially a single variable $x$ is assigned to $0$. Afterwards, the instructions are of two types:
increase $x$ by $1$;
decrease $x$ by $1$.
You are given $m$ queries of the following format:
query $l$ $r$ — how many distinct values is $x$ assigned to if all the instructions between the $l$-th one and the $r$-th one inclusive are ignored and the rest are executed without changing the order?
-----Input-----
The first line contains a single integer $t$ ($1 \le t \le 1000$) — the number of testcases.
Then the description of $t$ testcases follows.
The first line of each testcase contains two integers $n$ and $m$ ($1 \le n, m \le 2 \cdot 10^5$) — the number of instructions in the program and the number of queries.
The second line of each testcase contains a program — a string of $n$ characters: each character is either '+' or '-' — increment and decrement instruction, respectively.
Each of the next $m$ lines contains two integers $l$ and $r$ ($1 \le l \le r \le n$) — the description of the query.
The sum of $n$ over all testcases doesn't exceed $2 \cdot 10^5$. The sum of $m$ over all testcases doesn't exceed $2 \cdot 10^5$.
-----Output-----
For each testcase print $m$ integers — for each query $l$, $r$ print the number of distinct values variable $x$ is assigned to if all the instructions between the $l$-th one and the $r$-th one inclusive are ignored and the rest are executed without changing the order.
-----Examples-----
Input
2
8 4
-+--+--+
1 8
2 8
2 5
1 1
4 10
+-++
1 1
1 2
2 2
1 3
2 3
3 3
1 4
2 4
3 4
4 4
Output
1
2
4
4
3
3
4
2
3
2
1
2
2
2
-----Note-----
The instructions that remain for each query of the first testcase are:
empty program — $x$ was only equal to $0$;
"-" — $x$ had values $0$ and $-1$;
"---+" — $x$ had values $0$, $-1$, $-2$, $-3$, $-2$ — there are $4$ distinct values among them;
"+--+--+" — the distinct values are $1$, $0$, $-1$, $-2$. | import sys
input = lambda: sys.stdin.readline().rstrip()
for _ in range(int(input())):
n, m = map(int, input().split())
s = input()
a = [0] * (n + 1)
mx, mn = [0] * (n + 1), [0] * (n + 1)
for i, ch in enumerate(s, 1):
a[i] = a[i - 1]
if ch == "+":
a[i] += 1
else:
a[i] -= 1
mx[i] = max(mx[i - 1], a[i])
mn[i] = min(mn[i - 1], a[i])
mx1, mn1 = [0] * (n + 1), [0] * (n + 1)
mx1[-1] = mn1[-1] = a[-1]
for i in range(n - 1, 0, -1):
mx1[i] = max(mx1[i + 1], a[i])
mn1[i] = min(mn1[i + 1], a[i])
for _ in range(m):
l, r = map(int, input().split())
sm = a[r] - a[l - 1]
MX, MN = mx[l - 1], mn[l - 1]
if r + 1 <= n:
MX = max(MX, mx1[r + 1] - sm)
MN = min(MN, mn1[r + 1] - sm)
print(MX - MN + 1) | IMPORT ASSIGN VAR FUNC_CALL FUNC_CALL 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 ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER BIN_OP LIST NUMBER BIN_OP VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER IF VAR STRING VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER 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 VAR FUNC_CALL 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 VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER IF BIN_OP VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER |
You are given a program that consists of $n$ instructions. Initially a single variable $x$ is assigned to $0$. Afterwards, the instructions are of two types:
increase $x$ by $1$;
decrease $x$ by $1$.
You are given $m$ queries of the following format:
query $l$ $r$ — how many distinct values is $x$ assigned to if all the instructions between the $l$-th one and the $r$-th one inclusive are ignored and the rest are executed without changing the order?
-----Input-----
The first line contains a single integer $t$ ($1 \le t \le 1000$) — the number of testcases.
Then the description of $t$ testcases follows.
The first line of each testcase contains two integers $n$ and $m$ ($1 \le n, m \le 2 \cdot 10^5$) — the number of instructions in the program and the number of queries.
The second line of each testcase contains a program — a string of $n$ characters: each character is either '+' or '-' — increment and decrement instruction, respectively.
Each of the next $m$ lines contains two integers $l$ and $r$ ($1 \le l \le r \le n$) — the description of the query.
The sum of $n$ over all testcases doesn't exceed $2 \cdot 10^5$. The sum of $m$ over all testcases doesn't exceed $2 \cdot 10^5$.
-----Output-----
For each testcase print $m$ integers — for each query $l$, $r$ print the number of distinct values variable $x$ is assigned to if all the instructions between the $l$-th one and the $r$-th one inclusive are ignored and the rest are executed without changing the order.
-----Examples-----
Input
2
8 4
-+--+--+
1 8
2 8
2 5
1 1
4 10
+-++
1 1
1 2
2 2
1 3
2 3
3 3
1 4
2 4
3 4
4 4
Output
1
2
4
4
3
3
4
2
3
2
1
2
2
2
-----Note-----
The instructions that remain for each query of the first testcase are:
empty program — $x$ was only equal to $0$;
"-" — $x$ had values $0$ and $-1$;
"---+" — $x$ had values $0$, $-1$, $-2$, $-3$, $-2$ — there are $4$ distinct values among them;
"+--+--+" — the distinct values are $1$, $0$, $-1$, $-2$. | import sys
input = sys.stdin.readline
for _ in range(int(input())):
n, m = map(int, input().split())
s = "#" + input().strip()
pf = [0] * (n + 2)
mnpf = [0] * (n + 2)
mxpf = [0] * (n + 2)
mnsff = [0] * (n + 2)
mxsff = [0] * (n + 2)
for i in range(1, n + 1):
pf[i] = pf[i - 1] + (s[i] == "+")
pf[i] -= s[i] == "-"
mnpf[i] = min(pf[i], mnpf[i - 1])
mxpf[i] = max(pf[i], mxpf[i - 1])
cur = 0
lstmn = 0
lstmx = 0
for i in range(n - 1, 0, -1):
cur -= s[i + 1] == "+"
cur += s[i + 1] == "-"
lstmn = min(cur, lstmn)
lstmx = max(cur, lstmx)
mnsff[i] = lstmn - cur
mxsff[i] = lstmx - cur
for qe in range(m):
l, r = map(int, input().split())
low = high = 0
if l == 1 and r == n:
print(1)
continue
if l == 1:
print(mxsff[r] - mnsff[r] + 1)
continue
if r == n:
print(mxpf[l - 1] - mnpf[l - 1] + 1)
continue
high = max(mxpf[l - 1], pf[l - 1] + mxsff[r])
low = min(mnpf[l - 1], pf[l - 1] + mnsff[r])
print(high - low + 1) | IMPORT ASSIGN VAR 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 BIN_OP STRING 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 BIN_OP LIST NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR STRING VAR VAR VAR VAR STRING ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER VAR VAR BIN_OP VAR NUMBER STRING VAR VAR BIN_OP VAR NUMBER STRING ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR NUMBER IF VAR NUMBER VAR VAR EXPR FUNC_CALL VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR VAR VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER |
You are given a program that consists of $n$ instructions. Initially a single variable $x$ is assigned to $0$. Afterwards, the instructions are of two types:
increase $x$ by $1$;
decrease $x$ by $1$.
You are given $m$ queries of the following format:
query $l$ $r$ — how many distinct values is $x$ assigned to if all the instructions between the $l$-th one and the $r$-th one inclusive are ignored and the rest are executed without changing the order?
-----Input-----
The first line contains a single integer $t$ ($1 \le t \le 1000$) — the number of testcases.
Then the description of $t$ testcases follows.
The first line of each testcase contains two integers $n$ and $m$ ($1 \le n, m \le 2 \cdot 10^5$) — the number of instructions in the program and the number of queries.
The second line of each testcase contains a program — a string of $n$ characters: each character is either '+' or '-' — increment and decrement instruction, respectively.
Each of the next $m$ lines contains two integers $l$ and $r$ ($1 \le l \le r \le n$) — the description of the query.
The sum of $n$ over all testcases doesn't exceed $2 \cdot 10^5$. The sum of $m$ over all testcases doesn't exceed $2 \cdot 10^5$.
-----Output-----
For each testcase print $m$ integers — for each query $l$, $r$ print the number of distinct values variable $x$ is assigned to if all the instructions between the $l$-th one and the $r$-th one inclusive are ignored and the rest are executed without changing the order.
-----Examples-----
Input
2
8 4
-+--+--+
1 8
2 8
2 5
1 1
4 10
+-++
1 1
1 2
2 2
1 3
2 3
3 3
1 4
2 4
3 4
4 4
Output
1
2
4
4
3
3
4
2
3
2
1
2
2
2
-----Note-----
The instructions that remain for each query of the first testcase are:
empty program — $x$ was only equal to $0$;
"-" — $x$ had values $0$ and $-1$;
"---+" — $x$ had values $0$, $-1$, $-2$, $-3$, $-2$ — there are $4$ distinct values among them;
"+--+--+" — the distinct values are $1$, $0$, $-1$, $-2$. | import sys
max_int = 2147483648
min_int = -max_int
t = int(input())
for _t in range(t):
n, m = map(int, sys.stdin.readline().split())
code = input()
lv = [0] * (n + 1)
lmax = [0] * (n + 1)
lmin = [0] * (n + 1)
rv = [0] * (n + 1)
rmax = [0] * (n + 1)
rmin = [0] * (n + 1)
for i, s in enumerate(code, 1):
lv[i] = lv[i - 1] + (1 if s == "+" else -1)
lmax[i] = max(lv[i], lmax[i - 1])
lmin[i] = min(lv[i], lmin[i - 1])
for i in range(n - 1, -1, -1):
s = code[i]
rv[i] = rv[i + 1] + (1 if s == "-" else -1)
rmax[i] = max(rmax[i + 1], rv[i])
rmin[i] = min(rmin[i + 1], rv[i])
out = [""] * m
for mm in range(m):
l, r = map(int, sys.stdin.readline().split())
lmn = lmin[l - 1] - lv[l - 1]
lmx = lmax[l - 1] - lv[l - 1]
rmn = rmin[r] - rv[r]
rmx = rmax[r] - rv[r]
out[mm] = str(max(rmx, lmx) - min(rmn, lmn) + 1)
print("\n".join(out)) | IMPORT ASSIGN VAR NUMBER ASSIGN VAR 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 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 BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR STRING NUMBER NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR STRING NUMBER NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR BIN_OP LIST STRING 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 ASSIGN VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR BIN_OP BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL STRING VAR |
You are given a program that consists of $n$ instructions. Initially a single variable $x$ is assigned to $0$. Afterwards, the instructions are of two types:
increase $x$ by $1$;
decrease $x$ by $1$.
You are given $m$ queries of the following format:
query $l$ $r$ — how many distinct values is $x$ assigned to if all the instructions between the $l$-th one and the $r$-th one inclusive are ignored and the rest are executed without changing the order?
-----Input-----
The first line contains a single integer $t$ ($1 \le t \le 1000$) — the number of testcases.
Then the description of $t$ testcases follows.
The first line of each testcase contains two integers $n$ and $m$ ($1 \le n, m \le 2 \cdot 10^5$) — the number of instructions in the program and the number of queries.
The second line of each testcase contains a program — a string of $n$ characters: each character is either '+' or '-' — increment and decrement instruction, respectively.
Each of the next $m$ lines contains two integers $l$ and $r$ ($1 \le l \le r \le n$) — the description of the query.
The sum of $n$ over all testcases doesn't exceed $2 \cdot 10^5$. The sum of $m$ over all testcases doesn't exceed $2 \cdot 10^5$.
-----Output-----
For each testcase print $m$ integers — for each query $l$, $r$ print the number of distinct values variable $x$ is assigned to if all the instructions between the $l$-th one and the $r$-th one inclusive are ignored and the rest are executed without changing the order.
-----Examples-----
Input
2
8 4
-+--+--+
1 8
2 8
2 5
1 1
4 10
+-++
1 1
1 2
2 2
1 3
2 3
3 3
1 4
2 4
3 4
4 4
Output
1
2
4
4
3
3
4
2
3
2
1
2
2
2
-----Note-----
The instructions that remain for each query of the first testcase are:
empty program — $x$ was only equal to $0$;
"-" — $x$ had values $0$ and $-1$;
"---+" — $x$ had values $0$, $-1$, $-2$, $-3$, $-2$ — there are $4$ distinct values among them;
"+--+--+" — the distinct values are $1$, $0$, $-1$, $-2$. | import sys
def lr():
lmin[0] = lmax[0] = lv[0] = v[0]
for i in range(1, n):
lv[i] = lv[i - 1] + v[i]
lmin[i] = min(lmin[i - 1], lv[i])
lmax[i] = max(lmax[i - 1], lv[i])
def rl():
rmin[n - 1] = rmax[n - 1] = v[n - 1]
for i in range(n - 2, -1, -1):
rmin[i] = rmin[i + 1] + v[i]
rmax[i] = rmax[i + 1] + v[i]
rmin[i] = min(rmin[i], v[i])
rmax[i] = max(rmax[i], v[i])
def calc(x1, y1, x2, y2, cur):
x2 = cur + x2
y2 = cur + y2
add = 0
if min(x1, x2) > 0:
add = 1
if max(y1, y2) < 0:
add = 1
return max(y2, y1) - min(x2, x1) + 1 + add
input = sys.stdin.readline
t = int(input())
lmin = [(0) for i in range(0, 200003)]
lmax = [(0) for i in range(0, 200003)]
lv = [(0) for i in range(0, 200003)]
rmax = [(0) for i in range(0, 200003)]
rmin = [(0) for i in range(0, 200003)]
v = [(0) for i in range(0, 200003)]
for i in range(0, t):
n, m = map(int, input().split())
s = input()
for j in range(0, n):
if s[j] == "-":
v[j] = -1
else:
v[j] = 1
lr()
rl()
for j in range(0, m):
a, b = map(int, input().split())
cur = 0
if a == 1:
x1 = y1 = cur = 0
else:
x1 = lmin[a - 2]
y1 = lmax[a - 2]
cur = lv[a - 2]
if b == n:
x2 = y2 = 0
else:
x2 = rmin[b]
y2 = rmax[b]
print(calc(x1, y1, x2, y2, cur)) | IMPORT FUNC_DEF ASSIGN VAR NUMBER VAR NUMBER 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 VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR FUNC_DEF ASSIGN VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR VAR FUNC_DEF ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR NUMBER IF FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR NUMBER IF FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR NUMBER RETURN BIN_OP BIN_OP BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR NUMBER VAR ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR STRING ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER IF VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR VAR |
You are given a program that consists of $n$ instructions. Initially a single variable $x$ is assigned to $0$. Afterwards, the instructions are of two types:
increase $x$ by $1$;
decrease $x$ by $1$.
You are given $m$ queries of the following format:
query $l$ $r$ — how many distinct values is $x$ assigned to if all the instructions between the $l$-th one and the $r$-th one inclusive are ignored and the rest are executed without changing the order?
-----Input-----
The first line contains a single integer $t$ ($1 \le t \le 1000$) — the number of testcases.
Then the description of $t$ testcases follows.
The first line of each testcase contains two integers $n$ and $m$ ($1 \le n, m \le 2 \cdot 10^5$) — the number of instructions in the program and the number of queries.
The second line of each testcase contains a program — a string of $n$ characters: each character is either '+' or '-' — increment and decrement instruction, respectively.
Each of the next $m$ lines contains two integers $l$ and $r$ ($1 \le l \le r \le n$) — the description of the query.
The sum of $n$ over all testcases doesn't exceed $2 \cdot 10^5$. The sum of $m$ over all testcases doesn't exceed $2 \cdot 10^5$.
-----Output-----
For each testcase print $m$ integers — for each query $l$, $r$ print the number of distinct values variable $x$ is assigned to if all the instructions between the $l$-th one and the $r$-th one inclusive are ignored and the rest are executed without changing the order.
-----Examples-----
Input
2
8 4
-+--+--+
1 8
2 8
2 5
1 1
4 10
+-++
1 1
1 2
2 2
1 3
2 3
3 3
1 4
2 4
3 4
4 4
Output
1
2
4
4
3
3
4
2
3
2
1
2
2
2
-----Note-----
The instructions that remain for each query of the first testcase are:
empty program — $x$ was only equal to $0$;
"-" — $x$ had values $0$ and $-1$;
"---+" — $x$ had values $0$, $-1$, $-2$, $-3$, $-2$ — there are $4$ distinct values among them;
"+--+--+" — the distinct values are $1$, $0$, $-1$, $-2$. | from sys import stdin
input = stdin.readline
t = int(input())
for _ in range(t):
n, m = [int(x) for x in input().split()]
a = input().strip()
p = []
x = 0
y = 0
c = 0
p.append((x, y, c))
for i in range(n):
if a[i] == "+":
c += 1
x = max(x, c)
else:
c -= 1
y = min(y, c)
p.append((x, y, c))
s = []
x = 0
y = 0
s.append((x, y))
for i in range(n - 1, -1, -1):
if a[i] == "+":
x += 1
y = min(y + 1, 0)
else:
x = max(x - 1, 0)
y -= 1
s.append((x, y))
for _ in range(m):
l, r = [int(x) for x in input().split()]
max_ = max(p[l - 1][0], p[l - 1][2] + s[n - r][0])
min_ = min(p[l - 1][1], p[l - 1][2] + s[n - r][1])
ans = max_ - min_ + 1
print(ans) | ASSIGN VAR VAR 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 FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR STRING VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR VAR STRING VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR 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 BIN_OP VAR NUMBER NUMBER BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR |
You are given a program that consists of $n$ instructions. Initially a single variable $x$ is assigned to $0$. Afterwards, the instructions are of two types:
increase $x$ by $1$;
decrease $x$ by $1$.
You are given $m$ queries of the following format:
query $l$ $r$ — how many distinct values is $x$ assigned to if all the instructions between the $l$-th one and the $r$-th one inclusive are ignored and the rest are executed without changing the order?
-----Input-----
The first line contains a single integer $t$ ($1 \le t \le 1000$) — the number of testcases.
Then the description of $t$ testcases follows.
The first line of each testcase contains two integers $n$ and $m$ ($1 \le n, m \le 2 \cdot 10^5$) — the number of instructions in the program and the number of queries.
The second line of each testcase contains a program — a string of $n$ characters: each character is either '+' or '-' — increment and decrement instruction, respectively.
Each of the next $m$ lines contains two integers $l$ and $r$ ($1 \le l \le r \le n$) — the description of the query.
The sum of $n$ over all testcases doesn't exceed $2 \cdot 10^5$. The sum of $m$ over all testcases doesn't exceed $2 \cdot 10^5$.
-----Output-----
For each testcase print $m$ integers — for each query $l$, $r$ print the number of distinct values variable $x$ is assigned to if all the instructions between the $l$-th one and the $r$-th one inclusive are ignored and the rest are executed without changing the order.
-----Examples-----
Input
2
8 4
-+--+--+
1 8
2 8
2 5
1 1
4 10
+-++
1 1
1 2
2 2
1 3
2 3
3 3
1 4
2 4
3 4
4 4
Output
1
2
4
4
3
3
4
2
3
2
1
2
2
2
-----Note-----
The instructions that remain for each query of the first testcase are:
empty program — $x$ was only equal to $0$;
"-" — $x$ had values $0$ and $-1$;
"---+" — $x$ had values $0$, $-1$, $-2$, $-3$, $-2$ — there are $4$ distinct values among them;
"+--+--+" — the distinct values are $1$, $0$, $-1$, $-2$. | import sys
input = sys.stdin.readline
MAXN = 2 * 100000 + 5
cnt, top, bot = (
[0] * MAXN,
[([0] * MAXN) for i in range(2)],
[([0] * MAXN) for i in range(2)],
)
def program(n, m, s):
for i in range(n):
if s[i] == "+":
cnt[i + 1] = cnt[i] + 1
else:
cnt[i + 1] = cnt[i] - 1
for i in range(1, n + 1):
top[0][i] = bot[0][i] = top[1][i] = bot[1][i] = cnt[i]
for i in range(1, n + 1):
top[0][i] = max(top[0][i], top[0][i - 1])
bot[0][i] = min(bot[0][i], bot[0][i - 1])
for i in range(n - 1, -1, -1):
top[1][i] = max(top[1][i], top[1][i + 1])
bot[1][i] = min(bot[1][i], bot[1][i + 1])
for _ in range(m):
l, r = map(int, input().strip().split(" "))
diff = cnt[l - 1] - cnt[r]
if r < n:
t = max(top[0][l - 1], top[1][r + 1] + diff)
b = min(bot[0][l - 1], bot[1][r + 1] + diff)
else:
t, b = top[0][l - 1], bot[0][l - 1]
sys.stdout.write(str(t - b + 1) + "\n")
def main():
for t in range(int(input().strip())):
n, m = map(int, input().strip().split(" "))
s = input().strip()
program(n, m, s)
main() | IMPORT ASSIGN VAR VAR ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER ASSIGN VAR VAR VAR BIN_OP LIST NUMBER VAR BIN_OP LIST NUMBER VAR VAR FUNC_CALL VAR NUMBER BIN_OP LIST NUMBER VAR VAR FUNC_CALL VAR NUMBER FUNC_DEF FOR VAR FUNC_CALL VAR VAR IF VAR VAR STRING ASSIGN VAR BIN_OP VAR NUMBER BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER BIN_OP VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER VAR VAR NUMBER VAR VAR NUMBER VAR VAR NUMBER VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR NUMBER VAR VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR NUMBER VAR VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR NUMBER VAR VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR NUMBER VAR VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR ASSIGN VAR VAR VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER STRING FUNC_DEF FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR |
You are given a program that consists of $n$ instructions. Initially a single variable $x$ is assigned to $0$. Afterwards, the instructions are of two types:
increase $x$ by $1$;
decrease $x$ by $1$.
You are given $m$ queries of the following format:
query $l$ $r$ — how many distinct values is $x$ assigned to if all the instructions between the $l$-th one and the $r$-th one inclusive are ignored and the rest are executed without changing the order?
-----Input-----
The first line contains a single integer $t$ ($1 \le t \le 1000$) — the number of testcases.
Then the description of $t$ testcases follows.
The first line of each testcase contains two integers $n$ and $m$ ($1 \le n, m \le 2 \cdot 10^5$) — the number of instructions in the program and the number of queries.
The second line of each testcase contains a program — a string of $n$ characters: each character is either '+' or '-' — increment and decrement instruction, respectively.
Each of the next $m$ lines contains two integers $l$ and $r$ ($1 \le l \le r \le n$) — the description of the query.
The sum of $n$ over all testcases doesn't exceed $2 \cdot 10^5$. The sum of $m$ over all testcases doesn't exceed $2 \cdot 10^5$.
-----Output-----
For each testcase print $m$ integers — for each query $l$, $r$ print the number of distinct values variable $x$ is assigned to if all the instructions between the $l$-th one and the $r$-th one inclusive are ignored and the rest are executed without changing the order.
-----Examples-----
Input
2
8 4
-+--+--+
1 8
2 8
2 5
1 1
4 10
+-++
1 1
1 2
2 2
1 3
2 3
3 3
1 4
2 4
3 4
4 4
Output
1
2
4
4
3
3
4
2
3
2
1
2
2
2
-----Note-----
The instructions that remain for each query of the first testcase are:
empty program — $x$ was only equal to $0$;
"-" — $x$ had values $0$ and $-1$;
"---+" — $x$ had values $0$, $-1$, $-2$, $-3$, $-2$ — there are $4$ distinct values among them;
"+--+--+" — the distinct values are $1$, $0$, $-1$, $-2$. | import sys
input = sys.stdin.readline
def solve():
n, m = map(int, input().split())
s = input().strip()
ps = [0] * (n + 1)
f = [[0, 0] for _ in range(n + 1)]
curr = 0
for i in range(n):
curr += 1 if s[i] == "+" else -1
ps[i + 1] = curr
f[i + 1] = [max(curr, f[i][0]), min(curr, f[i][1])]
b = [[ps[-1], ps[-1]] for _ in range(n + 1)]
for i in range(n - 1, -1, -1):
b[i] = [max(b[i + 1][0], ps[i]), min(b[i + 1][1], ps[i])]
for _ in range(m):
left, right = map(int, input().split())
hi = max(f[left - 1][0], b[right][0] + ps[left - 1] - ps[right])
lo = min(f[left - 1][1], b[right][1] + ps[left - 1] - ps[right])
print(hi - lo + 1)
return
for _ in range(int(input())):
solve() | IMPORT ASSIGN VAR VAR FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR LIST NUMBER NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR VAR STRING NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP VAR NUMBER LIST FUNC_CALL VAR VAR VAR VAR NUMBER FUNC_CALL VAR VAR VAR VAR NUMBER ASSIGN VAR LIST VAR NUMBER VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR LIST FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER BIN_OP BIN_OP VAR VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER BIN_OP BIN_OP VAR VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER RETURN FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR |
You are given a program that consists of $n$ instructions. Initially a single variable $x$ is assigned to $0$. Afterwards, the instructions are of two types:
increase $x$ by $1$;
decrease $x$ by $1$.
You are given $m$ queries of the following format:
query $l$ $r$ — how many distinct values is $x$ assigned to if all the instructions between the $l$-th one and the $r$-th one inclusive are ignored and the rest are executed without changing the order?
-----Input-----
The first line contains a single integer $t$ ($1 \le t \le 1000$) — the number of testcases.
Then the description of $t$ testcases follows.
The first line of each testcase contains two integers $n$ and $m$ ($1 \le n, m \le 2 \cdot 10^5$) — the number of instructions in the program and the number of queries.
The second line of each testcase contains a program — a string of $n$ characters: each character is either '+' or '-' — increment and decrement instruction, respectively.
Each of the next $m$ lines contains two integers $l$ and $r$ ($1 \le l \le r \le n$) — the description of the query.
The sum of $n$ over all testcases doesn't exceed $2 \cdot 10^5$. The sum of $m$ over all testcases doesn't exceed $2 \cdot 10^5$.
-----Output-----
For each testcase print $m$ integers — for each query $l$, $r$ print the number of distinct values variable $x$ is assigned to if all the instructions between the $l$-th one and the $r$-th one inclusive are ignored and the rest are executed without changing the order.
-----Examples-----
Input
2
8 4
-+--+--+
1 8
2 8
2 5
1 1
4 10
+-++
1 1
1 2
2 2
1 3
2 3
3 3
1 4
2 4
3 4
4 4
Output
1
2
4
4
3
3
4
2
3
2
1
2
2
2
-----Note-----
The instructions that remain for each query of the first testcase are:
empty program — $x$ was only equal to $0$;
"-" — $x$ had values $0$ and $-1$;
"---+" — $x$ had values $0$, $-1$, $-2$, $-3$, $-2$ — there are $4$ distinct values among them;
"+--+--+" — the distinct values are $1$, $0$, $-1$, $-2$. | import sys
t = int(sys.stdin.readline().strip())
for _ in range(t):
n, m = map(int, sys.stdin.readline().split())
s = sys.stdin.readline().strip()
psa = [(0) for k in range(n + 1)]
for j in range(1, n + 1):
if s[j - 1] == "+":
psa[j] = psa[j - 1] + 1
else:
psa[j] = psa[j - 1] - 1
sufMax = [(0) for k in range(n + 1)]
sufMin = [(0) for k in range(n + 1)]
preMax = [(0) for k in range(n + 1)]
preMin = [(0) for k in range(n + 1)]
sufMax[-1] = psa[-1]
sufMin[-1] = psa[-1]
preMax[1] = psa[1]
preMin[1] = psa[1]
for i in range(1, n + 1):
preMax[i] = max(psa[i], preMax[i - 1])
preMin[i] = min([psa[i], preMin[i - 1]])
for i in range(n - 1, 0, -1):
sufMax[i] = max(psa[i], sufMax[i + 1])
sufMin[i] = min(psa[i], sufMin[i + 1])
for j in range(m):
l, r = map(int, sys.stdin.readline().split())
bestMax = preMax[l - 1]
bestMin = preMin[l - 1]
if r == n:
print(bestMax - bestMin + 1)
else:
temp = psa[l - 1] - psa[r]
bestMax = max(bestMax, sufMax[r + 1] + temp)
bestMin = min(bestMin, sufMin[r + 1] + temp)
print(bestMax - bestMin + 1) | IMPORT ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR BIN_OP VAR NUMBER STRING ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR BIN_OP VAR BIN_OP 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 NUMBER ASSIGN VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR LIST VAR VAR VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR 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 ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER |
You are given a program that consists of $n$ instructions. Initially a single variable $x$ is assigned to $0$. Afterwards, the instructions are of two types:
increase $x$ by $1$;
decrease $x$ by $1$.
You are given $m$ queries of the following format:
query $l$ $r$ — how many distinct values is $x$ assigned to if all the instructions between the $l$-th one and the $r$-th one inclusive are ignored and the rest are executed without changing the order?
-----Input-----
The first line contains a single integer $t$ ($1 \le t \le 1000$) — the number of testcases.
Then the description of $t$ testcases follows.
The first line of each testcase contains two integers $n$ and $m$ ($1 \le n, m \le 2 \cdot 10^5$) — the number of instructions in the program and the number of queries.
The second line of each testcase contains a program — a string of $n$ characters: each character is either '+' or '-' — increment and decrement instruction, respectively.
Each of the next $m$ lines contains two integers $l$ and $r$ ($1 \le l \le r \le n$) — the description of the query.
The sum of $n$ over all testcases doesn't exceed $2 \cdot 10^5$. The sum of $m$ over all testcases doesn't exceed $2 \cdot 10^5$.
-----Output-----
For each testcase print $m$ integers — for each query $l$, $r$ print the number of distinct values variable $x$ is assigned to if all the instructions between the $l$-th one and the $r$-th one inclusive are ignored and the rest are executed without changing the order.
-----Examples-----
Input
2
8 4
-+--+--+
1 8
2 8
2 5
1 1
4 10
+-++
1 1
1 2
2 2
1 3
2 3
3 3
1 4
2 4
3 4
4 4
Output
1
2
4
4
3
3
4
2
3
2
1
2
2
2
-----Note-----
The instructions that remain for each query of the first testcase are:
empty program — $x$ was only equal to $0$;
"-" — $x$ had values $0$ and $-1$;
"---+" — $x$ had values $0$, $-1$, $-2$, $-3$, $-2$ — there are $4$ distinct values among them;
"+--+--+" — the distinct values are $1$, $0$, $-1$, $-2$. | INF = 1 << 64
def slv():
n, m = map(int, input().split())
s = input()
querys = [tuple(map(int, input().split())) for i in range(m)]
dpr = [(0, 0) for i in range(n + 1)]
dpl = [(0, 0) for i in range(n + 1)]
dps = [(0) for i in range(n + 1)]
for i in range(n - 1, -1, -1):
M, m = dpr[i + 1]
if s[i] == "+":
dpr[i] = M + 1, min(m + 1, 0)
else:
dpr[i] = max(M - 1, 0), m - 1
tot = 0
for i in range(1, n + 1):
if s[i - 1] == "+":
tot += 1
else:
tot -= 1
tmpM, tmpm = max(dpl[i - 1][0], tot), min(dpl[i - 1][1], tot)
dpl[i] = tmpM, tmpm
dps[i] = tot
for l, r in querys:
lM, lm = dpl[l - 1]
rM, rm = dpr[r]
m = min(lm, dps[l - 1] + rm)
M = max(lM, dps[l - 1] + rM)
if m <= 0 <= M:
print(M - m + 1)
else:
print(M - m + 2)
return
def main():
T = int(input())
for _ in range(T):
slv()
return
main() | ASSIGN VAR BIN_OP NUMBER NUMBER FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER IF VAR VAR STRING ASSIGN VAR VAR BIN_OP VAR NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR BIN_OP VAR NUMBER STRING VAR NUMBER VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR VAR FOR VAR VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR IF VAR NUMBER VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER RETURN FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR RETURN EXPR FUNC_CALL VAR |
You are given a program that consists of $n$ instructions. Initially a single variable $x$ is assigned to $0$. Afterwards, the instructions are of two types:
increase $x$ by $1$;
decrease $x$ by $1$.
You are given $m$ queries of the following format:
query $l$ $r$ — how many distinct values is $x$ assigned to if all the instructions between the $l$-th one and the $r$-th one inclusive are ignored and the rest are executed without changing the order?
-----Input-----
The first line contains a single integer $t$ ($1 \le t \le 1000$) — the number of testcases.
Then the description of $t$ testcases follows.
The first line of each testcase contains two integers $n$ and $m$ ($1 \le n, m \le 2 \cdot 10^5$) — the number of instructions in the program and the number of queries.
The second line of each testcase contains a program — a string of $n$ characters: each character is either '+' or '-' — increment and decrement instruction, respectively.
Each of the next $m$ lines contains two integers $l$ and $r$ ($1 \le l \le r \le n$) — the description of the query.
The sum of $n$ over all testcases doesn't exceed $2 \cdot 10^5$. The sum of $m$ over all testcases doesn't exceed $2 \cdot 10^5$.
-----Output-----
For each testcase print $m$ integers — for each query $l$, $r$ print the number of distinct values variable $x$ is assigned to if all the instructions between the $l$-th one and the $r$-th one inclusive are ignored and the rest are executed without changing the order.
-----Examples-----
Input
2
8 4
-+--+--+
1 8
2 8
2 5
1 1
4 10
+-++
1 1
1 2
2 2
1 3
2 3
3 3
1 4
2 4
3 4
4 4
Output
1
2
4
4
3
3
4
2
3
2
1
2
2
2
-----Note-----
The instructions that remain for each query of the first testcase are:
empty program — $x$ was only equal to $0$;
"-" — $x$ had values $0$ and $-1$;
"---+" — $x$ had values $0$, $-1$, $-2$, $-3$, $-2$ — there are $4$ distinct values among them;
"+--+--+" — the distinct values are $1$, $0$, $-1$, $-2$. | import sys
input = sys.stdin.readline
t = int(input())
for _ in range(t):
n, m = map(int, input().split())
s = input()
ruiseki_end = [[] for i in range(n)]
ma = 0
mi = 0
now = 0
for i in range(n):
if s[i] == "+":
now += 1
else:
now -= 1
ma = max(ma, now)
mi = min(mi, now)
ruiseki_end[i] = [ma, mi, now]
ruiseki_start = [[] for i in range(n)]
ma = 0
mi = 0
now = 0
for i in reversed(range(n)):
if s[i] == "+":
ma += 1
if mi < 0:
mi += 1
else:
mi -= 1
if ma > 0:
ma -= 1
ruiseki_start[i] = [ma, mi]
for i in range(m):
l, r = map(int, input().split())
l -= 1
if l == 0 and r == n:
ans = 1
elif l == 0:
ans = ruiseki_start[r][0] - ruiseki_start[r][1] + 1
elif r == n:
ans = ruiseki_end[l - 1][0] - ruiseki_end[l - 1][1] + 1
else:
x = max(ruiseki_end[l - 1][0], ruiseki_end[l - 1][2] + ruiseki_start[r][0])
y = min(ruiseki_end[l - 1][1], ruiseki_end[l - 1][2] + ruiseki_start[r][1])
ans = x - y + 1
print(ans) | IMPORT ASSIGN VAR 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 ASSIGN VAR LIST 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 NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR LIST VAR VAR VAR ASSIGN VAR LIST VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR STRING VAR NUMBER IF VAR NUMBER VAR NUMBER VAR NUMBER IF VAR NUMBER VAR NUMBER ASSIGN VAR VAR LIST VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR VAR NUMBER IF VAR NUMBER VAR VAR ASSIGN VAR NUMBER IF VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER VAR VAR NUMBER NUMBER IF VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR |
You are given a program that consists of $n$ instructions. Initially a single variable $x$ is assigned to $0$. Afterwards, the instructions are of two types:
increase $x$ by $1$;
decrease $x$ by $1$.
You are given $m$ queries of the following format:
query $l$ $r$ — how many distinct values is $x$ assigned to if all the instructions between the $l$-th one and the $r$-th one inclusive are ignored and the rest are executed without changing the order?
-----Input-----
The first line contains a single integer $t$ ($1 \le t \le 1000$) — the number of testcases.
Then the description of $t$ testcases follows.
The first line of each testcase contains two integers $n$ and $m$ ($1 \le n, m \le 2 \cdot 10^5$) — the number of instructions in the program and the number of queries.
The second line of each testcase contains a program — a string of $n$ characters: each character is either '+' or '-' — increment and decrement instruction, respectively.
Each of the next $m$ lines contains two integers $l$ and $r$ ($1 \le l \le r \le n$) — the description of the query.
The sum of $n$ over all testcases doesn't exceed $2 \cdot 10^5$. The sum of $m$ over all testcases doesn't exceed $2 \cdot 10^5$.
-----Output-----
For each testcase print $m$ integers — for each query $l$, $r$ print the number of distinct values variable $x$ is assigned to if all the instructions between the $l$-th one and the $r$-th one inclusive are ignored and the rest are executed without changing the order.
-----Examples-----
Input
2
8 4
-+--+--+
1 8
2 8
2 5
1 1
4 10
+-++
1 1
1 2
2 2
1 3
2 3
3 3
1 4
2 4
3 4
4 4
Output
1
2
4
4
3
3
4
2
3
2
1
2
2
2
-----Note-----
The instructions that remain for each query of the first testcase are:
empty program — $x$ was only equal to $0$;
"-" — $x$ had values $0$ and $-1$;
"---+" — $x$ had values $0$, $-1$, $-2$, $-3$, $-2$ — there are $4$ distinct values among them;
"+--+--+" — the distinct values are $1$, $0$, $-1$, $-2$. | import sys
def main():
t = int(sys.stdin.readline())
for _ in range(t):
n, m = map(int, sys.stdin.readline().split())
seq = sys.stdin.readline().strip()
res = [0] * (len(seq) + 1)
for i in range(1, len(res)):
if seq[i - 1] == "+":
res[i] = res[i - 1] + 1
else:
res[i] = res[i - 1] - 1
mins, maxs = [0] * (len(seq) + 1), [0] * (len(seq) + 1)
for i in range(1, len(mins)):
mins[i] = min(mins[i - 1], res[i])
maxs[i] = max(maxs[i - 1], res[i])
mins_rev, maxs_rev = [0] * (len(seq) + 1), [0] * (len(seq) + 1)
mins_rev[-1], maxs_rev[-1] = res[-1], res[-1]
for i in reversed(range(len(mins_rev) - 1)):
mins_rev[i] = min(mins_rev[i + 1], res[i])
maxs_rev[i] = max(maxs_rev[i + 1], res[i])
for _ in range(m):
l, r = map(int, sys.stdin.readline().split())
minl, maxl = mins[l - 1], maxs[l - 1]
try:
minr = mins_rev[r + 1] - res[r] + res[l - 1]
maxr = maxs_rev[r + 1] - res[r] + res[l - 1]
except IndexError:
minr = minl
maxr = maxl
print(max(maxr, maxl) - min(minl, minr) + 1)
main() | IMPORT FUNC_DEF 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 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 VAR BIN_OP VAR NUMBER STRING ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR BIN_OP LIST NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER BIN_OP LIST NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR VAR BIN_OP LIST NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER BIN_OP LIST NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR VAR FUNC_CALL 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 VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR |
You are given a program that consists of $n$ instructions. Initially a single variable $x$ is assigned to $0$. Afterwards, the instructions are of two types:
increase $x$ by $1$;
decrease $x$ by $1$.
You are given $m$ queries of the following format:
query $l$ $r$ — how many distinct values is $x$ assigned to if all the instructions between the $l$-th one and the $r$-th one inclusive are ignored and the rest are executed without changing the order?
-----Input-----
The first line contains a single integer $t$ ($1 \le t \le 1000$) — the number of testcases.
Then the description of $t$ testcases follows.
The first line of each testcase contains two integers $n$ and $m$ ($1 \le n, m \le 2 \cdot 10^5$) — the number of instructions in the program and the number of queries.
The second line of each testcase contains a program — a string of $n$ characters: each character is either '+' or '-' — increment and decrement instruction, respectively.
Each of the next $m$ lines contains two integers $l$ and $r$ ($1 \le l \le r \le n$) — the description of the query.
The sum of $n$ over all testcases doesn't exceed $2 \cdot 10^5$. The sum of $m$ over all testcases doesn't exceed $2 \cdot 10^5$.
-----Output-----
For each testcase print $m$ integers — for each query $l$, $r$ print the number of distinct values variable $x$ is assigned to if all the instructions between the $l$-th one and the $r$-th one inclusive are ignored and the rest are executed without changing the order.
-----Examples-----
Input
2
8 4
-+--+--+
1 8
2 8
2 5
1 1
4 10
+-++
1 1
1 2
2 2
1 3
2 3
3 3
1 4
2 4
3 4
4 4
Output
1
2
4
4
3
3
4
2
3
2
1
2
2
2
-----Note-----
The instructions that remain for each query of the first testcase are:
empty program — $x$ was only equal to $0$;
"-" — $x$ had values $0$ and $-1$;
"---+" — $x$ had values $0$, $-1$, $-2$, $-3$, $-2$ — there are $4$ distinct values among them;
"+--+--+" — the distinct values are $1$, $0$, $-1$, $-2$. | import sys
def comb(x, y):
low = min(x[0], y[0] + x[2])
high = max(x[1], y[1] + x[2])
tot = y[2] + x[2]
return low, high, tot
class SegmentTree:
def __init__(self, data, default=(0, 0, 0), func=comb):
self._default = default
self._func = func
self._len = len(data)
self._size = _size = 1 << (self._len - 1).bit_length()
self.data = [default] * (2 * _size)
self.data[_size : _size + self._len] = data
for i in reversed(range(_size)):
self.data[i] = func(self.data[i + i], self.data[i + i + 1])
def __delitem__(self, idx):
self[idx] = self._default
def __getitem__(self, idx):
return self.data[idx + self._size]
def __setitem__(self, idx, value):
idx += self._size
self.data[idx] = value
idx >>= 1
while idx:
self.data[idx] = self._func(self.data[2 * idx], self.data[2 * idx + 1])
idx >>= 1
def __len__(self):
return self._len
def query(self, start, stop):
start += self._size
stop += self._size
res_left = res_right = self._default
while start < stop:
if start & 1:
res_left = self._func(res_left, self.data[start])
start += 1
if stop & 1:
stop -= 1
res_right = self._func(self.data[stop], res_right)
start >>= 1
stop >>= 1
return self._func(res_left, res_right)
def __repr__(self):
return "SegmentTree({0})".format(self.data)
input = sys.stdin.readline
t = int(input())
out = []
for _ in range(t):
n, m = map(int, input().split())
data = [(0, 0, 0)] * (n + 5)
s = input().strip()
for i in range(n):
data[i] = (0, 1, 1) if s[i] == "+" else (-1, 0, -1)
seg = SegmentTree(data)
for q in range(m):
l, r = map(int, input().split())
left = seg.query(0, l - 1)
right = seg.query(r, n + 2)
tot = comb(left, right)
out.append(1 + tot[1] - tot[0])
print("\n".join(map(str, out))) | IMPORT FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR NUMBER RETURN VAR VAR VAR CLASS_DEF FUNC_DEF NUMBER NUMBER NUMBER VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP NUMBER FUNC_CALL BIN_OP VAR NUMBER ASSIGN VAR BIN_OP LIST VAR BIN_OP NUMBER VAR ASSIGN VAR VAR BIN_OP VAR VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER FUNC_DEF ASSIGN VAR VAR VAR FUNC_DEF RETURN VAR BIN_OP VAR VAR FUNC_DEF VAR VAR ASSIGN VAR VAR VAR VAR NUMBER WHILE VAR ASSIGN VAR VAR FUNC_CALL VAR VAR BIN_OP NUMBER VAR VAR BIN_OP BIN_OP NUMBER VAR NUMBER VAR NUMBER FUNC_DEF RETURN VAR FUNC_DEF VAR VAR VAR VAR ASSIGN VAR VAR VAR WHILE VAR VAR IF BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR NUMBER IF BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR NUMBER VAR NUMBER RETURN FUNC_CALL VAR VAR VAR FUNC_DEF RETURN FUNC_CALL STRING VAR ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER NUMBER NUMBER BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR STRING NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER 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 FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP NUMBER VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR |
You are given a program that consists of $n$ instructions. Initially a single variable $x$ is assigned to $0$. Afterwards, the instructions are of two types:
increase $x$ by $1$;
decrease $x$ by $1$.
You are given $m$ queries of the following format:
query $l$ $r$ — how many distinct values is $x$ assigned to if all the instructions between the $l$-th one and the $r$-th one inclusive are ignored and the rest are executed without changing the order?
-----Input-----
The first line contains a single integer $t$ ($1 \le t \le 1000$) — the number of testcases.
Then the description of $t$ testcases follows.
The first line of each testcase contains two integers $n$ and $m$ ($1 \le n, m \le 2 \cdot 10^5$) — the number of instructions in the program and the number of queries.
The second line of each testcase contains a program — a string of $n$ characters: each character is either '+' or '-' — increment and decrement instruction, respectively.
Each of the next $m$ lines contains two integers $l$ and $r$ ($1 \le l \le r \le n$) — the description of the query.
The sum of $n$ over all testcases doesn't exceed $2 \cdot 10^5$. The sum of $m$ over all testcases doesn't exceed $2 \cdot 10^5$.
-----Output-----
For each testcase print $m$ integers — for each query $l$, $r$ print the number of distinct values variable $x$ is assigned to if all the instructions between the $l$-th one and the $r$-th one inclusive are ignored and the rest are executed without changing the order.
-----Examples-----
Input
2
8 4
-+--+--+
1 8
2 8
2 5
1 1
4 10
+-++
1 1
1 2
2 2
1 3
2 3
3 3
1 4
2 4
3 4
4 4
Output
1
2
4
4
3
3
4
2
3
2
1
2
2
2
-----Note-----
The instructions that remain for each query of the first testcase are:
empty program — $x$ was only equal to $0$;
"-" — $x$ had values $0$ and $-1$;
"---+" — $x$ had values $0$, $-1$, $-2$, $-3$, $-2$ — there are $4$ distinct values among them;
"+--+--+" — the distinct values are $1$, $0$, $-1$, $-2$. | from itertools import accumulate
from sys import stdin, stdout
readline = stdin.readline
write = stdout.write
def read_ints():
return map(int, readline().split())
def read_string():
return readline()[:-1]
def write_int(x):
write(str(x) + "\n")
def find_prefix_ranges(op_seq):
x = 0
min_x = max_x = x
result = [(min_x, max_x)]
for op in op_seq:
x += op
if x < min_x:
min_x = x
if x > max_x:
max_x = x
result.append((min_x, max_x))
return result
def find_suffix_ranges(op_seq):
min_x = max_x = 0
result = [(min_x, max_x)]
for op in op_seq[::-1]:
min_x += op
max_x += op
if op < min_x:
min_x = op
if op > max_x:
max_x = op
result.append((min_x, max_x))
return result[::-1]
(t_n,) = read_ints()
for i_t in range(t_n):
n, q_n = read_ints()
s = read_string()
op_seq = [(+1 if char == "+" else -1) for char in s]
prefix_sums = [0] + list(accumulate(op_seq))
prefix_ranges = find_prefix_ranges(op_seq)
suffix_ranges = find_suffix_ranges(op_seq)
for i_q in range(q_n):
l, r = read_ints()
range_before = prefix_ranges[l - 1]
range_after = suffix_ranges[r]
delta_for_after = prefix_sums[l - 1]
min_x, max_x = range_after
min_x += delta_for_after
max_x += delta_for_after
range_after = min_x, max_x
a, b = range_before, range_after
if a > b:
a, b = b, a
assert a <= b
if a[1] < b[0]:
result = a[1] - a[0] + 1 + b[1] - b[0] + 1
else:
final_range = a[0], max(a[1], b[1])
result = final_range[1] - final_range[0] + 1
write_int(result) | ASSIGN VAR VAR ASSIGN VAR VAR FUNC_DEF RETURN FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR NUMBER FUNC_DEF EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR STRING FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR LIST VAR VAR FOR VAR VAR VAR VAR IF VAR VAR ASSIGN VAR VAR IF VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR VAR NUMBER ASSIGN VAR LIST VAR VAR FOR VAR VAR NUMBER VAR VAR VAR VAR IF VAR VAR ASSIGN VAR VAR IF VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR VAR RETURN VAR NUMBER ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR STRING NUMBER NUMBER VAR VAR ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR VAR IF VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR IF VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP BIN_OP BIN_OP VAR NUMBER VAR NUMBER NUMBER VAR NUMBER VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR |
You are given a program that consists of $n$ instructions. Initially a single variable $x$ is assigned to $0$. Afterwards, the instructions are of two types:
increase $x$ by $1$;
decrease $x$ by $1$.
You are given $m$ queries of the following format:
query $l$ $r$ — how many distinct values is $x$ assigned to if all the instructions between the $l$-th one and the $r$-th one inclusive are ignored and the rest are executed without changing the order?
-----Input-----
The first line contains a single integer $t$ ($1 \le t \le 1000$) — the number of testcases.
Then the description of $t$ testcases follows.
The first line of each testcase contains two integers $n$ and $m$ ($1 \le n, m \le 2 \cdot 10^5$) — the number of instructions in the program and the number of queries.
The second line of each testcase contains a program — a string of $n$ characters: each character is either '+' or '-' — increment and decrement instruction, respectively.
Each of the next $m$ lines contains two integers $l$ and $r$ ($1 \le l \le r \le n$) — the description of the query.
The sum of $n$ over all testcases doesn't exceed $2 \cdot 10^5$. The sum of $m$ over all testcases doesn't exceed $2 \cdot 10^5$.
-----Output-----
For each testcase print $m$ integers — for each query $l$, $r$ print the number of distinct values variable $x$ is assigned to if all the instructions between the $l$-th one and the $r$-th one inclusive are ignored and the rest are executed without changing the order.
-----Examples-----
Input
2
8 4
-+--+--+
1 8
2 8
2 5
1 1
4 10
+-++
1 1
1 2
2 2
1 3
2 3
3 3
1 4
2 4
3 4
4 4
Output
1
2
4
4
3
3
4
2
3
2
1
2
2
2
-----Note-----
The instructions that remain for each query of the first testcase are:
empty program — $x$ was only equal to $0$;
"-" — $x$ had values $0$ and $-1$;
"---+" — $x$ had values $0$, $-1$, $-2$, $-3$, $-2$ — there are $4$ distinct values among them;
"+--+--+" — the distinct values are $1$, $0$, $-1$, $-2$. | from sys import stdin
t = int(stdin.readline())
for _ in range(t):
L = [int(x) for x in stdin.readline().split(" ")]
n, m = L[0], L[1]
P = [(-1 if x == "-" else 1) for x in stdin.readline()][:n]
cumSumL2R = [(0) for _ in range(n)]
maxL2R = [(0) for _ in range(n)]
minL2R = [(0) for _ in range(n)]
maxR2L = [(0) for _ in range(n)]
minR2L = [(0) for _ in range(n)]
cumSumL2R[0] = P[0]
maxL2R[0] = P[0]
minL2R[0] = P[0]
maxR2L[n - 1] = P[-1]
minR2L[n - 1] = P[-1]
for i in range(1, n):
cumSumL2R[i] = P[i] + cumSumL2R[i - 1]
maxL2R[i] = max(maxL2R[i - 1], cumSumL2R[i])
minL2R[i] = min(minL2R[i - 1], cumSumL2R[i])
for i in range(n - 2, -1, -1):
maxR2L[i] = max(maxR2L[i + 1] + P[i], P[i])
minR2L[i] = min(minR2L[i + 1] + P[i], P[i])
for _ in range(m):
L = [int(x) for x in stdin.readline().split(" ")]
l, r = L[0], L[1]
lowLeft = 0 if l == 1 else minL2R[l - 2]
highLeft = 0 if l == 1 else maxL2R[l - 2]
endLeft = 0 if l == 1 else cumSumL2R[l - 2]
lowRight = endLeft + (0 if r == n else minR2L[r])
highRight = endLeft + (0 if r == n else maxR2L[r])
low = min(min(lowLeft, lowRight), 0)
high = max(max(highLeft, highRight), 0)
print(str(high - low + 1)) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR STRING NUMBER NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR NUMBER VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER |
You are given a program that consists of $n$ instructions. Initially a single variable $x$ is assigned to $0$. Afterwards, the instructions are of two types:
increase $x$ by $1$;
decrease $x$ by $1$.
You are given $m$ queries of the following format:
query $l$ $r$ — how many distinct values is $x$ assigned to if all the instructions between the $l$-th one and the $r$-th one inclusive are ignored and the rest are executed without changing the order?
-----Input-----
The first line contains a single integer $t$ ($1 \le t \le 1000$) — the number of testcases.
Then the description of $t$ testcases follows.
The first line of each testcase contains two integers $n$ and $m$ ($1 \le n, m \le 2 \cdot 10^5$) — the number of instructions in the program and the number of queries.
The second line of each testcase contains a program — a string of $n$ characters: each character is either '+' or '-' — increment and decrement instruction, respectively.
Each of the next $m$ lines contains two integers $l$ and $r$ ($1 \le l \le r \le n$) — the description of the query.
The sum of $n$ over all testcases doesn't exceed $2 \cdot 10^5$. The sum of $m$ over all testcases doesn't exceed $2 \cdot 10^5$.
-----Output-----
For each testcase print $m$ integers — for each query $l$, $r$ print the number of distinct values variable $x$ is assigned to if all the instructions between the $l$-th one and the $r$-th one inclusive are ignored and the rest are executed without changing the order.
-----Examples-----
Input
2
8 4
-+--+--+
1 8
2 8
2 5
1 1
4 10
+-++
1 1
1 2
2 2
1 3
2 3
3 3
1 4
2 4
3 4
4 4
Output
1
2
4
4
3
3
4
2
3
2
1
2
2
2
-----Note-----
The instructions that remain for each query of the first testcase are:
empty program — $x$ was only equal to $0$;
"-" — $x$ had values $0$ and $-1$;
"---+" — $x$ had values $0$, $-1$, $-2$, $-3$, $-2$ — there are $4$ distinct values among them;
"+--+--+" — the distinct values are $1$, $0$, $-1$, $-2$. | import sys
input = sys.stdin.readline
for _ in range(int(input())):
psa = [0]
n, m = map(int, input().split())
mx = [0] * (n + 1)
mn = [0] * (n + 1)
mx2 = [0]
mn2 = [0]
s = input().strip("\n")
for i in range(n):
if s[i] == "+":
k = 1
else:
k = -1
psa.append(psa[-1] + k)
mx2.append(max(mx2[-1], psa[-1]))
mn2.append(min(mn2[-1], psa[-1]))
for i in range(n, -1, -1):
if i == n:
mx[i] = psa[i]
mn[i] = psa[i]
else:
mx[i] = max(mx[i + 1], psa[i])
mn[i] = min(mn[i + 1], psa[i])
mx.append(mx[-1])
mn.append(mn[-1])
for i in range(m):
l, r = map(int, input().split())
v = psa[l - 1]
x = mx[r + 1] - psa[r]
y = mn[r + 1] - psa[r]
print(max(v + x, mx2[l - 1]) - min(v + y, mn2[l - 1]) + 1) | IMPORT ASSIGN VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST NUMBER 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 LIST NUMBER ASSIGN VAR LIST NUMBER ASSIGN VAR FUNC_CALL FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR VAR IF VAR VAR STRING ASSIGN VAR NUMBER ASSIGN VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR VAR NUMBER NUMBER IF VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP FUNC_CALL VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER FUNC_CALL VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER NUMBER |
You are given a program that consists of $n$ instructions. Initially a single variable $x$ is assigned to $0$. Afterwards, the instructions are of two types:
increase $x$ by $1$;
decrease $x$ by $1$.
You are given $m$ queries of the following format:
query $l$ $r$ — how many distinct values is $x$ assigned to if all the instructions between the $l$-th one and the $r$-th one inclusive are ignored and the rest are executed without changing the order?
-----Input-----
The first line contains a single integer $t$ ($1 \le t \le 1000$) — the number of testcases.
Then the description of $t$ testcases follows.
The first line of each testcase contains two integers $n$ and $m$ ($1 \le n, m \le 2 \cdot 10^5$) — the number of instructions in the program and the number of queries.
The second line of each testcase contains a program — a string of $n$ characters: each character is either '+' or '-' — increment and decrement instruction, respectively.
Each of the next $m$ lines contains two integers $l$ and $r$ ($1 \le l \le r \le n$) — the description of the query.
The sum of $n$ over all testcases doesn't exceed $2 \cdot 10^5$. The sum of $m$ over all testcases doesn't exceed $2 \cdot 10^5$.
-----Output-----
For each testcase print $m$ integers — for each query $l$, $r$ print the number of distinct values variable $x$ is assigned to if all the instructions between the $l$-th one and the $r$-th one inclusive are ignored and the rest are executed without changing the order.
-----Examples-----
Input
2
8 4
-+--+--+
1 8
2 8
2 5
1 1
4 10
+-++
1 1
1 2
2 2
1 3
2 3
3 3
1 4
2 4
3 4
4 4
Output
1
2
4
4
3
3
4
2
3
2
1
2
2
2
-----Note-----
The instructions that remain for each query of the first testcase are:
empty program — $x$ was only equal to $0$;
"-" — $x$ had values $0$ and $-1$;
"---+" — $x$ had values $0$, $-1$, $-2$, $-3$, $-2$ — there are $4$ distinct values among them;
"+--+--+" — the distinct values are $1$, $0$, $-1$, $-2$. | e = int(input())
l = []
y = []
A = []
for i in range(e):
[a, b] = input().split(" ")
n = int(a)
m = int(b)
A.append((n, m))
x = []
z = input()
y.append(z)
for j in range(m):
t = input().split(" ")
for i in range(2):
t[i] = int(t[i])
x.append(t)
l.append(x)
for i in range(e):
n, m = A[i]
x = 0
L = [0]
M = [(0, 0)]
mi = 0
ma = 0
for j in range(n):
if y[i][j] == "-":
x = x - 1
else:
x = x + 1
mi = min(mi, x)
ma = max(ma, x)
M.append((mi, ma))
L.append(x)
N = [(L[-1], L[-1])]
mi, ma = L[-1], L[-1]
for j in reversed(range(n)):
mi = min(mi, L[j])
ma = max(ma, L[j])
N.append((mi, ma))
for j in range(m):
[a, b] = l[i][j]
X, Y = M[a - 1]
K, P = N[n - b]
if b < n:
X = min(K - (L[b] - L[a - 1]), X)
Y = max(P - (L[b] - L[a - 1]), Y)
print(Y - X + 1) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN LIST VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR LIST ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR LIST NUMBER ASSIGN VAR LIST NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR STRING ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR LIST VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN LIST VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR VAR IF VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER |
You are given a program that consists of $n$ instructions. Initially a single variable $x$ is assigned to $0$. Afterwards, the instructions are of two types:
increase $x$ by $1$;
decrease $x$ by $1$.
You are given $m$ queries of the following format:
query $l$ $r$ — how many distinct values is $x$ assigned to if all the instructions between the $l$-th one and the $r$-th one inclusive are ignored and the rest are executed without changing the order?
-----Input-----
The first line contains a single integer $t$ ($1 \le t \le 1000$) — the number of testcases.
Then the description of $t$ testcases follows.
The first line of each testcase contains two integers $n$ and $m$ ($1 \le n, m \le 2 \cdot 10^5$) — the number of instructions in the program and the number of queries.
The second line of each testcase contains a program — a string of $n$ characters: each character is either '+' or '-' — increment and decrement instruction, respectively.
Each of the next $m$ lines contains two integers $l$ and $r$ ($1 \le l \le r \le n$) — the description of the query.
The sum of $n$ over all testcases doesn't exceed $2 \cdot 10^5$. The sum of $m$ over all testcases doesn't exceed $2 \cdot 10^5$.
-----Output-----
For each testcase print $m$ integers — for each query $l$, $r$ print the number of distinct values variable $x$ is assigned to if all the instructions between the $l$-th one and the $r$-th one inclusive are ignored and the rest are executed without changing the order.
-----Examples-----
Input
2
8 4
-+--+--+
1 8
2 8
2 5
1 1
4 10
+-++
1 1
1 2
2 2
1 3
2 3
3 3
1 4
2 4
3 4
4 4
Output
1
2
4
4
3
3
4
2
3
2
1
2
2
2
-----Note-----
The instructions that remain for each query of the first testcase are:
empty program — $x$ was only equal to $0$;
"-" — $x$ had values $0$ and $-1$;
"---+" — $x$ had values $0$, $-1$, $-2$, $-3$, $-2$ — there are $4$ distinct values among them;
"+--+--+" — the distinct values are $1$, $0$, $-1$, $-2$. | import sys
input = lambda: sys.stdin.readline().rstrip("\r\n")
for _ in range(int(input())):
n, m = map(int, input().split())
s = input()
leftmax = [0] * (n + 1)
leftmin = [0] * (n + 1)
rightmax = [0] * (n + 1)
rightmin = [0] * (n + 1)
end = [0] * (n + 1)
endr = [0] * (n + 1)
minn, maxx = 0, 0
x = 0
for i in range(n):
if s[i] == "-":
x -= 1
else:
x += 1
minn = min(minn, x)
maxx = max(maxx, x)
leftmin[i + 1] = minn
leftmax[i + 1] = maxx
end[i + 1] = x
x = 0
minn, maxx = 0, 0
for i in range(n - 1, -1, -1):
if s[i] == "+":
x -= 1
else:
x += 1
minn = min(minn, x)
maxx = max(maxx, x)
rightmin[i] = minn
rightmax[i] = maxx
endr[i] = x
for i in range(m):
l, r = map(int, input().split())
l -= 1
nowmax = end[l] + rightmax[r] - endr[r]
nowmin = end[l] + rightmin[r] - endr[r]
maxx = max(nowmax, leftmax[l])
minn = min(nowmin, leftmin[l])
ans = maxx - minn
print(ans + 1) | IMPORT ASSIGN VAR FUNC_CALL FUNC_CALL VAR STRING 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 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 BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR STRING VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP VAR NUMBER VAR ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR VAR STRING VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR 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 VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER |
You are given a program that consists of $n$ instructions. Initially a single variable $x$ is assigned to $0$. Afterwards, the instructions are of two types:
increase $x$ by $1$;
decrease $x$ by $1$.
You are given $m$ queries of the following format:
query $l$ $r$ — how many distinct values is $x$ assigned to if all the instructions between the $l$-th one and the $r$-th one inclusive are ignored and the rest are executed without changing the order?
-----Input-----
The first line contains a single integer $t$ ($1 \le t \le 1000$) — the number of testcases.
Then the description of $t$ testcases follows.
The first line of each testcase contains two integers $n$ and $m$ ($1 \le n, m \le 2 \cdot 10^5$) — the number of instructions in the program and the number of queries.
The second line of each testcase contains a program — a string of $n$ characters: each character is either '+' or '-' — increment and decrement instruction, respectively.
Each of the next $m$ lines contains two integers $l$ and $r$ ($1 \le l \le r \le n$) — the description of the query.
The sum of $n$ over all testcases doesn't exceed $2 \cdot 10^5$. The sum of $m$ over all testcases doesn't exceed $2 \cdot 10^5$.
-----Output-----
For each testcase print $m$ integers — for each query $l$, $r$ print the number of distinct values variable $x$ is assigned to if all the instructions between the $l$-th one and the $r$-th one inclusive are ignored and the rest are executed without changing the order.
-----Examples-----
Input
2
8 4
-+--+--+
1 8
2 8
2 5
1 1
4 10
+-++
1 1
1 2
2 2
1 3
2 3
3 3
1 4
2 4
3 4
4 4
Output
1
2
4
4
3
3
4
2
3
2
1
2
2
2
-----Note-----
The instructions that remain for each query of the first testcase are:
empty program — $x$ was only equal to $0$;
"-" — $x$ had values $0$ and $-1$;
"---+" — $x$ had values $0$, $-1$, $-2$, $-3$, $-2$ — there are $4$ distinct values among them;
"+--+--+" — the distinct values are $1$, $0$, $-1$, $-2$. | import sys
input = sys.stdin.readline
for _ in range(int(input())):
n, m = map(int, input().split())
s = input()
l = [[0, 0] for i in range(n + 2)]
for i in range(n, 0, -1):
if s[i - 1] == "+":
l[i][0] = min(0, l[i + 1][0] + 1)
l[i][1] = l[i + 1][1] + 1
else:
l[i][0] = l[i + 1][0] - 1
l[i][1] = max(0, l[i + 1][1] - 1)
p = [[0, 0]]
v = [0]
for c in s:
if c == "+":
v.append(v[-1] + 1)
else:
v.append(v[-1] - 1)
p.append([min(v[-1], p[-1][0]), max(v[-1], p[-1][1])])
for i in range(m):
a, b = map(int, input().split())
mn = min(p[a - 1][0], v[a - 1] + l[b + 1][0])
mx = max(p[a - 1][1], v[a - 1] + l[b + 1][1])
print(mx - mn + 1) | IMPORT ASSIGN VAR 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 ASSIGN VAR LIST NUMBER NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR NUMBER NUMBER IF VAR BIN_OP VAR NUMBER STRING ASSIGN VAR VAR NUMBER FUNC_CALL VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR NUMBER FUNC_CALL VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR LIST LIST NUMBER NUMBER ASSIGN VAR LIST NUMBER FOR VAR VAR IF VAR STRING EXPR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR LIST FUNC_CALL VAR VAR NUMBER VAR NUMBER NUMBER FUNC_CALL VAR VAR NUMBER VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER |
You are given a program that consists of $n$ instructions. Initially a single variable $x$ is assigned to $0$. Afterwards, the instructions are of two types:
increase $x$ by $1$;
decrease $x$ by $1$.
You are given $m$ queries of the following format:
query $l$ $r$ — how many distinct values is $x$ assigned to if all the instructions between the $l$-th one and the $r$-th one inclusive are ignored and the rest are executed without changing the order?
-----Input-----
The first line contains a single integer $t$ ($1 \le t \le 1000$) — the number of testcases.
Then the description of $t$ testcases follows.
The first line of each testcase contains two integers $n$ and $m$ ($1 \le n, m \le 2 \cdot 10^5$) — the number of instructions in the program and the number of queries.
The second line of each testcase contains a program — a string of $n$ characters: each character is either '+' or '-' — increment and decrement instruction, respectively.
Each of the next $m$ lines contains two integers $l$ and $r$ ($1 \le l \le r \le n$) — the description of the query.
The sum of $n$ over all testcases doesn't exceed $2 \cdot 10^5$. The sum of $m$ over all testcases doesn't exceed $2 \cdot 10^5$.
-----Output-----
For each testcase print $m$ integers — for each query $l$, $r$ print the number of distinct values variable $x$ is assigned to if all the instructions between the $l$-th one and the $r$-th one inclusive are ignored and the rest are executed without changing the order.
-----Examples-----
Input
2
8 4
-+--+--+
1 8
2 8
2 5
1 1
4 10
+-++
1 1
1 2
2 2
1 3
2 3
3 3
1 4
2 4
3 4
4 4
Output
1
2
4
4
3
3
4
2
3
2
1
2
2
2
-----Note-----
The instructions that remain for each query of the first testcase are:
empty program — $x$ was only equal to $0$;
"-" — $x$ had values $0$ and $-1$;
"---+" — $x$ had values $0$, $-1$, $-2$, $-3$, $-2$ — there are $4$ distinct values among them;
"+--+--+" — the distinct values are $1$, $0$, $-1$, $-2$. | import sys
input = sys.stdin.readline
def main():
t = int(input())
for _ in range(t):
n, m = map(int, input().split())
mini = [0] * (n + 1)
maxi = [0] * (n + 1)
xi = [0] * (n + 1)
s = input()
x = 0
for i in range(1, n + 1):
if s[i - 1] == "-":
x -= 1
else:
x += 1
xi[i] = x
if i > 0:
mini[i] = min(mini[i - 1], x)
maxi[i] = max(maxi[i - 1], x)
x = 0
minif = [0] * (n + 1)
maxif = [0] * (n + 1)
xf = [0] * (n + 1)
for i in range(1, n + 1):
if s[n - (i - 1) - 1] == "+":
x -= 1
else:
x += 1
xf[i] = x
if i > 0:
minif[i] = min(minif[i - 1], x)
maxif[i] = max(maxif[i - 1], x)
for req in range(m):
l, r = map(int, input().split())
min1, max1, x1 = mini[l - 1], maxi[l - 1], xi[l - 1]
min2, max2 = minif[n - r] + x1 - xf[n - r], maxif[n - r] + x1 - xf[n - r]
sys.stdout.write(str(max(max1, max2) - min(min1, min2) + 1) + "\n")
main() | IMPORT ASSIGN VAR VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR BIN_OP VAR NUMBER STRING VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR IF VAR NUMBER 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 BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER STRING VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR IF VAR NUMBER 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 VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR VAR VAR VAR BIN_OP VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR VAR VAR VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR BIN_OP BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR NUMBER STRING EXPR FUNC_CALL VAR |
You are given a program that consists of $n$ instructions. Initially a single variable $x$ is assigned to $0$. Afterwards, the instructions are of two types:
increase $x$ by $1$;
decrease $x$ by $1$.
You are given $m$ queries of the following format:
query $l$ $r$ — how many distinct values is $x$ assigned to if all the instructions between the $l$-th one and the $r$-th one inclusive are ignored and the rest are executed without changing the order?
-----Input-----
The first line contains a single integer $t$ ($1 \le t \le 1000$) — the number of testcases.
Then the description of $t$ testcases follows.
The first line of each testcase contains two integers $n$ and $m$ ($1 \le n, m \le 2 \cdot 10^5$) — the number of instructions in the program and the number of queries.
The second line of each testcase contains a program — a string of $n$ characters: each character is either '+' or '-' — increment and decrement instruction, respectively.
Each of the next $m$ lines contains two integers $l$ and $r$ ($1 \le l \le r \le n$) — the description of the query.
The sum of $n$ over all testcases doesn't exceed $2 \cdot 10^5$. The sum of $m$ over all testcases doesn't exceed $2 \cdot 10^5$.
-----Output-----
For each testcase print $m$ integers — for each query $l$, $r$ print the number of distinct values variable $x$ is assigned to if all the instructions between the $l$-th one and the $r$-th one inclusive are ignored and the rest are executed without changing the order.
-----Examples-----
Input
2
8 4
-+--+--+
1 8
2 8
2 5
1 1
4 10
+-++
1 1
1 2
2 2
1 3
2 3
3 3
1 4
2 4
3 4
4 4
Output
1
2
4
4
3
3
4
2
3
2
1
2
2
2
-----Note-----
The instructions that remain for each query of the first testcase are:
empty program — $x$ was only equal to $0$;
"-" — $x$ had values $0$ and $-1$;
"---+" — $x$ had values $0$, $-1$, $-2$, $-3$, $-2$ — there are $4$ distinct values among them;
"+--+--+" — the distinct values are $1$, $0$, $-1$, $-2$. | T = int(input())
for t in range(T):
n, m = tuple([int(x) for x in input().split()])
s = input()
qs = []
for i in range(m):
qs.append(tuple([int(x) for x in input().split()]))
cur_num = 0
cur_low = 0
cur_high = 0
fwd = []
for op in s:
if op == "-":
cur_num -= 1
elif op == "+":
cur_num += 1
if cur_low > cur_num:
cur_low = cur_num
if cur_high < cur_num:
cur_high = cur_num
fwd.append((cur_num, cur_low, cur_high))
cur_low = 0
cur_high = 0
bwd = []
for op in s[::-1]:
if op == "-":
cur_low = cur_low - 1
cur_high = max(cur_high - 1, 0)
elif op == "+":
cur_low = min(cur_low + 1, 0)
cur_high = cur_high + 1
bwd.append((cur_low, cur_high))
bwd = bwd[::-1]
for l, r in qs:
forward_to = l - 1 - 1
backward_from = r + 1 - 1
cur_low = 0
cur_high = 0
cur_num = 0
if forward_to >= 0:
cur_num, cur_low, cur_high = fwd[forward_to]
if backward_from < len(bwd):
add_low, add_high = bwd[backward_from]
cur_low = min(cur_num + add_low, cur_low)
cur_high = max(cur_num + add_high, cur_high)
result = cur_high - cur_low + 1
print(result) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST FOR VAR VAR IF VAR STRING VAR NUMBER IF VAR STRING VAR NUMBER IF VAR VAR ASSIGN VAR VAR IF VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST FOR VAR VAR NUMBER IF VAR STRING ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER IF VAR STRING ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR NUMBER FOR VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR VAR VAR VAR IF VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR |
You are given a program that consists of $n$ instructions. Initially a single variable $x$ is assigned to $0$. Afterwards, the instructions are of two types:
increase $x$ by $1$;
decrease $x$ by $1$.
You are given $m$ queries of the following format:
query $l$ $r$ — how many distinct values is $x$ assigned to if all the instructions between the $l$-th one and the $r$-th one inclusive are ignored and the rest are executed without changing the order?
-----Input-----
The first line contains a single integer $t$ ($1 \le t \le 1000$) — the number of testcases.
Then the description of $t$ testcases follows.
The first line of each testcase contains two integers $n$ and $m$ ($1 \le n, m \le 2 \cdot 10^5$) — the number of instructions in the program and the number of queries.
The second line of each testcase contains a program — a string of $n$ characters: each character is either '+' or '-' — increment and decrement instruction, respectively.
Each of the next $m$ lines contains two integers $l$ and $r$ ($1 \le l \le r \le n$) — the description of the query.
The sum of $n$ over all testcases doesn't exceed $2 \cdot 10^5$. The sum of $m$ over all testcases doesn't exceed $2 \cdot 10^5$.
-----Output-----
For each testcase print $m$ integers — for each query $l$, $r$ print the number of distinct values variable $x$ is assigned to if all the instructions between the $l$-th one and the $r$-th one inclusive are ignored and the rest are executed without changing the order.
-----Examples-----
Input
2
8 4
-+--+--+
1 8
2 8
2 5
1 1
4 10
+-++
1 1
1 2
2 2
1 3
2 3
3 3
1 4
2 4
3 4
4 4
Output
1
2
4
4
3
3
4
2
3
2
1
2
2
2
-----Note-----
The instructions that remain for each query of the first testcase are:
empty program — $x$ was only equal to $0$;
"-" — $x$ had values $0$ and $-1$;
"---+" — $x$ had values $0$, $-1$, $-2$, $-3$, $-2$ — there are $4$ distinct values among them;
"+--+--+" — the distinct values are $1$, $0$, $-1$, $-2$. | import sys
def load_sys():
return sys.stdin.readlines()
def load_local():
with open("input.txt", "r") as f:
input = f.readlines()
return input
def solve(N, instructions, queries):
A = list(instructions.strip())
A = [(1 if x == "+" else -1) for x in A]
f = [[0, 0] for _ in range(N)]
g = [[0, 0] for _ in range(N)]
for i in range(N - 1, -1, -1):
g[i][0] = A[i] + max(0, g[i + 1][0] if i + 1 < N else 0)
g[i][1] = A[i] + min(0, g[i + 1][1] if i + 1 < N else 0)
for i in range(N):
A[i] += A[i - 1] if i - 1 >= 0 else 0
f[i][0] = max(A[i], f[i - 1][0] if i - 1 >= 0 else 0)
f[i][1] = min(A[i], f[i - 1][1] if i - 1 >= 0 else 0)
for i, j in queries:
i -= 1
j -= 1
mx1, mn1 = f[i - 1] if i - 1 >= 0 else [0, 0]
mx2, mn2 = g[j + 1] if j + 1 < N else [0, 0]
pref = A[i - 1] if i - 1 >= 0 else 0
res = max(mx1, pref + mx2) - min(mn1, pref + mn2) + 1
print(res)
input = load_sys()
i = 1
while i < len(input):
n, m = [int(x) for x in input[i].split()]
i += 1
instructions = input[i]
queries = []
for _ in range(m):
i += 1
queries.append([int(x) for x in input[i].split()])
i += 1
solve(n, instructions, queries) | IMPORT FUNC_DEF RETURN FUNC_CALL VAR FUNC_DEF FUNC_CALL VAR STRING STRING VAR ASSIGN VAR FUNC_CALL VAR RETURN VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR STRING NUMBER NUMBER VAR VAR ASSIGN VAR LIST NUMBER NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR LIST NUMBER NUMBER VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR NUMBER BIN_OP VAR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR NUMBER BIN_OP VAR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR NUMBER FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER NUMBER NUMBER FOR VAR VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER LIST NUMBER NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER LIST NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP FUNC_CALL VAR VAR BIN_OP VAR VAR FUNC_CALL VAR VAR BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR |
You are given a program that consists of $n$ instructions. Initially a single variable $x$ is assigned to $0$. Afterwards, the instructions are of two types:
increase $x$ by $1$;
decrease $x$ by $1$.
You are given $m$ queries of the following format:
query $l$ $r$ — how many distinct values is $x$ assigned to if all the instructions between the $l$-th one and the $r$-th one inclusive are ignored and the rest are executed without changing the order?
-----Input-----
The first line contains a single integer $t$ ($1 \le t \le 1000$) — the number of testcases.
Then the description of $t$ testcases follows.
The first line of each testcase contains two integers $n$ and $m$ ($1 \le n, m \le 2 \cdot 10^5$) — the number of instructions in the program and the number of queries.
The second line of each testcase contains a program — a string of $n$ characters: each character is either '+' or '-' — increment and decrement instruction, respectively.
Each of the next $m$ lines contains two integers $l$ and $r$ ($1 \le l \le r \le n$) — the description of the query.
The sum of $n$ over all testcases doesn't exceed $2 \cdot 10^5$. The sum of $m$ over all testcases doesn't exceed $2 \cdot 10^5$.
-----Output-----
For each testcase print $m$ integers — for each query $l$, $r$ print the number of distinct values variable $x$ is assigned to if all the instructions between the $l$-th one and the $r$-th one inclusive are ignored and the rest are executed without changing the order.
-----Examples-----
Input
2
8 4
-+--+--+
1 8
2 8
2 5
1 1
4 10
+-++
1 1
1 2
2 2
1 3
2 3
3 3
1 4
2 4
3 4
4 4
Output
1
2
4
4
3
3
4
2
3
2
1
2
2
2
-----Note-----
The instructions that remain for each query of the first testcase are:
empty program — $x$ was only equal to $0$;
"-" — $x$ had values $0$ and $-1$;
"---+" — $x$ had values $0$, $-1$, $-2$, $-3$, $-2$ — there are $4$ distinct values among them;
"+--+--+" — the distinct values are $1$, $0$, $-1$, $-2$. | for _ in range(int(input())):
n, m = list(map(int, input().split()))
s = input()
query = []
for i in range(m):
query.append(list(map(int, input().split())))
pre, suff = [[] for i in range(n)], [[] for i in range(n)]
values = []
cur, mn, mex = 0, 0, 0
for i in range(n):
if s[i] == "+":
cur += 1
else:
cur -= 1
values.append(cur)
mn, mex = min(mn, cur), max(mex, cur)
pre[i] = [mn, mex]
cur, mn, mex = 0, 0, 0
for i in range(n - 1, -1, -1):
if s[i] == "+":
mex += 1
mn += 1
else:
mex -= 1
mn -= 1
mn, mex = min(mn, cur), max(mex, cur)
suff[i] = [mn, mex]
for i in query:
l, r = i
l -= 1
r -= 1
mn1, mex1 = 0, 0
mn2, mex2 = 0, 0
cur = 0
if l - 1 >= 0:
mn1 = pre[l - 1][0]
mex1 = pre[l - 1][1]
cur = values[l - 1]
if r + 1 < n:
mn2 = cur + suff[r + 1][0]
mex2 = cur + suff[r + 1][1]
mn = min(mn1, mn2)
mex = max(mex1, mex2)
print(mex - mn + 1) | 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 ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR LIST VAR FUNC_CALL VAR VAR LIST VAR FUNC_CALL VAR VAR ASSIGN VAR LIST ASSIGN VAR VAR VAR NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR STRING VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR LIST VAR VAR ASSIGN VAR VAR VAR NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR VAR STRING VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR LIST VAR VAR FOR VAR VAR ASSIGN VAR VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER IF BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.