bzoj3262陌上花开 cdq分治入门题

Description 
有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 Input 

复制代码
10 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 Output 

复制代码
3 
1 
3 
0 
1 
0 
1 
0 
0 
1
复制代码

水一发cdq分治

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
#include <stdio.h>
#include <string.h>
#include <algorithm>
#define RG register
#define dmax(a, b) ((a) > (b) ? (a) : (b))
#define dmin(a, b) ((a) < (b) ? (a) : (b))
#define gmax(i, j) ((i) < (j) ? (i = j) : (i))
#define gmin(i, j) ((i) > (j) ? (i = j) : (i))
#define sqr(x) ((x) * (x))
#define dabs(x) ((x) < (0) ? (-x) : (x))
#define lowbit(x) ((x) & (-x))
#define For(i, a, b) for(RG int i = a, __u = b; i <= __u; i++)
#define set_file(File) freopen(#File".in","r",stdin)
 
const int MaxN = 100010;
const int MaxK = 200010;
 
struct flower{
    int x, y, z, cnt, ans;
}a[MaxN];
 
bool cmpx(const flower &u, const flower &v){
    if(u.x != v.x)return u.x < v.x;
    if(u.y != v.y)return u.y < v.y;
    return u.z < v.z;
}
 
bool cmpy(const flower &u, const flower &v){
    if(u.y != v.y)return u.y < v.y;
    return u.z < v.z;
}
 
int n, K, tot, ans[MaxN];
 
struct BIT{
    int c[MaxK];
    inline void add(RG int i, RG int d){
        for(; i <= n; i += lowbit(i))
            c[i] += d;
    }
    inline int sum(RG int i){
        RG int res=0;
        for(; i; i -= lowbit(i))
            res += c[i];
        return res;
    }
}b;
 
void CDQ(RG int l, RG int r){
    if(l == r){
        a[l].ans += a[l].cnt - 1;
        return;
    }
    RG int mid = l + r >> 1;
    CDQ(l, mid), CDQ(mid + 1, r);
    std::sort(a + l, a + mid + 1, cmpy);
    std::sort(a + mid + 1, a + r + 1, cmpy);
    RG int j = l;
    For(i, mid + 1, r){
        for(; a[j].y <= a[i].y && j <= mid; j++)
            b.add(a[j].z, a[j].cnt);
        a[i].ans += b.sum(a[i].z);
    }
    For(i, l, j - 1) b.add(a[i].z, -a[i].cnt);
    std::sort(a + l, a + r + 1, cmpy);
}
 
int main(){
    set_file(bzoj3262);
    scanf("%d%d", &n, &K);
    For(i, 1, n) scanf("%d%d%d", &a[i].x, &a[i].y, &a[i].z), a[i].ans = 1;
    std::sort(a + 1, a + 1 + n, cmpx);
    For(i, 1, n)
        if(i != 1 && a[i - 1].x == a[i].x && a[i - 1].y == a[i].y && a[i - 1].z == a[i].z)
            a[tot].cnt++;
        else
            a[++tot] = a[i], a[tot].cnt = 1;
    std::sort(a + 1, a + 1 + tot, cmpx);
    CDQ(1, tot);
    For(i, 1, tot) ans[a[i].ans] += a[i].cnt;
    For(i, 1, n) printf("%d\n",ans[i]);
    return 0;
}

  

posted @   keshuqi  阅读(323)  评论(1编辑  收藏  举报
编辑推荐:
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· 写一个简单的SQL生成工具
· AI 智能体引爆开源社区「GitHub 热点速览」
· C#/.NET/.NET Core技术前沿周刊 | 第 29 期(2025年3.1-3.9)
点击右上角即可分享
微信分享提示