P3810 【模板】三维偏序（陌上花开）

2018-05-16 19:25:53

#include<cstdio>
#include<cstring>
#include<cctype>
#include<algorithm>
#define reg register
using namespace std;
const int N=1e5+5;
struct flower
{
int a,b,c,id,siz;
friend bool operator < (flower x,flower y)
{
if (x.b!=y.b) return x.b<y.b;
if (x.c!=y.c) return x.c<y.c;
return x.id<y.id;
}
}s[N],t[N<<1],tmp[N<<1];
int n,m,top,belong[N],c[N<<1],f[N<<1],ans[N];
inline int read()
{
int x=0,w=1;
char c=getchar();
while (!isdigit(c)&&c!='-') c=getchar();
if (c=='-') c=getchar(),w=-1;
while (isdigit(c))
{
x=(x<<1)+(x<<3)+c-'0';
c=getchar();
}
return x*w;
}
inline int lowbit(int x)
{
return x&(-x);
}
inline void add(int x,int k)
{
while (x<=m)
{
c[x]+=k; x+=lowbit(x);
}
}
inline void cover(int x,int k)
{
while (x<=m)
{
c[x]=k; x+=lowbit(x);
}
}
inline int sum(int x)
{
int res=0;
while (x)
{
res+=c[x]; x-=lowbit(x);
}
return res;
}
void CDQ(int l,int r)
{
if (l==r) return;
int mid=(l+r)>>1;
CDQ(l,mid); CDQ(mid+1,r);
int p=l,q=mid+1,now=l-1;
while (p<=mid&&q<=r)
if (t[p]<t[q])
add(t[p].c,t[p].siz),tmp[++now]=t[p++];
else f[t[q].id]+=sum(t[q].c),tmp[++now]=t[q++];
while (p<=mid) tmp[++now]=t[p++];
while (q<=r) f[t[q].id]+=sum(t[q].c),tmp[++now]=t[q++];
for (reg int i=l;i<=r;i++)
cover(tmp[i].c,0),t[i]=tmp[i];
}
inline bool check(int i)
{
if (!top) return 0;
return (s[i].a==t[top].a&&s[i].b==t[top].b&&s[i].c==t[top].c);
}
bool cmp(flower x,flower y)
{
if (x.a!=y.a) return x.a<y.a;
if (x.b!=y.b) return x.b<y.b;
if (x.c!=y.c) return x.c<y.c;
return x.id<y.id;
}
int main()
{
n=read(),m=read();
for (reg int i=1;i<=n;i++)
{
s[i].a=read(),s[i].b=read(),s[i].c=read();
s[i].siz=1,s[i].id=i;
}
sort(s+1,s+n+1,cmp);
for (reg int i=1;i<=n;i++)
{
if (!check(i))
t[++top]=s[i],t[top].id=top;
else ++t[top].siz;
belong[i]=top;
}
CDQ(1,top);
for (reg int i=1;i<=top;i++) f[t[i].id]+=t[i].siz-1;
for (reg int i=1;i<=n;i++) ++ans[f[belong[i]]];
for (reg int i=0;i<n;i++) printf("%d\n",ans[i]);
return 0;
}
• star
首页