zoukankan      html  css  js  c++  java
  • 数星星(树状数组或者线段树)

    天空中有一些星星,这些星星都在不同的位置,每个星星有个坐标。如果一个星星的左下方(包含正左和正下)有 kk颗星星,就说这颗星星是 kk 级的。
    一句话题意 给定 nn 个点,定义每个点的等级是在该点左下方(含正左、正下)的点的数目,试统计每个等级有多少个点
    输入
    第一行一个整数 N,表示星星的数目;
    接下来 N 行给出每颗星星的坐标,坐标用两个整数 x,yx,y 表示;
    不会有星星重叠。星星按 yy坐标增序给出,yy 坐标相同的按 xx 坐标增序给出。
    输出
    N 行,每行一个整数,分别是00 级,11 级,22 级,……,N−1N−1 级的星星的数目。
    样例输入
    5
    1 1
    5 1
    7 1
    3 3
    5 5
    样例输出
    1
    2
    1
    1
    0
    提示
    对于全部数据,1≤N≤1.5×10^4 ,0≤x,y≤3.2×10^4 ,1≤N≤1.5×10^4 ,0≤x,y≤3.2×10^4

    很简单的树状数组吧,只需要维护每个点当前的横坐标及之前有多少个星星

    #include<bits/stdc++.h>
    using namespace std;
    int n,c[32010],num[32010],x,y;
    inline int read(){
    	char ch=getchar();
    	int res=0;
    	while(!isdigit(ch)) ch=getchar();
    	while(isdigit(ch)) res=(res<<1)+(res<<3)+(ch^48),ch=getchar();
    	return res;
    }
    inline int lowbit(int x)
    {
    	return x&(-x);
    }
    inline int getx(int x)
    {
    	int sum=0;
    	while(x>0)
    	{
    		sum+=c[x];
    		x-=lowbit(x);
    	}
    	return sum;
    }
    inline void update(int x)
    {
    	while(x<=32010)
    	{
    		c[x]++;
    		x+=lowbit(x);
    	}
    }
    int main(){
    	n=read();
    	for(int i=1;i<=n;i++)
    	{
    		x=read(),y=read();
    		update(x+1);
    		num[getx(x+1)-1]++;//因为包括当前正下方的点,所以要getx(x+1);
    	}
    	for(int i=0;i<n;i++)
    	{
    		cout<<num[i]<<'
    ';
    	}
    }
    
    
    
    
  • 相关阅读:
    BurpSuite—-Spider模块(蜘蛛爬行)
    BurpSuite系列(一)----Proxy模块(代理模块)
    hadoop HA集群搭建步骤
    HBase详解
    MapReduce两种执行环境介绍:本地测试环境,服务器环境
    HBase性能优化方法总结
    HDFS原理解析
    ZooKeeper 典型应用场景
    Redis总结
    基于Apache Curator框架的ZooKeeper使用详解
  • 原文地址:https://www.cnblogs.com/forever-/p/9736065.html
Copyright © 2011-2022 走看看