POJ 2481 Cows(树状数组)

题目分析

这道题真的是给我wa出血!!主要是因为数组忘了初始化,以后还是要注意这些问题。这道题我以先按E排序从大到小排序,然后按照S从小到大排序,排序完成即可以树状数组求解即可。注意前后2个S,E值相同的情况。

#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
const int maxn = 1e5+100;

int n, C[maxn];

struct Node{
    int x, y, id;
    Node() {}
    Node(int a, int b, int c):x(a), y(b), id(c){}
    bool operator < (const Node& temp)const{
        if(y != temp.y) return y > temp.y;
        else return x < temp.x;
    }
    bool operator == (const Node& temp) const{
        if(x == temp.x && y == temp.y) return true;
        else return false;
    }
}node[maxn];

int ans[maxn];

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

void add(int x, int y){
    while(x <= n){
        C[x] += y;
        x += lowbit(x);
    }
}

int sum(int x){
    int ret = 0;
    while(x > 0){
        ret += C[x];
        x -= lowbit(x);
    }
    return ret;
}

int main(){
    while(scanf("%d", &n) != EOF && n){
        memset(C, 0, sizeof(C));
        for(int i = 1; i <= n; i++){
            scanf("%d%d", &node[i].x, &node[i].y);
            node[i].x++, node[i].y++;
            node[i].id = i;
        }
        sort(node+1, node+1+n);
        for(int i = 1; i <= n; i++){
            if(node[i] == node[i-1]) ans[node[i].id] = ans[node[i-1].id];
            else
                ans[node[i].id] = sum(node[i].x);
            add(node[i].x, 1);
        }
        for(int i = 1; i < n; i++)
            printf("%d ", ans[i]);
        printf("%d\n", ans[n]);
    }
    return 0;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值