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 76 77 78 79 80 81 82 83 84 85 86 87 88
| class Solution { class Node { Node l, r; int len; long val, lazy; } Node root = new Node(); void pushDown(Node cur, int l, int r) { int m = l + (r-l)/2; if (cur.l == null) { cur.l = new Node(); cur.l.len = m - l + 1; } if (cur.r == null) { cur.r = new Node(); cur.r.len = r - m; } if (cur.lazy == 1) { cur.l.lazy = 1 - cur.l.lazy; cur.r.lazy = 1 - cur.r.lazy; cur.l.val = cur.l.len - cur.l.val; cur.r.val = cur.r.len - cur.r.val; cur.lazy = 1 - cur.lazy; } } void pushUp(Node cur) { cur.val = cur.l.val + cur.r.val; } void upd(Node cur, int l, int r, int tl, int tr) { if (tl <= l && r <= tr) { cur.val = r - l + 1 - cur.val; cur.lazy = 1 - cur.lazy; } else { pushDown(cur, l, r); int m = l + (r-l)/2; if (tl <= m) upd(cur.l, l, m, tl, tr); if (m+1 <= tr) upd(cur.r, m+1, r, tl, tr); pushUp(cur); } } long query(Node cur, int l, int r, int tl, int tr) { if (tl <= l && r <= tr) { return cur.val; } else { pushDown(cur, l, r); int m = l + (r-l)/2, res = 0; if (tl <= m) res += query(cur.l, l, m, tl, tr); if (m+1 <= tr) res += query(cur.r, m+1, r, tl, tr); pushUp(cur); return res; } } void build(Node cur, int l, int r, int[] nums) { if (l == r) { cur.val = nums[l]; } else { pushDown(cur, l, r); int m = l + (r - l) / 2; build(cur.l, l, m, nums); build(cur.r, m + 1, r, nums); pushUp(cur); } } public long[] handleQuery(int[] nums1, int[] nums2, int[][] queries) { List<Long> list = new ArrayList<>(); int n = nums1.length; build(root, 0, n-1, nums1); long sum = 0; for (int num : nums2) sum += num; for (int[] q : queries) { if (q[0] == 1) { upd(root, 0, n-1, q[1], q[2]); } else if (q[0] == 2) { sum += q[1] * query(root, 0, n-1, 0, n-1); } else { list.add(sum); } } long[] res = new long[list.size()]; for (int i = 0; i < list.size(); i++) res[i] = list.get(i); return res; } }
|