#include using namespace std; struct query{ int x; int type; // 0: insert, 1: count int y; int idx; query(int _x, int _type, int _y, int _idx): x(_x), type(_type), y(_y), idx(_idx){} bool operator<(const query & rhs){ if (x < rhs.x){ return true; }else if (x > rhs.x){ return false; }else if (type < rhs.type){ return true; }else if (type > rhs.type){ return false; }else{ return y < rhs.y; } } }; int compress_y(vector & que){ vector> ys(que.size()); for (int i = 0; i < que.size(); ++i) { ys[i].first = que[i].y; ys[i].second = i; } sort(ys.begin(), ys.end()); int pos = 0; que[ys[0].second].y = pos; for (int i = 1; i < que.size(); ++i){ if (ys[i].first > ys[i - 1].first) ++pos; que[ys[i].second].y = pos; } return pos; } struct BIT{ vector bit; BIT(int n) : bit(n, 0){} int sum(int j){ int s = 0; for (; j >= 0; j = (j & (j + 1)) - 1) s += bit[j]; return s; } void add(int k, int a) {for (; k < bit.size(); k |= k + 1) bit[k] += a;} }; int main(){ cin.tie(0); ios::sync_with_stdio(false); int N; cin >> N; vector As(N, 0); for (auto & a : As) cin >> a; vector Ls(N, 0); vector Rs(N, 0); for (int i = 0; i < N; ++i){ cin >> Ls[i] >> Rs[i]; } vector que; for (int i = 0; i < N; ++i){ que.emplace_back(As[i], 0, As[i] - Ls[i], i); que.emplace_back(As[i] + Rs[i], 1, As[i], i); } sort(que.begin(), que.end()); int max_y = compress_y(que); BIT bit(max_y + 1); vector> aidx(N); for (int i = 0; i < N; ++i){ aidx[i].first = As[i]; aidx[i].second = i; } sort(aidx.begin(), aidx.end()); vector cum(N, 0); for (int i = 0; i < N; ++i){ cum[aidx[i].second] = i + 1; } long long ans = 0; for (auto & q : que){ if (q.type == 0){ bit.add(q.y, 1); }else{ ans += bit.sum(q.y) - cum[q.idx]; } } cout << ans << endl; return 0; }