POJ 2481 Cows 簡單樹狀數組區間覆蓋
點擊打開鏈接
Cows
Time Limit: 3000MS
Memory Limit: 65536K
Total Submissions: 13334
Accepted: 4413
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
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頭牛,每頭牛都有一個范圍[s,e],讓比它強壯的牛的個數。第i頭牛比第j頭牛強壯的定義:si<=sj&&ei>=ej&&ei-si>ej-si。
將n頭牛按照e從大到小排序,跟據s的值,用樹狀數組求得答案。
//2156K 1079MS
#include
#include
#include
#define M 100007
using namespace std;
int c[M],ans[M],max_e;
struct Cow
{
int s,e,id;
}p[M];
int cmp(Cow a,Cow b)
{
if(a.e==b.e)return a.sb.e;
}
int lowbit(int x)
{
return x&(-x);
}
void add(int pos,int val)
{
while(pos<=max_e+1)
{
c[pos]+=val;
pos+=lowbit(pos);
}
}
int getsum(int pos)
{
int res=0;
while(pos>0)
{
res+=c[pos];
pos-=lowbit(pos);
}
return res;
}
int main()
{
int n;
while(scanf("%d",&n),n)
{
max_e=0;
for(int i=0;i