Hdu4742-Pinball Game 3D(cdq分治+树状数组)

Problem Description
RD is a smart boy and excel in pinball game. However, playing common 2D pinball game for a great number of times results in accumulating tedium. 
Recently, RD has found a new type of pinball game, a 3D pinball game. The 3D pinball game space can be regarded as a three dimensional coordinate system containing N balls. A ball can be considered as a point. At the beginning, RD made a shot and hit a ball. The ball hit by RD will move and may hit another ball and the “another ball” may move and hit another another ball, etc. But once a ball hit another ball, it will disappear.
RD is skilled in this kind of game, so he is able to control every ball's moving direction. But there is a limitation: if ball A's coordinate is (x1,y1,z1) and ball B's coordinate is (x2,y2,z2), then A can hit B only if x1 <= x2 and y1 <= y2 and z1 <= z2.
Now, you should help RD to calculate the maximum number of balls that can be hit and the number of different shooting schemes that can achieve that number. Two schemes are different if the sets of hit balls are not the same. The order doesn't matter.
 
Input
The first line contains one integer T indicating the number of cases. In each case, the first line contains one integer N indicating the number of balls.  The next N lines each contains three non-negative integer (x, y, z), indicating the coordinate of a ball.  The data satisfies T <= 3, N <= 105, 0 <= x, y, z <= 230, no two balls have the same coordinate in one case.
 
Output
Print two integers for each case in a line, indicating the maximum number of balls that can be hit and the number of different shooting schemes. As the number of schemes can be quite large, you should output this number mod 230.
 
Sample Input
2
3
2 0 0
0 1 0
0 1 1
5
3 0 0
0 1 0
0 0 1
0 2 2
3 3 3
 
Sample Output
2 1
3 2

题意: 求满足最大的上升序列长度和个数,对于一个三元组(x,y,z) 要求x1<=x2,y1<=y2,z1<=z2

解析: CDQ处理,先把z值离散化,再把所有的三元组按照(x,y,z)排好序,CDQ分治处理 CDQ思想:对于区间[l,r],先递归处理左半区间[l,mid](左边的信息已经更新好了), 再处理当前区间,用前mid个元素更新后半部分的值,一般是插入到某种数据结构中, 后半部分通过查询更新值,最后处理右半区间[mid+1,r],相当于从左到右处理完所有 的元素。具体实现见代码。

代码

#include<cstdio>
#include<cstring>
#include<string>
#include<algorithm>
using namespace std;
#define fi first
#define se second
typedef pair<int,int> par;
const int maxn=;
int N,ms,A[maxn];//A用于离散化z值
par dp[maxn],tree[maxn],zero(,); //dp的fi保存长度,se保存大小,tree用于树状数组
struct node
{
int x,y,z,id;
node(int x=,int y=,int z=):x(x),y(y),z(z){}
bool operator < (const node& t) const //依次排x,y,z
{
if(x!=t.x) return x<t.x;
if(y!=t.y) return y<t.y;
return z<t.z;
}
}nod[maxn],tnod[maxn]; void init()
{
scanf("%d",&N);
int x,y,z;
for(int i=;i<N;i++)
{
scanf("%d%d%d",&x,&y,&z);
nod[i]=node(x,y,z);
A[i]=z;
}
sort(nod,nod+N);
sort(A,A+N);
ms=;
for(int i=;i<N;i++) if(A[ms]!=A[i]) A[++ms]=A[i];
for(int i=;i<N;i++)
{
nod[i].z=lower_bound(A,A+ms+,nod[i].z)-A+;
nod[i].id=i;
}
}
int lowbit(int x){ return x&(-x); }
void Update(par& a,const par& b) //更新
{
if(a.fi<b.fi) a=b; //长度小
else if(a.fi==b.fi) a.se+=b.se; //加上这么多种情况
}
void Modify(int i,const par& b){ for(;i<=ms;i+=lowbit(i)) Update(tree[i],b); }
par Query(int i)
{
par ret=zero;
for(;i>;i-=lowbit(i)) Update(ret,tree[i]);
return ret;
}
void Recover(int i){ for(;i<=ms;i+=lowbit(i)) tree[i]=zero; }
void CDQ(int l,int r)
{
if(l==r) return;
if(l>r) return;
int mid=(l+r)/;
CDQ(l,mid);//先处理左边边
int k=;
for(int i=l;i<=r;i++) { tnod[k]=nod[i]; tnod[k++].x=; }
sort(tnod,tnod+k);
for(int i=;i<k;i++)
{
node& t=tnod[i];
if(t.id<=mid) Modify(t.z,dp[t.id]); //左边已经处理过,只需要插入即可
else // 更新右边
{
par a=Query(t.z);
a.fi++;
Update(dp[t.id],a);
}
}
for(int i=;i<k;i++) //一定要恢复,不然会影响后面的
{
node& t=tnod[i];
if(t.id<=mid) Recover(t.z);
}
CDQ(mid+,r); //再处理右边
}
int main()
{
int T;
scanf("%d",&T);
while(T--)
{
init();
for(int i=;i<N;i++) dp[i].fi=dp[i].se=;
CDQ(,N-);
par ans=zero;
for(int i=;i<N;i++) Update(ans,dp[i]);
printf("%d %d\n",ans.fi,ans.se);
}
return ;
}
上一篇:[BZOJ3456]城市规划:DP+NTT+多项式求逆


下一篇:Protecting the Flowers 牛客题解