BZOJ3262 : 陌上花开

先去重,然后一维排序,二维分治,三维树状数组维护,$O(n\log^2n)$

 

#include<cstdio>

#include<algorithm>

#define N 100010

using std::sort;

int n,m,k,i,j,t1,t2,b[N],c[N],fin[N],bit[N<<1],pos[N<<1],T;

struct P{int x,y,z,v,t;}a[N];

inline void read(int&a){char c;while(!(((c=getchar())>='0')&&(c<='9')));a=c-'0';while(((c=getchar())>='0')&&(c<='9'))(a*=10)+=c-'0';}

inline bool cmpx(P a,P b){return a.x==b.x&&a.y==b.y?a.z<b.z:(a.x==b.x?a.y<b.y:a.x<b.x);}

inline bool cmpy(int x,int y){return a[x].y==a[y].y?a[x].z<a[y].z:a[x].y<a[y].y;}

inline void add(int x,int y){for(;x<=k;x+=x&-x)if(pos[x]<T)pos[x]=T,bit[x]=y;else bit[x]+=y;}

inline int sum(int x){int t=0;for(;x;x-=x&-x)if(pos[x]==T)t+=bit[x];return t;}

void solve(int l,int r){

  if(l==r)return;

  int mid=(l+r)>>1;

  for(t1=0,i=l;i<=mid;i++)b[++t1]=i;

  for(t2=0,i=r;i>mid;i--)c[++t2]=i;

  sort(b+1,b+t1+1,cmpy),sort(c+1,c+t2+1,cmpy);

  for(T++,i=j=1;i<=t2;a[c[i]].t+=sum(a[c[i]].z),i++)while(j<=t1&&a[b[j]].y<=a[c[i]].y)add(a[b[j]].z,a[b[j]].v),j++;

  solve(l,mid),solve(mid+1,r);

}

int main(){

  read(n),read(k);

  for(i=1;i<=n;i++)read(a[i].x),read(a[i].y),read(a[i].z);

  sort(a+1,a+n+1,cmpx);

  for(i=1;i<=n;a[m].v++,i++)if(a[i].x!=a[m].x||a[i].y!=a[m].y||a[i].z!=a[m].z)a[++m]=a[i];

  solve(1,m);

  for(i=1;i<=m;i++)fin[a[i].t+a[i].v-1]+=a[i].v;

  for(i=0;i<n;i++)printf("%d\n",fin[i]);

  return 0;

}

  

 

你可能感兴趣的:(ZOJ)