【数据结构】bzoj1651专用牛棚

Description

Oh those picky N (1 <= N <= 50,000) cows! They are so picky that each one will only be milked over some precise time interval A..B (1 <= A <= B <= 1,000,000), which includes both times A and B. Obviously, FJ must create a reservation system to determine which stall each cow can be assigned for her milking time. Of course, no cow will share such a private moment with other cows. Help FJ by determining: * The minimum number of stalls required in the barn so that each cow can have her private milking period * An assignment of cows to these stalls over time

Input

* Line 1: A single integer, N

* Lines 2..N+1: Line i+1 describes cow i's milking interval with two space-separated integers.

Output

* Line 1: The minimum number of stalls the barn must have.

* Lines 2..N+1: Line i+1 describes the stall to which cow i will be assigned for her milking period.

5
1 10
2 4
3 6
5 8
4 7

Sample Output

4

OUTPUT DETAILS:

Here's a graphical schedule for this output:

Time 1 2 3 4 5 6 7 8 9 10
Stall 1 c1>>>>>>>>>>>>>>>>>>>>>>>>>>>
Stall 2 .. c2>>>>>> c4>>>>>>>>> .. ..
Stall 3 .. .. c3>>>>>>>>> .. .. .. ..
Stall 4 .. .. .. c5>>>>>>>>> .. .. ..

Other outputs using the same number of stalls are possible.

===================================华丽丽的分割线============================================

 1 #include <bits/stdc++.h>
2 #define Maxn 1000007
3 using namespace std;
5 {
6     int x=0,f=1;char ch=getchar();
7     while (ch<'0'||ch>'9'){if (ch=='-') f=-1;ch=getchar();}
8     while (ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
9     return x*f;
10 }
11 struct seg
12 {
13     int lx,rx,mx,tag;
14 };
15 seg tree[Maxn*4];
16 int n;
17 void build(int node, int l, int r)
18 {
19     tree[node].lx=l,tree[node].rx=r;
20     tree[node].tag=0,tree[node].mx=0;
21     if (l==r) return;
22     int mid=(l+r)/2;
23     build(node*2,l,mid);
24     build(node*2+1,mid+1,r);
25 }
26 void pushdown(int node)
27 {
28     if (tree[node].tag==0) return;
29     tree[node*2].tag+=tree[node].tag;
30     tree[node*2].mx+=tree[node].tag;
31     tree[node*2+1].tag+=tree[node].tag;
32     tree[node*2+1].mx+=tree[node].tag;
33     tree[node].tag=0;
34 }
35 void update(int node, int l, int r, int del)
36 {
37     if (tree[node].rx<l) return;
38     if (tree[node].lx>r) return;
39     if (tree[node].lx>=l&&tree[node].rx<=r)
40     {
41         tree[node].tag+=del;
42         tree[node].mx+=del;
43         return;
44     }
45     pushdown(node);
46     update(node*2,l,r,del);
47     update(node*2+1,l,r,del);
48     tree[node].mx=max(tree[node*2].mx,tree[node*2+1].mx);
49 }
50 int main()
51 {
61 }