两道相似的题,都可以转化为求一个点的某个方位(左上,左下,右上,右下)有多少个点,求和
Astronomers often examine star maps where stars are represented by points on a plane and each star has Cartesian coordinates. Let the level of a star be an amount of the stars that are not higher and not to the right of the given star. Astronomers want to know the distribution of the levels of the stars.
For example, look at the map shown on the figure above. Level of the star number 5 is equal to 3 (it's formed by three stars with a numbers 1, 2 and 4). And the levels of the stars numbered by 2 and 4 are 1. At this map there are only one star of the level 0, two stars of the level 1, one star of the level 2, and one star of the level 3.
You are to write a program that will count the amounts of the stars of each level on a given map.
Input
The first line of the input file contains a number of stars N (1<=N<=15000). The following N lines describe coordinates of stars (two integers X and Y per line separated by a space, 0<=X,Y<=32000). There can be only one star at one point of the plane. Stars are listed in ascending order of Y coordinate. Stars with equal Y coordinates are listed in ascending order of X coordinate.
Output
The output should contain N lines, one number per line. The first line contains amount of stars of the level 0, the second does amount of stars of the level 1 and so on, the last line contains amount of stars of the level N-1.
Sample Input
5 1 1 5 1 7 1 3 3 5 5
Sample Output
1 2 1 1 0
Hint
This problem has huge input data,use scanf() instead of cin to read data to avoid time limit exceed.
题意:这道题让求每个星左下角有多少颗星,每个位置最多只能有一个星。输入有顺序的,根据y由小到大,若y相同x由小到大的顺序输入。最后根据左下角的星的个数划分级别,若左下角没有星,那么当前的星就是0级星,若有一颗星,则为1级星...输出时按照0级星,1级星,2级星...的个数输出。
思路:构造树状数组,由于y是升序的,所以只用求x之前的即可(这有点绕,可以画图再结合代码理解)
#include<iostream>
#include<cstdio>
#include<string.h>
#define maxn 32005
using namespace std;
int c[maxn],a[maxn];
int x,y,n;
void init() {
memset(a,0,sizeof(a));
memset(c,0,sizeof(c));
}
int lowbit(int x) {
return x&(-x);
}
void add(int pos) {
for(int i=pos; i<=maxn; i+=lowbit(i))
c[i]++;
}
int sum(int pos) {
int ans = 0;
for(int i=pos; i>0; i-=lowbit(i))
ans+=c[i];
return ans;
}
int main() {
while(~scanf("%d",&n)) {
init();
for(int i=1; i<=n; i++) {
scanf("%d%d",&x,&y);
a[sum(x+1)]++;
add(x+1); //每输入一次都要相应更新一次
}
for(int i=0; i<n; i++) //从0级星开始输出
printf("%d\n",a[i]);
}
return 0;
}
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.
题意:一头牛的范围包含了另一头牛(注意是真子集,不能相等),则说明这头牛比另一头牛强,让求每一头牛中有多少头牛比他强
思路:这题十分巧妙,一头牛比另一头牛强,那么意味着它的S值要比另一头小,E值要比另一头大,那么求一头牛比它强的牛的个数可以转化成一个二维坐标,S看作x,E看作y,求有多少个点在当前点的左上角(还是要画图)。这道题输入数据是随机的,所以需要进行排序,按照y从大到小,若y相同按照x从小到大的顺序进行排序。最后只用统计有几个x比当前x小就行。注意需要用到离散化 离散化
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int N=100010;
int n,level[N],c[N];
struct node{
int s,e;
int id;
}a[N];
void init() {
memset(level,0,sizeof(level));
memset(c,0,sizeof(c));
}
//按照y从大到小,若y相同按照x从小到大的顺序
int cmp(node a,node b){
if(a.e==b.e)
return a.s<b.s;
return a.e>b.e;
}
int lowbit(int x){
return x&(-x);
}
void update(int i){
while(i<=n){
c[i]++;
i+=lowbit(i);
}
}
int Sum(int i){
int ans=0;
while(i>0){
ans+=c[i];
i-=lowbit(i);
}
return ans;
}
int main() {
while(~scanf("%d",&n)&&n){
init();
for(int i=1;i<=n;i++){
scanf("%d%d",&a[i].s,&a[i].e);
a[i].s++; //因为树状数组从1开始
a[i].e++;
a[i].id=i; //离散化
}
sort(a+1,a+n+1,cmp);
//先计算1
level[a[1].id]=Sum(a[1].s);
update(a[1].s);
//往后计算
for(int i=2;i<=n;i++){
if(a[i].s==a[i-1].s && a[i].e==a[i-1].e)//若两头牛强壮等级相同
level[a[i].id]=level[a[i-1].id];
else
level[a[i].id]=Sum(a[i].s);
update(a[i].s);//每统计一次更新一次
}
for(int i=1;i<n;i++)
printf("%d ",level[i]);
printf("%d\n",level[n]);
}
return 0;
}