#include using namespace std; using ll = long long; //1-indexであることに注意 template struct BIT { int n; vector data; BIT(int n) : n(n), data(n + 1, 0) {} T sum(int i) { T s = 0; while (i > 0) { s += data[i]; i -= i & -i; } return s; } void add(int i, T x) { while (i <= n) { data[i] += x; i += i & -i; } } }; //引数unzipには圧縮前のvectorを入れる int compress(vector& unzip, map& zip) { sort(unzip.begin(), unzip.end()); unzip.erase(unique(unzip.begin(), unzip.end()), unzip.end()); for (int i = 0; i < unzip.size(); i++) { zip[unzip[i]] = i; } return unzip.size(); } int main() { cin.tie(0); ios::sync_with_stdio(false); int n; cin >> n; vector a(n), L(n), R(n); for (int i = 0; i < n; i++) cin >> a[i]; for (int i = 0; i < n; i++) { cin >> L[i] >> R[i]; } using T = tuple; vector v; vector unzip; for (int i = 0; i < n; i++) { v.emplace_back(L[i], 0, i); v.emplace_back(R[i], 1, i); unzip.emplace_back(a[i]); unzip.emplace_back(a[i] - L[i]); unzip.emplace_back(a[i] + R[i]); } map zip; int sz = compress(unzip, zip); sort(v.begin(), v.end()); BIT bt(sz), bt2(sz); for (int i = 0; i < n; i++) { bt.add(zip[a[i]] + 1, 1); bt2.add(zip[a[i]] + 1, 1); } ll ans = 0; for (T& t : v) { int val, idx, flag; tie(val, flag, idx) = t; if (flag) { bt.add(zip[a[idx]] + 1, -1); ans += bt2.sum(zip[a[idx] + R[idx]] + 1) - bt2.sum(zip[a[idx]] + 1); } else { bt2.add(zip[a[idx]] + 1, -1); ans += bt.sum(zip[a[idx]]) - bt.sum(zip[a[idx] - L[idx]]); } } cout << ans << endl; return 0; }