# BZOJ4444 SCOI2015国旗计划 根据性质优化贪心

### 题目大意

M<=109$M <= 10^9$
N<=105$N <= 10^5$

### 程序

//YxuanwKeith
#include <cstring>
#include <cstdio>
#include <algorithm>

using namespace std;

const int MAXN = 2e5 + 5;

struct Node {
int Num, Bel, Ord;
Node(int num, int bel, int ord) {Num = num, Bel = bel, Ord = ord;}
Node() {}
};

Node Sort[MAXN * 2];
int N, M, Num, Least, top, L[MAXN], R[MAXN], F[MAXN * 4], Ans[MAXN * 4];
int tot, Last[MAXN * 4], Go[MAXN * 4], Next[MAXN * 4], D[MAXN * 4];

void Max(int &Now, int Comp) {
if (Comp > Now) Now = Comp;
}

bool Cmp(Node A, Node B) {
return A.Num < B.Num;
}

void Link(int u, int v) {
Next[++ tot] = Last[u], Last[u] = tot, Go[tot] = v;
}

void Prepare() {
sort(Sort + 1, Sort + 1 + N * 2, Cmp);
for (int i = 1; i <= N * 2; i ++) {
if (Sort[i].Num != Sort[i - 1].Num) ++ Num;
if (Sort[i].Ord == 0) L[Sort[i].Bel] = Num; else
R[Sort[i].Bel] = Num;
}

for (int i = 1; i <= N; i ++) {
if (L[i] < R[i]) {
Max(F[L[i]], R[i]);
Max(F[L[i] + Num], R[i] + Num);
} else {
Max(F[1], R[i]);
Max(F[L[i]], R[i] + Num);
Max(F[L[i] + Num], Num + Num);
}
}
for (int i = 1; i <= Num + Num; i ++) Max(F[i], F[i - 1]);

for (int i = 1; i <= Num; i = F[i]) Least ++;
Least -= 1;

for (int i = 1; i < 2 * Num; i ++) Link(F[i], i);
}

void Dfs(int Now) {
D[++ top] = Now;
if (Now <= Num)
for (int i = Least; ; i ++)
if (D[top - i] >= Now + Num) {
Ans[Now] = i;
break;
}

for (int p = Last[Now]; p; p = Next[p])
Dfs(Go[p]);
top --;
}

void Solve() {
Dfs(Num + Num);
for (int i = 1; i <= N; i ++) printf("%d ", Ans[L[i]]);
}

int main() {
scanf("%d%d", &N, &M);
int Cnt = 0;
for (int i = 1; i <= N; i ++) {
scanf("%d%d", &L[i], &R[i]);
Sort[++ Cnt] = Node(L[i], i, 0);
Sort[++ Cnt] = Node(R[i], i, 1);
}

Prepare();
Solve();
}