import bisect class FenwickTree(object): def __init__(self, n): self.n = n self.log = n.bit_length() self.data = [0] * n def __sum(self, r): s = 0 while r > 0: s += self.data[r - 1] r -= r & -r return s def add(self, p, x): """ a[p] += xを行う""" p += 1 while p <= self.n: self.data[p - 1] += x p += p & -p def sum(self, l, r): """a[l] + a[l+1] + .. + a[r-1]を返す""" return self.__sum(r) - self.__sum(l) def lower_bound(self, x): """a[0] + a[1] + .. a[i] >= x となる最小のiを返す""" if x <= 0: return -1 i = 0 k = 1 << self.log while k: if i + k <= self.n and self.data[i + k - 1] < x: x -= self.data[i + k - 1] i += k k >>= 1 return i def __repr__(self): res = [self.sum(i, i+1) for i in range(self.n)] return " ".join(map(str, res)) N = int(input()) P = list(map(int, input().split())) A = list(map(int, input().split())) S = [0] * (N + 1) for i, p in enumerate(P): S[i+1] += S[i] + p pos = {p: i for i, p in enumerate(P, start=1)} bit = FenwickTree(N+2) bit.add(0, 1) bit.add(N+1, 1) for i, a in enumerate(A, start=1): p = pos[i] tot = bit.sum(0, p) Lcnt = p - bit.lower_bound(tot) Rcnt = bit.lower_bound(tot + 1) - p bit.add(p, 1) ans = 0 if Lcnt < Rcnt: for L in range(p - Lcnt, p): R = bisect.bisect_right(S, S[L] + a) R = min(R, p + Rcnt) ans += max(0, R - p) else: for R in range(p, p + Rcnt): L = bisect.bisect_left(S, S[R] - a) L = max(L, p - Lcnt) ans += max(0, p - L) print(ans)