poj2481Cows樹狀數組

來源:互聯網
上載者:User

Cows

Time Limit: 3000MS   Memory Limit: 65536K
Total Submissions: 15446   Accepted: 5151

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: cow i and cow j, their favourite clover range is [Si, Ei] and [Sj, Ej]. If Si <= Sj and Ej <= Ei and Ei - Si > Ej - Sj, we say that cow i is stronger than cow j. 

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 <= 10 5), which is the number of cows. Then come N lines, the i-th of which contains two integers: S and E(0 <= S < E <= 10 5) 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 cow i. 

Sample Input

31 20 33 40

Sample Output

1 0 0

Hint Huge input and output,scanf and printf is recommended.

#include<cstdio>#include<cstdlib>#include<cstring>#include<algorithm>#include<cmath>#include<list>#include<queue>#include<vector>using namespace std;const int maxn=100010;struct Node{int s,e,id;}A[maxn];int c[maxn];int low(int x){return x&(-x);}bool cmp(Node a,Node b){//按照e降序排列相等按照s的升序排列 if(a.e==b.e)return a.s<b.s;return a.e>b.e;}int sum(int n){int value=0;while(n>0){value+=c[n];n-=low(n);}return value;}void add(int pos){while(pos<maxn){c[pos]+=1;pos+=low(pos);}}int ans[maxn];int main(){int n,i,j,k;while(scanf("%d",&n),n){for(i=0;i<n;++i){scanf("%d%d",&A[i].s,&A[i].e);A[i].s+=1;A[i].e+=1;A[i].id=i;}sort(A,A+n,cmp);memset(c,0,sizeof(c));for(i=0;i<n;++i){if(i>0&&A[i].s==A[i-1].s&&A[i].e==A[i-1].e){ans[A[i].id]=ans[A[i-1].id];}else {ans[A[i].id]=sum(A[i].s);}add(A[i].s);}for(i=0;i<n;++i){printf(i==n-1?"%d\n":"%d ",ans[i]);}}return 0;}


聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.