BZOJ3262: 陌上花开

题目:http://www.lydsy.com/JudgeOnline/problem.php?id=3262

三维。排序搞掉一维。让题目变成两维,然后用树状数组+cdq分治。

每次把平分的两个数组进行排序,然后扫一遍,由于排过序所以如果对于(l,mid)里的i对(mid+1,r)里的j有贡献的话,那它对j+1也是有贡献的。

可以先分治下去再处理当前的(l,r),这样可以少掉赋值的操作。记得最后要去掉(l,l1-1)的贡献。

然后就是对于完全相同的点可以先并起来。

#include<cstring>
#include<iostream>
#include<algorithm>
#include<cstdio>
#define rep(i,l,r) for (int i=l;i<=r;i++)
#define down(i,l,r) for (int i=l;i>=r;i--)
#define clr(x,y) memset(x,y,sizeof(x))
#define maxn 200500
using namespace std;
struct data{int x,y,z,ans,s;
}a[maxn],q[maxn];
int t[maxn],ans[maxn],n,m,N;
int read(){
    int x=0,f=1; char ch=getchar();
    while (!isdigit(ch)) {if (ch=='-') f=-1; ch=getchar();}
    while (isdigit(ch)) {x=x*10+ch-'0'; ch=getchar();}
    return x*f;
}
bool cmp(data a,data b){
    if (a.x==b.x&&a.y==b.y) return a.z<b.z;
    else if (a.x==b.x) return a.y<b.y;
    else return a.x<b.x; 
}
int low(int x){
    return (x&(-x));
}
void add(int x,int y){
    while (x<=m){
        t[x]+=y; x+=low(x);
    }
}
int ask(int x){
    int ans=0;
    while (x){
        ans+=t[x]; x-=low(x);
    }
    return ans;
}
bool cmp2(data a,data b){
    if (a.y==b.y) return a.z<b.z;
    else return a.y<b.y;
}
void cdq(int l,int r){
    if (l==r) return;
    int mid=(l+r)/2;
    cdq(l,mid); cdq(mid+1,r);
    sort(q+l,q+1+mid,cmp2); sort(q+1+mid,q+r+1,cmp2);
    int l1=l,l2=mid+1;
    while (l2<=r){
        while (l1<=mid&&q[l1].y<=q[l2].y){
            add(q[l1].z,q[l1].s);
            l1++;
        }
        q[l2].ans+=ask(q[l2].z);
        l2++;
    }
    rep(i,l,l1-1) add(q[i].z,-q[i].s);
}
int main(){
    N=read(); m=read();
    rep(i,1,N){
        a[i].x=read(); a[i].y=read(); a[i].z=read();
    }
    sort(a+1,a+1+N,cmp);
    int cnt=0;
    n=0;
    rep(i,1,N){
        cnt++;
        if (a[i].x!=a[i+1].x||a[i].y!=a[i+1].y||a[i].z!=a[i+1].z){
            q[++n]=a[i];
            q[n].s=cnt;
            cnt=0;
        }
    }
    cdq(1,n);
    rep(i,1,n) ans[q[i].ans+q[i].s-1]+=q[i].s;
    rep(i,0,N-1) printf("%d\n",ans[i]);
    return 0;
}

 

posted on 2015-12-14 21:34  ctlchild  阅读(594)  评论(0编辑  收藏  举报

导航