Advertisement
CyberN00b

Fenwik Tree 3D

May 26th, 2020 (edited)
234
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.07 KB | None | 0 0
  1. #include<bits/stdc++.h>
  2. #define spc << ' ' <<
  3. #define spce << ' '
  4. #define ele << '\n'
  5. #define el << '\n' <<
  6. #define INF 99999
  7. #define AINF -99999
  8. using namespace std;
  9. template<typename tmp>
  10. void output(vector<tmp>& vc, char en) {
  11.     for (tmp x : vc)
  12.         cout x << en;
  13. }
  14. template<typename tmp>
  15. void input(vector<tmp>& vc) {
  16.     for (tmp& x : vc)
  17.         cin >> x;
  18. }
  19. int f(int a) {
  20.     return (a & (a + 1));
  21. }
  22. int sum(int x, int y, int z, vector<vector<vector<int> > >& t) {
  23.     int res = 0;
  24.     while (x >= 0) {
  25.             int Y = y;
  26.             while (Y >= 0) {
  27.                     int Z = z;
  28.                     while (Z >= 0) {
  29.                         res += t[x][Y][Z];
  30.                         Z = f(Z) - 1;
  31.                     }
  32.                 Y = f(Y) - 1;
  33.             }
  34.         x = f(x) - 1;
  35.     }
  36.     return res;
  37. }
  38. void update(int x, int y, int z, int d, vector<vector<vector<int> > >& t) {
  39.     while (x <= t.size()) {
  40.             int Y = y;
  41.             while (Y <= t.size()) {
  42.                 int Z = z;
  43.                 while (Z <= t.size()) {
  44.                     t[x][Y][Z] += d;
  45.                     Z = Z | (Z + 1);
  46.                 }
  47.                 Y = Y | (Y + 1);
  48.             }
  49.         x = x | (x + 1);
  50.     }
  51. }
  52. int sum(int x1, int x2, int y1, int y2, int z1, int z2, vector<vector<vector<int> > >& t) {
  53.     return sum(x1, y1, z1, t) - sum(x1, y1, z2 - 1, t) - sum(x2 - 1, y1, z1, t) + sum(x2 - 1, y1, z2 - 1, t) - sum(x1, y2 - 1, z1, t) + sum(x1, y2 - 1, z2 - 1, t) + sum(x2 - 1, y2 - 1, z1, t) - sum(x2 - 1, y2 - 1, z2 - 1, t);
  54. }
  55. int main() {
  56.     int a;
  57.     cin >> a;
  58.     if (a % 2)
  59.         ++a;
  60.     vector<vector<vector<int> > > t(a, vector<vector<int>>(a, vector<int>(a, 0)));
  61.     int tmp1, x1, x2, y1, y2, z1, z2, d;
  62.     cin >> tmp1;
  63.     while (tmp1 - 3) {
  64.         if (tmp1 - 1) {
  65.             cin >> x2 >> y2 >> z2 >> x1 >> y1 >> z1;
  66.             cout << sum(x1, x2, y1, y2, z1, z2, t) ele;
  67.         } else {
  68.             cin >> x1 >> y1 >> z1 >> d;
  69.             update(x1, y1, z1, d, t);
  70.         }
  71.         cin >> tmp1;
  72.     }
  73. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement