#include <iostream> #include <map> #include <memory> #ifdef DEBUG #define DEBUG_LOG(x) do { \ std::cerr << x << std::endl;\ } while (0) #else #define DEBUG_LOG(x) do { \ } while (0) #endif using Color_t = uint8_t; Color_t const k_white = 0; Color_t const k_yellow = 1; //001 Color_t const k_blue = 2; //010 Color_t const k_green = 3; //011 Color_t const k_red = 4; //100 Color_t const k_orange = 5; //101 Color_t const k_violet = 6; //110 Color_t const k_brown= 7; //111 std::map<Color_t, std::string> k_colMap = { {k_white, "white"}, {k_yellow, "yellow"}, {k_blue, "blue"}, {k_green, "green"}, {k_red, "red"}, {k_orange, "orange"}, {k_violet, "violet"}, {k_brown, "brown"} }; Color_t MixColors(Color_t c1, Color_t c2) { return c1 | c2; } struct Range_t { uint64_t start; uint64_t len; Color_t color; Range_t(uint64_t s = 0, uint64_t l = 0, Color_t c = k_white) : start(s) , len(l) , color(c) {} friend std::ostream& operator<<(std::ostream& stream, Range_t const& r) { stream << r.start << ", "; stream << r.len << ", "; stream << k_colMap[r.color]; return stream; } uint64_t GetEnd() const { return start + len; } }; using RangePtr_t = std::unique_ptr<Range_t>; using RangeMap_t = std::map<uint64_t, RangePtr_t>; using RangeMapIt_t = RangeMap_t::iterator; RangePtr_t MakeRange(uint64_t s = 0, uint64_t l = 0, Color_t c = k_white) { return std::make_unique<Range_t>(s,l,c); } class RangeMap { public: RangeMap() = default; RangeMap(RangeMap const& other) = delete; virtual ~RangeMap() = default; void UpdatePreviousWhenIntersected_( RangeMapIt_t& prev, RangeMapIt_t& curr, uint64_t pStart, uint64_t pEnd, uint64_t cStart, uint64_t cEnd) { Color_t cColor = curr->second->color; Color_t pColor = prev->second->color; Color_t mix = MixColors(cColor, pColor); if (cEnd > pEnd) { curr->second->color = mix; curr->second->len = pEnd - cStart; Insert_(MakeRange(pEnd, cEnd-pEnd, cColor)); } else if (pEnd > cEnd) { curr->second->color = mix; curr->second->len = cEnd - cStart; Insert_(MakeRange(cEnd, pEnd-cEnd, pColor)); } else { curr->second->color = mix; curr->second->len = cEnd - cStart; } prev->second->len = cStart - pStart; } void UpdatePrevious_(RangeMapIt_t& prev, RangeMapIt_t& curr) { uint64_t pStart = prev->second->start; uint64_t pEnd = prev->second->GetEnd(); uint64_t cStart = curr->second->start; uint64_t cEnd = curr->second->GetEnd(); if (cStart >= pEnd) { return; } else { UpdatePreviousWhenIntersected_( prev, curr, pStart, pEnd, cStart, cEnd); } } void UpdateExisting_(RangeMapIt_t& ex, RangePtr_t && curr) { uint64_t eEnd = ex->second->GetEnd(); uint64_t cEnd = curr->GetEnd(); Color_t mix = MixColors(ex->second->color, curr->color); if (cEnd > eEnd) { curr->start = eEnd; curr->len = cEnd - eEnd; Insert_(std::move(curr)); } else if (eEnd > cEnd) { curr->start = cEnd; curr->len = eEnd - cEnd; curr->color = ex->second->color; Insert_(std::move(curr)); } ex->second->color = mix; } void Insert(RangePtr_t && range) { Insert_(std::move(range)); } void Insert_(RangePtr_t && range) { auto backup = MakeRange(range->start, range->len, range->color); auto insRes = ranges.emplace(range->start, std::move(range)); if (insRes.second) { if (insRes.first != std::begin(ranges)) { auto prev = std::prev(insRes.first); UpdatePrevious_(prev, insRes.first); } auto next = std::next(insRes.first); if (next != std::end(ranges)) { UpdatePrevious_(insRes.first, next); } } else { UpdateExisting_(insRes.first, std::move(backup)); } } uint64_t GetGreen() { uint64_t result = 0; for(auto const& el : ranges) { if (el.second->color == k_green) { result += el.second->len; } } return result; } friend std::ostream& operator<<(std::ostream& stream, RangeMap const& rm) { for (auto const& elem : rm.ranges) { stream << "{"<< elem.first << ": {" << *(elem.second) << "}\n"; } return stream; } private: RangeMap_t ranges{}; }; Color_t IntToColor(uint64_t c) { if (c == 1) return k_yellow; if (c == 2) return k_blue; if (c == 3) return k_red; return k_white; } void Run(std::istream& input, std::ostream& output) { uint64_t total = 0; uint64_t number = 0; RangeMap rm{}; input >> total; input >> number; for (uint64_t i = 0; i < number; i++) { uint64_t start = 0; uint64_t end = 0; uint64_t color=0; input >> start; input >> end; input >> color; --start; rm.Insert(MakeRange(start, end-start, IntToColor(color))); } output << rm.GetGreen() << std::endl; } #ifndef DEBUG int main(int argc, char* argv[]) #else int main_(int argc, char* argv[]) #endif { ::Run(std::cin, std::cout); return 0; }
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 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 | #include <iostream> #include <map> #include <memory> #ifdef DEBUG #define DEBUG_LOG(x) do { \ std::cerr << x << std::endl;\ } while (0) #else #define DEBUG_LOG(x) do { \ } while (0) #endif using Color_t = uint8_t; Color_t const k_white = 0; Color_t const k_yellow = 1; //001 Color_t const k_blue = 2; //010 Color_t const k_green = 3; //011 Color_t const k_red = 4; //100 Color_t const k_orange = 5; //101 Color_t const k_violet = 6; //110 Color_t const k_brown= 7; //111 std::map<Color_t, std::string> k_colMap = { {k_white, "white"}, {k_yellow, "yellow"}, {k_blue, "blue"}, {k_green, "green"}, {k_red, "red"}, {k_orange, "orange"}, {k_violet, "violet"}, {k_brown, "brown"} }; Color_t MixColors(Color_t c1, Color_t c2) { return c1 | c2; } struct Range_t { uint64_t start; uint64_t len; Color_t color; Range_t(uint64_t s = 0, uint64_t l = 0, Color_t c = k_white) : start(s) , len(l) , color(c) {} friend std::ostream& operator<<(std::ostream& stream, Range_t const& r) { stream << r.start << ", "; stream << r.len << ", "; stream << k_colMap[r.color]; return stream; } uint64_t GetEnd() const { return start + len; } }; using RangePtr_t = std::unique_ptr<Range_t>; using RangeMap_t = std::map<uint64_t, RangePtr_t>; using RangeMapIt_t = RangeMap_t::iterator; RangePtr_t MakeRange(uint64_t s = 0, uint64_t l = 0, Color_t c = k_white) { return std::make_unique<Range_t>(s,l,c); } class RangeMap { public: RangeMap() = default; RangeMap(RangeMap const& other) = delete; virtual ~RangeMap() = default; void UpdatePreviousWhenIntersected_( RangeMapIt_t& prev, RangeMapIt_t& curr, uint64_t pStart, uint64_t pEnd, uint64_t cStart, uint64_t cEnd) { Color_t cColor = curr->second->color; Color_t pColor = prev->second->color; Color_t mix = MixColors(cColor, pColor); if (cEnd > pEnd) { curr->second->color = mix; curr->second->len = pEnd - cStart; Insert_(MakeRange(pEnd, cEnd-pEnd, cColor)); } else if (pEnd > cEnd) { curr->second->color = mix; curr->second->len = cEnd - cStart; Insert_(MakeRange(cEnd, pEnd-cEnd, pColor)); } else { curr->second->color = mix; curr->second->len = cEnd - cStart; } prev->second->len = cStart - pStart; } void UpdatePrevious_(RangeMapIt_t& prev, RangeMapIt_t& curr) { uint64_t pStart = prev->second->start; uint64_t pEnd = prev->second->GetEnd(); uint64_t cStart = curr->second->start; uint64_t cEnd = curr->second->GetEnd(); if (cStart >= pEnd) { return; } else { UpdatePreviousWhenIntersected_( prev, curr, pStart, pEnd, cStart, cEnd); } } void UpdateExisting_(RangeMapIt_t& ex, RangePtr_t && curr) { uint64_t eEnd = ex->second->GetEnd(); uint64_t cEnd = curr->GetEnd(); Color_t mix = MixColors(ex->second->color, curr->color); if (cEnd > eEnd) { curr->start = eEnd; curr->len = cEnd - eEnd; Insert_(std::move(curr)); } else if (eEnd > cEnd) { curr->start = cEnd; curr->len = eEnd - cEnd; curr->color = ex->second->color; Insert_(std::move(curr)); } ex->second->color = mix; } void Insert(RangePtr_t && range) { Insert_(std::move(range)); } void Insert_(RangePtr_t && range) { auto backup = MakeRange(range->start, range->len, range->color); auto insRes = ranges.emplace(range->start, std::move(range)); if (insRes.second) { if (insRes.first != std::begin(ranges)) { auto prev = std::prev(insRes.first); UpdatePrevious_(prev, insRes.first); } auto next = std::next(insRes.first); if (next != std::end(ranges)) { UpdatePrevious_(insRes.first, next); } } else { UpdateExisting_(insRes.first, std::move(backup)); } } uint64_t GetGreen() { uint64_t result = 0; for(auto const& el : ranges) { if (el.second->color == k_green) { result += el.second->len; } } return result; } friend std::ostream& operator<<(std::ostream& stream, RangeMap const& rm) { for (auto const& elem : rm.ranges) { stream << "{"<< elem.first << ": {" << *(elem.second) << "}\n"; } return stream; } private: RangeMap_t ranges{}; }; Color_t IntToColor(uint64_t c) { if (c == 1) return k_yellow; if (c == 2) return k_blue; if (c == 3) return k_red; return k_white; } void Run(std::istream& input, std::ostream& output) { uint64_t total = 0; uint64_t number = 0; RangeMap rm{}; input >> total; input >> number; for (uint64_t i = 0; i < number; i++) { uint64_t start = 0; uint64_t end = 0; uint64_t color=0; input >> start; input >> end; input >> color; --start; rm.Insert(MakeRange(start, end-start, IntToColor(color))); } output << rm.GetGreen() << std::endl; } #ifndef DEBUG int main(int argc, char* argv[]) #else int main_(int argc, char* argv[]) #endif { ::Run(std::cin, std::cout); return 0; } |