// yukicoder: No.728 ギブ and テイク // bal4u 2019.8.19 #include #include typedef long long ll; //// 入出力関係 #if 1 #define gc() getchar_unlocked() #else #define gc() getchar() #endif int in() { // 非負整数の入力 int n = 0, c = gc(); do n = 10 * n + (c & 0xf); while ((c = gc()) >= '0'); return n; } #define MAX 300005 //// bit木 1-indexedに注意 int bit[MAX], bitn; void add(int i, int v) { while (i <= bitn) bit[i] += v, i += i & -i; } void add1(int i) { while (i <= bitn) bit[i]++, i += i & -i; } int sum(int i) { int s = 0; while (i > 0) s += bit[i], i -= i & -i; return s; } typedef struct { int v, i, id; } T; T tbl[2*MAX]; int M; int A[MAX], L[MAX], R[MAX]; int N; // a[i]>key という条件を満たす最小のi int upper_bound(int x) { int m, l = 0, r = N; while (l+1 < r) { m = (l+r) >> 1; if (A[m] <= x) l = m; else r = m; } return A[l] <= x? r: l; } int cmp(const void *u, const void *v) { int t; if (t = ((T *)u)->v - ((T *)v)->v) return t; return ((T *)u)->id - ((T *)v)->id; } int main() { int i, j; ll ans; N = in(); for (i = 0; i < N; i++) A[i] = in(); j = 0; for (i = 0; i < N; i++) { L[i] = A[i]-in(), R[i] = A[i]+in(); tbl[j].i = i, tbl[j].id = 1, tbl[j++].v = A[i]; tbl[j].i = i, tbl[j++].v = L[i]; } M = N << 1; qsort(tbl, M, sizeof(T), cmp); ans = 0, bitn = N; for (j = 0; j < M; j++) { i = tbl[j].i; if (!tbl[j].id) add1(i+1); else ans += sum(upper_bound(R[i])) - sum(i+1); } printf("%lld\n", ans); return 0; }