HDU 5618:Jam's problem again(CDQ分治+树状数组处理三维偏序)
http://acm.hdu.edu.cn/showproblem.php?pid=5618
题意:……
思路:和NEUOJ那题一样的。重新写了遍理解了一下,算作处理三维偏序的模板了。
#include <cstdio>
#include <algorithm>
#include <iostream>
#include <cstring>
using namespace std;
#define INF 0x3f3f3f3f
#define N 100010
typedef long long LL;
struct node {
int x, y, z, id, f;
} p[N], s[N];
int ans[N], same[N], bit[N], gap; bool cmpx(const node &a, const node &b) { if(a.x != b.x) return a.x < b.x; if(a.y != b.y) return a.y < b.y; return a.z < b.z; }
bool cmpy(const node &a, const node &b) { if(a.y != b.y) return a.y < b.y; if(a.x != b.x) return a.x < b.x; return a.z < b.z; }
bool cmpid(const node &a, const node &b) { return a.id < b.id; }
bool check(node a, node b) { return a.x == b.x && a.y == b.y && a.z == b.z; } int lowbit(int x) { return x & (-x); } void update(int x, int w) { while(x <= gap) bit[x] += w, x += lowbit(x); } int query(int x) { int ans = ; while(x) ans += bit[x], x -= lowbit(x); return ans; } void CDQ(int l, int r) {
if(l == r) return ;
int m = (l + r) >> ;
CDQ(l, m); CDQ(m + , r);
int cnt = ;
// 标记是在左边还是右边
for(int i = l; i <= r; i++) s[++cnt] = p[i], s[cnt].f = i <= m ? : ;
sort(s + , s + + cnt, cmpy);
for(int i = ; i <= cnt; i++)
if(!s[i].f) update(s[i].z, );
else ans[s[i].id] += query(s[i].z);
for(int i = ; i <= cnt; i++)
if(!s[i].f) update(s[i].z, -);
} int main() {
int t, n;
scanf("%d", &t);
while(t--) {
scanf("%d", &n); gap = ;
for(int i = ; i <= n; i++) {
scanf("%d%d%d", &p[i].x, &p[i].y, &p[i].z);
p[i].id = i; if(p[i].z > gap) gap = p[i].z;
}
sort(p + , p + + n, cmpx);
// 离散化
for(int i = ; i <= n; ) {
int j = i + ;
while(j <= n && check(p[j], p[i])) j++;
while(i < j) same[p[i++].id] = p[j-].id;
}
for(int i = ; i <= n; i++) p[i].x = i;
memset(bit, , sizeof(bit));
memset(ans, , sizeof(ans));
CDQ(, n);
sort(p + , p + + n, cmpid);
for(int i = ; i <= n; i++) printf("%d\n", ans[same[i]]);
}
return ;
}
HDU 5618:Jam's problem again(CDQ分治+树状数组处理三维偏序)的更多相关文章
- HDU 5618 Jam's problem again CDQ分治 BC ROUND 70
题意:给你1e5个点(x,y,z),对于每一个点询问有多少个点(x1,y1,z1)满足x1<=x&&y1<=y&&z1<=z 分析:(官方题解奉上)很 ...
- [BZOJ2225][SPOJ2371]LIS2 - Another Longest Increasing Subsequence Problem:CDQ分治+树状数组+DP
分析 这回试了一下三级标题,不知道效果怎么样? 回到正题,二维最长上升子序列......嗯,我会树套树. 考虑\(CDQ\)分治,算法流程: 先递归进入左子区间. 将左,右子区间按\(x\)排序. 归 ...
- HDU 5618 Jam's problem again (cdq分治+BIT 或 树状数组套Treap)
题意:给n个点,求每一个点的满足 x y z 都小于等于它的其他点的个数. 析:三维的,第一维直接排序就好按下标来,第二维按值来,第三维用数状数组维即可. 代码如下: cdq 分治: #pragma ...
- HDU - 5324:Boring Class (CDQ分治&树状数组&最小字典序)
题意:给定N个组合,每个组合有a和b,现在求最长序列,满足a不升,b不降. 思路:三位偏序,CDQ分治. 但是没想到怎么输出最小字典序,我好菜啊. 最小字典序: 我们倒序CDQ分治,ans[i]表 ...
- 【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分治却可以非常好 ...
随机推荐
- java线:辛格尔顿隐藏ThreadLocal实现线程数据共享
效果图分享: A和B需要共享同一线程,还有一组的相同A和B共享还有一组线程,两组相互之间不受影响. 代码: package cn.itcast.lesson6; import java.util.Ra ...
- XF 绝对布局
using System; using Xamarin.Forms; using Xamarin.Forms.Xaml; [assembly: XamlCompilation (XamlCompila ...
- siliverlight某些事件无法响应
对一些无法响应的时间,需要注册 控件名:XZWT_TreeViewItem 事件:this.XZWT_TreeViewItem_MouseLeftButtonDown 具体注册方法: XZWT_Tre ...
- 【全面解禁!真正的Expression Blend实战开发技巧】第三章 从最常用ButtonStyle开始 - TextButton
原文:[全面解禁!真正的Expression Blend实战开发技巧]第三章 从最常用ButtonStyle开始 - TextButton 在实际项目中,使用blend做的最多的一定是各种自定义But ...
- iOS UILabel显示html标签
iOS7以后系统提供了显示html标签的方法 UIKIT_EXTERN NSString *const NSHTMLTextDocumentType NS_AVAILABLE_IOS(7_0); 直接 ...
- LOCK_TIMEOUT
SET LOCK_TIMEOUT 1000 begin tran TranNameA select * from tablenameA WITH (updlock) where... waitfor ...
- generate eml file
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> <HTML> <HEAD ...
- 零元学Expression Blend 4 - Chapter 33 简单轻松的学会如何使用Visual States(下)
原文:零元学Expression Blend 4 - Chapter 33 简单轻松的学会如何使用Visual States(下) 上篇提到了Visual State Manager中文翻译为视觉状态 ...
- fprintf函数将格式打印到文件,非常好用(怎么没早点发现这个函数)
/* fprintf example */ #include <stdio.h> int main () { FILE * pFile; int n; ]; pFile = fopen ( ...
- gcc与vs2013的三个charset编译选项
以gcc为例,它有三个命令选项:-finput-charset=gb18030-fexec-charset=utf-8-fwide-exec-charset=utf32顾名思议,input-chars ...