#include <iostream> using namespace std; class Tree { public: Tree(int l, int r) { left = l; right = r; if(l == r) { p_left = NULL; p_right = NULL; } else { int m = (l + r) / 2; p_left = new Tree(l, m); p_right = new Tree(m + 1, r); } color.yellow = COLOR_IS_NOT_IN_BRACKET; color.blue = COLOR_IS_NOT_IN_BRACKET; color.red = COLOR_IS_NOT_IN_BRACKET; } void pushColor(int l, int r, int colorCode) { if(l > getRight() || r < getLeft()) return; if(l <= getLeft() && r >= getRight()) { if(colorCode == 1) color.yellow = COLOR_IS_IN_BRACKET; else if(colorCode == 2) color.blue = COLOR_IS_IN_BRACKET; else color.red = COLOR_IS_IN_BRACKET; return; } if(colorCode == 1) { if(color.yellow == COLOR_IS_NOT_IN_BRACKET) color.yellow = COLOR_IS_OR_IS_NOT_IN_BRACKET; if(color.yellow == COLOR_IS_IN_BRACKET) return; } else if(colorCode == 2) { if(color.blue == COLOR_IS_NOT_IN_BRACKET) color.blue = COLOR_IS_OR_IS_NOT_IN_BRACKET; if(color.blue == COLOR_IS_IN_BRACKET) return; } else { if(color.red == COLOR_IS_NOT_IN_BRACKET) color.red = COLOR_IS_OR_IS_NOT_IN_BRACKET; if(color.red == COLOR_IS_IN_BRACKET) return; } if(p_left != NULL) p_left->pushColor(l, r, colorCode); if(p_right != NULL) p_right->pushColor(l, r, colorCode); } bool isGreen(int bracketNumber) { return isYellow(bracketNumber) && isBlue(bracketNumber) && !isRed(bracketNumber); } bool isYellow(int bracketNumber) { if(bracketNumber >= getLeft() && bracketNumber <= getRight()) { if(color.yellow == COLOR_IS_IN_BRACKET) return true; if(color.yellow == COLOR_IS_NOT_IN_BRACKET) return false; int m = (left + right) / 2; if(bracketNumber <= m) { if(p_left != NULL) return p_left->isYellow(bracketNumber); } else { if(p_right != NULL) return p_right->isYellow(bracketNumber); } } return false; } bool isBlue(int bracketNumber) { if(bracketNumber >= getLeft() && bracketNumber <= getRight()) { if(color.blue == COLOR_IS_IN_BRACKET) return true; if(color.blue == COLOR_IS_NOT_IN_BRACKET) return false; int m = (left + right) / 2; if(bracketNumber <= m) { if(p_left != NULL) return p_left->isBlue(bracketNumber); } else { if(p_right != NULL) return p_right->isBlue(bracketNumber); } } return false; } bool isRed(int bracketNumber) { if(bracketNumber >= getLeft() && bracketNumber <= getRight()) { if(color.red == COLOR_IS_IN_BRACKET) return true; if(color.red == COLOR_IS_NOT_IN_BRACKET) return false; int m = (left + right) / 2; if(bracketNumber <= m) { if(p_left != NULL) return p_left->isRed(bracketNumber); } else { if(p_right != NULL) return p_right->isRed(bracketNumber); } } return false; } int getLeft() { return left; } int getRight() { return right; } /*void print() { cout << left << " " << right << "\n"; cout << color.yellow << " " << color.blue << " " << color.red << "\n\n"; if(p_left != NULL) p_left->print(); if(p_right != NULL) p_right->print(); }*/ private: int left; int right; Tree *p_left; Tree *p_right; const int COLOR_IS_OR_IS_NOT_IN_BRACKET = 0; const int COLOR_IS_IN_BRACKET = 1; const int COLOR_IS_NOT_IN_BRACKET = 2; struct Color { int yellow; int blue; int red; } color; }; int main() { ios_base::sync_with_stdio(0); cin.tie(0); int n, m; cin >> n >> m; Tree bracketTree(1, n); int l, r, k; for(int i = 0; i < m; i++) { cin >> l >> r >> k; bracketTree.pushColor(l, r, k); } //bracketTree.print(); int result = 0; for(int i = 0; i < n; i++) { if(bracketTree.isGreen(i + 1)) result++; } cout << result << "\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 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 | #include <iostream> using namespace std; class Tree { public: Tree(int l, int r) { left = l; right = r; if(l == r) { p_left = NULL; p_right = NULL; } else { int m = (l + r) / 2; p_left = new Tree(l, m); p_right = new Tree(m + 1, r); } color.yellow = COLOR_IS_NOT_IN_BRACKET; color.blue = COLOR_IS_NOT_IN_BRACKET; color.red = COLOR_IS_NOT_IN_BRACKET; } void pushColor(int l, int r, int colorCode) { if(l > getRight() || r < getLeft()) return; if(l <= getLeft() && r >= getRight()) { if(colorCode == 1) color.yellow = COLOR_IS_IN_BRACKET; else if(colorCode == 2) color.blue = COLOR_IS_IN_BRACKET; else color.red = COLOR_IS_IN_BRACKET; return; } if(colorCode == 1) { if(color.yellow == COLOR_IS_NOT_IN_BRACKET) color.yellow = COLOR_IS_OR_IS_NOT_IN_BRACKET; if(color.yellow == COLOR_IS_IN_BRACKET) return; } else if(colorCode == 2) { if(color.blue == COLOR_IS_NOT_IN_BRACKET) color.blue = COLOR_IS_OR_IS_NOT_IN_BRACKET; if(color.blue == COLOR_IS_IN_BRACKET) return; } else { if(color.red == COLOR_IS_NOT_IN_BRACKET) color.red = COLOR_IS_OR_IS_NOT_IN_BRACKET; if(color.red == COLOR_IS_IN_BRACKET) return; } if(p_left != NULL) p_left->pushColor(l, r, colorCode); if(p_right != NULL) p_right->pushColor(l, r, colorCode); } bool isGreen(int bracketNumber) { return isYellow(bracketNumber) && isBlue(bracketNumber) && !isRed(bracketNumber); } bool isYellow(int bracketNumber) { if(bracketNumber >= getLeft() && bracketNumber <= getRight()) { if(color.yellow == COLOR_IS_IN_BRACKET) return true; if(color.yellow == COLOR_IS_NOT_IN_BRACKET) return false; int m = (left + right) / 2; if(bracketNumber <= m) { if(p_left != NULL) return p_left->isYellow(bracketNumber); } else { if(p_right != NULL) return p_right->isYellow(bracketNumber); } } return false; } bool isBlue(int bracketNumber) { if(bracketNumber >= getLeft() && bracketNumber <= getRight()) { if(color.blue == COLOR_IS_IN_BRACKET) return true; if(color.blue == COLOR_IS_NOT_IN_BRACKET) return false; int m = (left + right) / 2; if(bracketNumber <= m) { if(p_left != NULL) return p_left->isBlue(bracketNumber); } else { if(p_right != NULL) return p_right->isBlue(bracketNumber); } } return false; } bool isRed(int bracketNumber) { if(bracketNumber >= getLeft() && bracketNumber <= getRight()) { if(color.red == COLOR_IS_IN_BRACKET) return true; if(color.red == COLOR_IS_NOT_IN_BRACKET) return false; int m = (left + right) / 2; if(bracketNumber <= m) { if(p_left != NULL) return p_left->isRed(bracketNumber); } else { if(p_right != NULL) return p_right->isRed(bracketNumber); } } return false; } int getLeft() { return left; } int getRight() { return right; } /*void print() { cout << left << " " << right << "\n"; cout << color.yellow << " " << color.blue << " " << color.red << "\n\n"; if(p_left != NULL) p_left->print(); if(p_right != NULL) p_right->print(); }*/ private: int left; int right; Tree *p_left; Tree *p_right; const int COLOR_IS_OR_IS_NOT_IN_BRACKET = 0; const int COLOR_IS_IN_BRACKET = 1; const int COLOR_IS_NOT_IN_BRACKET = 2; struct Color { int yellow; int blue; int red; } color; }; int main() { ios_base::sync_with_stdio(0); cin.tie(0); int n, m; cin >> n >> m; Tree bracketTree(1, n); int l, r, k; for(int i = 0; i < m; i++) { cin >> l >> r >> k; bracketTree.pushColor(l, r, k); } //bracketTree.print(); int result = 0; for(int i = 0; i < n; i++) { if(bracketTree.isGreen(i + 1)) result++; } cout << result << "\n"; return 0; } |