fork download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. const int MAXR = 52;
  5. const int MAXD = 360;
  6. const int MAXS = 181;
  7.  
  8. int fenw[MAXR+1][MAXD+1][MAXS+1];
  9.  
  10. // Funkcja konwersji liter R na indeks 1..52
  11. int rIndex(char c) {
  12. if ('A' <= c && c <= 'Z') return c - 'A' + 1;
  13. return c - 'a' + 27;
  14. }
  15.  
  16. // Funkcja konwersji string D (3 cyfry) na liczbę
  17. int dIndex(const string &d) {
  18. return stoi(d);
  19. }
  20.  
  21. // Funkcja konwersji S (+/- i 2 cyfry) na indeks 1..181
  22. int sIndex(const string &s) {
  23. int val = stoi(s.substr(1));
  24. if (s[0] == '+') return val + 90;
  25. return 90 - val + 1; // np. -90 -> 1, -01 -> 90
  26. }
  27.  
  28. // Fenwick Tree 3D - update
  29. void update(int r, int d, int s, int val) {
  30. for (int i = r; i <= MAXR; i += i & (-i))
  31. for (int j = d; j <= MAXD; j += j & (-j))
  32. for (int k = s; k <= MAXS; k += k & (-k))
  33. fenw[i][j][k] += val;
  34. }
  35.  
  36. // Fenwick Tree 3D - query prefix sum
  37. int query(int r, int d, int s) {
  38. int res = 0;
  39. for (int i = r; i > 0; i -= i & (-i))
  40. for (int j = d; j > 0; j -= j & (-j))
  41. for (int k = s; k > 0; k -= k & (-k))
  42. res += fenw[i][j][k];
  43. return res;
  44. }
  45.  
  46. // Suma w zakresie
  47. int rangeSum(int r1, int d1, int s1, int r2, int d2, int s2) {
  48. return query(r2,d2,s2) - query(r1-1,d2,s2) - query(r2,d1-1,s2) - query(r2,d2,s1-1)
  49. + query(r1-1,d1-1,s2) + query(r1-1,d2,s1-1) + query(r2,d1-1,s1-1) - query(r1-1,d1-1,s1-1);
  50. }
  51.  
  52. int main() {
  53. ios::sync_with_stdio(false);
  54. cin.tie(nullptr);
  55.  
  56. string line;
  57. while (getline(cin, line)) {
  58. if (line.empty()) continue;
  59. stringstream ss(line);
  60. int type; ss >> type;
  61. if (type == 1 || type == 2) {
  62. string RDS; long long W; ss >> RDS >> W;
  63. int r = rIndex(RDS[0]);
  64. int d = dIndex(RDS.substr(1,3));
  65. int s = sIndex(RDS.substr(4,3));
  66. int val = (type == 1) ? W : -W;
  67. update(r, d, s, val);
  68. } else if (type == 3) {
  69. string R1, R2; long long W; ss >> R1 >> R2 >> W;
  70. int r1 = rIndex(R1[0]), d1 = dIndex(R1.substr(1,3)), s1 = sIndex(R1.substr(4,3));
  71. int r2 = rIndex(R2[0]), d2 = dIndex(R2.substr(1,3)), s2 = sIndex(R2.substr(4,3));
  72. update(r1, d1, s1, -W);
  73. update(r2, d2, s2, W);
  74. } else if (type == 4) {
  75. string R1, R2; ss >> R1 >> R2;
  76. int r1 = rIndex(R1[0]), d1 = dIndex(R1.substr(1,3)), s1 = sIndex(R1.substr(4,3));
  77. int r2 = rIndex(R2[0]), d2 = dIndex(R2.substr(1,3)), s2 = sIndex(R2.substr(4,3));
  78. if (r1 > r2) swap(r1, r2);
  79. if (d1 > d2) swap(d1, d2);
  80. if (s1 > s2) swap(s1, s2);
  81. cout << rangeSum(r1, d1, s1, r2, d2, s2) << "\n";
  82. }
  83. }
  84. return 0;
  85. }
Success #stdin #stdout 0.01s 11904KB
stdin
1 A005+90 1000
1 C003-40 2000
4 A001-90 D010+90
2 C003-40 500
4 B002-55 c003+90
3 A005+90 x359+89 999
4 B001-90 z360+90
stdout
3000
1500
2499