bzoj3262 陌上花开【解法二】

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的每级花的数量。

解法一CDQ分治见【这里】
树套树,按 s 排序扫描,按c维护树状数组,每个位置存储以 m <script type="math/tex" id="MathJax-Element-45">m</script>为关键字的treap。

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cstdlib>
#include<ctime>
using namespace std;
int rd()
{
    int x=0;
    char c=getchar();
    while (c<'0'||c>'9') c=getchar();
    while (c>='0'&&c<='9')
    {
        x=x*10+c-'0';
        c=getchar();
    }
    return x;
}
struct str
{
    int x,y,z,ans,cnt;
    void read()
    {
        x=rd();
        y=rd();
        z=rd();
        cnt=1;
    }
    bool operator == (const str &ss) const
    {
        return x==ss.x&&y==ss.y&&z==ss.z;
    }
    bool operator < (const str &ss) const
    {
        if (x!=ss.x) return x<ss.x;
        if (y!=ss.y) return y<ss.y;
        return z<ss.z;
    }
}a[100010],b[100010];
int root[200010],son[4000010][2],w[4000010],v[4000010],size[4000010],num[4000010],ans[100010],
n,k,clo;
int rank(int p,int x)
{
    if (!p) return 0;
    if (x==v[p]) return size[son[p][0]]+num[p];
    if (x<v[p]) return rank(son[p][0],x);
    return size[son[p][0]]+num[p]+rank(son[p][1],x);
}
int qry(int p,int x)
{
    int ret=0;
    for (;p;p-=p&-p) ret+=rank(root[p],x);
    return ret;
}
void up(int p)
{
    size[p]=num[p]+size[son[p][0]]+size[son[p][1]];
}
void rot(int &p,int x)
{
    int u=son[p][x],v=son[u][x^1];
    son[p][x]=v;
    son[u][x^1]=p;
    up(p);
    up(u);
    p=u;
}
void ins(int &p,int x,int y)
{
    if (!p)
    {
        p=++clo;
        size[p]=num[p]=y;
        v[p]=x;
        w[p]=rand();
        return;
    }
    size[p]+=y;
    if (x==v[p])
    {
        num[p]+=y;
        return;
    }
    int z=x>v[p];
    ins(son[p][z],x,y);
    if (w[son[p][z]]>w[p]) rot(p,z);
}
void add(int p,int x,int y)
{
    for (;p<=k;p+=p&-p) ins(root[p],x,y);
}
int main()
{
    srand(123);
    int nn,x;
    nn=rd();
    k=rd();
    for (int i=1;i<=nn;i++) b[i].read();
    sort(b+1,b+nn+1);
    for (int i=1;i<=nn;i++)
        if (!(b[i]==b[i-1])) a[++n]=b[i];
        else a[n].cnt++;
    for (int i=1;i<=n;i++)
    {
        x=qry(a[i].y,a[i].z);
        ans[x+a[i].cnt-1]+=a[i].cnt;
        add(a[i].y,a[i].z,a[i].cnt);
    }
    for (int i=0;i<nn;i++) printf("%d\n",ans[i]);
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值