Skip to content

Commit 8c79cec

Browse files
committed
310/310
1 parent d67ab36 commit 8c79cec

File tree

2 files changed

+95
-1
lines changed

2 files changed

+95
-1
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
[LeetCode solutions](http://maskray.me/blog/2014-06-29-leetcode-solutions) gives some thoughts on selected problems.
44

5-
Solved 309/309 problems.
5+
Solved 310/310 problems.
66

77
## Database
88

@@ -12,6 +12,7 @@ See [database.md](database.md)
1212

1313
| # | Title | Solution |
1414
|---| ----- | -------- |
15+
|327|[Count of Range Sum](https://leetcode.com/problems/count-of-range-sum/)|[count-of-range-sum.cc](count-of-range-sum.cc)|
1516
|326|[Power of Three](https://leetcode.com/problems/power-of-three/)|[power-of-three.cc](power-of-three.cc)|
1617
|325|[Maximum Size Subarray Sum Equals k](https://leetcode.com/problems/maximum-size-subarray-sum-equals-k/)|[maximum-size-subarray-sum-equals-k.cc](maximum-size-subarray-sum-equals-k.cc)|
1718
|324|[Wiggle Sort II](https://leetcode.com/problems/wiggle-sort-ii/)|[wiggle-sort-ii.cc](wiggle-sort-ii.cc)|

count-of-range-sum.cc

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
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

Comments
 (0)