-
Notifications
You must be signed in to change notification settings - Fork 288
/
Copy pathAC_simulation_n.cpp
64 lines (57 loc) · 1.63 KB
/
AC_simulation_n.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
/*
* Author: illuz <iilluzen[at]gmail.com>
* File: AC_simulation_n.cpp
* Create Date: 2015-01-23 09:05:46
* Descripton: simulation
*/
#include <bits/stdc++.h>
using namespace std;
const int N = 0;
struct Interval {
int start;
int end;
Interval() : start(0), end(0) { }
Interval(int s, int e) : start(s), end(e) { }
};
class Solution {
public:
vector<Interval> insert(vector<Interval> &intervals, Interval newInterval) {
auto it = intervals.begin();
auto it_head = it; // head of erase items
while (it != intervals.end()) {
if (newInterval.end < it->start) {
it = intervals.erase(it_head, it);
intervals.insert(it, newInterval);
return intervals;
} else if (newInterval.start > it->end) {
it++;
it_head++;
} else {
newInterval.start = min(newInterval.start, it->start);
newInterval.end = max(newInterval.end, it->end);
// not erase immediately
// it = intervals.erase(it);
it++;
}
}
if (it_head != it)
it = intervals.erase(it_head, it);
intervals.insert(intervals.end(), newInterval);
return intervals;
}
};
int main() {
int a, b, n;
vector<Interval> itv;
Solution s;
cin >> n;
while (n--) {
cin >> a >> b;
itv.push_back(Interval(a, b));
}
cin >> a >> b;
itv = s.insert(itv, Interval(a, b));
for (auto &i : itv)
cout << i.start << ' ' << i.end << endl;
return 0;
}