#include <iostream> #include <list> using namespace std; class Color { int r; int b; int y; public: Color(int rr, int bb, int yy) : r(rr), b(bb), y(yy) {} Color minus() const { return {-r, -b, -y}; } int sum() const { return r + b + y; } bool isGreen() const { return r == 0 && b > 0 && y > 0; } bool isNegative() const { return sum() < 0; } void mix(Color c) { r += c.r; b += c.b; y += c.y; } }; class Point { int x; Color c; public: Point(int xx, Color cc) : x(xx), c(cc) {} int getX() const { return x; } Color getColor() const { return c; } int getColorSum() const { return c.sum(); } bool wasEnd() const { return c.isNegative(); } bool operator<(const Point &p) const { if (getX() == p.getX()) { return getColorSum() > p.getColorSum(); } return getX() < p.getX(); } }; Color getColor(int k) { switch (k) { case 1: return {0, 0, 1}; case 2: return {0, 1, 0}; case 3: return {1, 0, 0}; default: return {0, 0, 0}; } } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); int n, m; cin >> n >> m; list<Point> points; for (int i = 0; i < m; i++) { int l, r, k; cin >> l >> r >> k; Color color = getColor(k); points.emplace_back(l, color); points.emplace_back(r, color.minus()); } points.sort(); Color currentColor = getColor(0); Point lastPoint = Point(0, getColor(0)); int lastCounted = 0; int result = 0; for (Point const &point : points) { if (point.getX() == lastPoint.getX()) { if (point.getColor().isNegative() != lastPoint.getColor().isNegative()) { if (currentColor.isGreen()) { result++; } lastCounted = point.getX(); } } else { if (currentColor.isGreen()) { if (point.getColor().isNegative()) { result += point.getX() - lastCounted; lastCounted = point.getX(); } else { result += point.getX() - 1 - lastCounted; lastCounted = point.getX() - 1; } } else { if (point.getColor().isNegative()) { lastCounted = point.getX(); } else { lastCounted = point.getX() - 1; } } } currentColor.mix(point.getColor()); lastPoint = point; } if (currentColor.isGreen()) { if (n - lastCounted > 0) { result += n - lastCounted; } } cout << result << 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 | #include <iostream> #include <list> using namespace std; class Color { int r; int b; int y; public: Color(int rr, int bb, int yy) : r(rr), b(bb), y(yy) {} Color minus() const { return {-r, -b, -y}; } int sum() const { return r + b + y; } bool isGreen() const { return r == 0 && b > 0 && y > 0; } bool isNegative() const { return sum() < 0; } void mix(Color c) { r += c.r; b += c.b; y += c.y; } }; class Point { int x; Color c; public: Point(int xx, Color cc) : x(xx), c(cc) {} int getX() const { return x; } Color getColor() const { return c; } int getColorSum() const { return c.sum(); } bool wasEnd() const { return c.isNegative(); } bool operator<(const Point &p) const { if (getX() == p.getX()) { return getColorSum() > p.getColorSum(); } return getX() < p.getX(); } }; Color getColor(int k) { switch (k) { case 1: return {0, 0, 1}; case 2: return {0, 1, 0}; case 3: return {1, 0, 0}; default: return {0, 0, 0}; } } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); int n, m; cin >> n >> m; list<Point> points; for (int i = 0; i < m; i++) { int l, r, k; cin >> l >> r >> k; Color color = getColor(k); points.emplace_back(l, color); points.emplace_back(r, color.minus()); } points.sort(); Color currentColor = getColor(0); Point lastPoint = Point(0, getColor(0)); int lastCounted = 0; int result = 0; for (Point const &point : points) { if (point.getX() == lastPoint.getX()) { if (point.getColor().isNegative() != lastPoint.getColor().isNegative()) { if (currentColor.isGreen()) { result++; } lastCounted = point.getX(); } } else { if (currentColor.isGreen()) { if (point.getColor().isNegative()) { result += point.getX() - lastCounted; lastCounted = point.getX(); } else { result += point.getX() - 1 - lastCounted; lastCounted = point.getX() - 1; } } else { if (point.getColor().isNegative()) { lastCounted = point.getX(); } else { lastCounted = point.getX() - 1; } } } currentColor.mix(point.getColor()); lastPoint = point; } if (currentColor.isGreen()) { if (n - lastCounted > 0) { result += n - lastCounted; } } cout << result << endl; return 0; } |