// write the values stored in ledStatus to the cube, ideally this method should // be called atleast once every millisecond for multiplexing to give a smooth // image and avoid flickering. // displays 1 layer at a time at a high frequency to give a smooth image. void CubeInterface::writeCube() { // 9th shift register controls cathodes for(byte i = 0; i < 8; i++) { if(CATHODE_PINS[i] == currentLayer) highBit(); else lowBit(); } // for // first 8 shift registers control anodes for(byte i = 0; i < 64; i++) if(ledStatus[ANODE_PINS[i][0]] [ANODE_PINS[i][1]] [currentLayer] == HIGH) highBit(); else lowBit(); if(currentLayer < 7) currentLayer++; else currentLayer = 0; latch(); } // writeCube
int sumRange(int i, int j) { if(i > j) return 0; j++; //Note here! int sum_i = 0, sum_j = 0; for(int k = i; k > 0; k -= lowBit(k)) sum_i += bitree[k]; for(int k = j; k > 0; k -=lowBit(k)) sum_j += bitree[k]; return sum_j - sum_i; }
int sum(int index) { int sum = 0; for (int i = index ; i > 0 ; i -= lowBit(i)) { sum += C[i]; } return sum; }
int BIT_update(BIT_Handle handle, const unsigned int i, const int delta) { BIT_Array *pArray = (BIT_Array*)handle; unsigned int j; if (NULL == pArray) { ERROR("Invalid array handle %p.\n", handle); return -1; } if (i >= pArray->length) { ERROR("Invalid index %u.\n", i); return -1; } pArray->pSrc[i] += delta; for (j = i; j <= pArray->length; j += lowBit(j)) { pArray->pTree[j] += delta; } return 0; }
int BIT_sum(BIT_Handle handle, const unsigned int n) { BIT_Array *pArray = (BIT_Array*)handle; unsigned int i; int sum = 0; if (NULL == pArray) { ERROR("Invalid array handle %p.\n", handle); return BIT_INVALID_SUM; } if (n > pArray->length) { ERROR("Invalid n %u, exceeds the array length %u.\n", n, pArray->length); return BIT_INVALID_SUM; } /* * 对于求数列的前n项和,只需找到n以前的所有最大子树,把其根节点的C加起来即可。 * 不难发现,这些子树的数目是n在二进制时1的个数,或者说是把n展开成2的幂方和时的项数. * 1. 从下标为 n 的元素处开始往前计算. * 2. i -= lowBit(i) 是因为 pTree[i] 包含了源数组中 lowBit(i) 个元素之和. * 3. 下标从 1 开始,故结束条件是下标达到 0. */ for (i = n; i > 0; i -= lowBit(i)) { sum += pArray->pTree[i]; } return sum; }
void update(int i, int val) { int inc = val - input[i]; for (int k = i + 1; k <= len; k += lowBit(k)) { sum[k] += inc; } input[i] = val; }
int getSum(int i) { int ans = 0; for (int k = i + 1; k > 0; k -= lowBit(k)) { ans += sum[k]; } return ans; }
void add(int index, int delta = 1) { while(index <= len) { array[index] += delta; index += lowBit(index); } }
int sum(int index) { int res = 0; while(index > 0) { res+=array[index]; index -= lowBit(index); } return res; }
BIT_Handle BIT_create(const unsigned int length) { BIT_Array *pArray = NULL; size_t memorySize; unsigned int i, j; if (0 == length) { ERROR("length %u should be >= 1.\n", length); return NULL; } pArray = calloc(1, sizeof(*pArray)); if (NULL == pArray) { ERROR("Failed to allocate memory.\n"); return NULL; } /* subscript starts from 1, so the element at [0] is not used */ pArray->length = length; memorySize = (pArray->length + 1) * sizeof(pArray->pSrc[0]); pArray->pSrc = calloc(1, memorySize); pArray->pTree = calloc(1, memorySize); if (NULL == pArray->pSrc || NULL == pArray->pTree) { ERROR("Failed to allocate memory for array elements.\n"); BIT_destroy(pArray); } for (i = 1; i <= pArray->length; ++i) { pArray->pSrc[i] = i; } for (i = 1; i <= pArray->length; ++i) { pArray->pTree[i] = pArray->pSrc[i]; /* tree[i] 对 src[i] 之前的 lowBit(i) 个元素负责 */ for (j = i - 1; j > i - lowBit(i); --j) { pArray->pTree[i] += pArray->pSrc[j]; } } return (BIT_Handle)pArray; }
void add(int index, int value) { for (int i = index ; i <= size ; i += lowBit(i)) { C[i] += value; } }
void add(int beg, int end, int val) { for(int i = beg; i < end; i += lowBit(i)) bitree[i] += val; }
NumArray(vector<int> &nums): len(nums.size()), sum(len + 1, 0), input(nums) { for (int i = 0; i < len; ++i) { for (int j = i + 1; j > 0; j -= lowBit(j)) sum[i+1] += nums[j-1]; } }