Language: C++

#include <iostream>
#include <unordered_set>
#include <algorithm>
#include <utility>
#include <cstdlib>
#include <vector>

class interval {
public:
  int id;
  int begin;
  int end;
  constexpr interval() : id(0), begin(0), end(0) {}
  constexpr interval(const int id, const int begin, const int end) : id(id), begin(begin), end(end) {}
  friend constexpr bool operator== (const interval& lhs, const interval& rhs) {
    return lhs.id == rhs.id;
  }
  constexpr int next_position(const int current_position) const {
    return current_position < this->begin ? this->begin : this->end < current_position ? this->end : 0;
  }
};

namespace std {
  template <> struct hash<::interval> {
    std::size_t operator()(const ::interval& self) const {
      return hash<int>()(self.id);
    }
  };
}

int main() {
  int n;
  std::cin >> n;
  std::unordered_set<interval> used_intervals();
  std::vector<interval> intervals_order_by_begin_desc(n);
  std::vector<interval> intervals_order_by_end_asc(n);
  for (int i = 0; i < n; ++i) {
    int l, r;
    std::cin >> l >> r;
    intervals_order_by_begin_desc[i] = interval(i, l, r);
    intervals_order_by_end_asc[i] = interval(i, l, r);
  }
  std::sort(intervals_order_by_begin_desc.begin(), intervals_order_by_begin_desc.end(), [](const interval& x, const interval& y){ return x.begin > y.begin; });
  std::sort(intervals_order_by_end_asc.begin(), intervals_order_by_end_asc.end(), [](const interval& x, const interval& y){ return x.end < y.end; });
  int begins_cursor = 0;
  int ends_cursor = 0;
  int current_position = 0;
  int distance = 0;
  const int toggle = std::abs(intervals_order_by_begin_desc[0].next_position(0)) > std::abs(intervals_order_by_end_asc[0].next_position(0)) ? 0 : 1;
  for (int i = 0; i < n; ++i) {
    const interval current_interval = [&](){
      if (i % 2 == toggle) {
        for (; used_intervals.find(intervals_order_by_begin_desc[begins_cursor]) != used_intervals.end(); ++begins_cursor);
        return intervals_order_by_begin_desc[begins_cursor];
      } else {
        for (; used_intervals.find(intervals_order_by_end_asc[ends_cursor]) != used_intervals.end(); ++ends_cursor);
        return intervals_order_by_end_asc[ends_cursor];
      }
    }();
    const int old_position = current_position;
    current_position = current_interval.next_position(current_position);
    distance += std::abs(current_position - old_position);
    used_intervals.insert(current_interval);
  }
  distance += std::abs(current_position);
  std::cout << distance << std::endl;
  return 0;
}


Compiler output:
a.cc: In function 'int main()':
a.cc:34:46: warning: empty parentheses were disambiguated as a function declaration [-Wvexing-parse]
   34 |   std::unordered_set<interval> used_intervals();
      |                                              ^~
a.cc: In lambda function:
a.cc:53:31: error: request for member 'find' in 'used_intervals', which is of non-class type 'std::unordered_set<interval>()'
   53 |         for (; used_intervals.find(intervals_order_by_begin_desc[begins_cursor]) != used_intervals.end(); ++begins_cursor);
      |                               ^~~~
a.cc:53:100: error: request for member 'end' in 'used_intervals', which is of non-class type 'std::unordered_set<interval>()'
   53 |         for (; used_intervals.find(intervals_order_by_begin_desc[begins_cursor]) != used_intervals.end(); ++begins_cursor);
      |                                                                                                    ^~~
a.cc:56:31: error: request for member 'find' in 'used_intervals', which is of non-class type 'std::unordered_set<interval>()'
   56 |         for (; used_intervals.find(intervals_order_by_end_asc[ends_cursor]) != used_intervals.end(); ++ends_cursor);
      |                               ^~~~
a.cc:56:95: error: request for member 'end' in 'used_intervals', which is of non-class type 'std::unordered_set<interval>()'
   56 |         for (; used_intervals.find(intervals_order_by_end_asc[ends_cursor]) != used_intervals.end(); ++ends_cursor);
      |                                                                                               ^~~
a.cc: In function 'int main()':
a.cc:63:20: error: request for member 'insert' in 'used_intervals', which is of non-class type 'std::unordered_set<interval>()'
   63 |     used_intervals.insert(current_interval);
      |                    ^~~~~~