-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy path1885.cpp
More file actions
50 lines (47 loc) · 1.16 KB
/
1885.cpp
File metadata and controls
50 lines (47 loc) · 1.16 KB
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
class BIT {
private:
vector<int> tree;
public:
BIT(int size) {
tree.resize(size + 1, 0);
}
int lsb(int x) {
return x & (-x);
}
void add(int idx, int value) {
while (idx < tree.size()) {
tree[idx] += value;
idx += lsb(idx);
}
}
int query(int idx) {
int sum = 0;
while (idx > 0) {
sum += tree[idx];
idx -= lsb(idx);
}
return sum;
}
};
class Solution {
public:
long long countPairs(vector<int>& nums1, vector<int>& nums2) {
set<int> st;
vector<int> diff;
int n = nums1.size();
int minVal = INT_MAX;
int maxVal = INT_MIN;
for (int i = 0; i < n; ++i) {
diff.push_back(nums1[i] - nums2[i]);
minVal = min({minVal, diff[i], -diff[i]});
maxVal = max({maxVal, diff[i], -diff[i]});
}
BIT* bit = new BIT(maxVal - minVal + 1);
long long res = 0;
for (int i = n - 1; i >= 0; --i) {
res += bit->query(diff[i] - minVal);
bit->add(-diff[i] - minVal + 1, 1);
}
return res;
}
};