题目链接:https://vjudge.net/problem/HYSBZ-3262
题意:三维偏序,有重复。
转自:https://zhuanlan.zhihu.com/p/55322598
思路:先用sort对a排序,然后用归并排序的思路对b排序的同时对c用树状数组统计贡献。
第一维是a,第二维是b,第三维是c。
#include <bits/stdc++.h>
using namespace std;
const int maxn=1e5+5;
int n,m,c[maxn*2],ans[maxn],cnt;
struct NODE
{
int a,b,c,w,f;//w为相同的数量 f为符合题意且不与本体相同的个数
} e[maxn],t[maxn];
bool cmp(NODE a,NODE b)
{
if(a.a!=b.a)
return a.a<b.a;
if(a.b!=b.b)
return a.b<b.b;
return a.c<b.c;
}
int lowbit(int x)
{
return x&(-x);
}
void update(int x,int y)
{
for(; x<=m; x+=lowbit(x))
c[x]+=y;
}
int sum(int x)
{
int ans=0;
for(; x; x-=lowbit(x))
ans+=c[x];
return ans;
}
void CDQ (int l,int r)
{
int mid=(l+r)/2;
if(l==r)
return ;
CDQ(l,mid);
CDQ(mid+1,r);
int p=l,q=mid+1,tot=l;
while(p<=mid&&q<=r)
{
if(e[p].b<=e[q].b)
{
update(e[p].c,e[p].w);
t[tot++]=e[p++];
}
else
{
e[q].f+=sum(e[q].c);
t[tot++]=e[q++];
}
}
while(p<=mid)
{
update(e[p].c,e[p].w);
t[tot++]=e[p++];
}
while(q<=r)
{
e[q].f+=sum(e[q].c);
t[tot++]=e[q++];
}
for(int i=l; i<=mid; i++)//清空树状数组 memset会tle
{
update(e[i].c,-e[i].w);
}
for(int i=l; i<=r; i++)
{
e[i]=t[i];
}
}
int main()
{
scanf("%d%d",&n,&m);
for(int i=1; i<=n; i++)
{
scanf("%d%d%d",&e[i].a,&e[i].b,&e[i].c);
e[i].w=1;
}
sort(e+1,e+1+n,cmp);
cnt=1;
for(int i=2; i<=n; i++)//处理重复
{
if(e[i].a==e[cnt].a&&e[i].b==e[cnt].b&&e[i].c==e[cnt].c)
{
e[cnt].w++;
}
else
{
e[++cnt]=e[i];
}
}
CDQ(1,cnt);
for(int i=1; i<=cnt; i++)//统计答案
{
ans[e[i].f+e[i].w-1]+=e[i].w;
}
for(int i=0; i<n; i++)
{
printf("%d\n",ans[i]);
}
return 0;
}