Advertisement
slash0t

BIT 2D

Nov 4th, 2024
32
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.65 KB | None | 0 0
  1. struct bit2d {
  2.     int n, m;
  3.  
  4.     vector<vector<int>> tree;
  5.  
  6.     bit2d(int n, int m) : n(n), m(m) {
  7.         tree = vector<vector<int>>(n, vector<int>(m));
  8.     }
  9.  
  10.     int sum(int x, int y) {
  11.         int res = 0;
  12.         for (int i = x; i >= 0; i = (i & (i + 1)) - 1) {
  13.             for (int j = y; j >= 0; j = (j & (j + 1)) - 1) {
  14.                 res += tree[i][j];
  15.             }
  16.         }
  17.         return res;
  18.     }
  19.  
  20.     int sum(int x1, int y1, int x2, int y2) {
  21.         x1--;
  22.         y1--;
  23.         return sum(x2, y2) - sum(x2, y1) - sum(x1, y2) + sum(x1, y1);
  24.     }
  25.  
  26.     void add(int x, int y, int dif = 1) {
  27.         for (int i = x; i < n; i = i | (i + 1)) {
  28.             for (int j = y; j < m; j = j | (j + 1)) {
  29.                 tree[i][j] += dif;
  30.             }
  31.         }
  32.     }
  33. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement