Language: C++

#define _CRT_SECURE_NO_WARNINGS
#include "bits/stdc++.h"
using namespace std;

#define int int64_t

#define CHOOSE(a) CHOOSE2 a
#define CHOOSE2(a0,a1,a2,a3,x,...) x
#define REP1(i, s, cond, cal) for (signed i = signed(s); i cond; i cal)
#define REP2(i, s, n) REP1(i, s, < signed(n), ++)
#define REP3(i, n) REP2(i, 0, n)
#define rep(...) CHOOSE((__VA_ARGS__,REP1,REP2,REP3))(__VA_ARGS__)
#define rrep(i, s) rep(i, s, >= 0, --)

#define all(c) begin(c), end(c)
#define maxup(ans, x) (ans = (ans < x ? x : ans))
#define minup(ans, x) (ans = (ans > x ? x : ans))

#define breakif(cond) if(cond) break; else

using VV = vector<vector<int>>;
using V = vector<int>;
using P = pair<int, int>;
using IP = pair<int, P>;

template<typename T>
inline void input(vector<T>& v) { for (auto& x : v) cin >> x; }

signed main() {
	int n, m, p, q, r;
	vector<vector<P>> v(5001, vector<P>(5000));
	while (cin >> n && n) {
		cin >> m >> p >> q >> r;
		v[0].clear();
		v[0].push_back(P(1, n));
		vector<P> a(m), b(m), c(m);
		rep(i, m) {
			a.clear(); b.clear(); c.clear();
			v[i + 1].clear();
			int x, y; cin >> x >> y;
			int sum = 0;
			for (P p : v[i]) {
				int tmp = p.second - p.first + 1;
				if (sum + tmp <= x) {
					a.push_back(p);
				} else if (y <= sum) {
					c.push_back(p);
				} else if (x <= sum && sum + tmp <= y) {
					b.push_back(p);
				} else if (sum < x && y < sum + tmp) {
					P fst = P(p.first, p.first + (x - sum) - 1);
					P snd = P(fst.second + 1, fst.second + (y - x));
					P trd = P(snd.second + 1, p.second);
					a.push_back(fst);
					b.push_back(snd);
					c.push_back(trd);
				} else if (sum < x && x < sum + tmp) {
					P fst = P(p.first, p.first + (x - sum) - 1);
					P snd = P(fst.second + 1, p.second);
					a.push_back(fst);
					b.push_back(snd);
				} else if (sum < y && y < sum + tmp) {
					P fst = P(p.first, p.first + (y - sum) - 1);
					P snd = P(fst.second + 1, p.second);
					b.push_back(fst);
					c.push_back(snd);
				}
				sum += tmp;
			}
			v[i + 1].insert(v[i + 1].end(), all(c));
			v[i + 1].insert(v[i + 1].end(), all(b));
			v[i + 1].insert(v[i + 1].end(), all(a));
		}

		int sum = 0;
		int ans = 0;
		p--;
		for (P item : v[m]) {
			int tmp = item.second - item.first + 1;
			P ra = P(1ll << 60, 1ll << 60);
			if (sum + tmp <= p) {
				//skip
			} else if (q <= sum) {
				//skip
			} else if (p <= sum && sum + tmp <= q) {
				ra = item;
			} else if (sum < p && q < sum + tmp) {
				int l = item.first + (p - sum);
				int r = l + (q - p);
				ra = P(l, r);
			} else if (sum < p && p < sum + tmp) {
				int l = item.first + (p - sum);
				int r = item.second;
				ra = P(l, r);
			} else if (sum < q && q < sum + tmp) {
				ra = P(item.first, item.first + (q - sum) - 1);
			}

			ans += max(0ll, min(r, ra.second) - ra.first + 1);
			sum += tmp;
		}
		cout << ans << endl;
	}
}

Compiler output:
a.cc: In function 'int main()':
a.cc:99:35: error: no matching function for call to 'max(long long int, long int)'
   99 |                         ans += max(0ll, min(r, ra.second) - ra.first + 1);
      |                                ~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
In file included from /usr/include/c++/14/algorithm:60,
                 from /usr/include/x86_64-linux-gnu/c++/14/bits/stdc++.h:51,
                 from a.cc:2:
/usr/include/c++/14/bits/stl_algobase.h:257:5: note: candidate: 'template<class _Tp> constexpr const _Tp& std::max(const _Tp&, const _Tp&)'
  257 |     max(const _Tp& __a, const _Tp& __b)
      |     ^~~
/usr/include/c++/14/bits/stl_algobase.h:257:5: note:   template argument deduction/substitution failed:
a.cc:99:35: note:   deduced conflicting types for parameter 'const _Tp' ('long long int' and 'long int')
   99 |                         ans += max(0ll, min(r, ra.second) - ra.first + 1);
      |                                ~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/usr/include/c++/14/bits/stl_algobase.h:303:5: note: candidate: 'template<class _Tp, class _Compare> constexpr const _Tp& std::max(const _Tp&, const _Tp&, _Compare)'
  303 |     max(const _Tp& __a, const _Tp& __b, _Compare __comp)
      |     ^~~
/usr/include/c++/14/bits/stl_algobase.h:303:5: note:   candidate expects 3 arguments, 2 provided
In file included from /usr/include/c++/14/algorithm:61:
/usr/include/c++/14/bits/stl_algo.h:5706:5: note: candidate: 'template<class _Tp> constexpr _Tp std::max(initializer_list<_Tp>)'
 5706 |     max(initializer_list<_Tp> __l)
      |     ^~~
/usr/include/c++/14/bits/stl_algo.h:5706:5: note:   candidate expects 1 argument, 2 provided
/usr/include/c++/14/bits/stl_algo.h:5716:5: note: candidate: 'template<class _Tp, class _Compare> constexpr _Tp std::max(initializer_list<_Tp>, _Compare)'
 5716 |     max(initializer_list<_Tp> __l, _Compare __comp)
      |     ^~~
/usr/include/c++/14/bits/stl_algo.h:5716:5: note:   template argument deduction/substitution failed:
a.cc:99:35: note:   mismatched types 'std::initializer_list<_Tp>' and 'long long int'
   99 |                         ans += max(0ll, min(r, ra.second) - ra.first + 1);
      |                                ~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~