poj 2481 Cows[求逆序数]

COWs

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!

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.

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

31 20 33 40

Sample Output

1 0 0

Analyse:

s从小到大排,,e从大到小。然后依次modify,求>=c[i].e的sum;

注意几个坑点:

3

1 3

1 3

1 3

1 2

0 0 0 1

3

1 2

1 2

1 2

0 0 0

注意!!!!坑死!

CODE:

#include<iostream>#include<cstdio>#include<cstring>#include<cmath>#include<algorithm>#include<vector>#include<string>#include<queue>#include<deque>#include<stack>#include<map>#include<set>#define INF 0x7fffffff#define SUP 0x80000000#define mem(a,b) memset(a,b,sizeof(a))using namespace std;typedef long long LL;const int N=100007;struct Tree{int le,ri;int sum;}t[N<<2];struct Cow{int s,e,id;bool operator <(Cow tt) const{if(s==tt.s) return e>tt.e;return s<tt.s;}}c[N];int ans[N];void build(int ro,int le,int ri){t[ro].le=le;t[ro].ri=ri;t[ro].sum=0;if(le==ri) return;int mid=le+(ri-le)/2;build(ro<<1,le,mid);build(ro<<1|1,mid+1,ri);}void modify(int ro,int le,int ri,int x){if(le==ri){t[ro].sum+=1;return;}int mid=le+(ri-le)/2;if(x<=mid) modify(ro<<1,le,mid,x);else modify(ro<<1|1,mid+1,ri,x);t[ro].sum=t[ro<<1].sum+t[ro<<1|1].sum;}int query(int ro,int le,int ri,int L,int R){if(L<=le&&ri<=R){return t[ro].sum;}int mid=le+(ri-le)/2;int ret=0;if(L<=mid) ret+=query(ro<<1,le,mid,L,R);if(R>mid) ret+=query(ro<<1|1,mid+1,ri,L,R);return ret;}int main(){int n;while(scanf("%d",&n)==1,n){int maxx=0;for(int i=0;i<n;i++){scanf("%d%d",&c[i].s,&c[i].e);c[i].id=i;maxx=max(c[i].e,maxx);}sort(c,c+n);build(1,0,maxx+10);modify(1,0,maxx+10,c[0].e);ans[c[0].id]=0;for(int i=1;i<n;i++){if(c[i].s==c[i-1].s&&c[i].e==c[i-1].e){ans[c[i].id]=ans[c[i-1].id];modify(1,0,maxx+10,c[i].e);}else{ans[c[i].id]=query(1,0,maxx+10,c[i].e,maxx+10);modify(1,0,maxx+10,c[i].e);}}for(int i=0;i<n;i++)printf("%d%c",ans[i],i<n-1?' ':'\n');}return 0;}

我想有一天和你去旅行。去那没有去过的地方,

poj 2481 Cows[求逆序数]

相关文章:

你感兴趣的文章:

标签云: