1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75
| class RangeModule { class Node { Node l, r; int val, lazy; } Node root = new Node(); void pushDown(Node cur, int len) { if (cur.l == null) cur.l = new Node(); if (cur.r == null) cur.r = new Node(); if (cur.lazy == 0) return; else if (cur.lazy == 1) { cur.l.val = (len - len/2); cur.r.val = len/2; } else if (cur.lazy == -1) { cur.l.val = 0; cur.r.val = 0; } cur.l.lazy = cur.lazy; cur.r.lazy = cur.lazy; cur.lazy = 0; } void pushUp(Node cur) { cur.val = cur.l.val + cur.r.val; } void upd(Node cur, int l, int r, int tl, int tr, int val) { if (tl <= l && r <= tr) { cur.val = val == 1 ? r - l + 1 : 0; cur.lazy = val; } else { pushDown(cur, r-l+1); int m = l + (r-l)/2; if (tl <= m) upd(cur.l, l, m, tl, tr, val); if (m+1 <= tr) upd(cur.r, m+1, r, tl, tr, val); pushUp(cur); } } int query(Node cur, int l, int r, int tl, int tr) { if (tl <= l && r <= tr) { return cur.val; } else { pushDown(cur, r-l+1); int m = l + (r-l)/2, ans = 0; if (tl <= m) ans += query(cur.l, l, m, tl, tr); if (m+1 <= tr) ans += query(cur.r, m+1, r, tl, tr); pushUp(cur); return ans; } } public RangeModule() {
} int L = 1, R = (int)1e9; public void addRange(int left, int right) { upd(root, L, R, left, right-1, 1); } public boolean queryRange(int left, int right) { return query(root, L, R, left, right-1) == right - left; } public void removeRange(int left, int right) { upd(root, L, R, left, right-1, -1); } }
|