#include #define show(x) std::cerr << #x << " = " << x << std::endl using ull = unsigned long long; constexpr std::size_t PC(ull v) { return v = (v & 0x5555555555555555ULL) + (v >> 1 & 0x5555555555555555ULL), v = (v & 0x3333333333333333ULL) + (v >> 2 & 0x3333333333333333ULL), v = (v + (v >> 4)) & 0x0F0F0F0F0F0F0F0FULL, static_cast(v * 0x0101010101010101ULL >> 56 & 0x7f); } class WaveletMatrix { public: static constexpr std::size_t L = 31; using T = ull; template WaveletMatrix(const InIt first, const InIt last) : sz(std::distance(first, last)) { std::vector b(sz); std::vector v(first, last); for (std::size_t i = 0, j = L - 1; i < L; i++, j--) { std::vector z, o; for (std::size_t i = 0; i < sz; i++) { b[i] = (v[i] >> j) & 1, (b[i] ? o : z).push_back(v[i]); } table.push_back(FullyIndexableDictionary{b}), v = z, zero.push_back(z.size()); for (const T e : o) { v.push_back(e); } } } std::size_t noLessThan(std::size_t l, std::size_t r, const T v) const { std::size_t off = 0; for (std::size_t i = 0, j = L - 1; i < L; i++, j--) { const bool b = (v >> j) & 1; const std::size_t rl = table[i].rank(l), rr = table[i].rank(r); l = (b ? rl + zero[i] : l - rl), r = (b ? rr + zero[i] : r - rr), off += (b ? 0 : rr - rl); } return r - l + off; } std::size_t rangeFreq(const std::size_t l, const std::size_t r, const T vinf, const T vsup) const { return noLessThan(l, r, vinf) - noLessThan(l, r, vsup); } std::size_t rank(const std::size_t l, const std::size_t r, const T v) const { return rangeFreq(l, r, v, v + 1); } private: class FullyIndexableDictionary { private: using B = unsigned long long; static constexpr std::size_t BS = sizeof(B) * 8; const std::size_t sz; std::vector data; std::vector large; public: FullyIndexableDictionary(const std::vector& b) : sz(b.size()), data((sz + BS) / BS, 0), large((sz + BS) / BS, 0) { std::size_t one = 0; for (std::size_t i = 0; i < sz; i++) { data[i / BS] |= ((B)b[i] << (i % BS)), one += b[i]; if (i % BS == BS - 1) { large[(i + 1) / BS] = one; } } } bool operator[](const std::size_t n) const { return (data[n / BS] >> (n % BS)) & 1; } std::size_t rank(const std::size_t n) const { return large[n / BS] + (n % BS == 0 ? 0 : PC(data[n / BS] & ((1ULL << (n % BS)) - 1))); } }; const std::size_t sz; std::vector zero; std::vector table; }; int main() { std::cin.tie(0); std::ios::sync_with_stdio(false); constexpr ull OFFSET = 1000000000ULL; std::size_t N; std::cin >> N; std::vector A(N), L(N), R(N), B(N); for (auto& e : A) { std::cin >> e; } for (std::size_t i = 0; i < N; i++) { std::cin >> L[i] >> R[i], B[i] = A[i] - L[i] + OFFSET; } WaveletMatrix wm{B.begin(), B.end()}; ull ans = 0; for (std::size_t i = 0; i < N - 1; i++) { const std::size_t isup = std::upper_bound(A.begin(), A.end(), A[i] + R[i]) - A.begin(); ans += wm.rangeFreq(i + 1, isup, 0, A[i] + OFFSET + 1); } std::cout << ans << std::endl; return 0; }