#include <iostream> #include <vector> #include <algorithm> class Range { public: int l; int r; Range(const int l, const int r) { this->l = l; this->r = r; } bool overlaps(const Range &that) const { return (l <= that.l && that.l <= r) || (that.l <= l && l <= that.r); } void join(const Range &range) { this->l = std::min(l, range.l); this->r = std::max(r, range.r); } bool compare(const Range &that) const { return l < that.l; } bool isSibling(const Range &that) const { return l - 1 == that.r || that.l - 1 == r; } int count() const { return r - l + 1; } bool isEmpty() const { return l == 0 && r == 0; } } EMPTY = Range(0, 0); bool compare(Range &r1, Range &r2) { return r1.compare(r2); } class Ranges { std::vector<Range> *ranges; public: Ranges() { this->ranges = new std::vector<Range>(); } void add(Range r) { ranges->push_back(r); } Ranges *compact() { if (!ranges->empty()) { std::sort(ranges->begin(), ranges->end(), compare); auto *newVector = new std::vector<Range>(); newVector->push_back(ranges->front()); auto iter = newVector->begin(); for (auto it = ranges->begin() + 1; it != ranges->end(); ++it) { Range ¤tOld = *it; if (iter->overlaps(currentOld) || iter->isSibling(currentOld)) { iter->join(currentOld); } else { newVector->push_back(currentOld); iter = newVector->end() - 1; } } delete ranges; ranges = newVector; } return this; } Ranges *intersect(Ranges &other) { auto thisIt = this->ranges->begin(); auto thatIt = other.ranges->begin(); auto *newRanges = new Ranges(); while (thisIt != this->ranges->end() && thatIt != other.ranges->end()) { if (thisIt->overlaps(*thatIt)) { newRanges->add(Range(std::max(thisIt->l, thatIt->l), std::min(thisIt->r, thatIt->r))); } if (thisIt->r < thatIt->r) { thisIt++; } else { thatIt++; } } return newRanges; } int diffCount(Ranges &other) { if (other.ranges->empty()) { return this->count(); } if (this->ranges->empty()) { return 0; } auto thisIt = this->ranges->begin(); auto thatIt = other.ranges->begin(); Range rest = EMPTY; int count = 0; while (thisIt != this->ranges->end() && thatIt != other.ranges->end()) { if (rest.isEmpty()) { rest = *thisIt; } if (rest.r < thatIt->l) { count += rest.count(); rest = EMPTY; } else if (thatIt->r < rest.l) { thatIt++; } else if (rest.l >= thatIt->l && rest.r <= thatIt->r) { rest = EMPTY; } else if (rest.l < thatIt->l) { count += Range(rest.l, thatIt->l - 1).count(); if (thatIt->r < rest.r) { rest = Range(thatIt->r + 1, rest.r); thatIt++; } else if (thatIt->r == rest.r) { rest = EMPTY; } else { rest = EMPTY; } } else { rest = Range(thatIt->r + 1, rest.r); thatIt++; } if (rest.isEmpty()) { thisIt++; } } if (!rest.isEmpty()) { count += rest.count(); thisIt++; } while (thisIt != ranges->end()) { count += (*thisIt).count(); thisIt++; } return count; } int count() { int ret = 0; for (auto &range : *ranges) { ret += range.count(); } return ret; } // void print() { // for (auto &range : *ranges) { // std::cout << "[" << range.l << "," << range.r << "]"; // } // std::cout << std::endl; // } }; int main() { std::ios_base::sync_with_stdio(0); int n; int m; int l; int r; int color; std::cin >> n >> m; Ranges colors[] = {Ranges(), Ranges(), Ranges()}; for (int i = 0; i < m; i++) { std::cin >> l >> r >> color; colors[color - 1].add(Range(l, r)); } colors[0].compact(); colors[1].compact(); colors[2].compact(); Ranges *green = colors[0].intersect(colors[1]); // colors[0].print(); // colors[1].print(); // colors[2].print(); // green->print(); // green2->print(); std::cout << green->diffCount(colors[2]) << std::endl; 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 | #include <iostream> #include <vector> #include <algorithm> class Range { public: int l; int r; Range(const int l, const int r) { this->l = l; this->r = r; } bool overlaps(const Range &that) const { return (l <= that.l && that.l <= r) || (that.l <= l && l <= that.r); } void join(const Range &range) { this->l = std::min(l, range.l); this->r = std::max(r, range.r); } bool compare(const Range &that) const { return l < that.l; } bool isSibling(const Range &that) const { return l - 1 == that.r || that.l - 1 == r; } int count() const { return r - l + 1; } bool isEmpty() const { return l == 0 && r == 0; } } EMPTY = Range(0, 0); bool compare(Range &r1, Range &r2) { return r1.compare(r2); } class Ranges { std::vector<Range> *ranges; public: Ranges() { this->ranges = new std::vector<Range>(); } void add(Range r) { ranges->push_back(r); } Ranges *compact() { if (!ranges->empty()) { std::sort(ranges->begin(), ranges->end(), compare); auto *newVector = new std::vector<Range>(); newVector->push_back(ranges->front()); auto iter = newVector->begin(); for (auto it = ranges->begin() + 1; it != ranges->end(); ++it) { Range ¤tOld = *it; if (iter->overlaps(currentOld) || iter->isSibling(currentOld)) { iter->join(currentOld); } else { newVector->push_back(currentOld); iter = newVector->end() - 1; } } delete ranges; ranges = newVector; } return this; } Ranges *intersect(Ranges &other) { auto thisIt = this->ranges->begin(); auto thatIt = other.ranges->begin(); auto *newRanges = new Ranges(); while (thisIt != this->ranges->end() && thatIt != other.ranges->end()) { if (thisIt->overlaps(*thatIt)) { newRanges->add(Range(std::max(thisIt->l, thatIt->l), std::min(thisIt->r, thatIt->r))); } if (thisIt->r < thatIt->r) { thisIt++; } else { thatIt++; } } return newRanges; } int diffCount(Ranges &other) { if (other.ranges->empty()) { return this->count(); } if (this->ranges->empty()) { return 0; } auto thisIt = this->ranges->begin(); auto thatIt = other.ranges->begin(); Range rest = EMPTY; int count = 0; while (thisIt != this->ranges->end() && thatIt != other.ranges->end()) { if (rest.isEmpty()) { rest = *thisIt; } if (rest.r < thatIt->l) { count += rest.count(); rest = EMPTY; } else if (thatIt->r < rest.l) { thatIt++; } else if (rest.l >= thatIt->l && rest.r <= thatIt->r) { rest = EMPTY; } else if (rest.l < thatIt->l) { count += Range(rest.l, thatIt->l - 1).count(); if (thatIt->r < rest.r) { rest = Range(thatIt->r + 1, rest.r); thatIt++; } else if (thatIt->r == rest.r) { rest = EMPTY; } else { rest = EMPTY; } } else { rest = Range(thatIt->r + 1, rest.r); thatIt++; } if (rest.isEmpty()) { thisIt++; } } if (!rest.isEmpty()) { count += rest.count(); thisIt++; } while (thisIt != ranges->end()) { count += (*thisIt).count(); thisIt++; } return count; } int count() { int ret = 0; for (auto &range : *ranges) { ret += range.count(); } return ret; } // void print() { // for (auto &range : *ranges) { // std::cout << "[" << range.l << "," << range.r << "]"; // } // std::cout << std::endl; // } }; int main() { std::ios_base::sync_with_stdio(0); int n; int m; int l; int r; int color; std::cin >> n >> m; Ranges colors[] = {Ranges(), Ranges(), Ranges()}; for (int i = 0; i < m; i++) { std::cin >> l >> r >> color; colors[color - 1].add(Range(l, r)); } colors[0].compact(); colors[1].compact(); colors[2].compact(); Ranges *green = colors[0].intersect(colors[1]); // colors[0].print(); // colors[1].print(); // colors[2].print(); // green->print(); // green2->print(); std::cout << green->diffCount(colors[2]) << std::endl; return 0; } |