萌新刚学cdq分治,求助TLE50分
查看原帖
萌新刚学cdq分治,求助TLE50分
59388
VinstaG173かえで楼主2020/6/21 18:02

RT,

#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<algorithm>
using std::sort;
int n,k,t;
struct flower
{
	int id,nm,a,b,c;
}fl[100003],tp[100003];
inline bool cmp1(flower x,flower y)
{
	if(x.a==y.a)
	{
		if(x.b==y.b)
		{
			return x.c<y.c;
		}
		return x.b<y.b;
	}
	return x.a<y.a;
}
inline bool cmp2(flower x,flower y)
{
	if(x.b==y.b)
	{
		return x.c<y.c;
	}
	return x.b<y.b;
}
int num[100003];
int lb[200003],s[200003];
inline void BIT_init()
{
	for(int i=1;i<=k;++i)lb[i]=i&(-i);
}
inline void BIT_clear()
{
	memset(s,0,sizeof(s));
}
inline void BIT_add(int x,int v)
{
	while(x<=k)
	{
		s[x]+=v;x+=lb[x];
	}
}
inline int BIT_sum(int x)
{
	int res=0;
	while(x)
	{
		res+=s[x];x-=lb[x];
	}
	return res;
}
void solve(int l,int r)
{
	if(r-l<=1)return;
	int m=(l+r)>>1;
	solve(l,m);solve(m,r);
	sort(fl+l,fl+m,cmp2);sort(fl+m,fl+r,cmp2);
	int p=l,q=m;
	BIT_clear();
	while(q<r&&p<m)
	{
		while(p<m&&fl[p].b<=fl[q].b)BIT_add(fl[p].c,fl[p].nm),++p;
		num[fl[q].id]+=BIT_sum(fl[q].c),++q;
	}
	while(q<r)num[fl[q].id]+=BIT_sum(fl[q].c),++q;
}
int cnt[100003];
int con;
int main()
{
	scanf(" %d %d",&n,&k);
	for(int i=0;i<n;++i)
	{
		scanf(" %d %d %d",&tp[i].a,&tp[i].b,&tp[i].c);
	}
	sort(tp,tp+n,cmp1);
	fl[0]=tp[0];fl[0].id=0;fl[0].nm=1;con=0;
	for(int i=1;i<n;++i)
	{
		if(tp[i].a==tp[i-1].a&&tp[i].b==tp[i-1].b&&tp[i].c==tp[i-1].c)++fl[con].nm;
		else fl[++con]=tp[i],fl[con].id=con,fl[con].nm=1;
	}
	t=n,n=con+1;
	BIT_init();
	solve(0,n);
	for(int i=0;i<n;++i)num[fl[i].id]+=fl[i].nm-1;
	for(int i=0;i<n;++i)cnt[num[fl[i].id]]+=fl[i].nm;
	for(int i=0;i<t;++i)printf("%d\n",cnt[i]);
	return 0;
}
2020/6/21 18:02
加载中...