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

Problem C
Distribution Center

The factory of the Impractically Complicated Products Corporation has many manufacturing lines and the same number of corresponding storage rooms. The same number of conveyor lanes are laid out in parallel to transfer goods from manufacturing lines directly to the corresponding storage rooms. Now, they plan to install a number of robot arms here and there between pairs of adjacent conveyor lanes so that goods in one of the lanes can be picked up and released down on the other, and also in the opposite way. This should allow mixing up goods from different manufacturing lines to the storage rooms.

Depending on the positions of robot arms, the goods from each of the manufacturing lines can only be delivered to some of the storage rooms. Your task is to find the number of manufacturing lines from which goods can be transferred to each of the storage rooms, given the number of conveyor lanes and positions of robot arms.

Input

The input consists of a single test case, formatted as follows.

$n$ $m$

$x_1$ $y_1$

...

$x_m$ $y_m$

An integer $n$ ($2 \leq n \leq 200000$) in the first line is the number of conveyor lanes. The lanes are numbered from 1 to $n$, and two lanes with their numbers differing with 1 are adjacent. All of them start from the position $x = 0$ and end at $x = 100000$. The other integer $m$ ($1 \leq m < 100000$) is the number of robot arms.

The following $m$ lines indicate the positions of the robot arms by two integers $x_i$ ($0 < x_i < 100000$) and $y_i$ ($1 \leq y_i < n$). Here, $x_i$ is the x-coordinate of the $i$-th robot arm, which can pick goods on either the lane $y_i$ or the lane $y_i + 1$ at position $x = x_i$, and then release them on the other at the same x-coordinate.

You can assume that positions of no two robot arms have the same $x$-coordinate, that is, $x_i \ne x_j$ for any $i \ne j$.

Figure C.1. Illustration of Sample Input 1

Output

Output $n$ integers separated by a space in one line. The $i$-th integer is the number of the manufacturing lines from which the storage room connected to the conveyor lane $i$ can accept goods.

Sample Input 1

4 3
1000 1
2000 2
3000 3

Sample Output 1

2 3 4 4

Sample Input 2

4 3
1 1
3 2
2 3

Sample Output 2

2 4 4 2

Solution in JAVA (judge verdict: Memory Limit Exceeded)

import java.io.BufferedReader;
import java.io.Closeable;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.BitSet;
import java.util.HashSet;
import java.util.StringTokenizer;

public class Main {
	
	public static class Pair implements Comparable<Pair> {
		int x, y;
		
		public Pair(int x, int y){
			this.x = x;
			this.y = y;
		}
		
		@Override public int compareTo(Pair arg0){
			return Integer.compare(this.x, arg0.x);
		}
	}

	public static void main(String[] args) throws IOException {
		final Scanner sc = new Scanner(System.in);
		
		final int n = sc.nextInt();
		final int m = sc.nextInt();
		
		ArrayList<BitSet> upper_most = new ArrayList<BitSet>();
		ArrayList<BitSet> lower_most = new ArrayList<BitSet>();
		for(int i = 0; i < n; i++){
			upper_most.add(new BitSet());
			lower_most.add(new BitSet());
		}
		for(int i = 0; i < n; i++){
			upper_most.get(i).set(i);
			lower_most.get(i).set(i);
		}
		
		int[] output = new int[n];
		Arrays.fill(output, 1);
		
		Pair[] pair = new Pair[m];
		for(int i = 0; i < m; i++){
			final int x = sc.nextInt();
			final int y = sc.nextInt() - 1;
			
			pair[i] = new Pair(x, y);
		}
		Arrays.sort(pair);
		
		for(int i = m - 1; i >= 0; i--){
			//final int x = pair[i].x;
			final int y = pair[i].y;
			
			lower_most.get(y + 1).or(lower_most.get(y));
			lower_most.get(y).clear();
			
			upper_most.get(y).or(upper_most.get(y + 1));
			upper_most.get(y + 1).clear();
		}
		
		//System.out.println(upper_most + " " + lower_most);
		int[] upper = new int[n];
		int[] lower = new int[n];
		for(int i = 0; i < n; i++){
			for(int x = upper_most.get(i).nextSetBit(0); x >= 0; x = upper_most.get(i).nextSetBit(x + 1)){
				upper[x] = i;
			}
			
			for(int x = lower_most.get(i).nextSetBit(0); x >= 0; x = lower_most.get(i).nextSetBit(x + 1)){
				lower[x] = i;
			}
		}
		
		
		PrintWriter pw = new PrintWriter(System.out);
		for(int i = 0; i < n; i++){
			//pw.println(upper[i] + ", " + lower[i]);
			//*
			pw.print(i == 0 ? "" : " ");
			pw.print(lower[i] - upper[i] + 1);
			//*/
		}
		pw.println();
		pw.flush();
		pw.close();
	}

	public static class Scanner implements Closeable {
		private BufferedReader br;
		private StringTokenizer tok;

		public Scanner(InputStream is) throws IOException {
			br = new BufferedReader(new InputStreamReader(is));
		}

		private void getLine() throws IOException {
			while (!hasNext()) {
				tok = new StringTokenizer(br.readLine());
			}
		}

		private boolean hasNext() {
			return tok != null && tok.hasMoreTokens();
		}

		public String next() throws IOException {
			getLine();
			return tok.nextToken();
		}

		public int nextInt() throws IOException {
			return Integer.parseInt(next());
		}

		public long nextLong() throws IOException {
			return Long.parseLong(next());
		}

		public double nextDouble() throws IOException {
			return Double.parseDouble(next());
		}

		public void close() throws IOException {
			br.close();
		}
	}
}