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 <vector>
#include <map>
#include <algorithm>
 
#define rep(i,n) for (int i = 0; i < (n); ++i)
  
int n, a, b, friends[100005], fri, maxi;

int main() {
  std::cin.tie(0);
  std::ios::sync_with_stdio(false);

  std::cin >> n;
  rep(i,n) {
    std::cin >> a >> b;
    a--; b--;
    for (int i = a; i <= b; ++i) friends[i]++;
  }
  rep(i,n+1) {
    if (fri < friends[i] && friends[i] >= i) {
        fri = friends[i];
        maxi = i;
    }
  }

  std::cout << maxi << std::endl;

  return 0;
}

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

using ll=long long;
#define int ll

#define FOR(i,a,b) for(int i=int(a);i<int(b);i++)
#define REP(i,b) FOR(i,0,b)

int read(){
	int i;
	scanf("%lld",&i);
	return i;
}

signed main(){
	int N=read();
	int a[N],b[N];
	int check[100050]={0};
	REP(i,N){
		a[i]=read();
		b[i]=read();
	}
	
	REP(i,N){
		check[a[i]] += 1;
		check[b[i]+1] -=1;
	}
	
	int sum=1;
	int ans=0;
	REP(i,N+1){
		sum += check[i];
		if(sum>=i) ans=i;
	}
	cout << ans-1 << endl;
}

C++: #include <cstdio>
#include <iostream>
#include <cmath>
#include <ctype.h>
#include <string> 
#include <sstream>
#include <iostream>
#include <algorithm>
#include <cstdlib>
#include <map>
#include <queue>
#include <utility>
#include <vector>
#include <set>
#include <list>
#include <cstring>
   
using namespace std;

int main()
{
    int n;
    cin >> n;
    int result[100003] = {};
    for(int i = 0; i < n; i++){
        int l, r;
        cin >> l >> r;
        result[l - 1]++;
        result[r]--;
    }
    int ans = 0;
    for(int i = 2; i < n + 1; i++){
        result[i] += result[i - 1];
        // cout << result[i] << endl;
        if(result[i] >= i) ans = max(i, ans);
    }
    cout << ans << endl;
    return 0;
}

C++: #include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <functional>
#include <numeric>
#include <stack>
#include <queue>
#include <map>
#include <set>
#include <utility>
#include <sstream>
#include <complex>
#include <fstream>
#include <bitset>
#include <time.h>
#include <tuple>
#include <iomanip>

using namespace std;

typedef long long ll;
typedef pair<ll, ll> P;
typedef vector<ll> V;
typedef complex<double> Point;

#define PI acos(-1.0)
#define EPS 1e-10
const ll INF = 1e17;
const ll MOD = 1e9 + 7;

#define FOR(i,a,b) for(ll i=(a);i<(b);i++)
#define rep(i,N) for(ll i=0;i<(N);i++)
#define ALL(s) (s).begin(),(s).end()
#define EQ(a,b) (abs((a)-(b))<EPS)
#define EQV(a,b) ( EQ((a).real(), (b).real()) && EQ((a).imag(), (b).imag()) )
#define fi first
#define se second
#define N_SIZE (1LL << 20)
#define NIL -1
#define MAX_N 100100

ll sq(ll num) { return num*num; }
ll mod_pow(ll x, ll n) {
	if (n == 0)return 1;
	if (n == 1)return x%MOD;
	ll res = sq(mod_pow(x, n / 2));
	res %= MOD;
	if (n % 2 == 1) {
		res *= x;
		res %= MOD;
	}
	return res;
}
ll mod_add(ll a, ll b) { return (a + b) % MOD; }
ll mod_sub(ll a, ll b) { return (a - b + MOD) % MOD; }
ll mod_mul(ll a, ll b) { return a*b % MOD; }

double dist(Point p) { return abs(p); }//ベクトルpの絶対値
double dist(Point a, Point b) {//2点間の距離
	return abs(a - b);
}

int n;
int sum[100100];

int main() {
	cin >> n;
	rep(i, n) {
		int a, b;
		cin >> a >> b;
		sum[a]++;
		sum[b + 1]--;
	}
	rep(i, 100100 - 1)sum[i + 1] += sum[i];
	ll ans = 1;
	rep(i, n) {
		ll num = i + 1;
		//cout << num << " " << sum[num] << endl;
		if (sum[num] >= num)ans = num;
	}
	cout << ans - 1 << endl;
}

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 = 100100; i >= 0; i-- ) {
    if ( sum(i, i+1)+1 >= i ) {
      ans = i;
      break;
    }
  }

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

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

int num[100005];
int num2[100005];

int main(){
    int N,a,b;
    cin>>N;
    for(int n=0;n<N;n++){
        cin>>a>>b;
        // cout<<a<<' '<<b<<endl;
        num[a]++;
        num[b+1]--;
    }
    int ans=num2[0]=num[0];
    for(int i=1;i<100005;i++){
        num2[i]=num2[i-1]+num[i];
        if(num2[i]>=i) ans=i;
    }
    ans--;
    cout<<max(0,ans)<<endl;
}

C++: #include <bits/stdc++.h>
#define For(i, a, b) for(int (i)=(int)(a); (i)<(int)(b); ++(i))
#define rFor(i, a, b) for(int (i)=(int)(a)-1; (i)>=(int)(b); --(i))
#define rep(i, n) For((i), 0, (n))
#define rrep(i, n) rFor((i), (n), 0)
#define fi first
#define se second
using namespace std;
typedef long long lint;
typedef unsigned long long ulint;
typedef pair<int, int> pii;
typedef pair<int, lint> pil;
typedef pair<lint, lint> pll;
template<class T> bool chmax(T &a, const T &b){if(a<b){a=b; return true;} return false;}
template<class T> bool chmin(T &a, const T &b){if(a>b){a=b; return true;} return false;}
template<class T> T div_floor(const T a, const T b){return a>=0 ? a/b : (a+1)/b-1;}
template<class T> T div_ceil(const T a, const T b){return a>=0 ? (a-1)/b+1 : a/b;}

constexpr lint mod = 1e9+7;
constexpr lint INF = mod*mod;
constexpr int MAX = 100010;

int main(){
    int n;
    scanf("%d", &n);
    vector<int> s(MAX, 0);
    s[0] = 1;
    s[MAX-1] = -1;
    rep(i, n){
        int a, b;
        scanf("%d%d", &a, &b);
        ++s[a];
        --s[b+1];
    }
    partial_sum(s.begin(), s.end(), s.begin());
    int ans = 0;
    rep(i, MAX)if(i <= s[i]) ans = i-1;
    printf("%d\n", ans);
}