#include<cstdio> struct Range { int l; int r; Range() {} Range(int l, int r) { this->l = l; this->r = r; } }; class PQ { bool compare(const int pos1, const int pos2) { if (entries[pos1].l < entries[pos2].l) return true; if (entries[pos2].l < entries[pos1].l) return false; return entries[pos2].r < entries[pos1].r; } void swap(const int pos1, const int pos2) { Range tmp = entries[pos1]; entries[pos1] = entries[pos2]; entries[pos2] = tmp; } void HeapUp(int pos) { if (pos == 1) return; int parent = pos / 2; if (compare(pos, parent)) { swap(pos, parent); HeapUp(parent); } } void HeapDown(int pos) { if (count < 2 * pos) return; int smaller = 2 * pos; if (smaller + 1 <= count && compare(smaller + 1, smaller)) smaller++; if (compare(smaller, pos)) { swap(smaller, pos); HeapDown(smaller); } } public: int size; int count; Range* entries; PQ() { this->size = 2; this->count = 0; this->entries = new Range[this->size]; } void push(Range range_to_push) { count++; if (count == size) { size *= 2; Range* next = new Range[size]; for (int i = 1; i < count; ++i) next[i] = entries[i]; delete[] entries; entries = next; } entries[count] = range_to_push; HeapUp(count); } void push(int l, int r) { push(Range(l, r)); } Range pop() { Range result = entries[1]; entries[1] = entries[count--]; HeapDown(1); return result; } bool empty() { return count == 0; } }; PQ ranges[10]; int n, m, l, r, k; void merge_ranges(int id) { if (ranges[id].empty()) return; Range current_range = ranges[id].pop(); Range next_range; while (!ranges[id].empty()) { next_range = ranges[id].pop(); if (current_range.r + 1 < next_range.l) { ranges[id + 3].push(current_range); current_range = next_range; } else current_range.r = current_range.r < next_range.r ? next_range.r : current_range.r; } ranges[id + 3].push(current_range); } void reverse_range(int id1, int id2) { Range current_range; int new_l = 1; while (!ranges[id1].empty()) { current_range = ranges[id1].pop(); ranges[id2].push(new_l, current_range.l - 1); new_l = current_range.r + 1; } ranges[id2].push(new_l, n); } void intersect_ranges(int id1, int id2, int id3) { if (ranges[id1].empty() || ranges[id2].empty()) return; Range current_range1; Range current_range2; while (!ranges[id1].empty() && !ranges[id2].empty()) { current_range1 = ranges[id1].pop(); current_range2 = ranges[id2].pop(); if (current_range1.r < current_range2.l) { ranges[id2].push(current_range2); continue; } if (current_range2.r < current_range1.l) { ranges[id1].push(current_range1); continue; } if (current_range1.l < current_range2.l && current_range1.r < current_range2.r) { ranges[id3].push(current_range2.l, current_range1.r); ranges[id2].push(current_range1.r + 1, current_range2.r); continue; } if (current_range2.l <= current_range1.l && current_range1.r < current_range2.r) { ranges[id3].push(current_range1.l, current_range1.r); ranges[id2].push(current_range1.r + 1, current_range2.r); continue; } if (current_range1.l < current_range2.l && current_range2.r <= current_range1.r) { ranges[id3].push(current_range2.l, current_range2.r); if (current_range2.r < current_range1.r) ranges[id1].push(current_range2.r + 1, current_range1.r); continue; } if (current_range2.l <= current_range1.l && current_range2.r <= current_range1.r) { ranges[id3].push(current_range1.l, current_range2.r); if (current_range2.r < current_range1.r) ranges[id1].push(current_range2.r + 1, current_range1.r); continue; } } } int main() { scanf("%d%d", &n, &m); for (int i = 0; i < m; ++i) { scanf("%d%d%d", &l, &r, &k); ranges[k - 1].push(l, r); } for (int i = 0; i < 3; ++i) merge_ranges(i); reverse_range(5, 6); intersect_ranges(3, 4, 7); intersect_ranges(6, 7, 8); int ans = 0; while (!ranges[8].empty()) { Range tmp = ranges[8].pop(); ans += tmp.r - tmp.l + 1; } printf("%d\n", ans); 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 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 | #include<cstdio> struct Range { int l; int r; Range() {} Range(int l, int r) { this->l = l; this->r = r; } }; class PQ { bool compare(const int pos1, const int pos2) { if (entries[pos1].l < entries[pos2].l) return true; if (entries[pos2].l < entries[pos1].l) return false; return entries[pos2].r < entries[pos1].r; } void swap(const int pos1, const int pos2) { Range tmp = entries[pos1]; entries[pos1] = entries[pos2]; entries[pos2] = tmp; } void HeapUp(int pos) { if (pos == 1) return; int parent = pos / 2; if (compare(pos, parent)) { swap(pos, parent); HeapUp(parent); } } void HeapDown(int pos) { if (count < 2 * pos) return; int smaller = 2 * pos; if (smaller + 1 <= count && compare(smaller + 1, smaller)) smaller++; if (compare(smaller, pos)) { swap(smaller, pos); HeapDown(smaller); } } public: int size; int count; Range* entries; PQ() { this->size = 2; this->count = 0; this->entries = new Range[this->size]; } void push(Range range_to_push) { count++; if (count == size) { size *= 2; Range* next = new Range[size]; for (int i = 1; i < count; ++i) next[i] = entries[i]; delete[] entries; entries = next; } entries[count] = range_to_push; HeapUp(count); } void push(int l, int r) { push(Range(l, r)); } Range pop() { Range result = entries[1]; entries[1] = entries[count--]; HeapDown(1); return result; } bool empty() { return count == 0; } }; PQ ranges[10]; int n, m, l, r, k; void merge_ranges(int id) { if (ranges[id].empty()) return; Range current_range = ranges[id].pop(); Range next_range; while (!ranges[id].empty()) { next_range = ranges[id].pop(); if (current_range.r + 1 < next_range.l) { ranges[id + 3].push(current_range); current_range = next_range; } else current_range.r = current_range.r < next_range.r ? next_range.r : current_range.r; } ranges[id + 3].push(current_range); } void reverse_range(int id1, int id2) { Range current_range; int new_l = 1; while (!ranges[id1].empty()) { current_range = ranges[id1].pop(); ranges[id2].push(new_l, current_range.l - 1); new_l = current_range.r + 1; } ranges[id2].push(new_l, n); } void intersect_ranges(int id1, int id2, int id3) { if (ranges[id1].empty() || ranges[id2].empty()) return; Range current_range1; Range current_range2; while (!ranges[id1].empty() && !ranges[id2].empty()) { current_range1 = ranges[id1].pop(); current_range2 = ranges[id2].pop(); if (current_range1.r < current_range2.l) { ranges[id2].push(current_range2); continue; } if (current_range2.r < current_range1.l) { ranges[id1].push(current_range1); continue; } if (current_range1.l < current_range2.l && current_range1.r < current_range2.r) { ranges[id3].push(current_range2.l, current_range1.r); ranges[id2].push(current_range1.r + 1, current_range2.r); continue; } if (current_range2.l <= current_range1.l && current_range1.r < current_range2.r) { ranges[id3].push(current_range1.l, current_range1.r); ranges[id2].push(current_range1.r + 1, current_range2.r); continue; } if (current_range1.l < current_range2.l && current_range2.r <= current_range1.r) { ranges[id3].push(current_range2.l, current_range2.r); if (current_range2.r < current_range1.r) ranges[id1].push(current_range2.r + 1, current_range1.r); continue; } if (current_range2.l <= current_range1.l && current_range2.r <= current_range1.r) { ranges[id3].push(current_range1.l, current_range2.r); if (current_range2.r < current_range1.r) ranges[id1].push(current_range2.r + 1, current_range1.r); continue; } } } int main() { scanf("%d%d", &n, &m); for (int i = 0; i < m; ++i) { scanf("%d%d%d", &l, &r, &k); ranges[k - 1].push(l, r); } for (int i = 0; i < 3; ++i) merge_ranges(i); reverse_range(5, 6); intersect_ranges(3, 4, 7); intersect_ranges(6, 7, 8); int ans = 0; while (!ranges[8].empty()) { Range tmp = ranges[8].pop(); ans += tmp.r - tmp.l + 1; } printf("%d\n", ans); return 0; } |