bzoj3262: 陌上花开

链接:http://www.lydsy.com/JudgeOnline/problem.php?id=3262

题意:中文题。

分析:cdq分治练习题,详细分析戳这里,不过这题没有好的时间戳z,因为这题的三个值都是在1~k范围内的,那就是说有的值不唯一有的不存在,那么我们在分治的时候就要注意边界了,所以我改成了4个参数的分治,详见代码。O(nlogk^2)。

代码:

#include<map>
#include<set>
#include<cmath>
#include<queue>
#include<bitset>
#include<math.h>
#include<cstdio>
#include<vector>
#include<string>
#include<cstring>
#include<iostream>
#include<algorithm>
#pragma comment(linker, "/STACK:102400000,102400000")
using namespace std;
const int N=100010;
const int MAX=1000000100;
const int mod=100000000;
const int MOD1=1000000007;
const int MOD2=1000000009;
const double EPS=0.00000001;
typedef long long ll;
const ll MOD=998244353;
const ll INF=10000000010;
typedef double db;
typedef unsigned long long ull;
struct node {
    int x,y,z,id;
}a[N],b[N];
int k,f[2*N],ans[N],p[N];
int cmp(node a,node b) {
    if (a.x!=b.x) return a.x<b.x;
    else if (a.y!=b.y) return a.y<b.y;
        else if (a.z!=b.z) return a.z<b.z;
            else return a.id<b.id;
}
void add(int a,int b) {
    for (;a<=k;a+=a&(-a)) f[a]+=b;
}
int getsum(int a) {
    int ret=0;
    for (;a;a-=a&(-a)) ret+=f[a];
    return ret;
}
void cdq(int l,int r,int L,int R) {
    if (l>=r) return ;
    int i,mid=(L+R)>>1,w=l,z;
    if (L==R) {
        for (i=l;i<=r;i++) { ans[a[i].id]+=getsum(a[i].y);add(a[i].y,1); }
        for (i=l;i<=r;i++) add(a[i].y,-1);
        return ;
    }
    for (i=l;i<=r;i++)
    if (a[i].z<=mid) add(a[i].y,1);
    else ans[a[i].id]+=getsum(a[i].y);
    for (i=l;i<=r;i++)
    if (a[i].z<=mid) add(a[i].y,-1);
    for (i=l;i<=r;i++)
    if (a[i].z<=mid) { b[w]=a[i];w++; }
    z=w-1;
    for (i=l;i<=r;i++)
    if (a[i].z>mid) { b[w]=a[i];w++; }
    for (i=l;i<=r;i++) a[i]=b[i];
    cdq(l,z,L,mid);cdq(z+1,r,mid+1,R);
}
int main()
{
    int i,n;
    scanf("%d%d", &n, &k);
    for (i=1;i<=n;i++) {
        scanf("%d%d%d", &a[i].x, &a[i].y, &a[i].z);a[i].id=i;
    }
    sort(a+1,a+n+1,cmp);
    a[n+1].x=a[n+1].y=a[n+1].z=0;
    memset(f,0,sizeof(f));
    memset(ans,0,sizeof(ans));
    for (i=n;i;i--)
    if (a[i].x==a[i+1].x&&a[i].y==a[i+1].y&&a[i].z==a[i+1].z) {
        f[i]=f[i+1]+1;ans[a[i].id]+=f[i];
    }
    memset(f,0,sizeof(f));
    cdq(1,n,1,k);
    memset(p,0,sizeof(p));
    for (i=1;i<=n;i++) p[ans[i]]++;
    for (i=0;i<n;i++) printf("%d\n", p[i]);
    return 0;
}


你可能感兴趣的:(cdq分治)