/** * @FileName a.cpp * @Author kanpurin * @Created 2020.06.11 19:08:58 **/ #include "bits/stdc++.h" using namespace std; typedef long long ll; template< typename T > struct BinaryIndexedTree { std::vector< T > data; BinaryIndexedTree(int sz) { data.assign(++sz, 0); } inline T sum(int k) { T ret = 0; for (++k; k > 0; k -= k & -k) ret += data[k]; return (ret); } inline T sum(int left, int right) { return sum(right) - sum(left - 1); } inline void add(int k, T x) { for (++k; k < data.size(); k += k & -k) data[k] += x; } int get(ll k) { ++k; int res = 0; int N = 1; while (N < (int)data.size()) N *= 2; for (int i = N / 2; i > 0; i /= 2) { if (res + i < (int)data.size() && data[res + i] < k) { k = k - data[res + i]; res = res + i; } } return res + 1; } void print() { std::cout << "[ "; for (int i = 0; i < data.size() - 1; i++) { std::cout << sum(i, i); if (i < data.size() - 2) cout << ", "; } std::cout << " ]" << endl; } }; int main() { int n;cin >> n; vector a(n); map mp; for (int i = 0; i < n; i++) { cin >> a[i]; mp[a[i]] = i; } BinaryIndexedTree bit(n); set> st; ll ans = 0; for (int i = 0; i < n; i++) { int l,r;cin >> l >> r; while(!st.empty() && (*st.begin()).first < a[i]) { bit.add(mp[(*st.begin()).second],-1); st.erase(st.begin()); } ans += bit.sum(mp[*lower_bound(a.begin(), a.end(), a[i]-l)],n-1); st.insert({a[i]+r,i}); bit.add(mp[a[i]],1); } cout << ans << endl; return 0; }