zoukankan      html  css  js  c++  java
  • 树状数组第一题: POJ 2352 Stars

    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.

    #include<cstdio>
    #include<cstring>
    using namespace std;
    int n,f[100005];
    int lowbit(int x){
    return x&(-x);
    }
    int sum(int l,int r){//求和
    int ans=0;
    for(int i=r;i>0;i-=lowbit(i))ans+=f[i];
    for(int i=l;i>0;i-=lowbit(i))ans-=f[i];
    return ans;
    }
    void add(int x){//进行修改操作
    for(int i=x;i<=65536;i+=lowbit(i))f[i]++;
    }
    int x,y,vis[100005];
    int main(){
        scanf("%d",&n);
        for(int i=1;i<=n;i++){
        scanf("%d%d",&x,&y);
        vis[sum(0,++x)]++;
        add(x);
        }
    for(int i=0;i<n;i++){
    printf("%d ",vis[i]);
    }
    }

  • 相关阅读:
    【美团技术团队文章--学习笔记】之 Java动态追踪技术探究
    mq
    为啥要读写分离
    算法 数据结构
    对扩展开放,对修改关闭
    redis 事务
    准实时数仓设计方案
    Scala Puzzlers 系列(一):占位符的使用
    【面试题】大数据开发岗位
    Hive 分区和分桶
  • 原文地址:https://www.cnblogs.com/c201904xyorz/p/9990781.html
Copyright © 2011-2022 走看看