-
Notifications
You must be signed in to change notification settings - Fork 14
/
RangeModule.java
157 lines (121 loc) · 4.2 KB
/
RangeModule.java
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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
package leetcode.segmenttree;
public class RangeModule {
public static void main(String[] args) {
RangeModule rangeModule = new RangeModule();
rangeModule.addRange(10, 20);
rangeModule.removeRange(14, 16);
rangeModule.queryRange(10, 20);
rangeModule.addRange(5, 7);
rangeModule.queryRange(5, 7);
rangeModule.addRange(6, 9);
rangeModule.queryRange(5, 9);
rangeModule.addRange(2, 7);
rangeModule.queryRange(2, 9);
rangeModule.removeRange(3, 10);
rangeModule.queryRange(2, 9);
rangeModule.removeRange(1, 8);
rangeModule.queryRange(2, 9);
rangeModule.removeRange(1, 10);
rangeModule.queryRange(2, 9);
System.out.println(rangeModule.queryRange(4, 7));
}
SegmentTree segmentTree;
public RangeModule() {
segmentTree = new SegmentTree();
}
public void addRange(int left, int right) {
segmentTree.update(1, 1, (int) 1e9 + 1, left + 1, right, 1);
}
public boolean queryRange(int left, int right) {
return segmentTree.query(1, 1, (int) 1e9 + 1, left + 1, right) == right - left;
}
public void removeRange(int left, int right) {
segmentTree.update(1, 1, (int) 1e9 + 1, left + 1, right, -1);
}
static class SegmentTree {
static class Node {
int left;
int right;
int val;
int add;
}
int count;
Node[] tree;
public SegmentTree() {
count = 1;
tree = new Node[(int) 5e6 + 1];
tree[count++] = new Node();
}
public int query(int pos, int left, int right, int l, int r) {
if (l <= left && right <= r) {
return tree[pos].val;
}
lazyCreate(pos);
pushDown(pos);
int res = 0;
int mid = left + right >> 1;
if (l <= mid) {
res += query(tree[pos].left, left, mid, l, r);
}
if (r > mid) {
res += query(tree[pos].right, mid + 1, right, l, r);
}
return res;
}
public void update(int pos, int left, int right, int l, int r, int val) {
if (l <= left && right <= r) {
if (val == 1) {
tree[pos].val = right - left + 1;
tree[pos].add = right - left + 1;
} else {
tree[pos].val = 0;
tree[pos].add = -1;
}
return;
}
lazyCreate(pos);
pushDown(pos);
int mid = left + right >> 1;
if (l <= mid) {
update(tree[pos].left, left, mid, l, r, val);
}
if (r > mid) {
update(tree[pos].right, mid + 1, right, l, r, val);
}
pushUp(pos);
}
private void pushUp(int pos) {
tree[pos].val = tree[tree[pos].left].val + tree[tree[pos].right].val;
}
private void pushDown(int pos) {
if (tree[pos].left != 0 && tree[pos].right != 0 && tree[pos].add != 0) {
int add = tree[pos].add;
if (add == -1) {
tree[tree[pos].left].val = 0;
tree[tree[pos].left].add = -1;
tree[tree[pos].right].val = 0;
tree[tree[pos].right].add = -1;
} else {
tree[tree[pos].left].val = add - add / 2;
tree[tree[pos].left].add = add - add / 2;
tree[tree[pos].right].val = add / 2;
tree[tree[pos].right].add = add / 2;
}
tree[pos].add = 0;
}
}
private void lazyCreate(int pos) {
if (tree[pos] == null) {
tree[pos] = new Node();
}
if (tree[pos].left == 0) {
tree[pos].left = count++;
tree[tree[pos].left] = new Node();
}
if (tree[pos].right == 0) {
tree[pos].right = count++;
tree[tree[pos].right] = new Node();
}
}
}
}