Templates -- Meow  1.1.4
A C++ template which is unable and also not allowed to compile to obj-file first.
BinaryIndexTree.h
Go to the documentation of this file.
1 #ifndef dsa_BinaryIndexTree_H__
2 #define dsa_BinaryIndexTree_H__
3 
4 #include <cstdlib>
5 
6 #include <vector>
7 #include <algorithm>
8 
9 namespace meow {
10 
11 template<class Value>
22 private:
23  std::vector<Value> array_;
24 public:
29  }
30 
37  BinaryIndexTree(size_t size, Value const& value):
38  array_(size, value) {
39  }
40 
48  array_(tree2.array_) {
49  }
50 
60  void reset(size_t size, Value const& init) {
61  array_.clear();
62  array_.resize(size, init);
63  }
64 
74  void update(size_t index, Value const& value) {
75  index++;
76  for ( ; index <= array_.size(); index += (index & -index)) {
77  array_[index - 1] = array_[index - 1] + value;
78  }
79  }
80 
81 
90  Value query(ssize_t index) const {
91  index = std::min(index + 1, (ssize_t)array_.size());
92  Value ret(0);
93  for ( ; 0 < index; index -= (index & -index)) {
94  ret = ret + array_[index - 1];
95  }
96  return ret;
97  }
98 };
99 
100 }
101 
102 #endif // dsa_BinaryIndexTree_H__
void reset(size_t size, Value const &init)
將資料洗掉, 重設
極度簡化的 SegmentTree 已無區間更新的操作
BinaryIndexTree()
constructor
BinaryIndexTree(size_t size, Value const &value)
constructor
Value query(ssize_t index) const
詢問 0~index 的區間值
void update(size_t index, Value const &value)
將array中第 index (從零算起)個element多加上指定的值
BinaryIndexTree(BinaryIndexTree const &tree2)
constructor