description
stringlengths
171
4k
code
stringlengths
94
3.98k
normalized_code
stringlengths
57
4.99k
You are playing a computer game, where you lead a party of $m$ soldiers. Each soldier is characterised by his agility $a_i$. The level you are trying to get through can be represented as a straight line segment from point $0$ (where you and your squad is initially located) to point $n + 1$ (where the boss is located). The level is filled with $k$ traps. Each trap is represented by three numbers $l_i$, $r_i$ and $d_i$. $l_i$ is the location of the trap, and $d_i$ is the danger level of the trap: whenever a soldier with agility lower than $d_i$ steps on a trap (that is, moves to the point $l_i$), he gets instantly killed. Fortunately, you can disarm traps: if you move to the point $r_i$, you disarm this trap, and it no longer poses any danger to your soldiers. Traps don't affect you, only your soldiers. You have $t$ seconds to complete the level β€” that is, to bring some soldiers from your squad to the boss. Before the level starts, you choose which soldiers will be coming with you, and which soldiers won't be. After that, you have to bring all of the chosen soldiers to the boss. To do so, you may perform the following actions: if your location is $x$, you may move to $x + 1$ or $x - 1$. This action consumes one second; if your location is $x$ and the location of your squad is $x$, you may move to $x + 1$ or to $x - 1$ with your squad in one second. You may not perform this action if it puts some soldier in danger (i. e. the point your squad is moving into contains a non-disarmed trap with $d_i$ greater than agility of some soldier from the squad). This action consumes one second; if your location is $x$ and there is a trap $i$ with $r_i = x$, you may disarm this trap. This action is done instantly (it consumes no time). Note that after each action both your coordinate and the coordinate of your squad should be integers. You have to choose the maximum number of soldiers such that they all can be brought from the point $0$ to the point $n + 1$ (where the boss waits) in no more than $t$ seconds. -----Input----- The first line contains four integers $m$, $n$, $k$ and $t$ ($1 \le m, n, k, t \le 2 \cdot 10^5$, $n < t$) β€” the number of soldiers, the number of integer points between the squad and the boss, the number of traps and the maximum number of seconds you may spend to bring the squad to the boss, respectively. The second line contains $m$ integers $a_1$, $a_2$, ..., $a_m$ ($1 \le a_i \le 2 \cdot 10^5$), where $a_i$ is the agility of the $i$-th soldier. Then $k$ lines follow, containing the descriptions of traps. Each line contains three numbers $l_i$, $r_i$ and $d_i$ ($1 \le l_i \le r_i \le n$, $1 \le d_i \le 2 \cdot 10^5$) β€” the location of the trap, the location where the trap can be disarmed, and its danger level, respectively. -----Output----- Print one integer β€” the maximum number of soldiers you may choose so that you may bring them all to the boss in no more than $t$ seconds. -----Example----- Input 5 6 4 14 1 2 3 4 5 1 5 2 1 2 5 2 3 5 3 5 3 Output 3 -----Note----- In the first example you may take soldiers with agility $3$, $4$ and $5$ with you. The course of action is as follows: go to $2$ without your squad; disarm the trap $2$; go to $3$ without your squad; disartm the trap $3$; go to $0$ without your squad; go to $7$ with your squad. The whole plan can be executed in $13$ seconds.
import sys zz = 1 sys.setrecursionlimit(10**5) if zz: input = sys.stdin.readline else: sys.stdin = open("input.txt", "r") sys.stdout = open("all.txt", "w") di = [[-1, 0], [1, 0], [0, 1], [0, -1]] def fori(n): return [fi() for i in range(n)] def inc(d, c, x=1): d[c] = d[c] + x if c in d else x def ii(): return input().rstrip() def li(): return [int(xx) for xx in input().split()] def fli(): return [float(x) for x in input().split()] def comp(a, b): if a > b: return 2 return 2 if a == b else 0 def gi(): return [xx for xx in input().split()] def gtc(tc, ans): print("Case #" + str(tc) + ":", ans) def cil(n, m): return n // m + int(n % m > 0) def fi(): return int(input()) def pro(a): return reduce(lambda a, b: a * b, a) def swap(a, i, j): a[i], a[j] = a[j], a[i] def si(): return list(input().rstrip()) def mi(): return map(int, input().split()) def gh(): sys.stdout.flush() def isvalid(i, j, n, m): return 0 <= i < n and 0 <= j < m def bo(i): return ord(i) - ord("a") def graph(n, m): for i in range(m): x, y = mi() a[x].append(y) a[y].append(x) t = 1 uu = t mini = 10**18 def can(mid): ans = 0 l = r = 0 for i in range(k): if a[i][2] <= mid: continue if a[i][0] > r: ans += (r - l) * 3 + (a[i][0] - 1 - r) l = a[i][0] - 1 r = a[i][1] else: r = max(r, a[i][1]) ans += (r - l) * 3 + (n + 1 - r) return ans <= T while t > 0: t -= 1 m, n, k, T = mi() a = [] b = li() b.sort(reverse=True) for i in range(k): l, r, d = mi() a.append([l, r, d]) a.sort() l = ans = 0 r = m - 1 while l <= r: mid = (l + r) // 2 if can(b[mid]): ans = mid + 1 l = mid + 1 else: r = mid - 1 print(ans)
IMPORT ASSIGN VAR NUMBER EXPR FUNC_CALL VAR BIN_OP NUMBER NUMBER IF VAR ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR STRING STRING ASSIGN VAR FUNC_CALL VAR STRING STRING ASSIGN VAR LIST LIST NUMBER NUMBER LIST NUMBER NUMBER LIST NUMBER NUMBER LIST NUMBER NUMBER FUNC_DEF RETURN FUNC_CALL VAR VAR FUNC_CALL VAR VAR FUNC_DEF NUMBER ASSIGN VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR FUNC_DEF RETURN FUNC_CALL FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF IF VAR VAR RETURN NUMBER RETURN VAR VAR NUMBER NUMBER FUNC_DEF RETURN VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF EXPR FUNC_CALL VAR BIN_OP BIN_OP STRING FUNC_CALL VAR VAR STRING VAR FUNC_DEF RETURN BIN_OP BIN_OP VAR VAR FUNC_CALL VAR BIN_OP VAR VAR NUMBER FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR BIN_OP VAR VAR VAR FUNC_DEF ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF EXPR FUNC_CALL VAR FUNC_DEF RETURN NUMBER VAR VAR NUMBER VAR VAR FUNC_DEF RETURN BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR STRING FUNC_DEF FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR ASSIGN VAR BIN_OP NUMBER NUMBER FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER VAR IF VAR VAR NUMBER VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER NUMBER VAR ASSIGN VAR BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR NUMBER VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER BIN_OP BIN_OP VAR NUMBER VAR RETURN VAR VAR WHILE VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR EXPR FUNC_CALL VAR LIST VAR VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR
You are playing a computer game, where you lead a party of $m$ soldiers. Each soldier is characterised by his agility $a_i$. The level you are trying to get through can be represented as a straight line segment from point $0$ (where you and your squad is initially located) to point $n + 1$ (where the boss is located). The level is filled with $k$ traps. Each trap is represented by three numbers $l_i$, $r_i$ and $d_i$. $l_i$ is the location of the trap, and $d_i$ is the danger level of the trap: whenever a soldier with agility lower than $d_i$ steps on a trap (that is, moves to the point $l_i$), he gets instantly killed. Fortunately, you can disarm traps: if you move to the point $r_i$, you disarm this trap, and it no longer poses any danger to your soldiers. Traps don't affect you, only your soldiers. You have $t$ seconds to complete the level β€” that is, to bring some soldiers from your squad to the boss. Before the level starts, you choose which soldiers will be coming with you, and which soldiers won't be. After that, you have to bring all of the chosen soldiers to the boss. To do so, you may perform the following actions: if your location is $x$, you may move to $x + 1$ or $x - 1$. This action consumes one second; if your location is $x$ and the location of your squad is $x$, you may move to $x + 1$ or to $x - 1$ with your squad in one second. You may not perform this action if it puts some soldier in danger (i. e. the point your squad is moving into contains a non-disarmed trap with $d_i$ greater than agility of some soldier from the squad). This action consumes one second; if your location is $x$ and there is a trap $i$ with $r_i = x$, you may disarm this trap. This action is done instantly (it consumes no time). Note that after each action both your coordinate and the coordinate of your squad should be integers. You have to choose the maximum number of soldiers such that they all can be brought from the point $0$ to the point $n + 1$ (where the boss waits) in no more than $t$ seconds. -----Input----- The first line contains four integers $m$, $n$, $k$ and $t$ ($1 \le m, n, k, t \le 2 \cdot 10^5$, $n < t$) β€” the number of soldiers, the number of integer points between the squad and the boss, the number of traps and the maximum number of seconds you may spend to bring the squad to the boss, respectively. The second line contains $m$ integers $a_1$, $a_2$, ..., $a_m$ ($1 \le a_i \le 2 \cdot 10^5$), where $a_i$ is the agility of the $i$-th soldier. Then $k$ lines follow, containing the descriptions of traps. Each line contains three numbers $l_i$, $r_i$ and $d_i$ ($1 \le l_i \le r_i \le n$, $1 \le d_i \le 2 \cdot 10^5$) β€” the location of the trap, the location where the trap can be disarmed, and its danger level, respectively. -----Output----- Print one integer β€” the maximum number of soldiers you may choose so that you may bring them all to the boss in no more than $t$ seconds. -----Example----- Input 5 6 4 14 1 2 3 4 5 1 5 2 1 2 5 2 3 5 3 5 3 Output 3 -----Note----- In the first example you may take soldiers with agility $3$, $4$ and $5$ with you. The course of action is as follows: go to $2$ without your squad; disarm the trap $2$; go to $3$ without your squad; disartm the trap $3$; go to $0$ without your squad; go to $7$ with your squad. The whole plan can be executed in $13$ seconds.
m, n, k, t = list(map(int, input().split())) a = [0] + sorted(list(map(int, input().split()))) + [3 * 10**5] data = sorted([list(map(int, input().split())) for _ in range(k)]) ok = m + 1 ng = 0 while ok - ng > 1: mid = (ok + ng) // 2 A = a[mid] interval_set = [] for i in range(k): if data[i][2] <= A: continue if len(interval_set) == 0: interval_set.append([data[i][0], data[i][1]]) elif interval_set[-1][1] < data[i][0]: interval_set.append([data[i][0], data[i][1]]) elif interval_set[-1][1] < data[i][1]: new_interval = [interval_set.pop()[0], data[i][1]] interval_set.append(new_interval) time = n + 1 for interval in interval_set: time += (interval[1] - interval[0] + 1) * 2 if time <= t: ok = mid else: ng = mid print(m - ok + 1)
ASSIGN VAR VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP BIN_OP LIST NUMBER FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR LIST BIN_OP NUMBER BIN_OP NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER WHILE BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER VAR IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR LIST VAR VAR NUMBER VAR VAR NUMBER IF VAR NUMBER NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR LIST VAR VAR NUMBER VAR VAR NUMBER IF VAR NUMBER NUMBER VAR VAR NUMBER ASSIGN VAR LIST FUNC_CALL VAR NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER FOR VAR VAR VAR BIN_OP BIN_OP BIN_OP VAR NUMBER VAR NUMBER NUMBER NUMBER IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER
You are playing a computer game, where you lead a party of $m$ soldiers. Each soldier is characterised by his agility $a_i$. The level you are trying to get through can be represented as a straight line segment from point $0$ (where you and your squad is initially located) to point $n + 1$ (where the boss is located). The level is filled with $k$ traps. Each trap is represented by three numbers $l_i$, $r_i$ and $d_i$. $l_i$ is the location of the trap, and $d_i$ is the danger level of the trap: whenever a soldier with agility lower than $d_i$ steps on a trap (that is, moves to the point $l_i$), he gets instantly killed. Fortunately, you can disarm traps: if you move to the point $r_i$, you disarm this trap, and it no longer poses any danger to your soldiers. Traps don't affect you, only your soldiers. You have $t$ seconds to complete the level β€” that is, to bring some soldiers from your squad to the boss. Before the level starts, you choose which soldiers will be coming with you, and which soldiers won't be. After that, you have to bring all of the chosen soldiers to the boss. To do so, you may perform the following actions: if your location is $x$, you may move to $x + 1$ or $x - 1$. This action consumes one second; if your location is $x$ and the location of your squad is $x$, you may move to $x + 1$ or to $x - 1$ with your squad in one second. You may not perform this action if it puts some soldier in danger (i. e. the point your squad is moving into contains a non-disarmed trap with $d_i$ greater than agility of some soldier from the squad). This action consumes one second; if your location is $x$ and there is a trap $i$ with $r_i = x$, you may disarm this trap. This action is done instantly (it consumes no time). Note that after each action both your coordinate and the coordinate of your squad should be integers. You have to choose the maximum number of soldiers such that they all can be brought from the point $0$ to the point $n + 1$ (where the boss waits) in no more than $t$ seconds. -----Input----- The first line contains four integers $m$, $n$, $k$ and $t$ ($1 \le m, n, k, t \le 2 \cdot 10^5$, $n < t$) β€” the number of soldiers, the number of integer points between the squad and the boss, the number of traps and the maximum number of seconds you may spend to bring the squad to the boss, respectively. The second line contains $m$ integers $a_1$, $a_2$, ..., $a_m$ ($1 \le a_i \le 2 \cdot 10^5$), where $a_i$ is the agility of the $i$-th soldier. Then $k$ lines follow, containing the descriptions of traps. Each line contains three numbers $l_i$, $r_i$ and $d_i$ ($1 \le l_i \le r_i \le n$, $1 \le d_i \le 2 \cdot 10^5$) β€” the location of the trap, the location where the trap can be disarmed, and its danger level, respectively. -----Output----- Print one integer β€” the maximum number of soldiers you may choose so that you may bring them all to the boss in no more than $t$ seconds. -----Example----- Input 5 6 4 14 1 2 3 4 5 1 5 2 1 2 5 2 3 5 3 5 3 Output 3 -----Note----- In the first example you may take soldiers with agility $3$, $4$ and $5$ with you. The course of action is as follows: go to $2$ without your squad; disarm the trap $2$; go to $3$ without your squad; disartm the trap $3$; go to $0$ without your squad; go to $7$ with your squad. The whole plan can be executed in $13$ seconds.
from sys import stdin input = stdin.readline def works(mid): ag = A[-mid] MAX = 0 cur = 0 out = N + 1 for l, r, d in traps: if d <= ag: continue if l > MAX: out += (MAX - cur) * 2 MAX, cur = r, l - 1 else: MAX = max(MAX, r) return out <= time S, N, T, time = map(int, input().split()) A = list(map(int, input().split())) traps = [tuple(map(int, input().split())) for _ in range(T)] traps.sort() traps.append((N + 2, N + 5, 10**6)) A.sort() lo = 0 hi = S + 1 while hi - lo > 1: mid = (hi + lo) // 2 if works(mid): lo = mid else: hi = mid print(lo)
ASSIGN VAR VAR FUNC_DEF ASSIGN VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER FOR VAR VAR VAR VAR IF VAR VAR IF VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN VAR VAR ASSIGN VAR VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP NUMBER NUMBER EXPR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER WHILE BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR
You are playing a computer game, where you lead a party of $m$ soldiers. Each soldier is characterised by his agility $a_i$. The level you are trying to get through can be represented as a straight line segment from point $0$ (where you and your squad is initially located) to point $n + 1$ (where the boss is located). The level is filled with $k$ traps. Each trap is represented by three numbers $l_i$, $r_i$ and $d_i$. $l_i$ is the location of the trap, and $d_i$ is the danger level of the trap: whenever a soldier with agility lower than $d_i$ steps on a trap (that is, moves to the point $l_i$), he gets instantly killed. Fortunately, you can disarm traps: if you move to the point $r_i$, you disarm this trap, and it no longer poses any danger to your soldiers. Traps don't affect you, only your soldiers. You have $t$ seconds to complete the level β€” that is, to bring some soldiers from your squad to the boss. Before the level starts, you choose which soldiers will be coming with you, and which soldiers won't be. After that, you have to bring all of the chosen soldiers to the boss. To do so, you may perform the following actions: if your location is $x$, you may move to $x + 1$ or $x - 1$. This action consumes one second; if your location is $x$ and the location of your squad is $x$, you may move to $x + 1$ or to $x - 1$ with your squad in one second. You may not perform this action if it puts some soldier in danger (i. e. the point your squad is moving into contains a non-disarmed trap with $d_i$ greater than agility of some soldier from the squad). This action consumes one second; if your location is $x$ and there is a trap $i$ with $r_i = x$, you may disarm this trap. This action is done instantly (it consumes no time). Note that after each action both your coordinate and the coordinate of your squad should be integers. You have to choose the maximum number of soldiers such that they all can be brought from the point $0$ to the point $n + 1$ (where the boss waits) in no more than $t$ seconds. -----Input----- The first line contains four integers $m$, $n$, $k$ and $t$ ($1 \le m, n, k, t \le 2 \cdot 10^5$, $n < t$) β€” the number of soldiers, the number of integer points between the squad and the boss, the number of traps and the maximum number of seconds you may spend to bring the squad to the boss, respectively. The second line contains $m$ integers $a_1$, $a_2$, ..., $a_m$ ($1 \le a_i \le 2 \cdot 10^5$), where $a_i$ is the agility of the $i$-th soldier. Then $k$ lines follow, containing the descriptions of traps. Each line contains three numbers $l_i$, $r_i$ and $d_i$ ($1 \le l_i \le r_i \le n$, $1 \le d_i \le 2 \cdot 10^5$) β€” the location of the trap, the location where the trap can be disarmed, and its danger level, respectively. -----Output----- Print one integer β€” the maximum number of soldiers you may choose so that you may bring them all to the boss in no more than $t$ seconds. -----Example----- Input 5 6 4 14 1 2 3 4 5 1 5 2 1 2 5 2 3 5 3 5 3 Output 3 -----Note----- In the first example you may take soldiers with agility $3$, $4$ and $5$ with you. The course of action is as follows: go to $2$ without your squad; disarm the trap $2$; go to $3$ without your squad; disartm the trap $3$; go to $0$ without your squad; go to $7$ with your squad. The whole plan can be executed in $13$ seconds.
import sys readline = sys.stdin.readline readlines = sys.stdin.readlines ns = lambda: readline().rstrip() ni = lambda: int(readline().rstrip()) nm = lambda: map(int, readline().split()) nl = lambda: list(map(int, readline().split())) prn = lambda x: print(*x, sep="\n") def gcd(u, v): while v: u, v = v, u % v return u def solve(): m, n, k, t = nm() a = nl() trap = [tuple(nm()) for _ in range(k)] idx = 0 trap.sort() ok = max(a) + 1 ng = -1 while ok - ng > 1: mid = (ok + ng) // 2 imos = [0] * (n + 1) ctp = list() for l, r, d in trap: if d <= mid: continue if not ctp or ctp[-1][1] < l: ctp.append([l, r]) else: ctp[-1][1] = max(ctp[-1][1], r) cost = n + 1 for l, r in ctp: cost += (r - l + 1) * 2 if cost > t: ng = mid else: ok = mid print(sum(1 for x in a if x >= ok)) return solve()
IMPORT ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR STRING FUNC_DEF WHILE VAR ASSIGN VAR VAR VAR BIN_OP VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR VAR VAR VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER WHILE BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR FOR VAR VAR VAR VAR IF VAR VAR IF VAR VAR NUMBER NUMBER VAR EXPR FUNC_CALL VAR LIST VAR VAR ASSIGN VAR NUMBER NUMBER FUNC_CALL VAR VAR NUMBER NUMBER VAR ASSIGN VAR BIN_OP VAR NUMBER FOR VAR VAR VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER NUMBER IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR NUMBER VAR VAR VAR VAR RETURN EXPR FUNC_CALL VAR
You are playing a computer game, where you lead a party of $m$ soldiers. Each soldier is characterised by his agility $a_i$. The level you are trying to get through can be represented as a straight line segment from point $0$ (where you and your squad is initially located) to point $n + 1$ (where the boss is located). The level is filled with $k$ traps. Each trap is represented by three numbers $l_i$, $r_i$ and $d_i$. $l_i$ is the location of the trap, and $d_i$ is the danger level of the trap: whenever a soldier with agility lower than $d_i$ steps on a trap (that is, moves to the point $l_i$), he gets instantly killed. Fortunately, you can disarm traps: if you move to the point $r_i$, you disarm this trap, and it no longer poses any danger to your soldiers. Traps don't affect you, only your soldiers. You have $t$ seconds to complete the level β€” that is, to bring some soldiers from your squad to the boss. Before the level starts, you choose which soldiers will be coming with you, and which soldiers won't be. After that, you have to bring all of the chosen soldiers to the boss. To do so, you may perform the following actions: if your location is $x$, you may move to $x + 1$ or $x - 1$. This action consumes one second; if your location is $x$ and the location of your squad is $x$, you may move to $x + 1$ or to $x - 1$ with your squad in one second. You may not perform this action if it puts some soldier in danger (i. e. the point your squad is moving into contains a non-disarmed trap with $d_i$ greater than agility of some soldier from the squad). This action consumes one second; if your location is $x$ and there is a trap $i$ with $r_i = x$, you may disarm this trap. This action is done instantly (it consumes no time). Note that after each action both your coordinate and the coordinate of your squad should be integers. You have to choose the maximum number of soldiers such that they all can be brought from the point $0$ to the point $n + 1$ (where the boss waits) in no more than $t$ seconds. -----Input----- The first line contains four integers $m$, $n$, $k$ and $t$ ($1 \le m, n, k, t \le 2 \cdot 10^5$, $n < t$) β€” the number of soldiers, the number of integer points between the squad and the boss, the number of traps and the maximum number of seconds you may spend to bring the squad to the boss, respectively. The second line contains $m$ integers $a_1$, $a_2$, ..., $a_m$ ($1 \le a_i \le 2 \cdot 10^5$), where $a_i$ is the agility of the $i$-th soldier. Then $k$ lines follow, containing the descriptions of traps. Each line contains three numbers $l_i$, $r_i$ and $d_i$ ($1 \le l_i \le r_i \le n$, $1 \le d_i \le 2 \cdot 10^5$) β€” the location of the trap, the location where the trap can be disarmed, and its danger level, respectively. -----Output----- Print one integer β€” the maximum number of soldiers you may choose so that you may bring them all to the boss in no more than $t$ seconds. -----Example----- Input 5 6 4 14 1 2 3 4 5 1 5 2 1 2 5 2 3 5 3 5 3 Output 3 -----Note----- In the first example you may take soldiers with agility $3$, $4$ and $5$ with you. The course of action is as follows: go to $2$ without your squad; disarm the trap $2$; go to $3$ without your squad; disartm the trap $3$; go to $0$ without your squad; go to $7$ with your squad. The whole plan can be executed in $13$ seconds.
import sys input = lambda: sys.stdin.readline().rstrip() def chk(a): Y = [0] * (N + 10) for l, r, d in X: if d <= a: continue Y[l - 1] += 1 Y[r] -= 1 for i in range(1, N + 10): Y[i] += Y[i - 1] return 1 if sum([min(Y[i], 1) for i in range(N + 5)]) * 2 + N + 1 <= T else 0 M, N, K, T = map(int, input().split()) A = [int(a) for a in input().split()] X = [] for _ in range(K): l, r, d = map(int, input().split()) X.append((l, r, d)) l, r = 0, 2 * 10**5 while r - l > 1: m = (l + r) // 2 if chk(m): r = m else: l = m print(len([a for a in A if a >= r]))
IMPORT ASSIGN VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FOR VAR VAR VAR VAR IF VAR VAR VAR BIN_OP VAR NUMBER NUMBER VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER RETURN BIN_OP BIN_OP BIN_OP FUNC_CALL VAR FUNC_CALL VAR VAR VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER VAR NUMBER NUMBER ASSIGN VAR VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR NUMBER BIN_OP NUMBER BIN_OP NUMBER NUMBER WHILE BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR VAR
You are playing a computer game, where you lead a party of $m$ soldiers. Each soldier is characterised by his agility $a_i$. The level you are trying to get through can be represented as a straight line segment from point $0$ (where you and your squad is initially located) to point $n + 1$ (where the boss is located). The level is filled with $k$ traps. Each trap is represented by three numbers $l_i$, $r_i$ and $d_i$. $l_i$ is the location of the trap, and $d_i$ is the danger level of the trap: whenever a soldier with agility lower than $d_i$ steps on a trap (that is, moves to the point $l_i$), he gets instantly killed. Fortunately, you can disarm traps: if you move to the point $r_i$, you disarm this trap, and it no longer poses any danger to your soldiers. Traps don't affect you, only your soldiers. You have $t$ seconds to complete the level β€” that is, to bring some soldiers from your squad to the boss. Before the level starts, you choose which soldiers will be coming with you, and which soldiers won't be. After that, you have to bring all of the chosen soldiers to the boss. To do so, you may perform the following actions: if your location is $x$, you may move to $x + 1$ or $x - 1$. This action consumes one second; if your location is $x$ and the location of your squad is $x$, you may move to $x + 1$ or to $x - 1$ with your squad in one second. You may not perform this action if it puts some soldier in danger (i. e. the point your squad is moving into contains a non-disarmed trap with $d_i$ greater than agility of some soldier from the squad). This action consumes one second; if your location is $x$ and there is a trap $i$ with $r_i = x$, you may disarm this trap. This action is done instantly (it consumes no time). Note that after each action both your coordinate and the coordinate of your squad should be integers. You have to choose the maximum number of soldiers such that they all can be brought from the point $0$ to the point $n + 1$ (where the boss waits) in no more than $t$ seconds. -----Input----- The first line contains four integers $m$, $n$, $k$ and $t$ ($1 \le m, n, k, t \le 2 \cdot 10^5$, $n < t$) β€” the number of soldiers, the number of integer points between the squad and the boss, the number of traps and the maximum number of seconds you may spend to bring the squad to the boss, respectively. The second line contains $m$ integers $a_1$, $a_2$, ..., $a_m$ ($1 \le a_i \le 2 \cdot 10^5$), where $a_i$ is the agility of the $i$-th soldier. Then $k$ lines follow, containing the descriptions of traps. Each line contains three numbers $l_i$, $r_i$ and $d_i$ ($1 \le l_i \le r_i \le n$, $1 \le d_i \le 2 \cdot 10^5$) β€” the location of the trap, the location where the trap can be disarmed, and its danger level, respectively. -----Output----- Print one integer β€” the maximum number of soldiers you may choose so that you may bring them all to the boss in no more than $t$ seconds. -----Example----- Input 5 6 4 14 1 2 3 4 5 1 5 2 1 2 5 2 3 5 3 5 3 Output 3 -----Note----- In the first example you may take soldiers with agility $3$, $4$ and $5$ with you. The course of action is as follows: go to $2$ without your squad; disarm the trap $2$; go to $3$ without your squad; disartm the trap $3$; go to $0$ without your squad; go to $7$ with your squad. The whole plan can be executed in $13$ seconds.
import sys input = sys.stdin.readline def inter(a, b): if b[0] <= a[1]: return 1 return 0 def check(le): if le == 0: return 1 mi = it[le - 1] tr = [i[:2] for i in aaa if i[2] > mi] if tr == []: return 1 aa = [tr[0][:]] for i in range(1, len(tr)): if inter(aa[-1], tr[i]): aa[-1][1] = max(aa[-1][1], tr[i][1]) else: aa.append(tr[i][:]) tt = 0 a = 0 for i in range(len(aa)): tt += aa[i][0] - 1 - a a = aa[i][0] - 1 tt += 3 * (aa[i][1] - aa[i][0] + 1) a = aa[i][1] tt += n + 1 - a return tt <= t m, n, k, t = map(int, input().split()) it = list(map(int, input().split())) it.sort(reverse=True) aaa = [] for i in range(k): l, r, d = map(int, input().split()) aaa.append([l, r, d]) aaa.sort() low = 0 high = m while low < high - 1: mid = (low + high) // 2 a = check(mid) if a: low = mid else: high = mid - 1 for i in range(high, low - 1, -1): if check(i): ans = i break print(ans)
IMPORT ASSIGN VAR VAR FUNC_DEF IF VAR NUMBER VAR NUMBER RETURN NUMBER RETURN NUMBER FUNC_DEF IF VAR NUMBER RETURN NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER VAR VAR VAR NUMBER VAR IF VAR LIST RETURN NUMBER ASSIGN VAR LIST VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR NUMBER VAR VAR ASSIGN VAR NUMBER NUMBER FUNC_CALL VAR VAR NUMBER NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER NUMBER VAR ASSIGN VAR BIN_OP VAR VAR NUMBER NUMBER VAR BIN_OP NUMBER BIN_OP BIN_OP VAR VAR NUMBER VAR VAR NUMBER NUMBER ASSIGN VAR VAR VAR NUMBER VAR BIN_OP BIN_OP VAR NUMBER VAR RETURN VAR VAR ASSIGN VAR VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR LIST VAR VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR VAR WHILE VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR IF VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER IF FUNC_CALL VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR
You are playing a computer game, where you lead a party of $m$ soldiers. Each soldier is characterised by his agility $a_i$. The level you are trying to get through can be represented as a straight line segment from point $0$ (where you and your squad is initially located) to point $n + 1$ (where the boss is located). The level is filled with $k$ traps. Each trap is represented by three numbers $l_i$, $r_i$ and $d_i$. $l_i$ is the location of the trap, and $d_i$ is the danger level of the trap: whenever a soldier with agility lower than $d_i$ steps on a trap (that is, moves to the point $l_i$), he gets instantly killed. Fortunately, you can disarm traps: if you move to the point $r_i$, you disarm this trap, and it no longer poses any danger to your soldiers. Traps don't affect you, only your soldiers. You have $t$ seconds to complete the level β€” that is, to bring some soldiers from your squad to the boss. Before the level starts, you choose which soldiers will be coming with you, and which soldiers won't be. After that, you have to bring all of the chosen soldiers to the boss. To do so, you may perform the following actions: if your location is $x$, you may move to $x + 1$ or $x - 1$. This action consumes one second; if your location is $x$ and the location of your squad is $x$, you may move to $x + 1$ or to $x - 1$ with your squad in one second. You may not perform this action if it puts some soldier in danger (i. e. the point your squad is moving into contains a non-disarmed trap with $d_i$ greater than agility of some soldier from the squad). This action consumes one second; if your location is $x$ and there is a trap $i$ with $r_i = x$, you may disarm this trap. This action is done instantly (it consumes no time). Note that after each action both your coordinate and the coordinate of your squad should be integers. You have to choose the maximum number of soldiers such that they all can be brought from the point $0$ to the point $n + 1$ (where the boss waits) in no more than $t$ seconds. -----Input----- The first line contains four integers $m$, $n$, $k$ and $t$ ($1 \le m, n, k, t \le 2 \cdot 10^5$, $n < t$) β€” the number of soldiers, the number of integer points between the squad and the boss, the number of traps and the maximum number of seconds you may spend to bring the squad to the boss, respectively. The second line contains $m$ integers $a_1$, $a_2$, ..., $a_m$ ($1 \le a_i \le 2 \cdot 10^5$), where $a_i$ is the agility of the $i$-th soldier. Then $k$ lines follow, containing the descriptions of traps. Each line contains three numbers $l_i$, $r_i$ and $d_i$ ($1 \le l_i \le r_i \le n$, $1 \le d_i \le 2 \cdot 10^5$) β€” the location of the trap, the location where the trap can be disarmed, and its danger level, respectively. -----Output----- Print one integer β€” the maximum number of soldiers you may choose so that you may bring them all to the boss in no more than $t$ seconds. -----Example----- Input 5 6 4 14 1 2 3 4 5 1 5 2 1 2 5 2 3 5 3 5 3 Output 3 -----Note----- In the first example you may take soldiers with agility $3$, $4$ and $5$ with you. The course of action is as follows: go to $2$ without your squad; disarm the trap $2$; go to $3$ without your squad; disartm the trap $3$; go to $0$ without your squad; go to $7$ with your squad. The whole plan can be executed in $13$ seconds.
import sys input = sys.stdin.readline m, n, k, t = list(map(int, input().split())) A = list(map(int, input().split())) T = [tuple(map(int, input().split())) for i in range(k)] NG = 0 OK = 2 * 10**5 + 1 while OK - NG > 1: mid = (OK + NG) // 2 USE = [0] * (2 * 10**5 + 3) for l, r, d in T: if d >= mid: USE[l - 1] += 1 USE[r] -= 1 for i in range(1, 2 * 10**5 + 3): USE[i] += USE[i - 1] if (2 * 10**5 + 3 - USE.count(0)) * 2 + n + 1 <= t: OK = mid else: NG = mid ANS = 0 for a in A: if a >= OK - 1: ANS += 1 print(ANS)
IMPORT ASSIGN VAR VAR ASSIGN VAR VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP BIN_OP NUMBER BIN_OP NUMBER NUMBER NUMBER WHILE BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP BIN_OP NUMBER BIN_OP NUMBER NUMBER NUMBER FOR VAR VAR VAR VAR IF VAR VAR VAR BIN_OP VAR NUMBER NUMBER VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP BIN_OP NUMBER BIN_OP NUMBER NUMBER NUMBER VAR VAR VAR BIN_OP VAR NUMBER IF BIN_OP BIN_OP BIN_OP BIN_OP BIN_OP BIN_OP NUMBER BIN_OP NUMBER NUMBER NUMBER FUNC_CALL VAR NUMBER NUMBER VAR NUMBER VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR NUMBER FOR VAR VAR IF VAR BIN_OP VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR
You are playing a computer game, where you lead a party of $m$ soldiers. Each soldier is characterised by his agility $a_i$. The level you are trying to get through can be represented as a straight line segment from point $0$ (where you and your squad is initially located) to point $n + 1$ (where the boss is located). The level is filled with $k$ traps. Each trap is represented by three numbers $l_i$, $r_i$ and $d_i$. $l_i$ is the location of the trap, and $d_i$ is the danger level of the trap: whenever a soldier with agility lower than $d_i$ steps on a trap (that is, moves to the point $l_i$), he gets instantly killed. Fortunately, you can disarm traps: if you move to the point $r_i$, you disarm this trap, and it no longer poses any danger to your soldiers. Traps don't affect you, only your soldiers. You have $t$ seconds to complete the level β€” that is, to bring some soldiers from your squad to the boss. Before the level starts, you choose which soldiers will be coming with you, and which soldiers won't be. After that, you have to bring all of the chosen soldiers to the boss. To do so, you may perform the following actions: if your location is $x$, you may move to $x + 1$ or $x - 1$. This action consumes one second; if your location is $x$ and the location of your squad is $x$, you may move to $x + 1$ or to $x - 1$ with your squad in one second. You may not perform this action if it puts some soldier in danger (i. e. the point your squad is moving into contains a non-disarmed trap with $d_i$ greater than agility of some soldier from the squad). This action consumes one second; if your location is $x$ and there is a trap $i$ with $r_i = x$, you may disarm this trap. This action is done instantly (it consumes no time). Note that after each action both your coordinate and the coordinate of your squad should be integers. You have to choose the maximum number of soldiers such that they all can be brought from the point $0$ to the point $n + 1$ (where the boss waits) in no more than $t$ seconds. -----Input----- The first line contains four integers $m$, $n$, $k$ and $t$ ($1 \le m, n, k, t \le 2 \cdot 10^5$, $n < t$) β€” the number of soldiers, the number of integer points between the squad and the boss, the number of traps and the maximum number of seconds you may spend to bring the squad to the boss, respectively. The second line contains $m$ integers $a_1$, $a_2$, ..., $a_m$ ($1 \le a_i \le 2 \cdot 10^5$), where $a_i$ is the agility of the $i$-th soldier. Then $k$ lines follow, containing the descriptions of traps. Each line contains three numbers $l_i$, $r_i$ and $d_i$ ($1 \le l_i \le r_i \le n$, $1 \le d_i \le 2 \cdot 10^5$) β€” the location of the trap, the location where the trap can be disarmed, and its danger level, respectively. -----Output----- Print one integer β€” the maximum number of soldiers you may choose so that you may bring them all to the boss in no more than $t$ seconds. -----Example----- Input 5 6 4 14 1 2 3 4 5 1 5 2 1 2 5 2 3 5 3 5 3 Output 3 -----Note----- In the first example you may take soldiers with agility $3$, $4$ and $5$ with you. The course of action is as follows: go to $2$ without your squad; disarm the trap $2$; go to $3$ without your squad; disartm the trap $3$; go to $0$ without your squad; go to $7$ with your squad. The whole plan can be executed in $13$ seconds.
m, n, k, t = map(int, input().split()) a = list(map(int, input().split())) a.sort(reverse=True) traps = [tuple(map(int, input().split())) for _ in range(k)] def calc(ans): mina = a[ans - 1] tc = [0] * (n + 2) for l, r, d in traps: if mina < d: tc[l - 1] += 1 tc[r] -= 1 for i in range(1, n + 2): tc[i] += tc[i - 1] ans = 0 for i in range(n + 2): if tc[i] > 0: ans += 1 return ans * 2 + n + 1 l, r = 0, m + 1 while r - l > 1: mid = (l + r) // 2 + (l + r) % 2 if calc(mid) <= t: l = mid else: r = mid print(l)
ASSIGN VAR VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR VAR FUNC_CALL VAR VAR FUNC_DEF ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FOR VAR VAR VAR VAR IF VAR VAR VAR BIN_OP VAR NUMBER NUMBER VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR VAR NUMBER VAR NUMBER RETURN BIN_OP BIN_OP BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR VAR NUMBER BIN_OP VAR NUMBER WHILE BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR
You are playing a computer game, where you lead a party of $m$ soldiers. Each soldier is characterised by his agility $a_i$. The level you are trying to get through can be represented as a straight line segment from point $0$ (where you and your squad is initially located) to point $n + 1$ (where the boss is located). The level is filled with $k$ traps. Each trap is represented by three numbers $l_i$, $r_i$ and $d_i$. $l_i$ is the location of the trap, and $d_i$ is the danger level of the trap: whenever a soldier with agility lower than $d_i$ steps on a trap (that is, moves to the point $l_i$), he gets instantly killed. Fortunately, you can disarm traps: if you move to the point $r_i$, you disarm this trap, and it no longer poses any danger to your soldiers. Traps don't affect you, only your soldiers. You have $t$ seconds to complete the level β€” that is, to bring some soldiers from your squad to the boss. Before the level starts, you choose which soldiers will be coming with you, and which soldiers won't be. After that, you have to bring all of the chosen soldiers to the boss. To do so, you may perform the following actions: if your location is $x$, you may move to $x + 1$ or $x - 1$. This action consumes one second; if your location is $x$ and the location of your squad is $x$, you may move to $x + 1$ or to $x - 1$ with your squad in one second. You may not perform this action if it puts some soldier in danger (i. e. the point your squad is moving into contains a non-disarmed trap with $d_i$ greater than agility of some soldier from the squad). This action consumes one second; if your location is $x$ and there is a trap $i$ with $r_i = x$, you may disarm this trap. This action is done instantly (it consumes no time). Note that after each action both your coordinate and the coordinate of your squad should be integers. You have to choose the maximum number of soldiers such that they all can be brought from the point $0$ to the point $n + 1$ (where the boss waits) in no more than $t$ seconds. -----Input----- The first line contains four integers $m$, $n$, $k$ and $t$ ($1 \le m, n, k, t \le 2 \cdot 10^5$, $n < t$) β€” the number of soldiers, the number of integer points between the squad and the boss, the number of traps and the maximum number of seconds you may spend to bring the squad to the boss, respectively. The second line contains $m$ integers $a_1$, $a_2$, ..., $a_m$ ($1 \le a_i \le 2 \cdot 10^5$), where $a_i$ is the agility of the $i$-th soldier. Then $k$ lines follow, containing the descriptions of traps. Each line contains three numbers $l_i$, $r_i$ and $d_i$ ($1 \le l_i \le r_i \le n$, $1 \le d_i \le 2 \cdot 10^5$) β€” the location of the trap, the location where the trap can be disarmed, and its danger level, respectively. -----Output----- Print one integer β€” the maximum number of soldiers you may choose so that you may bring them all to the boss in no more than $t$ seconds. -----Example----- Input 5 6 4 14 1 2 3 4 5 1 5 2 1 2 5 2 3 5 3 5 3 Output 3 -----Note----- In the first example you may take soldiers with agility $3$, $4$ and $5$ with you. The course of action is as follows: go to $2$ without your squad; disarm the trap $2$; go to $3$ without your squad; disartm the trap $3$; go to $0$ without your squad; go to $7$ with your squad. The whole plan can be executed in $13$ seconds.
import sys input = sys.stdin.readline def check(num): count = 0 prev = 0 for i in trap: if i[2] > num: if i[0] <= prev: count += max(0, i[1] - prev) prev = max(prev, i[1]) else: count += i[1] - i[0] + 1 prev = i[1] count = count * 2 + n + 1 if count <= t: return True return False m, n, k, t = map(int, input().split()) a = sorted(list(map(int, input().split())))[::-1] trap = [] for i in range(k): trap.append(tuple(map(int, input().split()))) trap.sort() low = 0 high = m while low < high: mid = (low + high) // 2 if check(a[mid]): low = mid + 1 else: high = mid - 1 if low < m and check(a[low]): print(low + 1) else: print(low)
IMPORT ASSIGN VAR VAR FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF VAR NUMBER VAR IF VAR NUMBER VAR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER VAR BIN_OP BIN_OP VAR NUMBER VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR NUMBER VAR NUMBER IF VAR VAR RETURN NUMBER RETURN NUMBER ASSIGN VAR VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR NUMBER 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 EXPR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR VAR WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF VAR VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR
You are playing a computer game, where you lead a party of $m$ soldiers. Each soldier is characterised by his agility $a_i$. The level you are trying to get through can be represented as a straight line segment from point $0$ (where you and your squad is initially located) to point $n + 1$ (where the boss is located). The level is filled with $k$ traps. Each trap is represented by three numbers $l_i$, $r_i$ and $d_i$. $l_i$ is the location of the trap, and $d_i$ is the danger level of the trap: whenever a soldier with agility lower than $d_i$ steps on a trap (that is, moves to the point $l_i$), he gets instantly killed. Fortunately, you can disarm traps: if you move to the point $r_i$, you disarm this trap, and it no longer poses any danger to your soldiers. Traps don't affect you, only your soldiers. You have $t$ seconds to complete the level β€” that is, to bring some soldiers from your squad to the boss. Before the level starts, you choose which soldiers will be coming with you, and which soldiers won't be. After that, you have to bring all of the chosen soldiers to the boss. To do so, you may perform the following actions: if your location is $x$, you may move to $x + 1$ or $x - 1$. This action consumes one second; if your location is $x$ and the location of your squad is $x$, you may move to $x + 1$ or to $x - 1$ with your squad in one second. You may not perform this action if it puts some soldier in danger (i. e. the point your squad is moving into contains a non-disarmed trap with $d_i$ greater than agility of some soldier from the squad). This action consumes one second; if your location is $x$ and there is a trap $i$ with $r_i = x$, you may disarm this trap. This action is done instantly (it consumes no time). Note that after each action both your coordinate and the coordinate of your squad should be integers. You have to choose the maximum number of soldiers such that they all can be brought from the point $0$ to the point $n + 1$ (where the boss waits) in no more than $t$ seconds. -----Input----- The first line contains four integers $m$, $n$, $k$ and $t$ ($1 \le m, n, k, t \le 2 \cdot 10^5$, $n < t$) β€” the number of soldiers, the number of integer points between the squad and the boss, the number of traps and the maximum number of seconds you may spend to bring the squad to the boss, respectively. The second line contains $m$ integers $a_1$, $a_2$, ..., $a_m$ ($1 \le a_i \le 2 \cdot 10^5$), where $a_i$ is the agility of the $i$-th soldier. Then $k$ lines follow, containing the descriptions of traps. Each line contains three numbers $l_i$, $r_i$ and $d_i$ ($1 \le l_i \le r_i \le n$, $1 \le d_i \le 2 \cdot 10^5$) β€” the location of the trap, the location where the trap can be disarmed, and its danger level, respectively. -----Output----- Print one integer β€” the maximum number of soldiers you may choose so that you may bring them all to the boss in no more than $t$ seconds. -----Example----- Input 5 6 4 14 1 2 3 4 5 1 5 2 1 2 5 2 3 5 3 5 3 Output 3 -----Note----- In the first example you may take soldiers with agility $3$, $4$ and $5$ with you. The course of action is as follows: go to $2$ without your squad; disarm the trap $2$; go to $3$ without your squad; disartm the trap $3$; go to $0$ without your squad; go to $7$ with your squad. The whole plan can be executed in $13$ seconds.
import sys def army(weak, traps, t_max, n): t = n + 1 alone = [0] * (n + 1) for l, r, d in traps: if d <= weak: break alone[l - 1] += 1 alone[r] -= 1 interval = 0 for loc in range(n + 1): interval += alone[loc] if interval: t += 2 return t <= t_max reader = (list(map(int, s.split())) for s in sys.stdin) m, n, k, t_max = next(reader) a = list(next(reader)) traps = [] for _ in range(k): l, r, d = next(reader) traps.append((l, r, d)) a.sort(reverse=True) traps.sort(key=lambda x: x[2], reverse=True) L = 0 R = m + 1 while L + 1 < R: check = (L + R) // 2 if army(a[check - 1], traps, t_max, n): L = check else: R = check print(L)
IMPORT FUNC_DEF ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FOR VAR VAR VAR VAR IF VAR VAR VAR BIN_OP VAR NUMBER NUMBER VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR VAR IF VAR VAR NUMBER RETURN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER WHILE BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR
You are playing a computer game, where you lead a party of $m$ soldiers. Each soldier is characterised by his agility $a_i$. The level you are trying to get through can be represented as a straight line segment from point $0$ (where you and your squad is initially located) to point $n + 1$ (where the boss is located). The level is filled with $k$ traps. Each trap is represented by three numbers $l_i$, $r_i$ and $d_i$. $l_i$ is the location of the trap, and $d_i$ is the danger level of the trap: whenever a soldier with agility lower than $d_i$ steps on a trap (that is, moves to the point $l_i$), he gets instantly killed. Fortunately, you can disarm traps: if you move to the point $r_i$, you disarm this trap, and it no longer poses any danger to your soldiers. Traps don't affect you, only your soldiers. You have $t$ seconds to complete the level β€” that is, to bring some soldiers from your squad to the boss. Before the level starts, you choose which soldiers will be coming with you, and which soldiers won't be. After that, you have to bring all of the chosen soldiers to the boss. To do so, you may perform the following actions: if your location is $x$, you may move to $x + 1$ or $x - 1$. This action consumes one second; if your location is $x$ and the location of your squad is $x$, you may move to $x + 1$ or to $x - 1$ with your squad in one second. You may not perform this action if it puts some soldier in danger (i. e. the point your squad is moving into contains a non-disarmed trap with $d_i$ greater than agility of some soldier from the squad). This action consumes one second; if your location is $x$ and there is a trap $i$ with $r_i = x$, you may disarm this trap. This action is done instantly (it consumes no time). Note that after each action both your coordinate and the coordinate of your squad should be integers. You have to choose the maximum number of soldiers such that they all can be brought from the point $0$ to the point $n + 1$ (where the boss waits) in no more than $t$ seconds. -----Input----- The first line contains four integers $m$, $n$, $k$ and $t$ ($1 \le m, n, k, t \le 2 \cdot 10^5$, $n < t$) β€” the number of soldiers, the number of integer points between the squad and the boss, the number of traps and the maximum number of seconds you may spend to bring the squad to the boss, respectively. The second line contains $m$ integers $a_1$, $a_2$, ..., $a_m$ ($1 \le a_i \le 2 \cdot 10^5$), where $a_i$ is the agility of the $i$-th soldier. Then $k$ lines follow, containing the descriptions of traps. Each line contains three numbers $l_i$, $r_i$ and $d_i$ ($1 \le l_i \le r_i \le n$, $1 \le d_i \le 2 \cdot 10^5$) β€” the location of the trap, the location where the trap can be disarmed, and its danger level, respectively. -----Output----- Print one integer β€” the maximum number of soldiers you may choose so that you may bring them all to the boss in no more than $t$ seconds. -----Example----- Input 5 6 4 14 1 2 3 4 5 1 5 2 1 2 5 2 3 5 3 5 3 Output 3 -----Note----- In the first example you may take soldiers with agility $3$, $4$ and $5$ with you. The course of action is as follows: go to $2$ without your squad; disarm the trap $2$; go to $3$ without your squad; disartm the trap $3$; go to $0$ without your squad; go to $7$ with your squad. The whole plan can be executed in $13$ seconds.
from sys import stdin m, n, k, t = [int(x) for x in stdin.readline().split()] a = sorted([int(x) for x in stdin.readline().split()], reverse=True) traps = [] for x in range(k): l, r, d = [int(x) for x in stdin.readline().split()] traps.append((d, l - 1, r)) traps.sort() low = 0 high = m - 1 valid = -1 while low <= high: mid = (low + high) // 2 agi = a[mid] time = t - n - 1 line = [(0) for x in range(n + 1)] for d, l, r in traps: if d > agi: line[l] += 1 line[r] -= 1 total = 0 c = 0 for x in line: c += x if c: total += 1 time -= 2 * total if time >= 0: valid = max(valid, mid) low = mid + 1 else: high = mid - 1 print(valid + 1)
ASSIGN VAR VAR VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR VAR VAR VAR IF VAR VAR VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR VAR VAR IF VAR VAR NUMBER VAR BIN_OP NUMBER VAR IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER
You are playing a computer game, where you lead a party of $m$ soldiers. Each soldier is characterised by his agility $a_i$. The level you are trying to get through can be represented as a straight line segment from point $0$ (where you and your squad is initially located) to point $n + 1$ (where the boss is located). The level is filled with $k$ traps. Each trap is represented by three numbers $l_i$, $r_i$ and $d_i$. $l_i$ is the location of the trap, and $d_i$ is the danger level of the trap: whenever a soldier with agility lower than $d_i$ steps on a trap (that is, moves to the point $l_i$), he gets instantly killed. Fortunately, you can disarm traps: if you move to the point $r_i$, you disarm this trap, and it no longer poses any danger to your soldiers. Traps don't affect you, only your soldiers. You have $t$ seconds to complete the level β€” that is, to bring some soldiers from your squad to the boss. Before the level starts, you choose which soldiers will be coming with you, and which soldiers won't be. After that, you have to bring all of the chosen soldiers to the boss. To do so, you may perform the following actions: if your location is $x$, you may move to $x + 1$ or $x - 1$. This action consumes one second; if your location is $x$ and the location of your squad is $x$, you may move to $x + 1$ or to $x - 1$ with your squad in one second. You may not perform this action if it puts some soldier in danger (i. e. the point your squad is moving into contains a non-disarmed trap with $d_i$ greater than agility of some soldier from the squad). This action consumes one second; if your location is $x$ and there is a trap $i$ with $r_i = x$, you may disarm this trap. This action is done instantly (it consumes no time). Note that after each action both your coordinate and the coordinate of your squad should be integers. You have to choose the maximum number of soldiers such that they all can be brought from the point $0$ to the point $n + 1$ (where the boss waits) in no more than $t$ seconds. -----Input----- The first line contains four integers $m$, $n$, $k$ and $t$ ($1 \le m, n, k, t \le 2 \cdot 10^5$, $n < t$) β€” the number of soldiers, the number of integer points between the squad and the boss, the number of traps and the maximum number of seconds you may spend to bring the squad to the boss, respectively. The second line contains $m$ integers $a_1$, $a_2$, ..., $a_m$ ($1 \le a_i \le 2 \cdot 10^5$), where $a_i$ is the agility of the $i$-th soldier. Then $k$ lines follow, containing the descriptions of traps. Each line contains three numbers $l_i$, $r_i$ and $d_i$ ($1 \le l_i \le r_i \le n$, $1 \le d_i \le 2 \cdot 10^5$) β€” the location of the trap, the location where the trap can be disarmed, and its danger level, respectively. -----Output----- Print one integer β€” the maximum number of soldiers you may choose so that you may bring them all to the boss in no more than $t$ seconds. -----Example----- Input 5 6 4 14 1 2 3 4 5 1 5 2 1 2 5 2 3 5 3 5 3 Output 3 -----Note----- In the first example you may take soldiers with agility $3$, $4$ and $5$ with you. The course of action is as follows: go to $2$ without your squad; disarm the trap $2$; go to $3$ without your squad; disartm the trap $3$; go to $0$ without your squad; go to $7$ with your squad. The whole plan can be executed in $13$ seconds.
m, n, k, t = list(map(int, input().split())) soldiers = list(map(int, input().split())) traps = [] for _ in range(k): l, r, d = list(map(int, input().split())) traps.append((l, r, d)) soldiers.sort(reverse=True) traps.sort() def cost(count): agil = soldiers[count - 1] if count > 0 else 10**6 current_begin = 0 current_end = -1 inter_cost = 0 for l, r, d in traps: if d <= agil: continue if l > current_end: inter_cost += 2 * (current_end - current_begin + 1) current_begin = l current_end = max(current_end, r) inter_cost += 2 * (current_end - current_begin + 1) return inter_cost + n + 1 def bin_search(lo, hi): if lo + 1 >= hi: return lo mid = (lo + hi) // 2 if cost(mid) <= t: lo = mid else: hi = mid return bin_search(lo, hi) result = bin_search(0, m + 1) print(result)
ASSIGN VAR VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR FUNC_DEF ASSIGN VAR VAR NUMBER VAR BIN_OP VAR NUMBER BIN_OP NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR VAR VAR IF VAR VAR IF VAR VAR VAR BIN_OP NUMBER BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR BIN_OP NUMBER BIN_OP BIN_OP VAR VAR NUMBER RETURN BIN_OP BIN_OP VAR VAR NUMBER FUNC_DEF IF BIN_OP VAR NUMBER VAR RETURN VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR RETURN FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR
You are playing a computer game, where you lead a party of $m$ soldiers. Each soldier is characterised by his agility $a_i$. The level you are trying to get through can be represented as a straight line segment from point $0$ (where you and your squad is initially located) to point $n + 1$ (where the boss is located). The level is filled with $k$ traps. Each trap is represented by three numbers $l_i$, $r_i$ and $d_i$. $l_i$ is the location of the trap, and $d_i$ is the danger level of the trap: whenever a soldier with agility lower than $d_i$ steps on a trap (that is, moves to the point $l_i$), he gets instantly killed. Fortunately, you can disarm traps: if you move to the point $r_i$, you disarm this trap, and it no longer poses any danger to your soldiers. Traps don't affect you, only your soldiers. You have $t$ seconds to complete the level β€” that is, to bring some soldiers from your squad to the boss. Before the level starts, you choose which soldiers will be coming with you, and which soldiers won't be. After that, you have to bring all of the chosen soldiers to the boss. To do so, you may perform the following actions: if your location is $x$, you may move to $x + 1$ or $x - 1$. This action consumes one second; if your location is $x$ and the location of your squad is $x$, you may move to $x + 1$ or to $x - 1$ with your squad in one second. You may not perform this action if it puts some soldier in danger (i. e. the point your squad is moving into contains a non-disarmed trap with $d_i$ greater than agility of some soldier from the squad). This action consumes one second; if your location is $x$ and there is a trap $i$ with $r_i = x$, you may disarm this trap. This action is done instantly (it consumes no time). Note that after each action both your coordinate and the coordinate of your squad should be integers. You have to choose the maximum number of soldiers such that they all can be brought from the point $0$ to the point $n + 1$ (where the boss waits) in no more than $t$ seconds. -----Input----- The first line contains four integers $m$, $n$, $k$ and $t$ ($1 \le m, n, k, t \le 2 \cdot 10^5$, $n < t$) β€” the number of soldiers, the number of integer points between the squad and the boss, the number of traps and the maximum number of seconds you may spend to bring the squad to the boss, respectively. The second line contains $m$ integers $a_1$, $a_2$, ..., $a_m$ ($1 \le a_i \le 2 \cdot 10^5$), where $a_i$ is the agility of the $i$-th soldier. Then $k$ lines follow, containing the descriptions of traps. Each line contains three numbers $l_i$, $r_i$ and $d_i$ ($1 \le l_i \le r_i \le n$, $1 \le d_i \le 2 \cdot 10^5$) β€” the location of the trap, the location where the trap can be disarmed, and its danger level, respectively. -----Output----- Print one integer β€” the maximum number of soldiers you may choose so that you may bring them all to the boss in no more than $t$ seconds. -----Example----- Input 5 6 4 14 1 2 3 4 5 1 5 2 1 2 5 2 3 5 3 5 3 Output 3 -----Note----- In the first example you may take soldiers with agility $3$, $4$ and $5$ with you. The course of action is as follows: go to $2$ without your squad; disarm the trap $2$; go to $3$ without your squad; disartm the trap $3$; go to $0$ without your squad; go to $7$ with your squad. The whole plan can be executed in $13$ seconds.
import sys input = sys.stdin.readline sol_n, n, trap_n, t = [int(item) for item in input().split()] sold = [int(item) for item in input().split()] sold.sort(reverse=True) trap = [] for i in range(trap_n): l, r, d = [int(item) for item in input().split()] trap.append((d, l, r)) trap.sort() ll = 0 rr = sol_n + 1 while rr - ll > 1: mid = (ll + rr) // 2 to_save = sold[mid - 1] alone_walk = [0] * (n + 1) for d, l, r in trap: if d > to_save: alone_walk[l - 1] += 1 alone_walk[r] -= 1 total = 0 time = 0 for item in alone_walk: total += item if total > 0: time += 2 time += n + 1 if time <= t: ll = mid else: rr = mid print(ll)
IMPORT ASSIGN VAR VAR ASSIGN VAR VAR VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER WHILE BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FOR VAR VAR VAR VAR IF VAR VAR VAR BIN_OP VAR NUMBER NUMBER VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR VAR VAR IF VAR NUMBER VAR NUMBER VAR BIN_OP VAR NUMBER IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR
You are playing a computer game, where you lead a party of $m$ soldiers. Each soldier is characterised by his agility $a_i$. The level you are trying to get through can be represented as a straight line segment from point $0$ (where you and your squad is initially located) to point $n + 1$ (where the boss is located). The level is filled with $k$ traps. Each trap is represented by three numbers $l_i$, $r_i$ and $d_i$. $l_i$ is the location of the trap, and $d_i$ is the danger level of the trap: whenever a soldier with agility lower than $d_i$ steps on a trap (that is, moves to the point $l_i$), he gets instantly killed. Fortunately, you can disarm traps: if you move to the point $r_i$, you disarm this trap, and it no longer poses any danger to your soldiers. Traps don't affect you, only your soldiers. You have $t$ seconds to complete the level β€” that is, to bring some soldiers from your squad to the boss. Before the level starts, you choose which soldiers will be coming with you, and which soldiers won't be. After that, you have to bring all of the chosen soldiers to the boss. To do so, you may perform the following actions: if your location is $x$, you may move to $x + 1$ or $x - 1$. This action consumes one second; if your location is $x$ and the location of your squad is $x$, you may move to $x + 1$ or to $x - 1$ with your squad in one second. You may not perform this action if it puts some soldier in danger (i. e. the point your squad is moving into contains a non-disarmed trap with $d_i$ greater than agility of some soldier from the squad). This action consumes one second; if your location is $x$ and there is a trap $i$ with $r_i = x$, you may disarm this trap. This action is done instantly (it consumes no time). Note that after each action both your coordinate and the coordinate of your squad should be integers. You have to choose the maximum number of soldiers such that they all can be brought from the point $0$ to the point $n + 1$ (where the boss waits) in no more than $t$ seconds. -----Input----- The first line contains four integers $m$, $n$, $k$ and $t$ ($1 \le m, n, k, t \le 2 \cdot 10^5$, $n < t$) β€” the number of soldiers, the number of integer points between the squad and the boss, the number of traps and the maximum number of seconds you may spend to bring the squad to the boss, respectively. The second line contains $m$ integers $a_1$, $a_2$, ..., $a_m$ ($1 \le a_i \le 2 \cdot 10^5$), where $a_i$ is the agility of the $i$-th soldier. Then $k$ lines follow, containing the descriptions of traps. Each line contains three numbers $l_i$, $r_i$ and $d_i$ ($1 \le l_i \le r_i \le n$, $1 \le d_i \le 2 \cdot 10^5$) β€” the location of the trap, the location where the trap can be disarmed, and its danger level, respectively. -----Output----- Print one integer β€” the maximum number of soldiers you may choose so that you may bring them all to the boss in no more than $t$ seconds. -----Example----- Input 5 6 4 14 1 2 3 4 5 1 5 2 1 2 5 2 3 5 3 5 3 Output 3 -----Note----- In the first example you may take soldiers with agility $3$, $4$ and $5$ with you. The course of action is as follows: go to $2$ without your squad; disarm the trap $2$; go to $3$ without your squad; disartm the trap $3$; go to $0$ without your squad; go to $7$ with your squad. The whole plan can be executed in $13$ seconds.
import sys input = sys.stdin.readline m, n, k, t = map(int, input().split()) A = list(map(int, input().split())) T = [tuple(map(int, input().split())) for i in range(k)] seg_el = 1 << (n + 2).bit_length() seg_height = 1 + (n + 2).bit_length() SEG = [0] * (2 * seg_el) LAZY = [0] * (2 * seg_el) def indexes(L, R): INDLIST = [] R -= 1 L >>= 1 R >>= 1 while L != R: if L > R: INDLIST.append(L) L >>= 1 else: INDLIST.append(R) R >>= 1 while L != 0: INDLIST.append(L) L >>= 1 return INDLIST def updates(l, r, x): L = l + seg_el R = r + seg_el L //= L & -L R //= R & -R UPIND = indexes(L, R) for ind in UPIND[::-1]: if LAZY[ind] != None: update_lazy = LAZY[ind] * (1 << seg_height - 1 - ind.bit_length()) LAZY[ind << 1] = LAZY[1 + (ind << 1)] = LAZY[ind] SEG[ind << 1] = SEG[1 + (ind << 1)] = update_lazy LAZY[ind] = None while L != R: if L > R: SEG[L] = x * (1 << seg_height - L.bit_length()) LAZY[L] = x L += 1 L //= L & -L else: R -= 1 SEG[R] = x * (1 << seg_height - R.bit_length()) LAZY[R] = x R //= R & -R for ind in UPIND: SEG[ind] = SEG[ind << 1] + SEG[1 + (ind << 1)] def getvalues(l, r): L = l + seg_el R = r + seg_el L //= L & -L R //= R & -R UPIND = indexes(L, R) for ind in UPIND[::-1]: if LAZY[ind] != None: update_lazy = LAZY[ind] * (1 << seg_height - 1 - ind.bit_length()) LAZY[ind << 1] = LAZY[1 + (ind << 1)] = LAZY[ind] SEG[ind << 1] = SEG[1 + (ind << 1)] = update_lazy LAZY[ind] = None ANS = 0 while L != R: if L > R: ANS += SEG[L] L += 1 L //= L & -L else: R -= 1 ANS += SEG[R] R //= R & -R return ANS T.sort(key=lambda x: x[2], reverse=True) OK = 2 * 10**5 + 1 ind = 0 for i in range(2 * 10**5 + 2, -1, -1): while ind < k and T[ind][2] >= i: updates(T[ind][0], T[ind][1] + 1, 1) ind += 1 if getvalues(0, n + 2) * 2 + n + 1 <= t: OK = i else: break ANS = 0 for a in A: if a >= OK - 1: ANS += 1 print(ANS)
IMPORT ASSIGN VAR VAR ASSIGN VAR VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP NUMBER FUNC_CALL BIN_OP VAR NUMBER ASSIGN VAR BIN_OP NUMBER FUNC_CALL BIN_OP VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP NUMBER VAR FUNC_DEF ASSIGN VAR LIST VAR NUMBER VAR NUMBER VAR NUMBER WHILE VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER WHILE VAR NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER RETURN VAR FUNC_DEF ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FOR VAR VAR NUMBER IF VAR VAR NONE ASSIGN VAR BIN_OP VAR VAR BIN_OP NUMBER BIN_OP BIN_OP VAR NUMBER FUNC_CALL VAR ASSIGN VAR BIN_OP VAR NUMBER VAR BIN_OP NUMBER BIN_OP VAR NUMBER VAR VAR ASSIGN VAR BIN_OP VAR NUMBER VAR BIN_OP NUMBER BIN_OP VAR NUMBER VAR ASSIGN VAR VAR NONE WHILE VAR VAR IF VAR VAR ASSIGN VAR VAR BIN_OP VAR BIN_OP NUMBER BIN_OP VAR FUNC_CALL VAR ASSIGN VAR VAR VAR VAR NUMBER VAR BIN_OP VAR VAR VAR NUMBER ASSIGN VAR VAR BIN_OP VAR BIN_OP NUMBER BIN_OP VAR FUNC_CALL VAR ASSIGN VAR VAR VAR VAR BIN_OP VAR VAR FOR VAR VAR ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP NUMBER BIN_OP VAR NUMBER FUNC_DEF ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FOR VAR VAR NUMBER IF VAR VAR NONE ASSIGN VAR BIN_OP VAR VAR BIN_OP NUMBER BIN_OP BIN_OP VAR NUMBER FUNC_CALL VAR ASSIGN VAR BIN_OP VAR NUMBER VAR BIN_OP NUMBER BIN_OP VAR NUMBER VAR VAR ASSIGN VAR BIN_OP VAR NUMBER VAR BIN_OP NUMBER BIN_OP VAR NUMBER VAR ASSIGN VAR VAR NONE ASSIGN VAR NUMBER WHILE VAR VAR IF VAR VAR VAR VAR VAR VAR NUMBER VAR BIN_OP VAR VAR VAR NUMBER VAR VAR VAR VAR BIN_OP VAR VAR RETURN VAR EXPR FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP NUMBER BIN_OP NUMBER NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP BIN_OP NUMBER BIN_OP NUMBER NUMBER NUMBER NUMBER NUMBER WHILE VAR VAR VAR VAR NUMBER VAR EXPR FUNC_CALL VAR VAR VAR NUMBER BIN_OP VAR VAR NUMBER NUMBER NUMBER VAR NUMBER IF BIN_OP BIN_OP BIN_OP FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER NUMBER VAR NUMBER VAR ASSIGN VAR VAR ASSIGN VAR NUMBER FOR VAR VAR IF VAR BIN_OP VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR
You are playing a computer game, where you lead a party of $m$ soldiers. Each soldier is characterised by his agility $a_i$. The level you are trying to get through can be represented as a straight line segment from point $0$ (where you and your squad is initially located) to point $n + 1$ (where the boss is located). The level is filled with $k$ traps. Each trap is represented by three numbers $l_i$, $r_i$ and $d_i$. $l_i$ is the location of the trap, and $d_i$ is the danger level of the trap: whenever a soldier with agility lower than $d_i$ steps on a trap (that is, moves to the point $l_i$), he gets instantly killed. Fortunately, you can disarm traps: if you move to the point $r_i$, you disarm this trap, and it no longer poses any danger to your soldiers. Traps don't affect you, only your soldiers. You have $t$ seconds to complete the level β€” that is, to bring some soldiers from your squad to the boss. Before the level starts, you choose which soldiers will be coming with you, and which soldiers won't be. After that, you have to bring all of the chosen soldiers to the boss. To do so, you may perform the following actions: if your location is $x$, you may move to $x + 1$ or $x - 1$. This action consumes one second; if your location is $x$ and the location of your squad is $x$, you may move to $x + 1$ or to $x - 1$ with your squad in one second. You may not perform this action if it puts some soldier in danger (i. e. the point your squad is moving into contains a non-disarmed trap with $d_i$ greater than agility of some soldier from the squad). This action consumes one second; if your location is $x$ and there is a trap $i$ with $r_i = x$, you may disarm this trap. This action is done instantly (it consumes no time). Note that after each action both your coordinate and the coordinate of your squad should be integers. You have to choose the maximum number of soldiers such that they all can be brought from the point $0$ to the point $n + 1$ (where the boss waits) in no more than $t$ seconds. -----Input----- The first line contains four integers $m$, $n$, $k$ and $t$ ($1 \le m, n, k, t \le 2 \cdot 10^5$, $n < t$) β€” the number of soldiers, the number of integer points between the squad and the boss, the number of traps and the maximum number of seconds you may spend to bring the squad to the boss, respectively. The second line contains $m$ integers $a_1$, $a_2$, ..., $a_m$ ($1 \le a_i \le 2 \cdot 10^5$), where $a_i$ is the agility of the $i$-th soldier. Then $k$ lines follow, containing the descriptions of traps. Each line contains three numbers $l_i$, $r_i$ and $d_i$ ($1 \le l_i \le r_i \le n$, $1 \le d_i \le 2 \cdot 10^5$) β€” the location of the trap, the location where the trap can be disarmed, and its danger level, respectively. -----Output----- Print one integer β€” the maximum number of soldiers you may choose so that you may bring them all to the boss in no more than $t$ seconds. -----Example----- Input 5 6 4 14 1 2 3 4 5 1 5 2 1 2 5 2 3 5 3 5 3 Output 3 -----Note----- In the first example you may take soldiers with agility $3$, $4$ and $5$ with you. The course of action is as follows: go to $2$ without your squad; disarm the trap $2$; go to $3$ without your squad; disartm the trap $3$; go to $0$ without your squad; go to $7$ with your squad. The whole plan can be executed in $13$ seconds.
import sys input = sys.stdin.readline def judge(x): cnt = [0] * (n + 2) for l, r, d in lrd: if x < d: cnt[l] += 1 cnt[r + 1] -= 1 for i in range(1, n + 2): cnt[i] += cnt[i - 1] sec = 0 for i in range(1, n + 2): if cnt[i] == 0: sec += 1 else: sec += 3 return sec <= t def binary_search(): l, r = 0, m - 1 while l <= r: mid = (l + r) // 2 if judge(a[mid]): r = mid - 1 else: l = mid + 1 return l m, n, k, t = map(int, input().split()) a = list(map(int, input().split())) lrd = [tuple(map(int, input().split())) for _ in range(k)] a.sort() print(m - binary_search())
IMPORT ASSIGN VAR VAR FUNC_DEF ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FOR VAR VAR VAR VAR IF VAR VAR VAR VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR NUMBER VAR NUMBER VAR NUMBER RETURN VAR VAR FUNC_DEF ASSIGN VAR VAR NUMBER BIN_OP VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER RETURN VAR ASSIGN VAR VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR BIN_OP VAR FUNC_CALL VAR
You are playing a computer game, where you lead a party of $m$ soldiers. Each soldier is characterised by his agility $a_i$. The level you are trying to get through can be represented as a straight line segment from point $0$ (where you and your squad is initially located) to point $n + 1$ (where the boss is located). The level is filled with $k$ traps. Each trap is represented by three numbers $l_i$, $r_i$ and $d_i$. $l_i$ is the location of the trap, and $d_i$ is the danger level of the trap: whenever a soldier with agility lower than $d_i$ steps on a trap (that is, moves to the point $l_i$), he gets instantly killed. Fortunately, you can disarm traps: if you move to the point $r_i$, you disarm this trap, and it no longer poses any danger to your soldiers. Traps don't affect you, only your soldiers. You have $t$ seconds to complete the level β€” that is, to bring some soldiers from your squad to the boss. Before the level starts, you choose which soldiers will be coming with you, and which soldiers won't be. After that, you have to bring all of the chosen soldiers to the boss. To do so, you may perform the following actions: if your location is $x$, you may move to $x + 1$ or $x - 1$. This action consumes one second; if your location is $x$ and the location of your squad is $x$, you may move to $x + 1$ or to $x - 1$ with your squad in one second. You may not perform this action if it puts some soldier in danger (i. e. the point your squad is moving into contains a non-disarmed trap with $d_i$ greater than agility of some soldier from the squad). This action consumes one second; if your location is $x$ and there is a trap $i$ with $r_i = x$, you may disarm this trap. This action is done instantly (it consumes no time). Note that after each action both your coordinate and the coordinate of your squad should be integers. You have to choose the maximum number of soldiers such that they all can be brought from the point $0$ to the point $n + 1$ (where the boss waits) in no more than $t$ seconds. -----Input----- The first line contains four integers $m$, $n$, $k$ and $t$ ($1 \le m, n, k, t \le 2 \cdot 10^5$, $n < t$) β€” the number of soldiers, the number of integer points between the squad and the boss, the number of traps and the maximum number of seconds you may spend to bring the squad to the boss, respectively. The second line contains $m$ integers $a_1$, $a_2$, ..., $a_m$ ($1 \le a_i \le 2 \cdot 10^5$), where $a_i$ is the agility of the $i$-th soldier. Then $k$ lines follow, containing the descriptions of traps. Each line contains three numbers $l_i$, $r_i$ and $d_i$ ($1 \le l_i \le r_i \le n$, $1 \le d_i \le 2 \cdot 10^5$) β€” the location of the trap, the location where the trap can be disarmed, and its danger level, respectively. -----Output----- Print one integer β€” the maximum number of soldiers you may choose so that you may bring them all to the boss in no more than $t$ seconds. -----Example----- Input 5 6 4 14 1 2 3 4 5 1 5 2 1 2 5 2 3 5 3 5 3 Output 3 -----Note----- In the first example you may take soldiers with agility $3$, $4$ and $5$ with you. The course of action is as follows: go to $2$ without your squad; disarm the trap $2$; go to $3$ without your squad; disartm the trap $3$; go to $0$ without your squad; go to $7$ with your squad. The whole plan can be executed in $13$ seconds.
import sys input = sys.stdin.buffer.readline def movesRequired(agility): nMoves = n + 1 repeatedIntervals = [] for l, r, d in traps: if d > agility: if repeatedIntervals and repeatedIntervals[-1][1] >= l: repeatedIntervals[-1][1] = max(repeatedIntervals[-1][1], r) else: repeatedIntervals.append([l, r]) for l, r in repeatedIntervals: nMoves += (r - l + 1) * 2 return nMoves m, n, k, t = [int(x) for x in input().split()] traps = [] a = [int(x) for x in input().split()] maxD = -1 for _ in range(k): l, r, d = [int(x) for x in input().split()] traps.append([l, r, d]) maxD = max(maxD, d) traps.sort(key=lambda x: x[0]) b = maxD + 1 largestImpossible = -1 while b > 0: while True: moves = movesRequired(largestImpossible + b) if moves > t: largestImpossible += b else: break b //= 2 smallestPossible = largestImpossible + 1 a.sort() ans = 0 for i in range(m - 1, -1, -1): if a[i] >= smallestPossible: ans += 1 print(ans)
IMPORT ASSIGN VAR VAR FUNC_DEF ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR LIST FOR VAR VAR VAR VAR IF VAR VAR IF VAR VAR NUMBER NUMBER VAR ASSIGN VAR NUMBER NUMBER FUNC_CALL VAR VAR NUMBER NUMBER VAR EXPR FUNC_CALL VAR LIST VAR VAR FOR VAR VAR VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER NUMBER RETURN VAR ASSIGN VAR VAR VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR LIST VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER WHILE VAR NUMBER WHILE NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR IF VAR VAR VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
You are playing a computer game, where you lead a party of $m$ soldiers. Each soldier is characterised by his agility $a_i$. The level you are trying to get through can be represented as a straight line segment from point $0$ (where you and your squad is initially located) to point $n + 1$ (where the boss is located). The level is filled with $k$ traps. Each trap is represented by three numbers $l_i$, $r_i$ and $d_i$. $l_i$ is the location of the trap, and $d_i$ is the danger level of the trap: whenever a soldier with agility lower than $d_i$ steps on a trap (that is, moves to the point $l_i$), he gets instantly killed. Fortunately, you can disarm traps: if you move to the point $r_i$, you disarm this trap, and it no longer poses any danger to your soldiers. Traps don't affect you, only your soldiers. You have $t$ seconds to complete the level β€” that is, to bring some soldiers from your squad to the boss. Before the level starts, you choose which soldiers will be coming with you, and which soldiers won't be. After that, you have to bring all of the chosen soldiers to the boss. To do so, you may perform the following actions: if your location is $x$, you may move to $x + 1$ or $x - 1$. This action consumes one second; if your location is $x$ and the location of your squad is $x$, you may move to $x + 1$ or to $x - 1$ with your squad in one second. You may not perform this action if it puts some soldier in danger (i. e. the point your squad is moving into contains a non-disarmed trap with $d_i$ greater than agility of some soldier from the squad). This action consumes one second; if your location is $x$ and there is a trap $i$ with $r_i = x$, you may disarm this trap. This action is done instantly (it consumes no time). Note that after each action both your coordinate and the coordinate of your squad should be integers. You have to choose the maximum number of soldiers such that they all can be brought from the point $0$ to the point $n + 1$ (where the boss waits) in no more than $t$ seconds. -----Input----- The first line contains four integers $m$, $n$, $k$ and $t$ ($1 \le m, n, k, t \le 2 \cdot 10^5$, $n < t$) β€” the number of soldiers, the number of integer points between the squad and the boss, the number of traps and the maximum number of seconds you may spend to bring the squad to the boss, respectively. The second line contains $m$ integers $a_1$, $a_2$, ..., $a_m$ ($1 \le a_i \le 2 \cdot 10^5$), where $a_i$ is the agility of the $i$-th soldier. Then $k$ lines follow, containing the descriptions of traps. Each line contains three numbers $l_i$, $r_i$ and $d_i$ ($1 \le l_i \le r_i \le n$, $1 \le d_i \le 2 \cdot 10^5$) β€” the location of the trap, the location where the trap can be disarmed, and its danger level, respectively. -----Output----- Print one integer β€” the maximum number of soldiers you may choose so that you may bring them all to the boss in no more than $t$ seconds. -----Example----- Input 5 6 4 14 1 2 3 4 5 1 5 2 1 2 5 2 3 5 3 5 3 Output 3 -----Note----- In the first example you may take soldiers with agility $3$, $4$ and $5$ with you. The course of action is as follows: go to $2$ without your squad; disarm the trap $2$; go to $3$ without your squad; disartm the trap $3$; go to $0$ without your squad; go to $7$ with your squad. The whole plan can be executed in $13$ seconds.
m, n, k, t = [int(s) for s in input().split()] A = [int(s) for s in input().split()] A.sort() traps = [] for i in range(k): l, r, d = [int(s) for s in input().split()] traps.append((l, r, d)) traps.sort() def canTake(agi): farest = 0 trapIndex = 0 wasteTime = 0 while trapIndex < len(traps): trap = traps[trapIndex] if trap[2] <= agi: trapIndex += 1 continue if trap[1] <= farest: trapIndex += 1 continue newFarest = trap[1] if trap[0] > farest: wasteTime += (trap[1] - trap[0] + 1) * 2 else: wasteTime += (newFarest - farest) * 2 farest = newFarest trapIndex += 1 if wasteTime + n + 1 > t: return False else: return True def binSearch(A): if canTake(A[0]): return 0 low = 0 high = len(A) while high - low > 1: mid = (low + high) // 2 if canTake(A[mid]): high = mid else: low = mid return high firstCan = binSearch(A) print(len(A) - firstCan)
ASSIGN VAR VAR VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR IF VAR NUMBER VAR VAR NUMBER IF VAR NUMBER VAR VAR NUMBER ASSIGN VAR VAR NUMBER IF VAR NUMBER VAR VAR BIN_OP BIN_OP BIN_OP VAR NUMBER VAR NUMBER NUMBER NUMBER VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER IF BIN_OP BIN_OP VAR VAR NUMBER VAR RETURN NUMBER RETURN NUMBER FUNC_DEF IF FUNC_CALL VAR VAR NUMBER RETURN NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR WHILE BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR VAR
You are playing a computer game, where you lead a party of $m$ soldiers. Each soldier is characterised by his agility $a_i$. The level you are trying to get through can be represented as a straight line segment from point $0$ (where you and your squad is initially located) to point $n + 1$ (where the boss is located). The level is filled with $k$ traps. Each trap is represented by three numbers $l_i$, $r_i$ and $d_i$. $l_i$ is the location of the trap, and $d_i$ is the danger level of the trap: whenever a soldier with agility lower than $d_i$ steps on a trap (that is, moves to the point $l_i$), he gets instantly killed. Fortunately, you can disarm traps: if you move to the point $r_i$, you disarm this trap, and it no longer poses any danger to your soldiers. Traps don't affect you, only your soldiers. You have $t$ seconds to complete the level β€” that is, to bring some soldiers from your squad to the boss. Before the level starts, you choose which soldiers will be coming with you, and which soldiers won't be. After that, you have to bring all of the chosen soldiers to the boss. To do so, you may perform the following actions: if your location is $x$, you may move to $x + 1$ or $x - 1$. This action consumes one second; if your location is $x$ and the location of your squad is $x$, you may move to $x + 1$ or to $x - 1$ with your squad in one second. You may not perform this action if it puts some soldier in danger (i. e. the point your squad is moving into contains a non-disarmed trap with $d_i$ greater than agility of some soldier from the squad). This action consumes one second; if your location is $x$ and there is a trap $i$ with $r_i = x$, you may disarm this trap. This action is done instantly (it consumes no time). Note that after each action both your coordinate and the coordinate of your squad should be integers. You have to choose the maximum number of soldiers such that they all can be brought from the point $0$ to the point $n + 1$ (where the boss waits) in no more than $t$ seconds. -----Input----- The first line contains four integers $m$, $n$, $k$ and $t$ ($1 \le m, n, k, t \le 2 \cdot 10^5$, $n < t$) β€” the number of soldiers, the number of integer points between the squad and the boss, the number of traps and the maximum number of seconds you may spend to bring the squad to the boss, respectively. The second line contains $m$ integers $a_1$, $a_2$, ..., $a_m$ ($1 \le a_i \le 2 \cdot 10^5$), where $a_i$ is the agility of the $i$-th soldier. Then $k$ lines follow, containing the descriptions of traps. Each line contains three numbers $l_i$, $r_i$ and $d_i$ ($1 \le l_i \le r_i \le n$, $1 \le d_i \le 2 \cdot 10^5$) β€” the location of the trap, the location where the trap can be disarmed, and its danger level, respectively. -----Output----- Print one integer β€” the maximum number of soldiers you may choose so that you may bring them all to the boss in no more than $t$ seconds. -----Example----- Input 5 6 4 14 1 2 3 4 5 1 5 2 1 2 5 2 3 5 3 5 3 Output 3 -----Note----- In the first example you may take soldiers with agility $3$, $4$ and $5$ with you. The course of action is as follows: go to $2$ without your squad; disarm the trap $2$; go to $3$ without your squad; disartm the trap $3$; go to $0$ without your squad; go to $7$ with your squad. The whole plan can be executed in $13$ seconds.
m, n, k, t = list(map(int, input().split())) a = list(map(int, input().split())) trap = list() for i in range(k): x1, x2, x3 = list(map(int, input().split())) trap.append([x1, x2, x3]) trap.sort() a.sort() x = -1 l = 0 r = len(a) - 1 s = 0 while r - l >= 0: x = (l + r) // 2 weig = a[x] ltrap = 0 rtrap = 0 time = 0 for i in trap: if i[2] > weig: if i[0] <= rtrap: rtrap = max(rtrap, i[1]) time += i[0] - ltrap ltrap = i[0] else: time += rtrap - ltrap + 1 rtrap = i[1] ltrap = i[0] time += rtrap - ltrap if 2 * time + n + 1 <= t: s = max(s, len(a) - x) r = x - 1 else: l = x + 1 print(s)
ASSIGN VAR VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR LIST VAR VAR VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER WHILE BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF VAR NUMBER VAR IF VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR NUMBER VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER VAR BIN_OP VAR VAR IF BIN_OP BIN_OP BIN_OP NUMBER VAR VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR
Limak is a little polar bear. He plays by building towers from blocks. Every block is a cube with positive integer length of side. Limak has infinitely many blocks of each side length. A block with side a has volume a^3. A tower consisting of blocks with sides a_1, a_2, ..., a_{k} has the total volume a_1^3 + a_2^3 + ... + a_{k}^3. Limak is going to build a tower. First, he asks you to tell him a positive integer XΒ β€” the required total volume of the tower. Then, Limak adds new blocks greedily, one by one. Each time he adds the biggest block such that the total volume doesn't exceed X. Limak asks you to choose X not greater than m. Also, he wants to maximize the number of blocks in the tower at the end (however, he still behaves greedily). Secondarily, he wants to maximize X. Can you help Limak? Find the maximum number of blocks his tower can have and the maximum X ≀ m that results this number of blocks. -----Input----- The only line of the input contains one integer m (1 ≀ m ≀ 10^15), meaning that Limak wants you to choose X between 1 and m, inclusive. -----Output----- Print two integersΒ β€” the maximum number of blocks in the tower and the maximum required total volume X, resulting in the maximum number of blocks. -----Examples----- Input 48 Output 9 42 Input 6 Output 6 6 -----Note----- In the first sample test, there will be 9 blocks if you choose X = 23 or X = 42. Limak wants to maximize X secondarily so you should choose 42. In more detail, after choosing X = 42 the process of building a tower is: Limak takes a block with side 3 because it's the biggest block with volume not greater than 42. The remaining volume is 42 - 27 = 15. The second added block has side 2, so the remaining volume is 15 - 8 = 7. Finally, Limak adds 7 blocks with side 1, one by one. So, there are 9 blocks in the tower. The total volume is is 3^3 + 2^3 + 7Β·1^3 = 27 + 8 + 7 = 42.
def rec(m): if m <= 0: return m, 0 a = int((m + 1e-09) ** (1 / 3)) x, y = rec(m - a**3) q, w = rec(a**3 - (a - 1) ** 3 - 1) return max((x + 1, a**3 + y), (q + 1, (a - 1) ** 3 + w)) print(*rec(int(input())))
FUNC_DEF IF VAR NUMBER RETURN VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER BIN_OP NUMBER NUMBER ASSIGN VAR VAR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP VAR NUMBER BIN_OP BIN_OP VAR NUMBER NUMBER NUMBER RETURN FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER BIN_OP BIN_OP BIN_OP VAR NUMBER NUMBER VAR EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR
Limak is a little polar bear. He plays by building towers from blocks. Every block is a cube with positive integer length of side. Limak has infinitely many blocks of each side length. A block with side a has volume a^3. A tower consisting of blocks with sides a_1, a_2, ..., a_{k} has the total volume a_1^3 + a_2^3 + ... + a_{k}^3. Limak is going to build a tower. First, he asks you to tell him a positive integer XΒ β€” the required total volume of the tower. Then, Limak adds new blocks greedily, one by one. Each time he adds the biggest block such that the total volume doesn't exceed X. Limak asks you to choose X not greater than m. Also, he wants to maximize the number of blocks in the tower at the end (however, he still behaves greedily). Secondarily, he wants to maximize X. Can you help Limak? Find the maximum number of blocks his tower can have and the maximum X ≀ m that results this number of blocks. -----Input----- The only line of the input contains one integer m (1 ≀ m ≀ 10^15), meaning that Limak wants you to choose X between 1 and m, inclusive. -----Output----- Print two integersΒ β€” the maximum number of blocks in the tower and the maximum required total volume X, resulting in the maximum number of blocks. -----Examples----- Input 48 Output 9 42 Input 6 Output 6 6 -----Note----- In the first sample test, there will be 9 blocks if you choose X = 23 or X = 42. Limak wants to maximize X secondarily so you should choose 42. In more detail, after choosing X = 42 the process of building a tower is: Limak takes a block with side 3 because it's the biggest block with volume not greater than 42. The remaining volume is 42 - 27 = 15. The second added block has side 2, so the remaining volume is 15 - 8 = 7. Finally, Limak adds 7 blocks with side 1, one by one. So, there are 9 blocks in the tower. The total volume is is 3^3 + 2^3 + 7Β·1^3 = 27 + 8 + 7 = 42.
def c(n): if n < 8: return n, n t1 = int(n ** (1 / 3) + 1e-11) t2 = t1 - 1 v1 = c(n - t1 * t1 * t1) v1 = v1[0] + 1, v1[1] + t1 * t1 * t1 v2 = c(t1 * t1 * t1 - 1 - t2 * t2 * t2) v2 = v2[0] + 1, v2[1] + t2 * t2 * t2 if v2 > v1: v1 = v2 return v1 print(" ".join(map(str, c(int(input())))))
FUNC_DEF IF VAR NUMBER RETURN VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP VAR BIN_OP NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR BIN_OP BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER BIN_OP BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR VAR NUMBER BIN_OP BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER BIN_OP BIN_OP VAR VAR VAR IF VAR VAR ASSIGN VAR VAR RETURN VAR EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR
Limak is a little polar bear. He plays by building towers from blocks. Every block is a cube with positive integer length of side. Limak has infinitely many blocks of each side length. A block with side a has volume a^3. A tower consisting of blocks with sides a_1, a_2, ..., a_{k} has the total volume a_1^3 + a_2^3 + ... + a_{k}^3. Limak is going to build a tower. First, he asks you to tell him a positive integer XΒ β€” the required total volume of the tower. Then, Limak adds new blocks greedily, one by one. Each time he adds the biggest block such that the total volume doesn't exceed X. Limak asks you to choose X not greater than m. Also, he wants to maximize the number of blocks in the tower at the end (however, he still behaves greedily). Secondarily, he wants to maximize X. Can you help Limak? Find the maximum number of blocks his tower can have and the maximum X ≀ m that results this number of blocks. -----Input----- The only line of the input contains one integer m (1 ≀ m ≀ 10^15), meaning that Limak wants you to choose X between 1 and m, inclusive. -----Output----- Print two integersΒ β€” the maximum number of blocks in the tower and the maximum required total volume X, resulting in the maximum number of blocks. -----Examples----- Input 48 Output 9 42 Input 6 Output 6 6 -----Note----- In the first sample test, there will be 9 blocks if you choose X = 23 or X = 42. Limak wants to maximize X secondarily so you should choose 42. In more detail, after choosing X = 42 the process of building a tower is: Limak takes a block with side 3 because it's the biggest block with volume not greater than 42. The remaining volume is 42 - 27 = 15. The second added block has side 2, so the remaining volume is 15 - 8 = 7. Finally, Limak adds 7 blocks with side 1, one by one. So, there are 9 blocks in the tower. The total volume is is 3^3 + 2^3 + 7Β·1^3 = 27 + 8 + 7 = 42.
MAX = 100005 def big_pow3(n): l = 0 p = MAX while p - l != 1: mid = (p + l + 1) // 2 if mid * mid * mid <= n: l = mid else: p = mid return l def f(n): if n < 8: return [n, n] a = int((n + 0.5) ** 0.3333333333333333) r1 = f(n - a**3) r1 = [r1[0] + 1, r1[1] + a**3] a -= 1 r2 = f(3 * a * (a + 1)) r2 = [r2[0] + 1, r2[1] + a**3] return max(r1, r2) def __starting_point(): m = int(input()) print(*f(m)) __starting_point()
ASSIGN VAR NUMBER FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR VAR WHILE BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER NUMBER IF BIN_OP BIN_OP VAR VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR RETURN VAR FUNC_DEF IF VAR NUMBER RETURN LIST VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER ASSIGN VAR LIST BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR LIST BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER BIN_OP VAR NUMBER RETURN FUNC_CALL VAR VAR VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR
Limak is a little polar bear. He plays by building towers from blocks. Every block is a cube with positive integer length of side. Limak has infinitely many blocks of each side length. A block with side a has volume a^3. A tower consisting of blocks with sides a_1, a_2, ..., a_{k} has the total volume a_1^3 + a_2^3 + ... + a_{k}^3. Limak is going to build a tower. First, he asks you to tell him a positive integer XΒ β€” the required total volume of the tower. Then, Limak adds new blocks greedily, one by one. Each time he adds the biggest block such that the total volume doesn't exceed X. Limak asks you to choose X not greater than m. Also, he wants to maximize the number of blocks in the tower at the end (however, he still behaves greedily). Secondarily, he wants to maximize X. Can you help Limak? Find the maximum number of blocks his tower can have and the maximum X ≀ m that results this number of blocks. -----Input----- The only line of the input contains one integer m (1 ≀ m ≀ 10^15), meaning that Limak wants you to choose X between 1 and m, inclusive. -----Output----- Print two integersΒ β€” the maximum number of blocks in the tower and the maximum required total volume X, resulting in the maximum number of blocks. -----Examples----- Input 48 Output 9 42 Input 6 Output 6 6 -----Note----- In the first sample test, there will be 9 blocks if you choose X = 23 or X = 42. Limak wants to maximize X secondarily so you should choose 42. In more detail, after choosing X = 42 the process of building a tower is: Limak takes a block with side 3 because it's the biggest block with volume not greater than 42. The remaining volume is 42 - 27 = 15. The second added block has side 2, so the remaining volume is 15 - 8 = 7. Finally, Limak adds 7 blocks with side 1, one by one. So, there are 9 blocks in the tower. The total volume is is 3^3 + 2^3 + 7Β·1^3 = 27 + 8 + 7 = 42.
def cube(a): return a**3 def steps(m): if m <= 7: return m x = 1 while cube(x + 1) <= m: x += 1 return 1 + steps(max(m - cube(x), cube(x) - 1 - cube(x - 1))) class CodeforcesTask679BSolution: def __init__(self): self.result = "" self.m = 0 def read_input(self): self.m = int(input()) def process_task(self): sub = 0 ste = 0 while self.m: ste += 1 x = 1 while cube(x + 1) <= self.m: x += 1 if steps(self.m) == 1 + steps(self.m - cube(x)): self.m -= cube(x) sub += cube(x) else: self.m = cube(x) - 1 - cube(x - 1) sub += cube(x - 1) self.result = "{0} {1}".format(ste, sub) def get_result(self): return self.result Solution = CodeforcesTask679BSolution() Solution.read_input() Solution.process_task() print(Solution.get_result())
FUNC_DEF RETURN BIN_OP VAR NUMBER FUNC_DEF IF VAR NUMBER RETURN VAR ASSIGN VAR NUMBER WHILE FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR NUMBER RETURN BIN_OP NUMBER FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR FUNC_CALL VAR VAR BIN_OP BIN_OP FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER CLASS_DEF FUNC_DEF ASSIGN VAR STRING ASSIGN VAR NUMBER FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR NUMBER ASSIGN VAR NUMBER WHILE FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR NUMBER IF FUNC_CALL VAR VAR BIN_OP NUMBER FUNC_CALL VAR BIN_OP VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL STRING VAR VAR FUNC_DEF RETURN VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR
Limak is a little polar bear. He plays by building towers from blocks. Every block is a cube with positive integer length of side. Limak has infinitely many blocks of each side length. A block with side a has volume a^3. A tower consisting of blocks with sides a_1, a_2, ..., a_{k} has the total volume a_1^3 + a_2^3 + ... + a_{k}^3. Limak is going to build a tower. First, he asks you to tell him a positive integer XΒ β€” the required total volume of the tower. Then, Limak adds new blocks greedily, one by one. Each time he adds the biggest block such that the total volume doesn't exceed X. Limak asks you to choose X not greater than m. Also, he wants to maximize the number of blocks in the tower at the end (however, he still behaves greedily). Secondarily, he wants to maximize X. Can you help Limak? Find the maximum number of blocks his tower can have and the maximum X ≀ m that results this number of blocks. -----Input----- The only line of the input contains one integer m (1 ≀ m ≀ 10^15), meaning that Limak wants you to choose X between 1 and m, inclusive. -----Output----- Print two integersΒ β€” the maximum number of blocks in the tower and the maximum required total volume X, resulting in the maximum number of blocks. -----Examples----- Input 48 Output 9 42 Input 6 Output 6 6 -----Note----- In the first sample test, there will be 9 blocks if you choose X = 23 or X = 42. Limak wants to maximize X secondarily so you should choose 42. In more detail, after choosing X = 42 the process of building a tower is: Limak takes a block with side 3 because it's the biggest block with volume not greater than 42. The remaining volume is 42 - 27 = 15. The second added block has side 2, so the remaining volume is 15 - 8 = 7. Finally, Limak adds 7 blocks with side 1, one by one. So, there are 9 blocks in the tower. The total volume is is 3^3 + 2^3 + 7Β·1^3 = 27 + 8 + 7 = 42.
def func(n): if n < 8: return n, n max_a = int(n ** (1 / 3)) if (max_a + 1) ** 3 <= n: max_a += 1 v1 = func(n - max_a**3) v1 = v1[0] + 1, v1[1] + max_a**3 v2 = func(max_a**3 - 1 - (max_a - 1) ** 3) v2 = v2[0] + 1, v2[1] + (max_a - 1) ** 3 if v2 > v1: return v2 else: return v1 print(" ".join(map(str, func(int(input())))))
FUNC_DEF IF VAR NUMBER RETURN VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR BIN_OP NUMBER NUMBER IF BIN_OP BIN_OP VAR NUMBER NUMBER VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP VAR NUMBER NUMBER BIN_OP BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER BIN_OP BIN_OP VAR NUMBER NUMBER IF VAR VAR RETURN VAR RETURN VAR EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR
Limak is a little polar bear. He plays by building towers from blocks. Every block is a cube with positive integer length of side. Limak has infinitely many blocks of each side length. A block with side a has volume a^3. A tower consisting of blocks with sides a_1, a_2, ..., a_{k} has the total volume a_1^3 + a_2^3 + ... + a_{k}^3. Limak is going to build a tower. First, he asks you to tell him a positive integer XΒ β€” the required total volume of the tower. Then, Limak adds new blocks greedily, one by one. Each time he adds the biggest block such that the total volume doesn't exceed X. Limak asks you to choose X not greater than m. Also, he wants to maximize the number of blocks in the tower at the end (however, he still behaves greedily). Secondarily, he wants to maximize X. Can you help Limak? Find the maximum number of blocks his tower can have and the maximum X ≀ m that results this number of blocks. -----Input----- The only line of the input contains one integer m (1 ≀ m ≀ 10^15), meaning that Limak wants you to choose X between 1 and m, inclusive. -----Output----- Print two integersΒ β€” the maximum number of blocks in the tower and the maximum required total volume X, resulting in the maximum number of blocks. -----Examples----- Input 48 Output 9 42 Input 6 Output 6 6 -----Note----- In the first sample test, there will be 9 blocks if you choose X = 23 or X = 42. Limak wants to maximize X secondarily so you should choose 42. In more detail, after choosing X = 42 the process of building a tower is: Limak takes a block with side 3 because it's the biggest block with volume not greater than 42. The remaining volume is 42 - 27 = 15. The second added block has side 2, so the remaining volume is 15 - 8 = 7. Finally, Limak adds 7 blocks with side 1, one by one. So, there are 9 blocks in the tower. The total volume is is 3^3 + 2^3 + 7Β·1^3 = 27 + 8 + 7 = 42.
import sys def cube_root(x): v = max(int(x ** (1.0 / 3.0)) - 1, 0) while (v + 1) ** 3 <= x: v += 1 return v def solution(x): if x <= 7: return x, x next_smaller = cube_root(x) ** 3 candidate = solution(x - next_smaller) candidate = candidate[0] + 1, candidate[1] + next_smaller prenext_smaller = cube_root(next_smaller - 1) ** 3 if next_smaller - prenext_smaller > x - next_smaller: candidate2 = solution(next_smaller - 1) else: candidate2 = candidate if candidate >= candidate2: return candidate else: return candidate2 n = int(input()) s = solution(n) print(s[0], s[1])
IMPORT FUNC_DEF ASSIGN VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR BIN_OP VAR BIN_OP NUMBER NUMBER NUMBER NUMBER WHILE BIN_OP BIN_OP VAR NUMBER NUMBER VAR VAR NUMBER RETURN VAR FUNC_DEF IF VAR NUMBER RETURN VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER IF BIN_OP VAR VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR IF VAR VAR RETURN VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR NUMBER VAR NUMBER
Limak is a little polar bear. He plays by building towers from blocks. Every block is a cube with positive integer length of side. Limak has infinitely many blocks of each side length. A block with side a has volume a^3. A tower consisting of blocks with sides a_1, a_2, ..., a_{k} has the total volume a_1^3 + a_2^3 + ... + a_{k}^3. Limak is going to build a tower. First, he asks you to tell him a positive integer XΒ β€” the required total volume of the tower. Then, Limak adds new blocks greedily, one by one. Each time he adds the biggest block such that the total volume doesn't exceed X. Limak asks you to choose X not greater than m. Also, he wants to maximize the number of blocks in the tower at the end (however, he still behaves greedily). Secondarily, he wants to maximize X. Can you help Limak? Find the maximum number of blocks his tower can have and the maximum X ≀ m that results this number of blocks. -----Input----- The only line of the input contains one integer m (1 ≀ m ≀ 10^15), meaning that Limak wants you to choose X between 1 and m, inclusive. -----Output----- Print two integersΒ β€” the maximum number of blocks in the tower and the maximum required total volume X, resulting in the maximum number of blocks. -----Examples----- Input 48 Output 9 42 Input 6 Output 6 6 -----Note----- In the first sample test, there will be 9 blocks if you choose X = 23 or X = 42. Limak wants to maximize X secondarily so you should choose 42. In more detail, after choosing X = 42 the process of building a tower is: Limak takes a block with side 3 because it's the biggest block with volume not greater than 42. The remaining volume is 42 - 27 = 15. The second added block has side 2, so the remaining volume is 15 - 8 = 7. Finally, Limak adds 7 blocks with side 1, one by one. So, there are 9 blocks in the tower. The total volume is is 3^3 + 2^3 + 7Β·1^3 = 27 + 8 + 7 = 42.
n, c = 0, 0 def dfs(now, m, t): global n, c if now == 0: if m > n: n, c = m, t return i = 1 while i**3 <= now: i += 1 i -= 1 dfs(now - i**3, m + 1, t + i**3) dfs(i**3 - 1 - (i - 1) ** 3, m + 1, t + (i - 1) ** 3) m = int(input()) dfs(m, 0, 0) print(n, c)
ASSIGN VAR VAR NUMBER NUMBER FUNC_DEF IF VAR NUMBER IF VAR VAR ASSIGN VAR VAR VAR VAR RETURN ASSIGN VAR NUMBER WHILE BIN_OP VAR NUMBER VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP VAR NUMBER NUMBER BIN_OP BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER BIN_OP VAR BIN_OP BIN_OP VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR VAR
Limak is a little polar bear. He plays by building towers from blocks. Every block is a cube with positive integer length of side. Limak has infinitely many blocks of each side length. A block with side a has volume a^3. A tower consisting of blocks with sides a_1, a_2, ..., a_{k} has the total volume a_1^3 + a_2^3 + ... + a_{k}^3. Limak is going to build a tower. First, he asks you to tell him a positive integer XΒ β€” the required total volume of the tower. Then, Limak adds new blocks greedily, one by one. Each time he adds the biggest block such that the total volume doesn't exceed X. Limak asks you to choose X not greater than m. Also, he wants to maximize the number of blocks in the tower at the end (however, he still behaves greedily). Secondarily, he wants to maximize X. Can you help Limak? Find the maximum number of blocks his tower can have and the maximum X ≀ m that results this number of blocks. -----Input----- The only line of the input contains one integer m (1 ≀ m ≀ 10^15), meaning that Limak wants you to choose X between 1 and m, inclusive. -----Output----- Print two integersΒ β€” the maximum number of blocks in the tower and the maximum required total volume X, resulting in the maximum number of blocks. -----Examples----- Input 48 Output 9 42 Input 6 Output 6 6 -----Note----- In the first sample test, there will be 9 blocks if you choose X = 23 or X = 42. Limak wants to maximize X secondarily so you should choose 42. In more detail, after choosing X = 42 the process of building a tower is: Limak takes a block with side 3 because it's the biggest block with volume not greater than 42. The remaining volume is 42 - 27 = 15. The second added block has side 2, so the remaining volume is 15 - 8 = 7. Finally, Limak adds 7 blocks with side 1, one by one. So, there are 9 blocks in the tower. The total volume is is 3^3 + 2^3 + 7Β·1^3 = 27 + 8 + 7 = 42.
def g(m, s, n): k = int(m ** (1 / 3)) if m == 0 or k == 1: return s + m, n + m x, y = k**3, (k - 1) ** 3 a = g(m - x, s + x, n + 1) b = g(x - y - 1, s + y, n + 1) return b if a[1] < b[1] else a s, n = g(int(input()), 0, 0) print(n, s)
FUNC_DEF ASSIGN VAR FUNC_CALL VAR BIN_OP VAR BIN_OP NUMBER NUMBER IF VAR NUMBER VAR NUMBER RETURN BIN_OP VAR VAR BIN_OP VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER BIN_OP BIN_OP VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER BIN_OP VAR VAR BIN_OP VAR NUMBER RETURN VAR NUMBER VAR NUMBER VAR VAR ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR VAR
Limak is a little polar bear. He plays by building towers from blocks. Every block is a cube with positive integer length of side. Limak has infinitely many blocks of each side length. A block with side a has volume a^3. A tower consisting of blocks with sides a_1, a_2, ..., a_{k} has the total volume a_1^3 + a_2^3 + ... + a_{k}^3. Limak is going to build a tower. First, he asks you to tell him a positive integer XΒ β€” the required total volume of the tower. Then, Limak adds new blocks greedily, one by one. Each time he adds the biggest block such that the total volume doesn't exceed X. Limak asks you to choose X not greater than m. Also, he wants to maximize the number of blocks in the tower at the end (however, he still behaves greedily). Secondarily, he wants to maximize X. Can you help Limak? Find the maximum number of blocks his tower can have and the maximum X ≀ m that results this number of blocks. -----Input----- The only line of the input contains one integer m (1 ≀ m ≀ 10^15), meaning that Limak wants you to choose X between 1 and m, inclusive. -----Output----- Print two integersΒ β€” the maximum number of blocks in the tower and the maximum required total volume X, resulting in the maximum number of blocks. -----Examples----- Input 48 Output 9 42 Input 6 Output 6 6 -----Note----- In the first sample test, there will be 9 blocks if you choose X = 23 or X = 42. Limak wants to maximize X secondarily so you should choose 42. In more detail, after choosing X = 42 the process of building a tower is: Limak takes a block with side 3 because it's the biggest block with volume not greater than 42. The remaining volume is 42 - 27 = 15. The second added block has side 2, so the remaining volume is 15 - 8 = 7. Finally, Limak adds 7 blocks with side 1, one by one. So, there are 9 blocks in the tower. The total volume is is 3^3 + 2^3 + 7Β·1^3 = 27 + 8 + 7 = 42.
im = int(input()) best_steps = 0 best_length = 0 def rec(m, steps, substracted): global best_steps, best_length if m == 0: if steps > best_steps: best_steps = steps best_length = substracted elif steps == best_steps: best_length = max(best_length, substracted) return a = 1 while (a + 1) ** 3 <= m: a += 1 rec(m - a**3, steps + 1, substracted + a**3) if a - 1 != 0: rec(a**3 - 1 - (a - 1) ** 3, steps + 1, substracted + (a - 1) ** 3) rec(im, 0, 0) print(best_steps, best_length)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FUNC_DEF IF VAR NUMBER IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN ASSIGN VAR NUMBER WHILE BIN_OP BIN_OP VAR NUMBER NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER IF BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP VAR NUMBER NUMBER BIN_OP BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER BIN_OP VAR BIN_OP BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR VAR
Limak is a little polar bear. He plays by building towers from blocks. Every block is a cube with positive integer length of side. Limak has infinitely many blocks of each side length. A block with side a has volume a^3. A tower consisting of blocks with sides a_1, a_2, ..., a_{k} has the total volume a_1^3 + a_2^3 + ... + a_{k}^3. Limak is going to build a tower. First, he asks you to tell him a positive integer XΒ β€” the required total volume of the tower. Then, Limak adds new blocks greedily, one by one. Each time he adds the biggest block such that the total volume doesn't exceed X. Limak asks you to choose X not greater than m. Also, he wants to maximize the number of blocks in the tower at the end (however, he still behaves greedily). Secondarily, he wants to maximize X. Can you help Limak? Find the maximum number of blocks his tower can have and the maximum X ≀ m that results this number of blocks. -----Input----- The only line of the input contains one integer m (1 ≀ m ≀ 10^15), meaning that Limak wants you to choose X between 1 and m, inclusive. -----Output----- Print two integersΒ β€” the maximum number of blocks in the tower and the maximum required total volume X, resulting in the maximum number of blocks. -----Examples----- Input 48 Output 9 42 Input 6 Output 6 6 -----Note----- In the first sample test, there will be 9 blocks if you choose X = 23 or X = 42. Limak wants to maximize X secondarily so you should choose 42. In more detail, after choosing X = 42 the process of building a tower is: Limak takes a block with side 3 because it's the biggest block with volume not greater than 42. The remaining volume is 42 - 27 = 15. The second added block has side 2, so the remaining volume is 15 - 8 = 7. Finally, Limak adds 7 blocks with side 1, one by one. So, there are 9 blocks in the tower. The total volume is is 3^3 + 2^3 + 7Β·1^3 = 27 + 8 + 7 = 42.
def f(m, cnt, y): global ans, x if m == 0: ans, x = max((ans, x), (cnt, y)) return a = int(m ** (1 / 3)) k1, k2 = a**3, (a - 1) ** 3 f(m - k1, cnt + 1, y + k1) f(k1 - k2 - 1, cnt + 1, y + k2) m = int(input()) ans, x = 0, m f(m, 0, 0) print(ans, x)
FUNC_DEF IF VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR VAR RETURN ASSIGN VAR FUNC_CALL VAR BIN_OP VAR BIN_OP NUMBER NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER BIN_OP BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP VAR NUMBER BIN_OP VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR NUMBER VAR EXPR FUNC_CALL VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR VAR
Limak is a little polar bear. He plays by building towers from blocks. Every block is a cube with positive integer length of side. Limak has infinitely many blocks of each side length. A block with side a has volume a^3. A tower consisting of blocks with sides a_1, a_2, ..., a_{k} has the total volume a_1^3 + a_2^3 + ... + a_{k}^3. Limak is going to build a tower. First, he asks you to tell him a positive integer XΒ β€” the required total volume of the tower. Then, Limak adds new blocks greedily, one by one. Each time he adds the biggest block such that the total volume doesn't exceed X. Limak asks you to choose X not greater than m. Also, he wants to maximize the number of blocks in the tower at the end (however, he still behaves greedily). Secondarily, he wants to maximize X. Can you help Limak? Find the maximum number of blocks his tower can have and the maximum X ≀ m that results this number of blocks. -----Input----- The only line of the input contains one integer m (1 ≀ m ≀ 10^15), meaning that Limak wants you to choose X between 1 and m, inclusive. -----Output----- Print two integersΒ β€” the maximum number of blocks in the tower and the maximum required total volume X, resulting in the maximum number of blocks. -----Examples----- Input 48 Output 9 42 Input 6 Output 6 6 -----Note----- In the first sample test, there will be 9 blocks if you choose X = 23 or X = 42. Limak wants to maximize X secondarily so you should choose 42. In more detail, after choosing X = 42 the process of building a tower is: Limak takes a block with side 3 because it's the biggest block with volume not greater than 42. The remaining volume is 42 - 27 = 15. The second added block has side 2, so the remaining volume is 15 - 8 = 7. Finally, Limak adds 7 blocks with side 1, one by one. So, there are 9 blocks in the tower. The total volume is is 3^3 + 2^3 + 7Β·1^3 = 27 + 8 + 7 = 42.
def g(m, n, s): if not m: return n, s k = int(m ** (1 / 3)) x, y = k**3, (k - 1) ** 3 return max(g(m - x, n + 1, s + x), g(x - y - 1, n + 1, s + y)) print(*g(int(input()), 0, 0))
FUNC_DEF IF VAR RETURN VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR BIN_OP NUMBER NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER BIN_OP BIN_OP VAR NUMBER NUMBER RETURN FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP VAR NUMBER BIN_OP VAR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER NUMBER
Limak is a little polar bear. He plays by building towers from blocks. Every block is a cube with positive integer length of side. Limak has infinitely many blocks of each side length. A block with side a has volume a^3. A tower consisting of blocks with sides a_1, a_2, ..., a_{k} has the total volume a_1^3 + a_2^3 + ... + a_{k}^3. Limak is going to build a tower. First, he asks you to tell him a positive integer XΒ β€” the required total volume of the tower. Then, Limak adds new blocks greedily, one by one. Each time he adds the biggest block such that the total volume doesn't exceed X. Limak asks you to choose X not greater than m. Also, he wants to maximize the number of blocks in the tower at the end (however, he still behaves greedily). Secondarily, he wants to maximize X. Can you help Limak? Find the maximum number of blocks his tower can have and the maximum X ≀ m that results this number of blocks. -----Input----- The only line of the input contains one integer m (1 ≀ m ≀ 10^15), meaning that Limak wants you to choose X between 1 and m, inclusive. -----Output----- Print two integersΒ β€” the maximum number of blocks in the tower and the maximum required total volume X, resulting in the maximum number of blocks. -----Examples----- Input 48 Output 9 42 Input 6 Output 6 6 -----Note----- In the first sample test, there will be 9 blocks if you choose X = 23 or X = 42. Limak wants to maximize X secondarily so you should choose 42. In more detail, after choosing X = 42 the process of building a tower is: Limak takes a block with side 3 because it's the biggest block with volume not greater than 42. The remaining volume is 42 - 27 = 15. The second added block has side 2, so the remaining volume is 15 - 8 = 7. Finally, Limak adds 7 blocks with side 1, one by one. So, there are 9 blocks in the tower. The total volume is is 3^3 + 2^3 + 7Β·1^3 = 27 + 8 + 7 = 42.
def kil(n): if n < 8: return n, n m = 2 while m * m * m <= n: m += 1 m -= 1 k1, v1 = kil(n - m * m * m) k2, v2 = kil(m * m * m - 1 - (m - 1) * (m - 1) * (m - 1)) return ( (k1 + 1, v1 + m * m * m) if (k1, v1 + m * m * m) > (k2, v2 + (m - 1) * (m - 1) * (m - 1)) else (k2 + 1, v2 + (m - 1) * (m - 1) * (m - 1)) ) n = int(input()) print(*kil(n))
FUNC_DEF IF VAR NUMBER RETURN VAR VAR ASSIGN VAR NUMBER WHILE BIN_OP BIN_OP VAR VAR VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR BIN_OP VAR BIN_OP BIN_OP VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR VAR NUMBER BIN_OP BIN_OP BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR NUMBER RETURN VAR BIN_OP VAR BIN_OP BIN_OP VAR VAR VAR VAR BIN_OP VAR BIN_OP BIN_OP BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR NUMBER BIN_OP VAR BIN_OP BIN_OP BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR
Limak is a little polar bear. He plays by building towers from blocks. Every block is a cube with positive integer length of side. Limak has infinitely many blocks of each side length. A block with side a has volume a^3. A tower consisting of blocks with sides a_1, a_2, ..., a_{k} has the total volume a_1^3 + a_2^3 + ... + a_{k}^3. Limak is going to build a tower. First, he asks you to tell him a positive integer XΒ β€” the required total volume of the tower. Then, Limak adds new blocks greedily, one by one. Each time he adds the biggest block such that the total volume doesn't exceed X. Limak asks you to choose X not greater than m. Also, he wants to maximize the number of blocks in the tower at the end (however, he still behaves greedily). Secondarily, he wants to maximize X. Can you help Limak? Find the maximum number of blocks his tower can have and the maximum X ≀ m that results this number of blocks. -----Input----- The only line of the input contains one integer m (1 ≀ m ≀ 10^15), meaning that Limak wants you to choose X between 1 and m, inclusive. -----Output----- Print two integersΒ β€” the maximum number of blocks in the tower and the maximum required total volume X, resulting in the maximum number of blocks. -----Examples----- Input 48 Output 9 42 Input 6 Output 6 6 -----Note----- In the first sample test, there will be 9 blocks if you choose X = 23 or X = 42. Limak wants to maximize X secondarily so you should choose 42. In more detail, after choosing X = 42 the process of building a tower is: Limak takes a block with side 3 because it's the biggest block with volume not greater than 42. The remaining volume is 42 - 27 = 15. The second added block has side 2, so the remaining volume is 15 - 8 = 7. Finally, Limak adds 7 blocks with side 1, one by one. So, there are 9 blocks in the tower. The total volume is is 3^3 + 2^3 + 7Β·1^3 = 27 + 8 + 7 = 42.
def main(): def f(n): if n < 8: return [n, n] a = int((n + 0.5) ** 0.3333333333333333) r1 = f(n - a * a * a) r1[1] += a * a * a a -= 1 r2 = f(3 * a * (a + 1)) r2[1] += a * a * a if r1 < r2: r1 = r2 r1[0] += 1 return r1 print(*f(int(input()))) def __starting_point(): main() __starting_point()
FUNC_DEF FUNC_DEF IF VAR NUMBER RETURN LIST VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR BIN_OP BIN_OP VAR VAR VAR VAR NUMBER BIN_OP BIN_OP VAR VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP NUMBER VAR BIN_OP VAR NUMBER VAR NUMBER BIN_OP BIN_OP VAR VAR VAR IF VAR VAR ASSIGN VAR VAR VAR NUMBER NUMBER RETURN VAR EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_DEF EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR
Recently in Divanovo, a huge river locks system was built. There are now $n$ locks, the $i$-th of them has the volume of $v_i$ liters, so that it can contain any amount of water between $0$ and $v_i$ liters. Each lock has a pipe attached to it. When the pipe is open, $1$ liter of water enters the lock every second. The locks system is built in a way to immediately transfer all water exceeding the volume of the lock $i$ to the lock $i + 1$. If the lock $i + 1$ is also full, water will be transferred further. Water exceeding the volume of the last lock pours out to the river. The picture illustrates $5$ locks with two open pipes at locks $1$ and $3$. Because locks $1$, $3$, and $4$ are already filled, effectively the water goes to locks $2$ and $5$. Note that the volume of the $i$-th lock may be greater than the volume of the $i + 1$-th lock. To make all locks work, you need to completely fill each one of them. The mayor of Divanovo is interested in $q$ independent queries. For each query, suppose that initially all locks are empty and all pipes are closed. Then, some pipes are opened simultaneously. For the $j$-th query the mayor asks you to calculate the minimum number of pipes to open so that all locks are filled no later than after $t_j$ seconds. Please help the mayor to solve this tricky problem and answer his queries. -----Input----- The first lines contains one integer $n$ ($1 \le n \le 200000$) β€” the number of locks. The second lines contains $n$ integers $v_1, v_2, \dots, v_n$ ($1 \le v_i \le 10^9$)) β€” volumes of the locks. The third line contains one integer $q$ ($1 \le q \le 200000$) β€” the number of queries. Each of the next $q$ lines contains one integer $t_j$ ($1 \le t_j \le 10^9$) β€” the number of seconds you have to fill all the locks in the query $j$. -----Output----- Print $q$ integers. The $j$-th of them should be equal to the minimum number of pipes to turn on so that after $t_j$ seconds all of the locks are filled. If it is impossible to fill all of the locks in given time, print $-1$. -----Examples----- Input 5 4 1 5 4 1 6 1 6 2 3 4 5 Output -1 3 -1 -1 4 3 Input 5 4 4 4 4 4 6 1 3 6 5 2 4 Output -1 -1 4 4 -1 5 -----Note----- There are $6$ queries in the first example test. In the queries $1, 3, 4$ the answer is $-1$. We need to wait $4$ seconds to fill the first lock even if we open all the pipes. In the sixth query we can open pipes in locks $1$, $3$, and $4$. After $4$ seconds the locks $1$ and $4$ are full. In the following $1$ second $1$ liter of water is transferred to the locks $2$ and $5$. The lock $3$ is filled by its own pipe. Similarly, in the second query one can open pipes in locks $1$, $3$, and $4$. In the fifth query one can open pipes $1, 2, 3, 4$.
def ceil_div(a, b): return (a - 1) // b + 1 n = int(input()) v = list(map(int, input().split())) p_v = [0] for v_i in v: p_v.append(p_v[-1] + v_i) m = max(ceil_div(p_v[i], i) for i in range(1, n + 1)) q = int(input()) for _ in range(q): t_j = int(input()) if t_j < m: print(-1) else: print(ceil_div(p_v[n], t_j))
FUNC_DEF RETURN BIN_OP BIN_OP BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST NUMBER FOR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR IF VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR
Recently in Divanovo, a huge river locks system was built. There are now $n$ locks, the $i$-th of them has the volume of $v_i$ liters, so that it can contain any amount of water between $0$ and $v_i$ liters. Each lock has a pipe attached to it. When the pipe is open, $1$ liter of water enters the lock every second. The locks system is built in a way to immediately transfer all water exceeding the volume of the lock $i$ to the lock $i + 1$. If the lock $i + 1$ is also full, water will be transferred further. Water exceeding the volume of the last lock pours out to the river. The picture illustrates $5$ locks with two open pipes at locks $1$ and $3$. Because locks $1$, $3$, and $4$ are already filled, effectively the water goes to locks $2$ and $5$. Note that the volume of the $i$-th lock may be greater than the volume of the $i + 1$-th lock. To make all locks work, you need to completely fill each one of them. The mayor of Divanovo is interested in $q$ independent queries. For each query, suppose that initially all locks are empty and all pipes are closed. Then, some pipes are opened simultaneously. For the $j$-th query the mayor asks you to calculate the minimum number of pipes to open so that all locks are filled no later than after $t_j$ seconds. Please help the mayor to solve this tricky problem and answer his queries. -----Input----- The first lines contains one integer $n$ ($1 \le n \le 200000$) β€” the number of locks. The second lines contains $n$ integers $v_1, v_2, \dots, v_n$ ($1 \le v_i \le 10^9$)) β€” volumes of the locks. The third line contains one integer $q$ ($1 \le q \le 200000$) β€” the number of queries. Each of the next $q$ lines contains one integer $t_j$ ($1 \le t_j \le 10^9$) β€” the number of seconds you have to fill all the locks in the query $j$. -----Output----- Print $q$ integers. The $j$-th of them should be equal to the minimum number of pipes to turn on so that after $t_j$ seconds all of the locks are filled. If it is impossible to fill all of the locks in given time, print $-1$. -----Examples----- Input 5 4 1 5 4 1 6 1 6 2 3 4 5 Output -1 3 -1 -1 4 3 Input 5 4 4 4 4 4 6 1 3 6 5 2 4 Output -1 -1 4 4 -1 5 -----Note----- There are $6$ queries in the first example test. In the queries $1, 3, 4$ the answer is $-1$. We need to wait $4$ seconds to fill the first lock even if we open all the pipes. In the sixth query we can open pipes in locks $1$, $3$, and $4$. After $4$ seconds the locks $1$ and $4$ are full. In the following $1$ second $1$ liter of water is transferred to the locks $2$ and $5$. The lock $3$ is filled by its own pipe. Similarly, in the second query one can open pipes in locks $1$, $3$, and $4$. In the fifth query one can open pipes $1, 2, 3, 4$.
num_locks = int(input()) volumn = list(map(int, input().split())) sum_cur = 0 min_t = [] max_min_t = [] for i in range(len(volumn)): sum_cur += volumn[i] t_i = sum_cur // (i + 1) + (sum_cur % (i + 1) > 0) * 1 min_t.append(t_i) if i == 0: max_min_t.append(t_i) else: max_min_t.append(max(max_min_t[-1], t_i)) num_queries = int(input()) sv = sum(volumn) for _ in range(num_queries): t = int(input()) min_open = sv // t + (sv % t > 0) * 1 if min_open > num_locks: print(-1) elif t < max_min_t[min_open - 1]: print(-1) else: print(min_open)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR BIN_OP BIN_OP VAR VAR NUMBER NUMBER IF VAR VAR EXPR FUNC_CALL VAR NUMBER IF VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR
Recently in Divanovo, a huge river locks system was built. There are now $n$ locks, the $i$-th of them has the volume of $v_i$ liters, so that it can contain any amount of water between $0$ and $v_i$ liters. Each lock has a pipe attached to it. When the pipe is open, $1$ liter of water enters the lock every second. The locks system is built in a way to immediately transfer all water exceeding the volume of the lock $i$ to the lock $i + 1$. If the lock $i + 1$ is also full, water will be transferred further. Water exceeding the volume of the last lock pours out to the river. The picture illustrates $5$ locks with two open pipes at locks $1$ and $3$. Because locks $1$, $3$, and $4$ are already filled, effectively the water goes to locks $2$ and $5$. Note that the volume of the $i$-th lock may be greater than the volume of the $i + 1$-th lock. To make all locks work, you need to completely fill each one of them. The mayor of Divanovo is interested in $q$ independent queries. For each query, suppose that initially all locks are empty and all pipes are closed. Then, some pipes are opened simultaneously. For the $j$-th query the mayor asks you to calculate the minimum number of pipes to open so that all locks are filled no later than after $t_j$ seconds. Please help the mayor to solve this tricky problem and answer his queries. -----Input----- The first lines contains one integer $n$ ($1 \le n \le 200000$) β€” the number of locks. The second lines contains $n$ integers $v_1, v_2, \dots, v_n$ ($1 \le v_i \le 10^9$)) β€” volumes of the locks. The third line contains one integer $q$ ($1 \le q \le 200000$) β€” the number of queries. Each of the next $q$ lines contains one integer $t_j$ ($1 \le t_j \le 10^9$) β€” the number of seconds you have to fill all the locks in the query $j$. -----Output----- Print $q$ integers. The $j$-th of them should be equal to the minimum number of pipes to turn on so that after $t_j$ seconds all of the locks are filled. If it is impossible to fill all of the locks in given time, print $-1$. -----Examples----- Input 5 4 1 5 4 1 6 1 6 2 3 4 5 Output -1 3 -1 -1 4 3 Input 5 4 4 4 4 4 6 1 3 6 5 2 4 Output -1 -1 4 4 -1 5 -----Note----- There are $6$ queries in the first example test. In the queries $1, 3, 4$ the answer is $-1$. We need to wait $4$ seconds to fill the first lock even if we open all the pipes. In the sixth query we can open pipes in locks $1$, $3$, and $4$. After $4$ seconds the locks $1$ and $4$ are full. In the following $1$ second $1$ liter of water is transferred to the locks $2$ and $5$. The lock $3$ is filled by its own pipe. Similarly, in the second query one can open pipes in locks $1$, $3$, and $4$. In the fifth query one can open pipes $1, 2, 3, 4$.
import sys n = int(input()) V = [int(i) for i in input().split()] time, total = 0, 0 for i in range(n): total += V[i] time = max(time, (total + i) // (i + 1)) pass for q in range(int(input())): t = int(input()) if t < time: res = -1 else: res = (total + t - 1) // t sys.stdout.write(str(res) + "\n") pass
IMPORT ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR IF VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER VAR EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR STRING
Recently in Divanovo, a huge river locks system was built. There are now $n$ locks, the $i$-th of them has the volume of $v_i$ liters, so that it can contain any amount of water between $0$ and $v_i$ liters. Each lock has a pipe attached to it. When the pipe is open, $1$ liter of water enters the lock every second. The locks system is built in a way to immediately transfer all water exceeding the volume of the lock $i$ to the lock $i + 1$. If the lock $i + 1$ is also full, water will be transferred further. Water exceeding the volume of the last lock pours out to the river. The picture illustrates $5$ locks with two open pipes at locks $1$ and $3$. Because locks $1$, $3$, and $4$ are already filled, effectively the water goes to locks $2$ and $5$. Note that the volume of the $i$-th lock may be greater than the volume of the $i + 1$-th lock. To make all locks work, you need to completely fill each one of them. The mayor of Divanovo is interested in $q$ independent queries. For each query, suppose that initially all locks are empty and all pipes are closed. Then, some pipes are opened simultaneously. For the $j$-th query the mayor asks you to calculate the minimum number of pipes to open so that all locks are filled no later than after $t_j$ seconds. Please help the mayor to solve this tricky problem and answer his queries. -----Input----- The first lines contains one integer $n$ ($1 \le n \le 200000$) β€” the number of locks. The second lines contains $n$ integers $v_1, v_2, \dots, v_n$ ($1 \le v_i \le 10^9$)) β€” volumes of the locks. The third line contains one integer $q$ ($1 \le q \le 200000$) β€” the number of queries. Each of the next $q$ lines contains one integer $t_j$ ($1 \le t_j \le 10^9$) β€” the number of seconds you have to fill all the locks in the query $j$. -----Output----- Print $q$ integers. The $j$-th of them should be equal to the minimum number of pipes to turn on so that after $t_j$ seconds all of the locks are filled. If it is impossible to fill all of the locks in given time, print $-1$. -----Examples----- Input 5 4 1 5 4 1 6 1 6 2 3 4 5 Output -1 3 -1 -1 4 3 Input 5 4 4 4 4 4 6 1 3 6 5 2 4 Output -1 -1 4 4 -1 5 -----Note----- There are $6$ queries in the first example test. In the queries $1, 3, 4$ the answer is $-1$. We need to wait $4$ seconds to fill the first lock even if we open all the pipes. In the sixth query we can open pipes in locks $1$, $3$, and $4$. After $4$ seconds the locks $1$ and $4$ are full. In the following $1$ second $1$ liter of water is transferred to the locks $2$ and $5$. The lock $3$ is filled by its own pipe. Similarly, in the second query one can open pipes in locks $1$, $3$, and $4$. In the fifth query one can open pipes $1, 2, 3, 4$.
import sys input = sys.stdin.readline def solve(): n = int(input()) V = list(map(int, input().split())) arr = [] curr = 0 val = -float("inf") for i in range(n): curr += V[i] val = max(val, curr // (i + 1) + (curr % (i + 1) > 0) * 1) arr.append(val) S = sum(V) q = int(input()) for _ in range(q): tj = int(input()) cnt = S // tj + (S % tj > 0) * 1 print(cnt) if cnt <= n and arr[cnt - 1] <= tj else print(-1) return solve()
IMPORT ASSIGN VAR VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR BIN_OP BIN_OP VAR VAR NUMBER NUMBER EXPR VAR VAR VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR VAR FUNC_CALL VAR NUMBER RETURN EXPR FUNC_CALL VAR
Recently in Divanovo, a huge river locks system was built. There are now $n$ locks, the $i$-th of them has the volume of $v_i$ liters, so that it can contain any amount of water between $0$ and $v_i$ liters. Each lock has a pipe attached to it. When the pipe is open, $1$ liter of water enters the lock every second. The locks system is built in a way to immediately transfer all water exceeding the volume of the lock $i$ to the lock $i + 1$. If the lock $i + 1$ is also full, water will be transferred further. Water exceeding the volume of the last lock pours out to the river. The picture illustrates $5$ locks with two open pipes at locks $1$ and $3$. Because locks $1$, $3$, and $4$ are already filled, effectively the water goes to locks $2$ and $5$. Note that the volume of the $i$-th lock may be greater than the volume of the $i + 1$-th lock. To make all locks work, you need to completely fill each one of them. The mayor of Divanovo is interested in $q$ independent queries. For each query, suppose that initially all locks are empty and all pipes are closed. Then, some pipes are opened simultaneously. For the $j$-th query the mayor asks you to calculate the minimum number of pipes to open so that all locks are filled no later than after $t_j$ seconds. Please help the mayor to solve this tricky problem and answer his queries. -----Input----- The first lines contains one integer $n$ ($1 \le n \le 200000$) β€” the number of locks. The second lines contains $n$ integers $v_1, v_2, \dots, v_n$ ($1 \le v_i \le 10^9$)) β€” volumes of the locks. The third line contains one integer $q$ ($1 \le q \le 200000$) β€” the number of queries. Each of the next $q$ lines contains one integer $t_j$ ($1 \le t_j \le 10^9$) β€” the number of seconds you have to fill all the locks in the query $j$. -----Output----- Print $q$ integers. The $j$-th of them should be equal to the minimum number of pipes to turn on so that after $t_j$ seconds all of the locks are filled. If it is impossible to fill all of the locks in given time, print $-1$. -----Examples----- Input 5 4 1 5 4 1 6 1 6 2 3 4 5 Output -1 3 -1 -1 4 3 Input 5 4 4 4 4 4 6 1 3 6 5 2 4 Output -1 -1 4 4 -1 5 -----Note----- There are $6$ queries in the first example test. In the queries $1, 3, 4$ the answer is $-1$. We need to wait $4$ seconds to fill the first lock even if we open all the pipes. In the sixth query we can open pipes in locks $1$, $3$, and $4$. After $4$ seconds the locks $1$ and $4$ are full. In the following $1$ second $1$ liter of water is transferred to the locks $2$ and $5$. The lock $3$ is filled by its own pipe. Similarly, in the second query one can open pipes in locks $1$, $3$, and $4$. In the fifth query one can open pipes $1, 2, 3, 4$.
import itertools n = int(input()) v = list(itertools.accumulate(map(int, input().split()))) m = max((v[i] - 1) // (i + 1) + 1 for i in range(n)) for _ in range(int(input())): t = int(input()) print((v[-1] - 1) // t + 1 if t >= m else -1)
IMPORT ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER BIN_OP VAR NUMBER NUMBER VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR BIN_OP BIN_OP BIN_OP VAR NUMBER NUMBER VAR NUMBER NUMBER
Recently in Divanovo, a huge river locks system was built. There are now $n$ locks, the $i$-th of them has the volume of $v_i$ liters, so that it can contain any amount of water between $0$ and $v_i$ liters. Each lock has a pipe attached to it. When the pipe is open, $1$ liter of water enters the lock every second. The locks system is built in a way to immediately transfer all water exceeding the volume of the lock $i$ to the lock $i + 1$. If the lock $i + 1$ is also full, water will be transferred further. Water exceeding the volume of the last lock pours out to the river. The picture illustrates $5$ locks with two open pipes at locks $1$ and $3$. Because locks $1$, $3$, and $4$ are already filled, effectively the water goes to locks $2$ and $5$. Note that the volume of the $i$-th lock may be greater than the volume of the $i + 1$-th lock. To make all locks work, you need to completely fill each one of them. The mayor of Divanovo is interested in $q$ independent queries. For each query, suppose that initially all locks are empty and all pipes are closed. Then, some pipes are opened simultaneously. For the $j$-th query the mayor asks you to calculate the minimum number of pipes to open so that all locks are filled no later than after $t_j$ seconds. Please help the mayor to solve this tricky problem and answer his queries. -----Input----- The first lines contains one integer $n$ ($1 \le n \le 200000$) β€” the number of locks. The second lines contains $n$ integers $v_1, v_2, \dots, v_n$ ($1 \le v_i \le 10^9$)) β€” volumes of the locks. The third line contains one integer $q$ ($1 \le q \le 200000$) β€” the number of queries. Each of the next $q$ lines contains one integer $t_j$ ($1 \le t_j \le 10^9$) β€” the number of seconds you have to fill all the locks in the query $j$. -----Output----- Print $q$ integers. The $j$-th of them should be equal to the minimum number of pipes to turn on so that after $t_j$ seconds all of the locks are filled. If it is impossible to fill all of the locks in given time, print $-1$. -----Examples----- Input 5 4 1 5 4 1 6 1 6 2 3 4 5 Output -1 3 -1 -1 4 3 Input 5 4 4 4 4 4 6 1 3 6 5 2 4 Output -1 -1 4 4 -1 5 -----Note----- There are $6$ queries in the first example test. In the queries $1, 3, 4$ the answer is $-1$. We need to wait $4$ seconds to fill the first lock even if we open all the pipes. In the sixth query we can open pipes in locks $1$, $3$, and $4$. After $4$ seconds the locks $1$ and $4$ are full. In the following $1$ second $1$ liter of water is transferred to the locks $2$ and $5$. The lock $3$ is filled by its own pipe. Similarly, in the second query one can open pipes in locks $1$, $3$, and $4$. In the fifth query one can open pipes $1, 2, 3, 4$.
n = int(input()) V = [int(i) for i in input().split()] time, total = 0, 0 for i in range(n): total += V[i] time = max(time, (total + i) // (i + 1)) pass for q in range(int(input())): t = int(input()) if t < time: print(-1) else: print((total + t - 1) // t) pass
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR IF VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER VAR
Recently in Divanovo, a huge river locks system was built. There are now $n$ locks, the $i$-th of them has the volume of $v_i$ liters, so that it can contain any amount of water between $0$ and $v_i$ liters. Each lock has a pipe attached to it. When the pipe is open, $1$ liter of water enters the lock every second. The locks system is built in a way to immediately transfer all water exceeding the volume of the lock $i$ to the lock $i + 1$. If the lock $i + 1$ is also full, water will be transferred further. Water exceeding the volume of the last lock pours out to the river. The picture illustrates $5$ locks with two open pipes at locks $1$ and $3$. Because locks $1$, $3$, and $4$ are already filled, effectively the water goes to locks $2$ and $5$. Note that the volume of the $i$-th lock may be greater than the volume of the $i + 1$-th lock. To make all locks work, you need to completely fill each one of them. The mayor of Divanovo is interested in $q$ independent queries. For each query, suppose that initially all locks are empty and all pipes are closed. Then, some pipes are opened simultaneously. For the $j$-th query the mayor asks you to calculate the minimum number of pipes to open so that all locks are filled no later than after $t_j$ seconds. Please help the mayor to solve this tricky problem and answer his queries. -----Input----- The first lines contains one integer $n$ ($1 \le n \le 200000$) β€” the number of locks. The second lines contains $n$ integers $v_1, v_2, \dots, v_n$ ($1 \le v_i \le 10^9$)) β€” volumes of the locks. The third line contains one integer $q$ ($1 \le q \le 200000$) β€” the number of queries. Each of the next $q$ lines contains one integer $t_j$ ($1 \le t_j \le 10^9$) β€” the number of seconds you have to fill all the locks in the query $j$. -----Output----- Print $q$ integers. The $j$-th of them should be equal to the minimum number of pipes to turn on so that after $t_j$ seconds all of the locks are filled. If it is impossible to fill all of the locks in given time, print $-1$. -----Examples----- Input 5 4 1 5 4 1 6 1 6 2 3 4 5 Output -1 3 -1 -1 4 3 Input 5 4 4 4 4 4 6 1 3 6 5 2 4 Output -1 -1 4 4 -1 5 -----Note----- There are $6$ queries in the first example test. In the queries $1, 3, 4$ the answer is $-1$. We need to wait $4$ seconds to fill the first lock even if we open all the pipes. In the sixth query we can open pipes in locks $1$, $3$, and $4$. After $4$ seconds the locks $1$ and $4$ are full. In the following $1$ second $1$ liter of water is transferred to the locks $2$ and $5$. The lock $3$ is filled by its own pipe. Similarly, in the second query one can open pipes in locks $1$, $3$, and $4$. In the fifth query one can open pipes $1, 2, 3, 4$.
n = int(input()) v = list(map(int, input().split())) assert n == len(v) vs = v[0] ts = [0] * n ts[0] = v[0] for i in range(1, n): vs += v[i] ts[i] = max(ts[i - 1], (vs + i + 1 - 1) // (i + 1)) q = int(input()) for i in range(q): t = int(input()) if t < ts[-1]: print(-1) else: print((vs + t - 1) // t)
ASSIGN VAR FUNC_CALL 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 VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR NUMBER NUMBER BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR IF VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER VAR
Recently in Divanovo, a huge river locks system was built. There are now $n$ locks, the $i$-th of them has the volume of $v_i$ liters, so that it can contain any amount of water between $0$ and $v_i$ liters. Each lock has a pipe attached to it. When the pipe is open, $1$ liter of water enters the lock every second. The locks system is built in a way to immediately transfer all water exceeding the volume of the lock $i$ to the lock $i + 1$. If the lock $i + 1$ is also full, water will be transferred further. Water exceeding the volume of the last lock pours out to the river. The picture illustrates $5$ locks with two open pipes at locks $1$ and $3$. Because locks $1$, $3$, and $4$ are already filled, effectively the water goes to locks $2$ and $5$. Note that the volume of the $i$-th lock may be greater than the volume of the $i + 1$-th lock. To make all locks work, you need to completely fill each one of them. The mayor of Divanovo is interested in $q$ independent queries. For each query, suppose that initially all locks are empty and all pipes are closed. Then, some pipes are opened simultaneously. For the $j$-th query the mayor asks you to calculate the minimum number of pipes to open so that all locks are filled no later than after $t_j$ seconds. Please help the mayor to solve this tricky problem and answer his queries. -----Input----- The first lines contains one integer $n$ ($1 \le n \le 200000$) β€” the number of locks. The second lines contains $n$ integers $v_1, v_2, \dots, v_n$ ($1 \le v_i \le 10^9$)) β€” volumes of the locks. The third line contains one integer $q$ ($1 \le q \le 200000$) β€” the number of queries. Each of the next $q$ lines contains one integer $t_j$ ($1 \le t_j \le 10^9$) β€” the number of seconds you have to fill all the locks in the query $j$. -----Output----- Print $q$ integers. The $j$-th of them should be equal to the minimum number of pipes to turn on so that after $t_j$ seconds all of the locks are filled. If it is impossible to fill all of the locks in given time, print $-1$. -----Examples----- Input 5 4 1 5 4 1 6 1 6 2 3 4 5 Output -1 3 -1 -1 4 3 Input 5 4 4 4 4 4 6 1 3 6 5 2 4 Output -1 -1 4 4 -1 5 -----Note----- There are $6$ queries in the first example test. In the queries $1, 3, 4$ the answer is $-1$. We need to wait $4$ seconds to fill the first lock even if we open all the pipes. In the sixth query we can open pipes in locks $1$, $3$, and $4$. After $4$ seconds the locks $1$ and $4$ are full. In the following $1$ second $1$ liter of water is transferred to the locks $2$ and $5$. The lock $3$ is filled by its own pipe. Similarly, in the second query one can open pipes in locks $1$, $3$, and $4$. In the fifth query one can open pipes $1, 2, 3, 4$.
n = int(input()) v = list(map(int, input().split())) q = int(input()) sm = sum(v) pr = v[0] t = [sm] smp = v[0] for i in range(1, n): smp += v[i] pr = max(pr, pr + (smp - pr * (i + 1) + i) // (i + 1)) t += [max(pr, pr + (sm - (i + 1) * pr + i) // (i + 1))] for _ in range(q): ta = int(input()) if ta < t[-1]: print(-1) else: print((sm + ta - 1) // ta)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR LIST VAR ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR BIN_OP BIN_OP BIN_OP VAR BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR LIST FUNC_CALL VAR VAR BIN_OP VAR BIN_OP BIN_OP BIN_OP VAR BIN_OP BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR IF VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER VAR
Read problems statements in Mandarin Chinese , Russian and Vietnamese Churu is taking the course called β€œIntroduction to Data Structures”. Yesterday, he learned how to use a stack to check is a given parentheses expression is balanced or not. He finds it intriguing, and more importantly, he was given an assignment. The professor gave him a string S containing characters β€œ(” and β€œ)”, and asked him numerous queries of the form (x, y), i.e., if the substring S[x, y] represents a balanced parentheses expression or not. Here, S[x, y] refers to the substring of S from index x to y (both inclusive), assuming 0-based indexing. Diligently working through his assignment, our ace student Churu finds that all the queries given by the professor represented balanced substrings. Later, Churu lost his original string but he has all the queries. Churu wants to restore his original string. As there can be many valid strings, print any one among them. ------ Input ------ First line of input contains an integer T denoting the number of test cases. First line of each of test case contains two space-separated integers: N, K representing the length of the string and number of queries asked by professor, respectively. Each of the next K lines contains a space-separated pair of integers: x and y, denoting a query. ------ Output ------ Print T lines, with the i^{th} one containing the solution string for the i^{th} test case. ------ Constraints ------ Subtask #1: 20 points $1 ≀ T ≀ 5, 2 ≀ N ≀ 16, 1 ≀ K ≀ 20, x ≀ y$ Subtask #2: 80 points $1 ≀ T ≀ 5, 2 ≀ N ≀ 2000, 1 ≀ K ≀ 30, x ≀ y$ ----- Sample Input 1 ------ 2 4 1 0 3 4 2 0 3 1 2 ----- Sample Output 1 ------ ()() (()) ----- explanation 1 ------ For the first sample case, "(())" are "()()" are two possible outputs. Printing anyone will do.
__author__ = "Om Kumar Sahoo" cas = int(input()) for i in range(cas): a, b = [int(x) for x in input().split()] arr = [0] * a for j in range(b): c, d = [int(x) for x in input().split()] flag = 0 for k in range(c, d + 1): if flag == 0: arr[k] = "(" flag = 1 else: arr[k] = ")" flag = 0 for j in range(a): if arr[j] == 0: if flag == 0: arr[j] = "(" flag = 1 else: arr[j] = ")" flag = 0 for x in arr: print(x, end="") print()
ASSIGN VAR STRING 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 BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR STRING ASSIGN VAR NUMBER ASSIGN VAR VAR STRING ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR STRING ASSIGN VAR NUMBER ASSIGN VAR VAR STRING ASSIGN VAR NUMBER FOR VAR VAR EXPR FUNC_CALL VAR VAR STRING EXPR FUNC_CALL VAR
Read problems statements in Mandarin Chinese , Russian and Vietnamese Churu is taking the course called β€œIntroduction to Data Structures”. Yesterday, he learned how to use a stack to check is a given parentheses expression is balanced or not. He finds it intriguing, and more importantly, he was given an assignment. The professor gave him a string S containing characters β€œ(” and β€œ)”, and asked him numerous queries of the form (x, y), i.e., if the substring S[x, y] represents a balanced parentheses expression or not. Here, S[x, y] refers to the substring of S from index x to y (both inclusive), assuming 0-based indexing. Diligently working through his assignment, our ace student Churu finds that all the queries given by the professor represented balanced substrings. Later, Churu lost his original string but he has all the queries. Churu wants to restore his original string. As there can be many valid strings, print any one among them. ------ Input ------ First line of input contains an integer T denoting the number of test cases. First line of each of test case contains two space-separated integers: N, K representing the length of the string and number of queries asked by professor, respectively. Each of the next K lines contains a space-separated pair of integers: x and y, denoting a query. ------ Output ------ Print T lines, with the i^{th} one containing the solution string for the i^{th} test case. ------ Constraints ------ Subtask #1: 20 points $1 ≀ T ≀ 5, 2 ≀ N ≀ 16, 1 ≀ K ≀ 20, x ≀ y$ Subtask #2: 80 points $1 ≀ T ≀ 5, 2 ≀ N ≀ 2000, 1 ≀ K ≀ 30, x ≀ y$ ----- Sample Input 1 ------ 2 4 1 0 3 4 2 0 3 1 2 ----- Sample Output 1 ------ ()() (()) ----- explanation 1 ------ For the first sample case, "(())" are "()()" are two possible outputs. Printing anyone will do.
a = int(input()) for _ in range(a): aa = list(map(int, input().split())) brr = [] for i in range(aa[1]): cc = tuple(map(int, input().split())) brr.append(cc) brr = sorted(brr, key=lambda x: x[1] - x[0]) crr = [0] * aa[0] for i in brr: k = 0 j = i[0] while j <= i[1]: if crr[j] == 0: if k % 2 == 0: crr[j] = "(" else: crr[j] = ")" k += 1 j += 1 j = 0 for i in crr: if i == 0 and j % 2 == 0: print("(", end="") j += 1 elif i == 0 and j % 2 == 1: print(")", end="") j += 1 else: print(i, end="") print("\n", end="")
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR NUMBER FOR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER WHILE VAR VAR NUMBER IF VAR VAR NUMBER IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR STRING ASSIGN VAR VAR STRING VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF VAR NUMBER BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR STRING STRING VAR NUMBER IF VAR NUMBER BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR STRING STRING VAR NUMBER EXPR FUNC_CALL VAR VAR STRING EXPR FUNC_CALL VAR STRING STRING
Read problems statements in Mandarin Chinese , Russian and Vietnamese Churu is taking the course called β€œIntroduction to Data Structures”. Yesterday, he learned how to use a stack to check is a given parentheses expression is balanced or not. He finds it intriguing, and more importantly, he was given an assignment. The professor gave him a string S containing characters β€œ(” and β€œ)”, and asked him numerous queries of the form (x, y), i.e., if the substring S[x, y] represents a balanced parentheses expression or not. Here, S[x, y] refers to the substring of S from index x to y (both inclusive), assuming 0-based indexing. Diligently working through his assignment, our ace student Churu finds that all the queries given by the professor represented balanced substrings. Later, Churu lost his original string but he has all the queries. Churu wants to restore his original string. As there can be many valid strings, print any one among them. ------ Input ------ First line of input contains an integer T denoting the number of test cases. First line of each of test case contains two space-separated integers: N, K representing the length of the string and number of queries asked by professor, respectively. Each of the next K lines contains a space-separated pair of integers: x and y, denoting a query. ------ Output ------ Print T lines, with the i^{th} one containing the solution string for the i^{th} test case. ------ Constraints ------ Subtask #1: 20 points $1 ≀ T ≀ 5, 2 ≀ N ≀ 16, 1 ≀ K ≀ 20, x ≀ y$ Subtask #2: 80 points $1 ≀ T ≀ 5, 2 ≀ N ≀ 2000, 1 ≀ K ≀ 30, x ≀ y$ ----- Sample Input 1 ------ 2 4 1 0 3 4 2 0 3 1 2 ----- Sample Output 1 ------ ()() (()) ----- explanation 1 ------ For the first sample case, "(())" are "()()" are two possible outputs. Printing anyone will do.
for _ in range(int(input())): n, k = map(int, input().split()) ans = "00" * (n // 2) + "0" * (n % 2) ans = list(ans) qry = [] for i in range(k): x, y = map(int, input().split()) qry.append([x, y]) qry.sort(key=lambda x: x[1] - x[0]) for i in range(k): x = qry[i][0] y = qry[i][1] mauka = 0 for i in range(x, y + 1): if mauka == 0 and ans[i] == "0": ans[i] = "(" elif mauka == 1 and ans[i] == "0": ans[i] = ")" mauka = 1 - mauka for i in range(n): if ans[i] == "0" and i % 2: ans[i] = ")" elif ans[i] == "0" and i % 2 == 0: ans[i] = "(" print(*ans, sep="")
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 BIN_OP STRING BIN_OP VAR NUMBER BIN_OP STRING BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR LIST VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER IF VAR NUMBER VAR VAR STRING ASSIGN VAR VAR STRING IF VAR NUMBER VAR VAR STRING ASSIGN VAR VAR STRING ASSIGN VAR BIN_OP NUMBER VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR STRING BIN_OP VAR NUMBER ASSIGN VAR VAR STRING IF VAR VAR STRING BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR STRING EXPR FUNC_CALL VAR VAR STRING
Read problems statements in Mandarin Chinese , Russian and Vietnamese Churu is taking the course called β€œIntroduction to Data Structures”. Yesterday, he learned how to use a stack to check is a given parentheses expression is balanced or not. He finds it intriguing, and more importantly, he was given an assignment. The professor gave him a string S containing characters β€œ(” and β€œ)”, and asked him numerous queries of the form (x, y), i.e., if the substring S[x, y] represents a balanced parentheses expression or not. Here, S[x, y] refers to the substring of S from index x to y (both inclusive), assuming 0-based indexing. Diligently working through his assignment, our ace student Churu finds that all the queries given by the professor represented balanced substrings. Later, Churu lost his original string but he has all the queries. Churu wants to restore his original string. As there can be many valid strings, print any one among them. ------ Input ------ First line of input contains an integer T denoting the number of test cases. First line of each of test case contains two space-separated integers: N, K representing the length of the string and number of queries asked by professor, respectively. Each of the next K lines contains a space-separated pair of integers: x and y, denoting a query. ------ Output ------ Print T lines, with the i^{th} one containing the solution string for the i^{th} test case. ------ Constraints ------ Subtask #1: 20 points $1 ≀ T ≀ 5, 2 ≀ N ≀ 16, 1 ≀ K ≀ 20, x ≀ y$ Subtask #2: 80 points $1 ≀ T ≀ 5, 2 ≀ N ≀ 2000, 1 ≀ K ≀ 30, x ≀ y$ ----- Sample Input 1 ------ 2 4 1 0 3 4 2 0 3 1 2 ----- Sample Output 1 ------ ()() (()) ----- explanation 1 ------ For the first sample case, "(())" are "()()" are two possible outputs. Printing anyone will do.
t = input() for i in range(0, int(t)): n, k = input().split(" ") g = "" h = "" for p in range(0, int(n)): g = g + "." h = list(g) for j in range(0, int(k)): a, b = input().split(" ") h[int(a)] = "(" h[int(b)] = ")" fd = 1 for o in range(int(a) + 1, int(b)): if fd % 2 != 0: h[o] = ")" else: h[o] = "(" fd = fd + 1 for y in range(0, int(n)): if h[y] == ".": h[y] = ")" test = "".join(h) print(test)
ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR STRING ASSIGN VAR STRING FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR STRING ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR VAR STRING ASSIGN VAR FUNC_CALL VAR VAR STRING ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR STRING ASSIGN VAR VAR STRING ASSIGN VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF VAR VAR STRING ASSIGN VAR VAR STRING ASSIGN VAR FUNC_CALL STRING VAR EXPR FUNC_CALL VAR VAR
Read problems statements in Mandarin Chinese , Russian and Vietnamese Churu is taking the course called β€œIntroduction to Data Structures”. Yesterday, he learned how to use a stack to check is a given parentheses expression is balanced or not. He finds it intriguing, and more importantly, he was given an assignment. The professor gave him a string S containing characters β€œ(” and β€œ)”, and asked him numerous queries of the form (x, y), i.e., if the substring S[x, y] represents a balanced parentheses expression or not. Here, S[x, y] refers to the substring of S from index x to y (both inclusive), assuming 0-based indexing. Diligently working through his assignment, our ace student Churu finds that all the queries given by the professor represented balanced substrings. Later, Churu lost his original string but he has all the queries. Churu wants to restore his original string. As there can be many valid strings, print any one among them. ------ Input ------ First line of input contains an integer T denoting the number of test cases. First line of each of test case contains two space-separated integers: N, K representing the length of the string and number of queries asked by professor, respectively. Each of the next K lines contains a space-separated pair of integers: x and y, denoting a query. ------ Output ------ Print T lines, with the i^{th} one containing the solution string for the i^{th} test case. ------ Constraints ------ Subtask #1: 20 points $1 ≀ T ≀ 5, 2 ≀ N ≀ 16, 1 ≀ K ≀ 20, x ≀ y$ Subtask #2: 80 points $1 ≀ T ≀ 5, 2 ≀ N ≀ 2000, 1 ≀ K ≀ 30, x ≀ y$ ----- Sample Input 1 ------ 2 4 1 0 3 4 2 0 3 1 2 ----- Sample Output 1 ------ ()() (()) ----- explanation 1 ------ For the first sample case, "(())" are "()()" are two possible outputs. Printing anyone will do.
def brackets(N, intervals): sequence = ["-" for _ in range(N)] intervals.sort(key=lambda x: x[1]) b = ["(", ")"] for interval in intervals: l, r = interval p = 0 for i in range(l, r + 1): if sequence[i] == "-": sequence[i] = b[p] p = 1 - p return "".join([(s if s != "-" else "(") for s in sequence]) for _ in range(int(input())): N, K = (int(x) for x in input().split()) intervals = [] for k in range(K): l, r = input().split() intervals.append((int(l), int(r))) print(brackets(N, intervals))
FUNC_DEF ASSIGN VAR STRING VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR NUMBER ASSIGN VAR LIST STRING STRING FOR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER IF VAR VAR STRING ASSIGN VAR VAR VAR VAR ASSIGN VAR BIN_OP NUMBER VAR RETURN FUNC_CALL STRING VAR STRING VAR STRING VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR
Chef has a grid of size N \times M. In one move, Chef can move from his current cell to an adjacent cell. If the Chef is currently at (i, j) then, in one move, Chef can move to (i + 1, j), (i - 1, j), (i, j + 1) or (i, j - 1). There are also K *special* cells in the grid. These special cells support a corner move as well. If the Chef is currently at a *special* cell (i, j) then, in one move, Chef can move to (i + 1, j + 1), (i - 1, j - 1), (i + 1, j - 1) or (i - 1, j + 1) in addition to the adjacent cells ((i + 1, j), (i - 1, j), (i, j + 1) or (i, j - 1)). Chef is not allowed to leave the grid at any moment. Determine the minimum number of moves Chef needs to go from (1, 1) to (N, M). ------ Input Format ------ - The first line contains a single integer T β€” the number of test cases. Then the test cases follow. - The first line of each test case contains three space-separated integers N, M and K β€” the number of rows in the grid, the number of columns in the grid and the number of special cells. - K lines follow. The i-th line of these K lines contains two space-separated integers X_{i} and Y_{i} such that (X_{i}, Y_{i}) is the i-th special cell. ------ Output Format ------ For each test case, output the minimum number of moves required by the Chef to go to (N, M) from (1, 1). ------ Constraints ------ $1 ≀ T ≀ 10^{4}$ $1 ≀ N, M ≀ 10^{9}$ $0 ≀ K ≀ 2 \cdot 10^{5}$ $1 ≀ X_{i} ≀ N$, $1 ≀ Y_{i} ≀ M$ - All $(X_{i}, Y_{i})$ are pairwise distinct. - Sum of $K$ over all test cases does not exceed $2 \cdot 10^{5}$. ----- Sample Input 1 ------ 2 3 3 0 3 3 2 2 2 2 3 ----- Sample Output 1 ------ 4 3 ----- explanation 1 ------ Test Case 1: There is no special cell in this case. One of the optimal ways is: $(1,1) \rightarrow (1,2) \rightarrow (1,3) \rightarrow (2,3) \rightarrow (3,3)$. Therefore a total of $4$ moves is required. Test Case 2: There are $2$ special cells in this case. One of the optimal ways is: $(1,1) \rightarrow (2,1) \rightarrow (2,2) \rightarrow (3,3)$. Therefore a total of $3$ moves is required.
def lis(nums, cmp=lambda x, y: x < y): P = [0] * len(nums) M = [0] * (len(nums) + 1) L = 0 for i in range(len(nums)): lo, hi = 1, L while lo <= hi: mid = (lo + hi) // 2 if cmp(nums[M[mid]], nums[i]): lo = mid + 1 else: hi = mid - 1 newL = lo P[i] = M[newL - 1] M[newL] = i L = max(L, newL) S = [0] * L k = M[L] for i in range(L - 1, -1, -1): S[i], k = nums[k], P[k] return S for _ in range(int(input())): n, m, k = map(int, input().split()) cells = [] for i in range(k): x, y = map(int, input().split()) if x < n and y < m: cells.append([x, -y]) cells.sort() vals = [] for x, y in cells: vals.append(-y) print(n - 1 + m - 1 - len(lis(vals)))
FUNC_DEF VAR VAR ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER VAR WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR VAR VAR VAR VAR VAR RETURN VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR VAR VAR VAR EXPR FUNC_CALL VAR LIST VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR NUMBER VAR NUMBER FUNC_CALL VAR FUNC_CALL VAR VAR
Chef has a grid of size N \times M. In one move, Chef can move from his current cell to an adjacent cell. If the Chef is currently at (i, j) then, in one move, Chef can move to (i + 1, j), (i - 1, j), (i, j + 1) or (i, j - 1). There are also K *special* cells in the grid. These special cells support a corner move as well. If the Chef is currently at a *special* cell (i, j) then, in one move, Chef can move to (i + 1, j + 1), (i - 1, j - 1), (i + 1, j - 1) or (i - 1, j + 1) in addition to the adjacent cells ((i + 1, j), (i - 1, j), (i, j + 1) or (i, j - 1)). Chef is not allowed to leave the grid at any moment. Determine the minimum number of moves Chef needs to go from (1, 1) to (N, M). ------ Input Format ------ - The first line contains a single integer T β€” the number of test cases. Then the test cases follow. - The first line of each test case contains three space-separated integers N, M and K β€” the number of rows in the grid, the number of columns in the grid and the number of special cells. - K lines follow. The i-th line of these K lines contains two space-separated integers X_{i} and Y_{i} such that (X_{i}, Y_{i}) is the i-th special cell. ------ Output Format ------ For each test case, output the minimum number of moves required by the Chef to go to (N, M) from (1, 1). ------ Constraints ------ $1 ≀ T ≀ 10^{4}$ $1 ≀ N, M ≀ 10^{9}$ $0 ≀ K ≀ 2 \cdot 10^{5}$ $1 ≀ X_{i} ≀ N$, $1 ≀ Y_{i} ≀ M$ - All $(X_{i}, Y_{i})$ are pairwise distinct. - Sum of $K$ over all test cases does not exceed $2 \cdot 10^{5}$. ----- Sample Input 1 ------ 2 3 3 0 3 3 2 2 2 2 3 ----- Sample Output 1 ------ 4 3 ----- explanation 1 ------ Test Case 1: There is no special cell in this case. One of the optimal ways is: $(1,1) \rightarrow (1,2) \rightarrow (1,3) \rightarrow (2,3) \rightarrow (3,3)$. Therefore a total of $4$ moves is required. Test Case 2: There are $2$ special cells in this case. One of the optimal ways is: $(1,1) \rightarrow (2,1) \rightarrow (2,2) \rightarrow (3,3)$. Therefore a total of $3$ moves is required.
from sys import stdin input = stdin.readline def lis(nums, cmp=lambda x, y: x < y): P = [0] * len(nums) M = [0] * (len(nums) + 1) L = 0 for i in range(len(nums)): lo, hi = 1, L while lo <= hi: mid = (lo + hi) // 2 if cmp(nums[M[mid]], nums[i]): lo = mid + 1 else: hi = mid - 1 newL = lo P[i] = M[newL - 1] M[newL] = i L = max(L, newL) S = [0] * L k = M[L] for i in range(L - 1, -1, -1): S[i], k = nums[k], P[k] return S def solve(N, M, specials): specials = [(X, -Y) for X, Y in specials] specials = sorted(specials) specials = [(-Y) for X, Y in specials] return N + M - 2 - len(lis(specials)) T = int(input().strip()) for problem in range(1, T + 1): N, M, K = [int(x) for x in input().strip().split()] specials = set() for i in range(K): X, Y = [int(x) for x in input().strip().split()] if X < N and Y < M: specials.add((X, Y)) print(solve(N, M, specials))
ASSIGN VAR VAR FUNC_DEF VAR VAR ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER VAR WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR VAR VAR VAR VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR VAR RETURN BIN_OP BIN_OP BIN_OP VAR VAR NUMBER FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR IF VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR
Neil has a [perfect binary tree] with N nodes, and an integer M. He can assign values to nodes. He calls the tree good if the following conditions are met: Nodes' values are positive integers no more than M. Nodes at even levels have values strictly more than their parents' values. Nodes at odd levels have values strictly less than their parents's values. How many ways can Neil assign values to all nodes to get a good perfect binary tree? Since the answer can be large, please output it under modulo 10^{9} + 7. Note: The root of the tree is at layer 1. Two assignments are different if there is at least one node with different values in both assignments. You may need to use 64-bit data types in your programming language to take input. ------ Input Format ------ - The first line of each input contains T - the number of test cases. The test cases then follow. - The only line of each test case contains two space-separated integers N and M - the number of nodes on the tree and the maximum value of any node. ------ Output Format ------ For each test case, output on a single line the number of different assignment modulo 10^{9} + 7. ------ Constraints ------ $1 ≀ T ≀ 100$ $1 ≀ M ≀ 1000$ $1 ≀ N < 2^{59}$ - It is guaranteed you can make a perfect binary tree with exactly $N$ nodes. ----- Sample Input 1 ------ 1 3 3 ----- Sample Output 1 ------ 5 ----- explanation 1 ------ - Test case $1$: Here are all the possible assignments. ![1-2-2] ![1-2-3] ![1-3-2] ![1-3-3] ![2-3-3]
MOD = int(10**9) + 7 def solve(N, M): height = 0 while N > 0: height += 1 N = N // 2 dp = [[(0) for i in range(0, M + 1)] for j in range(0, height + 1)] for j in range(1, M + 1): dp[1][j] = 1 for i in range(2, height + 1): if i & 1 == 0: sum = 0 for j in range(1, M + 1): dp[i][j] += sum * sum sum += dp[i - 1][j] dp[i][j] %= MOD sum %= MOD else: sum = 0 for j in range(M, 0, -1): dp[i][j] += sum * sum sum += dp[i - 1][j] dp[i][j] %= MOD sum %= MOD ans = 0 for i in range(1, M + 1): ans += dp[height][i] ans %= MOD print(ans) T = int(input()) for _ in range(T): N, M = map(int, input().split()) solve(N, M)
ASSIGN VAR BIN_OP FUNC_CALL VAR BIN_OP NUMBER NUMBER NUMBER FUNC_DEF ASSIGN VAR NUMBER WHILE VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR NUMBER NUMBER VAR VAR VAR BIN_OP VAR VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL 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 EXPR FUNC_CALL VAR VAR VAR
Neil has a [perfect binary tree] with N nodes, and an integer M. He can assign values to nodes. He calls the tree good if the following conditions are met: Nodes' values are positive integers no more than M. Nodes at even levels have values strictly more than their parents' values. Nodes at odd levels have values strictly less than their parents's values. How many ways can Neil assign values to all nodes to get a good perfect binary tree? Since the answer can be large, please output it under modulo 10^{9} + 7. Note: The root of the tree is at layer 1. Two assignments are different if there is at least one node with different values in both assignments. You may need to use 64-bit data types in your programming language to take input. ------ Input Format ------ - The first line of each input contains T - the number of test cases. The test cases then follow. - The only line of each test case contains two space-separated integers N and M - the number of nodes on the tree and the maximum value of any node. ------ Output Format ------ For each test case, output on a single line the number of different assignment modulo 10^{9} + 7. ------ Constraints ------ $1 ≀ T ≀ 100$ $1 ≀ M ≀ 1000$ $1 ≀ N < 2^{59}$ - It is guaranteed you can make a perfect binary tree with exactly $N$ nodes. ----- Sample Input 1 ------ 1 3 3 ----- Sample Output 1 ------ 5 ----- explanation 1 ------ - Test case $1$: Here are all the possible assignments. ![1-2-2] ![1-2-3] ![1-3-2] ![1-3-3] ![2-3-3]
from sys import stdin input = stdin.readline mod = 10**9 + 7 def add(a, b): return (a % mod + b % mod) % mod def mul(a, b): return a % mod * (b % mod) % mod def sub(a, b): return (a - b + mod) % mod def answer(): for i in range(60): if n >> i & 1: x = i dp = [(0) for j in range(m + 1)] for i in range(1, m + 1): dp[i] = 1 for i in range(x - 1, -1, -1): prefix = [(0) for j in range(m + 1)] for j in range(1, m + 1): prefix[j] = add(prefix[j - 1], dp[j]) for j in range(1, m + 1): if i & 1 == 0: this = sub(prefix[m], prefix[j]) dp[j] = mul(this, this) else: this = prefix[j - 1] dp[j] = mul(this, this) ans = 0 for j in range(1, m + 1): ans = add(ans, dp[j]) return ans for T in range(int(input())): n, m = map(int, input().split()) print(answer())
ASSIGN VAR VAR ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER FUNC_DEF RETURN BIN_OP BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR VAR FUNC_DEF RETURN BIN_OP BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR VAR FUNC_DEF RETURN BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR FUNC_DEF FOR VAR FUNC_CALL VAR NUMBER IF BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR 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 EXPR FUNC_CALL VAR FUNC_CALL VAR
It's late at night, and the college students are required to return to their college hostels from the hostel garden. The hostel garden is a rectangular field of dimensions N \times M, with two hostels β€” one across the left edge and one across the top edge of the field. Students are currently in the cells of the field. You are given two matrices of size N \times M, A and B. A_{i, j} denotes the number of students currently in the i^{th} row and the j^{th} column of the field who want to reach the hostel along the left edge of the field, and similarly B_{i, j} denotes the number of students currently in the i^{th} row and the j^{th} column of the field who want to reach the hostel along the top edge of the field. Students will always move toward their respective hostels as a straight path from their positions (so the students represented by A will always move straight left, and those represented by B will always move straight up), and are allowed to enter the hostel from any cell adjacent to the edge of the field (i.e., there are no specific entry points for the hostels). There is only one restriction: there should not exist two students such that one of them reaches the left-edge hostel, the other one reaches the top-edge hostel, and the paths followed by these two students intersect. For example, it is not allowed for a student in cell (1, 5) to go left and a student in cell (28, 3) to go up, because their paths would intersect at cell (1, 3). Under this restriction, what is the maximum number of students who can reach their hostels? ------ Input Format ------ - The first line of input will contain a single integer T, the number of test cases. Then the testcases follow. - Each test case consists of multiple lines of input. - The first line of each test case contains two space-separated integers N, M. - The next N lines describe the matrix A. The i-th of these N lines contains M space-separated integers A_{i, 1}, A_{i, 2}, \ldots, A_{i, M} - The next N lines describe the matrix B. The i-th of these N lines contains M space-separated integers B_{i, 1}, B_{i, 2}, \ldots, B_{i, M} ------ Output Format ------ For each test case, output in a single line the maximum number of students who can reach their hostels. ------ Constraints ------ $1 ≀ T ≀ 10^{6}$ $1 ≀ N, M ≀ 10^{6}$ $0 ≀ A_{i, j} < 10^{9}$ $0 ≀ B_{i, j} < 10^{9}$ - The sum of $N\times M$ over all test cases does not exceed $10^{6}$ ----- Sample Input 1 ------ 2 1 3 6 7 0 9 1 0 2 2 89 0 67 99 12 44 23 54 ----- Sample Output 1 ------ 13 299 ----- explanation 1 ------ Test case $1$: It is optimal to move all students of the left-edge hostel, hence the answer is $6+7+0=13$. Test case $2$: It is optimal to move the students of (row, column)=$(1,2)$ to the top-edge hostel, and students from the remaining cells to move to the left-edge hostel, therefore the answer is $44+89+67+99=299$.
t = int(input()) for _ in range(t): n, m = list(map(int, input().split())) A = [[(0) for i in range(m)] for j in range(n)] B = [[(0) for i in range(m)] for j in range(n)] for i in range(n): A[i] = list(map(int, input().split())) for i in range(n): B[i] = list(map(int, input().split())) for i in range(n - 2, -1, -1): for j in range(m): A[i][j] += A[i + 1][j] for i in range(1, n): for j in range(m): B[i][j] += B[i - 1][j] dp = [[(0) for i in range(m + 1)] for j in range(n + 1)] for col in range(m - 1, -1, -1): for row in range(n, -1, -1): if col < m - 1: dp[row][col] += dp[row][col + 1] if row: dp[row][col] += B[row - 1][col] if row < n: dp[row][col] += A[row][col] dp[row][col] = max(dp[row][col], dp[row + 1][col]) print(dp[0][0])
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR BIN_OP VAR NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER VAR FOR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR BIN_OP VAR NUMBER 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 BIN_OP VAR NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR NUMBER NUMBER IF VAR BIN_OP VAR NUMBER VAR VAR VAR VAR VAR BIN_OP VAR NUMBER IF VAR VAR VAR VAR VAR BIN_OP VAR NUMBER VAR IF VAR VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR VAR VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR VAR NUMBER NUMBER
It's late at night, and the college students are required to return to their college hostels from the hostel garden. The hostel garden is a rectangular field of dimensions N \times M, with two hostels β€” one across the left edge and one across the top edge of the field. Students are currently in the cells of the field. You are given two matrices of size N \times M, A and B. A_{i, j} denotes the number of students currently in the i^{th} row and the j^{th} column of the field who want to reach the hostel along the left edge of the field, and similarly B_{i, j} denotes the number of students currently in the i^{th} row and the j^{th} column of the field who want to reach the hostel along the top edge of the field. Students will always move toward their respective hostels as a straight path from their positions (so the students represented by A will always move straight left, and those represented by B will always move straight up), and are allowed to enter the hostel from any cell adjacent to the edge of the field (i.e., there are no specific entry points for the hostels). There is only one restriction: there should not exist two students such that one of them reaches the left-edge hostel, the other one reaches the top-edge hostel, and the paths followed by these two students intersect. For example, it is not allowed for a student in cell (1, 5) to go left and a student in cell (28, 3) to go up, because their paths would intersect at cell (1, 3). Under this restriction, what is the maximum number of students who can reach their hostels? ------ Input Format ------ - The first line of input will contain a single integer T, the number of test cases. Then the testcases follow. - Each test case consists of multiple lines of input. - The first line of each test case contains two space-separated integers N, M. - The next N lines describe the matrix A. The i-th of these N lines contains M space-separated integers A_{i, 1}, A_{i, 2}, \ldots, A_{i, M} - The next N lines describe the matrix B. The i-th of these N lines contains M space-separated integers B_{i, 1}, B_{i, 2}, \ldots, B_{i, M} ------ Output Format ------ For each test case, output in a single line the maximum number of students who can reach their hostels. ------ Constraints ------ $1 ≀ T ≀ 10^{6}$ $1 ≀ N, M ≀ 10^{6}$ $0 ≀ A_{i, j} < 10^{9}$ $0 ≀ B_{i, j} < 10^{9}$ - The sum of $N\times M$ over all test cases does not exceed $10^{6}$ ----- Sample Input 1 ------ 2 1 3 6 7 0 9 1 0 2 2 89 0 67 99 12 44 23 54 ----- Sample Output 1 ------ 13 299 ----- explanation 1 ------ Test case $1$: It is optimal to move all students of the left-edge hostel, hence the answer is $6+7+0=13$. Test case $2$: It is optimal to move the students of (row, column)=$(1,2)$ to the top-edge hostel, and students from the remaining cells to move to the left-edge hostel, therefore the answer is $44+89+67+99=299$.
for _ in range(int(input())): x, y = map(int, input().split()) a, b = [], [] for _ in range(x): a.append(list(map(int, input().split()))) for _ in range(x): b.append(list(map(int, input().split()))) for i in range(x): temp1, temp2 = 0, 0 for j in range(y): a[i][j] = a[i][j] + temp1 b[i][j] = b[i][j] + temp2 temp1, temp2 = a[i][j], b[i][j] ans = [[(0) for _ in range(y + 1)] for _ in range(x)] for i in range(x): for j in range(y + 1): if j == 0: ans[i][j] = b[i][-1] if i != 0: ans[i][j] = ans[i][j] + ans[i - 1][j] elif j == y: if i == 0: ans[i][j] = max(a[i][-1], ans[i][j - 1]) else: ans[i][j] = max(a[i][-1] + ans[i - 1][j], ans[i][j - 1]) else: ans[i][j] = max( ans[i - 1][j] + (b[i][-1] - b[i][j - 1]) + a[i][j - 1], ans[i][j - 1], ) print(ans[-1][-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 VAR LIST LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR VAR VAR VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR VAR VAR VAR BIN_OP VAR NUMBER VAR IF VAR VAR IF VAR NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR NUMBER VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR BIN_OP VAR VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR VAR NUMBER VAR VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER NUMBER
It's late at night, and the college students are required to return to their college hostels from the hostel garden. The hostel garden is a rectangular field of dimensions N \times M, with two hostels β€” one across the left edge and one across the top edge of the field. Students are currently in the cells of the field. You are given two matrices of size N \times M, A and B. A_{i, j} denotes the number of students currently in the i^{th} row and the j^{th} column of the field who want to reach the hostel along the left edge of the field, and similarly B_{i, j} denotes the number of students currently in the i^{th} row and the j^{th} column of the field who want to reach the hostel along the top edge of the field. Students will always move toward their respective hostels as a straight path from their positions (so the students represented by A will always move straight left, and those represented by B will always move straight up), and are allowed to enter the hostel from any cell adjacent to the edge of the field (i.e., there are no specific entry points for the hostels). There is only one restriction: there should not exist two students such that one of them reaches the left-edge hostel, the other one reaches the top-edge hostel, and the paths followed by these two students intersect. For example, it is not allowed for a student in cell (1, 5) to go left and a student in cell (28, 3) to go up, because their paths would intersect at cell (1, 3). Under this restriction, what is the maximum number of students who can reach their hostels? ------ Input Format ------ - The first line of input will contain a single integer T, the number of test cases. Then the testcases follow. - Each test case consists of multiple lines of input. - The first line of each test case contains two space-separated integers N, M. - The next N lines describe the matrix A. The i-th of these N lines contains M space-separated integers A_{i, 1}, A_{i, 2}, \ldots, A_{i, M} - The next N lines describe the matrix B. The i-th of these N lines contains M space-separated integers B_{i, 1}, B_{i, 2}, \ldots, B_{i, M} ------ Output Format ------ For each test case, output in a single line the maximum number of students who can reach their hostels. ------ Constraints ------ $1 ≀ T ≀ 10^{6}$ $1 ≀ N, M ≀ 10^{6}$ $0 ≀ A_{i, j} < 10^{9}$ $0 ≀ B_{i, j} < 10^{9}$ - The sum of $N\times M$ over all test cases does not exceed $10^{6}$ ----- Sample Input 1 ------ 2 1 3 6 7 0 9 1 0 2 2 89 0 67 99 12 44 23 54 ----- Sample Output 1 ------ 13 299 ----- explanation 1 ------ Test case $1$: It is optimal to move all students of the left-edge hostel, hence the answer is $6+7+0=13$. Test case $2$: It is optimal to move the students of (row, column)=$(1,2)$ to the top-edge hostel, and students from the remaining cells to move to the left-edge hostel, therefore the answer is $44+89+67+99=299$.
def integer_list(): return list(map(int, input().split())) def string_list(): return list(map(str, input().split())) def hetro_list(): return list(input().split()) def pprint(matrix): for i in range(len(matrix)): print(*matrix[i]) t = int(input()) for _ in range(t): n, m = integer_list() A = [integer_list() for i in range(n)] B = [integer_list() for i in range(n)] students = [[[0, 0] for _ in range(m)] for i in range(n)] students[0][0][0] = B[0][0] students[0][0][1] = A[0][0] cumm_top = [] cumm_left = [] for i in range(n): temp = [] s = 0 for j in range(m): s += A[i][j] temp.append(s) cumm_left.append(temp) for j in range(m): temp = [] s = 0 for i in range(n): s += B[i][j] temp.append(s) cumm_top.append(temp) cumm_top = [list(i) for i in zip(*cumm_top)] prev_top = 0 for i in range(1, m): students[0][i][0] = max(students[0][i - 1]) + B[0][i] students[0][i][1] = cumm_left[0][i] prev_left = 0 for i in range(1, n): students[i][0][0] = cumm_top[i][0] students[i][0][1] = max(students[i - 1][0]) + A[i][0] for i in range(1, n): for j in range(1, m): students[i][j][0] = cumm_top[i][j] + max(students[i][j - 1]) students[i][j][1] = cumm_left[i][j] + max(students[i - 1][j]) print(max(students[n - 1][m - 1]))
FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR LIST NUMBER NUMBER VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER NUMBER NUMBER VAR NUMBER NUMBER ASSIGN VAR NUMBER NUMBER NUMBER VAR NUMBER NUMBER ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR NUMBER VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER VAR ASSIGN VAR NUMBER VAR NUMBER VAR NUMBER VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR NUMBER NUMBER VAR VAR NUMBER ASSIGN VAR VAR NUMBER NUMBER BIN_OP FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR VAR NUMBER BIN_OP VAR VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR NUMBER BIN_OP VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER
It's late at night, and the college students are required to return to their college hostels from the hostel garden. The hostel garden is a rectangular field of dimensions N \times M, with two hostels β€” one across the left edge and one across the top edge of the field. Students are currently in the cells of the field. You are given two matrices of size N \times M, A and B. A_{i, j} denotes the number of students currently in the i^{th} row and the j^{th} column of the field who want to reach the hostel along the left edge of the field, and similarly B_{i, j} denotes the number of students currently in the i^{th} row and the j^{th} column of the field who want to reach the hostel along the top edge of the field. Students will always move toward their respective hostels as a straight path from their positions (so the students represented by A will always move straight left, and those represented by B will always move straight up), and are allowed to enter the hostel from any cell adjacent to the edge of the field (i.e., there are no specific entry points for the hostels). There is only one restriction: there should not exist two students such that one of them reaches the left-edge hostel, the other one reaches the top-edge hostel, and the paths followed by these two students intersect. For example, it is not allowed for a student in cell (1, 5) to go left and a student in cell (28, 3) to go up, because their paths would intersect at cell (1, 3). Under this restriction, what is the maximum number of students who can reach their hostels? ------ Input Format ------ - The first line of input will contain a single integer T, the number of test cases. Then the testcases follow. - Each test case consists of multiple lines of input. - The first line of each test case contains two space-separated integers N, M. - The next N lines describe the matrix A. The i-th of these N lines contains M space-separated integers A_{i, 1}, A_{i, 2}, \ldots, A_{i, M} - The next N lines describe the matrix B. The i-th of these N lines contains M space-separated integers B_{i, 1}, B_{i, 2}, \ldots, B_{i, M} ------ Output Format ------ For each test case, output in a single line the maximum number of students who can reach their hostels. ------ Constraints ------ $1 ≀ T ≀ 10^{6}$ $1 ≀ N, M ≀ 10^{6}$ $0 ≀ A_{i, j} < 10^{9}$ $0 ≀ B_{i, j} < 10^{9}$ - The sum of $N\times M$ over all test cases does not exceed $10^{6}$ ----- Sample Input 1 ------ 2 1 3 6 7 0 9 1 0 2 2 89 0 67 99 12 44 23 54 ----- Sample Output 1 ------ 13 299 ----- explanation 1 ------ Test case $1$: It is optimal to move all students of the left-edge hostel, hence the answer is $6+7+0=13$. Test case $2$: It is optimal to move the students of (row, column)=$(1,2)$ to the top-edge hostel, and students from the remaining cells to move to the left-edge hostel, therefore the answer is $44+89+67+99=299$.
import sys sys.setrecursionlimit(10**7) def f(i, j): if i >= n: return 0 if j > m: return 0 if dp[i][j] != -1: return dp[i][j] case1 = pref[i][j] + suff[i][j] + f(i + 1, j) case2 = f(i, j + 1) dp[i][j] = max(case1, case2) return dp[i][j] for _ in range(int(input())): n, m = map(int, input().split()) a = [] for _ in range(n): a.append(list(map(int, input().split()))) b = [] for _ in range(n): b.append(list(map(int, input().split()))) pref = [[(0) for i in range(m)] for j in range(n)] suff = [[(0) for i in range(m)] for j in range(n)] for i in range(n): pref[i][0] = a[i][0] for j in range(1, m): pref[i][j] = a[i][j] + pref[i][j - 1] pref[i] = [0] + pref[i] for i in range(n): suff[i][m - 1] = b[i][m - 1] for j in range(m - 2, -1, -1): suff[i][j] = b[i][j] + suff[i][j + 1] suff[i].append(0) dp = [[(-1) for _ in range(m + 1)] for _ in range(n)] print(f(0, 0))
IMPORT EXPR FUNC_CALL VAR BIN_OP NUMBER NUMBER FUNC_DEF IF VAR VAR RETURN NUMBER IF VAR VAR RETURN NUMBER IF VAR VAR VAR NUMBER RETURN VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR RETURN VAR 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 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 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 NUMBER VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR VAR BIN_OP VAR VAR VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP LIST NUMBER VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR VAR BIN_OP VAR VAR VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR NUMBER NUMBER
Given a triplet of integers (X , Y , Z), such that X ≀ Y and Y β‰₯ Z, we define f(X , Y , Z) to be (X + Y) * (Y + Z). If either X > Y or Y < Z, or both, then f(X , Y , Z) is defined to be 0. You are provided three arrays A , B and C of any length (their lengths may or may not be equal). Your task is to find the sum of f(X , Y , Z) over all triplets (X, Y , Z) where X, Y and Z belong to A, B and C respectively. Output your sum for each test case modulo 1000000007. -----Input----- - The first line contains a single integer, T, which is the number of test cases. The description of each testcase follows: - The first line of each testcase contains 3 integers: p, q and r. These denote the lengths of A,B and C respectively. - The second line contains p integers, which are the elements of A - The third line contains q integers, which are the elements of B - The fourth line contains r integers, which are the elements of C -----Output----- Output the required sum modulo 1000000007 for each test case in a new line. -----Constraints----- - 1 ≀ T ≀ 10 - 1 ≀ p, q, r ≀ 100000 - 1 ≀ every array element ≀ 1000000000 -----Subtasks ----- - Subtask #1 (30 points): 1 ≀ p,q,r ≀ 100 - Subtask #2 (70 points): 1 ≀ p,q,r ≀ 100000 -----Example : ----- Input: 1 3 1 3 1 2 3 5 4 5 6 Output: 399 -----Explanation: ----- As there is only one choice for Y which equals to 5, to get a non-zero function value,we can choose any element for X from the set { 1 , 2 , 3 } and for Z from the set { 4 , 5 } So triplets which give non-zero function values are: { 1 , 5 , 4 } : ( 1 + 5 ) * ( 5 + 4 ) = 54 { 1 , 5 , 5 } : ( 1 + 5 ) * ( 5 + 5 ) = 60 { 2 , 5 , 4 } : ( 2 + 5 ) * ( 5 + 4 ) = 63 { 2 , 5 , 5 } : ( 2 + 5 ) * ( 5 + 5 ) = 70 { 3 , 5 , 4 } : ( 3 + 5 ) * ( 5 + 4 ) = 72 { 3 , 5 , 5 } : ( 3 + 5 ) * ( 5 + 5 ) = 80 Final answer : 54 + 60 + 63 + 70 + 72 + 80 = 399
t = int(input()) mod = 10**9 + 7 for _ in range(t): sum = 0 p, q, r = map(int, input().split()) a = sorted(int(x) for x in input().split()) b = sorted(int(x) for x in input().split()) c = sorted(int(x) for x in input().split()) t1 = t2 = sum1 = sum2 = 0 for i in b: while t1 < p and a[t1] <= i: sum1 += a[t1] t1 += 1 while t2 < r and c[t2] <= i: sum2 += c[t2] t2 += 1 sum += (i * t1 + sum1) * (i * t2 + sum2) print(sum % mod)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR VAR VAR NUMBER FOR VAR VAR WHILE VAR VAR VAR VAR VAR VAR VAR VAR VAR NUMBER WHILE VAR VAR VAR VAR VAR VAR VAR VAR VAR NUMBER VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR
Given a triplet of integers (X , Y , Z), such that X ≀ Y and Y β‰₯ Z, we define f(X , Y , Z) to be (X + Y) * (Y + Z). If either X > Y or Y < Z, or both, then f(X , Y , Z) is defined to be 0. You are provided three arrays A , B and C of any length (their lengths may or may not be equal). Your task is to find the sum of f(X , Y , Z) over all triplets (X, Y , Z) where X, Y and Z belong to A, B and C respectively. Output your sum for each test case modulo 1000000007. -----Input----- - The first line contains a single integer, T, which is the number of test cases. The description of each testcase follows: - The first line of each testcase contains 3 integers: p, q and r. These denote the lengths of A,B and C respectively. - The second line contains p integers, which are the elements of A - The third line contains q integers, which are the elements of B - The fourth line contains r integers, which are the elements of C -----Output----- Output the required sum modulo 1000000007 for each test case in a new line. -----Constraints----- - 1 ≀ T ≀ 10 - 1 ≀ p, q, r ≀ 100000 - 1 ≀ every array element ≀ 1000000000 -----Subtasks ----- - Subtask #1 (30 points): 1 ≀ p,q,r ≀ 100 - Subtask #2 (70 points): 1 ≀ p,q,r ≀ 100000 -----Example : ----- Input: 1 3 1 3 1 2 3 5 4 5 6 Output: 399 -----Explanation: ----- As there is only one choice for Y which equals to 5, to get a non-zero function value,we can choose any element for X from the set { 1 , 2 , 3 } and for Z from the set { 4 , 5 } So triplets which give non-zero function values are: { 1 , 5 , 4 } : ( 1 + 5 ) * ( 5 + 4 ) = 54 { 1 , 5 , 5 } : ( 1 + 5 ) * ( 5 + 5 ) = 60 { 2 , 5 , 4 } : ( 2 + 5 ) * ( 5 + 4 ) = 63 { 2 , 5 , 5 } : ( 2 + 5 ) * ( 5 + 5 ) = 70 { 3 , 5 , 4 } : ( 3 + 5 ) * ( 5 + 4 ) = 72 { 3 , 5 , 5 } : ( 3 + 5 ) * ( 5 + 5 ) = 80 Final answer : 54 + 60 + 63 + 70 + 72 + 80 = 399
def solve(p, q, r, a, b, c): mod = 10**9 + 7 for i in range(len(b)): a.sort() b.sort() c.sort() m = 0 n = 0 sum1 = 0 sum2 = 0 count = 0 for i in b: while m < p and a[m] <= i: sum1 += a[m] m += 1 while n < r and c[n] <= i: sum2 += c[n] n += 1 count += (sum1 + m * i) * (sum2 + n * i) return count % mod test = int(input()) for _ in range(test): p, q, r = map(float, input().rstrip().split()) a = list(map(int, input().rstrip().split())) b = list(map(int, input().rstrip().split())) c = list(map(int, input().rstrip().split())) print(solve(p, q, r, a, b, c))
FUNC_DEF ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR WHILE VAR VAR VAR VAR VAR VAR VAR VAR VAR NUMBER WHILE VAR VAR VAR VAR VAR VAR VAR VAR VAR NUMBER VAR BIN_OP BIN_OP VAR BIN_OP VAR VAR BIN_OP VAR BIN_OP VAR VAR RETURN BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR
Given a triplet of integers (X , Y , Z), such that X ≀ Y and Y β‰₯ Z, we define f(X , Y , Z) to be (X + Y) * (Y + Z). If either X > Y or Y < Z, or both, then f(X , Y , Z) is defined to be 0. You are provided three arrays A , B and C of any length (their lengths may or may not be equal). Your task is to find the sum of f(X , Y , Z) over all triplets (X, Y , Z) where X, Y and Z belong to A, B and C respectively. Output your sum for each test case modulo 1000000007. -----Input----- - The first line contains a single integer, T, which is the number of test cases. The description of each testcase follows: - The first line of each testcase contains 3 integers: p, q and r. These denote the lengths of A,B and C respectively. - The second line contains p integers, which are the elements of A - The third line contains q integers, which are the elements of B - The fourth line contains r integers, which are the elements of C -----Output----- Output the required sum modulo 1000000007 for each test case in a new line. -----Constraints----- - 1 ≀ T ≀ 10 - 1 ≀ p, q, r ≀ 100000 - 1 ≀ every array element ≀ 1000000000 -----Subtasks ----- - Subtask #1 (30 points): 1 ≀ p,q,r ≀ 100 - Subtask #2 (70 points): 1 ≀ p,q,r ≀ 100000 -----Example : ----- Input: 1 3 1 3 1 2 3 5 4 5 6 Output: 399 -----Explanation: ----- As there is only one choice for Y which equals to 5, to get a non-zero function value,we can choose any element for X from the set { 1 , 2 , 3 } and for Z from the set { 4 , 5 } So triplets which give non-zero function values are: { 1 , 5 , 4 } : ( 1 + 5 ) * ( 5 + 4 ) = 54 { 1 , 5 , 5 } : ( 1 + 5 ) * ( 5 + 5 ) = 60 { 2 , 5 , 4 } : ( 2 + 5 ) * ( 5 + 4 ) = 63 { 2 , 5 , 5 } : ( 2 + 5 ) * ( 5 + 5 ) = 70 { 3 , 5 , 4 } : ( 3 + 5 ) * ( 5 + 4 ) = 72 { 3 , 5 , 5 } : ( 3 + 5 ) * ( 5 + 5 ) = 80 Final answer : 54 + 60 + 63 + 70 + 72 + 80 = 399
ls1 = [] ls2 = [] ls3 = [] for x in range(int(input())): p, q, r = map(int, input().split()) ls1 = list(map(int, input().split())) ls2 = list(map(int, input().split())) ls3 = list(map(int, input().split())) ls1.sort() ls2.sort() ls3.sort() ans = 0 i = 0 j = 0 sum1 = 0 sum2 = 0 for a in range(q): if i < p: while ls2[a] >= ls1[i]: sum1 += ls1[i] i += 1 if i >= p: break if j < r: while ls2[a] >= ls3[j]: sum2 += ls3[j] j += 1 if j >= r: break if i != 0 and j != 0: ans += (ls2[a] * j + sum2) * (ls2[a] * i + sum1) print(ans % 1000000007)
ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR WHILE VAR VAR VAR VAR VAR VAR VAR VAR NUMBER IF VAR VAR IF VAR VAR WHILE VAR VAR VAR VAR VAR VAR VAR VAR NUMBER IF VAR VAR IF VAR NUMBER VAR NUMBER VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER
Given a triplet of integers (X , Y , Z), such that X ≀ Y and Y β‰₯ Z, we define f(X , Y , Z) to be (X + Y) * (Y + Z). If either X > Y or Y < Z, or both, then f(X , Y , Z) is defined to be 0. You are provided three arrays A , B and C of any length (their lengths may or may not be equal). Your task is to find the sum of f(X , Y , Z) over all triplets (X, Y , Z) where X, Y and Z belong to A, B and C respectively. Output your sum for each test case modulo 1000000007. -----Input----- - The first line contains a single integer, T, which is the number of test cases. The description of each testcase follows: - The first line of each testcase contains 3 integers: p, q and r. These denote the lengths of A,B and C respectively. - The second line contains p integers, which are the elements of A - The third line contains q integers, which are the elements of B - The fourth line contains r integers, which are the elements of C -----Output----- Output the required sum modulo 1000000007 for each test case in a new line. -----Constraints----- - 1 ≀ T ≀ 10 - 1 ≀ p, q, r ≀ 100000 - 1 ≀ every array element ≀ 1000000000 -----Subtasks ----- - Subtask #1 (30 points): 1 ≀ p,q,r ≀ 100 - Subtask #2 (70 points): 1 ≀ p,q,r ≀ 100000 -----Example : ----- Input: 1 3 1 3 1 2 3 5 4 5 6 Output: 399 -----Explanation: ----- As there is only one choice for Y which equals to 5, to get a non-zero function value,we can choose any element for X from the set { 1 , 2 , 3 } and for Z from the set { 4 , 5 } So triplets which give non-zero function values are: { 1 , 5 , 4 } : ( 1 + 5 ) * ( 5 + 4 ) = 54 { 1 , 5 , 5 } : ( 1 + 5 ) * ( 5 + 5 ) = 60 { 2 , 5 , 4 } : ( 2 + 5 ) * ( 5 + 4 ) = 63 { 2 , 5 , 5 } : ( 2 + 5 ) * ( 5 + 5 ) = 70 { 3 , 5 , 4 } : ( 3 + 5 ) * ( 5 + 4 ) = 72 { 3 , 5 , 5 } : ( 3 + 5 ) * ( 5 + 5 ) = 80 Final answer : 54 + 60 + 63 + 70 + 72 + 80 = 399
for _ in range(int(input())): p, q, r = map(int, input().split()) ar1 = sorted(list(map(int, input().split()))) ar2 = sorted(list(map(int, input().split()))) ar3 = sorted(list(map(int, input().split()))) a = b = c = d = 0 n = 0 mod = 10**9 + 7 for i in ar2: while a < p and ar1[a] <= i: c += ar1[a] a += 1 while b < r and ar3[b] <= i: d += ar3[b] b += 1 n += (i * a + c) * (i * b + d) n %= mod print(n)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER FOR VAR VAR WHILE VAR VAR VAR VAR VAR VAR VAR VAR VAR NUMBER WHILE VAR VAR VAR VAR VAR VAR VAR VAR VAR NUMBER VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR
Given a triplet of integers (X , Y , Z), such that X ≀ Y and Y β‰₯ Z, we define f(X , Y , Z) to be (X + Y) * (Y + Z). If either X > Y or Y < Z, or both, then f(X , Y , Z) is defined to be 0. You are provided three arrays A , B and C of any length (their lengths may or may not be equal). Your task is to find the sum of f(X , Y , Z) over all triplets (X, Y , Z) where X, Y and Z belong to A, B and C respectively. Output your sum for each test case modulo 1000000007. -----Input----- - The first line contains a single integer, T, which is the number of test cases. The description of each testcase follows: - The first line of each testcase contains 3 integers: p, q and r. These denote the lengths of A,B and C respectively. - The second line contains p integers, which are the elements of A - The third line contains q integers, which are the elements of B - The fourth line contains r integers, which are the elements of C -----Output----- Output the required sum modulo 1000000007 for each test case in a new line. -----Constraints----- - 1 ≀ T ≀ 10 - 1 ≀ p, q, r ≀ 100000 - 1 ≀ every array element ≀ 1000000000 -----Subtasks ----- - Subtask #1 (30 points): 1 ≀ p,q,r ≀ 100 - Subtask #2 (70 points): 1 ≀ p,q,r ≀ 100000 -----Example : ----- Input: 1 3 1 3 1 2 3 5 4 5 6 Output: 399 -----Explanation: ----- As there is only one choice for Y which equals to 5, to get a non-zero function value,we can choose any element for X from the set { 1 , 2 , 3 } and for Z from the set { 4 , 5 } So triplets which give non-zero function values are: { 1 , 5 , 4 } : ( 1 + 5 ) * ( 5 + 4 ) = 54 { 1 , 5 , 5 } : ( 1 + 5 ) * ( 5 + 5 ) = 60 { 2 , 5 , 4 } : ( 2 + 5 ) * ( 5 + 4 ) = 63 { 2 , 5 , 5 } : ( 2 + 5 ) * ( 5 + 5 ) = 70 { 3 , 5 , 4 } : ( 3 + 5 ) * ( 5 + 4 ) = 72 { 3 , 5 , 5 } : ( 3 + 5 ) * ( 5 + 5 ) = 80 Final answer : 54 + 60 + 63 + 70 + 72 + 80 = 399
for _ in range(int(input())): lx, ly, lz = [int(x) for x in input().split()] x = sorted(int(x) for x in input().split()) y = sorted(int(x) for x in input().split()) z = sorted(int(x) for x in input().split()) nx = nz = sx = sz = r = 0 for yy in y: while nx < lx and x[nx] <= yy: sx += x[nx] nx += 1 while nz < lz and z[nz] <= yy: sz += z[nz] nz += 1 r = (r + (nx * yy + sx) * (nz * yy + sz)) % 1000000007 print(r)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR VAR VAR VAR NUMBER FOR VAR VAR WHILE VAR VAR VAR VAR VAR VAR VAR VAR VAR NUMBER WHILE VAR VAR VAR VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
Given a triplet of integers (X , Y , Z), such that X ≀ Y and Y β‰₯ Z, we define f(X , Y , Z) to be (X + Y) * (Y + Z). If either X > Y or Y < Z, or both, then f(X , Y , Z) is defined to be 0. You are provided three arrays A , B and C of any length (their lengths may or may not be equal). Your task is to find the sum of f(X , Y , Z) over all triplets (X, Y , Z) where X, Y and Z belong to A, B and C respectively. Output your sum for each test case modulo 1000000007. -----Input----- - The first line contains a single integer, T, which is the number of test cases. The description of each testcase follows: - The first line of each testcase contains 3 integers: p, q and r. These denote the lengths of A,B and C respectively. - The second line contains p integers, which are the elements of A - The third line contains q integers, which are the elements of B - The fourth line contains r integers, which are the elements of C -----Output----- Output the required sum modulo 1000000007 for each test case in a new line. -----Constraints----- - 1 ≀ T ≀ 10 - 1 ≀ p, q, r ≀ 100000 - 1 ≀ every array element ≀ 1000000000 -----Subtasks ----- - Subtask #1 (30 points): 1 ≀ p,q,r ≀ 100 - Subtask #2 (70 points): 1 ≀ p,q,r ≀ 100000 -----Example : ----- Input: 1 3 1 3 1 2 3 5 4 5 6 Output: 399 -----Explanation: ----- As there is only one choice for Y which equals to 5, to get a non-zero function value,we can choose any element for X from the set { 1 , 2 , 3 } and for Z from the set { 4 , 5 } So triplets which give non-zero function values are: { 1 , 5 , 4 } : ( 1 + 5 ) * ( 5 + 4 ) = 54 { 1 , 5 , 5 } : ( 1 + 5 ) * ( 5 + 5 ) = 60 { 2 , 5 , 4 } : ( 2 + 5 ) * ( 5 + 4 ) = 63 { 2 , 5 , 5 } : ( 2 + 5 ) * ( 5 + 5 ) = 70 { 3 , 5 , 4 } : ( 3 + 5 ) * ( 5 + 4 ) = 72 { 3 , 5 , 5 } : ( 3 + 5 ) * ( 5 + 5 ) = 80 Final answer : 54 + 60 + 63 + 70 + 72 + 80 = 399
m = 1000000007 def tripet(l1, l2, l3, n1, n2, n3): a = 0 b = 0 s = 0 r = 0 res = 0 for i in l2: while a < n1 and l1[a] <= i: s += l1[a] a += 1 while b < n3 and l3[b] <= i: r += l3[b] b += 1 res += (i * a + s) * (i * b + r) res = res % m print(res) for _ in range(int(input())): n1, n2, n3 = map(int, input().split()) l1 = sorted(list(map(int, input().strip().split()))) l2 = sorted(list(map(int, input().strip().split()))) l3 = sorted(list(map(int, input().strip().split()))) tripet(l1, l2, l3, n1, n2, n3)
ASSIGN VAR NUMBER FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR WHILE VAR VAR VAR VAR VAR VAR VAR VAR VAR NUMBER WHILE VAR VAR VAR VAR VAR VAR VAR VAR VAR NUMBER VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR
Given a triplet of integers (X , Y , Z), such that X ≀ Y and Y β‰₯ Z, we define f(X , Y , Z) to be (X + Y) * (Y + Z). If either X > Y or Y < Z, or both, then f(X , Y , Z) is defined to be 0. You are provided three arrays A , B and C of any length (their lengths may or may not be equal). Your task is to find the sum of f(X , Y , Z) over all triplets (X, Y , Z) where X, Y and Z belong to A, B and C respectively. Output your sum for each test case modulo 1000000007. -----Input----- - The first line contains a single integer, T, which is the number of test cases. The description of each testcase follows: - The first line of each testcase contains 3 integers: p, q and r. These denote the lengths of A,B and C respectively. - The second line contains p integers, which are the elements of A - The third line contains q integers, which are the elements of B - The fourth line contains r integers, which are the elements of C -----Output----- Output the required sum modulo 1000000007 for each test case in a new line. -----Constraints----- - 1 ≀ T ≀ 10 - 1 ≀ p, q, r ≀ 100000 - 1 ≀ every array element ≀ 1000000000 -----Subtasks ----- - Subtask #1 (30 points): 1 ≀ p,q,r ≀ 100 - Subtask #2 (70 points): 1 ≀ p,q,r ≀ 100000 -----Example : ----- Input: 1 3 1 3 1 2 3 5 4 5 6 Output: 399 -----Explanation: ----- As there is only one choice for Y which equals to 5, to get a non-zero function value,we can choose any element for X from the set { 1 , 2 , 3 } and for Z from the set { 4 , 5 } So triplets which give non-zero function values are: { 1 , 5 , 4 } : ( 1 + 5 ) * ( 5 + 4 ) = 54 { 1 , 5 , 5 } : ( 1 + 5 ) * ( 5 + 5 ) = 60 { 2 , 5 , 4 } : ( 2 + 5 ) * ( 5 + 4 ) = 63 { 2 , 5 , 5 } : ( 2 + 5 ) * ( 5 + 5 ) = 70 { 3 , 5 , 4 } : ( 3 + 5 ) * ( 5 + 4 ) = 72 { 3 , 5 , 5 } : ( 3 + 5 ) * ( 5 + 5 ) = 80 Final answer : 54 + 60 + 63 + 70 + 72 + 80 = 399
t = int(input()) x = [] for i in range(t): s1 = input().split() p = int(s1[0]) q = int(s1[1]) r = int(s1[2]) suma = 0 sumc = 0 a = input().split() for j in range(p): a[j] = int(a[j]) suma += a[j] b = input().split() for j in range(q): b[j] = int(b[j]) c = input().split() for j in range(r): c[j] = int(c[j]) sumc += c[j] a.sort() b.sort() c.sort() sum = 0 ca = p - 1 cc = r - 1 for j in range(q - 1, -1, -1): while a[ca] > b[j] and ca >= 0: suma -= a[ca] ca -= 1 while cc >= 0 and c[cc] > b[j]: sumc -= c[cc] cc -= 1 if suma == 0 or sumc == 0: break sum += ( suma * sumc + b[j] ** 2 * (ca + 1) * (cc + 1) + (suma * (cc + 1) + sumc * (ca + 1)) * b[j] ) x.append(sum % 1000000007) for i in range(t): print(x[i])
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER WHILE VAR VAR VAR VAR VAR NUMBER VAR VAR VAR VAR NUMBER WHILE VAR NUMBER VAR VAR VAR VAR VAR VAR VAR VAR NUMBER IF VAR NUMBER VAR NUMBER VAR BIN_OP BIN_OP BIN_OP VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP BIN_OP BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR
Given a triplet of integers (X , Y , Z), such that X ≀ Y and Y β‰₯ Z, we define f(X , Y , Z) to be (X + Y) * (Y + Z). If either X > Y or Y < Z, or both, then f(X , Y , Z) is defined to be 0. You are provided three arrays A , B and C of any length (their lengths may or may not be equal). Your task is to find the sum of f(X , Y , Z) over all triplets (X, Y , Z) where X, Y and Z belong to A, B and C respectively. Output your sum for each test case modulo 1000000007. -----Input----- - The first line contains a single integer, T, which is the number of test cases. The description of each testcase follows: - The first line of each testcase contains 3 integers: p, q and r. These denote the lengths of A,B and C respectively. - The second line contains p integers, which are the elements of A - The third line contains q integers, which are the elements of B - The fourth line contains r integers, which are the elements of C -----Output----- Output the required sum modulo 1000000007 for each test case in a new line. -----Constraints----- - 1 ≀ T ≀ 10 - 1 ≀ p, q, r ≀ 100000 - 1 ≀ every array element ≀ 1000000000 -----Subtasks ----- - Subtask #1 (30 points): 1 ≀ p,q,r ≀ 100 - Subtask #2 (70 points): 1 ≀ p,q,r ≀ 100000 -----Example : ----- Input: 1 3 1 3 1 2 3 5 4 5 6 Output: 399 -----Explanation: ----- As there is only one choice for Y which equals to 5, to get a non-zero function value,we can choose any element for X from the set { 1 , 2 , 3 } and for Z from the set { 4 , 5 } So triplets which give non-zero function values are: { 1 , 5 , 4 } : ( 1 + 5 ) * ( 5 + 4 ) = 54 { 1 , 5 , 5 } : ( 1 + 5 ) * ( 5 + 5 ) = 60 { 2 , 5 , 4 } : ( 2 + 5 ) * ( 5 + 4 ) = 63 { 2 , 5 , 5 } : ( 2 + 5 ) * ( 5 + 5 ) = 70 { 3 , 5 , 4 } : ( 3 + 5 ) * ( 5 + 4 ) = 72 { 3 , 5 , 5 } : ( 3 + 5 ) * ( 5 + 5 ) = 80 Final answer : 54 + 60 + 63 + 70 + 72 + 80 = 399
t = int(input()) for i in range(t): p, q, r = list(map(int, input().split())) a = list(sorted(list(map(int, input().split())))) b = list(sorted(list(map(int, input().split())))) c = list(sorted(list(map(int, input().split())))) la = lc = 0 sa = sc = 0 s = 0 for y in b: while la < p and a[la] <= y: sa += a[la] la += 1 while lc < r and c[lc] <= y: sc += c[lc] lc += 1 s += (la * y + sa) * (lc * y + sc) % 1000000007 print(s % 1000000007)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR WHILE VAR VAR VAR VAR VAR VAR VAR VAR VAR NUMBER WHILE VAR VAR VAR VAR VAR VAR VAR VAR VAR NUMBER VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER
Given a triplet of integers (X , Y , Z), such that X ≀ Y and Y β‰₯ Z, we define f(X , Y , Z) to be (X + Y) * (Y + Z). If either X > Y or Y < Z, or both, then f(X , Y , Z) is defined to be 0. You are provided three arrays A , B and C of any length (their lengths may or may not be equal). Your task is to find the sum of f(X , Y , Z) over all triplets (X, Y , Z) where X, Y and Z belong to A, B and C respectively. Output your sum for each test case modulo 1000000007. -----Input----- - The first line contains a single integer, T, which is the number of test cases. The description of each testcase follows: - The first line of each testcase contains 3 integers: p, q and r. These denote the lengths of A,B and C respectively. - The second line contains p integers, which are the elements of A - The third line contains q integers, which are the elements of B - The fourth line contains r integers, which are the elements of C -----Output----- Output the required sum modulo 1000000007 for each test case in a new line. -----Constraints----- - 1 ≀ T ≀ 10 - 1 ≀ p, q, r ≀ 100000 - 1 ≀ every array element ≀ 1000000000 -----Subtasks ----- - Subtask #1 (30 points): 1 ≀ p,q,r ≀ 100 - Subtask #2 (70 points): 1 ≀ p,q,r ≀ 100000 -----Example : ----- Input: 1 3 1 3 1 2 3 5 4 5 6 Output: 399 -----Explanation: ----- As there is only one choice for Y which equals to 5, to get a non-zero function value,we can choose any element for X from the set { 1 , 2 , 3 } and for Z from the set { 4 , 5 } So triplets which give non-zero function values are: { 1 , 5 , 4 } : ( 1 + 5 ) * ( 5 + 4 ) = 54 { 1 , 5 , 5 } : ( 1 + 5 ) * ( 5 + 5 ) = 60 { 2 , 5 , 4 } : ( 2 + 5 ) * ( 5 + 4 ) = 63 { 2 , 5 , 5 } : ( 2 + 5 ) * ( 5 + 5 ) = 70 { 3 , 5 , 4 } : ( 3 + 5 ) * ( 5 + 4 ) = 72 { 3 , 5 , 5 } : ( 3 + 5 ) * ( 5 + 5 ) = 80 Final answer : 54 + 60 + 63 + 70 + 72 + 80 = 399
counter = int(input()) for mrinal in range(counter): x_size, y_size, z_size = map(int, input().split()) x = [int(i) for i in input().split()] y = [int(i) for i in input().split()] z = [int(i) for i in input().split()] x.sort() y.sort() z.sort() x_sum = [] z_sum = [] if y[y_size - 1] < x[0] or y[y_size - 1] < y[0]: print(0) continue count = 0 for jithalel in x: count += jithalel x_sum.append(count) count = 0 for bhide in z: count += bhide z_sum.append(count) y_counter = 0 x_counter = 0 z_counter = 0 triplet_sum = 0 while y_counter < y_size: while x_counter < x_size and x[x_counter] <= y[y_counter]: x_counter += 1 while z_counter < z_size and z[z_counter] <= y[y_counter]: z_counter += 1 if x_counter == 0 or z_counter == 0: y_counter += 1 continue a = (x_sum[x_counter - 1] + x_counter * y[y_counter]) % 1000000007 b = (z_sum[z_counter - 1] + z_counter * y[y_counter]) % 1000000007 triplet_sum += a * b y_counter += 1 print(triplet_sum % 1000000007)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR LIST IF VAR BIN_OP VAR NUMBER VAR NUMBER VAR BIN_OP VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR WHILE VAR VAR VAR VAR VAR VAR VAR NUMBER WHILE VAR VAR VAR VAR VAR VAR VAR NUMBER IF VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR VAR VAR NUMBER VAR BIN_OP VAR VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER
Given a triplet of integers (X , Y , Z), such that X ≀ Y and Y β‰₯ Z, we define f(X , Y , Z) to be (X + Y) * (Y + Z). If either X > Y or Y < Z, or both, then f(X , Y , Z) is defined to be 0. You are provided three arrays A , B and C of any length (their lengths may or may not be equal). Your task is to find the sum of f(X , Y , Z) over all triplets (X, Y , Z) where X, Y and Z belong to A, B and C respectively. Output your sum for each test case modulo 1000000007. -----Input----- - The first line contains a single integer, T, which is the number of test cases. The description of each testcase follows: - The first line of each testcase contains 3 integers: p, q and r. These denote the lengths of A,B and C respectively. - The second line contains p integers, which are the elements of A - The third line contains q integers, which are the elements of B - The fourth line contains r integers, which are the elements of C -----Output----- Output the required sum modulo 1000000007 for each test case in a new line. -----Constraints----- - 1 ≀ T ≀ 10 - 1 ≀ p, q, r ≀ 100000 - 1 ≀ every array element ≀ 1000000000 -----Subtasks ----- - Subtask #1 (30 points): 1 ≀ p,q,r ≀ 100 - Subtask #2 (70 points): 1 ≀ p,q,r ≀ 100000 -----Example : ----- Input: 1 3 1 3 1 2 3 5 4 5 6 Output: 399 -----Explanation: ----- As there is only one choice for Y which equals to 5, to get a non-zero function value,we can choose any element for X from the set { 1 , 2 , 3 } and for Z from the set { 4 , 5 } So triplets which give non-zero function values are: { 1 , 5 , 4 } : ( 1 + 5 ) * ( 5 + 4 ) = 54 { 1 , 5 , 5 } : ( 1 + 5 ) * ( 5 + 5 ) = 60 { 2 , 5 , 4 } : ( 2 + 5 ) * ( 5 + 4 ) = 63 { 2 , 5 , 5 } : ( 2 + 5 ) * ( 5 + 5 ) = 70 { 3 , 5 , 4 } : ( 3 + 5 ) * ( 5 + 4 ) = 72 { 3 , 5 , 5 } : ( 3 + 5 ) * ( 5 + 5 ) = 80 Final answer : 54 + 60 + 63 + 70 + 72 + 80 = 399
z = 1000000007 for _ in range(int(input())): p, q, r = map(int, input().split()) A = list(map(int, input().split())) B = list(map(int, input().split())) C = list(map(int, input().split())) A.sort() B.sort() C.sort() sA = [0] * p sB = [0] * q sC = [0] * r sA[0] = A[0] sB[0] = B[0] sC[0] = C[0] for i in range(1, p): sA[i] = (sA[i - 1] + A[i]) % z for i in range(1, q): sB[i] = (sB[i - 1] + B[i]) % z for i in range(1, r): sC[i] = (sC[i - 1] + C[i]) % z j = 0 k = 0 ans = 0 for i in B: while j < p: if A[j] <= i: j += 1 else: break while k < r: if C[k] <= i: k += 1 else: break if A[j - 1] > i or C[k - 1] > i: continue ans = (ans + (j * i % z + sA[j - 1] % z) * (k * i % z + sC[k - 1] % z) % z) % z print(ans)
ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR WHILE VAR VAR IF VAR VAR VAR VAR NUMBER WHILE VAR VAR IF VAR VAR VAR VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR VAR EXPR FUNC_CALL VAR VAR
Given a triplet of integers (X , Y , Z), such that X ≀ Y and Y β‰₯ Z, we define f(X , Y , Z) to be (X + Y) * (Y + Z). If either X > Y or Y < Z, or both, then f(X , Y , Z) is defined to be 0. You are provided three arrays A , B and C of any length (their lengths may or may not be equal). Your task is to find the sum of f(X , Y , Z) over all triplets (X, Y , Z) where X, Y and Z belong to A, B and C respectively. Output your sum for each test case modulo 1000000007. -----Input----- - The first line contains a single integer, T, which is the number of test cases. The description of each testcase follows: - The first line of each testcase contains 3 integers: p, q and r. These denote the lengths of A,B and C respectively. - The second line contains p integers, which are the elements of A - The third line contains q integers, which are the elements of B - The fourth line contains r integers, which are the elements of C -----Output----- Output the required sum modulo 1000000007 for each test case in a new line. -----Constraints----- - 1 ≀ T ≀ 10 - 1 ≀ p, q, r ≀ 100000 - 1 ≀ every array element ≀ 1000000000 -----Subtasks ----- - Subtask #1 (30 points): 1 ≀ p,q,r ≀ 100 - Subtask #2 (70 points): 1 ≀ p,q,r ≀ 100000 -----Example : ----- Input: 1 3 1 3 1 2 3 5 4 5 6 Output: 399 -----Explanation: ----- As there is only one choice for Y which equals to 5, to get a non-zero function value,we can choose any element for X from the set { 1 , 2 , 3 } and for Z from the set { 4 , 5 } So triplets which give non-zero function values are: { 1 , 5 , 4 } : ( 1 + 5 ) * ( 5 + 4 ) = 54 { 1 , 5 , 5 } : ( 1 + 5 ) * ( 5 + 5 ) = 60 { 2 , 5 , 4 } : ( 2 + 5 ) * ( 5 + 4 ) = 63 { 2 , 5 , 5 } : ( 2 + 5 ) * ( 5 + 5 ) = 70 { 3 , 5 , 4 } : ( 3 + 5 ) * ( 5 + 4 ) = 72 { 3 , 5 , 5 } : ( 3 + 5 ) * ( 5 + 5 ) = 80 Final answer : 54 + 60 + 63 + 70 + 72 + 80 = 399
mod = 1000000007 t = int(input()) for j in range(t): p, q, r = input().split() p = int(p) q = int(q) r = int(r) a = [int(x) for x in input().split()] b = [int(x) for x in input().split()] c = [int(x) for x in input().split()] res = 0 a.sort() b.sort() c.sort() prev_suma = 0 prev_sumc = 0 counta = 0 countc = 0 prev_i = 0 indexa = 0 indexc = 0 for i in b: suma = (prev_suma + counta * (i - prev_i) % mod) % mod sumc = (prev_sumc + countc * (i - prev_i) % mod) % mod while indexa < p: if i >= a[indexa]: suma = (suma + (i + a[indexa]) % mod) % mod counta += 1 indexa += 1 else: break while indexc < r: if i >= c[indexc]: sumc = (sumc + (i + c[indexc]) % mod) % mod countc += 1 indexc += 1 else: break prev_i = i prev_suma = suma prev_sumc = sumc res = (res + suma * sumc % mod) % mod print(res)
ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP BIN_OP VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP BIN_OP VAR BIN_OP VAR VAR VAR VAR WHILE VAR VAR IF VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR VAR NUMBER VAR NUMBER WHILE VAR VAR IF VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP BIN_OP VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR
Given a triplet of integers (X , Y , Z), such that X ≀ Y and Y β‰₯ Z, we define f(X , Y , Z) to be (X + Y) * (Y + Z). If either X > Y or Y < Z, or both, then f(X , Y , Z) is defined to be 0. You are provided three arrays A , B and C of any length (their lengths may or may not be equal). Your task is to find the sum of f(X , Y , Z) over all triplets (X, Y , Z) where X, Y and Z belong to A, B and C respectively. Output your sum for each test case modulo 1000000007. -----Input----- - The first line contains a single integer, T, which is the number of test cases. The description of each testcase follows: - The first line of each testcase contains 3 integers: p, q and r. These denote the lengths of A,B and C respectively. - The second line contains p integers, which are the elements of A - The third line contains q integers, which are the elements of B - The fourth line contains r integers, which are the elements of C -----Output----- Output the required sum modulo 1000000007 for each test case in a new line. -----Constraints----- - 1 ≀ T ≀ 10 - 1 ≀ p, q, r ≀ 100000 - 1 ≀ every array element ≀ 1000000000 -----Subtasks ----- - Subtask #1 (30 points): 1 ≀ p,q,r ≀ 100 - Subtask #2 (70 points): 1 ≀ p,q,r ≀ 100000 -----Example : ----- Input: 1 3 1 3 1 2 3 5 4 5 6 Output: 399 -----Explanation: ----- As there is only one choice for Y which equals to 5, to get a non-zero function value,we can choose any element for X from the set { 1 , 2 , 3 } and for Z from the set { 4 , 5 } So triplets which give non-zero function values are: { 1 , 5 , 4 } : ( 1 + 5 ) * ( 5 + 4 ) = 54 { 1 , 5 , 5 } : ( 1 + 5 ) * ( 5 + 5 ) = 60 { 2 , 5 , 4 } : ( 2 + 5 ) * ( 5 + 4 ) = 63 { 2 , 5 , 5 } : ( 2 + 5 ) * ( 5 + 5 ) = 70 { 3 , 5 , 4 } : ( 3 + 5 ) * ( 5 + 4 ) = 72 { 3 , 5 , 5 } : ( 3 + 5 ) * ( 5 + 5 ) = 80 Final answer : 54 + 60 + 63 + 70 + 72 + 80 = 399
t = int(input()) modulo = 1000000007 for i in range(t): lens = input() lenlist = list(map(int, lens.split())) a1 = input() a = list(map(int, a1.split())) b1 = input() b = list(map(int, b1.split())) c1 = input() c = list(map(int, c1.split())) maxy = min(c) miny = min(a) b = [x for x in b if x >= maxy] b = [x for x in b if x >= miny] a.sort() b.sort() c.sort() alen = lenlist[0] blen = len(b) clen = lenlist[2] check = 0 first = 0 second = 0 upsum = 0 sum1 = 0 sum2 = 0 auxsum1 = 0 mark1 = 0 auxsum2 = 0 mark2 = 0 mach1 = 0 mach2 = 0 for yolo in range(blen): f = "n" for q in range(mark1, alen): if b[yolo] < a[q]: f = "y" mark1 = q break sum1 += a[q] % modulo first += 1 mark1 = q if f == "y": pass else: mark1 += 1 mach1 = (sum1 % modulo + mark1 % modulo * b[yolo] % modulo % modulo) % modulo f = "n" for q in range(mark2, clen): if b[yolo] < c[q]: f = "y" break sum2 += c[q] % modulo second += 1 mark2 = q if f == "y": pass else: mark2 += 1 mach2 = (sum2 % modulo + mark2 % modulo * b[yolo] % modulo % modulo) % modulo upsum += mach1 % modulo * mach2 % modulo % modulo print(upsum % modulo)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR STRING FOR VAR FUNC_CALL VAR VAR VAR IF VAR VAR VAR VAR ASSIGN VAR STRING ASSIGN VAR VAR VAR BIN_OP VAR VAR VAR VAR NUMBER ASSIGN VAR VAR IF VAR STRING VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR STRING FOR VAR FUNC_CALL VAR VAR VAR IF VAR VAR VAR VAR ASSIGN VAR STRING VAR BIN_OP VAR VAR VAR VAR NUMBER ASSIGN VAR VAR IF VAR STRING VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR VAR VAR VAR VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR
Given a triplet of integers (X , Y , Z), such that X ≀ Y and Y β‰₯ Z, we define f(X , Y , Z) to be (X + Y) * (Y + Z). If either X > Y or Y < Z, or both, then f(X , Y , Z) is defined to be 0. You are provided three arrays A , B and C of any length (their lengths may or may not be equal). Your task is to find the sum of f(X , Y , Z) over all triplets (X, Y , Z) where X, Y and Z belong to A, B and C respectively. Output your sum for each test case modulo 1000000007. -----Input----- - The first line contains a single integer, T, which is the number of test cases. The description of each testcase follows: - The first line of each testcase contains 3 integers: p, q and r. These denote the lengths of A,B and C respectively. - The second line contains p integers, which are the elements of A - The third line contains q integers, which are the elements of B - The fourth line contains r integers, which are the elements of C -----Output----- Output the required sum modulo 1000000007 for each test case in a new line. -----Constraints----- - 1 ≀ T ≀ 10 - 1 ≀ p, q, r ≀ 100000 - 1 ≀ every array element ≀ 1000000000 -----Subtasks ----- - Subtask #1 (30 points): 1 ≀ p,q,r ≀ 100 - Subtask #2 (70 points): 1 ≀ p,q,r ≀ 100000 -----Example : ----- Input: 1 3 1 3 1 2 3 5 4 5 6 Output: 399 -----Explanation: ----- As there is only one choice for Y which equals to 5, to get a non-zero function value,we can choose any element for X from the set { 1 , 2 , 3 } and for Z from the set { 4 , 5 } So triplets which give non-zero function values are: { 1 , 5 , 4 } : ( 1 + 5 ) * ( 5 + 4 ) = 54 { 1 , 5 , 5 } : ( 1 + 5 ) * ( 5 + 5 ) = 60 { 2 , 5 , 4 } : ( 2 + 5 ) * ( 5 + 4 ) = 63 { 2 , 5 , 5 } : ( 2 + 5 ) * ( 5 + 5 ) = 70 { 3 , 5 , 4 } : ( 3 + 5 ) * ( 5 + 4 ) = 72 { 3 , 5 , 5 } : ( 3 + 5 ) * ( 5 + 5 ) = 80 Final answer : 54 + 60 + 63 + 70 + 72 + 80 = 399
T = int(input().strip()) for test in range(T): mod = 1000000007 p, q, r = input().strip().split(" ") p, q, r = int(p), int(q), int(r) A = [int(x) for x in input().split()] B = [int(x) for x in input().split()] C = [int(x) for x in input().split()] A.sort() B.sort() C.sort() sumofx = [] countofx = [] sumofz = [] countofz = [] ans, i, summ = 0, 0, 0 for y in B: while i < p and A[i] <= y: summ += A[i] i += 1 countofx.append(i) sumofx.append(summ) i = 0 summ = 0 for y in B: while i < r and C[i] <= y: summ += C[i] i += 1 countofz.append(i) sumofz.append(summ) for i in range(q): ans += ( B[i] * B[i] % mod * countofx[i] % mod * countofz[i] % mod % mod + B[i] * countofz[i] % mod * sumofx[i] % mod % mod + B[i] * sumofz[i] % mod * countofx[i] % mod % mod + sumofz[i] * sumofx[i] % mod ) % mod print(ans % mod)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR VAR VAR NUMBER NUMBER NUMBER FOR VAR VAR WHILE VAR VAR VAR VAR VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR WHILE VAR VAR VAR VAR VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR VAR BIN_OP BIN_OP BIN_OP BIN_OP BIN_OP BIN_OP BIN_OP BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR BIN_OP BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR VAR VAR VAR VAR VAR BIN_OP BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR VAR VAR VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR
Given a triplet of integers (X , Y , Z), such that X ≀ Y and Y β‰₯ Z, we define f(X , Y , Z) to be (X + Y) * (Y + Z). If either X > Y or Y < Z, or both, then f(X , Y , Z) is defined to be 0. You are provided three arrays A , B and C of any length (their lengths may or may not be equal). Your task is to find the sum of f(X , Y , Z) over all triplets (X, Y , Z) where X, Y and Z belong to A, B and C respectively. Output your sum for each test case modulo 1000000007. -----Input----- - The first line contains a single integer, T, which is the number of test cases. The description of each testcase follows: - The first line of each testcase contains 3 integers: p, q and r. These denote the lengths of A,B and C respectively. - The second line contains p integers, which are the elements of A - The third line contains q integers, which are the elements of B - The fourth line contains r integers, which are the elements of C -----Output----- Output the required sum modulo 1000000007 for each test case in a new line. -----Constraints----- - 1 ≀ T ≀ 10 - 1 ≀ p, q, r ≀ 100000 - 1 ≀ every array element ≀ 1000000000 -----Subtasks ----- - Subtask #1 (30 points): 1 ≀ p,q,r ≀ 100 - Subtask #2 (70 points): 1 ≀ p,q,r ≀ 100000 -----Example : ----- Input: 1 3 1 3 1 2 3 5 4 5 6 Output: 399 -----Explanation: ----- As there is only one choice for Y which equals to 5, to get a non-zero function value,we can choose any element for X from the set { 1 , 2 , 3 } and for Z from the set { 4 , 5 } So triplets which give non-zero function values are: { 1 , 5 , 4 } : ( 1 + 5 ) * ( 5 + 4 ) = 54 { 1 , 5 , 5 } : ( 1 + 5 ) * ( 5 + 5 ) = 60 { 2 , 5 , 4 } : ( 2 + 5 ) * ( 5 + 4 ) = 63 { 2 , 5 , 5 } : ( 2 + 5 ) * ( 5 + 5 ) = 70 { 3 , 5 , 4 } : ( 3 + 5 ) * ( 5 + 4 ) = 72 { 3 , 5 , 5 } : ( 3 + 5 ) * ( 5 + 5 ) = 80 Final answer : 54 + 60 + 63 + 70 + 72 + 80 = 399
mdl = 1000000007 for _ in range(int(input())): z = input().split() p, q, r = map(int, z[:3]) z = input().split() a = list(map(int, z[:p])) z = input().split() b = list(map(int, z[:q])) z = input().split() c = list(map(int, z[:r])) a.sort() b.sort() c.sort() cta = ctc = 0 suma = sumc = 0 ans = 0 for eb in b: while cta < p: if a[cta] > eb: break suma += a[cta] cta += 1 while ctc < r: if c[ctc] > eb: break sumc += c[ctc] ctc += 1 ans += suma * sumc + eb * (suma * ctc + cta * sumc + cta * ctc * eb) ans %= mdl print(ans)
ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR WHILE VAR VAR IF VAR VAR VAR VAR VAR VAR VAR NUMBER WHILE VAR VAR IF VAR VAR VAR VAR VAR VAR VAR NUMBER VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR BIN_OP BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR
Given a triplet of integers (X , Y , Z), such that X ≀ Y and Y β‰₯ Z, we define f(X , Y , Z) to be (X + Y) * (Y + Z). If either X > Y or Y < Z, or both, then f(X , Y , Z) is defined to be 0. You are provided three arrays A , B and C of any length (their lengths may or may not be equal). Your task is to find the sum of f(X , Y , Z) over all triplets (X, Y , Z) where X, Y and Z belong to A, B and C respectively. Output your sum for each test case modulo 1000000007. -----Input----- - The first line contains a single integer, T, which is the number of test cases. The description of each testcase follows: - The first line of each testcase contains 3 integers: p, q and r. These denote the lengths of A,B and C respectively. - The second line contains p integers, which are the elements of A - The third line contains q integers, which are the elements of B - The fourth line contains r integers, which are the elements of C -----Output----- Output the required sum modulo 1000000007 for each test case in a new line. -----Constraints----- - 1 ≀ T ≀ 10 - 1 ≀ p, q, r ≀ 100000 - 1 ≀ every array element ≀ 1000000000 -----Subtasks ----- - Subtask #1 (30 points): 1 ≀ p,q,r ≀ 100 - Subtask #2 (70 points): 1 ≀ p,q,r ≀ 100000 -----Example : ----- Input: 1 3 1 3 1 2 3 5 4 5 6 Output: 399 -----Explanation: ----- As there is only one choice for Y which equals to 5, to get a non-zero function value,we can choose any element for X from the set { 1 , 2 , 3 } and for Z from the set { 4 , 5 } So triplets which give non-zero function values are: { 1 , 5 , 4 } : ( 1 + 5 ) * ( 5 + 4 ) = 54 { 1 , 5 , 5 } : ( 1 + 5 ) * ( 5 + 5 ) = 60 { 2 , 5 , 4 } : ( 2 + 5 ) * ( 5 + 4 ) = 63 { 2 , 5 , 5 } : ( 2 + 5 ) * ( 5 + 5 ) = 70 { 3 , 5 , 4 } : ( 3 + 5 ) * ( 5 + 4 ) = 72 { 3 , 5 , 5 } : ( 3 + 5 ) * ( 5 + 5 ) = 80 Final answer : 54 + 60 + 63 + 70 + 72 + 80 = 399
t = int(input()) for i in range(t): x, y, z = map(int, input().split()) l = list(map(int, input().split())) m = list(map(int, input().split())) n = list(map(int, input().split())) s = 0 for j in range(x): for k in range(y): for f in range(z): if l[j] <= m[k] and m[k] >= n[f]: s = s + (l[j] + m[k]) * (m[k] + n[f]) print(s % 1000000007)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP BIN_OP VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER
Given a triplet of integers (X , Y , Z), such that X ≀ Y and Y β‰₯ Z, we define f(X , Y , Z) to be (X + Y) * (Y + Z). If either X > Y or Y < Z, or both, then f(X , Y , Z) is defined to be 0. You are provided three arrays A , B and C of any length (their lengths may or may not be equal). Your task is to find the sum of f(X , Y , Z) over all triplets (X, Y , Z) where X, Y and Z belong to A, B and C respectively. Output your sum for each test case modulo 1000000007. -----Input----- - The first line contains a single integer, T, which is the number of test cases. The description of each testcase follows: - The first line of each testcase contains 3 integers: p, q and r. These denote the lengths of A,B and C respectively. - The second line contains p integers, which are the elements of A - The third line contains q integers, which are the elements of B - The fourth line contains r integers, which are the elements of C -----Output----- Output the required sum modulo 1000000007 for each test case in a new line. -----Constraints----- - 1 ≀ T ≀ 10 - 1 ≀ p, q, r ≀ 100000 - 1 ≀ every array element ≀ 1000000000 -----Subtasks ----- - Subtask #1 (30 points): 1 ≀ p,q,r ≀ 100 - Subtask #2 (70 points): 1 ≀ p,q,r ≀ 100000 -----Example : ----- Input: 1 3 1 3 1 2 3 5 4 5 6 Output: 399 -----Explanation: ----- As there is only one choice for Y which equals to 5, to get a non-zero function value,we can choose any element for X from the set { 1 , 2 , 3 } and for Z from the set { 4 , 5 } So triplets which give non-zero function values are: { 1 , 5 , 4 } : ( 1 + 5 ) * ( 5 + 4 ) = 54 { 1 , 5 , 5 } : ( 1 + 5 ) * ( 5 + 5 ) = 60 { 2 , 5 , 4 } : ( 2 + 5 ) * ( 5 + 4 ) = 63 { 2 , 5 , 5 } : ( 2 + 5 ) * ( 5 + 5 ) = 70 { 3 , 5 , 4 } : ( 3 + 5 ) * ( 5 + 4 ) = 72 { 3 , 5 , 5 } : ( 3 + 5 ) * ( 5 + 5 ) = 80 Final answer : 54 + 60 + 63 + 70 + 72 + 80 = 399
for v in range(int(input())): p, q, r = input().split(" ") a = [int(x) for x in input().split()] b = [int(x) for x in input().split()] d = [int(x) for x in input().split()] a.sort() b.sort() d.sort() s = 0 su = 0 c = 0 ss = 0 cc = 0 sum = 0 susu = 0 for w in range(int(q)): s = s + ss + b[w] * c while c < int(p): if a[c] <= b[w]: s = s + a[c] + b[w] ss = ss + a[c] c = c + 1 else: break su = su + susu + b[w] * cc while cc < int(r): if d[cc] <= b[w]: su = su + d[cc] + b[w] susu = susu + d[cc] cc = cc + 1 else: break sum = sum + s * su s = 0 su = 0 print(sum % 1000000007)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR VAR WHILE VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR VAR WHILE VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER
Given a triplet of integers (X , Y , Z), such that X ≀ Y and Y β‰₯ Z, we define f(X , Y , Z) to be (X + Y) * (Y + Z). If either X > Y or Y < Z, or both, then f(X , Y , Z) is defined to be 0. You are provided three arrays A , B and C of any length (their lengths may or may not be equal). Your task is to find the sum of f(X , Y , Z) over all triplets (X, Y , Z) where X, Y and Z belong to A, B and C respectively. Output your sum for each test case modulo 1000000007. -----Input----- - The first line contains a single integer, T, which is the number of test cases. The description of each testcase follows: - The first line of each testcase contains 3 integers: p, q and r. These denote the lengths of A,B and C respectively. - The second line contains p integers, which are the elements of A - The third line contains q integers, which are the elements of B - The fourth line contains r integers, which are the elements of C -----Output----- Output the required sum modulo 1000000007 for each test case in a new line. -----Constraints----- - 1 ≀ T ≀ 10 - 1 ≀ p, q, r ≀ 100000 - 1 ≀ every array element ≀ 1000000000 -----Subtasks ----- - Subtask #1 (30 points): 1 ≀ p,q,r ≀ 100 - Subtask #2 (70 points): 1 ≀ p,q,r ≀ 100000 -----Example : ----- Input: 1 3 1 3 1 2 3 5 4 5 6 Output: 399 -----Explanation: ----- As there is only one choice for Y which equals to 5, to get a non-zero function value,we can choose any element for X from the set { 1 , 2 , 3 } and for Z from the set { 4 , 5 } So triplets which give non-zero function values are: { 1 , 5 , 4 } : ( 1 + 5 ) * ( 5 + 4 ) = 54 { 1 , 5 , 5 } : ( 1 + 5 ) * ( 5 + 5 ) = 60 { 2 , 5 , 4 } : ( 2 + 5 ) * ( 5 + 4 ) = 63 { 2 , 5 , 5 } : ( 2 + 5 ) * ( 5 + 5 ) = 70 { 3 , 5 , 4 } : ( 3 + 5 ) * ( 5 + 4 ) = 72 { 3 , 5 , 5 } : ( 3 + 5 ) * ( 5 + 5 ) = 80 Final answer : 54 + 60 + 63 + 70 + 72 + 80 = 399
def R(): return list(map(int, input().split())) m = 1000000007 for i in range(int(input())): a, b, c = R() a1 = sorted(R()) b1 = sorted(R()) c1 = sorted(R()) ass = css = asum = csum = s = 0 for i in b1: while ass < a and a1[ass] <= i: asum += a1[ass] ass += 1 while css < c and c1[css] <= i: csum += c1[css] css += 1 s += (i * ass + asum) * (i * css + csum) s = s % m print(s)
FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR VAR VAR VAR NUMBER FOR VAR VAR WHILE VAR VAR VAR VAR VAR VAR VAR VAR VAR NUMBER WHILE VAR VAR VAR VAR VAR VAR VAR VAR VAR NUMBER VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR
Given a triplet of integers (X , Y , Z), such that X ≀ Y and Y β‰₯ Z, we define f(X , Y , Z) to be (X + Y) * (Y + Z). If either X > Y or Y < Z, or both, then f(X , Y , Z) is defined to be 0. You are provided three arrays A , B and C of any length (their lengths may or may not be equal). Your task is to find the sum of f(X , Y , Z) over all triplets (X, Y , Z) where X, Y and Z belong to A, B and C respectively. Output your sum for each test case modulo 1000000007. -----Input----- - The first line contains a single integer, T, which is the number of test cases. The description of each testcase follows: - The first line of each testcase contains 3 integers: p, q and r. These denote the lengths of A,B and C respectively. - The second line contains p integers, which are the elements of A - The third line contains q integers, which are the elements of B - The fourth line contains r integers, which are the elements of C -----Output----- Output the required sum modulo 1000000007 for each test case in a new line. -----Constraints----- - 1 ≀ T ≀ 10 - 1 ≀ p, q, r ≀ 100000 - 1 ≀ every array element ≀ 1000000000 -----Subtasks ----- - Subtask #1 (30 points): 1 ≀ p,q,r ≀ 100 - Subtask #2 (70 points): 1 ≀ p,q,r ≀ 100000 -----Example : ----- Input: 1 3 1 3 1 2 3 5 4 5 6 Output: 399 -----Explanation: ----- As there is only one choice for Y which equals to 5, to get a non-zero function value,we can choose any element for X from the set { 1 , 2 , 3 } and for Z from the set { 4 , 5 } So triplets which give non-zero function values are: { 1 , 5 , 4 } : ( 1 + 5 ) * ( 5 + 4 ) = 54 { 1 , 5 , 5 } : ( 1 + 5 ) * ( 5 + 5 ) = 60 { 2 , 5 , 4 } : ( 2 + 5 ) * ( 5 + 4 ) = 63 { 2 , 5 , 5 } : ( 2 + 5 ) * ( 5 + 5 ) = 70 { 3 , 5 , 4 } : ( 3 + 5 ) * ( 5 + 4 ) = 72 { 3 , 5 , 5 } : ( 3 + 5 ) * ( 5 + 5 ) = 80 Final answer : 54 + 60 + 63 + 70 + 72 + 80 = 399
T = int(input()) for i in range(T): n = list(map(int, input().split())) a = n[0] b = n[1] c = n[2] l = [] A = list(map(int, input().split())) B = list(map(int, input().split())) C = list(map(int, input().split())) for i in range(b): for j in range(a): for k in range(c): if A[j] <= B[i] and B[i] >= C[k]: sum = (A[j] + B[i]) * (B[i] + C[k]) l.append(int(sum)) sum = 0 for i in range(len(l)): sum += int(l[i]) print(sum % 1000000007)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR LIST ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER
Given a triplet of integers (X , Y , Z), such that X ≀ Y and Y β‰₯ Z, we define f(X , Y , Z) to be (X + Y) * (Y + Z). If either X > Y or Y < Z, or both, then f(X , Y , Z) is defined to be 0. You are provided three arrays A , B and C of any length (their lengths may or may not be equal). Your task is to find the sum of f(X , Y , Z) over all triplets (X, Y , Z) where X, Y and Z belong to A, B and C respectively. Output your sum for each test case modulo 1000000007. -----Input----- - The first line contains a single integer, T, which is the number of test cases. The description of each testcase follows: - The first line of each testcase contains 3 integers: p, q and r. These denote the lengths of A,B and C respectively. - The second line contains p integers, which are the elements of A - The third line contains q integers, which are the elements of B - The fourth line contains r integers, which are the elements of C -----Output----- Output the required sum modulo 1000000007 for each test case in a new line. -----Constraints----- - 1 ≀ T ≀ 10 - 1 ≀ p, q, r ≀ 100000 - 1 ≀ every array element ≀ 1000000000 -----Subtasks ----- - Subtask #1 (30 points): 1 ≀ p,q,r ≀ 100 - Subtask #2 (70 points): 1 ≀ p,q,r ≀ 100000 -----Example : ----- Input: 1 3 1 3 1 2 3 5 4 5 6 Output: 399 -----Explanation: ----- As there is only one choice for Y which equals to 5, to get a non-zero function value,we can choose any element for X from the set { 1 , 2 , 3 } and for Z from the set { 4 , 5 } So triplets which give non-zero function values are: { 1 , 5 , 4 } : ( 1 + 5 ) * ( 5 + 4 ) = 54 { 1 , 5 , 5 } : ( 1 + 5 ) * ( 5 + 5 ) = 60 { 2 , 5 , 4 } : ( 2 + 5 ) * ( 5 + 4 ) = 63 { 2 , 5 , 5 } : ( 2 + 5 ) * ( 5 + 5 ) = 70 { 3 , 5 , 4 } : ( 3 + 5 ) * ( 5 + 4 ) = 72 { 3 , 5 , 5 } : ( 3 + 5 ) * ( 5 + 5 ) = 80 Final answer : 54 + 60 + 63 + 70 + 72 + 80 = 399
t = int(input()) while t: p, q, r = map(int, input().split()) a = [int(x) for x in input().split()] b = [int(x) for x in input().split()] c = [int(x) for x in input().split()] a.append(1000000000000) c.append(1000000000000) a.sort() b.sort() c.sort() sumx = [a[0]] sumz = [c[0]] x = 0 for i in range(1, p): sumx.append(sumx[x] + a[i]) x += 1 x = 0 for i in range(1, r): sumz.append(sumz[x] + c[i]) x += 1 nx = 0 nz = 0 x = 0 for i in range(q): while a[nx] <= b[i]: nx += 1 while c[nz] <= b[i]: nz += 1 if nx and nz: x = ( x + nx * nz * b[i] * b[i] + sumx[nx - 1] * sumz[nz - 1] + (nz * sumx[nx - 1] + nx * sumz[nz - 1]) * b[i] ) print(x % 1000000007) sumx.clear() sumz.clear() t -= 1
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR LIST VAR NUMBER ASSIGN VAR LIST VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR WHILE VAR VAR VAR VAR VAR NUMBER WHILE VAR VAR VAR VAR VAR NUMBER IF VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER BIN_OP BIN_OP BIN_OP VAR VAR BIN_OP VAR NUMBER BIN_OP VAR VAR BIN_OP VAR NUMBER VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR NUMBER
Given a triplet of integers (X , Y , Z), such that X ≀ Y and Y β‰₯ Z, we define f(X , Y , Z) to be (X + Y) * (Y + Z). If either X > Y or Y < Z, or both, then f(X , Y , Z) is defined to be 0. You are provided three arrays A , B and C of any length (their lengths may or may not be equal). Your task is to find the sum of f(X , Y , Z) over all triplets (X, Y , Z) where X, Y and Z belong to A, B and C respectively. Output your sum for each test case modulo 1000000007. -----Input----- - The first line contains a single integer, T, which is the number of test cases. The description of each testcase follows: - The first line of each testcase contains 3 integers: p, q and r. These denote the lengths of A,B and C respectively. - The second line contains p integers, which are the elements of A - The third line contains q integers, which are the elements of B - The fourth line contains r integers, which are the elements of C -----Output----- Output the required sum modulo 1000000007 for each test case in a new line. -----Constraints----- - 1 ≀ T ≀ 10 - 1 ≀ p, q, r ≀ 100000 - 1 ≀ every array element ≀ 1000000000 -----Subtasks ----- - Subtask #1 (30 points): 1 ≀ p,q,r ≀ 100 - Subtask #2 (70 points): 1 ≀ p,q,r ≀ 100000 -----Example : ----- Input: 1 3 1 3 1 2 3 5 4 5 6 Output: 399 -----Explanation: ----- As there is only one choice for Y which equals to 5, to get a non-zero function value,we can choose any element for X from the set { 1 , 2 , 3 } and for Z from the set { 4 , 5 } So triplets which give non-zero function values are: { 1 , 5 , 4 } : ( 1 + 5 ) * ( 5 + 4 ) = 54 { 1 , 5 , 5 } : ( 1 + 5 ) * ( 5 + 5 ) = 60 { 2 , 5 , 4 } : ( 2 + 5 ) * ( 5 + 4 ) = 63 { 2 , 5 , 5 } : ( 2 + 5 ) * ( 5 + 5 ) = 70 { 3 , 5 , 4 } : ( 3 + 5 ) * ( 5 + 4 ) = 72 { 3 , 5 , 5 } : ( 3 + 5 ) * ( 5 + 5 ) = 80 Final answer : 54 + 60 + 63 + 70 + 72 + 80 = 399
T = int(input()) for t in range(T): inp = input().split() P = int(inp[0]) Q = int(inp[1]) R = int(inp[2]) A = input().split() A = list(map(int, A)) A.sort() B = input().split() B = list(map(int, B)) B.sort() C = input().split() C = list(map(int, C)) C.sort() A_sum = [] C_sum = [] A_sum.append(0) C_sum.append(0) i = 0 for a in A: A_sum.append(a + A_sum[i]) i = i + 1 j = 0 for c in C: C_sum.append(c + C_sum[j]) j = j + 1 p_visited = 0 r_visited = 0 output = 0 for q in B: while p_visited < P and A[p_visited] <= q: p_visited = p_visited + 1 while r_visited < R and C[r_visited] <= q: r_visited = r_visited + 1 if r_visited != 0 and p_visited != 0: output = output + (A_sum[p_visited] + p_visited * q) * ( C_sum[r_visited] + r_visited * q ) print(output % (10**9 + 7))
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR LIST EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR WHILE VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER IF VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR BIN_OP VAR VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER
Given a triplet of integers (X , Y , Z), such that X ≀ Y and Y β‰₯ Z, we define f(X , Y , Z) to be (X + Y) * (Y + Z). If either X > Y or Y < Z, or both, then f(X , Y , Z) is defined to be 0. You are provided three arrays A , B and C of any length (their lengths may or may not be equal). Your task is to find the sum of f(X , Y , Z) over all triplets (X, Y , Z) where X, Y and Z belong to A, B and C respectively. Output your sum for each test case modulo 1000000007. -----Input----- - The first line contains a single integer, T, which is the number of test cases. The description of each testcase follows: - The first line of each testcase contains 3 integers: p, q and r. These denote the lengths of A,B and C respectively. - The second line contains p integers, which are the elements of A - The third line contains q integers, which are the elements of B - The fourth line contains r integers, which are the elements of C -----Output----- Output the required sum modulo 1000000007 for each test case in a new line. -----Constraints----- - 1 ≀ T ≀ 10 - 1 ≀ p, q, r ≀ 100000 - 1 ≀ every array element ≀ 1000000000 -----Subtasks ----- - Subtask #1 (30 points): 1 ≀ p,q,r ≀ 100 - Subtask #2 (70 points): 1 ≀ p,q,r ≀ 100000 -----Example : ----- Input: 1 3 1 3 1 2 3 5 4 5 6 Output: 399 -----Explanation: ----- As there is only one choice for Y which equals to 5, to get a non-zero function value,we can choose any element for X from the set { 1 , 2 , 3 } and for Z from the set { 4 , 5 } So triplets which give non-zero function values are: { 1 , 5 , 4 } : ( 1 + 5 ) * ( 5 + 4 ) = 54 { 1 , 5 , 5 } : ( 1 + 5 ) * ( 5 + 5 ) = 60 { 2 , 5 , 4 } : ( 2 + 5 ) * ( 5 + 4 ) = 63 { 2 , 5 , 5 } : ( 2 + 5 ) * ( 5 + 5 ) = 70 { 3 , 5 , 4 } : ( 3 + 5 ) * ( 5 + 4 ) = 72 { 3 , 5 , 5 } : ( 3 + 5 ) * ( 5 + 5 ) = 80 Final answer : 54 + 60 + 63 + 70 + 72 + 80 = 399
import sys t = int(input()) while t: x, y, z = map(int, sys.stdin.readline().split()) a = [int(x) for x in input().split()] b = [int(x) for x in input().split()] c = [int(x) for x in input().split()] count = 0 a.sort() b.sort() c.sort() i = 0 j = 0 asum = 0 csum = 0 ans = 0 for elm in b: if i < x: while elm >= a[i]: asum += a[i] i += 1 if i >= x: break if j < z: while elm >= c[j]: csum += c[j] j += 1 if j >= z: break if i != 0 and j != 0: ans += (asum + elm * i) * (csum + elm * j) print(ans % 1000000007) t -= 1
IMPORT ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF VAR VAR WHILE VAR VAR VAR VAR VAR VAR VAR NUMBER IF VAR VAR IF VAR VAR WHILE VAR VAR VAR VAR VAR VAR VAR NUMBER IF VAR VAR IF VAR NUMBER VAR NUMBER VAR BIN_OP BIN_OP VAR BIN_OP VAR VAR BIN_OP VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER
Given a triplet of integers (X , Y , Z), such that X ≀ Y and Y β‰₯ Z, we define f(X , Y , Z) to be (X + Y) * (Y + Z). If either X > Y or Y < Z, or both, then f(X , Y , Z) is defined to be 0. You are provided three arrays A , B and C of any length (their lengths may or may not be equal). Your task is to find the sum of f(X , Y , Z) over all triplets (X, Y , Z) where X, Y and Z belong to A, B and C respectively. Output your sum for each test case modulo 1000000007. -----Input----- - The first line contains a single integer, T, which is the number of test cases. The description of each testcase follows: - The first line of each testcase contains 3 integers: p, q and r. These denote the lengths of A,B and C respectively. - The second line contains p integers, which are the elements of A - The third line contains q integers, which are the elements of B - The fourth line contains r integers, which are the elements of C -----Output----- Output the required sum modulo 1000000007 for each test case in a new line. -----Constraints----- - 1 ≀ T ≀ 10 - 1 ≀ p, q, r ≀ 100000 - 1 ≀ every array element ≀ 1000000000 -----Subtasks ----- - Subtask #1 (30 points): 1 ≀ p,q,r ≀ 100 - Subtask #2 (70 points): 1 ≀ p,q,r ≀ 100000 -----Example : ----- Input: 1 3 1 3 1 2 3 5 4 5 6 Output: 399 -----Explanation: ----- As there is only one choice for Y which equals to 5, to get a non-zero function value,we can choose any element for X from the set { 1 , 2 , 3 } and for Z from the set { 4 , 5 } So triplets which give non-zero function values are: { 1 , 5 , 4 } : ( 1 + 5 ) * ( 5 + 4 ) = 54 { 1 , 5 , 5 } : ( 1 + 5 ) * ( 5 + 5 ) = 60 { 2 , 5 , 4 } : ( 2 + 5 ) * ( 5 + 4 ) = 63 { 2 , 5 , 5 } : ( 2 + 5 ) * ( 5 + 5 ) = 70 { 3 , 5 , 4 } : ( 3 + 5 ) * ( 5 + 4 ) = 72 { 3 , 5 , 5 } : ( 3 + 5 ) * ( 5 + 5 ) = 80 Final answer : 54 + 60 + 63 + 70 + 72 + 80 = 399
def mod(n): return n % 1000000007 for _ in range(int(input())): p, q, r = map(int, input().split()) A = sorted(list(map(int, input().split()))) B = sorted(list(map(int, input().split()))) C = sorted(list(map(int, input().split()))) ans, i, l = 0, 0, 0 sa, sc = 0, 0 for b in B: while i < p and A[i] <= b: sa = mod(sa + A[i]) i += 1 while l < r and C[l] <= b: sc = mod(sc + C[l]) l += 1 if l > 0 and i > 0: ans = mod( ans + mod(b * b * i * l) + mod(b * l * sa) + mod(b * i * sc) + mod(sa * sc) ) print(ans)
FUNC_DEF RETURN BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR NUMBER NUMBER FOR VAR VAR WHILE VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR NUMBER WHILE VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR NUMBER IF VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR VAR FUNC_CALL VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR
Given a triplet of integers (X , Y , Z), such that X ≀ Y and Y β‰₯ Z, we define f(X , Y , Z) to be (X + Y) * (Y + Z). If either X > Y or Y < Z, or both, then f(X , Y , Z) is defined to be 0. You are provided three arrays A , B and C of any length (their lengths may or may not be equal). Your task is to find the sum of f(X , Y , Z) over all triplets (X, Y , Z) where X, Y and Z belong to A, B and C respectively. Output your sum for each test case modulo 1000000007. -----Input----- - The first line contains a single integer, T, which is the number of test cases. The description of each testcase follows: - The first line of each testcase contains 3 integers: p, q and r. These denote the lengths of A,B and C respectively. - The second line contains p integers, which are the elements of A - The third line contains q integers, which are the elements of B - The fourth line contains r integers, which are the elements of C -----Output----- Output the required sum modulo 1000000007 for each test case in a new line. -----Constraints----- - 1 ≀ T ≀ 10 - 1 ≀ p, q, r ≀ 100000 - 1 ≀ every array element ≀ 1000000000 -----Subtasks ----- - Subtask #1 (30 points): 1 ≀ p,q,r ≀ 100 - Subtask #2 (70 points): 1 ≀ p,q,r ≀ 100000 -----Example : ----- Input: 1 3 1 3 1 2 3 5 4 5 6 Output: 399 -----Explanation: ----- As there is only one choice for Y which equals to 5, to get a non-zero function value,we can choose any element for X from the set { 1 , 2 , 3 } and for Z from the set { 4 , 5 } So triplets which give non-zero function values are: { 1 , 5 , 4 } : ( 1 + 5 ) * ( 5 + 4 ) = 54 { 1 , 5 , 5 } : ( 1 + 5 ) * ( 5 + 5 ) = 60 { 2 , 5 , 4 } : ( 2 + 5 ) * ( 5 + 4 ) = 63 { 2 , 5 , 5 } : ( 2 + 5 ) * ( 5 + 5 ) = 70 { 3 , 5 , 4 } : ( 3 + 5 ) * ( 5 + 4 ) = 72 { 3 , 5 , 5 } : ( 3 + 5 ) * ( 5 + 5 ) = 80 Final answer : 54 + 60 + 63 + 70 + 72 + 80 = 399
M = 1000000007 def solve(): sum = 0 i, k = 0, 0 x_sum, z_sum = 0, 0 for b in B: while i < p and A[i] <= b: x_sum = (x_sum + A[i]) % M i += 1 while k < r and C[k] <= b: z_sum = (z_sum + C[k]) % M k += 1 sum = (sum + (i * b % M + x_sum) % M * (k * b % M + z_sum) % M % M) % M return sum T = int(input()) while T: p, q, r = map(int, input().split()) A = sorted(list(map(int, input().split()))) B = sorted(list(map(int, input().split()))) C = sorted(list(map(int, input().split()))) print(solve()) T -= 1
ASSIGN VAR NUMBER FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER NUMBER FOR VAR VAR WHILE VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR NUMBER WHILE VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP BIN_OP BIN_OP BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR VAR VAR VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER
Given a triplet of integers (X , Y , Z), such that X ≀ Y and Y β‰₯ Z, we define f(X , Y , Z) to be (X + Y) * (Y + Z). If either X > Y or Y < Z, or both, then f(X , Y , Z) is defined to be 0. You are provided three arrays A , B and C of any length (their lengths may or may not be equal). Your task is to find the sum of f(X , Y , Z) over all triplets (X, Y , Z) where X, Y and Z belong to A, B and C respectively. Output your sum for each test case modulo 1000000007. -----Input----- - The first line contains a single integer, T, which is the number of test cases. The description of each testcase follows: - The first line of each testcase contains 3 integers: p, q and r. These denote the lengths of A,B and C respectively. - The second line contains p integers, which are the elements of A - The third line contains q integers, which are the elements of B - The fourth line contains r integers, which are the elements of C -----Output----- Output the required sum modulo 1000000007 for each test case in a new line. -----Constraints----- - 1 ≀ T ≀ 10 - 1 ≀ p, q, r ≀ 100000 - 1 ≀ every array element ≀ 1000000000 -----Subtasks ----- - Subtask #1 (30 points): 1 ≀ p,q,r ≀ 100 - Subtask #2 (70 points): 1 ≀ p,q,r ≀ 100000 -----Example : ----- Input: 1 3 1 3 1 2 3 5 4 5 6 Output: 399 -----Explanation: ----- As there is only one choice for Y which equals to 5, to get a non-zero function value,we can choose any element for X from the set { 1 , 2 , 3 } and for Z from the set { 4 , 5 } So triplets which give non-zero function values are: { 1 , 5 , 4 } : ( 1 + 5 ) * ( 5 + 4 ) = 54 { 1 , 5 , 5 } : ( 1 + 5 ) * ( 5 + 5 ) = 60 { 2 , 5 , 4 } : ( 2 + 5 ) * ( 5 + 4 ) = 63 { 2 , 5 , 5 } : ( 2 + 5 ) * ( 5 + 5 ) = 70 { 3 , 5 , 4 } : ( 3 + 5 ) * ( 5 + 4 ) = 72 { 3 , 5 , 5 } : ( 3 + 5 ) * ( 5 + 5 ) = 80 Final answer : 54 + 60 + 63 + 70 + 72 + 80 = 399
t = int(input()) for k in range(t): p, q, r = map(int, input().rstrip().split()) l1 = sorted(list(map(int, input().rstrip().split()))) l2 = sorted(list(map(int, input().rstrip().split()))) l3 = sorted(list(map(int, input().rstrip().split()))) i, j, k, s, sum = 0, 0, 0, 0, 0 for y in l2: while i < p and y >= l1[i]: k = k + l1[i] i = i + 1 while j < r and y >= l3[j]: s = s + l3[j] j = j + 1 sum = sum + (i * y + k) * (j * y + s) print(sum % (10**9 + 7))
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR VAR VAR VAR NUMBER NUMBER NUMBER NUMBER NUMBER FOR VAR VAR WHILE VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER
Given a triplet of integers (X , Y , Z), such that X ≀ Y and Y β‰₯ Z, we define f(X , Y , Z) to be (X + Y) * (Y + Z). If either X > Y or Y < Z, or both, then f(X , Y , Z) is defined to be 0. You are provided three arrays A , B and C of any length (their lengths may or may not be equal). Your task is to find the sum of f(X , Y , Z) over all triplets (X, Y , Z) where X, Y and Z belong to A, B and C respectively. Output your sum for each test case modulo 1000000007. -----Input----- - The first line contains a single integer, T, which is the number of test cases. The description of each testcase follows: - The first line of each testcase contains 3 integers: p, q and r. These denote the lengths of A,B and C respectively. - The second line contains p integers, which are the elements of A - The third line contains q integers, which are the elements of B - The fourth line contains r integers, which are the elements of C -----Output----- Output the required sum modulo 1000000007 for each test case in a new line. -----Constraints----- - 1 ≀ T ≀ 10 - 1 ≀ p, q, r ≀ 100000 - 1 ≀ every array element ≀ 1000000000 -----Subtasks ----- - Subtask #1 (30 points): 1 ≀ p,q,r ≀ 100 - Subtask #2 (70 points): 1 ≀ p,q,r ≀ 100000 -----Example : ----- Input: 1 3 1 3 1 2 3 5 4 5 6 Output: 399 -----Explanation: ----- As there is only one choice for Y which equals to 5, to get a non-zero function value,we can choose any element for X from the set { 1 , 2 , 3 } and for Z from the set { 4 , 5 } So triplets which give non-zero function values are: { 1 , 5 , 4 } : ( 1 + 5 ) * ( 5 + 4 ) = 54 { 1 , 5 , 5 } : ( 1 + 5 ) * ( 5 + 5 ) = 60 { 2 , 5 , 4 } : ( 2 + 5 ) * ( 5 + 4 ) = 63 { 2 , 5 , 5 } : ( 2 + 5 ) * ( 5 + 5 ) = 70 { 3 , 5 , 4 } : ( 3 + 5 ) * ( 5 + 4 ) = 72 { 3 , 5 , 5 } : ( 3 + 5 ) * ( 5 + 5 ) = 80 Final answer : 54 + 60 + 63 + 70 + 72 + 80 = 399
t = int(input()) for temp in range(0, t): p, q, r = map(int, input().strip().split()) mod = 1000000007 res = 0 a = list(map(int, input().strip().split())) b = list(map(int, input().strip().split())) c = list(map(int, input().strip().split())) a.sort() b.sort(reverse=True) c.sort() j = p - 1 k = r - 1 bc = [0] * q ba = [0] * q for i in range(0, q): while j >= 0 and b[i] < a[j]: j -= 1 while k >= 0 and b[i] < c[k]: k -= 1 ba[i] = j + 1 bc[i] = k + 1 suma = [0] * (p + 1) sumc = [0] * (r + 1) for i in range(1, p + 1): suma[i] = (suma[i - 1] + a[i - 1]) % mod for i in range(1, r + 1): sumc[i] = (sumc[i - 1] + c[i - 1]) % mod for i in range(0, q): res = res + ba[i] * bc[i] % mod * b[i] % mod * b[i] res = res + suma[ba[i]] * b[i] % mod * bc[i] res = res + sumc[bc[i]] * b[i] % mod * ba[i] res = res + suma[ba[i]] * sumc[bc[i]] res %= mod print(res % mod)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER VAR WHILE VAR NUMBER VAR VAR VAR VAR VAR NUMBER WHILE VAR NUMBER VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR BIN_OP VAR BIN_OP BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR VAR VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR
Given a triplet of integers (X , Y , Z), such that X ≀ Y and Y β‰₯ Z, we define f(X , Y , Z) to be (X + Y) * (Y + Z). If either X > Y or Y < Z, or both, then f(X , Y , Z) is defined to be 0. You are provided three arrays A , B and C of any length (their lengths may or may not be equal). Your task is to find the sum of f(X , Y , Z) over all triplets (X, Y , Z) where X, Y and Z belong to A, B and C respectively. Output your sum for each test case modulo 1000000007. -----Input----- - The first line contains a single integer, T, which is the number of test cases. The description of each testcase follows: - The first line of each testcase contains 3 integers: p, q and r. These denote the lengths of A,B and C respectively. - The second line contains p integers, which are the elements of A - The third line contains q integers, which are the elements of B - The fourth line contains r integers, which are the elements of C -----Output----- Output the required sum modulo 1000000007 for each test case in a new line. -----Constraints----- - 1 ≀ T ≀ 10 - 1 ≀ p, q, r ≀ 100000 - 1 ≀ every array element ≀ 1000000000 -----Subtasks ----- - Subtask #1 (30 points): 1 ≀ p,q,r ≀ 100 - Subtask #2 (70 points): 1 ≀ p,q,r ≀ 100000 -----Example : ----- Input: 1 3 1 3 1 2 3 5 4 5 6 Output: 399 -----Explanation: ----- As there is only one choice for Y which equals to 5, to get a non-zero function value,we can choose any element for X from the set { 1 , 2 , 3 } and for Z from the set { 4 , 5 } So triplets which give non-zero function values are: { 1 , 5 , 4 } : ( 1 + 5 ) * ( 5 + 4 ) = 54 { 1 , 5 , 5 } : ( 1 + 5 ) * ( 5 + 5 ) = 60 { 2 , 5 , 4 } : ( 2 + 5 ) * ( 5 + 4 ) = 63 { 2 , 5 , 5 } : ( 2 + 5 ) * ( 5 + 5 ) = 70 { 3 , 5 , 4 } : ( 3 + 5 ) * ( 5 + 4 ) = 72 { 3 , 5 , 5 } : ( 3 + 5 ) * ( 5 + 5 ) = 80 Final answer : 54 + 60 + 63 + 70 + 72 + 80 = 399
M = 1000000007 def func(y, xsum, zsum, vx, vz): return ( y * ((xsum * vz % M + zsum * vx % M) % M) % M + vx * (vz * (y * y % M) % M) % M + xsum * zsum % M ) t = int(input()) while t > 0: p, q, r = input().split() a = list(map(int, input().split())) b = list(map(int, input().split())) c = list(map(int, input().split())) p = int(p) q = int(q) r = int(r) a.sort() b.sort() c.sort() Sum = 0 xsum = 0 zsum = 0 vx = 0 vz = 0 for i in range(0, q): for j in range(vx, p): if a[j] > b[i]: break vx += 1 xsum = (xsum + a[j]) % M for j in range(vz, r): if c[j] > b[i]: break vz += 1 zsum = (zsum + c[j]) % M Sum = (Sum + func(b[i], xsum, zsum, vx, vz)) % M print(Sum, "\n") t = t - 1
ASSIGN VAR NUMBER FUNC_DEF RETURN BIN_OP BIN_OP BIN_OP BIN_OP VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR BIN_OP BIN_OP VAR BIN_OP BIN_OP VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR NUMBER ASSIGN VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR FOR VAR FUNC_CALL VAR VAR VAR IF VAR VAR VAR VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR VAR FOR VAR FUNC_CALL VAR VAR VAR IF VAR VAR VAR VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR STRING ASSIGN VAR BIN_OP VAR NUMBER
Given a triplet of integers (X , Y , Z), such that X ≀ Y and Y β‰₯ Z, we define f(X , Y , Z) to be (X + Y) * (Y + Z). If either X > Y or Y < Z, or both, then f(X , Y , Z) is defined to be 0. You are provided three arrays A , B and C of any length (their lengths may or may not be equal). Your task is to find the sum of f(X , Y , Z) over all triplets (X, Y , Z) where X, Y and Z belong to A, B and C respectively. Output your sum for each test case modulo 1000000007. -----Input----- - The first line contains a single integer, T, which is the number of test cases. The description of each testcase follows: - The first line of each testcase contains 3 integers: p, q and r. These denote the lengths of A,B and C respectively. - The second line contains p integers, which are the elements of A - The third line contains q integers, which are the elements of B - The fourth line contains r integers, which are the elements of C -----Output----- Output the required sum modulo 1000000007 for each test case in a new line. -----Constraints----- - 1 ≀ T ≀ 10 - 1 ≀ p, q, r ≀ 100000 - 1 ≀ every array element ≀ 1000000000 -----Subtasks ----- - Subtask #1 (30 points): 1 ≀ p,q,r ≀ 100 - Subtask #2 (70 points): 1 ≀ p,q,r ≀ 100000 -----Example : ----- Input: 1 3 1 3 1 2 3 5 4 5 6 Output: 399 -----Explanation: ----- As there is only one choice for Y which equals to 5, to get a non-zero function value,we can choose any element for X from the set { 1 , 2 , 3 } and for Z from the set { 4 , 5 } So triplets which give non-zero function values are: { 1 , 5 , 4 } : ( 1 + 5 ) * ( 5 + 4 ) = 54 { 1 , 5 , 5 } : ( 1 + 5 ) * ( 5 + 5 ) = 60 { 2 , 5 , 4 } : ( 2 + 5 ) * ( 5 + 4 ) = 63 { 2 , 5 , 5 } : ( 2 + 5 ) * ( 5 + 5 ) = 70 { 3 , 5 , 4 } : ( 3 + 5 ) * ( 5 + 4 ) = 72 { 3 , 5 , 5 } : ( 3 + 5 ) * ( 5 + 5 ) = 80 Final answer : 54 + 60 + 63 + 70 + 72 + 80 = 399
t = int(input()) for i in range(t): [p, q, r] = [int(n) for n in input().split()] A = [int(n) for n in input().split()] B = [int(n) for n in input().split()] C = [int(n) for n in input().split()] A.sort() B.sort() C.sort() sumA = p * [0] sumB = q * [0] sumC = r * [0] sumA[0] = A[0] sumB[0] = B[0] sumC[0] = C[0] for i in range(1, q): sumB[i] = sumB[i - 1] + B[i] for i in range(1, p): sumA[i] = sumA[i - 1] + A[i] for i in range(1, r): sumC[i] = sumC[i - 1] + C[i] sum = 0 yindex = 0 xindex = 0 zindex = 0 while yindex < q: flag = True while xindex < p and B[yindex] >= A[xindex]: xindex = xindex + 1 flag = False if not flag: xindex = xindex - 1 flag = True while zindex < r and B[yindex] >= C[zindex]: zindex = zindex + 1 flag = False if not flag: zindex = zindex - 1 if B[yindex] >= A[xindex] and B[yindex] >= C[zindex]: sum = ( sum + B[yindex] * B[yindex] * (xindex + 1) * (zindex + 1) + B[yindex] * (sumA[xindex] * (zindex + 1) + sumC[zindex] * (xindex + 1)) + sumA[xindex] * sumC[zindex] ) yindex = yindex + 1 print(sum % 1000000007)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN LIST VAR VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR BIN_OP VAR LIST NUMBER ASSIGN VAR BIN_OP VAR LIST NUMBER ASSIGN VAR BIN_OP VAR LIST NUMBER ASSIGN VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR NUMBER WHILE VAR VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER IF VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER IF VAR ASSIGN VAR BIN_OP VAR NUMBER IF VAR VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR NUMBER BIN_OP VAR VAR BIN_OP VAR NUMBER BIN_OP VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER
Given a triplet of integers (X , Y , Z), such that X ≀ Y and Y β‰₯ Z, we define f(X , Y , Z) to be (X + Y) * (Y + Z). If either X > Y or Y < Z, or both, then f(X , Y , Z) is defined to be 0. You are provided three arrays A , B and C of any length (their lengths may or may not be equal). Your task is to find the sum of f(X , Y , Z) over all triplets (X, Y , Z) where X, Y and Z belong to A, B and C respectively. Output your sum for each test case modulo 1000000007. -----Input----- - The first line contains a single integer, T, which is the number of test cases. The description of each testcase follows: - The first line of each testcase contains 3 integers: p, q and r. These denote the lengths of A,B and C respectively. - The second line contains p integers, which are the elements of A - The third line contains q integers, which are the elements of B - The fourth line contains r integers, which are the elements of C -----Output----- Output the required sum modulo 1000000007 for each test case in a new line. -----Constraints----- - 1 ≀ T ≀ 10 - 1 ≀ p, q, r ≀ 100000 - 1 ≀ every array element ≀ 1000000000 -----Subtasks ----- - Subtask #1 (30 points): 1 ≀ p,q,r ≀ 100 - Subtask #2 (70 points): 1 ≀ p,q,r ≀ 100000 -----Example : ----- Input: 1 3 1 3 1 2 3 5 4 5 6 Output: 399 -----Explanation: ----- As there is only one choice for Y which equals to 5, to get a non-zero function value,we can choose any element for X from the set { 1 , 2 , 3 } and for Z from the set { 4 , 5 } So triplets which give non-zero function values are: { 1 , 5 , 4 } : ( 1 + 5 ) * ( 5 + 4 ) = 54 { 1 , 5 , 5 } : ( 1 + 5 ) * ( 5 + 5 ) = 60 { 2 , 5 , 4 } : ( 2 + 5 ) * ( 5 + 4 ) = 63 { 2 , 5 , 5 } : ( 2 + 5 ) * ( 5 + 5 ) = 70 { 3 , 5 , 4 } : ( 3 + 5 ) * ( 5 + 4 ) = 72 { 3 , 5 , 5 } : ( 3 + 5 ) * ( 5 + 5 ) = 80 Final answer : 54 + 60 + 63 + 70 + 72 + 80 = 399
T = int(input()) for _ in range(0, T): x, y, z = map(int, input().split()) X = list(map(int, input().split())) Y = list(map(int, input().split())) Z = list(map(int, input().split())) x = x + 1 z = z + 1 X.insert(0, 0) Z.insert(0, 0) X.sort() Y.sort() Z.sort() sumx = list() sumx.append(0) for i in range(1, x): sumx.append(sumx[i - 1] + X[i]) sumz = list() sumz.append(0) for i in range(1, z): sumz.append(sumz[i - 1] + Z[i]) sum = 0 px = 1 py = 1 pz = 1 for i in range(y): while X[px] <= Y[i] and px <= x - 1: px = px + 1 if px == x: break px = px - 1 while Z[pz] <= Y[i] and pz <= z - 1: pz = pz + 1 if pz == z: break pz = pz - 1 sum += Y[i] * (px * pz * Y[i] + px * sumz[pz]) + sumx[px] * ( pz * Y[i] + sumz[pz] ) print(sum % 1000000007)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER NUMBER EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR EXPR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR EXPR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR WHILE VAR VAR VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF VAR VAR ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR VAR VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF VAR VAR ASSIGN VAR BIN_OP VAR NUMBER VAR BIN_OP BIN_OP VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER
Given a triplet of integers (X , Y , Z), such that X ≀ Y and Y β‰₯ Z, we define f(X , Y , Z) to be (X + Y) * (Y + Z). If either X > Y or Y < Z, or both, then f(X , Y , Z) is defined to be 0. You are provided three arrays A , B and C of any length (their lengths may or may not be equal). Your task is to find the sum of f(X , Y , Z) over all triplets (X, Y , Z) where X, Y and Z belong to A, B and C respectively. Output your sum for each test case modulo 1000000007. -----Input----- - The first line contains a single integer, T, which is the number of test cases. The description of each testcase follows: - The first line of each testcase contains 3 integers: p, q and r. These denote the lengths of A,B and C respectively. - The second line contains p integers, which are the elements of A - The third line contains q integers, which are the elements of B - The fourth line contains r integers, which are the elements of C -----Output----- Output the required sum modulo 1000000007 for each test case in a new line. -----Constraints----- - 1 ≀ T ≀ 10 - 1 ≀ p, q, r ≀ 100000 - 1 ≀ every array element ≀ 1000000000 -----Subtasks ----- - Subtask #1 (30 points): 1 ≀ p,q,r ≀ 100 - Subtask #2 (70 points): 1 ≀ p,q,r ≀ 100000 -----Example : ----- Input: 1 3 1 3 1 2 3 5 4 5 6 Output: 399 -----Explanation: ----- As there is only one choice for Y which equals to 5, to get a non-zero function value,we can choose any element for X from the set { 1 , 2 , 3 } and for Z from the set { 4 , 5 } So triplets which give non-zero function values are: { 1 , 5 , 4 } : ( 1 + 5 ) * ( 5 + 4 ) = 54 { 1 , 5 , 5 } : ( 1 + 5 ) * ( 5 + 5 ) = 60 { 2 , 5 , 4 } : ( 2 + 5 ) * ( 5 + 4 ) = 63 { 2 , 5 , 5 } : ( 2 + 5 ) * ( 5 + 5 ) = 70 { 3 , 5 , 4 } : ( 3 + 5 ) * ( 5 + 4 ) = 72 { 3 , 5 , 5 } : ( 3 + 5 ) * ( 5 + 5 ) = 80 Final answer : 54 + 60 + 63 + 70 + 72 + 80 = 399
t = int(input()) m = 1000000007 for i in range(t): p, q, r = map(int, input().split()) pa = [] qa = [] ra = [] pa.extend(map(int, input().split())) qa.extend(map(int, input().split())) ra.extend(map(int, input().split())) pa.sort(reverse=True) qa.sort(reverse=True) ra.sort(reverse=True) pas = [0] ras = [0] s = 0 for j in range(p): s += pa[j] pas.append(s) s = 0 for j in range(r): s += ra[j] ras.append(s) pind = 0 rind = 0 sm = 0 for j in range(q): try: while pa[pind] > qa[j]: pind += 1 while ra[rind] > qa[j]: rind += 1 except: break sm = ( sm + (pas[-1] - pas[pind] + (p - pind) * qa[j]) % m * ((ras[-1] - ras[rind] + (r - rind) * qa[j]) % m) % m ) % m print(sm)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR LIST EXPR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR LIST NUMBER ASSIGN VAR LIST NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR WHILE VAR VAR VAR VAR VAR NUMBER WHILE VAR VAR VAR VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP BIN_OP BIN_OP BIN_OP BIN_OP VAR NUMBER VAR VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR BIN_OP BIN_OP BIN_OP VAR NUMBER VAR VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR
Given a triplet of integers (X , Y , Z), such that X ≀ Y and Y β‰₯ Z, we define f(X , Y , Z) to be (X + Y) * (Y + Z). If either X > Y or Y < Z, or both, then f(X , Y , Z) is defined to be 0. You are provided three arrays A , B and C of any length (their lengths may or may not be equal). Your task is to find the sum of f(X , Y , Z) over all triplets (X, Y , Z) where X, Y and Z belong to A, B and C respectively. Output your sum for each test case modulo 1000000007. -----Input----- - The first line contains a single integer, T, which is the number of test cases. The description of each testcase follows: - The first line of each testcase contains 3 integers: p, q and r. These denote the lengths of A,B and C respectively. - The second line contains p integers, which are the elements of A - The third line contains q integers, which are the elements of B - The fourth line contains r integers, which are the elements of C -----Output----- Output the required sum modulo 1000000007 for each test case in a new line. -----Constraints----- - 1 ≀ T ≀ 10 - 1 ≀ p, q, r ≀ 100000 - 1 ≀ every array element ≀ 1000000000 -----Subtasks ----- - Subtask #1 (30 points): 1 ≀ p,q,r ≀ 100 - Subtask #2 (70 points): 1 ≀ p,q,r ≀ 100000 -----Example : ----- Input: 1 3 1 3 1 2 3 5 4 5 6 Output: 399 -----Explanation: ----- As there is only one choice for Y which equals to 5, to get a non-zero function value,we can choose any element for X from the set { 1 , 2 , 3 } and for Z from the set { 4 , 5 } So triplets which give non-zero function values are: { 1 , 5 , 4 } : ( 1 + 5 ) * ( 5 + 4 ) = 54 { 1 , 5 , 5 } : ( 1 + 5 ) * ( 5 + 5 ) = 60 { 2 , 5 , 4 } : ( 2 + 5 ) * ( 5 + 4 ) = 63 { 2 , 5 , 5 } : ( 2 + 5 ) * ( 5 + 5 ) = 70 { 3 , 5 , 4 } : ( 3 + 5 ) * ( 5 + 4 ) = 72 { 3 , 5 , 5 } : ( 3 + 5 ) * ( 5 + 5 ) = 80 Final answer : 54 + 60 + 63 + 70 + 72 + 80 = 399
t = int(input()) for i in range(t): sum1 = 0 sum2 = 0 n = list(map(int, input().split())) p = n[0] q = n[1] r = n[2] arr1 = list(map(int, input().split())) arr2 = list(map(int, input().split())) arr3 = list(map(int, input().split())) sum1 = sum(arr1) sum2 = sum(arr3) arr1.sort() arr2.sort() arr3.sort() flag = q - 1 sumq = 0 temp1 = p - 1 temp2 = r - 1 while flag >= 0: while temp1 >= 0 and arr1[temp1] > arr2[flag]: sum1 = sum1 - arr1[temp1] temp1 = temp1 - 1 while temp2 >= 0 and arr3[temp2] > arr2[flag]: sum2 = sum2 - arr3[temp2] temp2 = temp2 - 1 if temp1 == -1 or temp2 == -1: flag = -1 else: sum1 = sum1 % 1000000007 g = (temp1 + 1) * (arr2[flag] % 1000000007) % 1000000007 h = (temp2 + 1) * (arr2[flag] % 1000000007) % 1000000007 sumq = sumq + (sum1 + g) * (sum2 + h) sumq = sumq % 1000000007 flag = flag - 1 print(sumq)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR NUMBER WHILE VAR NUMBER VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR NUMBER VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER IF VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR NUMBER BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR NUMBER BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR
Given a triplet of integers (X , Y , Z), such that X ≀ Y and Y β‰₯ Z, we define f(X , Y , Z) to be (X + Y) * (Y + Z). If either X > Y or Y < Z, or both, then f(X , Y , Z) is defined to be 0. You are provided three arrays A , B and C of any length (their lengths may or may not be equal). Your task is to find the sum of f(X , Y , Z) over all triplets (X, Y , Z) where X, Y and Z belong to A, B and C respectively. Output your sum for each test case modulo 1000000007. -----Input----- - The first line contains a single integer, T, which is the number of test cases. The description of each testcase follows: - The first line of each testcase contains 3 integers: p, q and r. These denote the lengths of A,B and C respectively. - The second line contains p integers, which are the elements of A - The third line contains q integers, which are the elements of B - The fourth line contains r integers, which are the elements of C -----Output----- Output the required sum modulo 1000000007 for each test case in a new line. -----Constraints----- - 1 ≀ T ≀ 10 - 1 ≀ p, q, r ≀ 100000 - 1 ≀ every array element ≀ 1000000000 -----Subtasks ----- - Subtask #1 (30 points): 1 ≀ p,q,r ≀ 100 - Subtask #2 (70 points): 1 ≀ p,q,r ≀ 100000 -----Example : ----- Input: 1 3 1 3 1 2 3 5 4 5 6 Output: 399 -----Explanation: ----- As there is only one choice for Y which equals to 5, to get a non-zero function value,we can choose any element for X from the set { 1 , 2 , 3 } and for Z from the set { 4 , 5 } So triplets which give non-zero function values are: { 1 , 5 , 4 } : ( 1 + 5 ) * ( 5 + 4 ) = 54 { 1 , 5 , 5 } : ( 1 + 5 ) * ( 5 + 5 ) = 60 { 2 , 5 , 4 } : ( 2 + 5 ) * ( 5 + 4 ) = 63 { 2 , 5 , 5 } : ( 2 + 5 ) * ( 5 + 5 ) = 70 { 3 , 5 , 4 } : ( 3 + 5 ) * ( 5 + 4 ) = 72 { 3 , 5 , 5 } : ( 3 + 5 ) * ( 5 + 5 ) = 80 Final answer : 54 + 60 + 63 + 70 + 72 + 80 = 399
def func(arr, key): low = 0 high = len(arr) mid = -1 flag = False while low < high: mid = (low + high) // 2 if arr[mid] == key: flag = True break elif arr[mid] < key: low = mid + 1 else: high = mid if flag: while mid < len(arr) and arr[mid] == key: mid = mid + 1 mid = mid - 1 return mid elif low >= len(arr): return len(arr) - 1 elif low - 1 > len(arr) and arr[low - 1] > key: return -1 else: return low - 1 T = int(input()) for i in range(T): mod = 1000000007 p, q, r = map(int, input().split()) parr = list(map(int, input().split())) qarr = list(map(int, input().split())) rarr = list(map(int, input().split())) parr.sort() rarr.sort() dicty = {} cumP = [0] * p cumR = [0] * r cumP[0] = parr[0] cumR[0] = rarr[0] maxp = parr[len(parr) - 1] maxr = rarr[len(rarr) - 1] for i in range(1, p): cumP[i] = cumP[i - 1] + parr[i] for i in range(1, r): cumR[i] = cumR[i - 1] + rarr[i] for j in range(len(qarr)): if qarr[j] in dicty: dicty[qarr[j]] = dicty[qarr[j]] + 1 else: dicty[qarr[j]] = 1 final = 0 cy = 0 temp = False for key in dicty: if key < cy and temp: continue if key >= maxp: nx = len(parr) else: nx = func(parr, key) + 1 if key >= maxr: nz = len(rarr) else: nz = func(rarr, key) + 1 if nx == 0 or nz == 0: cy = key temp = True continue sx = cumP[nx - 1] sz = cumR[nz - 1] con = sx * sz cp = nx * sz + nz * sx cp2 = nx * nz cp2 = cp2 * key**2 cp = cp * key cp2 = cp2 % mod cp = cp % mod con = con % mod ans = (cp2 + cp + con) % mod final = (final + ans * dicty[key]) % mod print(final)
FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR VAR ASSIGN VAR NUMBER IF VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR IF VAR WHILE VAR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER RETURN VAR IF VAR FUNC_CALL VAR VAR RETURN BIN_OP FUNC_CALL VAR VAR NUMBER IF BIN_OP VAR NUMBER FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR RETURN NUMBER RETURN BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR DICT ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF VAR VAR VAR IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER IF VAR NUMBER VAR NUMBER ASSIGN VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR
Given a triplet of integers (X , Y , Z), such that X ≀ Y and Y β‰₯ Z, we define f(X , Y , Z) to be (X + Y) * (Y + Z). If either X > Y or Y < Z, or both, then f(X , Y , Z) is defined to be 0. You are provided three arrays A , B and C of any length (their lengths may or may not be equal). Your task is to find the sum of f(X , Y , Z) over all triplets (X, Y , Z) where X, Y and Z belong to A, B and C respectively. Output your sum for each test case modulo 1000000007. -----Input----- - The first line contains a single integer, T, which is the number of test cases. The description of each testcase follows: - The first line of each testcase contains 3 integers: p, q and r. These denote the lengths of A,B and C respectively. - The second line contains p integers, which are the elements of A - The third line contains q integers, which are the elements of B - The fourth line contains r integers, which are the elements of C -----Output----- Output the required sum modulo 1000000007 for each test case in a new line. -----Constraints----- - 1 ≀ T ≀ 10 - 1 ≀ p, q, r ≀ 100000 - 1 ≀ every array element ≀ 1000000000 -----Subtasks ----- - Subtask #1 (30 points): 1 ≀ p,q,r ≀ 100 - Subtask #2 (70 points): 1 ≀ p,q,r ≀ 100000 -----Example : ----- Input: 1 3 1 3 1 2 3 5 4 5 6 Output: 399 -----Explanation: ----- As there is only one choice for Y which equals to 5, to get a non-zero function value,we can choose any element for X from the set { 1 , 2 , 3 } and for Z from the set { 4 , 5 } So triplets which give non-zero function values are: { 1 , 5 , 4 } : ( 1 + 5 ) * ( 5 + 4 ) = 54 { 1 , 5 , 5 } : ( 1 + 5 ) * ( 5 + 5 ) = 60 { 2 , 5 , 4 } : ( 2 + 5 ) * ( 5 + 4 ) = 63 { 2 , 5 , 5 } : ( 2 + 5 ) * ( 5 + 5 ) = 70 { 3 , 5 , 4 } : ( 3 + 5 ) * ( 5 + 4 ) = 72 { 3 , 5 , 5 } : ( 3 + 5 ) * ( 5 + 5 ) = 80 Final answer : 54 + 60 + 63 + 70 + 72 + 80 = 399
def findind(sizea, mini, lista): lo = 0 hi = sizea mid = (lo + hi) // 2 while lo < hi: mid = (lo + hi) // 2 if lista[mid] > mini: hi = mid else: lo = mid + 1 return lo def findind1(sizea, mini, lista): lo = 0 hi = sizea mid = (lo + hi) // 2 while lo < hi: mid = (lo + hi) // 2 if lista[mid] >= mini: hi = mid else: lo = mid + 1 return lo test = int(input()) for codechef in range(test): blah = [int(x) for x in input().split()] a = [int(x) for x in input().split()] b = [int(x) for x in input().split()] c = [int(x) for x in input().split()] a = sorted(a) b = sorted(b) c = sorted(c) sizea = blah[0] sizeb = blah[1] sizec = blah[2] j = b[-1] pos1 = findind(sizea, j, a) pos2 = findind(sizec, j, c) a = a[:pos1] c = c[:pos2] sizea = len(a) sizeb = len(b) sizec = len(c) if sizea == 0 or sizec == 0: print("0") continue mini1 = a[0] mini2 = c[0] if mini1 > mini2: posw = findind1(sizeb, mini1, b) else: posw = findind1(sizeb, mini2, b) b = b[posw:] sizea = len(a) sizeb = len(b) sizec = len(c) ans = 0 prepos1 = -1 prepos2 = -1 sumarraya = [] tem = 0 for i in a: tem += i sumarraya.append(tem) tem = 0 sumarrayc = [] for i in c: tem += i sumarrayc.append(tem) jp = 0 ansprev = 0 prepos1 = 0 prepos2 = 0 flaga, flagc = 0, 0 for j in b: if j == jp: ans += ansprev continue if pos1 <= sizea - 1: if a[pos1] <= j: pos1 = findind(sizea, j, a) if flaga == 0 and pos1 >= sizea: pos1 = findind(sizea, j, a) flaga = 1 if pos2 <= sizec - 1: if c[pos2] <= j: pos2 = findind(sizec, j, c) if flagc == 0 and pos2 >= sizec: pos2 = findind(sizec, j, c) flagc = 1 if pos2 == 0 or pos1 == 0: jp = j continue ansprev = ( j * j * pos1 * pos2 % 1000000007 + j * (sumarraya[pos1 - 1] * pos2 + sumarrayc[pos2 - 1] * pos1) % 1000000007 + sumarraya[pos1 - 1] * sumarrayc[pos2 - 1] % 1000000007 ) ans += ansprev jp = j print(ans % 1000000007)
FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER RETURN VAR FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER RETURN VAR 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 ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR STRING ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR LIST FOR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER NUMBER FOR VAR VAR IF VAR VAR VAR VAR IF VAR BIN_OP VAR NUMBER IF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR IF VAR NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR NUMBER IF VAR BIN_OP VAR NUMBER IF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR IF VAR NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR NUMBER IF VAR NUMBER VAR NUMBER ASSIGN VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR NUMBER BIN_OP BIN_OP VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR BIN_OP VAR NUMBER VAR NUMBER BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER
Given a triplet of integers (X , Y , Z), such that X ≀ Y and Y β‰₯ Z, we define f(X , Y , Z) to be (X + Y) * (Y + Z). If either X > Y or Y < Z, or both, then f(X , Y , Z) is defined to be 0. You are provided three arrays A , B and C of any length (their lengths may or may not be equal). Your task is to find the sum of f(X , Y , Z) over all triplets (X, Y , Z) where X, Y and Z belong to A, B and C respectively. Output your sum for each test case modulo 1000000007. -----Input----- - The first line contains a single integer, T, which is the number of test cases. The description of each testcase follows: - The first line of each testcase contains 3 integers: p, q and r. These denote the lengths of A,B and C respectively. - The second line contains p integers, which are the elements of A - The third line contains q integers, which are the elements of B - The fourth line contains r integers, which are the elements of C -----Output----- Output the required sum modulo 1000000007 for each test case in a new line. -----Constraints----- - 1 ≀ T ≀ 10 - 1 ≀ p, q, r ≀ 100000 - 1 ≀ every array element ≀ 1000000000 -----Subtasks ----- - Subtask #1 (30 points): 1 ≀ p,q,r ≀ 100 - Subtask #2 (70 points): 1 ≀ p,q,r ≀ 100000 -----Example : ----- Input: 1 3 1 3 1 2 3 5 4 5 6 Output: 399 -----Explanation: ----- As there is only one choice for Y which equals to 5, to get a non-zero function value,we can choose any element for X from the set { 1 , 2 , 3 } and for Z from the set { 4 , 5 } So triplets which give non-zero function values are: { 1 , 5 , 4 } : ( 1 + 5 ) * ( 5 + 4 ) = 54 { 1 , 5 , 5 } : ( 1 + 5 ) * ( 5 + 5 ) = 60 { 2 , 5 , 4 } : ( 2 + 5 ) * ( 5 + 4 ) = 63 { 2 , 5 , 5 } : ( 2 + 5 ) * ( 5 + 5 ) = 70 { 3 , 5 , 4 } : ( 3 + 5 ) * ( 5 + 4 ) = 72 { 3 , 5 , 5 } : ( 3 + 5 ) * ( 5 + 5 ) = 80 Final answer : 54 + 60 + 63 + 70 + 72 + 80 = 399
for ti in range(int(input())): pn, qn, rn = list(map(int, input().split())) p = list(map(int, input().split())) q = list(map(int, input().split())) r = list(map(int, input().split())) p.sort() q.sort() r.sort() x = y = z = 0 psum = rsum = 0 tsum = 0 for y in range(0, qn): while x < pn and p[x] <= q[y]: psum = psum + p[x] x = x + 1 while z < rn and r[z] <= q[y]: rsum = rsum + r[z] z = z + 1 if x > 0 and z > 0: tsum = (tsum + (psum + q[y] * x) * (q[y] * z + rsum)) % 1000000007 print(tsum)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR WHILE VAR VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER IF VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP BIN_OP VAR BIN_OP VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
Given a triplet of integers (X , Y , Z), such that X ≀ Y and Y β‰₯ Z, we define f(X , Y , Z) to be (X + Y) * (Y + Z). If either X > Y or Y < Z, or both, then f(X , Y , Z) is defined to be 0. You are provided three arrays A , B and C of any length (their lengths may or may not be equal). Your task is to find the sum of f(X , Y , Z) over all triplets (X, Y , Z) where X, Y and Z belong to A, B and C respectively. Output your sum for each test case modulo 1000000007. -----Input----- - The first line contains a single integer, T, which is the number of test cases. The description of each testcase follows: - The first line of each testcase contains 3 integers: p, q and r. These denote the lengths of A,B and C respectively. - The second line contains p integers, which are the elements of A - The third line contains q integers, which are the elements of B - The fourth line contains r integers, which are the elements of C -----Output----- Output the required sum modulo 1000000007 for each test case in a new line. -----Constraints----- - 1 ≀ T ≀ 10 - 1 ≀ p, q, r ≀ 100000 - 1 ≀ every array element ≀ 1000000000 -----Subtasks ----- - Subtask #1 (30 points): 1 ≀ p,q,r ≀ 100 - Subtask #2 (70 points): 1 ≀ p,q,r ≀ 100000 -----Example : ----- Input: 1 3 1 3 1 2 3 5 4 5 6 Output: 399 -----Explanation: ----- As there is only one choice for Y which equals to 5, to get a non-zero function value,we can choose any element for X from the set { 1 , 2 , 3 } and for Z from the set { 4 , 5 } So triplets which give non-zero function values are: { 1 , 5 , 4 } : ( 1 + 5 ) * ( 5 + 4 ) = 54 { 1 , 5 , 5 } : ( 1 + 5 ) * ( 5 + 5 ) = 60 { 2 , 5 , 4 } : ( 2 + 5 ) * ( 5 + 4 ) = 63 { 2 , 5 , 5 } : ( 2 + 5 ) * ( 5 + 5 ) = 70 { 3 , 5 , 4 } : ( 3 + 5 ) * ( 5 + 4 ) = 72 { 3 , 5 , 5 } : ( 3 + 5 ) * ( 5 + 5 ) = 80 Final answer : 54 + 60 + 63 + 70 + 72 + 80 = 399
from itertools import groupby t = int(input()) for test in range(t): p, q, r = map(int, input().split()) A = list(map(int, input().split())) A.sort() tmpB = list(map(int, input().split())) tmpB.sort() C = list(map(int, input().split())) C.sort() B = [] freqB = [] for key, group in groupby(tmpB): B.append(key) freqB.append(len(list(group))) q = len(B) sumA = [0] sumC = [0] ans = 0 index = 0 for j in A: sumA.append((j + sumA[index]) % 1000000007) index += 1 index = 0 for j in C: sumC.append((j + sumC[index]) % 1000000007) index += 1 someMax = max(A[-1], C[-1]) for i in range(q): query = B[i] if query >= someMax: while i < q: ans = ( ( ans + (sumA[p] + B[i] * p % 1000000007) % 1000000007 * ((sumC[r] + B[i] * r % 1000000007) % 1000000007) % 1000000007 * freqB[i] ) % 1000000007 % 1000000007 ) i += 1 break if query >= A[-1]: tmpA = (sumA[p] + query * p % 1000000007) % 1000000007 else: low = 0 high = p - 1 while low <= high: mid = low + high >> 1 if A[mid] <= query: low = mid + 1 else: high = mid - 1 if not low: continue tmpA = (sumA[low] + query * low % 1000000007) % 1000000007 if query >= C[-1]: tmpC = (sumC[r] + query * r % 1000000007) % 1000000007 else: low = 0 high = r - 1 while low <= high: mid = low + high >> 1 if C[mid] <= query: low = mid + 1 else: high = mid - 1 if not low: continue tmpC = (sumC[low] + query * low % 1000000007) % 1000000007 ans = (ans + tmpA * tmpC % 1000000007 * freqB[i] % 1000000007) % 1000000007 print(ans)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR LIST NUMBER ASSIGN VAR LIST NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR IF VAR VAR WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR BIN_OP BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR BIN_OP BIN_OP VAR VAR VAR NUMBER NUMBER BIN_OP BIN_OP VAR VAR BIN_OP BIN_OP VAR VAR VAR NUMBER NUMBER NUMBER VAR VAR NUMBER NUMBER VAR NUMBER IF VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR BIN_OP BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR BIN_OP BIN_OP VAR VAR NUMBER NUMBER IF VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR BIN_OP BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR BIN_OP BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR NUMBER VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR
Given a triplet of integers (X , Y , Z), such that X ≀ Y and Y β‰₯ Z, we define f(X , Y , Z) to be (X + Y) * (Y + Z). If either X > Y or Y < Z, or both, then f(X , Y , Z) is defined to be 0. You are provided three arrays A , B and C of any length (their lengths may or may not be equal). Your task is to find the sum of f(X , Y , Z) over all triplets (X, Y , Z) where X, Y and Z belong to A, B and C respectively. Output your sum for each test case modulo 1000000007. -----Input----- - The first line contains a single integer, T, which is the number of test cases. The description of each testcase follows: - The first line of each testcase contains 3 integers: p, q and r. These denote the lengths of A,B and C respectively. - The second line contains p integers, which are the elements of A - The third line contains q integers, which are the elements of B - The fourth line contains r integers, which are the elements of C -----Output----- Output the required sum modulo 1000000007 for each test case in a new line. -----Constraints----- - 1 ≀ T ≀ 10 - 1 ≀ p, q, r ≀ 100000 - 1 ≀ every array element ≀ 1000000000 -----Subtasks ----- - Subtask #1 (30 points): 1 ≀ p,q,r ≀ 100 - Subtask #2 (70 points): 1 ≀ p,q,r ≀ 100000 -----Example : ----- Input: 1 3 1 3 1 2 3 5 4 5 6 Output: 399 -----Explanation: ----- As there is only one choice for Y which equals to 5, to get a non-zero function value,we can choose any element for X from the set { 1 , 2 , 3 } and for Z from the set { 4 , 5 } So triplets which give non-zero function values are: { 1 , 5 , 4 } : ( 1 + 5 ) * ( 5 + 4 ) = 54 { 1 , 5 , 5 } : ( 1 + 5 ) * ( 5 + 5 ) = 60 { 2 , 5 , 4 } : ( 2 + 5 ) * ( 5 + 4 ) = 63 { 2 , 5 , 5 } : ( 2 + 5 ) * ( 5 + 5 ) = 70 { 3 , 5 , 4 } : ( 3 + 5 ) * ( 5 + 4 ) = 72 { 3 , 5 , 5 } : ( 3 + 5 ) * ( 5 + 5 ) = 80 Final answer : 54 + 60 + 63 + 70 + 72 + 80 = 399
T = eval(input()) while T > 0: inp = input() a = inp.split() Alen = eval(a[0]) Blen = eval(a[1]) Clen = eval(a[2]) A = list(map(int, input().split())) A.sort() B = list(map(int, input().split())) B.sort() C = list(map(int, input().split())) C.sort() y = 0 sumfinal = 0 Astart = 0 Cstart = 0 sumA = 0 sumC = 0 for i in range(0, Blen): sumy = 0 y = B[i] j = Astart while j < Alen and A[j] <= y: x = A[j] sumA = sumA + x j = j + 1 Astart = j k = Cstart while k < Clen and C[k] <= y: z = C[k] sumC = sumC + z k = k + 1 Cstart = k sumy = (sumA + j * y) * (sumC + k * y) sumfinal = sumfinal + sumy print(sumfinal % 1000000007) T = T - 1
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR WHILE VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR WHILE VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP VAR VAR BIN_OP VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER
Given a triplet of integers (X , Y , Z), such that X ≀ Y and Y β‰₯ Z, we define f(X , Y , Z) to be (X + Y) * (Y + Z). If either X > Y or Y < Z, or both, then f(X , Y , Z) is defined to be 0. You are provided three arrays A , B and C of any length (their lengths may or may not be equal). Your task is to find the sum of f(X , Y , Z) over all triplets (X, Y , Z) where X, Y and Z belong to A, B and C respectively. Output your sum for each test case modulo 1000000007. -----Input----- - The first line contains a single integer, T, which is the number of test cases. The description of each testcase follows: - The first line of each testcase contains 3 integers: p, q and r. These denote the lengths of A,B and C respectively. - The second line contains p integers, which are the elements of A - The third line contains q integers, which are the elements of B - The fourth line contains r integers, which are the elements of C -----Output----- Output the required sum modulo 1000000007 for each test case in a new line. -----Constraints----- - 1 ≀ T ≀ 10 - 1 ≀ p, q, r ≀ 100000 - 1 ≀ every array element ≀ 1000000000 -----Subtasks ----- - Subtask #1 (30 points): 1 ≀ p,q,r ≀ 100 - Subtask #2 (70 points): 1 ≀ p,q,r ≀ 100000 -----Example : ----- Input: 1 3 1 3 1 2 3 5 4 5 6 Output: 399 -----Explanation: ----- As there is only one choice for Y which equals to 5, to get a non-zero function value,we can choose any element for X from the set { 1 , 2 , 3 } and for Z from the set { 4 , 5 } So triplets which give non-zero function values are: { 1 , 5 , 4 } : ( 1 + 5 ) * ( 5 + 4 ) = 54 { 1 , 5 , 5 } : ( 1 + 5 ) * ( 5 + 5 ) = 60 { 2 , 5 , 4 } : ( 2 + 5 ) * ( 5 + 4 ) = 63 { 2 , 5 , 5 } : ( 2 + 5 ) * ( 5 + 5 ) = 70 { 3 , 5 , 4 } : ( 3 + 5 ) * ( 5 + 4 ) = 72 { 3 , 5 , 5 } : ( 3 + 5 ) * ( 5 + 5 ) = 80 Final answer : 54 + 60 + 63 + 70 + 72 + 80 = 399
t = int(input()) while t > 0: p_a = [] q_a = [] r_a = [] p, q, r = input().split() p, q, r = int(p), int(q), int(r) p_a = list(map(int, input().split())) q_a = list(map(int, input().split())) r_a = list(map(int, input().split())) p_a.sort() q_a.sort() r_a.sort() tsum = 0 sumx = 0 sumz = 0 x = 0 z = 0 for i in range(0, q): while x < p and p_a[x] <= q_a[i]: sumx = sumx + p_a[x] x = x + 1 while z < r and r_a[z] <= q_a[i]: sumz = sumz + r_a[z] z = z + 1 val = sumx * sumz + q_a[i] * q_a[i] * x * z + (sumx * z + sumz * x) * q_a[i] tsum = tsum + val tsum = tsum % 1000000007 print(tsum) t = t - 1
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR NUMBER ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR WHILE VAR VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER
Given a triplet of integers (X , Y , Z), such that X ≀ Y and Y β‰₯ Z, we define f(X , Y , Z) to be (X + Y) * (Y + Z). If either X > Y or Y < Z, or both, then f(X , Y , Z) is defined to be 0. You are provided three arrays A , B and C of any length (their lengths may or may not be equal). Your task is to find the sum of f(X , Y , Z) over all triplets (X, Y , Z) where X, Y and Z belong to A, B and C respectively. Output your sum for each test case modulo 1000000007. -----Input----- - The first line contains a single integer, T, which is the number of test cases. The description of each testcase follows: - The first line of each testcase contains 3 integers: p, q and r. These denote the lengths of A,B and C respectively. - The second line contains p integers, which are the elements of A - The third line contains q integers, which are the elements of B - The fourth line contains r integers, which are the elements of C -----Output----- Output the required sum modulo 1000000007 for each test case in a new line. -----Constraints----- - 1 ≀ T ≀ 10 - 1 ≀ p, q, r ≀ 100000 - 1 ≀ every array element ≀ 1000000000 -----Subtasks ----- - Subtask #1 (30 points): 1 ≀ p,q,r ≀ 100 - Subtask #2 (70 points): 1 ≀ p,q,r ≀ 100000 -----Example : ----- Input: 1 3 1 3 1 2 3 5 4 5 6 Output: 399 -----Explanation: ----- As there is only one choice for Y which equals to 5, to get a non-zero function value,we can choose any element for X from the set { 1 , 2 , 3 } and for Z from the set { 4 , 5 } So triplets which give non-zero function values are: { 1 , 5 , 4 } : ( 1 + 5 ) * ( 5 + 4 ) = 54 { 1 , 5 , 5 } : ( 1 + 5 ) * ( 5 + 5 ) = 60 { 2 , 5 , 4 } : ( 2 + 5 ) * ( 5 + 4 ) = 63 { 2 , 5 , 5 } : ( 2 + 5 ) * ( 5 + 5 ) = 70 { 3 , 5 , 4 } : ( 3 + 5 ) * ( 5 + 4 ) = 72 { 3 , 5 , 5 } : ( 3 + 5 ) * ( 5 + 5 ) = 80 Final answer : 54 + 60 + 63 + 70 + 72 + 80 = 399
MODVAL = 10**9 + 7 for t in range(int(input())): p, q, r = map(int, input().split()) A = list(map(int, input().split())) B = list(map(int, input().split())) C = list(map(int, input().split())) A.sort() B.sort() C.sort() ans = i = j = Xsum = Zsum = XZsum = 0 for Y in B: while i < p and A[i] <= Y: XZsum += A[i] * Zsum Xsum += A[i] i += 1 while j < r and C[j] <= Y: XZsum += C[j] * Xsum Zsum += C[j] j += 1 ans = (ans + i * j * Y * Y + j * Xsum * Y + i * Zsum * Y + XZsum) % MODVAL print(ans)
ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR VAR VAR VAR VAR VAR NUMBER FOR VAR VAR WHILE VAR VAR VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR VAR VAR VAR NUMBER WHILE VAR VAR VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP BIN_OP BIN_OP VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR
Given a triplet of integers (X , Y , Z), such that X ≀ Y and Y β‰₯ Z, we define f(X , Y , Z) to be (X + Y) * (Y + Z). If either X > Y or Y < Z, or both, then f(X , Y , Z) is defined to be 0. You are provided three arrays A , B and C of any length (their lengths may or may not be equal). Your task is to find the sum of f(X , Y , Z) over all triplets (X, Y , Z) where X, Y and Z belong to A, B and C respectively. Output your sum for each test case modulo 1000000007. -----Input----- - The first line contains a single integer, T, which is the number of test cases. The description of each testcase follows: - The first line of each testcase contains 3 integers: p, q and r. These denote the lengths of A,B and C respectively. - The second line contains p integers, which are the elements of A - The third line contains q integers, which are the elements of B - The fourth line contains r integers, which are the elements of C -----Output----- Output the required sum modulo 1000000007 for each test case in a new line. -----Constraints----- - 1 ≀ T ≀ 10 - 1 ≀ p, q, r ≀ 100000 - 1 ≀ every array element ≀ 1000000000 -----Subtasks ----- - Subtask #1 (30 points): 1 ≀ p,q,r ≀ 100 - Subtask #2 (70 points): 1 ≀ p,q,r ≀ 100000 -----Example : ----- Input: 1 3 1 3 1 2 3 5 4 5 6 Output: 399 -----Explanation: ----- As there is only one choice for Y which equals to 5, to get a non-zero function value,we can choose any element for X from the set { 1 , 2 , 3 } and for Z from the set { 4 , 5 } So triplets which give non-zero function values are: { 1 , 5 , 4 } : ( 1 + 5 ) * ( 5 + 4 ) = 54 { 1 , 5 , 5 } : ( 1 + 5 ) * ( 5 + 5 ) = 60 { 2 , 5 , 4 } : ( 2 + 5 ) * ( 5 + 4 ) = 63 { 2 , 5 , 5 } : ( 2 + 5 ) * ( 5 + 5 ) = 70 { 3 , 5 , 4 } : ( 3 + 5 ) * ( 5 + 4 ) = 72 { 3 , 5 , 5 } : ( 3 + 5 ) * ( 5 + 5 ) = 80 Final answer : 54 + 60 + 63 + 70 + 72 + 80 = 399
m = 1000000007 def value(a, b, c): return (a + b) * (b + c) % m t = int(input()) while t: t -= 1 sum = 0 p, q, r = map(int, input().split()) a = [int(x) for x in input().split()] b = [int(x) for x in input().split()] c = [int(x) for x in input().split()] for i in b: a1 = [j for j in a if j <= i] c1 = [j for j in c if j <= i] for j in a1: for k in c1: sum = sum % m + value(j, i, k) print(sum % m)
ASSIGN VAR NUMBER FUNC_DEF RETURN BIN_OP BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR FOR VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR FOR VAR VAR FOR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR
Given a triplet of integers (X , Y , Z), such that X ≀ Y and Y β‰₯ Z, we define f(X , Y , Z) to be (X + Y) * (Y + Z). If either X > Y or Y < Z, or both, then f(X , Y , Z) is defined to be 0. You are provided three arrays A , B and C of any length (their lengths may or may not be equal). Your task is to find the sum of f(X , Y , Z) over all triplets (X, Y , Z) where X, Y and Z belong to A, B and C respectively. Output your sum for each test case modulo 1000000007. -----Input----- - The first line contains a single integer, T, which is the number of test cases. The description of each testcase follows: - The first line of each testcase contains 3 integers: p, q and r. These denote the lengths of A,B and C respectively. - The second line contains p integers, which are the elements of A - The third line contains q integers, which are the elements of B - The fourth line contains r integers, which are the elements of C -----Output----- Output the required sum modulo 1000000007 for each test case in a new line. -----Constraints----- - 1 ≀ T ≀ 10 - 1 ≀ p, q, r ≀ 100000 - 1 ≀ every array element ≀ 1000000000 -----Subtasks ----- - Subtask #1 (30 points): 1 ≀ p,q,r ≀ 100 - Subtask #2 (70 points): 1 ≀ p,q,r ≀ 100000 -----Example : ----- Input: 1 3 1 3 1 2 3 5 4 5 6 Output: 399 -----Explanation: ----- As there is only one choice for Y which equals to 5, to get a non-zero function value,we can choose any element for X from the set { 1 , 2 , 3 } and for Z from the set { 4 , 5 } So triplets which give non-zero function values are: { 1 , 5 , 4 } : ( 1 + 5 ) * ( 5 + 4 ) = 54 { 1 , 5 , 5 } : ( 1 + 5 ) * ( 5 + 5 ) = 60 { 2 , 5 , 4 } : ( 2 + 5 ) * ( 5 + 4 ) = 63 { 2 , 5 , 5 } : ( 2 + 5 ) * ( 5 + 5 ) = 70 { 3 , 5 , 4 } : ( 3 + 5 ) * ( 5 + 4 ) = 72 { 3 , 5 , 5 } : ( 3 + 5 ) * ( 5 + 5 ) = 80 Final answer : 54 + 60 + 63 + 70 + 72 + 80 = 399
T = int(input()) while T > 0: T -= 1 [p, q, r] = map(int, input().split()) A = list(map(int, input().split())) B = list(map(int, input().split())) C = list(map(int, input().split())) A.sort() B.sort() C.sort() A_pos = 0 A_sum = 0 C_pos = 0 C_sum = 0 S = 0 for i in range(q): for j in range(A_pos, p): if A[j] > B[i]: break A_sum += A[j] A_pos += 1 for k in range(C_pos, r): if C[k] > B[i]: break C_sum += C[k] C_pos += 1 S = (S + (A_pos * B[i] + A_sum) * (C_pos * B[i] + C_sum)) % 1000000007 print(S)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR NUMBER VAR NUMBER ASSIGN LIST VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR VAR IF VAR VAR VAR VAR VAR VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR IF VAR VAR VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
Given a triplet of integers (X , Y , Z), such that X ≀ Y and Y β‰₯ Z, we define f(X , Y , Z) to be (X + Y) * (Y + Z). If either X > Y or Y < Z, or both, then f(X , Y , Z) is defined to be 0. You are provided three arrays A , B and C of any length (their lengths may or may not be equal). Your task is to find the sum of f(X , Y , Z) over all triplets (X, Y , Z) where X, Y and Z belong to A, B and C respectively. Output your sum for each test case modulo 1000000007. -----Input----- - The first line contains a single integer, T, which is the number of test cases. The description of each testcase follows: - The first line of each testcase contains 3 integers: p, q and r. These denote the lengths of A,B and C respectively. - The second line contains p integers, which are the elements of A - The third line contains q integers, which are the elements of B - The fourth line contains r integers, which are the elements of C -----Output----- Output the required sum modulo 1000000007 for each test case in a new line. -----Constraints----- - 1 ≀ T ≀ 10 - 1 ≀ p, q, r ≀ 100000 - 1 ≀ every array element ≀ 1000000000 -----Subtasks ----- - Subtask #1 (30 points): 1 ≀ p,q,r ≀ 100 - Subtask #2 (70 points): 1 ≀ p,q,r ≀ 100000 -----Example : ----- Input: 1 3 1 3 1 2 3 5 4 5 6 Output: 399 -----Explanation: ----- As there is only one choice for Y which equals to 5, to get a non-zero function value,we can choose any element for X from the set { 1 , 2 , 3 } and for Z from the set { 4 , 5 } So triplets which give non-zero function values are: { 1 , 5 , 4 } : ( 1 + 5 ) * ( 5 + 4 ) = 54 { 1 , 5 , 5 } : ( 1 + 5 ) * ( 5 + 5 ) = 60 { 2 , 5 , 4 } : ( 2 + 5 ) * ( 5 + 4 ) = 63 { 2 , 5 , 5 } : ( 2 + 5 ) * ( 5 + 5 ) = 70 { 3 , 5 , 4 } : ( 3 + 5 ) * ( 5 + 4 ) = 72 { 3 , 5 , 5 } : ( 3 + 5 ) * ( 5 + 5 ) = 80 Final answer : 54 + 60 + 63 + 70 + 72 + 80 = 399
t = int(input()) for i in range(t): sumf = 0 sumf1 = 0 p, q, r = map(int, input().split(" ")) a = list(map(int, input().split(" "))) b = list(map(int, input().split(" "))) c = list(map(int, input().split(" "))) a.sort() b.sort() c.sort() sum1 = 0 sum2 = 0 index1 = len(a) - 1 index2 = len(c) - 1 v = b[len(b) - 1] for g in range(len(a)): sum1 += a[g] if a[g] > v: sum1 -= a[g] index1 = g - 1 break for g in range(len(c)): sum2 += c[g] if c[g] > v: sum2 -= c[g] index2 = g - 1 break sumf += (sum1 + (index1 + 1) * v) * (sum2 + (index2 + 1) * v) if len(b) > 1: sum3 = 0 sum4 = 0 for h1 in range(len(b) - 2, -1, -1): flag1 = 0 flag2 = 0 for h in range(index1, -1, -1): sum3 += a[h] if a[h] <= b[h1]: sum3 -= a[h] flag1 = 1 index1 = h break if flag1 == 1: for h in range(index2, -1, -1): sum4 += c[h] if c[h] <= b[h1]: sum4 -= c[h] flag2 = 1 index2 = h break if flag1 == 1 and flag2 == 1: sumf1 = (sum1 - sum3 + (index1 + 1) * b[h1]) * ( sum2 - sum4 + (index2 + 1) * b[h1] ) else: sumf1 = 0 if sumf1 > 0: sumf += sumf1 elif sumf1 == 0: break print(sumf % 1000000007) else: print(sumf % 1000000007)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR STRING EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR IF VAR VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR IF VAR VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER VAR BIN_OP BIN_OP VAR BIN_OP BIN_OP VAR NUMBER VAR BIN_OP VAR BIN_OP BIN_OP VAR NUMBER VAR IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR NUMBER NUMBER VAR VAR VAR IF VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR IF VAR NUMBER FOR VAR FUNC_CALL VAR VAR NUMBER NUMBER VAR VAR VAR IF VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR IF VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR BIN_OP BIN_OP VAR NUMBER VAR VAR BIN_OP BIN_OP VAR VAR BIN_OP BIN_OP VAR NUMBER VAR VAR ASSIGN VAR NUMBER IF VAR NUMBER VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER
Given a triplet of integers (X , Y , Z), such that X ≀ Y and Y β‰₯ Z, we define f(X , Y , Z) to be (X + Y) * (Y + Z). If either X > Y or Y < Z, or both, then f(X , Y , Z) is defined to be 0. You are provided three arrays A , B and C of any length (their lengths may or may not be equal). Your task is to find the sum of f(X , Y , Z) over all triplets (X, Y , Z) where X, Y and Z belong to A, B and C respectively. Output your sum for each test case modulo 1000000007. -----Input----- - The first line contains a single integer, T, which is the number of test cases. The description of each testcase follows: - The first line of each testcase contains 3 integers: p, q and r. These denote the lengths of A,B and C respectively. - The second line contains p integers, which are the elements of A - The third line contains q integers, which are the elements of B - The fourth line contains r integers, which are the elements of C -----Output----- Output the required sum modulo 1000000007 for each test case in a new line. -----Constraints----- - 1 ≀ T ≀ 10 - 1 ≀ p, q, r ≀ 100000 - 1 ≀ every array element ≀ 1000000000 -----Subtasks ----- - Subtask #1 (30 points): 1 ≀ p,q,r ≀ 100 - Subtask #2 (70 points): 1 ≀ p,q,r ≀ 100000 -----Example : ----- Input: 1 3 1 3 1 2 3 5 4 5 6 Output: 399 -----Explanation: ----- As there is only one choice for Y which equals to 5, to get a non-zero function value,we can choose any element for X from the set { 1 , 2 , 3 } and for Z from the set { 4 , 5 } So triplets which give non-zero function values are: { 1 , 5 , 4 } : ( 1 + 5 ) * ( 5 + 4 ) = 54 { 1 , 5 , 5 } : ( 1 + 5 ) * ( 5 + 5 ) = 60 { 2 , 5 , 4 } : ( 2 + 5 ) * ( 5 + 4 ) = 63 { 2 , 5 , 5 } : ( 2 + 5 ) * ( 5 + 5 ) = 70 { 3 , 5 , 4 } : ( 3 + 5 ) * ( 5 + 4 ) = 72 { 3 , 5 , 5 } : ( 3 + 5 ) * ( 5 + 5 ) = 80 Final answer : 54 + 60 + 63 + 70 + 72 + 80 = 399
import itertools T = int(input()) for case in range(T): p, q, r = map(int, input().split()) xs = list(map(int, input().split())) ys = list(map(int, input().split())) zs = list(map(int, input().split())) xs.sort(), ys.sort(), zs.sort() sumx, sumz, ix, iz = 0, 0, 0, 0 ans = 0 for iy, y in enumerate(ys): while ix < p and xs[ix] <= y: sumx = sumx + xs[ix] ix = ix + 1 while iz < r and zs[iz] <= y: sumz = sumz + zs[iz] iz = iz + 1 ans = ans + y * y * ix * iz + y * (sumx * iz + sumz * ix) + sumx * sumz ans = ans % 1000000007 print(ans)
IMPORT ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR VAR VAR NUMBER NUMBER NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR WHILE VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR BIN_OP VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR
Given a triplet of integers (X , Y , Z), such that X ≀ Y and Y β‰₯ Z, we define f(X , Y , Z) to be (X + Y) * (Y + Z). If either X > Y or Y < Z, or both, then f(X , Y , Z) is defined to be 0. You are provided three arrays A , B and C of any length (their lengths may or may not be equal). Your task is to find the sum of f(X , Y , Z) over all triplets (X, Y , Z) where X, Y and Z belong to A, B and C respectively. Output your sum for each test case modulo 1000000007. -----Input----- - The first line contains a single integer, T, which is the number of test cases. The description of each testcase follows: - The first line of each testcase contains 3 integers: p, q and r. These denote the lengths of A,B and C respectively. - The second line contains p integers, which are the elements of A - The third line contains q integers, which are the elements of B - The fourth line contains r integers, which are the elements of C -----Output----- Output the required sum modulo 1000000007 for each test case in a new line. -----Constraints----- - 1 ≀ T ≀ 10 - 1 ≀ p, q, r ≀ 100000 - 1 ≀ every array element ≀ 1000000000 -----Subtasks ----- - Subtask #1 (30 points): 1 ≀ p,q,r ≀ 100 - Subtask #2 (70 points): 1 ≀ p,q,r ≀ 100000 -----Example : ----- Input: 1 3 1 3 1 2 3 5 4 5 6 Output: 399 -----Explanation: ----- As there is only one choice for Y which equals to 5, to get a non-zero function value,we can choose any element for X from the set { 1 , 2 , 3 } and for Z from the set { 4 , 5 } So triplets which give non-zero function values are: { 1 , 5 , 4 } : ( 1 + 5 ) * ( 5 + 4 ) = 54 { 1 , 5 , 5 } : ( 1 + 5 ) * ( 5 + 5 ) = 60 { 2 , 5 , 4 } : ( 2 + 5 ) * ( 5 + 4 ) = 63 { 2 , 5 , 5 } : ( 2 + 5 ) * ( 5 + 5 ) = 70 { 3 , 5 , 4 } : ( 3 + 5 ) * ( 5 + 4 ) = 72 { 3 , 5 , 5 } : ( 3 + 5 ) * ( 5 + 5 ) = 80 Final answer : 54 + 60 + 63 + 70 + 72 + 80 = 399
mod = 1000000007 for i in range(int(input())): p, q, r = map(int, input().split()) A = sorted(list(map(int, input().split()))) B = sorted(list(map(int, input().split()))) C = sorted(list(map(int, input().split()))) ans = 0 k = 0 l = 0 s1 = 0 c1 = 0 s2 = 0 c2 = 0 for j in B: while k < p and A[k] <= j: s1 += A[k] c1 += 1 k += 1 while l < r and C[l] <= j: s2 += C[l] c2 += 1 l += 1 ans += s1 * s2 + c2 * s1 * j + c1 * s2 * j + c1 * c2 * j * j print(ans % mod)
ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR WHILE VAR VAR VAR VAR VAR VAR VAR VAR VAR NUMBER VAR NUMBER WHILE VAR VAR VAR VAR VAR VAR VAR VAR VAR NUMBER VAR NUMBER VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR BIN_OP BIN_OP VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR
Given a triplet of integers (X , Y , Z), such that X ≀ Y and Y β‰₯ Z, we define f(X , Y , Z) to be (X + Y) * (Y + Z). If either X > Y or Y < Z, or both, then f(X , Y , Z) is defined to be 0. You are provided three arrays A , B and C of any length (their lengths may or may not be equal). Your task is to find the sum of f(X , Y , Z) over all triplets (X, Y , Z) where X, Y and Z belong to A, B and C respectively. Output your sum for each test case modulo 1000000007. -----Input----- - The first line contains a single integer, T, which is the number of test cases. The description of each testcase follows: - The first line of each testcase contains 3 integers: p, q and r. These denote the lengths of A,B and C respectively. - The second line contains p integers, which are the elements of A - The third line contains q integers, which are the elements of B - The fourth line contains r integers, which are the elements of C -----Output----- Output the required sum modulo 1000000007 for each test case in a new line. -----Constraints----- - 1 ≀ T ≀ 10 - 1 ≀ p, q, r ≀ 100000 - 1 ≀ every array element ≀ 1000000000 -----Subtasks ----- - Subtask #1 (30 points): 1 ≀ p,q,r ≀ 100 - Subtask #2 (70 points): 1 ≀ p,q,r ≀ 100000 -----Example : ----- Input: 1 3 1 3 1 2 3 5 4 5 6 Output: 399 -----Explanation: ----- As there is only one choice for Y which equals to 5, to get a non-zero function value,we can choose any element for X from the set { 1 , 2 , 3 } and for Z from the set { 4 , 5 } So triplets which give non-zero function values are: { 1 , 5 , 4 } : ( 1 + 5 ) * ( 5 + 4 ) = 54 { 1 , 5 , 5 } : ( 1 + 5 ) * ( 5 + 5 ) = 60 { 2 , 5 , 4 } : ( 2 + 5 ) * ( 5 + 4 ) = 63 { 2 , 5 , 5 } : ( 2 + 5 ) * ( 5 + 5 ) = 70 { 3 , 5 , 4 } : ( 3 + 5 ) * ( 5 + 4 ) = 72 { 3 , 5 , 5 } : ( 3 + 5 ) * ( 5 + 5 ) = 80 Final answer : 54 + 60 + 63 + 70 + 72 + 80 = 399
T = int(input()) ans = [] mod = 10**9 + 7 for _ in range(T): p, q, r = [int(i) for i in input().split()] A = [int(i) for i in input().split()] B = [int(i) for i in input().split()] C = [int(i) for i in input().split()] A.sort() B.sort() C.sort() m = 0 n = 0 sum1 = 0 sum2 = 0 count = 0 for i in B: while m < p and A[m] <= i: sum1 += A[m] m += 1 while n < r and C[n] <= i: sum2 += C[n] n += 1 count += (sum1 + m * i) * (sum2 + n * i) ans.append(count % mod) for i in ans: print(i)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR WHILE VAR VAR VAR VAR VAR VAR VAR VAR VAR NUMBER WHILE VAR VAR VAR VAR VAR VAR VAR VAR VAR NUMBER VAR BIN_OP BIN_OP VAR BIN_OP VAR VAR BIN_OP VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR
Given a triplet of integers (X , Y , Z), such that X ≀ Y and Y β‰₯ Z, we define f(X , Y , Z) to be (X + Y) * (Y + Z). If either X > Y or Y < Z, or both, then f(X , Y , Z) is defined to be 0. You are provided three arrays A , B and C of any length (their lengths may or may not be equal). Your task is to find the sum of f(X , Y , Z) over all triplets (X, Y , Z) where X, Y and Z belong to A, B and C respectively. Output your sum for each test case modulo 1000000007. -----Input----- - The first line contains a single integer, T, which is the number of test cases. The description of each testcase follows: - The first line of each testcase contains 3 integers: p, q and r. These denote the lengths of A,B and C respectively. - The second line contains p integers, which are the elements of A - The third line contains q integers, which are the elements of B - The fourth line contains r integers, which are the elements of C -----Output----- Output the required sum modulo 1000000007 for each test case in a new line. -----Constraints----- - 1 ≀ T ≀ 10 - 1 ≀ p, q, r ≀ 100000 - 1 ≀ every array element ≀ 1000000000 -----Subtasks ----- - Subtask #1 (30 points): 1 ≀ p,q,r ≀ 100 - Subtask #2 (70 points): 1 ≀ p,q,r ≀ 100000 -----Example : ----- Input: 1 3 1 3 1 2 3 5 4 5 6 Output: 399 -----Explanation: ----- As there is only one choice for Y which equals to 5, to get a non-zero function value,we can choose any element for X from the set { 1 , 2 , 3 } and for Z from the set { 4 , 5 } So triplets which give non-zero function values are: { 1 , 5 , 4 } : ( 1 + 5 ) * ( 5 + 4 ) = 54 { 1 , 5 , 5 } : ( 1 + 5 ) * ( 5 + 5 ) = 60 { 2 , 5 , 4 } : ( 2 + 5 ) * ( 5 + 4 ) = 63 { 2 , 5 , 5 } : ( 2 + 5 ) * ( 5 + 5 ) = 70 { 3 , 5 , 4 } : ( 3 + 5 ) * ( 5 + 4 ) = 72 { 3 , 5 , 5 } : ( 3 + 5 ) * ( 5 + 5 ) = 80 Final answer : 54 + 60 + 63 + 70 + 72 + 80 = 399
__author__ = ["vjn4006(Vaibhav Nagda)"] def main(): t = int(input().strip()) for _ in range(t): l, m, n = map(int, input().strip().split(" ")) a = sorted(list(map(int, input().strip().split(" ")))) b = sorted(list(map(int, input().strip().split(" ")))) c = sorted(list(map(int, input().strip().split(" ")))) x = 0 y = 0 cnt_a = 0 cnt_b = 0 ans = 0 a_add = 0 c_add = 0 for i in b: for j in range(x, l): if i >= a[j]: cnt_a += 1 x += 1 a_add += a[j] else: break for j in range(y, n): if i >= c[j]: cnt_b += 1 y += 1 c_add += c[j] else: break ans += ( (a_add + cnt_a * i) % (10**9 + 7) * (c_add + cnt_b * i) % (10**9 + 7) % (10**9 + 7) ) print(ans % (10**9 + 7)) return main()
ASSIGN VAR LIST STRING FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR FOR VAR FUNC_CALL VAR VAR VAR IF VAR VAR VAR VAR NUMBER VAR NUMBER VAR VAR VAR FOR VAR FUNC_CALL VAR VAR VAR IF VAR VAR VAR VAR NUMBER VAR NUMBER VAR VAR VAR VAR BIN_OP BIN_OP BIN_OP BIN_OP BIN_OP VAR BIN_OP VAR VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER BIN_OP VAR BIN_OP VAR VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER BIN_OP BIN_OP NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER RETURN EXPR FUNC_CALL VAR
Given a triplet of integers (X , Y , Z), such that X ≀ Y and Y β‰₯ Z, we define f(X , Y , Z) to be (X + Y) * (Y + Z). If either X > Y or Y < Z, or both, then f(X , Y , Z) is defined to be 0. You are provided three arrays A , B and C of any length (their lengths may or may not be equal). Your task is to find the sum of f(X , Y , Z) over all triplets (X, Y , Z) where X, Y and Z belong to A, B and C respectively. Output your sum for each test case modulo 1000000007. -----Input----- - The first line contains a single integer, T, which is the number of test cases. The description of each testcase follows: - The first line of each testcase contains 3 integers: p, q and r. These denote the lengths of A,B and C respectively. - The second line contains p integers, which are the elements of A - The third line contains q integers, which are the elements of B - The fourth line contains r integers, which are the elements of C -----Output----- Output the required sum modulo 1000000007 for each test case in a new line. -----Constraints----- - 1 ≀ T ≀ 10 - 1 ≀ p, q, r ≀ 100000 - 1 ≀ every array element ≀ 1000000000 -----Subtasks ----- - Subtask #1 (30 points): 1 ≀ p,q,r ≀ 100 - Subtask #2 (70 points): 1 ≀ p,q,r ≀ 100000 -----Example : ----- Input: 1 3 1 3 1 2 3 5 4 5 6 Output: 399 -----Explanation: ----- As there is only one choice for Y which equals to 5, to get a non-zero function value,we can choose any element for X from the set { 1 , 2 , 3 } and for Z from the set { 4 , 5 } So triplets which give non-zero function values are: { 1 , 5 , 4 } : ( 1 + 5 ) * ( 5 + 4 ) = 54 { 1 , 5 , 5 } : ( 1 + 5 ) * ( 5 + 5 ) = 60 { 2 , 5 , 4 } : ( 2 + 5 ) * ( 5 + 4 ) = 63 { 2 , 5 , 5 } : ( 2 + 5 ) * ( 5 + 5 ) = 70 { 3 , 5 , 4 } : ( 3 + 5 ) * ( 5 + 4 ) = 72 { 3 , 5 , 5 } : ( 3 + 5 ) * ( 5 + 5 ) = 80 Final answer : 54 + 60 + 63 + 70 + 72 + 80 = 399
from sys import stdin, stdout t = stdin.readline() t = int(t) M = 1000000007 for i in range(t): length = [int(x) for x in stdin.readline().split()] a = [int(x) for x in stdin.readline().split()] b = [int(x) for x in stdin.readline().split()] c = [int(x) for x in stdin.readline().split()] a.sort() b.sort() c.sort() a.append(-1) c.append(-1) x = 0 y = 0 z = 0 s = 0 sum_x = 0 sum_z = 0 for y in range(0, length[1]): while x < length[0] and a[x] <= b[y]: sum_x += a[x] x = x + 1 while z < length[2] and c[z] <= b[y]: sum_z += c[z] z += 1 s = ( s + (sum_x % M + x % M * (b[y] % M) % M) * (sum_z % M + z % M * (b[y] % M) % M) % M ) print(s % M)
ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR NUMBER WHILE VAR VAR NUMBER VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR VAR NUMBER VAR VAR VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR VAR VAR BIN_OP BIN_OP VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR
Given n, how many structurally unique BST's (binary search trees) that store values 1 ...Β n? Example: Input: 3 Output: 5 Explanation: Given n = 3, there are a total of 5 unique BST's: 1 3 3 2 1 \ / / / \ \ 3 2 1 1 3 2 / / \ \ 2 1 2 3
class Solution: def numTrees(self, n): dp = [0] * (n + 1) dp[0] = dp[1] = 1 for i in range(2, n + 1): total = 0 for j in range(i): total += dp[j] * dp[i - j - 1] dp[i] = total return dp[n]
CLASS_DEF FUNC_DEF ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR VAR VAR RETURN VAR VAR
Given n, how many structurally unique BST's (binary search trees) that store values 1 ...Β n? Example: Input: 3 Output: 5 Explanation: Given n = 3, there are a total of 5 unique BST's: 1 3 3 2 1 \ / / / \ \ 3 2 1 1 3 2 / / \ \ 2 1 2 3
class Solution: def numTrees(self, n): if n < 1: return 1 tree_nums = [0] * (n + 1) tree_nums[0] = 1 tree_nums[1] = 1 for t in range(2, n + 1): for root in range(1, t + 1): lt_num = root - 1 rt_num = t - root tree_nums[t] += tree_nums[lt_num] * tree_nums[rt_num] return tree_nums[n]
CLASS_DEF FUNC_DEF IF VAR NUMBER RETURN NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER NUMBER ASSIGN VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR RETURN VAR VAR
Given n, how many structurally unique BST's (binary search trees) that store values 1 ...Β n? Example: Input: 3 Output: 5 Explanation: Given n = 3, there are a total of 5 unique BST's: 1 3 3 2 1 \ / / / \ \ 3 2 1 1 3 2 / / \ \ 2 1 2 3
class Solution: def numTrees(self, n): dp = [1, 1, 2] if n < 3: return dp[n] for i in range(3, n + 1): dp.append(sum(dp[j - 1] * dp[i - j] for j in range(1, i + 1))) return dp[n]
CLASS_DEF FUNC_DEF ASSIGN VAR LIST NUMBER NUMBER NUMBER IF VAR NUMBER RETURN VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR VAR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER RETURN VAR VAR