bhupixb

lazy implementation

Jan 9th, 2019
124
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.96 KB | None | 0 0
  1. // I can't tell you what it really is,
  2. // I can only tell you what it feels like.
  3. #include "bits/stdc++.h"
  4.  
  5. using namespace std;
  6.  
  7. #define all(v) v.begin(), v.end()
  8. #define pb push_back
  9. #define P pair < int, int >
  10. #define fast ios_base::sync_with_stdio(0);cin.tie(NULL);cout.tie(NULL);
  11. #define reno(args...) { string _s = #args; replace(_s.begin(), _s.end(), ',', ' '); stringstream _ss(_s); istream_iterator<string> _it(_ss); err(_it, args); }
  12. void err(istream_iterator<string> it) {}
  13.  
  14. template<typename T, typename... Args>
  15. void err(istream_iterator<string> it, T a, Args... args) {
  16. cout << *it << " = " << a << ' ';
  17. err(++it, args...);
  18. }
  19.  
  20. const int N = 1e6 + 5;
  21. int sum[N], arr[N], lazy[N<<2], tree[N<<2];
  22.  
  23. void build(int l, int r, int x) {
  24. if(l == r)
  25. tree[x] = sum[l];
  26. else {
  27. int mid = l + r >> 1, lc = x + x, rc = lc + 1;
  28. build(l, mid, lc); build(mid + 1, r, rc);
  29.  
  30. tree[x] = min(tree[lc], tree[rc]);
  31. }
  32. }
  33. void prop(int l, int r, int x) {
  34. if(lazy[x]) {
  35. tree[x] += lazy[x];
  36. if(l ^ r) {
  37. int lc = x + x, rc = lc + 1;
  38. lazy[lc] += lazy[x];
  39. lazy[rc] += lazy[x];
  40. }
  41. lazy[x] = 0;
  42. }
  43. }
  44. void update(int l, int r, int ql, int qr, int val, int x) {
  45. prop(l, r, x);
  46. if(qr < l || r < ql)
  47. return;
  48.  
  49. if(ql <= l && r <= qr) {
  50. lazy[x] = val;
  51. //reno(l, r, x, val);cout << '\n';
  52. prop(l, r, x);
  53. return;
  54. }
  55.  
  56. int mid = l + r >> 1, lc = x + x, rc = lc + 1;
  57.  
  58. update(l, mid, ql, qr, val, lc);
  59. update(mid + 1, r, ql, qr, val, rc);
  60.  
  61. tree[x] = min(tree[lc], tree[rc]);
  62. }
  63. int query(int l, int r, int ql, int qr, int x) {
  64. if(qr < l || r < ql)
  65. return 2e9;
  66. prop(l, r, x);
  67. if(ql <= l and r <= qr) {
  68. prop(l, r, x);
  69. return tree[x];
  70. }
  71. int mid = l + r >> 1, lc = x + x, rc = lc + 1;
  72.  
  73. return min(query(l, mid, ql, qr, lc), query(mid + 1, r, ql, qr, rc));
  74. }
  75. signed main(){
  76. fast;
  77. int n, let, ans(0); string s;
  78. cin >> n;
  79. build(1, n, 1);
  80.  
  81.  
  82. cout << ans;
  83. return 0;
  84. }
Add Comment
Please, Sign In to add comment