【BZOJ3262】陌上花开

3262: 陌上花开

Time Limit: 20 Sec  Memory Limit: 256 MB
Submit: 2010  Solved: 898
[Submit][Status][Discuss]

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

HINT

1 <= N <= 100,000, 1 <= K <= 200,000

Source

树套树 CDQ分治

这个题有也许是三种排序方法吧

第一种全部升序,然后统计[l,mid]中比[mid+1,r]中小的 这样保证的是si<=sj,ci<=cj,mi<=mj

第二种全部降序,然后统计[l,mid]中比[mid+1,r]中大的 保证的是si>=sj,ci>=cj,mi>=mj

但是第三种 先全部升序再统计大的 我就不是很懂了

我写的是第一种

注意统计边界上 a[l]==a[r]这时ans+=a[l].cnt-1

以及注意当a[i]==a[j]时,这时答案肯定会出问题(因为你统计时将他拆开了) 所以我们合并相同的

/*To The End Of The Galaxy*/
#include<cstdio>
#include<cstdlib>
#include<iostream>
#include<cstring>
#include<algorithm>
#include<queue>
#include<iomanip>
#include<stack>
#include<map>
#include<time.h>
#include<set>
#include<cmath>
#define debug(x) cerr<<#x<<"="<<x<<endl
#define INF 0x7f7f7f7f
#define llINF 0x7fffffffffffll
#define P(x,y) (((x-1)*m)+y)
using namespace std;
typedef pair<int,int> pii;
typedef long long ll;
inline int init()
{
    int now=0,ju=1;char c;bool flag=false;
    while(1)
    {
        c=getchar();
        if(c=='-')ju=-1;
        else if(c>='0'&&c<='9')
        {
            now=now*10+c-'0';
            flag=true;
        }
        else if(flag)return now*ju;
    }
}
inline long long llinit()
{
    long long now=0,ju=1;char c;bool flag=false;
    while(1)
    {
        c=getchar();
        if(c=='-')ju=-1;
        else if(c>='0'&&c<='9')
        {
            now=now*10+c-'0';
            flag=true;
        }
        else if(flag)return now*ju;
    }
}
int n,k;
int s[200005];
#define lowbit(x) (x&(-x))
void modify(int pos,int v)
{
    for(int i=pos;i<=k;i+=lowbit(i))
    {
        s[i]+=v;
    }
}
int ask(int pos)
{
    int res=0;
    for(int i=pos;i>=1;i-=lowbit(i))
    {
        res+=s[i];
    }
    return res;
}
int tot=0;
struct node
{
    int s,c,m,cnt,ans;
}query[100005],tmpq[100005];
bool cmp(node a,node b)
{
    if(a.s!=b.s)return a.s<b.s;
    else if(a.c!=b.c)return a.c<b.c;
    else return a.m<b.m;
}
bool operator < (node a,node b)
{
    return a.c<b.c;
}
#define mid ((l+r)>>1)
void cdq(int l,int r)
{
    if(l==r)
    {
        query[l].ans+=query[l].cnt-1;
        return;
    }
    cdq(l,mid);
    cdq(mid+1,r);
    int i=l,j=mid+1,last=0;
    while(j<=r)
    {
        if(i<=mid&&query[i].c<=query[j].c)
        {
            modify(query[i].m,query[i].cnt);
            last=i;i++;
        }
        else
        {
            query[j].ans+=ask(query[j].m);
            j++;
        }
    }
    for(int i=l;i<=last;i++)
    {
        modify(query[i].m,-query[i].cnt);
    }
    merge(query+l,query+mid+1,query+mid+1,query+r+1,tmpq+l);
    for(int i=l;i<=r;i++)
    {
        query[i]=tmpq[i];
    }
}
int ans[200005];
int main()
{
    n=init();k=init();
    for(int i=1;i<=n;i++)
    {
        tmpq[i].s=init();tmpq[i].c=init();tmpq[i].m=init();tmpq[i].cnt=1;
    }
    sort(tmpq+1,tmpq+1+n,cmp);
    for(int i=1;i<=n;i++)
    {
        if(tmpq[i].s==query[tot].s&&tmpq[i].c==query[tot].c&&tmpq[i].m==query[tot].m)
        {
            query[tot].cnt++;
        }
        else
        {
            query[++tot]=tmpq[i];
        }
    }
    cdq(1,tot);
    for(int i=1;i<=tot;i++)
    {
        ans[query[i].ans]+=query[i].cnt;
    }
    for(int i=0;i<n;i++)
    {
        printf("%d
",ans[i]);
    }
    return 0;
}
View Code
原文地址:https://www.cnblogs.com/redwind/p/6526477.html