#include <bits/stdc++.h> using namespace std; // How much dye is added using Color = array<int, 3>; bool is_green(const Color& color) { return color[0] > 0 && color[1] > 0 && color[2] == 0; } struct AddDye { int dye; size_t begin, end; }; struct TestCase { int length; int operation_count; list<AddDye> operations; }; TestCase read_test_case(); void solve_test_case(const TestCase&); int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); solve_test_case(read_test_case()); } istream& operator>>(istream& stream, AddDye& add_dye) { stream >> add_dye.begin >> add_dye.end >> add_dye.dye; return stream; } TestCase read_test_case() { TestCase test_case; cin >> test_case.length >> test_case.operation_count; test_case.operations.resize(test_case.operation_count); for (auto& op : test_case.operations) cin >> op; return test_case; } template <typename T, size_t size> array<T, size> add(const array<T, size>& a, const array<T, size>& b) { array<T, size> result; for (size_t i = 0; i < size; i++) result[i] = a[i] + b[i]; return result; } void solve_test_case(const TestCase& test_case) { vector<Color> colors(test_case.length + 1, {0, 0, 0}); for (auto op : test_case.operations) { colors[op.begin - 1][op.dye - 1]++; colors[op.end][op.dye - 1]--; } Color current_color = {0, 0, 0}; int green_count = count_if(colors.begin(), colors.end(), [&](auto color) { current_color = add(current_color, color); return is_green(current_color); }); cout << green_count << "\n"; }
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 | #include <bits/stdc++.h> using namespace std; // How much dye is added using Color = array<int, 3>; bool is_green(const Color& color) { return color[0] > 0 && color[1] > 0 && color[2] == 0; } struct AddDye { int dye; size_t begin, end; }; struct TestCase { int length; int operation_count; list<AddDye> operations; }; TestCase read_test_case(); void solve_test_case(const TestCase&); int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); solve_test_case(read_test_case()); } istream& operator>>(istream& stream, AddDye& add_dye) { stream >> add_dye.begin >> add_dye.end >> add_dye.dye; return stream; } TestCase read_test_case() { TestCase test_case; cin >> test_case.length >> test_case.operation_count; test_case.operations.resize(test_case.operation_count); for (auto& op : test_case.operations) cin >> op; return test_case; } template <typename T, size_t size> array<T, size> add(const array<T, size>& a, const array<T, size>& b) { array<T, size> result; for (size_t i = 0; i < size; i++) result[i] = a[i] + b[i]; return result; } void solve_test_case(const TestCase& test_case) { vector<Color> colors(test_case.length + 1, {0, 0, 0}); for (auto op : test_case.operations) { colors[op.begin - 1][op.dye - 1]++; colors[op.end][op.dye - 1]--; } Color current_color = {0, 0, 0}; int green_count = count_if(colors.begin(), colors.end(), [&](auto color) { current_color = add(current_color, color); return is_green(current_color); }); cout << green_count << "\n"; } |