树状数组模板 POJ 2352 [有赠品]

本文介绍了一种算法,用于统计星图上星星的级别分布。通过使用树状数组或线段树,该算法能够高效地计算出每个级别的星星数量。
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
Hint
This problem has huge input data,use scanf() instead of cin to read data to avoid time limit exceed.

注意题目中有一句这样的话哦

Stars are listed in ascending order of Y coordinate. Stars with equal Y coordinates are listed in ascending order of X coordinate.

这也就是说 对于第i个点(xi,yi)来说,所有可能在[0,xi],[0,yi]之内的点已经输入完毕 他之后的点是不会再影响这个点的level值的.

所以我们的树状数组存储的就应该是某一段范围内有多少个点,用线段树当然可以咯 时间大概在600多ms 用树状数组的话时间在516ms 空间压缩到了996kb 差不多是线段树的一半,所以赠品就是线段树的代码咯~

代码如下:

#include<cstdio>
#include<cstdlib>
#include<cmath>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<queue>
#include<map>
#include<stack>
#include<set>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const int MAX_N=100000;
const int INF = 0x3f3f3f3f;
const int mod = 1e9+7;  
int c[32010];
int ans[32010];
int sum(int x){  
    int ret=0;  
    while(x>0){  
        ret=ret+c[x];  
        x=x-(x&(-x));  
    }  
    return ret;  
}  

void add(int x,int d){  
    while(x<=32010){
        c[x]=c[x]+d;  
        x=x+(x&(-x));  
    }  
}  

int main(){
    ios::sync_with_stdio(false);
    int n,x,y;
    while(cin>>n){  
        memset(ans,0,sizeof(ans));
        memset(c,0,sizeof(c));
        for(int i=0;i<n;i++){
            cin>>x>>y;
            x++;
            ans[sum(x)]++;  
            add(x,1);  
        }  
        for(int i=0;i<n;i++)  
            cout<<ans[i]<<endl;
    }
    return 0;  
}  

赠品 线段树题解:

#include<cstdio>
#include<cstdlib>
#include<cmath>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<queue>
#include<map>
#include<stack>
#include<set>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const int MAX_N=100000;
const int INF = 0x3f3f3f3f;

ll tree[800000];
ll lazy[802020];
int ans[800000];
void init(ll n){
    for(ll i=0;i<n*4;i++)
        tree[i]=0;
    for(ll i=0;i<n;i++)
        ans[i]=0;
}
void build(ll l,ll r,ll root){
    if(l==r){
        cin>>tree[root];
        return;
    }
    ll mid=(l+r)>>1;
    build(l,mid,root<<1);
    build(mid+1,r,root<<1|1);
    tree[root]=tree[root<<1]+tree[root<<1|1];
}

void changepoint(ll l,ll r,ll root,ll poll,ll val){
    if(l==r){
        tree[root]+=val;
        return;
    }
    ll mid=(l+r)>>1;
    if(poll<=mid)
        changepoint(l,mid,root<<1,poll,val);
    else    
        changepoint(mid+1,r,root<<1|1,poll,val);
    tree[root]=tree[root*2]+tree[root*2+1];
}

ll searchpoint(ll l,ll r,ll root,ll L,ll R){
    if(L<=l&&R>=r)
        return tree[root];
    ll mid=(l+r)>>1;
    if(R<=mid)
        return searchpoint(l,mid,root<<1,L,R);
    else if(L>mid)
        return searchpoint(mid+1,r,root<<1|1,L,R);
    else
        return searchpoint(l,mid,root<<1,L,mid)+searchpoint(mid+1,r,root<<1|1,mid+1,R); 
}
int main(){
    ios::sync_with_stdio(false);
    int n,x,y;
    while(cin>>n){
        init(n);
        for(int i=0;i<n;i++){
            cin>>x>>y;
            changepoint(0,33000,1,x,1);
            ans[searchpoint(0,33000,1,0,x)]++;
        }
        for(int i=1;i<=n;i++)
            cout<<ans[i]<<endl;
    }
    return 0;
}
dnSpy是目前业界广泛使用的一款.NET程序的反编译工具,支持32位和64位系统环境。它允许用户查看和编辑.NET汇编和反编译代码,以及调试.NET程序。该工具通常用于程序开发者在维护和调试过程中分析程序代码,尤其在源代码丢失或者无法获取的情况下,dnSpy能提供很大的帮助。 V6.1.8版本的dnSpy是在此系列软件更新迭代中的一个具体版本号,代表着该软件所具备的功能与性能已经达到了一个相对稳定的水平,对于处理.NET程序具有较高的可用性和稳定性。两个版本,即32位的dnSpy-net-win32和64位的dnSpy-net-win64,确保了不同操作系统架构的用户都能使用dnSpy进行软件分析。 32位的系统架构相较于64位,由于其地址空间的限制,只能支持最多4GB的内存空间使用,这在处理大型项目时可能会出现不足。而64位的系统能够支持更大的内存空间,使得在处理大型项目时更为方便。随着计算机硬件的发展,64位系统已经成为了主流,因此64位的dnSpy也更加受开发者欢迎。 压缩包文件名“dnSpy-net-win64.7z”和“dnSpy-net-win32.7z”中的“.7z”表示该压缩包采用了7-Zip压缩格式,它是一种开源的文件压缩软件,以其高压缩比著称。在实际使用dnSpy时,用户需要下载对应架构的压缩包进行解压安装,以确保软件能够正确运行在用户的操作系统上。 dnSpy工具V6.1.8版本的发布,对于.NET程序员而言,无论是32位系统还是64位系统用户,都是一个提升工作效率的好工具。用户可以根据自己计算机的操作系统架构,选择合适的版本进行下载使用。而对于希望进行深度分析.NET程序的开发者来说,这个工具更是不可或缺的利器。
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值