-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrange.cpp
145 lines (137 loc) · 3.62 KB
/
range.cpp
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
#include <iostream>
#include <set>
#define NDEBUG
#include <cassert>
using namespace std;
class Range {
public:
int left, right;
Range(){}
Range(int i){
left = i;
right = i+1;
}
Range(int l, int r){
left = l;
right = r;
assert(l<r && "Empty set encountered");
}
Range(const Range &rhs){
left = rhs.left;
right = rhs.right;
}
~Range(){}
bool operator<(const Range &rhs)const{
return right < rhs.left;
}
//connect: intersect or touch
bool operator==(const Range &rhs)const{
return (rhs.right >= left) && (right >= rhs.left);
}
//Intersection is not empty
bool intersect(const Range &rhs)const{
return (rhs.right > left) && (right > rhs.left);
}
bool operator>(const Range &rhs)const{
return left > rhs.right;
}
//Merge rhs
const Range operator+=(const Range &rhs){
assert(*this == rhs);
if (left>rhs.left) left = rhs.left;
if (right<rhs.right) right = rhs.right;
return *this;
}
//Merge two conecting ranges
const Range operator+(const Range &rhs)const{
Range lhs(*this);
lhs += rhs;
return lhs;
}
//Test equivalence
bool equiv(const Range &rhs) const{
return left==rhs.left && right == rhs.right;
}
//Test subset
bool in(const Range &rhs) const{
return left>=rhs.left && right <= rhs.right;
}
bool in_proper(const Range &rhs) const{
return left>rhs.left && right < rhs.right;
}
int length()const{
return right-left;
}
friend ostream &operator <<(ostream& os, const Range &r){
os << "["<<r.left<<", "<<r.right<<")";
return os;
}
};
class RangeModule {
set<Range> R;
public:
void addRange(const Range &rhs) {
auto loc = R.find(rhs);
if (loc != R.end() && !rhs.in(*loc)){
int left = loc->left, right;
do {
right = loc->right;
loc = R.erase(loc);
} while(loc != R.end() && (rhs == *loc));
if (left > rhs.left) left = rhs.left;
if (right < rhs.right) right = rhs.right;
R.insert(Range(left, right));
}
else R.insert(rhs);
}
bool queryRange(const Range q) const{
auto loc = R.find(q);
return (loc != R.end()) && q.in(*loc);
}
void removeRange(const Range q) {
auto loc = R.find(q);
if (loc == R.end()) return;
if (!q.intersect(*loc)) loc++;
if (loc == R.end() || !q.intersect(*loc)) return;
int left = loc->left, right;
do {right = loc->right;
loc = R.erase(loc);
}while (loc != R.end() && q.intersect(*loc));
if (left < q.left) R.insert(Range(left, q.left));
if (right > q.right) R.insert(Range(q.right, right));
}
void addRange(int left, int right) {
this->addRange(Range(left, right));
}
bool queryRange(int left, int right) const{
return this->queryRange(Range(left, right));
}
void removeRange(int left, int right) {
this->removeRange(Range(left, right));
}
friend ostream &operator<<(ostream &os, const RangeModule &r){
for (auto i:r.R){
os<<i<<" U ";
}
os<<"\b\b "<<endl;
return os;
}
};
int main(){
Range r(3,4), s(4, 7);
RangeModule R;
R.addRange(2,5);
cout<<R;
R.addRange(6,7);
cout<<R;
R.addRange(8,10);
cout<<R;
R.addRange(4,9);
cout<<R;
R.removeRange(4,7);
cout<<R;
R.addRange(5,6);
cout<<R;
R.removeRange(4,5);
cout<<R;
}