Poj 2352 Stars

本文介绍了一种解决二维偏序问题的算法实现,通过使用线段树和离散化等技术来加速处理大量坐标数据的比较操作。该算法利用自定义的数据结构Bit实现了高效的前缀和查询及更新,适用于解决诸如比赛排名等实际问题。

简单二维偏序

#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <vector>
#define DBG(x) cerr << #x << " = " << x << endl

using namespace std;
typedef long long LL;

const int N = 32000 + 16;

struct Point {
    int x, y;
    Point() {}
    Point(int x, int y): x(x), y(y) {}
    bool operator < (const Point &rhs) const { return x < rhs.x || (x == rhs.x && y < rhs.y); }
    void read() { scanf("%d%d", &x, &y); ++x, ++y; }
} P[N];

struct Bit {
    int C[N];
    int n;
    void init(int n) {
        this->n = n;
        fill(C, C + n + 1, 0);
    }
    int lowbit(int x) { return x & -x; }
    void add(int x, int v) {
        for(; x <= n; x += lowbit(x)) C[x] += v;
    }
    int sum(int x) {
        int res = 0;
        for(; x; x -= lowbit(x)) res += C[x];
        return res;
    }
    int sum(int l, int r) {
        return sum(r) - sum(l - 1);
    }
} bit;

int ans[N];

int main(int argc, char **argv) {
    int n;
    while(~scanf("%d", &n)) {
        for(int i = 0; i < n; ++i) P[i].read();
        fill(ans, ans + n, 0);
        sort(P, P + n);
        bit.init(N - 1);
        for(int i = 0; i < n; ++i) {
            ++ans[bit.sum(P[i].y)];
            bit.add(P[i].y, 1);
        }
        for(int i = 0; i < n; ++i) printf("%d\n", ans[i]);
    }
    return 0;
}

转载于:https://www.cnblogs.com/ToRapture/p/7552784.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值