#include <stdio.h> #include <vector> #include<algorithm> using namespace std; struct Op { int pos; int color; bool added; }; struct less_than_key { inline bool operator() (const Op& struct1, const Op& struct2) { return (struct1.pos < struct2.pos); } }; int main() { int n, m, h = 0; scanf("%d %d", &n, &m); vector<Op> ops(2 * m); for (int i = 0; i < m; i++) { int l, r, k; scanf("%d %d %d", &l, &r, &k); ops[2 * i].added = true; ops[2 * i].color = k; ops[2 * i].pos = l; ops[2 * i + 1].added = false; ops[2 * i + 1].color = k; ops[2 * i + 1].pos = r + 1; } sort(ops.begin(), ops.end(), less_than_key()); int r = 0, y = 0, b = 0, last_pos = 0, sum = 0; for (int i = 0; i < 2 * m;) { int cur_pos = ops[i].pos; if (r == 0 && y > 0 && b > 0) { sum += cur_pos - last_pos; } last_pos = cur_pos; while (i < 2 * m && ops[i].pos == cur_pos) { int f = ops[i].added ? 1 : -1; if (ops[i].color == 1) { y += f; } else if (ops[i].color == 2) { b += f; } else if (ops[i].color == 3) { r += f; } i++; } } printf("%d", sum); }
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 | #include <stdio.h> #include <vector> #include<algorithm> using namespace std; struct Op { int pos; int color; bool added; }; struct less_than_key { inline bool operator() (const Op& struct1, const Op& struct2) { return (struct1.pos < struct2.pos); } }; int main() { int n, m, h = 0; scanf("%d %d", &n, &m); vector<Op> ops(2 * m); for (int i = 0; i < m; i++) { int l, r, k; scanf("%d %d %d", &l, &r, &k); ops[2 * i].added = true; ops[2 * i].color = k; ops[2 * i].pos = l; ops[2 * i + 1].added = false; ops[2 * i + 1].color = k; ops[2 * i + 1].pos = r + 1; } sort(ops.begin(), ops.end(), less_than_key()); int r = 0, y = 0, b = 0, last_pos = 0, sum = 0; for (int i = 0; i < 2 * m;) { int cur_pos = ops[i].pos; if (r == 0 && y > 0 && b > 0) { sum += cur_pos - last_pos; } last_pos = cur_pos; while (i < 2 * m && ops[i].pos == cur_pos) { int f = ops[i].added ? 1 : -1; if (ops[i].color == 1) { y += f; } else if (ops[i].color == 2) { b += f; } else if (ops[i].color == 3) { r += f; } i++; } } printf("%d", sum); } |