POJ 2481 Cows(树状数组)

来源:互联网 发布:淘宝服装图片拍摄技巧 编辑:程序博客网 时间:2024/06/14 10:54

题目分析

这道题真的是给我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 0
原创粉丝点击