#include <iostream> #include <string> #include <algorithm> #include <utility> #include <numeric> #include <cassert> #include <vector> #include <list> typedef unsigned int uint; typedef long long ll; typedef unsigned long long ull; // create binary tree struct Node { bool c[3]; // colors - [0] yellow, [1] blue, [2] red std::list<uint> edge; }; struct BinaryTree { std::vector<std::vector<std::vector<bool>>> v; size_t height() const // will break if size == 0 { return v.size(); } BinaryTree(const size_t& size) { size_t capacity = 1; v.push_back(std::vector<std::vector<bool>>(capacity, std::vector<bool>(3))); while (capacity < size) { capacity <<= 1; v.push_back(std::vector<std::vector<bool>>(capacity, std::vector<bool>(3))); } } void Update(const size_t& begin, const size_t& end, const size_t& color) { size_t b = begin, e = end + 1; for (size_t h = height(); h-- > 0 && b < e; e >>= 1, b >>= 1) { if (e - b == 1) { v[h][b][color] = true; break; } if (b % 2) v[h][b++][color] = true; if (e >= 1 && e % 2) v[h][e-- - 1][color] = true; } } size_t Result() { size_t result = 0; for (size_t i = 0; i < v[height() - 1].size(); i++) { for (size_t h = height() - 1, d = 1; h-- > 0; d++) for (size_t j = 0; j < 3; j++) if (v[h][i >> d][j]) v[height() - 1][i][j] = true; if (v[height() - 1][i][0] && v[height() - 1][i][1] && !v[height() - 1][i][2]) result++; } return result; } }; int main() { std::ios_base::sync_with_stdio(0); std::cin.tie(0); uint n, m; std::cin >> n >> m; BinaryTree t(n); for (uint i = 0; i < m; i++) { uint l, r, k; std::cin >> l >> r >> k; t.Update(l - 1, r - 1, k - 1); } std::cout << t.Result(); 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 | #include <iostream> #include <string> #include <algorithm> #include <utility> #include <numeric> #include <cassert> #include <vector> #include <list> typedef unsigned int uint; typedef long long ll; typedef unsigned long long ull; // create binary tree struct Node { bool c[3]; // colors - [0] yellow, [1] blue, [2] red std::list<uint> edge; }; struct BinaryTree { std::vector<std::vector<std::vector<bool>>> v; size_t height() const // will break if size == 0 { return v.size(); } BinaryTree(const size_t& size) { size_t capacity = 1; v.push_back(std::vector<std::vector<bool>>(capacity, std::vector<bool>(3))); while (capacity < size) { capacity <<= 1; v.push_back(std::vector<std::vector<bool>>(capacity, std::vector<bool>(3))); } } void Update(const size_t& begin, const size_t& end, const size_t& color) { size_t b = begin, e = end + 1; for (size_t h = height(); h-- > 0 && b < e; e >>= 1, b >>= 1) { if (e - b == 1) { v[h][b][color] = true; break; } if (b % 2) v[h][b++][color] = true; if (e >= 1 && e % 2) v[h][e-- - 1][color] = true; } } size_t Result() { size_t result = 0; for (size_t i = 0; i < v[height() - 1].size(); i++) { for (size_t h = height() - 1, d = 1; h-- > 0; d++) for (size_t j = 0; j < 3; j++) if (v[h][i >> d][j]) v[height() - 1][i][j] = true; if (v[height() - 1][i][0] && v[height() - 1][i][1] && !v[height() - 1][i][2]) result++; } return result; } }; int main() { std::ios_base::sync_with_stdio(0); std::cin.tie(0); uint n, m; std::cin >> n >> m; BinaryTree t(n); for (uint i = 0; i < m; i++) { uint l, r, k; std::cin >> l >> r >> k; t.Update(l - 1, r - 1, k - 1); } std::cout << t.Result(); return 0; } |