|
| 1 | +#include<bits/stdc++.h> |
| 2 | + |
| 3 | +using namespace std; |
| 4 | + |
| 5 | +#define inf 1e18 |
| 6 | + |
| 7 | + |
| 8 | +int lazy[10000] = {0}; |
| 9 | +void lazyUpdate(int tree[], int s, int e, int l, int r, int val , int index) { |
| 10 | + if (lazy[index] != 0) { |
| 11 | + tree[index] += lazy[index]; |
| 12 | + if (e != s) { |
| 13 | + lazy[2 * index] += lazy[index]; |
| 14 | + lazy[2 * index + 1] += lazy[index]; |
| 15 | + } |
| 16 | + lazy[index] = 0; |
| 17 | + } |
| 18 | + |
| 19 | + if (s > r or e < l) return; |
| 20 | + |
| 21 | + if (s >= l and e <= r) { |
| 22 | + tree[index] += val; |
| 23 | + if (e != s) { |
| 24 | + lazy[2 * index] += val; |
| 25 | + lazy[2 * index + 1] += val; |
| 26 | + } |
| 27 | + return; |
| 28 | + } |
| 29 | + |
| 30 | + int mid = (s + e) / 2; |
| 31 | + lazyUpdate(tree, s, mid, l, r, val, 2 * index); |
| 32 | + lazyUpdate(tree, mid + 1, e, l, r, val, 2 * index + 1); |
| 33 | + tree[index] = min(tree[2 * index], tree[2 * index + 1]); |
| 34 | + return; |
| 35 | +} |
| 36 | + |
| 37 | +int lazyQuery(int tree[], int s, int e, int l, int r, int index) { |
| 38 | + if (lazy[index] != 0) { |
| 39 | + tree[index] += lazy[index]; |
| 40 | + if (e != s) { |
| 41 | + lazy[2 * index] = lazy[index]; |
| 42 | + lazy[2 * index + 1] = lazy[index]; |
| 43 | + } |
| 44 | + lazy[index] = 0; |
| 45 | + } |
| 46 | + |
| 47 | + if (s > r or e < l) return inf; |
| 48 | + if (s >= l and e <= r) return tree[index]; |
| 49 | + |
| 50 | + int mid = (s + e) / 2; |
| 51 | + int left = lazyQuery(tree, s, mid, l, r, 2 * index); |
| 52 | + int right = lazyQuery(tree, mid + 1, e, l, r, 2 * index + 1); |
| 53 | + return min(left, right); |
| 54 | + |
| 55 | + |
| 56 | +} |
| 57 | + |
| 58 | +void build(int a[], int tree[], int s, int e, int index) { |
| 59 | + if (s == e) { |
| 60 | + tree[index] = a[e]; |
| 61 | + return; |
| 62 | + } |
| 63 | + int mid = (s + e) / 2; |
| 64 | + build(a, tree, s, mid, 2 * index); |
| 65 | + build(a, tree, mid + 1, e, 2 * index + 1); |
| 66 | + tree[index] = min(tree[2 * index], tree[2 * index + 1]); |
| 67 | + return; |
| 68 | +} |
| 69 | + |
| 70 | + |
| 71 | +int32_t main() |
| 72 | +{ |
| 73 | + //An example of implementing this code |
| 74 | + // int a[] = {1, 3, 2, -5, 6, 4}; |
| 75 | + // int n = sizeof(a) / sizeof(int); |
| 76 | + // int tree[4 * n + 1]; |
| 77 | + // build(a, tree, 0, n - 1, 1); |
| 78 | + // lazyUpdate(tree, 0, n - 1, 0, 2, 10, 1); |
| 79 | + // return 0; |
| 80 | +} |
0 commit comments