-
Notifications
You must be signed in to change notification settings - Fork 288
/
AC_simulation_nm.cpp
70 lines (63 loc) · 1.78 KB
/
AC_simulation_nm.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
/*
* Author: illuz <iilluzen[at]gmail.com>
* File: AC_simulation_nm.cpp
* Create Date: 2015-01-23 10:21:53
* Descripton: Time: O(n1+n2+..)
*/
#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 {
private:
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;
}
public:
vector<Interval> merge(vector<Interval> &intervals) {
vector<Interval> res;
for (auto &i : intervals)
insert(res, i);
return res;
}
};
int main() {
int a, b, n;
vector<Interval> itv;
Solution s;
cin >> n;
while (n--) {
cin >> a >> b;
itv.push_back(Interval(a, b));
}
itv = s.merge(itv);
for (auto &i : itv)
cout << i.start << ' ' << i.end << endl;
return 0;
}