|
| 1 | +#include <vector> |
| 2 | +#include <iostream> |
| 3 | +#include <algorithm> |
| 4 | +using namespace std; |
| 5 | + |
| 6 | +#define REP(i,n) for(int i=0;i<(n);++i) |
| 7 | +#define FOR(i,a,b) for(int i=(a);i<=(b);++i) |
| 8 | +#define RFOR(i,a,b) for(int i=(a);i>=(b);--i) |
| 9 | +typedef long long LL; |
| 10 | + |
| 11 | +struct Interval { |
| 12 | + int start; |
| 13 | + int end; |
| 14 | + Interval() : start(0), end(0) {} |
| 15 | + Interval(int s, int e) : start(s), end(e) {} |
| 16 | +}; |
| 17 | + |
| 18 | +class Solution { |
| 19 | +public: |
| 20 | + vector<Interval> insert(vector<Interval> &intervals, Interval newInterval) { |
| 21 | + vector<Interval> res; |
| 22 | + int n = intervals.size(); |
| 23 | + |
| 24 | + int lt, rt, mt; |
| 25 | + |
| 26 | + lt = 0, rt = n - 1; |
| 27 | + while (lt <= rt) { |
| 28 | + mt = (lt + rt) / 2; |
| 29 | + if (intervals[mt].end >= newInterval.start) rt = mt - 1; |
| 30 | + else lt = mt + 1; |
| 31 | + } |
| 32 | + |
| 33 | + int idx1 = rt; |
| 34 | + FOR(i,0,idx1) res.push_back(intervals[i]); |
| 35 | + |
| 36 | + lt = rt + 1, rt = n - 1; |
| 37 | + while (lt <= rt) { |
| 38 | + mt = (lt + rt) / 2; |
| 39 | + if (intervals[mt].start <= newInterval.end) lt = mt + 1; |
| 40 | + else rt = mt - 1; |
| 41 | + } |
| 42 | + |
| 43 | + int idx2 = lt; |
| 44 | + if (idx2 - idx1 >= 2) { |
| 45 | + newInterval.start = min(newInterval.start, intervals[idx1 + 1].start); |
| 46 | + newInterval.end = max(newInterval.end, intervals[idx2 - 1].end); |
| 47 | + } |
| 48 | + res.push_back(newInterval); |
| 49 | + FOR(i,idx2,n-1) res.push_back(intervals[i]); |
| 50 | + |
| 51 | + return res; |
| 52 | + } |
| 53 | +}; |
| 54 | + |
| 55 | +int main() { |
| 56 | + Solution s; |
| 57 | + |
| 58 | + vector<Interval> mm; |
| 59 | + mm.push_back(Interval(1, 3)); |
| 60 | + mm.push_back(Interval(6, 9)); |
| 61 | + |
| 62 | + vector<Interval> ret = s.insert(mm, Interval(2, 5)); |
| 63 | + REP(i,ret.size()) cout << "[" << ret[i].start << "," << ret[i].end << "] "; |
| 64 | + cout << endl; |
| 65 | + |
| 66 | + mm.clear(); |
| 67 | + mm.push_back(Interval(1, 2)); |
| 68 | + mm.push_back(Interval(3, 5)); |
| 69 | + mm.push_back(Interval(6, 7)); |
| 70 | + mm.push_back(Interval(8, 10)); |
| 71 | + mm.push_back(Interval(12, 16)); |
| 72 | + |
| 73 | + ret = s.insert(mm, Interval(4, 9)); |
| 74 | + REP(i,ret.size()) cout << "[" << ret[i].start << "," << ret[i].end << "] "; |
| 75 | + cout << endl; |
| 76 | + |
| 77 | + mm.clear(); |
| 78 | + |
| 79 | + ret = s.insert(mm, Interval(4, 9)); |
| 80 | + REP(i,ret.size()) cout << "[" << ret[i].start << "," << ret[i].end << "] "; |
| 81 | + cout << endl; |
| 82 | + |
| 83 | + mm.clear(); |
| 84 | + mm.push_back(Interval(1, 3)); |
| 85 | + |
| 86 | + ret = s.insert(mm, Interval(4, 9)); |
| 87 | + REP(i,ret.size()) cout << "[" << ret[i].start << "," << ret[i].end << "] "; |
| 88 | + cout << endl; |
| 89 | + |
| 90 | + mm.clear(); |
| 91 | + mm.push_back(Interval(1, 2)); |
| 92 | + mm.push_back(Interval(3, 5)); |
| 93 | + mm.push_back(Interval(6, 7)); |
| 94 | + mm.push_back(Interval(8, 10)); |
| 95 | + mm.push_back(Interval(12, 16)); |
| 96 | + |
| 97 | + ret = s.insert(mm, Interval(17, 19)); |
| 98 | + REP(i,ret.size()) cout << "[" << ret[i].start << "," << ret[i].end << "] "; |
| 99 | + cout << endl; |
| 100 | + |
| 101 | + mm.clear(); |
| 102 | + mm.push_back(Interval(1, 2)); |
| 103 | + mm.push_back(Interval(3, 5)); |
| 104 | + mm.push_back(Interval(6, 7)); |
| 105 | + mm.push_back(Interval(8, 10)); |
| 106 | + mm.push_back(Interval(12, 16)); |
| 107 | + |
| 108 | + ret = s.insert(mm, Interval(-14, -9)); |
| 109 | + REP(i,ret.size()) cout << "[" << ret[i].start << "," << ret[i].end << "] "; |
| 110 | + cout << endl; |
| 111 | + |
| 112 | + return 0; |
| 113 | +} |
0 commit comments