Hdu4742-Pinball Game 3D(cdq分治+树状数组)
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.
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
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 ;
}
Hdu4742-Pinball Game 3D(cdq分治+树状数组)的更多相关文章
- HDU 4247 Pinball Game 3D(cdq 分治+树状数组+动态规划)
Pinball Game 3D Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) ...
- hdu_4742_Pinball Game 3D(cdq分治+树状数组)
题目链接:hdu_4742_Pinball Game 3D 题意: 给你n个点,让你求三维的LIS,并且求出有多少种组合能达到LIS. 题解: 求三维的LIS,典型的三维偏序问题,x排序,解决一维,c ...
- 【BZOJ4553】[Tjoi2016&Heoi2016]序列 cdq分治+树状数组
[BZOJ4553][Tjoi2016&Heoi2016]序列 Description 佳媛姐姐过生日的时候,她的小伙伴从某宝上买了一个有趣的玩具送给他.玩具上有一个数列,数列中某些项的值可能 ...
- BZOJ 1176 Mokia CDQ分治+树状数组
1176: [Balkan2007]Mokia Time Limit: 30 Sec Memory Limit: 162 MBSubmit: 1854 Solved: 821[Submit][St ...
- 【bzoj3262】陌上花开 CDQ分治+树状数组
题目描述 有n朵花,每朵花有三个属性:花形(s).颜色(c).气味(m),又三个整数表示.现要对每朵花评级,一朵花的级别是它拥有的美丽能超过的花的数量.定义一朵花A比另一朵花B要美丽,当且仅当Sa&g ...
- 【bzoj2225】[Spoj 2371]Another Longest Increasing CDQ分治+树状数组
题目描述 给定N个数对(xi, yi),求最长上升子序列的长度.上升序列定义为{(xi, yi)}满足对i<j有xi<xj且yi<yj. 样例输入 8 1 3 3 2 1 1 4 5 ...
- BZOJ 2683 简单题 cdq分治+树状数组
题意:链接 **方法:**cdq分治+树状数组 解析: 首先对于这道题,看了范围之后.二维的数据结构是显然不能过的.于是我们可能会考虑把一维排序之后还有一位上数据结构什么的,然而cdq分治却可以非常好 ...
- LOJ3146 APIO2019路灯(cdq分治+树状数组)
每个时刻都形成若干段满足段内任意两点可达.将其视为若干正方形.则查询相当于求历史上某点被正方形包含的时刻数量.并且注意到每个时刻只有O(1)个正方形出现或消失,那么求出每个矩形的出现时间和消失时间,就 ...
- BZOJ 4553 [Tjoi2016&Heoi2016]序列 ——CDQ分治 树状数组
考虑答案的构成,发现是一个有限制条件的偏序问题. 然后三个维度的DP,可以排序.CDQ.树状数组各解决一维. #include <map> #include <cmath> # ...
随机推荐
- 专题笔记--Java 类集框架
Java 类集框架 1. Java类集框架产生的原因 在基础的应用中,我们可以通过数组来保存一组对象或者基本数据,但数组的大小是不可更改的,因此出于灵活性的考虑和对空间价值的担忧,我们可以使用链表来实 ...
- Class的生命周期
之前的<JVM类载入机制-ClassLoader>和<初探JVM-ClassLoader源代码>,仅仅是讨论了Class的载入部分,如今来纵观一下整个Class的生命周期. C ...
- CentOS CVS安装使用
CentOS CVS安装使用 一.CVS简介 CVS(Concurrent Versions System)版本控制系统:是一种GNU软件包,CVS是一个C/S系统,主要用于在多人开发环境下的源码 ...
- Fantageek翻译系列之《使用Autolayout显示变化高度的UITableViewCell》
这篇博客主要在于,解释如何通过仅仅使用Autolayout很很少的代码,显示高度不同的Cell.虽然标题说的是TableView,但是CollectionView同样适合.但是,这种方法只使用iOS7 ...
- EClipse开发NDK流程
EClipse开发NDK流程(现在studio也在2.2之后支持了非常简单,只要创建项目的时候勾选c++支持就可以了) 什么情况下使用ndk,1.保护代码,java很容易反编译,c/c++反汇编比 ...
- 《第一行代码》学习笔记29-内容提供器Content Provider(2)
1.查询操作: if (cursor != null) { while (cusor.moveToNext()) { String column1 = cursor.getString(cursor. ...
- jedis处理redis cluster集群的密码问题
环境介绍:jedis:2.8.0 redis版本:3.2 首先说一下redis集群的方式,一种是cluster的 一种是sentinel的,cluster的是redis 3.0之后出来新的集群方式 本 ...
- sql 创建临时表
declare @channelid varchar(100) set @channelid='''WH00026'',''WH00083''' declare @sql varchar(1000) ...
- hdu1215 正整数唯一分解定理应用
B - (例题)因子和 Crawling in process... Crawling failed Time Limit:1000MS Memory Limit:32768KB 64 ...
- 使用python发邮件
使用python发邮件 网上有很多发邮件的例子,本人在网上找了一份,稍加修改后使用 上源码 # encoding=utf-8 from email.mime.image import MIMEImag ...