#include <algorithm> #include <cstdio> #include <set> #include <utility> #include <vector> struct Range { int a, b; int i; }; struct RangeP : Range { int p; }; int n, k, m; struct Points { int n2; int n_; int min_pointer; std::vector<int> count; std::vector<int> can_move; std::vector<int> add; std::vector<int> right; void Drop(int w) { can_move[w] += add[w]; if (w < n2) { add[w * 2] += add[w]; add[w * 2 + 1] += add[w]; } add[w] = 0; } void DropOnPath(int w) { if (w) { DropOnPath(w / 2); Drop(w); } } void Subtract(int w, int p, int k, int a, int b) { Drop(w); if (k < a or b < p) { return; } if (a <= p and k <= b) { add[w]--; } else { Subtract(w * 2, p, (p + k) / 2, a, b); Subtract(w * 2 + 1, (p + k + 1) / 2, k, a, b); } } void Erase(int x) { const int pos = std::upper_bound(right.begin(), right.end(), x) - right.begin() - 1; int poc = pos; int w = n2 + pos; DropOnPath(w); if (can_move[w] != 0) { poc = -1; while (w) { if (w % 2 == 1) { Drop(w); Drop(w - 1); if (can_move[w - 1] == 0) { w--; while (w < n2) { w *= 2; Drop(w); Drop(w + 1); if (can_move[w + 1] == 0) { w++; } } poc = w - n2; break; } } w /= 2; } } poc = std::max(poc, min_pointer); count[poc]--; Subtract(1, 0, n2 - 1, poc + 1, pos); } int Min() { while (count[min_pointer] == 0) { min_pointer++; } return right[min_pointer] - count[min_pointer] + 1; } void SetN(int n) { n_ = n; n2 = 1; while (n2 < n) { n2 *= 2; } count.resize(n); right.resize(n); can_move.resize(2 * n2, 0); add.resize(2 * n2, 0); } void SetCount(int pos, int cnt) { count[pos] = cnt; } void SetRight(int pos, int r) { right[pos] = r; } void SetMove(int pos, int cnt_move) { can_move[pos + n2] = cnt_move; } void Init() { min_pointer = 0; for (int i = 1; i < n_; i++) { if (right[i] == right[i - 1]) { can_move[i + n2] = can_move[i - 1 + n2] + count[i - 1]; } } for (int i = n2 - 1; i >= 1; i--) { can_move[i] = std::min(can_move[2 * i], can_move[2 * i + 1]); } } }; std::vector<RangeP> ranges; std::vector<Range> group[1000005]; Points points[1000005]; // Deletes the biggest point <= x. void ErasePoint(int g, int x) { points[g].Erase(x); } int MinPoint(int g) { return points[g].Min(); } bool ComputeGroup(int g) { std::sort(group[g].begin(), group[g].end(), [](const Range& a, const Range& b) { return a.b > b.b; }); struct compare_by_a { bool operator()(const Range& a, const Range& b) { if (a.a == b.a) { return a.i < b.i; } return a.a > b.a; } }; std::set<Range, compare_by_a> active_ranges; int im_at = 1e9 + 1; auto collect = [&active_ranges, &im_at]() -> bool { const Range first = *active_ranges.begin(); active_ranges.erase(active_ranges.begin()); if (first.a > im_at) { return false; } im_at--; return true; }; int n = (int) group[g].size(); points[g].SetN(n); int cnt = 0; for (int i = 0; i < n; i++) { const Range& range = group[g][i]; points[g].SetRight(n - 1 - i, range.b); while (!active_ranges.empty() and im_at > range.b) { if (!collect()) { return false; } cnt++; } if (i) { points[g].SetCount(n - 1 - (i - 1), cnt); points[g].SetMove(n - 1 - (i - 1), (int) active_ranges.size()); cnt = 0; } active_ranges.insert(range); im_at = range.b; } while (!active_ranges.empty()) { if (!collect()) { return false; } cnt++; } points[g].SetCount(0, cnt); points[g].SetMove(0, 0); points[g].Init(); return true; } int position[1000005]; struct compare_by_b { bool operator()(const Range& a, const Range& b) { if (a.b == b.b) { return a.i < b.i; } return a.b < b.b; } }; std::set<Range, compare_by_b> active_in_groups[1000005]; void ComputeEverything() { std::sort(ranges.begin(), ranges.end(), [](const RangeP& a, const RangeP& b) { return a.a < b.a; }); std::set<std::pair<int, int>> last_moments; int im_at = 0; auto collect = [&last_moments, &im_at]() -> void { std::vector<int> which_groups; for (const auto& moment : last_moments) { which_groups.push_back(moment.second); } last_moments.clear(); for (int g : which_groups) { const Range first = *active_in_groups[g].begin(); active_in_groups[g].erase(active_in_groups[g].begin()); position[first.i] = im_at; ErasePoint(g, first.b); if (!active_in_groups[g].empty()) { last_moments.insert({MinPoint(g), g}); } } im_at++; }; for (const RangeP& range : ranges) { while (!last_moments.empty() and last_moments.begin()->first < range.a) { collect(); } const int g = range.p; if (active_in_groups[g].empty()) { last_moments.insert({MinPoint(g), g}); } active_in_groups[g].insert(range); im_at = range.a; } while (!last_moments.empty()) { collect(); } } bool ComputeGroups() { for (int i = 1; i <= m; i++) { if (!ComputeGroup(i)) { return false; } } return true; } int main() { scanf("%d%d", &n, &k); ranges.resize(n); for (int i = 0; i < n; i++) { RangeP& r = ranges[i]; r.i = i; scanf("%d%d%d", &r.a, &r.b, &r.p); } std::sort(ranges.begin(), ranges.end(), [](const RangeP& a, const RangeP& b) { return a.p < b.p; }); int next_i = 0; for (int i = 0; i < n; i++) { int p_value = ranges[i].p; next_i++; ranges[i].p = next_i; while (i + 1 < n and ranges[i + 1].p == p_value) { ranges[++i].p = next_i; } } m = next_i; for (int i = 0; i < n; i++) { group[ranges[i].p].push_back(ranges[i]); } if (!ComputeGroups()) { printf("NIE\n"); return 0; } ComputeEverything(); std::vector<int> all_positions; for (int i = 0; i < n; i++) { all_positions.push_back(position[i]); } std::sort(all_positions.begin(), all_positions.end()); printf("%d\n", (int) (std::unique(all_positions.begin(), all_positions.end()) - all_positions.begin())); for (int i = 0; i < n; i++) { printf("%d\n", position[i]); } 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 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 | #include <algorithm> #include <cstdio> #include <set> #include <utility> #include <vector> struct Range { int a, b; int i; }; struct RangeP : Range { int p; }; int n, k, m; struct Points { int n2; int n_; int min_pointer; std::vector<int> count; std::vector<int> can_move; std::vector<int> add; std::vector<int> right; void Drop(int w) { can_move[w] += add[w]; if (w < n2) { add[w * 2] += add[w]; add[w * 2 + 1] += add[w]; } add[w] = 0; } void DropOnPath(int w) { if (w) { DropOnPath(w / 2); Drop(w); } } void Subtract(int w, int p, int k, int a, int b) { Drop(w); if (k < a or b < p) { return; } if (a <= p and k <= b) { add[w]--; } else { Subtract(w * 2, p, (p + k) / 2, a, b); Subtract(w * 2 + 1, (p + k + 1) / 2, k, a, b); } } void Erase(int x) { const int pos = std::upper_bound(right.begin(), right.end(), x) - right.begin() - 1; int poc = pos; int w = n2 + pos; DropOnPath(w); if (can_move[w] != 0) { poc = -1; while (w) { if (w % 2 == 1) { Drop(w); Drop(w - 1); if (can_move[w - 1] == 0) { w--; while (w < n2) { w *= 2; Drop(w); Drop(w + 1); if (can_move[w + 1] == 0) { w++; } } poc = w - n2; break; } } w /= 2; } } poc = std::max(poc, min_pointer); count[poc]--; Subtract(1, 0, n2 - 1, poc + 1, pos); } int Min() { while (count[min_pointer] == 0) { min_pointer++; } return right[min_pointer] - count[min_pointer] + 1; } void SetN(int n) { n_ = n; n2 = 1; while (n2 < n) { n2 *= 2; } count.resize(n); right.resize(n); can_move.resize(2 * n2, 0); add.resize(2 * n2, 0); } void SetCount(int pos, int cnt) { count[pos] = cnt; } void SetRight(int pos, int r) { right[pos] = r; } void SetMove(int pos, int cnt_move) { can_move[pos + n2] = cnt_move; } void Init() { min_pointer = 0; for (int i = 1; i < n_; i++) { if (right[i] == right[i - 1]) { can_move[i + n2] = can_move[i - 1 + n2] + count[i - 1]; } } for (int i = n2 - 1; i >= 1; i--) { can_move[i] = std::min(can_move[2 * i], can_move[2 * i + 1]); } } }; std::vector<RangeP> ranges; std::vector<Range> group[1000005]; Points points[1000005]; // Deletes the biggest point <= x. void ErasePoint(int g, int x) { points[g].Erase(x); } int MinPoint(int g) { return points[g].Min(); } bool ComputeGroup(int g) { std::sort(group[g].begin(), group[g].end(), [](const Range& a, const Range& b) { return a.b > b.b; }); struct compare_by_a { bool operator()(const Range& a, const Range& b) { if (a.a == b.a) { return a.i < b.i; } return a.a > b.a; } }; std::set<Range, compare_by_a> active_ranges; int im_at = 1e9 + 1; auto collect = [&active_ranges, &im_at]() -> bool { const Range first = *active_ranges.begin(); active_ranges.erase(active_ranges.begin()); if (first.a > im_at) { return false; } im_at--; return true; }; int n = (int) group[g].size(); points[g].SetN(n); int cnt = 0; for (int i = 0; i < n; i++) { const Range& range = group[g][i]; points[g].SetRight(n - 1 - i, range.b); while (!active_ranges.empty() and im_at > range.b) { if (!collect()) { return false; } cnt++; } if (i) { points[g].SetCount(n - 1 - (i - 1), cnt); points[g].SetMove(n - 1 - (i - 1), (int) active_ranges.size()); cnt = 0; } active_ranges.insert(range); im_at = range.b; } while (!active_ranges.empty()) { if (!collect()) { return false; } cnt++; } points[g].SetCount(0, cnt); points[g].SetMove(0, 0); points[g].Init(); return true; } int position[1000005]; struct compare_by_b { bool operator()(const Range& a, const Range& b) { if (a.b == b.b) { return a.i < b.i; } return a.b < b.b; } }; std::set<Range, compare_by_b> active_in_groups[1000005]; void ComputeEverything() { std::sort(ranges.begin(), ranges.end(), [](const RangeP& a, const RangeP& b) { return a.a < b.a; }); std::set<std::pair<int, int>> last_moments; int im_at = 0; auto collect = [&last_moments, &im_at]() -> void { std::vector<int> which_groups; for (const auto& moment : last_moments) { which_groups.push_back(moment.second); } last_moments.clear(); for (int g : which_groups) { const Range first = *active_in_groups[g].begin(); active_in_groups[g].erase(active_in_groups[g].begin()); position[first.i] = im_at; ErasePoint(g, first.b); if (!active_in_groups[g].empty()) { last_moments.insert({MinPoint(g), g}); } } im_at++; }; for (const RangeP& range : ranges) { while (!last_moments.empty() and last_moments.begin()->first < range.a) { collect(); } const int g = range.p; if (active_in_groups[g].empty()) { last_moments.insert({MinPoint(g), g}); } active_in_groups[g].insert(range); im_at = range.a; } while (!last_moments.empty()) { collect(); } } bool ComputeGroups() { for (int i = 1; i <= m; i++) { if (!ComputeGroup(i)) { return false; } } return true; } int main() { scanf("%d%d", &n, &k); ranges.resize(n); for (int i = 0; i < n; i++) { RangeP& r = ranges[i]; r.i = i; scanf("%d%d%d", &r.a, &r.b, &r.p); } std::sort(ranges.begin(), ranges.end(), [](const RangeP& a, const RangeP& b) { return a.p < b.p; }); int next_i = 0; for (int i = 0; i < n; i++) { int p_value = ranges[i].p; next_i++; ranges[i].p = next_i; while (i + 1 < n and ranges[i + 1].p == p_value) { ranges[++i].p = next_i; } } m = next_i; for (int i = 0; i < n; i++) { group[ranges[i].p].push_back(ranges[i]); } if (!ComputeGroups()) { printf("NIE\n"); return 0; } ComputeEverything(); std::vector<int> all_positions; for (int i = 0; i < n; i++) { all_positions.push_back(position[i]); } std::sort(all_positions.begin(), all_positions.end()); printf("%d\n", (int) (std::unique(all_positions.begin(), all_positions.end()) - all_positions.begin())); for (int i = 0; i < n; i++) { printf("%d\n", position[i]); } return 0; } |