-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy path2426.cpp
50 lines (49 loc) · 1.34 KB
/
2426.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
class BIT {
private:
vector<int> tree;
public:
BIT(int size) {
tree.resize(size + 1, 0);
}
int lsb(int x) {
return x & (-x);
}
void update(int index, int value) {
while (index < tree.size()) {
tree[index] += value;
index += lsb(index);
}
}
int getSum(int index) {
int sum = 0;
while (index) {
sum += tree[index];
index -= lsb(index);
}
return sum;
}
int queryRange(int low, int high) {
// [low, high]
return getSum(high) - getSum(low - 1);
}
};
class Solution {
public:
long long numberOfPairs(vector<int>& nums1, vector<int>& nums2, int diff) {
BIT* bit = new BIT(60006);
int base = 30003;
int n = nums1.size();
vector<int> nums1MinusNums2(n, 0);
for (int i = 0; i < n; ++i) nums1MinusNums2[i] = nums1[i] - nums2[i];
long long res = 0;
for (int i = n - 1; i >= 0; --i) {
int cnt = bit->queryRange(nums1MinusNums2[i] + base, 60006);
res += cnt;
bit->update(nums1MinusNums2[i] + base + diff, 1);
}
return res;
}
};
// i < j
// nums1[i] - nums1[j] <= nums2[i] - nums2[j] + diff
// nums1[i] - nums2[i] <= nums1[j] - nums2[j] + diff => query [nums1[j] - nums2[j] + diff, ...]