1 条题解

  • 1
    @ 2025-1-18 19:50:05
    #include <bits/stdc++.h>
    using namespace std;
    const int N = 1e5 + 5;
    struct Node 
    {
        int a, b, c, cnt, ans;
    } a[N], b[N];
    inline int lowbit(int x) 
    {
        return x & (-x);
    }
    int n, k, tr[N * 2], ans[N * 2];
    void Update(int p, int d) 
    {
        for (; p <= k; p += lowbit(p))
            tr[p] += d;
    }
    int Q(int p) 
    {
        int ret = 0;
    
        for (; p; p -= lowbit(p))
            ret += tr[p];
    
        return ret;
    }
    bool cmp1(Node a, Node b) 
    {
        if (a.a != b.a)
            return a.a < b.a;
    
        if (a.b != b.b)
            return a.b < b.b;
    
        return a.c < b.c;
    }
    bool cmp2(Node a, Node b) 
    {
        if (a.b != b.b)
            return a.b < b.b;
    
        return a.c < b.c;
    }
    bool operator != (Node a, Node b) 
    {
        return a.a != b.a || a.b != b.b || a.c != b.c;
    }
    void Discrete() 
    {
        sort(b + 1, b + n + 1, cmp1);
        int cnt = 0, t = 0;
    
        for (int i = 1; i <= n; i++) 
        {
            cnt++;
    
            if (b[i] != b[i + 1])
                a[++t] = b[i], a[t].cnt = cnt, cnt = 0;
        }
    
        n = t;
    }
    void CDQ(int l, int r) 
    {
        if (l >= r) return ;
    
        int mid = (l + r) / 2;
        CDQ(l, mid);
        CDQ(mid + 1, r);
        int j = l, o = l;
    
        for (int i = mid + 1; i <= r; i++) 
        {
            while (j <= mid && a[i].b >= a[j].b) 
            {
                b[o++] = a[j];
                Update(a[j].c, a[j].cnt), j++;
            }
    
            a[i].ans += Q(a[i].c);
            b[o++] = a[i];
        }
    
        for (int i = l; i < j; i++)
            Update(a[i].c, -a[i].cnt);
    
        for (int i = j; i <= mid; i++)
            b[o++] = a[i];
    
        for (int i = l; i <= r; i++)
            a[i] = b[i];
    }
    int main() 
    {
        scanf("%d%d", &n, &k);
        int m = n;
    
        for (int i = 1; i <= n; i++)
            scanf("%d%d%d", &b[i].a, &b[i].b, &b[i].c);
    
        Discrete();
        memset(b, 0, sizeof(b));
        CDQ(1, n);
    
        for (int i = 1; i <= n; i++)
            ans[a[i].ans + a[i].cnt - 1] += a[i].cnt;
    
        for (int i = 0; i < m; i++)
            printf("%d\n", ans[i]);
    
        return 0;
    }
    
    

    信息

    ID
    5543
    时间
    1000ms
    内存
    256MiB
    难度
    10
    标签
    递交数
    1
    已通过
    1
    上传者