Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <numeric>
- #include <algorithm>
- #include <string>
- #include <vector>
- #include <list>
- #include <iostream>
- using namespace std;
- using itvl = std::pair< int, int >;
- using Litvl = std::list< itvl >;
- vector< itvl > intervalz { {1,2}, {1,5}, {2,3}, {6,8},
- {7,10}, {8,11}, {14,18}, {99,99}
- };
- struct Acc {
- itvl current; // {0,0};
- Litvl list ; // {};
- } acc;
- auto consume = [] ( Acc& acc, const itvl& i) // assumes sorted on interval first element
- {
- if ( i.first > acc.current.second ) { // disjoint intervals
- acc.list.push_back( acc.current );
- acc.current = i;
- }
- else // overlap
- if ( i.second > acc.current.second ) {
- acc.current.second = i.second; // -> extend
- }
- else
- ; // -> swallow
- return acc;
- };
- int main()
- {
- auto d = accumulate ( intervalz.begin(),
- intervalz.end(),
- acc,
- consume);
- cout << d.current.first << " " << d.current.second << "\n\n";
- for ( auto i : d.list) {
- cout << i.first << " " << i.second << "\n";
- }
- getchar();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement