【数据结构】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
有N头牛,每头牛有个喝水时间,这段时间它将专用一个Stall 现在给出每头牛的喝水时间段,问至少要多少个Stall才能满足它们的要求
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.
Sample Input
1 10
2 4
3 6
5 8
4 7
Sample Output
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.
===================================华丽丽的分割线============================================
只要写一个支持区间修改和全局最大值查询的东西就好辣~
那不如直接写一个线段数暖手手~~~
这题好像可以直接差分然后就完了吧。。。
时间复杂度O(nlogn),代码如下:
1 #include <bits/stdc++.h> 2 #define Maxn 1000007 3 using namespace std; 4 int read() 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 { 52 n=read(); 53 build(1,1,1000000); 54 for (int i=1;i<=n;i++) 55 { 56 int x=read(),y=read(); 57 update(1,x,y,1); 58 } 59 printf("%d\n",tree[1].mx); 60 return 0; 61 }