Go: // A Way to Invite Friends(友だちの誘い方)
// http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=2331&lang=ja
package main

import (
	"bufio"
	"fmt"
	"math"
	"os"
	"strconv"
)

const N_MAX = 1000001

func main() {
	sc := bufio.NewScanner(os.Stdin)
	sc.Split(bufio.ScanWords)

	n := nextInt(sc)
	allow_list := make([]int, N_MAX+100)
	nMax := 0

	for i := 0; i < n; i++ {
		a, b := nextInt(sc), nextInt(sc)
		allow_list[a] += 1
		allow_list[b+1] += -1
		nMax = int(math.Max(float64(nMax), float64(b)))
	}

	for i := 2; i <= N_MAX; i++ {
		allow_list[i] += allow_list[i-1]
	}

	res := 0
	for i := 2; i <= nMax; i++ {
		if i-1 <= allow_list[i] { // i人誘う時に行きたい人の人数を超えないか(自分を含めて)
			res = i - 1 // 自分は除く
		}
	}

	fmt.Println(res)
}

func nextInt(sc *bufio.Scanner) int {
	sc.Scan()
	i, err := strconv.Atoi(sc.Text())
	if err != nil {
		panic(err)
	}
	return i
}

C++: #include <iostream>
#include <iomanip>
#include <sstream>
#include <cstdio>
#include <string>
#include <vector>
#include <algorithm>
#include <complex>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <cassert>
#include <climits>
#include <queue>
#include <set>
#include <map>
#include <valarray>
#include <bitset>
#include <stack>
using namespace std;

#define REP(i,n) for(int i=0;i<(int)n;++i)
#define FOR(i,c) for(__typeof((c).begin())i=(c).begin();i!=(c).end();++i)
#define ALL(c) (c).begin(), (c).end()
typedef long long ll;
typedef pair<int,int> pii;
const int INF = 1<<29;
const double PI = acos(-1);
const double EPS = 1e-8;

int a[100000];
int b[100000];

template<class T> T upper_search(T lb, T ub, bool (*C)(T)) {
  while(ub-lb>1) { // ツつアツつアツづ債適ツ督鳴づ可。
    T mid = (lb+ub)/2;
    if (C(mid)) lb = mid;       // ツ催渉ャツ値ツづーツ仰づ淞づゥツづ按づァlbツづbツづーツ禿シツづェツ妥鳴つヲツづゥ
    else ub = mid;
  }
  return lb;
}
int n;
bool C(int c) {
  int hoge = 0;
  REP(i, n) {
    if (a[i] <= c+1 && c+1 <= b[i]) hoge++;
    if (hoge == c) return 1;
  }
  return 0;
}

int main() {
  while(cin >> n) {
    REP(i,n) cin >> a[i] >> b[i];
    cout << upper_search(0, 100005, C) << endl;
  }
}

C++: #include <stdio.h>

int n, x, y, imos[100003], table[100002];

int main()
{
    scanf("%d", &n);
    
    for(int i = 0; i < n; i++)
    {
        scanf("%d", &x);
        scanf("%d", &y); y++;
        
        imos[x]++;
        imos[y]--;
    }
    
    for(int i = 1; i <= 100001; i++)
    {
        table[i] = table[i - 1] + imos[i];
    }
    
    int ret = 0;
    
    for(int i = 1; i <= 100001; i++)
    {
        if(i - 1 <= table[i])
        {
            ret = i - 1;
        }
    }
    
    printf("%d\n", ret);
    
    return 0;
}

C++: #include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <climits>
#include <iostream>
#include <vector>
#include <string>
#include <sstream>
#include <algorithm>
#include <utility>
#include <set>
#include <map>
#include <stack>
#include <queue>
#include <deque>
#include <functional>

using namespace std;

#define fst first
#define scd second
#define PB push_back
#define MP make_pair
#define rep(i,x) for(int i=0;i<(x);++i)
#define rep1(i,x) for(int i=1;i<=(x);++i)
#define rrep(i,x) for(int i=(x)-1;i>=0;--i)
#define rrep1(i,x) for(int i=(x);i>=1;--i)
#define int long long
#define FOR(i,a,x) for(int i=(a);i<(x);++i)
#define all(a) a.begin(),a.end()
#define rall(a) a.rbegin(),a.rend()
#define omajinai ios::sync_with_stdio(false);cin.tie(0)

typedef long long ll;
typedef vector<int> vi;
typedef pair<int, int> pii;
typedef vector<pii> vpii;

template<typename T>T& max(T&a,T&b){if(a>=b)return a;return b;}
template<typename T>T& min(T&a,T&b){if(a<b)return a;return b;}
template<typename T>bool chmax(T&a,T b){if(a<b){a=b;return true;}return false;}
template<typename T>bool chmin(T&a,T b){if(a>b){a=b;return true;}return false;}
template<typename T>T get(){T a;cin>>a;return a;}
template<typename T>T rev(T a){reverse(all(a));return a;}
template<typename T>istream&operator>>(istream&is,vector<T>&vec){rep(i,vec.size())is>>vec[i];return is;}
template<typename T>vector<T>&sort(vector<T>&a){sort(all(a));return a;}

const int inf = 1e9;
const ll linf = 3e18;
const double eps = 1e-9;

int imos[100005];

signed main()
{
    int N; cin >> N;

    imos[0] = 1;

    rep(i, N) {
        int a, b; cin >> a >> b;

        imos[a]++, imos[b + 1]--;
    }

    rep(i, 100005) if (i) imos[i] += imos[i - 1];

    int ma = 0;

    rep(i, 100005) {
        if (imos[i] >= i) ma = i;
    }

    cout << ma - 1 << endl;
}

C++: #include <bits/stdc++.h>

#define REP(i,n) for(int i=0;i<(int)(n);i++)
#define ALL(x) (x).begin(), (x).end()

using namespace std;

template <typename T> T &chmin(T &a, const T &b) {return a = min(a, b);}
template <typename T> T &chmax(T &a, const T &b) {return a = max(a, b);}

using ll = long long;
using ld = long double;

const int INF = 1e9;
const ld eps = 1e-9, pi = acos(-1.0);

int N;
using pii = pair<int, int>;
int num[100005];

int main(){
	cin >> N;
	vector<pii> req; 
	REP(cnt, N) {
		int a, b;
		cin >> a >> b;
		num[a]++; num[b+1]--;
	}
	num[0]++; num[100000]--;
	REP(i, N+1) num[i+1] += num[i];
	int res = 0;
	REP(i, N+1) if(num[i] >= i) res = max(res, i);
	cout << res-1 << endl;
	return 0;

}

C++: #include <vector>
#include <iostream>
#include <utility>
#include <algorithm>
#include <string>
#include <deque>
#include <queue>
#include <tuple>
#include <queue>
#include <functional>
#include <cmath>
#include <iomanip>
#include <map>
#include <set>
#include <numeric>
#include <unordered_map>
#include <unordered_set>
#include <complex>
#include <iterator>
#include <array>
#include <memory>
#include <stack>
#define vi vector<int>
#define vvi vector<vector<int> >
#define ll long long int
#define vl vector<ll>
#define vvl vector<vector<ll>>
#define vb vector<bool>
#define vc vector<char>
#define vs vector<string>
#define ld long double
#define INF 1e9
#define EPS 0.0000000001
#define rep(i,n) for(int i=0;i<n;i++)
#define loop(i,s,n) for(int i=s;i<n;i++)
#define all(in) in.begin(), in.end()
template<class T, class S> void cmin(T &a, const S &b) { if (a > b)a = b; }
template<class T, class S> void cmax(T &a, const S &b) { if (a < b)a = b; }
#define MAX 9999999
using namespace std;
typedef pair<int, int> pii;
typedef pair<double,double>pdd;
typedef pair<ll,ll>pll;
int main(){
    int imos[1000000]={0};
    int gotiusa; cin>>gotiusa;
    int range=0;
    vector<pii>v;
    rep(i,gotiusa){
        int tiya,rize; cin>>tiya>>rize;
        imos[tiya]++; imos[rize]--;
        cmax(range,rize+1);
        v.push_back(pii(tiya,rize));
    }
    rep(_,100000)
        if(_)imos[_]+=imos[_-1];
    int ans=0;
    rep(i,v.size())
        if(v[i].first<=imos[v[i].first]&&v[i].second>=imos[v[i].second])
            ans++;
    cout<<ans<<endl;
    
}