POJ - 2532 Stars

VJ的题目链接:http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=14610


题意:给你一个笛卡尔坐标系,存在很多点, 问左下方(左边,下面, 左下边都算)有0到N-1个点的 点有多少个。


解决方法线段树,或者树状数组。


树状数组AC代码:

#include<iostream>
#include<cstdio>
#include<cstring>

using namespace std;
const int maxn=32000+10;
int n, x;
int a[maxn];
int tree[maxn*2];
int num[maxn];

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

int getsum(int u){
    int sum=0;
    while(u>0){
        sum+=tree[u];
        u-=lowbit(u);
    }
    return sum;
}

void update(int u){
    while(u<=maxn){
        ++tree[u];
        u+=lowbit(u);
    }
}

int main(){
    while(~scanf("%d", &n)){
        for(int i =1; i<=n;++i) scanf("%d%d", &a[i], &x);

        memset(num, 0, sizeof(num));
        memset(tree, 0, sizeof(tree));

        for(int i =1; i<=n;++i){
            ++num[getsum(a[i]+1)];
            update(a[i]+1);
        }
        for(int i =0; i<n; ++i)
            printf("%d\n", num[i]);
    }
    return 0;
}



线段树AC代码:

#include<iostream>
#include<cstdio>
#include<cstring>

using namespace std;
int n, x;
const int maxn=32000+5;
struct Node{
    int l, r, num;
}tree[maxn*4];
int a[maxn];
int num[maxn];

void build(int l, int r, int o){
    tree[o].l=l;
    tree[o].r=r;
    tree[o].num=0;
    if(l==r) return;
    int mid=(l+r)>>1;
    build(l, mid, o<<1);
    build(mid+1, r, (o<<1)+1);
}

void update(int c, int o){
    int l=tree[o].l;
    int r=tree[o].r;
    if(l==r) { tree[o].num++; return; }

    int mid=(l+r)>>1;
    if(c<=mid) update(c, o<<1);
    else update(c, (o<<1)+1);
    tree[o].num=tree[o<<1].num+tree[o*2+1].num;
}

int query(int L, int R, int o){
    int l=tree[o].l;
    int r=tree[o].r;
    if(l==L&& r==R) return tree[o].num;

    int mid=(l+r)>>1;
    if(R<=mid) return query(L, R, o<<1);
    else if(L>mid)  return query(L, R, (o<<1)+1);
    else return query(L, mid, o<<1)+query(mid+1, R, (o<<1)+1);
}

int main(){
    while(~scanf("%d", &n)){
        for(int i=1; i<=n;++i)
            scanf("%d%d", &a[i], &x);
        build(1, 32002, 1);

        memset(num, 0, sizeof(num));
        for(int i =1; i<=n;++i){
            int nn=query(1,a[i]+1,1);
            ++num[nn];
            update(a[i]+1, 1);
        }
        for(int i =0;i<n;++i)
            printf("%d\n", num[i]);
    }
    return 0;
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值