Cows(树状数组)

Posted qq-1585047819

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Cows(树状数组)相关的知识,希望对你有一定的参考价值。

Cows
Time Limit: 3000MS   Memory Limit: 65536K
Total Submissions: 24787   Accepted: 8296

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

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
 
解题思路 : 先按照E从大到小排序(满足一个条件) 如果相等再按照S从小到大排序 特别注意相等的情况 题目中这句话Ei - Si > Ej - Sj(代表完全相等的情况下不算比他强的!)
技术图片
 1 #include <iostream>
 2 #include <algorithm>
 3 #include <cstring>
 4 #include <cstdio>
 5 using namespace std;
 6 int n,x,y;
 7 const int maxn=1e5+5;
 8 int cnt[maxn],res[maxn];
 9 struct Node int x,y,id; A[maxn];
10 bool cmp(Node a,Node b) if(a.y!=b.y) return a.y>b.y; return a.x<b.x; 
11 int lowbits(int x)  return x&-x; 
12 void add(int x) while(x<=maxn) cnt[x]++,x+=lowbits(x); 
13 int query(int x) int sum=0;while(x>0) sum+=cnt[x],x-=lowbits(x); return sum; 
14 
15 int main()
16     while(scanf("%d",&n),n)
17         memset(cnt,0,sizeof(cnt)),memset(res,0,sizeof(res));
18         for(int i=1;i<=n;i++) scanf("%d%d",&A[i].x,&A[i].y),A[i].id=i;
19         sort(A+1,A+1+n,cmp);
20         res[A[1].id]=0;  add(A[1].x+1);
21         for(int i=2;i<=n;i++)
22             if(A[i].x==A[i-1].x&&A[i].y==A[i-1].y) res[A[i].id]=res[A[i-1].id];
23             else res[A[i].id]=query(A[i].x+1);
24             add(A[i].x+1);
25         
26         for(int i=1;i<=n;i++)
27             if(i!=1) printf(" ");
28             printf("%d",res[i]);
29         
30         printf("\n");
31     
32     return 0;
33 
View Code

 

以上是关于Cows(树状数组)的主要内容,如果未能解决你的问题,请参考以下文章

Cows(树状数组)

POJ2182 Lost Cows (树状数组+二分)

POJ 2481 Cows树状数组

poj2182Lost Cows——树状数组快速查找

POJ 2481 Cows(树状数组)

POJ2182 HDU2711 Lost Cows树状数组+线段树