Jam's problem again
Time Limit: 5000/2500 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 1563 Accepted Submission(s): 567
Problem Description
Jam like to solve the problem which on the 3D-axis,given
N(1≤N≤100000) points
(x,y,z)(1≤x,y,z≤100000)
If two point such as (xi,yi,zi) and (xj,yj,zj) xi≥xj yi≥yj zi≥zj, the bigger one level add 1
Ask for the each level of the point.
If two point such as (xi,yi,zi) and (xj,yj,zj) xi≥xj yi≥yj zi≥zj, the bigger one level add 1
Ask for the each level of the point.
Input
The first line is
T(1≤T≤15) means
T Case
For each case
The first line is N means the number of Point and next there are N line, each line has (x,y,z)
For each case
The first line is N means the number of Point and next there are N line, each line has (x,y,z)
Output
Output with N line,each line has one number means the lever of point
Sample Input
1410 4 710 6 68 2 57 3 10
Sample Output
1
1
0
0
题意:
就是给你n个点,问你每个点(x0,y0,z0)对应有多少个点(x,y,z)满足x<x0,y<y0,z<z0
解析:
这道题是三维的,按照CDQ分治,我们首先要让一维有序,在这里我们让按照x有序,插入到数组里面。
再在两个区间合并[l,M),[M,R)时,我们先让左右区间合起来一起按照y排序,这样扫一遍,如果搜到一个点原来是属于左区间[L,M),那么就按照他的z对树状数组进行单点修改(为右区间查询提供依据),如果是右区间[M,R),就进行查询,看在树状数组中有多少比当前z小的数(就是[1,z)的累和)(因为此时树状数里只有左区间的值,而这些点的x,都比右区间小,y都比这个点小)。这样就满足CDQ分治中,左区间对右区间的影响,左区间值做修改,右区间只查询
#include <cstdio>
#include <algorithm>
using namespace std;
const int MAXN = 1e5+100;
typedef struct Pointer
{
int x,y,z;
int loc;
int mla;
}point;
bool cmp(point a,point b)
{
if(a.x==b.x)
{
if(a.y==b.y)
return a.z<b.z;
else
return a.y<b.y;
}
else
return a.x<b.x;
}
bool cmpx(point a,point b)
{
if(a.y==b.y) return a.z<b.z;
return a.y<b.y;
}
point node[MAXN];
int tra[MAXN],n;
int maxz;
int res[MAXN];
inline int lowbit(int x)
{
return x&(-x);
}
void add(int x,int inc)
{
for(int i=x;i<=maxz;i+=lowbit(i))
{
tra[i]+=inc;
}
}
int que(int x)
{
int sum=0;
for(int i=x;i>0;i-=lowbit(i))
{
sum+=tra[i];
}
return sum;
}
void CDQ(int L,int R)
{
if(R-L<=1) return;
int M=(L+R)>>1;
CDQ(L,M);
CDQ(M,R);
for(int i=L;i<M;i++) node[i].loc=0;
for(int i=M;i<R;i++) node[i].loc=1;
sort(node+L,node+R,cmpx);
for(int i=L;i<R;i++)
{
if(node[i].loc==0)
{
add(node[i].z,1);
}
else if(node[i].loc==1)
{
res[node[i].mla]+=que(node[i].z);
}
}
for(int i=L;i<R;i++)
{
if(node[i].loc==0)
{
add(node[i].z,-1);
}
}
}
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
scanf("%d",&n);
maxz=-1;
for(int i=0;i<n;i++)
{
scanf("%d%d%d",&node[i].x,&node[i].y,&node[i].z);
node[i].loc=-1;
node[i].mla=i;
res[i]=0;
maxz=max(node[i].z,maxz);
}
sort(node,node+n,cmp);
int cnt=0;
for(int i=n-2;i>=0;i--) //以有序为前提去重
{
if(node[i].x==node[i+1].x&&node[i].y==node[i+1].y&&node[i].z==node[i+1].z) cnt++;
else cnt=0;
res[node[i].mla]+=cnt;
}
CDQ(0,n); //左闭右开
for(int i=0;i<n;i++)
{
printf("%d\n",res[i]);
}
}
return 0;
}