All tests in forum didn't help me :( WA5
#include <bits/stdc++.h>
#define f first
#define s second
#define pb push_back
#define ppb pop_back
#define mp make_pair
#define pii pair <int, int>
#define pll pair <ll, ll>
#define ld long double
#define ll long long
#define ull unsigned ll
#define bit(x) __builtin_popcountll(x)
#define all(x) x.begin(), x.end()
#define sqr(x) ((x) * 1ll * (x))
#define sz(x) (int)x.size()
#define purple ios_base :: sync_with_stdio(0), cin.tie(0), cout.tie(0);
#define rep(_i, _from, _to) for (int _i = _from; _i <= _to; _i++)
#define per(_i, _from, _to) for (int _i = _from; _i >= _to; _i--)
#define nl '\n'
#define ioi exit(0);
#define _225day ""
using namespace std;
const int N = 1e6 + 7, mod = 1e9 + 9, inf = 1e9 + 7;
const ll linf = (ll)1e18 + 7;
const ld eps = 1e-15, pi = 3.141592;
const int dx[] = {-1, 0, 1, 0, 1, -1, -1, 1}, dy[] = {0, 1, 0, -1, 1, -1, 1, -1};
int n, m, base = 50000;
int s[N];
pii a[N];
struct Tree{
int ans, add;
} t[N << 2];
inline bool cmp(pii x, pii y){
if (x.f != y.f) return x.f < y.f;
}
inline bool Check(int pref){
memset(s, 0, sizeof(s));
rep(i, 1, pref)
s[a[i].f]++, s[a[i].s + 1]--;
rep(i, 0, m + base){
s[i] += s[i - 1];
if (i > base && !s[i]) return 0;
}
return 1;
}
inline void Push(int v, int tl, int tr){
if (t[v].add && tl != tr){
t[v + v].add += t[v].add, t[v + v + 1].add += t[v].add;
t[v + v].ans += t[v].add, t[v + v + 1].ans += t[v].add;
t[v].add = 0;
}
}
inline void Update(int l, int r, int add, int v = 1, int tl = 0, int tr = N){
Push(v, tl, tr);
if (l <= tl && tr <= r){
t[v].ans += add;
t[v].add += add;
return;
}
if (tl > r || tr < l) return;
int tm = tl + tr >> 1;
Update(l, r, add, v + v, tl, tm);
Update(l, r, add, v + v + 1, tm + 1, tr);
t[v].ans = min(t[v + v].ans, t[v + v + 1].ans);
}
inline int Get(int l, int r, int v = 1, int tl = 0, int tr = N){
Push(v, tl, tr);
if (l <= tl && tr <= r) return t[v].ans;
if (tl > r || tr < l) return inf;
int tm = tl + tr >> 1;
return min(Get(l, r, v + v, tl, tm), Get(l, r, v + v + 1, tm + 1, tr));
}
int main(){
#ifndef _225day
freopen (_225day".in", "r", stdin);
freopen (_225day".out", "w", stdout);
#endif
cin >> m;
while (1){
int x, y;
cin >> x >> y;
if (x == 0 && y == 0) break;
a[++n] = {x + base + 1, y + base};
}
sort (a + 1, a + 1 + n, cmp);
int l = 1, r = n, ans = -1, mid;
while (l <= r){
mid = l + r >> 1;
if (Check(mid)) ans = mid, r = mid - 1;
else l = mid + 1;
}
if (ans == -1) cout << "No solution", ioi
ans = n;
rep(i, 1, ans)
Update(a[i].f, a[i].s, 1);
vector <pii> res;
rep(i, 1, ans){
Update(a[i].f, a[i].s, -1);
if (!Get(base + 1, m + base))
Update(a[i].f, a[i].s, 1), res.pb({a[i].f, a[i].s});
}
sort (all(res), cmp);
cout << sz(res) << nl;
for (auto i : res)
cout << i.f - base - 1 << ' ' << i.s - base << nl;
ioi
}