#include const int inf = 0; int leaf[200001]; typedef struct { int left, right, max; } seg_node; void init_node(seg_node v[], int k, int l, int r) { v[k].left = l; v[k].right = r; v[k].max = inf; if (l < r) { init_node(v, k << 1, l, (l + r) / 2); init_node(v, (k << 1) ^ 1, (l + r) / 2 + 1, r); } else leaf[l] = k; } void update_node(seg_node v[], int k, int x) { int i, j = leaf[k]; v[j].max = x; for (i = j >> 1; i > 0; j = i, i >>= 1) v[i].max = (v[j].max > v[j^1].max)? v[j].max: v[j^1].max; } int get_max(seg_node v[], int k, int l, int r) { int tmp[2]; if (v[k].right < l || v[k].left > r) return inf; else if (v[k].left >= l && v[k].right <= r) return v[k].max; else { tmp[0] = get_max(v, k << 1, l, r); tmp[1] = get_max(v, (k << 1) ^ 1, l, r); return (tmp[0] < tmp[1])? tmp[1]: tmp[0]; } } int BS_left(seg_node v[], int k, int l, int r, int x) { int tmp; if (v[k].max < x || v[k].right < l || v[k].left > r) return r + 1; else if (v[k].left == v[k].right) return v[k].left; else { tmp = BS_left(v, k << 1, l, r, x); if (tmp <= r) return tmp; else return BS_left(v, (k << 1) ^ 1, l, r, x); } } int BS_right(seg_node v[], int k, int l, int r, int x) { int tmp; if (v[k].max < x || v[k].right < l || v[k].left > r) return l - 1; else if (v[k].left == v[k].right) return v[k].left; else { tmp = BS_right(v, (k << 1) ^ 1, l, r, x); if (tmp >= l) return tmp; else return BS_right(v, k << 1, l, r, x); } } int main() { int i, N, P[200001], P_inv[200001]; long long A, sum[200001], ans; scanf("%d", &N); for (i = 1; i <= N; i++) { scanf("%d", &(P[i])); P_inv[P[i]] = i; } for (i = 1, sum[0] = 0; i <= N; i++) sum[i] = sum[i-1] + P[i]; int j, k, l, r, m, L, R; seg_node v[524288]; init_node(v, 1, 1, N); for (i = 1; i <= N; i++) { scanf("%lld", &A); k = P_inv[i]; L = BS_right(v, 1, 1, k - 1, 1) + 1; R = BS_left(v, 1, k + 1, N, 1) - 1; ans = 0; if (k - L < R - k) { for (j = k, r = R; j >= L; j--) { l = k - 1; while (l < r) { m = (l + r + 1) / 2; if (sum[m] - sum[j-1] <= A) l = m; else r = m - 1; } ans += l - k + 1; } } else { for (j = k, l = L; j <= R; j++) { r = k + 1; while (l < r) { m = (l + r) / 2; if (sum[j] - sum[m-1] <= A) r = m; else l = m + 1; } ans += k - l + 1; } } printf("%lld\n", ans); update_node(v, k, 1); } fflush(stdout); return 0; }