• Day6


    有n朵花,每朵花有三个属性:花形(s)、颜色(c)、气味(m),用三个整数表示。
    现在要对每朵花评级,一朵花的级别是它拥有的美丽能超过的花的数量。
    定义一朵花A比另一朵花B要美丽,当且仅Sa>=Sb,Ca>=Cb,Ma>=Mb。
    显然,两朵花可能有同样的属性。需要统计出评出每个等级的花的数量。
     
    Input
    第一行为N,K (1 <= N <= 100,000, 1 <= K <= 200,000 ), 分别表示花的数量和最大属性值。
    以下N行,每行三个整数si, ci, mi (1 <= si, ci, mi <= K),表示第i朵花的属性
    Output

    包含N行,分别表示评级为0...N-1的每级花的数量。

    Sample Input10 3 3 3 3 2 3 3 2 3 1 3 1 1 3 1 2 1 3 1 1 1 2 1 2 2 1 3 2 1 2 1

    Sample Output3 1 3 0 1 0 1 0 0 1

     

    思路:CDQ分治板子题,这题注意去重+排序时要三个关键字都用

    using namespace std;
    #define lowbit(x) ((x)&(-x))
    typedef long long LL;
    
    const int maxm = 2e5+10;
    
    int C[maxm], N, K, cnt[maxm];
    
    void add(int x, int val) {
        for(; x <= K; x += lowbit(x))
            C[x] += val;
    }
    
    int getsum(int x) {
        int ret = 0;
        for(; x; x -= lowbit(x))
            ret += C[x];
        return ret;
    }
    
    void clearr(int x) {
        for(; x <= K; x += lowbit(x)) {
            if(C[x] == 0) break;
            C[x] = 0;
        }
    }
    
    struct Node {
        int S, C, M, sum, ans;
    } buf[maxm], res[maxm];
    
    bool cmp_S(Node a, Node b) {
        if(a.S == b.S && a.C == b.C) return a.M < b.M;
        if(a.S == b.S) return a.C < b.C;
        return a.S < b.S;
    }
    
    
    void CDQ(int L, int R) {
        if(L >= R) return;
        int mid = L+R >> 1;
        CDQ(L, mid), CDQ(mid+1, R);
        int i = L, j = mid+1, k = L;
        //左修改右查询
        while(i <= mid && j <= R) {
            if(res[i].C <= res[j].C) {
                add(res[i].M, res[i].sum);
                buf[k++] = res[i++];
            } else {
                res[j].ans += getsum(res[j].M);
                buf[k++] = res[j++];
            }
        }
        while(i <= mid)
            buf[k++] = res[i++];
        while(j <= R) {
            res[j].ans += getsum(res[j].M);
            buf[k++] = res[j++];
        }
        for(int t = L; t <= R; ++t) {
            clearr(buf[t].M); // 清空树状数组
            res[t] = buf[t];
        }
    }
    
    int main() {
        scanf("%d%d", &N, &K);
        for(int i = 0; i < N; ++i) {
            scanf("%d%d%d", &buf[i].S, &buf[i].C, &buf[i].M);
            buf[i].ans = 0;
        }
        sort(buf, buf+N, cmp_S);
        // 去重
        int now = 0, sz = 0;
        for(int i = 0; i < N; ++i) {
            now++;
            if(buf[i].S != buf[i+1].S || buf[i].C != buf[i+1].C || buf[i].M != buf[i+1].M) {
                res[sz] = buf[i];
                res[sz++].sum = now;
                now = 0;
            }
        }
        CDQ(0, sz-1);
        for(int i = 0; i < sz; ++i)
            cnt[res[i].ans+res[i].sum-1]+=res[i].sum;  //比他小的+和他一样的-自身(1)
        for(int i = 0; i < N; ++i)
            printf("%d
    ", cnt[i]);
        return 0;
    }
    View Code
  • 相关阅读:
    04_Javascript初步第二天(上)
    用python实现省市县多级嵌套下拉列表
    爬虫之 BeautifulSoup与Xpath
    爬虫之 selenium模块
    爬虫请求库 requests
    抽屉网自动点赞 评论
    爬取京东商品信息
    爬取豆瓣电影top250
    爬虫基本原理
    Django auth认证
  • 原文地址:https://www.cnblogs.com/GRedComeT/p/12208771.html
Copyright © 2020-2023  润新知