#include using namespace std; #define rep(i,n) for(int i = 0; i < (int)n; i++) #define FOR(n) for(int i = 0; i < (int)n; i++) #define repi(i,a,b) for(int i = (int)a; i < (int)b; i++) #define pb push_back #define m0(x) memset(x,0,sizeof(x)) #define fill(x,y) memset(x,y,sizeof(x)) #define bg begin() #define ed end() #define all(x) x.bg,x.ed //#define mp make_pair #define vi vector #define vvi vector #define vll vector #define vvll vector #define vs vector #define vvs vector #define vc vector #define vvc vector #define pii pair #define pllll pair #define vpii vector> #define vpllll vector> #define vpis vector> #define vplls vector> #define vpsi vector> #define vpsll vector> template void chmax(T &a, const T &b) {a = (a > b? a : b);} template void chmin(T &a, const T &b) {a = (a < b? a : b);} using ll = long long; using ld = long double; using ull = unsigned long long; const ll INF = numeric_limits::max() / 2; const ld pi = 3.1415926535897932384626433832795028; const ll mod = 1e9 + 7; int dx[] = {-1, 0, 1, 0, -1, -1, 1, 1}; int dy[] = {0, -1, 0, 1, -1, 1, -1, 1}; #define int long long template struct RMQ { const T e = -INF; function fx = [](T x1, T x2) -> T { return max(x1, x2); }; int n; vector dat; RMQ(int n_) : n(), dat(n_ * 4, e) { int x = 1; while (n_ > x) { x *= 2; } n = x; } void add(int i, T x) { i += n - 1; dat[i] += x; while(i > 0) { i = (i - 1) / 2; dat[i] = fx(dat[i * 2 + 1], dat[i * 2 + 2]); } } void update(int i, T x) { i += n - 1; dat[i] = x; while (i > 0) { i = (i - 1) / 2; // parent dat[i] = fx(dat[i * 2 + 1], dat[i * 2 + 2]); } } // the minimum element of [a,b) T query(int a, int b) { return query_sub(a, b, 0, 0, n); } T query_sub(int a, int b, int k, int l, int r) { if (r <= a || b <= l) { return e; } else if (a <= l && r <= b) { return dat[k]; } else { T vl = query_sub(a, b, k * 2 + 1, l, (l + r) / 2); T vr = query_sub(a, b, k * 2 + 2, (l + r) / 2, r); return fx(vl, vr); } } int find_rightest(int a, int b, T x) { return find_rightest_sub(a, b, x, 0, 0, n); } int find_leftest(int a, int b, T x) { return find_leftest_sub(a, b, x, 0, 0, n); } int find_rightest_sub(int a, int b, T x, int k, int l, int r) { if (dat[k] > x || r <= a || b <= l) { // 自分の値がxより大きい or [a,b)が[l,r)の範囲外ならreturn a-1 return a - 1; } else if (k >= n - 1) { // 自分が葉ならその位置をreturn return (k - (n - 1)); } else { int vr = find_rightest_sub(a, b, x, 2 * k + 2, (l + r) / 2, r); if (vr != a - 1) { // 右の部分木を見て a-1 以外ならreturn return vr; } else { // 左の部分木を見て値をreturn return find_rightest_sub(a, b, x, 2 * k + 1, l, (l + r) / 2); } } } int find_leftest_sub(int a, int b, T x, int k, int l, int r) { if (dat[k] > x || r <= a || b <= l) { // 自分の値がxより大きい or [a,b)が[l,r)の範囲外ならreturn b return b; } else if (k >= n - 1) { // 自分が葉ならその位置をreturn return (k - (n - 1)); } else { int vl = find_leftest_sub(a, b, x, 2 * k + 1, l, (l + r) / 2); if (vl != b) { // 左の部分木を見て b 以外ならreturn return vl; } else { // 右の部分木を見て値をreturn return find_leftest_sub(a, b, x, 2 * k + 2, (l + r) / 2, r); } } } inline T operator[](int i) { return query(i, i + 1); } void print() { for(int i = 0; i < n; i++) { cout << (*this)[i]; if(i != n - 1) cout << ","; } cout << "\n"; } }; void solve() { // a[i] + b[i] < a[j] + b[j] // a[i] + b[j] < a[j] + b[i] // a[i] - b[i] < a[j] - b[j]; int n; cin >> n; vi a(n), b(n); FOR(n) cin >> a[i]; FOR(n) cin >> b[i]; RMQ tree1(n); RMQ tree2(n); FOR(n) tree1.update(i, a[i] + b[i]); FOR(n) tree2.update(i, a[i] - b[i]); int ans = 0; int j = 0; for(int i = 0; i < n - 1; i++) { while(j < n && tree1.query(i, j) < tree1[j] && tree2.query(i, j) < tree2[j]) { j++; } ans += j - i - 1; } cout << ans << endl; } signed main() { cin.tie(nullptr); ios::sync_with_stdio(false); solve(); return 0; }