HDU1541 Stars

题解:

注意y是递增输入的,所以后面输入的数不可能对前面星星的等级有贡献,所以直接边输入,边更新求和

注意到这里x最小值是可以为0的,在add操作中无法执行,所以全部x都+1好了

代码:

#include<bits/stdc++.h>
using namespace std;
#define LL long long
const int N=15100;
const int M=32100;

int x,y,c[M],q[N];

int lowbit(int x){return x&-x;}

void add(int x){
    int cnt=x;
    while(cnt<M){
        c[cnt]++;
        cnt+=lowbit(cnt);
    }
}

void sum(int x){
    int cnt=x,ans=0;
    while(cnt){
        ans+=c[cnt];
        cnt-=lowbit(cnt);
    }
    q[ans]++;
}

int main(){
    int n;
    while(~scanf("%d",&n)){
    memset(c,0,sizeof(c));
    memset(q,0,sizeof(q));
    for(int i=1;i<=n;i++){
        scanf("%d%d",&x,&y);
        x++;
        sum(x);
        add(x);
    }
    for(int i=0;i<n;i++) printf("%d\n",q[i]);
   }
    return 0;
}
时间: 2024-12-06 10:07:33

HDU1541 Stars的相关文章

HDU1541 Stars(树状数组)

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

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

HDU1541 Stars【树状数组】

题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1541 题目大意: 按顺序给你N颗星星的坐标,y是从小到大给出的.每个星星有一个等级,该等级为它左下角的星星 的个数.求每个等级的点有多少个. 思路: 因为y是从小到大给出的,那么可以直接忽略y,只记录x,求出(x,y)左边有多少个点就可以了. 用Ans[]数组表示每个等级的星星数.求(x,y)左边有多少个点用树状数组来做,每给一个点,就求出 x左边的点个数.作为Ans数组下标,累加个数,最后输出A

树状数组入门 hdu1541 Stars

树状数组 树状数组(Binary Indexed Tree(B.I.T), Fenwick Tree)是一个查询和修改复杂度都为log(n)的数据结构.主要用于查询任意两位之间的所有元素之和,但是每次只能修改一个元素的值:经过简单修改可以在log(n)的复杂度下进行范围修改,但是这时只能查询其中一个元素的值(如果加入多个辅助数组则可以实现区间修改与区间查询). 树状数组和线段树很像,但能用树状数组解决的问题,基本上都能用线段树解决,而线段树能解决的树状数组不一定能解决.相比较而言,树状数组效率要

ACM-树状数组之Stars——hdu1541,poj2352

***************************************转载请注明出处:http://blog.csdn.net/lttree*************************************** Stars Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 4129    Accepted Submissi

hdu 1541 Stars 解题报告

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1541 题目意思:有 N 颗星星,每颗星星都有各自的等级.给出每颗星星的坐标(x, y),它的等级由所有比它低层(或者同层)的或者在它左手边的星星数决定.计算出每个等级(0 ~ n-1)的星星各有多少颗. 我只能说,题目换了一下就不会变通了,泪~~~~ 星星的分布是不是很像树状数组呢~~~没错,就是树状数组题来滴! 按照题目输入,当前星星与后面的星星没有关系.所以只要把 x 之前的横坐标加起来就可以了

POJ 2352 Stars(树状数组)

                                                                 Stars Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 44309   Accepted: 19236 Description Astronomers often examine star maps where stars are represented by points on a pla

HDU 5533 Dancing Stars on Me 计算几何瞎暴力

Dancing Stars on Me Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others)Total Submission(s): 1184    Accepted Submission(s): 651 Problem Description The sky was brushed clean by the wind and the stars were cold in a b

ZOJ 2352 Stars

Stars Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 22870 Accepted: 9967 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