#include <iostream>
#include <vector>
#include <algorithm>
std::vector<std::pair<int, int> > merge(const std::vector<std::pair<int, int> > &vec) {
std::vector<std::pair<int, int> > res;
std::pair<int, int> actualInterval;
if (!vec.empty()) {
actualInterval = *vec.begin();
}
for (auto interval : vec) {
if (interval.first <= actualInterval.second) {
if (interval.second > actualInterval.second) {
actualInterval.second = interval.second;
}
} else {
res.push_back(actualInterval);
actualInterval = interval;
}
}
if (!vec.empty()) {
res.push_back(actualInterval);
}
return res;
}
std::pair<int, int> getIntersection(std::pair<int, int> p1, std::pair<int, int> p2) {
int l, r;
if (p1.first < p2.first) {
l = p2.first;
} else {
l = p1.first;
}
if (p1.second > p2.second) {
r = p2.second;
} else {
r = p1.second;
}
return std::make_pair(l, r);
}
std::vector<std::pair<int, int> >
intersect(const std::vector<std::pair<int, int> > &vec1, const std::vector<std::pair<int, int> > &vec2) {
std::vector<std::pair<int, int> > res;
std::pair<int, int> actualIntersection;
int i, j;
i = 0;
j = 0;
while (i < vec1.size() && j < vec2.size()) {
actualIntersection = getIntersection(vec1[i], vec2[j]);
if (actualIntersection.first <= actualIntersection.second) {
res.push_back(actualIntersection);
}
if (vec1[i].second < vec2[j].second) {
i++;
} else {
j++;
}
}
return res;
}
int main() {
int n, m;
std::vector<std::pair<int, int> > yellow, blue, red;
std::cin >> n >> m;
for (int i = 0; i < m; i++) {
int l, r, c;
std::cin >> l >> r >> c;
if (c == 1) {
yellow.emplace_back(l, r);
}
if (c == 2) {
blue.emplace_back(l, r);
}
if (c == 3) {
red.emplace_back(l, r);
}
}
std::sort(yellow.begin(), yellow.end());
std::sort(blue.begin(), blue.end());
std::sort(red.begin(), red.end());
yellow = merge(yellow);
blue = merge(blue);
red = merge(red);
auto green = intersect(yellow, blue);
auto other = intersect(green, red);
int res = 0;
for (auto interval : green) {
res += interval.second - interval.first + 1;
}
for (auto interval : other) {
res -= interval.second - interval.first + 1;
}
std::cout << res << "\n";
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 | #include <iostream> #include <vector> #include <algorithm> std::vector<std::pair<int, int> > merge(const std::vector<std::pair<int, int> > &vec) { std::vector<std::pair<int, int> > res; std::pair<int, int> actualInterval; if (!vec.empty()) { actualInterval = *vec.begin(); } for (auto interval : vec) { if (interval.first <= actualInterval.second) { if (interval.second > actualInterval.second) { actualInterval.second = interval.second; } } else { res.push_back(actualInterval); actualInterval = interval; } } if (!vec.empty()) { res.push_back(actualInterval); } return res; } std::pair<int, int> getIntersection(std::pair<int, int> p1, std::pair<int, int> p2) { int l, r; if (p1.first < p2.first) { l = p2.first; } else { l = p1.first; } if (p1.second > p2.second) { r = p2.second; } else { r = p1.second; } return std::make_pair(l, r); } std::vector<std::pair<int, int> > intersect(const std::vector<std::pair<int, int> > &vec1, const std::vector<std::pair<int, int> > &vec2) { std::vector<std::pair<int, int> > res; std::pair<int, int> actualIntersection; int i, j; i = 0; j = 0; while (i < vec1.size() && j < vec2.size()) { actualIntersection = getIntersection(vec1[i], vec2[j]); if (actualIntersection.first <= actualIntersection.second) { res.push_back(actualIntersection); } if (vec1[i].second < vec2[j].second) { i++; } else { j++; } } return res; } int main() { int n, m; std::vector<std::pair<int, int> > yellow, blue, red; std::cin >> n >> m; for (int i = 0; i < m; i++) { int l, r, c; std::cin >> l >> r >> c; if (c == 1) { yellow.emplace_back(l, r); } if (c == 2) { blue.emplace_back(l, r); } if (c == 3) { red.emplace_back(l, r); } } std::sort(yellow.begin(), yellow.end()); std::sort(blue.begin(), blue.end()); std::sort(red.begin(), red.end()); yellow = merge(yellow); blue = merge(blue); red = merge(red); auto green = intersect(yellow, blue); auto other = intersect(green, red); int res = 0; for (auto interval : green) { res += interval.second - interval.first + 1; } for (auto interval : other) { res -= interval.second - interval.first + 1; } std::cout << res << "\n"; return 0; } |
English