Problem:
MathJax.Hub.Config({ tex2jax: { inlineMath: [["$","$"], ["\\(","\\)"]], processEscapes: true }});

The Maximum Number of Customers

$N$ persons visited a restaurant. The restaurant is open from 0 to $T$. The $i$-th person entered the restaurant at $l_i$ and left at $r_i$. Find the maximum number of persons during the business hours.

Constraints

$ 1 \leq N \leq 10^5 $

$ 1 \leq T \leq 10^5 $

$ 0 \leq l_i < r_i \leq T $

Input

The input is given in the following format.

$N$ $T$

$l_1$ $r_1$

$l_2$ $r_2$

:

$l_N$ $r_N$

Output

Print the maximum number of persons in a line.

Sample Input 1

6 10
0 2
1 3
2 6
3 8
4 10
5 10

Sample Output 1

4

Sample Input 2

2 2
0 1
1 2

Sample Output 2

1

Solution in JAVA (judge verdict: Accepted)

import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.*;
import java.math.BigInteger;
 
public class Main implements Runnable {
	
	static int mod = 1000000007;
	
    public static void main(String[] args) {
    	new Thread(null, new Main(), "", 1024 * 1024 * 1024).start();
    }
    
    public void run() {
    	PrintWriter out = new PrintWriter(System.out);
        FastScanner sc = new FastScanner();

        int n = sc.nextInt();
        int t = sc.nextInt();
        Imos im = new Imos(0,t);
        
        for(int i=0;i<n;i++){
        	im.addSquare(0, sc.nextInt(), 0, sc.nextInt()-1, 1);
        }

        out.println(im.max());
        
        out.flush();
    }

    
}

class Imos {
	int[][] mat;
	int h;
	int w;
	
	//0<=x<=h, 0<=y<=w
	public Imos(int h, int w){
		mat = new int[h+1][w+1];
		this.h = h+1;
		this.w = w+1;
	}
	
	//左下のマスが(x1,y1)、右下のマスが(x2,y2)の正方形領域に重みwを追加
	void addSquare(int x1, int y1, int x2, int y2, int weight){
		mat[x1][y1] += weight;
		if(y2+1<w){
			mat[x1][y2+1] -= weight;
		}
		if(x2+1<h){
			mat[x2+1][y1] -= weight;
			if(y2+1<w){
				mat[x2+1][y2+1] += weight;
			}
		}
	}
	
	//最も重みの大きいマスの重み
	int max(){
		int[][] cm = copy(mat);
		cumulativeSum(cm);
		return max(cm);
	}
	
	//行列のディープコピー
	static int[][] copy(int[][] x){
		int[][] y = new int[x.length][x[0].length];
		for(int i=0;i<x.length;i++){
			y[i] = x[i].clone();
		}
		return y;
	}
	
	//左上からの累積和にする
	static void cumulativeSum(int[][] x){
		for(int i=0;i<x.length;i++){
			for(int j=1;j<x[0].length;j++){
				x[i][j] += x[i][j-1];
			}
		}
		
		for(int j=0;j<x[0].length;j++){
			for(int i=1;i<x.length;i++){
				x[i][j] += x[i-1][j];
			}
		}
	}

	//最大値
	static int max(int[][] x){
		int max = Integer.MIN_VALUE;
		for(int i=0;i<x.length;i++){
			for(int j=0;j<x[0].length;j++){
				if(x[i][j] > max){
					max = x[i][j];
				}
			}
		}
		return max;
	}
	
}

class FastScanner {
	private final InputStream in = System.in;
	private final byte[] buffer = new byte[1024];
	private int ptr = 0;
	private int buflen = 0;
	private boolean hasNextByte() {
		if (ptr < buflen) {
			return true;
		} else {
			ptr = 0;
			try {
				buflen = in.read(buffer);
			} catch (IOException e) {
				e.printStackTrace();
			}
			if (buflen <= 0) {
				return false;
			}
		}
		return true;
	}
	private int readByte() {
		if (hasNextByte())
			return buffer[ptr++];
		else
			return -1;
	}
	private static boolean isPrintableChar(int c) {
		return 33 <= c && c <= 126;
	}
	public boolean hasNext() {
		while (hasNextByte() && !isPrintableChar(buffer[ptr]))
			ptr++;
		return hasNextByte();
	}
	public String next() {
		if (!hasNext())
			throw new NoSuchElementException();
		StringBuilder sb = new StringBuilder();
		int b = readByte();
		while (isPrintableChar(b)) {
			sb.appendCodePoint(b);
			b = readByte();
		}
		return sb.toString();
	}
	public long nextLong() {
		if (!hasNext())
			throw new NoSuchElementException();
		long n = 0;
		boolean minus = false;
		int b = readByte();
		if (b == '-') {
			minus = true;
			b = readByte();
		}
		if (b < '0' || '9' < b) {
			throw new NumberFormatException();
		}
		while (true) {
			if ('0' <= b && b <= '9') {
				n *= 10;
				n += b - '0';
			} else if (b == -1 || !isPrintableChar(b)) {
				return minus ? -n : n;
			} else {
				throw new NumberFormatException();
			}
			b = readByte();
		}
	}
	public int nextInt() {
		long nl = nextLong();
		if (nl < Integer.MIN_VALUE || nl > Integer.MAX_VALUE)
			throw new NumberFormatException();
		return (int) nl;
	}
	public int[] nextintArray(int n){
		int[] a = new int[n];
		for(int i=0;i<n;i++){
			a[i] = nextInt();
		}
		return a;
	}
	public long[] nextlongArray(int n){
		long[] a = new long[n];
		for(int i=0;i<n;i++){
			a[i] = nextLong();
		}
		return a;
	}
	public Integer[] nextIntegerArray(int n){
		Integer[] a = new Integer[n];
		for(int i=0;i<n;i++){
			a[i] = nextInt();
		}
		return a;
	}
	public double nextDouble() {
		return Double.parseDouble(next());
	}
}