Cows
Time Limit: 3000MS | Memory Limit: 65536K | |
Total Submissions: 14749 | Accepted: 4879 |
Description
Farmer John's cows have discovered that the clover growing along the ridge of the hill (which we can think of as a one-dimensional number line) in his field is particularly good.
Farmer John has N cows (we number the cows from 1 to N). Each of Farmer John's N cows has a range of clover that she particularly likes (these ranges might overlap). The ranges are defined by a closed interval [S,E].
But some cows are strong and some are weak. Given two cows: cowi and cowj, their favourite clover range is [Si, Ei] and [Sj, Ej]. If Si <= Sj and Ej <= Ei and Ei - Si > Ej - Sj, we say that cowi is stronger than cowj.
For each cow, how many cows are stronger than her? Farmer John needs your help!
Farmer John has N cows (we number the cows from 1 to N). Each of Farmer John's N cows has a range of clover that she particularly likes (these ranges might overlap). The ranges are defined by a closed interval [S,E].
But some cows are strong and some are weak. Given two cows: cowi and cowj, their favourite clover range is [Si, Ei] and [Sj, Ej]. If Si <= Sj and Ej <= Ei and Ei - Si > Ej - Sj, we say that cowi is stronger than cowj.
For each cow, how many cows are stronger than her? Farmer John needs your help!
Input
The input contains multiple test cases.
For each test case, the first line is an integer N (1 <= N <= 105), which is the number of cows. Then come N lines, the i-th of which contains two integers: S and E(0 <= S < E <= 105) specifying the start end location respectively of a range preferred by some cow. Locations are given as distance from the start of the ridge.
The end of the input contains a single 0.
For each test case, the first line is an integer N (1 <= N <= 105), which is the number of cows. Then come N lines, the i-th of which contains two integers: S and E(0 <= S < E <= 105) specifying the start end location respectively of a range preferred by some cow. Locations are given as distance from the start of the ridge.
The end of the input contains a single 0.
Output
For each test case, output one line containing n space-separated integers, the i-th of which specifying the number of cows that are stronger than cowi.
Sample Input
3 1 2 0 3 3 4 0
Sample Output
1 0 0
Hint
Huge input and output,scanf and printf is recommended.
Source
POJ Contest,Author:Mathematica@ZSU
题目大意:
给你n个区间,n<=1e5,求出有多个区间是完全覆盖另外一个区间的,相同的区间不算覆盖。[s_i,e_i] e_i <=1e5
解题思路:
很基础的线段树单点更新的题目,把线段区间看做是点,然后,每次插入这个区间的时候,其实是插入的点,统计这个点后面有多少个点比它大,就是答案了。
注意,排序的时候,要按照a[i].l 排序,如果a[i].l相同的情况下,按照a[i].r的大小排序。从大到小排,这样,才能保证我们每次排完的区间都尽可能在另外一个区间的内部。
然后,我们查询的时候,只要统计query(1,a[i].r,max)的和就行了,每次插入一个新的区间,我们就update(1,a[i].r,1)就行了。
代码:
# include<cstdio> # include<iostream> # include<cstring> # include<algorithm> using namespace std; # define MAX 100004 # define lid id<<1 # define rid id<<1|1 struct node { int l,r; int id; }cow[MAX]; int ans[MAX]; struct Segtree { int l,r; int sum; }tree[MAX*4]; void push_up( int id ) { tree[id].sum = tree[lid].sum+tree[rid].sum; } int cmp ( const struct node&a, const struct node &b ) { if ( a.l==b.l ) return a.r >= b.r; else return a.l < b.l; } void build ( int id ,int l,int r ) { tree[id].l = l; tree[id].r = r; tree[id].sum = 0; if (l==r) return; int mid = ( tree[id].l+tree[id].r )/2; build(lid,l,mid); build(rid,mid+1,r); push_up(id); } void update( int id ,int x,int val ) { if ( tree[id].l==tree[id].r ) { tree[id].sum += val; return; } int mid = ( tree[id].l+tree[id].r )/2; if ( x <= mid ) update(lid,x,val); else update(rid,x,val); push_up(id); } int query( int id ,int l,int r ) { if ( tree[id].l==l&&tree[id].r==r ) { return tree[id].sum; } int mid = ( tree[id].l+tree[id].r )/2; if ( r <= mid ) return query(lid,l,r); else if ( l > mid ) return query(rid,l,r); else return query(lid,l,mid)+query(rid,mid+1,r); } int main(void) { int n; while ( scanf("%d",&n)!=EOF ) { if ( n==0 ) break; for ( int i = 0;i < n;i++ ) { int l,r;scanf("%d%d",&l,&r); cow[i].l = l; cow[i].r = r; cow[i].id = i; } sort(cow,cow+n,cmp); build(1,0,MAX); for ( int i = 0;i < n;i++ ) { if ( i>=1&&cow[i].l==cow[i-1].l&&cow[i].r==cow[i-1].r ) ans[cow[i].id] = ans[cow[i-1].id]; else ans[cow[i].id] = query(1,cow[i].r,MAX); update(1,cow[i].r,1); } for ( int i = 0;i < n;i++ ) { if (!i) printf("%d",ans[i]); else printf(" %d",ans[i]); } puts(""); memset(ans,0,sizeof(ans)); } return 0; }