You are given the following: Integers n, c, d Arr...
Created on: September 2, 2025
Answered using GPT-5 Thinking by Chat01
Created on: September 2, 2025
Answered using GPT-5 Thinking by Chat01
You are given the following:
Integers n, c, d
Array a as {a1, a2, ..., an} of length n
Array b as {b1, b2, ..., bn} of length n
Determine the number of pairs i, j (1 <= i < j <= n), satisfying the inequality
ai - aj + c <= bi - bj + d
code this in java
Here’s an O(n log n) Java solution. It reduces the condition
ai − aj + c ≤ bi − bj + d
⟺ (ai − bi) − (aj − bj) ≤ (d − c)
Let xi = ai − bi and k = d − c. For each j (in index order), we need the count of previous i < j with xi ≤ xj + k. We maintain a Fenwick tree (Binary Indexed Tree) over compressed xi values to query “how many previous xi are ≤ threshold” in logarithmic time.
javaimport java.io.*; import java.util.*; public class Main { // Fenwick (Binary Indexed) Tree for prefix sums static final class Fenwick { long[] bit; // 1-indexed Fenwick(int n) { bit = new long[n + 1]; } void add(int idx, long delta) { for (int i = idx; i < bit.length; i += i & -i) bit[i] += delta; } long sum(int idx) { long res = 0; for (int i = idx; i > 0; i -= i & -i) res += bit[i]; return res; } } // Fast input static final class FastScanner { private final InputStream in; private final byte[] buffer = new byte[1 << 16]; private int ptr = 0, len = 0; FastScanner(InputStream is) { in = is; } private int read() throws IOException { if (ptr >= len) { len = in.read(buffer); ptr = 0; if (len <= 0) return -1; } return buffer[ptr++]; } long nextLong() throws IOException { int c; do { c = read(); } while (c <= 32); int sign = 1; if (c == '-') { sign = -1; c = read(); } long val = 0; while (c > 32) { val = val * 10 + (c - '0'); c = read(); } return val * sign; } int nextInt() throws IOException { return (int) nextLong(); } } // Binary searches on sorted unique array static int lowerBound(long[] arr, long x) { int l = 0, r = arr.length; while (l < r) { int m = (l + r) >>> 1; if (arr[m] < x) l = m + 1; else r = m; } return l; } static int upperBound(long[] arr, long x) { int l = 0, r = arr.length; while (l < r) { int m = (l + r) >>> 1; if (arr[m] <= x) l = m + 1; else r = m; } return l; // number of elements <= x } public static void main(String[] args) throws Exception { FastScanner fs = new FastScanner(System.in); int n = fs.nextInt(); long c = fs.nextLong(); long d = fs.nextLong(); long[] a = new long[n]; long[] b = new long[n]; for (int i = 0; i < n; i++) a[i] = fs.nextLong(); for (int i = 0; i < n; i++) b[i] = fs.nextLong(); long k = d - c; long[] x = new long[n]; for (int i = 0; i < n; i++) x[i] = a[i] - b[i]; // Coordinate compress the x values (the only values we insert in Fenwick) long[] xs = x.clone(); Arrays.sort(xs); int m = 0; for (int i = 0; i < xs.length; i++) { if (i == 0 || xs[i] != xs[i - 1]) xs[m++] = xs[i]; } long[] uniq = Arrays.copyOf(xs, m); Fenwick fw = new Fenwick(uniq.length); long ans = 0; // Process j from left to right (respecting i < j) for (int j = 0; j < n; j++) { long threshold = x[j] + k; int r = upperBound(uniq, threshold); // number of unique xi <= threshold if (r > 0) ans += fw.sum(r); // count previous xi satisfying xi <= threshold int pos = lowerBound(uniq, x[j]) + 1; // 1-indexed for Fenwick fw.add(pos, 1); } System.out.println(ans); } }
n c da1 a2 ... anb1 b2 ... bnThe program prints the number of valid pairs (i, j) with 1 ≤ i < j ≤ n satisfying the inequality. Uses long everywhere to be safe for large values and returns in O(n log n) time.