POJ 2481 Cows(树状数组)
2016-10-25 19:42
218 查看
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
Sample Output
Hint
Huge input and output,scanf and printf is recommended.
这题需要把y按照从大到小排序然后x用树状数组记录,因为y小而x大的点一定可以被包含,,只要想到这个层面就可构造出树状数组的结构了
题意:给定n个区间,问每个区间被多少个区间包含(自己不能包含自己)
思路:先按照右端点从大到小排序,然后左端点从小到大排序。然后查询每个左端点前面有几个数,就是可以包含当前区间的区间个数。注意特判两个区间相等时的情况
#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
using namespace std;
const int N = 100010;
struct node
{
int x, y, id;
}p
;
int a
, res
;
int cmp(node A,node B)
{
if(A.y!=B.y)
{
return A.y>=B.y;
}
return A.x<=B.x;
}
int lowbit(int k)
{
return k&-k;
}
int sum(int x)
{
int ans=0;
while(x)
{
ans+=a[x];
x-=lowbit(x);
}
return ans;
}
void add(int x)
{
while(x<N)
{
a[x]+=1;
x+=lowbit(x);
}
return ;
}
int main()
{
int n;
while(scanf("%d",&n),n!=0)
{
memset(p,0,sizeof(p));
for(int i=1;i<=n;i++)
{
scanf("%d %d", &p[i].x, &p[i].y);
p[i].x++, p[i].y++;
p[i].id=i;
}
memset(a,0,sizeof(a));
sort(p+1,p+n+1,cmp);
for(int i=1;i<=n;i++)
{
if(p[i].x==p[i-1].x&&p[i].y==p[i-1].y)
{
res[p[i].id]=res[p[i-1].id];
}
else
{
res[p[i].id]=sum(p[i].x);
}
add(p[i].x);
}
for(int i=1;i<=n;i++)
{
printf("%d%c",res[i],i==n?'\n':' ');
}
}
return 0;
}
Time Limit: 3000MS | Memory Limit: 65536K | |
Total Submissions: 16986 | Accepted: 5687 |
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.
这题需要把y按照从大到小排序然后x用树状数组记录,因为y小而x大的点一定可以被包含,,只要想到这个层面就可构造出树状数组的结构了
题意:给定n个区间,问每个区间被多少个区间包含(自己不能包含自己)
思路:先按照右端点从大到小排序,然后左端点从小到大排序。然后查询每个左端点前面有几个数,就是可以包含当前区间的区间个数。注意特判两个区间相等时的情况
#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
using namespace std;
const int N = 100010;
struct node
{
int x, y, id;
}p
;
int a
, res
;
int cmp(node A,node B)
{
if(A.y!=B.y)
{
return A.y>=B.y;
}
return A.x<=B.x;
}
int lowbit(int k)
{
return k&-k;
}
int sum(int x)
{
int ans=0;
while(x)
{
ans+=a[x];
x-=lowbit(x);
}
return ans;
}
void add(int x)
{
while(x<N)
{
a[x]+=1;
x+=lowbit(x);
}
return ;
}
int main()
{
int n;
while(scanf("%d",&n),n!=0)
{
memset(p,0,sizeof(p));
for(int i=1;i<=n;i++)
{
scanf("%d %d", &p[i].x, &p[i].y);
p[i].x++, p[i].y++;
p[i].id=i;
}
memset(a,0,sizeof(a));
sort(p+1,p+n+1,cmp);
for(int i=1;i<=n;i++)
{
if(p[i].x==p[i-1].x&&p[i].y==p[i-1].y)
{
res[p[i].id]=res[p[i-1].id];
}
else
{
res[p[i].id]=sum(p[i].x);
}
add(p[i].x);
}
for(int i=1;i<=n;i++)
{
printf("%d%c",res[i],i==n?'\n':' ');
}
}
return 0;
}
相关文章推荐
- POJ 2481 Cows (树状数组)
- POJ 2481 Cows (树状数组)
- poj 2481 Cows (树状数组)
- POJ - 2481 Cows(树状数组)
- POJ 2481 Cows(树状数组)
- poj 2481 Cows (树状数组 +1 p2352 妙用加深~)
- POJ 2481 Cows && POJ 2352 Stars(树状数组妙用)
- POJ 2481 Cows 【树状数组】
- POJ 2481 Cows(树状数组)
- poj 2481 Cows(树状数组 | 线段树)
- poj 2481 cows(树状数组)
- POJ 2481 Cows【树状数组】
- poj 2481 Cows 【树状数组】
- poj 2481 Cows(树状数组)题目有陷阱,转换后与stars类似
- POJ 题目2481 Cows(树状数组)
- POJ 2481 Cows【树状数组】
- poj 2481 Cows(树状数组)
- POJ 2481——Cows(树状数组)
- POJ 2481 Cows (树状数组)
- POJ 2481 Cows(树状数组)