|
1 | 1 | #include "public.h" |
2 | 2 |
|
3 | | -//224ms, 24.73%, 待优化 |
| 3 | +//40ms, 91.86% |
| 4 | +//分段求和, 伪线段树 |
| 5 | + |
| 6 | +class NumArray { |
| 7 | +private: |
| 8 | + vector<int> sums; |
| 9 | + vector<int> cpNums; |
| 10 | + int segSize; |
| 11 | + int sSize; |
| 12 | + |
| 13 | +public: |
| 14 | + NumArray(vector<int>& nums) { |
| 15 | + int nSize = nums.size(); |
| 16 | + if (nSize == 0) return; |
| 17 | + |
| 18 | + cpNums = nums; |
| 19 | + segSize = (int)sqrt(nSize); |
| 20 | + sSize = nSize / segSize; |
| 21 | + sums.resize(sSize); |
| 22 | + //初始化sums: 下标i 存放 nums中的[segSize*(i-1): segSize*i)的和 |
| 23 | + for (int index = 0; index < sSize; ++index) |
| 24 | + { |
| 25 | + sums[index] = accumulate(nums.begin() + segSize * index, |
| 26 | + nums.begin() + segSize * (index + 1), |
| 27 | + 0); |
| 28 | + } |
| 29 | + } |
| 30 | + |
| 31 | + void update(int i, int val) { |
| 32 | + //求i的segSize的整数倍数, 向下取整 |
| 33 | + int mulIndex = i / segSize; |
| 34 | + if (mulIndex != sSize) sums[mulIndex] += val - cpNums[i]; |
| 35 | + cpNums[i] = val; |
| 36 | + } |
| 37 | + |
| 38 | + int sumRange(int i, int j) { |
| 39 | + //按区间求和 |
| 40 | + //求i/j的segSize整数倍数, 向上/下取整 |
| 41 | + //sti是实际连续求和段的起始sums坐标, enj是结束坐标 |
| 42 | + int muli, mulj, sti, enj; |
| 43 | + int res = 0; |
| 44 | + |
| 45 | + muli = i / segSize; |
| 46 | + if (i%segSize != 0) //例如: segSize:3, i:1, j=7; 一共是 012 345 678 |
| 47 | + { |
| 48 | + res += accumulate(cpNums.begin() + i, cpNums.begin() + (muli + 1)*segSize, 0); |
| 49 | + sti = muli + 1; |
| 50 | + } |
| 51 | + else sti = muli; |
| 52 | + |
| 53 | + mulj = j / segSize; |
| 54 | + if ((j + 1) % segSize != 0) |
| 55 | + { |
| 56 | + res += accumulate(cpNums.begin() + mulj * segSize, cpNums.begin() + j + 1, 0); |
| 57 | + enj = mulj - 1; |
| 58 | + } |
| 59 | + else enj = mulj; |
| 60 | + |
| 61 | + if (sti == enj + 2) return res - sums[enj + 1]; |
| 62 | + else if (sti <= (enj + 1)) |
| 63 | + { |
| 64 | + return res + accumulate(sums.begin() + sti, sums.begin() + enj + 1, 0); |
| 65 | + } |
| 66 | + else return -99999; //dump |
| 67 | + } |
| 68 | +}; |
| 69 | + |
| 70 | +/* |
| 71 | +//224ms, 24.73% |
| 72 | +//纯暴力 |
4 | 73 | //vector<int>保存下标0至当前的元素和 |
5 | 74 |
|
6 | 75 | class NumArray { |
@@ -32,10 +101,17 @@ class NumArray { |
32 | 101 | else return sums[j] - sums[i - 1]; |
33 | 102 | } |
34 | 103 | }; |
| 104 | +*/ |
35 | 105 |
|
36 | | -/** |
37 | | - * Your NumArray object will be instantiated and called as such: |
38 | | - * NumArray* obj = new NumArray(nums); |
39 | | - * obj->update(i,val); |
40 | | - * int param_2 = obj->sumRange(i,j); |
41 | | - */ |
| 106 | +/* |
| 107 | +int main() |
| 108 | +{ |
| 109 | + vector<int> nums = { 1,3,5 }; |
| 110 | + NumArray* n = new NumArray(nums); |
| 111 | + cout << n->sumRange(0, 2) << endl; //9 |
| 112 | + n->update(1, 2); |
| 113 | + cout << n->sumRange(0, 2) << endl; //8 |
| 114 | + cout << n->sumRange(0, 1) << endl; //3 |
| 115 | + cout << n->sumRange(0, 0) << endl; //1 |
| 116 | +} |
| 117 | +*/ |
0 commit comments