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<cstdio>
using namespace std;

int main()
{
  int n;
  int num[100000][2];
  int maxa,minb;
  int i,j,k;
  int index;

  cin>>n;

  for(i=0;i<n;i++)
    cin>>num[i][0]>>num[i][1];

  for(k=0;k<n;k++){

    maxa=num[0][0];
    minb=num[0][1];
    index=0;
    for(i=1;i<n;i++){
      if(maxa<num[i][0]){
	maxa=num[i][0];
	index=i;
      }
      if(minb>num[i][1])
	minb=num[i][1];
    }
    if(maxa<=n-k && n-k<=minb){
      cout<<n-k-1<<endl;
      break;
    }
    num[index][0]=0;
    num[index][1]=100001;
  }
  if(k==n-1)
    cout<<"0"<<endl;
}

C++: #include<iostream>
#include<map>
#include<queue>
#include<algorithm>
using namespace std;

#define fs first
#define sc second
typedef pair<int,int> P;

int n;
P p[200100];

int main(){
  cin >> n;
  for(int i=0;i<n;i++)cin >> p[i].fs >> p[i].sc;
  p[n] = P(1,100001);
  sort(p,p+n+1);

  priority_queue<int, vector<int>, greater<int> > q;
  int ans = 1;

  for(int i=0;i<=n;i++){
    while(!q.empty() && q.top() < p[i].fs)q.pop();
    q.push(p[i].sc);    
    if(p[i].fs <= q.size() && q.size() <= q.top())ans = q.size();
  }

  cout << ans-1 << endl;
}

C++: #include<bits/stdc++.h>
using namespace std;

#define AA 100001
 
int main(){
  int N, a, b, temp[AA+100] = {}, ma = 0;

  cin>>N;

  for(int i = 0; i < N; i++){
    cin>>a>>b;
    temp[a]++;
    temp[b+1]--;
  }

  temp[0] = 1;

  for(int i = 1; i <= AA; i++){
    temp[i]+= temp[i-1];
    if(temp[i] >= i){
      ma = i;
    }
    // cout<<temp[i]<<" ";
  }

  cout<<ma-1<<endl;
  
  return 0;
}

C++: #include <bits/stdc++.h>
using namespace std ;

#define pb(n) push_back(n)
#define fi first
#define se second
#define all(r) begin(r),end(r)
#define vmax(ary) *max_element(all(ary))
#define vmin(ary) *min_element(all(ary))
#define debug(x) cout<<#x<<": "<<x<<endl
#define fcout(n) cout<<fixed<<setprecision((n))
#define scout(n) cout<<setw(n)
#define vary(type,name,size,init) vector< type> name(size,init)
#define vvl(v,w,h,init) vector<vector<ll>> v(w,vector<ll>(h,init))
#define mp(a,b) make_pair(a,b)

#define rep(i,n) for(int i = 0; i < (int)(n);++i)
#define REP(i,a,b) for(int i = (a);i < (int)(b);++i)
#define repi(it,array) for(auto it = array.begin(),end = array.end(); it != end;++it)
#define repa(n,array) for(auto &n :(array))

using ll = long long;
using pii = pair<int,int> ;
using pll = pair<ll,ll> ;

template<typename T>
void O(T t){
  cout << t << endl;
}

const ll mod = 1e9+7;
constexpr ll inf = ((1<<30)-1)*2+1 ;
constexpr double PI = acos(-1.0) ;
double eps = 1e-10 ;
const int dy[] = {-1,0,1,0,1,-1,1,-1};
const int dx[] = {0,-1,0,1,1,-1,-1,1};

inline bool value(int x,int y,int w,int h){
  return (x >= 0 && x < w && y >= 0 && y < h);
}

int main(){
  cin.tie(0);
  ios::sync_with_stdio(false);
  ll n,a,b;
  cin >> n;
  vector<ll> ans(1000010);
  ans[0] = 1;
  rep(i,n){
    cin >> a >> b;
    /* if(a) a--; */
    /* if(b) b--; */
    ++b;
    ans[a] ++;
    ans[b] --;
  }
  rep(i,100001){
    ans[i+1] += ans[i];
  }
  ll k = 0;
  REP(i,1,n+2){
    k = max(k,ans[i]); 
  }
  /* debug(ans[5]); */
  if(k <= 1) cout << 0 << endl;
  else
    cout << k-1 << endl;
  return 0;
}

C++:     #include <iostream>
    #include <vector>
    #include <algorithm>
    using namespace std;
     
    int main() {
    	int N;
    	vector<int> F;
    	
    	cin>>N;
    	for(int i=0;i<N+1;i++){
    		F.push_back(0);
    	}
     
    	for(int i=0;i<N;i++){
    		int x,y;
    		cin>>x>>y;
     		
     		int xn=max(0,x-1);
     		if(xn<=N){
    			F[xn]++;
     		}
    		if(y<=N){
    			F[y]--;
    		}
    	}
     
    	for(int i=0;i<N;i++){
    		F[i+1]+=F[i];
    	}
     
    	int ans;
    	for(int i=0;i<N+1;i++){
    		if(F[i]>=i){
    			ans=i;
    		}
    	}
     
    	cout<<ans<<endl;
     
    	return 0;
    }

C++: #include <bits/stdc++.h>
using namespace std;

#define int long long

static const int MAX_SIZE = 1 << 17; //segment tree のサイズ。 2^17 ≒ 1.3 * 10^5

typedef long long Int;
Int all[2 * MAX_SIZE - 1], part[2 * MAX_SIZE - 1]; // segment tree

//区間[a, b)に値xを加算する.
void add(int a, int b, int x, int k = 0, int l = 0, int r = MAX_SIZE)
{
  if (a <= l && r <= b){ //[l, r)が[a, b)に完全に内包されていれば
    all[k] += x; //[l, r)の全ての区間が持つ値としてxを足す.
  }
  else if (l < b && a < r){ //[l, r)と[a, b)が交差していれば
    part[k] += (min(b, r) - max(a, l)) * x;  //交差している分の値を, 部分的な和を持つノードに加算する.
    add(a, b, x, k * 2 + 1, l, (l + r) / 2); //子でも同じ処理を行う.
    add(a, b, x, k * 2 + 2, (l + r) / 2, r); //〃.
  }
}

Int sum(int a, int b, int k = 0, int l = 0, int r = MAX_SIZE)
{
  if (b <= l || r <= a){ //[a, b)と[l, r)が全く交差しない場合
    return (0);
  }
  else if (a <= l && r <= b){ //完全に内包されていれば
    return (all[k] * (r - l) + part[k]);
  }
  else { //[l, r)と[a, b)が交差していれば
    Int res;
    res = (min(b, r) - max(a, l)) * all[k]; 
    res += sum(a, b, k * 2 + 1, l, (l + r) / 2); //子ノードで和を求める.
    res += sum(a, b, k * 2 + 2, (l + r) / 2, r); //〃
    return (res);
  }
}

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

  for ( int i = 0; i < N; i++ ) {
    int a, b;
    cin >> a >> b;
    add(a, b+1, 1);
  }

  int ans = 0;
  for ( int i = 100000; i >= 0; i-- ) {
    if ( sum(i, i+1)+1 >= i ) {
      ans = i;
      break;
    }
  }

  cout << ans-1 << endl;
  
  return 0;
}

C++: /*
 *
 * http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=2331
 */

#include <bits/stdc++.h>

using namespace std;

#define LOG(...) fprintf(stderr, __VA_ARGS__)
//#define LOG(...)
#define FOR(i, a, b) for(int i = (int)(a); i < (int)(b); ++i)
#define REP(i, n) for(int i = 0; i < (int)(n); ++i)
#define ALL(a) (a).begin(), (a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define EXIST(s, e) ((s).find(e) != (s).end())
#define SORT(c) sort(ALL(c))
#define RSORT(c) sort(RALL(c))
#define SQ(n) (n) * (n)

typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef vector<bool> vb;
typedef vector<int> vi;
typedef vector<pii> vpi;
typedef vector<pll> vpl;
typedef vector<ll> vll;
typedef vector<vb> vvb;
typedef vector<vi> vvi;
typedef vector<vll> vvll;

int main() {
  int n;
  cin >> n;
  vi a(n), b(n);
  int maxn = 0;
  REP(i, n){
    cin >> a[i] >> b[i];
    maxn = max(b[i], maxn);
  }

  SORT(a);
  SORT(b);

  auto ita = a.begin();
  auto itb = b.begin();

  int res = 0;
  int cnt = 0;

  REP(i, maxn){
    while(*ita == i) {
      ita++;
      cnt++;
    }
    while(*itb == i - 1) {
      itb++;
      cnt--;
    }

    if (i - 1 <= cnt) {
      res = max(res, i - 1);
    }
  }
  cout << res << endl;
}