#include <iostream> #include <algorithm> #include <cmath> using namespace std; struct iv { int begin, end; short col; } tab[1000000]; int n, m; int main() { ios_base::sync_with_stdio(0); cin >> n >> m; int i, num = 0; for (i = 0; i < m; i++) { cin >> tab[i].begin >> tab[i].end >> tab[i].col; } sort( tab, tab + m, [](const iv& a, const iv& b) -> bool { return a.begin < b.begin; }); int yend, bend, beg, bprev = 0; beg = yend = bend = 0; for (i = 0; i < m; i++) { if (yend != 0 && bend != 0 && tab[i].begin > bprev) num += max( 0, min( tab[i].begin - 1, min(yend, bend) ) - beg + 1 ); beg = max(tab[i].begin, beg); switch (tab[i].col) { case 1: // y yend = max(tab[i].end, yend); break; case 2: // b bend = max(tab[i].end, bend); break; case 3: // r beg = max(tab[i].end + 1, beg); break; } bprev = tab[i].begin; } if (yend != 0 && bend != 0) num += max( 0, min( n, min(yend, bend) ) - beg + 1 ); cout << num; 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 | #include <iostream> #include <algorithm> #include <cmath> using namespace std; struct iv { int begin, end; short col; } tab[1000000]; int n, m; int main() { ios_base::sync_with_stdio(0); cin >> n >> m; int i, num = 0; for (i = 0; i < m; i++) { cin >> tab[i].begin >> tab[i].end >> tab[i].col; } sort( tab, tab + m, [](const iv& a, const iv& b) -> bool { return a.begin < b.begin; }); int yend, bend, beg, bprev = 0; beg = yend = bend = 0; for (i = 0; i < m; i++) { if (yend != 0 && bend != 0 && tab[i].begin > bprev) num += max( 0, min( tab[i].begin - 1, min(yend, bend) ) - beg + 1 ); beg = max(tab[i].begin, beg); switch (tab[i].col) { case 1: // y yend = max(tab[i].end, yend); break; case 2: // b bend = max(tab[i].end, bend); break; case 3: // r beg = max(tab[i].end + 1, beg); break; } bprev = tab[i].begin; } if (yend != 0 && bend != 0) num += max( 0, min( n, min(yend, bend) ) - beg + 1 ); cout << num; return 0; } |