#include <iostream> #include <vector> #include <algorithm> #include <utility> using namespace std; vector<int> Red; vector<int> Yellow; vector<int> Blue; vector<int> tab[3]; int n, m; void add(vector<int>& v, int start, int end){ int index = lower_bound(v.begin(), v.end(), start) - v.begin(); if (v.size() == 0){ v.push_back(start); v.push_back(end); return; } if (index % 2 == 1); else if (index == 0){ v.insert(v.begin(), start); index++; } else if (v[index - 1] == start - 1){ v.erase(v.begin() + index - 1); index--; } else { if (index == v.size()) v.push_back(start); else v.insert(v.begin() + index, start); index++; } int d = v.size(); while (index < d){ if (v[index] <= end){ v.erase(v.begin() + index); d -= 1; } else break; } if (index == v.size()) v.push_back(end); else if (v.size() % 2 == 1 && v[index] <= end + 1) v.erase(v.begin() + index); else if (v.size() % 2 == 1) v.insert(v.begin() + index, end); } int main() { tab[0] = Yellow; tab[1] = Blue; tab[2] = Red; cin >> n; cin >> m; int s, e, t; for (int i = 0; i < m; i++){ cin >> s >> e >> t; add(tab[t - 1], s, e); } vector<int> Green; int counter = 0; int NS; int NE; int i1 = 0; int i2 = 0; bool b = true; int start; int end; while (i1 < tab[0].size()){ NS = tab[0][i1]; NE = tab[0][i1 + 1]; i1 += 2; while (tab[1][i2] <= NE && i2 < tab[1].size()){ start = max(NS, tab[1][i2]); end = min(NE, tab[1][i2 + 1]); Green.push_back(start); Green.push_back(end); counter += max(0, end - start + 1); i2 += 2; } i2 -= 2; i2 = max(0, i2); } if (tab[2].size() == 0) { cout << counter; return 0; } i1 = 0; i2 = 0; b = true; while (i1 < Green.size()){ NS = Green[i1]; NE = Green[i1 + 1]; i1 += 2; while (tab[2][i2] <= NE && i2 < tab[2].size()){ start = max(NS, tab[2][i2]); end = min(NE, tab[2][i2 + 1]); counter -= max(0, end - start + 1); i2 += 2; } i2 -= 2; i2 = max(i2, 0); } cout << counter; 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 | #include <iostream> #include <vector> #include <algorithm> #include <utility> using namespace std; vector<int> Red; vector<int> Yellow; vector<int> Blue; vector<int> tab[3]; int n, m; void add(vector<int>& v, int start, int end){ int index = lower_bound(v.begin(), v.end(), start) - v.begin(); if (v.size() == 0){ v.push_back(start); v.push_back(end); return; } if (index % 2 == 1); else if (index == 0){ v.insert(v.begin(), start); index++; } else if (v[index - 1] == start - 1){ v.erase(v.begin() + index - 1); index--; } else { if (index == v.size()) v.push_back(start); else v.insert(v.begin() + index, start); index++; } int d = v.size(); while (index < d){ if (v[index] <= end){ v.erase(v.begin() + index); d -= 1; } else break; } if (index == v.size()) v.push_back(end); else if (v.size() % 2 == 1 && v[index] <= end + 1) v.erase(v.begin() + index); else if (v.size() % 2 == 1) v.insert(v.begin() + index, end); } int main() { tab[0] = Yellow; tab[1] = Blue; tab[2] = Red; cin >> n; cin >> m; int s, e, t; for (int i = 0; i < m; i++){ cin >> s >> e >> t; add(tab[t - 1], s, e); } vector<int> Green; int counter = 0; int NS; int NE; int i1 = 0; int i2 = 0; bool b = true; int start; int end; while (i1 < tab[0].size()){ NS = tab[0][i1]; NE = tab[0][i1 + 1]; i1 += 2; while (tab[1][i2] <= NE && i2 < tab[1].size()){ start = max(NS, tab[1][i2]); end = min(NE, tab[1][i2 + 1]); Green.push_back(start); Green.push_back(end); counter += max(0, end - start + 1); i2 += 2; } i2 -= 2; i2 = max(0, i2); } if (tab[2].size() == 0) { cout << counter; return 0; } i1 = 0; i2 = 0; b = true; while (i1 < Green.size()){ NS = Green[i1]; NE = Green[i1 + 1]; i1 += 2; while (tab[2][i2] <= NE && i2 < tab[2].size()){ start = max(NS, tab[2][i2]); end = min(NE, tab[2][i2 + 1]); counter -= max(0, end - start + 1); i2 += 2; } i2 -= 2; i2 = max(i2, 0); } cout << counter; return 0; } |