class FindSumPairs {
public:
FindSumPairs(vector<int>& nums1, vector<int>& nums2) {
}
void add(int index, int val) {
}
int count(int tot) {
}
};
/**
* Your FindSumPairs object will be instantiated and called as such:
* FindSumPairs* obj = new FindSumPairs(nums1, nums2);
* obj->add(index,val);
* int param_2 = obj->count(tot);
*/
class FindSumPairs {
int[] nums1;
int[] nums2;
Map<Integer, Integer> hash;
public FindSumPairs(int[] nums1, int[] nums2) {
this.nums1 = nums1;
this.nums2 = nums2;
hash = new HashMap<>();
// hash num1 会超时
for (int i = 0; i < nums2.length; i++) {
int num = nums2[i];
hash.put(num, hash.getOrDefault(num, 0) + 1);
}
}
public void add(int index, int val) {
hash.put(nums2[index], hash.get(nums2[index]) - 1);
hash.put(nums2[index] + val, hash.getOrDefault(nums2[index]+val, 0) + 1);
nums2[index] += val;
}
public int count(int tot) {
int ans = 0;
for (int i = 0; i < nums1.length; i++) {
int dif = tot - nums1[i];
ans += hash.getOrDefault(dif, 0);
}
return ans;
}
}
/**
* Your FindSumPairs object will be instantiated and called as such:
* FindSumPairs obj = new FindSumPairs(nums1, nums2);
* obj.add(index,val);
* int param_2 = obj.count(tot);
*/
type FindSumPairs struct{}
var (
x, y []int
cnt map[int]int
)
func Constructor(nums1, nums2 []int) (_ FindSumPairs) {
cnt = map[int]int{}
for _, v := range nums2 {
cnt[v]++
}
x, y = nums1, nums2
return
}
func (FindSumPairs) Add(i, val int) {
cur := y[i]
cnt[cur]--
cnt[cur+val]++
y[i] += val
}
func (FindSumPairs) Count(tot int) (ans int) {
for _, v := range x {
ans += cnt[tot-v]
}
return
}
/**
* Your FindSumPairs object will be instantiated and called as such:
* obj := Constructor(nums1, nums2);
* obj.Add(index,val);
* param_2 := obj.Count(tot);
*/
class FindSumPairs:
def __init__(self, nums1: List[int], nums2: List[int]):
self.nums1 = nums1
self.nums2 = nums2
self.cnt = collections.Counter(nums2)
def add(self, index: int, val: int) -> None:
self.cnt[self.nums2[index]] -= 1
self.nums2[index] += val
self.cnt[self.nums2[index]] += 1
def count(self, tot: int) -> int:
ans = 0
for num in self.nums1:
if tot - num in self.cnt:
ans += self.cnt[tot-num]
return ans
# Your FindSumPairs object will be instantiated and called as such:
# obj = FindSumPairs(nums1, nums2)
# obj.add(index,val)
# param_2 = obj.count(tot)