//如果树状数组的代码提交TLE了,很有可能是下标含0;
Cows
Time Limit: 3000MS | Memory Limit: 65536K | |
Total Submissions: 19145 | Accepted: 6470 |
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!
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.
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
题意:每个牛牛都有一个对青草喜欢的范围是稀饭范围,如果一个牛牛的稀饭范围包含了另一个的,但是不能重合!那么这么牛牛比另一个牛牛强壮;问每一个牛牛比多少个牛牛强壮;思路:因为两个牛牛的范围重合的话就不能比较强弱了,所以这里需要特判;把每一个牛牛的范围进行排序,s从小到大,e从大到小(举样例验证);然后s,e可以是0哦,所以这里也要处理一下;
代码:
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
using namespace std;
const int maxn=100005;
struct node
{
int id;
int s,e;
int ans;
} a[maxn];
int C[maxn];
int cmp1(node a,node b)
{
if(a.s!=b.s)
return a.s<b.s;
return a.e>b.e;
}
int cmp2(node a,node b)
{
return a.id<b.id;
}
int lowbit(int x)
{
return x&(-x);
}
int sum(int x)
{
int ret=0;
while(x>0)
{
ret+=C[x];
x-=lowbit(x);
}
return ret;
}
void add(int x,int d)
{
while(x<=maxn)
{
C[x]+=d;
x+=lowbit(x);
}
}
int main()
{
int n;
while(~scanf("%d",&n))
{
if(n==0)
break;
memset(a,0,sizeof(a));
memset(C,0,sizeof(C));
for(int i=1; i<=n; i++)
{
scanf("%d%d",&a[i].s,&a[i].e);
a[i].s+=1,a[i].e+=1;
a[i].id=i;
}
// for(int i=1; i<=n; i++)
// printf("%d %d\n",a[i].s,a[i].e);
// printf("\n");
sort(a+1,a+1+n,cmp1);
for(int i=1; i<=n; i++)
{
if(a[i].s==a[i-1].s&&a[i].e==a[i-1].e)
{
a[i].ans=a[i-1].ans;
}
else
{
a[i].ans=i-1-sum(a[i].e-1);
}
add(a[i].e,1);
}
sort(a+1,a+1+n,cmp2);
for(int i=1;i<=n;i++)
{
if(i!=n)
printf("%d ",a[i].ans);
else
printf("%d\n",a[i].ans);
}
}
return 0;
}