三维偏序问题:
例题:陌上花开(点击这里)
我们先对第一维进行排序来对
a
a
a 降维,再在第二维用归并排序来对
b
b
b 实现降维,再在归并排序中套一个树状数组来计算
c
c
c 的贡献,即可求出答案(cdq分治的思想)
其复杂度为
O
(
n
l
o
g
2
n
)
O(nlog^2n)
O(nlog2n)
代码如下:
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cstring>
#include<algorithm>
#define ll long long
#define inf 0x3f3f3f3f
using namespace std;
const int maxn = 1e5+5;
struct node{
int a,b,c,w,f;//{a,b,c}三元组有w个 其贡献为f
bool operator < (node const x) const{
if(a != x.a) return a < x.a;
if(b != x.b) return b < x.b;
return c < x.c;
}
}nod[maxn],tmp[maxn];
inline int read()
{
register int res = 0,flag = 1;
char ch = getchar();
while(ch<'0' || ch>'9')
{
if(ch == '-') flag = -1;
ch = getchar();
}
while(ch>='0' && ch<='9')
{
res = (res<<3)+(res<<1)+(ch^48);
ch = getchar();
}
return flag*res;
}
int n,m,c[maxn<<1],ans[maxn],cnt;
int lowbit(int u)
{
return u&(-u);
}
void add(int u,int val)
{
while(u <= m)
{
c[u] += val;
u += lowbit(u);
}
}
int sum(int u)
{
int res = 0;
while(u)
{
res += c[u];
u -= lowbit(u);
}
return res;
}
void cdq(int l,int r)
{
int mid = (l+r)>>1;
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(nod[p].b <= nod[q].b)
{
add(nod[p].c,nod[p].w);
tmp[tot++] = nod[p++];
}
else {
nod[q].f += sum(nod[q].c);
tmp[tot++] = nod[q++];
}
}
while(p <= mid)
{
add(nod[p].c,nod[p].w);
tmp[tot++] =nod[p++];
}
while(q <= r)
{
nod[q].f += sum(nod[q].c);
tmp[tot++] = nod[q++];
}
for(int i = l;i <= mid;i++)//清空树状数组
add(nod[i].c,-nod[i].w);
for(int i = l;i <= r;i++)
nod[i] = tmp[i];
}
int main()
{
n = read(),m = read();
for(int i = 1;i <= n;i++)
{
nod[i].a = read();
nod[i].b = read();
nod[i].c = read();
nod[i].w = 1;
}
sort(nod+1,nod+n+1);
cnt = 1;
for(int i = 2;i <= n;i++)
{
if(nod[i].a==nod[cnt].a && nod[i].b==nod[cnt].b && nod[i].c==nod[cnt].c)
nod[cnt].w++;
else nod[++cnt] = nod[i];
}
cdq(1,cnt);
for(int i = 1;i <= cnt;i++)
ans[nod[i].f+nod[i].w-1] += nod[i].w;
for(int i = 0;i < n;i++)
printf("%d\n",ans[i]);
return 0;
}