Guest User

Untitled

a guest
Feb 24th, 2018
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.06 KB | None | 0 0
  1. // Binary Indexed Tree
  2. // 0-indexed
  3. template <typename T>
  4. class BIT {
  5. private:
  6. vector<T> bit;
  7. // 単位元
  8. int neutral = 0;
  9. // 更新クエリ, 区間クエリ
  10. function<T(T,T)> f = [](const T l, const T r) { return l+r; };
  11. function<T(T,T)> g = [](const T l, const T r) { return l+r; };
  12. public:
  13. // 初期化
  14. BIT(int neu = 0,
  15. function<T(T,T)> _f = [](const T l, const T r) { return l+r; },
  16. function<T(T,T)> _g = [](const T l, const T r) { return l+r; },
  17. int n = 1e5)
  18. {
  19. init(neu, _f, _g, n);
  20. }
  21. void init(int neu = 0,
  22. function<T(T,T)> _f = [](const T l, const T r) { return l+r; },
  23. function<T(T,T)> _g = [](const T l, const T r) { return l+r; },
  24. int n = 1e5)
  25. {
  26. neutral = neu; f = _f; g = _g;
  27. bit.assign(n+5, neutral);
  28. }
  29. // iに対する点更新クエリ
  30. void update(int i, T w) {
  31. for(int x = i+1; x < bit.size(); x += x&-x) bit[x] = f(bit[x], w);
  32. }
  33. // [0,i)に対する区間クエリ
  34. T query(int i) {
  35. T ret = neutral;
  36. for(int x = i+1; x > 0; x -= x & -x) ret = g(ret, bit[x]);
  37. return ret;
  38. }
  39. };
Add Comment
Please, Sign In to add comment