Advertisement
nikunjsoni

1865

May 17th, 2021
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.03 KB | None | 0 0
  1. class FindSumPairs {
  2. public:
  3.     unordered_map<long long, long long> count2;
  4.     unordered_map<long long, long long> count1;
  5.     vector<int> n2, n1;
  6.     FindSumPairs(vector<int>& nums1, vector<int>& nums2) {
  7.         n2 = nums2; n1 = nums1;
  8.         for(auto num: nums2)
  9.             count2[num]++;
  10.         for(auto num: nums1)
  11.             count1[num]++;
  12.     }
  13.    
  14.     void add(int index, int val) {
  15.         long long tmp = n2[index];
  16.         n2[index] += val;
  17.         count2[tmp]--;
  18.         count2[n2[index]]++;
  19.     }
  20.    
  21.     int count(int tot) {
  22.         long long ans=0;
  23.         for(auto p: count1){
  24.             long long num  = p.first;
  25.             if(tot < num)continue;
  26.             int rem = tot - num;
  27.             if(count2.count(rem))
  28.                 ans += (count1[num]*count2[rem]);
  29.         }
  30.         return ans;
  31.     }
  32. };
  33.  
  34. /**
  35.  * Your FindSumPairs object will be instantiated and called as such:
  36.  * FindSumPairs* obj = new FindSumPairs(nums1, nums2);
  37.  * obj->add(index,val);
  38.  * int param_2 = obj->count(tot);
  39.  */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement