MathQ_

Untitled

Nov 17th, 2020
146
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.65 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3.  
  4. typedef long long ll;
  5.  
  6. using namespace std;
  7.  
  8. struct fenwick {
  9.     int n, m, k;
  10.     vector<vector<vector<int>>> t;
  11.  
  12.     fenwick(int _n, int _m, int _k) {
  13.         n = _n; m = _m; k = _k;
  14.         t.resize(n, vector<vector<int>>(m, vector<int>(k, 0)));
  15.     }
  16.  
  17.     ll sum(int x, int y, int z) {
  18.         ll result = 0;
  19.         for (; x >= 0; x &= (x + 1), --x) {
  20.             for (; y >= 0; y &= (y + 1), --y) {
  21.                 for (; z >= 0; z &= (z + 1), --z) {
  22.                     result += t[x][y][z];
  23.                 }
  24.             }
  25.         }
  26.         return result;
  27.     }
  28.  
  29.     void upd(int x, int y, int z, int delta) {
  30.         for (; x < n; x |= (x + 1)) {
  31.             for (; y < m; y |= (y + 1)) {
  32.                 for (; z < k; z |= (z + 1)) {
  33.                     t[x][y][z] += delta;
  34.                 }
  35.             }
  36.         }
  37.     }
  38. };
  39.  
  40. int main() {
  41.     fast
  42.     file_in
  43. //  file_in_out
  44.    
  45.     int n;
  46.     cin >> n;
  47.     fenwick f(n, n, n);
  48.     int type, x, y, z, k, x1, y1, z1, x2, y2, z2;
  49.     while (true) {
  50.         cin >> type;
  51.         if (type == 1) {
  52.             cin >> x >> y >> z >> k;
  53.             f.upd(x, y, z, k);
  54.         }
  55.         if (type == 2) {
  56.             cin >> x1 >> y1 >> z1 >> x2 >> y2 >> z2;
  57.             ll f1 = f.sum(x2, y2, z2) + f.sum(x1 - 1, y1 - 1, z2) + f.sum(x2, y1 - 1, z1 - 1) + f.sum(x1 - 1, y2, z1 - 1);
  58.             ll f2 = f.sum(x1 - 1, y1 - 1, z1 - 1) + f.sum(x2, y1 - 1, z2) + f.sum(x1 - 1, y2, z2) +  f.sum(x2, y2, z1 - 1);
  59.             cout << f1 - f2 << "\n";
  60.         }
  61.         if (type == 3) {
  62.             break;
  63.         }
  64.     }
  65.     return 0;
  66. }
Advertisement
Add Comment
Please, Sign In to add comment