-
Notifications
You must be signed in to change notification settings - Fork 13
/
Solution1893.java
106 lines (80 loc) · 2.76 KB
/
Solution1893.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
package leetcode.segmenttree;
public class Solution1893 {
public boolean isCovered(int[][] ranges, int left, int right) {
SegmentTree segmentTree = new SegmentTree();
for (int[] range : ranges) {
segmentTree.update(1, range[0], range[1]);
}
return segmentTree.query(1, left, right) == (right - left + 1);
}
// 区间修改和区间查询
static class SegmentTree {
static class Node {
int left;
int right;
int val;
int add;
public Node(int left, int right) {
this.left = left;
this.right = right;
}
}
Node[] tree;
public SegmentTree() {
tree = new Node[200];
build(1, 1, 50);
}
private void build(int pos, int left, int right) {
tree[pos] = new Node(left, right);
if (left == right) {
return;
}
int mid = left + right >> 1;
build(pos << 1, left, mid);
build(pos << 1 | 1, mid + 1, right);
}
public void update(int pos, int left, int right) {
if (left <= tree[pos].left && tree[pos].right <= right) {
tree[pos].val = tree[pos].right - tree[pos].left + 1;
tree[pos].add = 1;
return;
}
pushDown(pos);
int mid = tree[pos].left + tree[pos].right >> 1;
if (left <= mid) {
update(pos << 1, left, right);
}
if (right > mid) {
update(pos << 1 | 1, left, right);
}
pushUp(pos);
}
public int query(int pos, int left, int right) {
if (left <= tree[pos].left && tree[pos].right <= right) {
return tree[pos].val;
}
pushDown(pos);
int res = 0;
int mid = tree[pos].left + tree[pos].right >> 1;
if (left <= mid) {
res += query(pos << 1, left, right);
}
if (right > mid) {
res += query(pos << 1 | 1, left, right);
}
return res;
}
private void pushDown(int pos) {
if (tree[pos].left != tree[pos].right && tree[pos].add != 0) {
tree[pos << 1].val = tree[pos << 1].right - tree[pos << 1].left + 1;
tree[pos << 1 | 1].val = tree[pos << 1 | 1].right - tree[pos << 1 | 1].left + 1;
tree[pos << 1].add = 1;
tree[pos << 1 | 1].add = 1;
tree[pos].add = 0;
}
}
private void pushUp(int pos) {
tree[pos].val = tree[pos << 1].val + tree[pos << 1 | 1].val;
}
}
}