#include <iostream> #include <cstdint> #include <vector> #include <utility> #include <algorithm> #include <bitset> #include <limits> #include <unordered_map> using namespace std; struct bla{ int position; int colour; int operation; // +1 add, -1 remove bool operator < (const bla &rhs) const { return (position<rhs.position); } }; int main(){ std::ios::sync_with_stdio(false); cin.tie(0); int n, m; cin >>n>>m; vector <bla> tab; tab.reserve(m*2); while (m-->0){ int l, r, k; cin>>l>>r>>k; tab.push_back(bla{l,k,1}); tab.push_back(bla{r+1,k,-1}); } sort (begin(tab), end(tab)); int counter =0; int last_position =1; int colours[3]={0,0,0}; for (auto it = begin(tab); it!=end(tab);++it){ if ((colours[0]>0)&&(colours[1]>0)&&(colours[2]==0)) counter += it->position - last_position; colours[it->colour-1] += it->operation; last_position = it->position; } cout<< counter<<endl; 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 | #include <iostream> #include <cstdint> #include <vector> #include <utility> #include <algorithm> #include <bitset> #include <limits> #include <unordered_map> using namespace std; struct bla{ int position; int colour; int operation; // +1 add, -1 remove bool operator < (const bla &rhs) const { return (position<rhs.position); } }; int main(){ std::ios::sync_with_stdio(false); cin.tie(0); int n, m; cin >>n>>m; vector <bla> tab; tab.reserve(m*2); while (m-->0){ int l, r, k; cin>>l>>r>>k; tab.push_back(bla{l,k,1}); tab.push_back(bla{r+1,k,-1}); } sort (begin(tab), end(tab)); int counter =0; int last_position =1; int colours[3]={0,0,0}; for (auto it = begin(tab); it!=end(tab);++it){ if ((colours[0]>0)&&(colours[1]>0)&&(colours[2]==0)) counter += it->position - last_position; colours[it->colour-1] += it->operation; last_position = it->position; } cout<< counter<<endl; return 0; } |