HDU4742----Pinball Game 3D(三维LIS、CDQ分治)

题意:三维空间内 n个小球,对应坐标(x,y,z)。输出LIS的长度以及方案数。

首先可以先按x排序,先降低一维,然后 剩下y 、z,在y上进行CDQ分治,按y的大小用前面的更新后面的。z方向离散化之后用树状数组维护就可以了。

  1 #include <set>
  2 #include <map>
  3 #include <cmath>
  4 #include <ctime>
  5 #include <queue>
  6 #include <stack>
  7 #include <cstdio>
  8 #include <string>
  9 #include <vector>
 10 #include <cstdlib>
 11 #include <cstring>
 12 #include <iostream>
 13 #include <algorithm>
 14 using namespace std;
 15 typedef unsigned long long ull;
 16 typedef long long ll;
 17 const int inf = 0x3f3f3f3f;
 18 const double eps = 1e-8;
 19 const int maxn = 1e5+10;
 20 const int mod = 1 << 30;
 21 struct Ball
 22 {
 23     int x,y,z,idx;
 24     bool operator < (const Ball &rhs)const
 25     {
 26         return x < rhs.x || (x == rhs.x && y < rhs.y) || (x == rhs.x && y == rhs.y && z < rhs.z);
 27     }
 28 } ball[maxn],tmpball[maxn];
 29 struct DP
 30 {
 31     int len,cnt;
 32     DP(){}
 33     DP(int _len,int _cnt):
 34         len(_len),cnt(_cnt) {}
 35 } dp[maxn],c[maxn];
 36 int vec[maxn],idx;
 37 inline int lowbit (int x)
 38 {
 39     return x & -x;
 40 }
 41 inline void update (DP &dp1, DP &dp2)
 42 {
 43     if (dp1.len < dp2.len)
 44         dp1 = dp2;
 45     else if (dp1.len == dp2.len)
 46         dp1.cnt += dp2.cnt;
 47 }
 48 inline void modify(int x,DP &d)
 49 {
 50     while (x <= idx)
 51     {
 52         update(c[x],d);
 53         x += lowbit(x);
 54     }
 55 }
 56 DP query(int x)
 57 {
 58     DP ans = DP (0,0);
 59     while (x)
 60     {
 61         update(ans,c[x]);
 62         x -= lowbit(x);
 63     }
 64     return ans;
 65 }
 66 inline void CLR(int x)
 67 {
 68     while (x <= idx)
 69     {
 70         c[x] = DP(0,0);
 71         x += lowbit(x);
 72     }
 73 }
 74 void CDQ (int l, int r)
 75 {
 76     if (l == r)
 77         return ;
 78     int mid = (l + r) >> 1;
 79     CDQ (l, mid);
 80     for (int i = l; i <= r; i++)
 81     {
 82         tmpball[i] = ball[i];
 83         tmpball[i].x = 0;
 84     }
 85    sort(tmpball+l,tmpball+r+1);
 86     for (int i = l; i <= r; i++)
 87     {
 88         if (tmpball[i].idx <= mid)
 89         {
 90             modify(tmpball[i].z,dp[tmpball[i].idx]);
 91         }
 92         else
 93         {
 94             DP tmp = query(tmpball[i].z);
 95             tmp.len++;
 96             update(dp[tmpball[i].idx],tmp);
 97         }
 98     }
 99     for (int i = l; i <= r; i++)
100     {
101         if (tmpball[i].idx <= mid)
102         {
103             CLR(tmpball[i].z);
104         }
105     }
106     CDQ (mid+1, r);
107 }
108 int main(void)
109 {
110 #ifndef ONLINE_JUDGE
111     freopen("in.txt","r",stdin);
112 #endif
113     int T, n;
114     scanf ("%d",&T);
115     while (T--)
116     {
117         scanf ("%d",&n);
118         for (int i = 1; i <= n; i++)
119         {
120             scanf ("%d%d%d",&ball[i].x, &ball[i].y, &ball[i].z);
121             vec[i-1] = ball[i].z;
122         }
123         sort (ball+1, ball+n+1);
124         sort (vec,vec+n);
125         idx = unique(vec,vec+n) - vec;
126         for (int i = 1; i <= n ; i++)
127         {
128             ball[i].z = lower_bound(vec,vec+idx,ball[i].z) - vec + 1;
129             ball[i].idx = i;
130             dp[i] = DP(1,1);
131         }
132         CDQ(1,n);
133         DP ans = DP(0,0);
134         for (int i = 1; i <= n ;i++)
135         {
136             update(ans,dp[i]);
137         }
138         printf("%d %d\n",ans.len, ans.cnt % mod);
139     }
140     return 0;
141 }
时间: 2024-11-06 07:13:15

HDU4742----Pinball Game 3D(三维LIS、CDQ分治)的相关文章

hdu 4742 Pinball Game 3D(三维LIS&amp;cdq分治&amp;BIT维护最值)

Pinball Game 3D Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 688    Accepted Submission(s): 276 Problem Description RD is a smart boy and excel in pinball game. However, playing common 2D p

hdu 4742 Pinball Game 3D(三维LIS&amp;amp;cdq分治&amp;amp;BIT维护最值)

Pinball Game 3D Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 688    Accepted Submission(s): 276 Problem Description RD is a smart boy and excel in pinball game. However, playing common 2D p

BZOJ 3295:[Cqoi2011]动态逆序对(三维偏序 CDQ分治+树状数组)

http://www.lydsy.com/JudgeOnline/problem.php?id=3295 题意:简单明了. 思路:终于好像有点明白CDQ分治处理三维偏序了.把删除操作看作是插入操作,那么可以按照插入的时间顺序看作是一维x,插入的数在原本序列的下标是一维y,插入的数本身是一维z.那么问题可以转化成每插入一个数(xx,yy,zz),求有多少个数(x,y,z)使得 x < xx,y < yy,z > zz .一开始先对 x 进行排序,然后进行CDQ分治.这样可以干掉一维,保证随

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

BZOJ 3262: 陌上花开 [CDQ分治 三维偏序]

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行,每

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

SPOJ LIS2 Another Longest Increasing Subsequence Problem 三维偏序最长链 CDQ分治

Another Longest Increasing Subsequence Problem Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hust.edu.cn/vjudge/problem/visitOriginUrl.action?id=19929 Description Given a sequence of N pairs of integers, find the length of the longest incre

【CDQ】 HDU 4742 Pinball Game 3D

通道 题意:给你n(1e5)个三元组.然后要你求这n个三元组的LIS.和这样LIS的方案数.一个三元祖a比另一个元祖b大的条件是ax>=bx,ay>=by,az>=bz 思路:先按x排序,先降低一维,然后 剩下y .z,在y上进行CDQ分治,按y的大小用前面的更新后面的.z方向离散化之后用树状数组维护就可以 代码: #include <cstdio> #include <cstring> #include <algorithm> using names

hdu5618(cdq分治求三维偏序)

题意:给n(n<=100000)个点,坐标为(xi,yi,zi)(1<=xi,yi,zi<=100000),定义一个点A比一个点B小,当且仅当xA<=xB,yA<=yB,zA<=zB.求对于每个点,有多少个点比它小. 分析: 首先肯定按照x递增顺序排个序 接下来就是每次往平面插入一个点,求这个点左下方已经有多少个点,这可以用二维树状数组来搞,但是很明显会爆空间,不可以接受(当然树套树也是不可以的) 可以考虑对第二维cdq分治 对于一个区间[l,r],先递归区间[l,mi