hdu 1541 Stars 树状数组水题

Stars

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)

Total Submission(s): 5210    Accepted Submission(s): 2053

Problem 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.

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

按照y的增序给出星星的坐标 如果有n颗星星在一颗星星的左下角 那这颗星星的等级就是n+1,问每个等级有几颗星星

树状数组题目 每次给出坐标时求一次getsum(x)

#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
int bit[32222],ans[15555];
int n;
int lowbit(int x){
    return x&(-x);
}
int getsum(int x){
    int cnt=0;
    while(x>0){
	cnt+=bit[x];
	x-=lowbit(x);
    }
    return cnt;
}
void add(int x,int a){
   while(x<32222){
      bit[x]+=a;
     x+=lowbit(x);
   }
}
int main(){
    int x,y;
    while(scanf("%d",&n)!=EOF){
	memset(ans,0,sizeof(ans));
	memset(bit,0,sizeof(bit));
	for(int i=1;i<=n;i++){
	    scanf("%d %d",&x,&y);
	    x++;
	    ans[getsum(x)]++;
	    add(x,1);
	}
	for(int i=0;i<n;i++)
	    printf("%d\n",ans[i]);
    }
    return 0;
}
时间: 2024-08-05 19:35:03

hdu 1541 Stars 树状数组水题的相关文章

hdu 1541 Stars 树状数组模板题

#include<iostream> #include<cstdio> #include<cstring> using namespace std; const int  maxn=15010; const int maxlen=32010; int tree[4*maxn]; int lowbit(int n) { return (n&-n); } int getsum(int i) { int sum=0; while(i>0) { sum+=tree

POJ 2352 &amp;&amp; HDU 1541 Stars (树状数组)

一开始想,总感觉是DP,可是最后什么都没想到.还暴力的交了一发. 然后开始写线段树,结果超时.感觉自己线段树的写法有问题.改天再写.先把树状数组的写法贴出来吧. ~~~~~~~~~~~~~~~~~~~~~~~~ 树状数组不懂的去看刘汝佳的大白书,那个图画得很清楚. 题目大意:星星的坐标以y递增的顺序给出,这些点的左下方的点数代表这个点的级数,问0~N-1的级数有多少个?其实y根本木有用. 题目链接:http://poj.org/problem?id=2352 http://acm.hdu.edu

POJ 2352 &amp;amp;&amp;amp; HDU 1541 Stars (树状数组)

一開始想,总感觉是DP,但是最后什么都没想到.还暴力的交了一发. 然后開始写线段树,结果超时.感觉自己线段树的写法有问题.改天再写.先把树状数组的写法贴出来吧. ~~~~~~~~~~~~~~~~~~~~~~~~ 树状数组不懂的去看刘汝佳的大白书,那个图画得非常清楚. 题目大意:星星的坐标以y递增的顺序给出,这些点的左下方的点数代表这个点的级数,问0~N-1的级数有多少个?事实上y根本木实用. 题目链接:http://poj.org/problem?id=2352 http://acm.hdu.e

HDU 1541 Stars 树状数组简单应用

点击打开链接 Stars Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 5140    Accepted Submission(s): 2020 Problem Description Astronomers often examine star maps where stars are represented by points o

hdu1541 Stars 树状数组水题

Stars Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 5378    Accepted Submission(s): 2125 Problem Description Astronomers often examine star maps where stars are represented by points on a pla

HDU 1541 Stars 树状数组

看完题解自己敲了一遍 还需要加深理解吧 #include<cstdio> #include<cstring> #include<iostream> using namespace std; int c[50000],num[50000]; int N=32100; int lowbit(int x) { return x&(-x); } int sum(int x) { int ret=0; while(x>0) { ret+=c[x]; x-=lowbi

HDU1166 敌兵布阵 树状数组水题

中文题目,很简单的题目,区间求和,当然对于线段树来说也很水,为了练习一下树状数组,多做做水题吧,加深理解,并且打好基础,我算是被没打好基础给吓坏了,宁可多花几个小时 刷刷水题扎实点, 很裸的题目 操作也很裸,了解树状数组的肯定能做 #include<iostream> #include<cstdio> #include<list> #include<algorithm> #include<cstring> #include<string&g

HDU 5147 Sequence II 树状数组水题

无聊咯,学某y,水一发 给你n个数的排列,A[1]到A[n],统计多少四元组(a,b,c,d)满足,1<=a<b<c<d<=n,且A[a]<A[b],A[c]<A[d] 树状数组统计前缀和咯 1 #include<cstdio> 2 #include<cstring> 3 #include<cctype> 4 typedef long long ll; 5 const int maxn=5e4+10; 6 int T,n,a[m

【不可能的任务1/200】bzoj1103树状数组水题

(卧槽,居然规定了修改的两点直接相连,亏我想半天) 非常水的题,用dfs序(而且不用重复,应该是直接规模为n的dfs序)+树状数组可以轻松水 收获:树状数组一遍A(没啥好骄傲的,那么简单的东西) 1 #include <cstdio> 2 #include <iostream> 3 using namespace std; 4 int N=0,n,m,p,q,a[300000],l[300000],pos[300000],end[300000],son[300000],bro[30