fork download
  1. // lazy part is pending -> other code is fine
  2. class SGtree
  3. {
  4. vi seg, lazy;
  5.  
  6. public:
  7. SGtree(int n)
  8. {
  9. // Adjust deafault value (if needed) --> size = [4*arr.size()]
  10. seg.resize(4 * n + 1, 0);
  11. lazy.resize(4 * n + 1, 0);
  12. }
  13.  
  14. void build(vi &arr, int l, int r, int ind)
  15. {
  16. if (l == r)
  17. {
  18. // Change accordingly (for single element)
  19. seg[ind] = arr[l];
  20. return;
  21. }
  22.  
  23. int mid = (l + r) / 2;
  24. build(arr, l, mid, 2 * ind + 1);
  25. build(arr, mid + 1, r, 2 * ind + 2);
  26.  
  27. // Assuming operation is sum of range[l, r]
  28. seg[ind] = seg[2 * ind + 1] + seg[2 * ind + 2];
  29. }
  30.  
  31. // query on [l, r] where l and r are fixed
  32. int tell(int l, int r, int a, int b, int ind)
  33. {
  34. // Adjust deafault value of lazy (if needed)
  35. if (lazy[ind])
  36. {
  37. if (a != b)
  38. {
  39. // if (a!=b) -> not leaf -> have childs
  40. lazy[2 * ind + 1] = lazy[ind];
  41. lazy[2 * ind + 2] = lazy[ind];
  42. }
  43.  
  44. // Assuming operation is sum of range[l, r]
  45. // Update accordingly -> added val (b-a+1) times {bcz sum query on index[a...b]}
  46. seg[ind] += lazy[ind] * (b - a + 1);
  47.  
  48. lazy[ind] = 0; // Adjust deafault value of lazy (if needed)
  49. }
  50.  
  51. if (l <= a && b <= r)
  52. {
  53. return seg[ind];
  54. }
  55.  
  56. if (r < a || b < l) // NO overlap
  57. {
  58. // Return the default value
  59. return 0;
  60. }
  61.  
  62. int mid = (a + b) / 2;
  63. int x = tell(l, r, a, mid, 2 * ind + 1);
  64. int y = tell(l, r, mid + 1, b, 2 * ind + 2);
  65.  
  66. // Do the operation
  67. return x + y;
  68. }
  69.  
  70. // point update of arr[x] = val
  71. void update(int l, int r, int ind, int x, int val)
  72. {
  73. if (l == r)
  74. {
  75. seg[ind] = val;
  76. return;
  77. }
  78.  
  79. int mid = (l + r) / 2;
  80. if (x <= mid)
  81. {
  82. update(l, mid, 2 * ind + 1, x, val);
  83. }
  84. else
  85. {
  86. update(mid + 1, r, 2 * ind + 2, x, val);
  87. }
  88.  
  89. // Assuming operation is sum of range[l, r]
  90. seg[ind] = seg[2 * ind + 1] + seg[2 * ind + 2];
  91. }
  92.  
  93. // range update from {arr[l].....arr[r]} to val
  94. void lazyPropagation(int l, int r, int a, int b, int ind, int val)
  95. {
  96. if (l <= a && b <= r)
  97. {
  98. // If 2 consecutive updates on same node
  99. lazy[ind] += val;
  100. cout << lazy[ind] << " " << ind << " " << a << " " << b << endl;
  101. return;
  102. }
  103.  
  104. if (r < a || b < l) // NO overlap
  105. {
  106. return;
  107. }
  108.  
  109. int mid = (a + b) / 2;
  110. lazyPropagation(l, r, a, mid, 2 * ind + 1, val);
  111. lazyPropagation(l, r, mid + 1, b, 2 * ind + 2, val);
  112. }
  113. };
  114.  
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
prog.cpp:4:5: error: ‘vi’ does not name a type; did you mean ‘void’?
     vi seg, lazy;
     ^~
     void
prog.cpp:14:16: error: ‘vi’ has not been declared
     void build(vi &arr, int l, int r, int ind)
                ^~
prog.cpp: In constructor ‘SGtree::SGtree(int)’:
prog.cpp:10:9: error: ‘seg’ was not declared in this scope
         seg.resize(4 * n + 1, 0);
         ^~~
prog.cpp:11:9: error: ‘lazy’ was not declared in this scope
         lazy.resize(4 * n + 1, 0);
         ^~~~
prog.cpp: In member function ‘void SGtree::build(int&, int, int, int)’:
prog.cpp:19:13: error: ‘seg’ was not declared in this scope
             seg[ind] = arr[l];
             ^~~
prog.cpp:19:29: error: invalid types ‘int[int]’ for array subscript
             seg[ind] = arr[l];
                             ^
prog.cpp:28:9: error: ‘seg’ was not declared in this scope
         seg[ind] = seg[2 * ind + 1] + seg[2 * ind + 2];
         ^~~
prog.cpp: In member function ‘int SGtree::tell(int, int, int, int, int)’:
prog.cpp:35:13: error: ‘lazy’ was not declared in this scope
         if (lazy[ind])
             ^~~~
prog.cpp:46:13: error: ‘seg’ was not declared in this scope
             seg[ind] += lazy[ind] * (b - a + 1);
             ^~~
prog.cpp:53:20: error: ‘seg’ was not declared in this scope
             return seg[ind];
                    ^~~
prog.cpp: In member function ‘void SGtree::update(int, int, int, int, int)’:
prog.cpp:75:13: error: ‘seg’ was not declared in this scope
             seg[ind] = val;
             ^~~
prog.cpp:90:9: error: ‘seg’ was not declared in this scope
         seg[ind] = seg[2 * ind + 1] + seg[2 * ind + 2];
         ^~~
prog.cpp: In member function ‘void SGtree::lazyPropagation(int, int, int, int, int, int)’:
prog.cpp:99:13: error: ‘lazy’ was not declared in this scope
             lazy[ind] += val;
             ^~~~
prog.cpp:100:13: error: ‘cout’ was not declared in this scope
             cout << lazy[ind] << " " << ind << " " << a << " " << b << endl;
             ^~~~
prog.cpp:100:72: error: ‘endl’ was not declared in this scope
             cout << lazy[ind] << " " << ind << " " << a << " " << b << endl;
                                                                        ^~~~
prog.cpp:100:72: note: suggested alternative: ‘ind’
             cout << lazy[ind] << " " << ind << " " << a << " " << b << endl;
                                                                        ^~~~
                                                                        ind
stdout
Standard output is empty