kien_coi_1997

BIT 1D, BIT 2D

Jun 9th, 2014
326
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.84 KB | None | 0 0
  1. // Đây là code BIT 1D
  2. =====================
  3.  
  4. const int N=100005;
  5. int T[N];
  6.  
  7. void update(int u, int x){
  8.    for (int i=u; i<N; i+=i&-i)
  9.    T[i]+=x;
  10. }
  11.  
  12. int get(int u, int x){
  13.    int Sum = 0;
  14.    for (int i=u; i>0; i-=i&-i)
  15.    Sum += T[i];
  16.    return Sum;
  17. }
  18.  
  19. // Đây là code BIT 1D anh dùng
  20. ==============================
  21.  
  22. struct fenwick_tree{
  23.    int T[N];
  24.    void update(int u, int x)
  25.       { for (int i=u; i<N; i+=i&-i) T[i]+=x; }
  26.    int get(int u)
  27.       { int Sum=T[u]; while (u-=u&-u) Sum+=T[u]; return Sum; }
  28. } BIT;
  29.  
  30. // Đây là code BIT 2D
  31. =====================
  32.  
  33. void update(int x, int y, int k){
  34.    for (int i=x; i<N; i+=i&-i)
  35.    for (int j=y; j<N; j+=j&-j)
  36.    T[i][j]+=k;
  37. }
  38.  
  39. int get(int x, int y){
  40.    int Sum = 0;
  41.    for (int i=x; i>0; i-=i&-i)
  42.    for (int j=y; j>0; j-=j&-j)
  43.    Sum += T[i][j];
  44.    return Sum;
  45. }
Advertisement
Add Comment
Please, Sign In to add comment