|
| 1 | +// Count of Range Sum |
| 2 | + |
| 3 | +// Fenwick tree |
| 4 | +class Solution { |
| 5 | + void add(vector<int> &fenwick, int n, int x) { |
| 6 | + for (; x < n; x |= x+1) |
| 7 | + fenwick[x]++; |
| 8 | + } |
| 9 | + int getSum(vector<int> &fenwick, int x) { |
| 10 | + int s = 0; |
| 11 | + for (; x; x &= x-1) |
| 12 | + s += fenwick[x-1]; |
| 13 | + return s; |
| 14 | + } |
| 15 | +public: |
| 16 | + int countRangeSum(vector<int>& nums, int lower, int upper) { |
| 17 | + int n = nums.size(); |
| 18 | + long s = 0, cnt = 0; |
| 19 | + vector<int> fenwick(n+1, 0); |
| 20 | + vector<long> scale; |
| 21 | + scale.push_back(0); |
| 22 | + for (int x: nums) |
| 23 | + scale.push_back(s += x); |
| 24 | + sort(scale.begin(), scale.end()); |
| 25 | + s = 0; |
| 26 | + add(fenwick, n+1, lower_bound(scale.begin(), scale.end(), 0) - scale.begin()); |
| 27 | + for (int x: nums) { |
| 28 | + s += x; |
| 29 | + cnt += getSum(fenwick, upper_bound(scale.begin(), scale.end(), s-lower) - scale.begin()) - |
| 30 | + getSum(fenwick, lower_bound(scale.begin(), scale.end(), s-upper) - scale.begin()); |
| 31 | + add(fenwick, n+1, lower_bound(scale.begin(), scale.end(), s) - scale.begin()); |
| 32 | + } |
| 33 | + return cnt; |
| 34 | + } |
| 35 | +}; |
| 36 | + |
| 37 | +/// divide and conquer |
| 38 | + |
| 39 | +class Solution { |
| 40 | + vector<long> a, b; |
| 41 | + int lower, upper; |
| 42 | + int f(int l, int h) { |
| 43 | + if (h-l <= 1) |
| 44 | + return 0; |
| 45 | + int m = l+h >> 1, cnt = f(l, m) + f(m, h), i = l, j = l; |
| 46 | + for (int k = m; k < h; k++) { |
| 47 | + while (i < m && a[i] < a[k]-upper) |
| 48 | + i++; |
| 49 | + while (j < m && a[j] <= a[k]-lower) |
| 50 | + j++; |
| 51 | + cnt += j-i; |
| 52 | + } |
| 53 | + merge(a.begin()+l, a.begin()+m, a.begin()+m, a.begin()+h, b.begin()+l); |
| 54 | + copy_n(b.begin()+l, h-l, a.begin()+l); |
| 55 | + return cnt; |
| 56 | + } |
| 57 | +public: |
| 58 | + int countRangeSum(vector<int>& nums, int lower, int upper) { |
| 59 | + this->lower = lower; |
| 60 | + this->upper = upper; |
| 61 | + int n = nums.size(); |
| 62 | + long s = 0; |
| 63 | + a.clear(); |
| 64 | + a.push_back(0); |
| 65 | + for (int x: nums) |
| 66 | + a.push_back(s += x); |
| 67 | + b.resize(n+1); |
| 68 | + return f(0, n+1); |
| 69 | + } |
| 70 | +}; |
| 71 | + |
| 72 | +/// order statistics tree |
| 73 | + |
| 74 | +#include <ext/pb_ds/assoc_container.hpp> |
| 75 | +#include <ext/pb_ds/tree_policy.hpp> |
| 76 | +using namespace __gnu_pbds; |
| 77 | + |
| 78 | +class Solution { |
| 79 | +public: |
| 80 | + int countRangeSum(vector<int>& nums, int lower, int upper) { |
| 81 | + typedef pair<long, int> pli; |
| 82 | + tree<pli, null_type, less<pli>, rb_tree_tag, tree_order_statistics_node_update> t; |
| 83 | + t.insert({0, 0}); |
| 84 | + int id = 1; |
| 85 | + long s = 0, cnt = 0; |
| 86 | + for (int x: nums) { |
| 87 | + s += x; |
| 88 | + cnt += t.order_of_key({s-lower, id}) - t.order_of_key({s-upper, 0}); |
| 89 | + t.insert({s, id++}); |
| 90 | + } |
| 91 | + return cnt; |
| 92 | + } |
| 93 | +}; |
0 commit comments