题目链接:http://poj.org/problem?id=2352
Description
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.
![](https://i-blog.csdnimg.cn/blog_migrate/fd47938575a43734c7155490c64a749e.jpeg)
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.
题目大意:给出n个坐标,先按y顺序升序排,y相等时在按x升序排列,对于每个坐标到(0,0)内所包含的点的个数成为该坐标的水平,输出从水平到0到水平n-1的点的个数
ps :此题树状数组和线段树都可以,以为他的输入时有规律的,直接对x坐标求和就可以和,更新时也只对x坐标更新(不信可以模拟一下),
注意:对于数张数组直接用模板就可以了,但是x坐标要都+1,因为x可能是0,回形成死循环,tle了无数次
线段树要用数组,用指针链表也超时,因为建树的时候会耗时,可能是因为这个,反正一直在tle 啊,,
///线段树
#include <iostream>
#include<cstring>
#include<cstdio>
#include<cstdlib>
#include<algorithm>
#include<cmath>
#include<stack>
using namespace std;
int vis[32010*4];
int M[32010*4];
void build(int node,int b,int e,int x)
{
int mid=(b+e)/2;
M[node]++;
if(b==e)return ;
if(x<=mid)build(2*node,b,mid,x);
else build(2*node+1,mid+1,e,x);
}
int query(int node,int b,int e,int l,int r)
{
if(l<=b&&e<=r)
{
return M[node];
}
else
{
int mid=(b+e)/2;
int ans=0;
if(l<=mid)ans+= query(2*node,b,mid,l,r);
if(r>mid)ans+=query(2*node+1,mid+1,e,l,r);
return ans;
}
return 0;
}
int main()
{
int n;
int a,b;
while(~scanf("%d",&n))
{
memset(vis,0,sizeof(vis));
memset(M,0,sizeof(M));
for(int i=0; i<n; i++)
{
scanf("%d%d",&a,&b);
a++;
vis[query(1,1,32010,1,a)]++;
build(1,1,32010,a);
}
for(int i=0; i<n; i++)
{
printf("%d\n",vis[i]);
}
}
return 0;
}
///树状数组
#include <iostream>
#include<cstring>
#include<cstdio>
#include<cstdlib>
#include<algorithm>
#include<cmath>
#include<stack>
using namespace std;
int c[35000];
int ans[16000];
const int m=32005;
int lowbit(int i)
{
return i&(-i);
}
int sum(int i)
{
int s=0;
while(i>0)
{
s+=c[i];
i-=lowbit(i);
}
return s;
}
void updata(int i)
{
while(i<=m)
{
c[i]++;
i+=lowbit(i);
}
}
int main()
{
int n;
while(~scanf("%d",&n))
{
int a,b;
memset(c,0,sizeof(c));
memset(ans,0,sizeof(ans));
for(int i=0;i<n;i++)
{
scanf("%d%d",&a,&b);
a++;
ans[sum(a)]++;
updata(a);
}
for(int i=0;i<n;i++)
{
printf("%d\n",ans[i]);
}
}
return 0;
}
贴上线段树超时的代码 ,有看出的大神请留言(感激不尽):
#include <iostream>
#include<cstring>
#include<cstdio>
#include<cstdlib>
#include<algorithm>
#include<cmath>
#include<stack>
using namespace std;
struct node
{
int l,r;
int sum;
struct node *left,*right;
}*cur;
int vis[32010*4];
void creat(struct node *cur,int l,int r)
{
cur->l=l;
cur->r=r;
cur->sum=0;
if(l<r)
{
cur->left=new node;
cur->right=new node;
int mid=(l+r)>>1;
creat(cur->left,l,mid);
creat(cur->right,mid+1,r);
}
else cur->left=cur->right=NULL;
}
void change(struct node *cur,int l,int r)
{
if(l<=cur->l&&cur->r<=r)
{
cur->sum++;
}
else
{
int mid=(cur->l+cur->r)>>1;
if(l<=mid)
{
change(cur->left,l,r);
}
if(r>mid)
{
change(cur->right,l,r);
}
cur->sum=cur->left->sum+cur->right->sum;
}
}
int query(struct node *cur,int l,int r)
{
if(l<=cur->l&&cur->r<=r)
{
return cur->sum;
}
else
{
int ans=0;
int mid=(cur->l+cur->r)/2;
if(l<=mid)
{
ans+=query(cur->left,l,r);
}
if(r>mid)ans+=query(cur->right,l,r);
return ans;
}
//return 0;
}
int main()
{
int n;
while(~scanf("%d",&n))
{
cur=new node;
creat(cur,1,32010);
int x,y;
memset(vis,0,sizeof(vis));
for(int i=0;i<n;i++)
{
scanf("%d%d",&x,&y);
x+=2;
int q=query(cur,1,x);
vis[q]++;
change(cur,x,x);
}
for(int i=0;i<n;i++)
{
printf("%d\n",vis[i]);
}
}
return 0;
}