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 <algorithm>
#include <vector>

using namespace std;

template <class T>
struct SegmentTree/*{{{*/
{
    vector<T> nodes;
    SegmentTree(int size) {
        nodes.resize(size*2, 0);
    }

    void maintain_consistency(int pos) {
        // テ」ツδ偲」ツδシテ」ツδ嘉」ツ?ョテ・ツ?、テ」ツ?古・ツュツ静」ツ?ョテ・ツ青暗ィツィツ暗」ツ?ィテ、ツクツ?ィツ?エテ」ツ?療」ツ?ェテ」ツ??・ツ?エテ・ツ青暗ッツシツ古、ツクツ催・ツョツ古・ツ?ィテ」ツ?ェテヲツ崢エテヲツ鳴ーテ」ツ?古ィツ。ツ古」ツ?ェテ」ツつ湘」ツつ古」ツ?ヲテ」ツ??」ツつ凝」ツ?淌」ツつ?
        // テ、ツクツ凝」ツ?ョテ」ツδ偲」ツδシテ」ツδ嘉」ツ?ォテ、ツシツ敕ヲツ青ャテ」ツ?陛」ツ?崚」ツつ凝ッツシツ?
        int children_sum = nodes[pos*2+1] + nodes[pos*2+2];
        if(children_sum != nodes[pos]) { 
            //cout << "maintain: " << pos << ' ' << nodes[pos] << ' ' << children_sum << endl;
            int diff = nodes[pos] - children_sum;
            nodes[pos*2+1] += diff/2;
            nodes[pos*2+2] += diff/2;
        }
    }

    // [left, right) テ」ツ?ォテ・ツッツセテ」ツ?凖」ツつ凝」ツつッテ」ツつィテ」ツδェテッツシツ?
    // テァツ渉セテ・ツ慊ィテ」ツ?ョテ」ツδ偲」ツδシテ」ツδ嘉」ツ?ッpos テ」ツ?ァテッツシツ?[pl, pr) テ」ツつ津ィツ。ツィテ」ツつ湘」ツ?療」ツ?ヲテ」ツ??」ツつ凝ッツシツ?
    int get_inner(int left, int right, int pos, int pl, int pr) {
        //cout << left << ' ' << right << ' ' << pos << ' ' <<  pl << ' ' << pr << endl;
        if(pr <= left || right <= pl) return 0; // テ、ツコツ、テ・ツキツョテ」ツ?療」ツ?ェテ」ツ??
        if(left <= pl && pr <= right) return nodes[pos]; // テ・ツョツ古・ツ?ィテ」ツ?ォテ・ツ青ォテ」ツ?セテ」ツつ古」ツつ?

        maintain_consistency(pos);

        const int center = (pl+pr) / 2;
        int lv = get_inner(left, right, pos*2+1, pl, center);
        int rv = get_inner(left, right, pos*2+2, center, pr);
        return lv + rv;
    }

    int get(int left, int right) {
        return get_inner(left, right, 0, 0, nodes.size()/2);
    }

    int add_inner(int left, int right, int pos, int pl, int pr) {
        //cout << left << ' ' << right << ' ' << pos << ' ' <<  pl << ' ' << pr << endl;
        if(pr <= left || right <= pl) return nodes[pos]; // テ、ツコツ、テ・ツキツョテ」ツ?療」ツ?ェテ」ツ??
        if(left <= pl && pr <= right) return nodes[pos] += pr-pl; // テ・ツョツ古・ツ?ィテ」ツ?ォテ・ツ青ォテ」ツ?セテ」ツつ古」ツつ?

        maintain_consistency(pos);

        const int center = (pl+pr)/2;
        int lv = add_inner(left, right, pos*2+1, pl, center);
        int rv = add_inner(left, right, pos*2+2, center, pr);
        return nodes[pos] = lv+rv;
    }

    int add(int left, int right) {
        return add_inner(left, right, 0, 0, nodes.size()/2);
    }
};/*}}}*/

int main() {
    cin.tie(0);
    ios::sync_with_stdio(0);
    int N;
    cin >> N;
    SegmentTree<int> segtree(1<<17);
    for(int i = 0; i < N; ++i) {
        int minval, maxval;
        cin >> minval >> maxval;
        segtree.add(minval-1, maxval);
    }
    //cout << "Added" << endl;
    //cout << segtree.nodes[0] << ' ' << segtree.nodes[1] << ' ' << segtree.nodes[2] << endl;
    int ans = 0;
    for(int n = N; n >= 0; --n) {
        //cout << segtree.nodes[0] << ' ' << segtree.nodes[1] << ' ' << segtree.nodes[2] << endl;
        const int val = segtree.get(n, n+1);
        //cout << val << endl;
        if(val >= n) {
            ans = n;
            break;
        }
    }
    cout << ans << endl;
    return 0;
}

C++: #include <iostream>
#include <algorithm>

int imos[100010];

using namespace std;


int main() {
  int n;
  cin >> n;
  
  for (int i = 0; i < n; i++) {
    int a, b;
    cin >> a >> b;
    imos[a - 1]++;
    imos[b]--;
  }

  int ans = 0;
  int current = 0;
  for (int i = 1; i < 100010; i++) {
    current += imos[i];
    if (current >= i) {
      ans = i;
    }
  }

  cout << ans << endl;

  return 0;
}

C++: #include<iostream>
using namespace std;
int main(){
  int ma[100005]={};
  int n;
  cin >> n;
  int i,j,k;
  int a,b;
  for(i=0;i<n;i++){
    cin >> a >> b;
    for(j=a;j<=b;j++) ma[j]++;
  }
  int m=0;
  for(i=0;i<100005;i++){
    //if(i<10)
    //cout << i <<":" << ma[i]<<endl;
    if(i<=ma[i]+1)
      m=max(m,i-1);
  }
  cout << m << endl;
  return 0;
}

C++: #include <iostream>
#include <algorithm>
#include <vector>
#include <string>
#include <memory.h>
#include <queue>
#include <cstdio>
#include <cstdlib>
#include <map>
#include <cmath>
using namespace std;

#define rep(i, n) for(int i = 0; i< n; i++)
#define rep2(i, m, n) for(int i = m; i < n; i++)
typedef long long ll;
typedef pair<int, int> P;
const int INF = 1 << 29;
const double EPS = 1e-10;

int cnt[100010];

int main(){
  int N, a, b;
  while(cin >> N){
    int best = 0;
    memset(cnt, 0, sizeof(cnt));
    rep(i, N){
      cin >> a >> b;
      cnt[a]++;
      cnt[++b]--;
    }
    int num = 1;
    rep(i, 100002){
      num += cnt[i];
      if(num >= i) best = i;
    } 
    cout << best - 1 << endl;
  }
  return 0;
}