zoukankan      html  css  js  c++  java
  • 【bzoj3262】陌上花开

    题目描述:

    有n朵花,每朵花有三个属性:花形(s)、颜色(c)、气味(m),又三个整数表示。现要对每朵花评级,一朵花的级别是它拥有的美丽能超过的花的数量。定义一朵花A比另一朵花B要美丽,当且仅当Sa>=Sb,Ca>=Cb,Ma>=Mb。显然,两朵花可能有同样的属性。需要统计出评出每个等级的花的数量。

    输入:
    第一行为N,K (1 <= N <= 100,000, 1 <= K <= 200,000 ), 分别表示花的数量和最大属性值。
    以下N行,每行三个整数si, ci, mi (1 <= si, ci, mi <= K),表示第i朵花的属性

    输出:

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

    样例输入:
    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

    样例输出:
    3
    1
    3
    0
    1
    0
    1
    0
    0
    1

    题解:

    三维偏序问题。排序一维,cdq分治一维,最后一维用树状数组就可以啦~最后两维比起树套树,分治+树状数组果然好写得多~

    代码:

    #include <cstdio>
    #include <cstring>
    #include <algorithm>
    #include <cmath>
    
    #ifdef WIN32
    	#define LL "%I64d"
    #else
    	#define LL "%lld"
    #endif
    
    #ifdef CT
    	#define debug(...) printf(__VA_ARGS__)
    	#define setfile() 
    #else
    	#define debug(...)
    	#define filename ""
    	#define setfile() freopen(filename".in", "r", stdin); freopen(filename".out", "w", stdout);
    #endif
    
    #define R register
    #define getc() (S == T && (T = (S = B) + fread(B, 1, 1 << 15, stdin), S == T) ? EOF : *S++)
    #define dmax(_a, _b) ((_a) > (_b) ? (_a) : (_b))
    #define dmin(_a, _b) ((_a) < (_b) ? (_a) : (_b))
    #define cmax(_a, _b) (_a < (_b) ? _a = (_b) : 0)
    #define cmin(_a, _b) (_a > (_b) ? _a = (_b) : 0)
    char B[1 << 15], *S = B, *T = B;
    inline int FastIn()
    {
    	R char ch; R int cnt = 0; R bool minus = 0;
    	while (ch = getc(), (ch < '0' || ch > '9') && ch != '-') ;
    	ch == '-' ? minus = 1 : cnt = ch - '0';
    	while (ch = getc(), ch >= '0' && ch <= '9') cnt = cnt * 10 + ch - '0';
    	return minus ? -cnt : cnt;
    }
    #define maxn 100010
    struct Point
    {
    	int x, y, z, ans, tim;
    	inline bool operator < (const Point &that) const
    	{
    		if (x != that.x) return x < that.x;
    		if (y != that.y) return y < that.y;
    		return z < that.z;
    	}
    	inline bool operator == (const Point &that) const
    	{
    		return x == that.x && y == that.y && z == that.z;
    	}
    }p[maxn], t[maxn];
    int ans[maxn], now, n, k;
    #define maxm 200010
    int bit[maxm], last[maxm];
    #define lowbit(_x) ((_x) & -(_x))
    inline void add(R int x, R int val)
    {
    	for (; x <= k; x += lowbit(x))
    	{
    		if (last[x] != now)
    			bit[x] = 0;
    		bit[x] += val;
    		last[x] = now;
    	}
    }
    inline int query(R int x)
    {
    	R int ret = 0;
    	for (; x; x -= lowbit(x))
    	{
    		if (last[x] == now) ret += bit[x];
    	}
    	return ret;
    }
    void cdq(R int left, R int right)
    {
    	if (left == right) return;
    	R int mid = left + right >> 1;
    	cdq(left, mid);
    	cdq(mid + 1, right);
    	++now;
    	for (R int i = left, j = mid + 1; j <= right; ++j)
    	{
    		for (; i <= mid && p[i].y <= p[j].y; ++i)
    			add(p[i].z, p[i].tim);
    		p[j].ans += query(p[j].z);
    	}
    	R int i, j, kk = 0;
    	for (i = left, j = mid + 1; i <= mid && j <= right; )
    	{
    		if (p[i].y <= p[j].y)
    			t[kk++] = p[i++];
    		else
    			t[kk++] = p[j++];
    	}
    	for (; i <= mid; )
    		t[kk++] = p[i++];
    	for (; j <= right; )
    		t[kk++] = p[j++];
    	for (R int i = 0; i < kk; ++i)
    		p[left + i] = t[i];
    }
    int main()
    {
    //	setfile();
    	n = FastIn(); k = FastIn();
    	for (R int i = 1; i <= n; ++i)
    	{
    		R int x = FastIn(), y = FastIn(), z = FastIn();
    		p[i] = (Point) {x, y, z, 0, 1};
    	}
    	std::sort(p + 1, p + n + 1);
    	R int cnt = 0;
    	for (R int i = 1; i <= n; ++i)
    	{
    		if (p[i] == p[i - 1])
    			p[cnt].tim++;
    		else p[++cnt] = p[i];
    	}
    	for(R int i = 1; i <= cnt; ++i) p[i].ans = p[i].tim - 1;
    	cdq(1, cnt);
    	for (R int i = 1; i <= cnt; ++i)
    		ans[p[i].ans] += p[i].tim;
    	for (R int i = 0; i < n; ++i) printf("%d
    ",ans[i] );
    	return 0;
    }
    /*
    5 3
    2 2 2
    1 1 1
    1 1 1
    1 1 1
    2 2 2
    */



  • 相关阅读:
    NGINX将PHP带参数的URL地址重定向二级或多级域名访问
    mysql优化 ON DUPLICATE KEY UPDATE
    修改Nginx配置文件来隐藏index.php
    Redis 如何保持和MySQL数据一致【二】
    Redis 如何保持和MySQL数据一致【一】
    Redis计算地理位置距离-GeoHash
    了解一下Redis队列【缓兵之计-延时队列】
    来了解一下Redis的分布式锁
    Kafka学习之(七)搭建kafka可视化服务Kafka Eagle
    include与jsp:include区别
  • 原文地址:https://www.cnblogs.com/cocottt/p/5550970.html
Copyright © 2011-2022 走看看