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=100005;
int N,ms,A[maxn];//A用于离散化z值
par dp[maxn],tree[maxn],zero(0,0); //dp的fi保存长度,se保存大小,tree用于树状数组
struct node
{
    int x,y,z,id;
    node(int x=0,int y=0,int z=0):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=0;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=0;
    for(int i=1;i<N;i++) if(A[ms]!=A[i]) A[++ms]=A[i];
    for(int i=0;i<N;i++)
    {
        nod[i].z=lower_bound(A,A+ms+1,nod[i].z)-A+1;
        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>0;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)/2;
    CDQ(l,mid);//先处理左边边
    int k=0;
    for(int i=l;i<=r;i++) { tnod[k]=nod[i]; tnod[k++].x=0; }
    sort(tnod,tnod+k);
    for(int i=0;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=0;i<k;i++) //一定要恢复,不然会影响后面的
    {
        node& t=tnod[i];
        if(t.id<=mid) Recover(t.z);
    }
    CDQ(mid+1,r); //再处理右边
}
int main()
{
    int T;
    scanf("%d",&T);
    while(T--)
    {
        init();
        for(int i=0;i<N;i++) dp[i].fi=dp[i].se=1;
        CDQ(0,N-1);
        par ans=zero;
        for(int i=0;i<N;i++) Update(ans,dp[i]);
        printf("%d %d\n",ans.fi,ans.se);
    }
    return 0;
}

时间: 2024-08-10 23:27:38

Hdu4742-Pinball Game 3D(cdq分治+树状数组)的相关文章

HDU 5618:Jam&#39;s problem again(CDQ分治+树状数组处理三维偏序)

http://acm.hdu.edu.cn/showproblem.php?pid=5618 题意:-- 思路:和NEUOJ那题一样的.重新写了遍理解了一下,算作处理三维偏序的模板了. 1 #include <cstdio> 2 #include <algorithm> 3 #include <iostream> 4 #include <cstring> 5 using namespace std; 6 #define INF 0x3f3f3f3f 7 #d

BZOJ 2683 简单题 cdq分治+树状数组

题意:链接 **方法:**cdq分治+树状数组 解析: 首先对于这道题,看了范围之后,二维的数据结构是显然不能过的,于是我们可能会考虑把一维排序之后另一位上数据结构什么的,然而cdq分治却能够很好的体现它的作用. 首先,对于每一个询问求和,显然是x在它左边的并且出现时间在它之前的所有的change对他可能会有影响. 我们按照x第一关键字,y第二关键字,操作第三关键字来排序所有的询问,然后在cdq的时候,每次递归处理左半区间,按照x动态的将y这一列的值加到树状数组里,来更新右半边的所有询问,注意这

XJOI NOIP2015模拟赛Day1 T2 ctps bitset优化 或 排序+cdq分治+树状数组+平衡树

题意: 4维空间中有1个点集A,|A|=n,用(a,b,c,d)表示每个点. 共有m个询问,每次询问输入一个点(a,b,c,d),求最大的S,其中S={p|p∈A且ap<=a,bp<=b,cp<=c,dp<=d},输出|S| 输入格式: 第一行n 接下来n行有n个4维点对 第n+2行有一个数m 再接下来m行每行有一个四维点对,表示每个询问 输出格式: 对于每个询问输出一个数 **方法:**bitset优化 或 排序+cdq分治+树状数组+平衡树 解析: 神题,考场不会,暴力骗40,

【BZOJ4553】[Tjoi2016&amp;Heoi2016]序列 cdq分治+树状数组

[BZOJ4553][Tjoi2016&Heoi2016]序列 Description 佳媛姐姐过生日的时候,她的小伙伴从某宝上买了一个有趣的玩具送给他.玩具上有一个数列,数列中某些项的值可能会变化,但同一个时刻最多只有一个值发生变化.现在佳媛姐姐已经研究出了所有变化的可能性,她想请教你,能否选出一个子序列,使得在任意一种变化中,这个子序列都是不降的?请你告诉她这个子序列的最长长度即可.注意:每种变化最多只有一个值发生变化.在样例输入1中,所有的变化是: 1 2 3 2 2 3 1 3 3 1

BZOJ2683: 简单题(CDQ分治 + 树状数组)

BZOJ2683: 简单题(CDQ分治 + 树状数组) 题意: 你有一个\(N*N\)的棋盘,每个格子内有一个整数,初始时的时候全部为\(0\),现在需要维护两种操作: 命令 参数限制 内容 \(1\ x\ y\ A\) \(1\le x,y \le N\),A是正整数 将格子\(x,y\)里的数字加上\(A\) \(2\ x1\ y1\ x2\ y2\) \(1\le x1\le x2\le N,1\le y1\le y2\le N\) 输出\(x1\ y1\ x2\ y2\)这个矩形内的数字

[cdq分治][树状数组] Bzoj P3262 陌上花开

Description 有n朵花,每朵花有三个属性:花形(s).颜色(c).气味(m),用三个整数表示. 现在要对每朵花评级,一朵花的级别是它拥有的美丽能超过的花的数量. 定义一朵花A比另一朵花B要美丽,当且仅Sa>=Sb,Ca>=Cb,Ma>=Mb. 显然,两朵花可能有同样的属性.需要统计出评出每个等级的花的数量. Input 第一行为N,K (1 <= N <= 100,000, 1 <= K <= 200,000 ), 分别表示花的数量和最大属性值. 以下N

BZOJ1176---[Balkan2007]Mokia (CDQ分治 + 树状数组)

题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=1176 CDQ第一题,warush了好久.. CDQ分治推荐论文: 1 <从<Cash>谈一类分治算法的应用> 陈丹琦 2 <浅谈数据结构题的几个非经典解法>  许昊然 关于CDQ分治,两种要求:①操作不相互影响  ②可以离线处理 题目描述是有问题的,,初始时 全部为0,不是s 题意:二维平面内,两种操作,1 x y v ,位于(x,y)的值加上v...2 x1,

bzoj2716 [ Violet 3 ] --cdq分治+树状数组

树状数组打错调了一个小时... 对于点(x,y),其它点只会在他的左下.右下.左上.右上四个方向上.我们只需求在左下方向上就可以了,因为其他方向可以通过改变相对位置求得. 考虑cdq分治.先按x坐标排序,然后将区间[l,r]分为[l,mid],[mid+1,r],因为只求左下方向上的点,所以可以去掉绝对值:dis=x+y-(x'+y') 只需求x'+y'最大的点就可以了.求(X,Y)时将[l,mid]中x值小于X的点的x+y值在树状数组中更新,然后查询y小于Y的最大的x+y,更新答案. 代码:

HDU 5126 stars cdq分治+树状数组

题目链接:点击打开链接 题意: T个case n个操作 1. (x,y,z) 在三维平面的点上增加1 2.询问区间范围内的权值和. 思路: cdq分治套cdq分治,然后套树状数组即可.. #include <stdio.h> #include <iostream> #include <algorithm> #include <sstream> #include <stdlib.h> #include <string.h> #inclu