#include #include #include using namespace std; typedef pair PII; typedef long long LL; const int N = 300010; LL ans; int n, a[N], lidx[N], ridx[N], tr[N]; priority_queue, greater> pq; int LowBit(int x) { return x & -x; } void Add(int u, int v) { for (int i = u; i <= n; i += LowBit(i)) tr[i] += v; } LL Query(int x) { LL ret = 0LL; for (int i = x; i; i -= LowBit(i)) ret += tr[i]; return ret; } int main() { scanf("%d", &n); for (int i = 1; i <= n; ++i) scanf("%d", &a[i]); for (int i = 1, l, r; i <= n; ++i) { scanf("%d%d", &l, &r); LL left = 0LL + a[i] - l, right = 0LL + a[i] + r; lidx[i] = lower_bound(a + 1, a + n + 1, left) - a; ridx[i] = upper_bound(a + 1, a + n + 1, right) - a - 1; } for (int i = 1; i <= n; ++i) { while (!pq.empty() && pq.top().first < i) { int idx = pq.top().second; pq.pop(); Add(idx, -1); } int l = lidx[i]; if (l <= i - 1) ans += Query(i - 1) - Query(l - 1); Add(i, 1); pq.push({ ridx[i], i }); } printf("%lld\n", ans); return 0; }