#include <iostream> #include <vector> #include <algorithm> int n, m; struct Op { bool start; int pos; int col; }; std::vector<Op> ops; int main() { std::cin >> n >> m; ops.reserve(m*2); for(int i=0;i<m;++i) { int l,r,k; std::cin >> l >> r >> k; ops.push_back({true,l-1,k}); ops.push_back({false,r,k}); } std::sort(ops.begin(),ops.end(),[](const Op& a, const Op& b){ return a.pos<b.pos||(a.pos==b.pos&&!a.start&&b.start);}); int res=0; int col[3]={0}; int pos=0; int i=0; while(i<ops.size()) { int new_pos=ops[i].pos; if(col[0]&&col[1]&&!col[2]) res+=new_pos-pos; while(i<ops.size()&&ops[i].pos==new_pos) { col[ops[i].col-1]+=ops[i].start?1:-1; ++i; } pos=new_pos; } std::cout << res; 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 | #include <iostream> #include <vector> #include <algorithm> int n, m; struct Op { bool start; int pos; int col; }; std::vector<Op> ops; int main() { std::cin >> n >> m; ops.reserve(m*2); for(int i=0;i<m;++i) { int l,r,k; std::cin >> l >> r >> k; ops.push_back({true,l-1,k}); ops.push_back({false,r,k}); } std::sort(ops.begin(),ops.end(),[](const Op& a, const Op& b){ return a.pos<b.pos||(a.pos==b.pos&&!a.start&&b.start);}); int res=0; int col[3]={0}; int pos=0; int i=0; while(i<ops.size()) { int new_pos=ops[i].pos; if(col[0]&&col[1]&&!col[2]) res+=new_pos-pos; while(i<ops.size()&&ops[i].pos==new_pos) { col[ops[i].col-1]+=ops[i].start?1:-1; ++i; } pos=new_pos; } std::cout << res; return 0; } |