-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathgraffiti-on-the-fence.cpp
103 lines (87 loc) · 2.71 KB
/
graffiti-on-the-fence.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
#include <iostream>
#include <string>
#include <map>
#include <algorithm>
using namespace std;
struct stInterval
{
int m_start;
int m_end ;
stInterval(int p_st = 0, int p_ed = 0):
m_start(p_st),
m_end (p_ed)
{ }
bool interLaps(const stInterval& p_other) const
{
return ( ( (p_other.m_end >= m_start) && (p_other.m_end <= m_end) ) ||
( (p_other.m_start >= m_start) && (p_other.m_start <= m_end) ) ||
( (p_other.m_start <= m_start) && (p_other.m_end >= m_end) ) ||
( (p_other.m_start >= m_start) && (p_other.m_end <= m_end) ) );
}
void print(void) const
{
cout << m_start << " " << m_end << endl;
}
stInterval& operator+=(const stInterval& p_other)
{
if (this == &p_other ) { return *this; }
m_start = p_other.m_start < m_start ? p_other.m_start : m_start;
m_end = p_other.m_end > m_end ? p_other.m_end : m_end ;
return *this;
}
};
int main()
{
int st , ed;
int totalLength, reportsNumber;
cin >> totalLength >> reportsNumber; cin.ignore();
stInterval curInterval;
map<int, stInterval> states;
for (int i = 0; i < reportsNumber; i++)
{
cin >> curInterval.m_start
>> curInterval.m_end; cin.ignore();
for ( auto l_it = states.begin();
l_it != states.end (); )
{
if ( l_it->second.interLaps(curInterval) )
{
curInterval += l_it->second;
l_it = states.erase(l_it);
}
else
{
++l_it;
}
}
states[ curInterval.m_start ] = curInterval;
}
if ( (states.size() == 1 ) &&
(states.find(0) != states.end() ) &&
(states.at(0).m_end == totalLength ) )
{
cout << "All painted" << endl;
}
else
{
if ( states.begin()->first > 0 )
{
cout << 0 << " " << states.begin()->first << endl;
}
for ( auto l_it = states.begin();
l_it != states.end();
l_it++ )
{
auto l_it2 = next(l_it,1);
if( (l_it2 != states.end()) &&
( l_it2->second.m_start != l_it->second.m_end) )
{
cout << l_it->second.m_end << " " << l_it2->second.m_start << endl;
}
}
if ( states.rbegin()->second.m_end < totalLength )
{
cout << states.rbegin()->second.m_end << " " << totalLength << endl;
}
}
}