三维偏序

#include <bits/stdc++.h>
using namespace std;
#define ll long long
const int maxn = 2e5 + 5;
struct node
{
    int x, y, z, ans, cnt;
    bool operator == (const node& rhs) {
        return x==rhs.x && y == rhs.y && z == rhs.z;
    }
}a[maxn], b[maxn];

struct BIT
{
    int c[maxn];
    int lowbit(int x) {return x & -x;}
    void add(int x, int y) {while(x < maxn) c[x] += y, x += lowbit(x);}
    int sum(int x){int res = 0; while(x) res+=c[x], x-=lowbit(x); return res;}
}bit;

void solve(int l, int r) 
{
    if(l == r)return;
    int mid = (l + r) / 2;
    solve(l, mid), solve(mid + 1, r);
    sort(a + l, a + mid + 1, [](node a, node b) { return a.y == b.y ? a.z < b.z : a.y < b.y; });
    sort(a + mid + 1, a + r + 1, [](node a, node b) { return a.y == b.y ? a.z < b.z : a.y < b.y; });
    int i = l, j = mid + 1;
    while(j <= r)
    {
        while(i <= mid && a[i].y <= a[j].y)
        {
            bit.add(a[i].z, a[i].cnt);
            ++i;
        }
        a[j].ans += bit.sum(a[j].z);
        ++j;
    }
    for(int j = l; j < i; ++j)bit.add(a[j].z, -a[j].cnt);
}
int main() {
    ios::sync_with_stdio(false), cin.tie(nullptr);
    int n, k;
    while(cin >> n >> k) 
    {
        for(int i = 0; i < n; ++i)
        {
            cin >> b[i].x >> b[i].y >> b[i].z;
        }
        sort(b, b + n, [](node a, node b) { return a.x == b.x ? a.y == b.y ? a.z < b.z : a.y < b.y : a.x < b.x; });
        int cnt = 0, id = 0;
        for(int i = 0; i < n; ++i)
        {
            cnt++;
            if(b[i] == b[i + 1])continue;
            else  a[id] = b[i], a[id].cnt = cnt, cnt = 0, ++id;
        }
        solve(0, id - 1);
        vector<int>d(n, 0);
        for(int i = 0; i < id; ++i)
        {
            // cout << a[i].x << " " << a[i].y << " " << a[i].z << " " << a[i].ans << " " << a[i].cnt << endl;
            d[a[i].ans + a[i].cnt - 1] += a[i].cnt;
        }
        for(auto x : d)cout<< x << endl;
    }
}

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值