From c249225e06ca72356a9ce66bc2c4da6408cca2e4 Mon Sep 17 00:00:00 2001 From: yenru0 Date: Tue, 4 Jun 2024 15:59:08 +0900 Subject: [PATCH] complete 2143.py --- zeta_python/completed/2143.py | 54 +++++++++++++++++++++++++++++++++++ 1 file changed, 54 insertions(+) create mode 100644 zeta_python/completed/2143.py diff --git a/zeta_python/completed/2143.py b/zeta_python/completed/2143.py new file mode 100644 index 0000000..4760a43 --- /dev/null +++ b/zeta_python/completed/2143.py @@ -0,0 +1,54 @@ +import sys + +input = sys.stdin.readline + +import bisect + + +def cum_sum(S): + s = [0] + + for i in range(len(S)): + s.append(S[i] + s[-1]) + + return s + + +def interval_sum(cum, a, b): # b > a + return cum[b] - cum[a - 1] + + +def all_interval_sum(cum) -> list[int]: + ret = [] # none + start = 1 + while start < len(cum): + end = start + while end < len(cum): + ret.append(interval_sum(cum, start, end)) + end += 1 + start += 1 + return ret + + +def solve(T, A, B): + cum_A = cum_sum(A) + cum_B = cum_sum(B) + I_A = all_interval_sum(cum_A) + I_B = all_interval_sum(cum_B) + I_A.sort() + I_B.sort() + return sum( + [ + bisect.bisect_right(I_B, T - I_A[i]) - bisect.bisect_left(I_B, T - I_A[i]) + for i in range(len(I_A)) + ] + ) + + +if __name__ == "__main__": + T = int(input()) + N = int(input()) + A = list(map(int, input().split())) + M = int(input()) + B = list(map(int, input().split())) + print(solve(T, A, B))