树状数组模板 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;
}

猜你喜欢

转载自blog.csdn.net/qq_38842456/article/details/79857891
今日推荐