P3810 【模板】三维偏序(陌上花开)cdq分治
传送门:https://www.luogu.org/problemnew/show/P3810
cdq分治的模板题,第一层外部排序,第二层cdq归并排序,这个时候不用考虑第一次的顺序,第三次用树状数组。
注意,不要用memset,用队列保存加上的值,最后在把加上的值减去就行了。
#include <algorithm>
#include <iterator>
#include <iostream>
#include <cstring>
#include <cstdlib>
#include <iomanip>
#include <bitset>
#include <cctype>
#include <cstdio>
#include <string>
#include <vector>
#include <stack>
#include <cmath>
#include <queue>
#include <list>
#include <map>
#include <set>
#include <cassert>
//#include <unordered_map>
/* ⊂_ヽ
\\ Λ_Λ 来了老弟
\('ㅅ')
> ⌒ヽ
/ へ\
/ / \\
レ ノ ヽ_つ
/ /
/ /|
( (ヽ
| |、\
| 丿 \ ⌒)
| | ) /
'ノ ) Lノ */ using namespace std;
#define lson (l , mid , rt << 1)
#define rson (mid + 1 , r , rt << 1 | 1)
#define debug(x) cerr << #x << " = " << x << "\n";
#define pb push_back
#define pq priority_queue typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
//typedef __int128 bll;
typedef pair<ll ,ll > pll;
typedef pair<int ,int > pii;
typedef pair<int,pii> p3; //priority_queue<int> q;//这是一个大根堆q
//priority_queue<int,vector<int>,greater<int> >q;//这是一个小根堆q
#define fi first
#define se second
//#define endl '\n' #define boost ios::sync_with_stdio(false);cin.tie(0)
#define rep(a, b, c) for(int a = (b); a <= (c); ++ a)
#define max3(a,b,c) max(max(a,b), c);
#define min3(a,b,c) min(min(a,b), c); const ll oo = 1ll<<;
const ll mos = 0x7FFFFFFF; //
const ll nmos = 0x80000000; //-2147483648
const int inf = 0x3f3f3f3f;
const ll inff = 0x3f3f3f3f3f3f3f3f; //
const int mod = ;
const double esp = 1e-;
const double PI=acos(-1.0);
const double PHI=0.61803399; //黄金分割点
const double tPHI=0.38196601; template<typename T>
inline T read(T&x){
x=;int f=;char ch=getchar();
while (ch<''||ch>'') f|=(ch=='-'),ch=getchar();
while (ch>=''&&ch<='') x=x*+ch-'',ch=getchar();
return x=f?-x:x;
} inline void cmax(int &x,int y){if(x<y)x=y;}
inline void cmax(ll &x,ll y){if(x<y)x=y;}
inline void cmin(int &x,int y){if(x>y)x=y;}
inline void cmin(ll &x,ll y){if(x>y)x=y;}
#define MODmul(a, b) ((a*b >= mod) ? ((a*b)%mod + 2*mod) : (a*b))
#define MODadd(a, b) ((a+b >= mod) ? ((a+b)%mod + 2*mod) : (a+b)) /*-----------------------showtime----------------------*/
const int maxn = 1e5+;
struct node{
int x,y,z;
int id;
}a[maxn],b[maxn],tmp[maxn];
bool cmp(node a,node b){
if(a.x != b.x) return a.x < b.x;
if(a.y != b.y) return a.y < b.y;
else return a.z < b.z;
}
int cnt[maxn],ans[maxn]; int sum[maxn*];
int lowbit(int x){
return x & (-x);
}
void add(int x,int c){
while(x < maxn*){
sum[x] += c;
x += lowbit(x);
}
}
int getsum(int x){
int res = ;
while(x > ) {
res += sum[x];
x -= lowbit(x);
}
return res;
}
int lazy[maxn];
queue<int>que;
void cdq(int le,int ri){
int mid = (le + ri) >> ;
if(ri - le <= ) return ;
cdq(le, mid); cdq(mid+, ri); // memset(sum, 0, sizeof(sum)); int p = le, q = mid+;
int id = ;
while(p <= mid && q <= ri){
if(a[p].y <= a[q].y) {
add(a[p].z, lazy[a[p].id]);
que.push(p);
tmp[++id] = a[p++];
}
else {
cnt[a[q].id] += getsum(a[q].z);
tmp[++id] = a[q++];
}
}
while(p <= mid) tmp[++id] = a[p++];
while(q <= ri) {
cnt[a[q].id] += getsum(a[q].z);
tmp[++id] = a[q++];
}
while(!que.empty()){
int u = que.front(); que.pop();
add(a[u].z, -lazy[a[u].id]);
} for(int i=; i<=id; i++) a[i+le-] = tmp[i]; }
// int out[maxn];
map<p3,int>mp; int main(){
int n,k;
scanf("%d%d", &n, &k);
int tot = ;
rep(i, , n) {
int x, y, z;
scanf("%d%d%d", &x, &y, &z);
// read(x);read(y);read(z);
p3 tp = p3(x, pii(y, z));
if(mp.count(tp)) lazy[mp[tp]]++,cnt[mp[tp]]++;
else {
tot++;
a[tot].id = tot;
a[tot].x = x;
a[tot].y = y;
a[tot].z = z;
mp[tp] = tot;
lazy[tot] = ;
}
}
sort(a+, a++tot, cmp); cdq(, tot); rep(i, , tot) ans[cnt[a[i].id]] += lazy[a[i].id];
/*
rep(i, 1, n) {
out[a[i].id] = cnt[a[i].id];
}
rep(i, 1, n)
cout<<cnt[i]<<" ";
cout<<endl;
*/
rep(i, , n-) printf("%d\n", ans[i]);
return ;
}
P3810 【模板】三维偏序(陌上花开)cdq分治的更多相关文章
- BZOJ3262: 陌上花开(三维偏序,CDQ分治)
Description 有n朵花,每朵花有三个属性:花形(s).颜色(c).气味(m),用三个整数表示. 现在要对每朵花评级,一朵花的级别是它拥有的美丽能超过的花的数量. 定义一朵花A比另一朵花B要美 ...
- HDU 5618 Jam's problem again(三维偏序,CDQ分治,树状数组,线段树)
Jam's problem again Time Limit: 5000/2500 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Othe ...
- hdu5618 (三维偏序,cdq分治)
给定空间中的n个点,问每个点有多少个点小于等于自己. 先来分析简单的二维的情况,那么只要将x坐标排序,那么这样的问题就可以划分为两个子问题,,这样的分治有一个特点,即前一个子问题的解决是独立的,而后一 ...
- P3810 陌上花开 CDQ分治
陌上花开 CDQ分治 传送门:https://www.luogu.org/problemnew/show/P3810 题意: \[ 有n 个元素,第 i 个元素有 a_i. b_i. c_i 三个属性 ...
- 洛谷P3810 陌上花开 CDQ分治(三维偏序)
好,这是一道三维偏序的模板题 当然没那么简单..... 首先谴责洛谷一下:可怜的陌上花开的题面被无情的消灭了: 这么好听的名字#(滑稽) 那么我们看了题面后就发现:这就是一个三维偏序.只不过ans不加 ...
- BZOJ 3262: 陌上花开 [CDQ分治 三维偏序]
Description 有n朵花,每朵花有三个属性:花形(s).颜色(c).气味(m),又三个整数表示.现要对每朵花评级,一朵花的级别是它拥有的美丽能超过的花的数量.定义一朵花A比另一朵花B要美丽,当 ...
- 【BZOJ-3262】陌上花开 CDQ分治(3维偏序)
3262: 陌上花开 Time Limit: 20 Sec Memory Limit: 256 MBSubmit: 1439 Solved: 648[Submit][Status][Discuss ...
- N维偏序:cdq分治
cdq(陈丹琦)分治,是一种类似二分的算法.基本思想同分治: 递归,把大问题划分成若干个结构相同的子问题,直到(L==R): 处理左区间[L,mid]对右区间[mid+1,R]的影响: 合并. 它可以 ...
- bzoj3262陌上花开 cdq分治
3262: 陌上花开 Time Limit: 20 Sec Memory Limit: 256 MBSubmit: 2794 Solved: 1250[Submit][Status][Discus ...
- bzoj3262: 陌上花开(cdq分治+树状数组)
3262: 陌上花开 题目:传送门 题解: %%%cdq分治 很强大的一个暴力...感觉比分块高级多了 这道题目就是一个十分经典的三维偏序的例题: 一维直接暴力排序x 二维用csq维护y 三维用树状数 ...
随机推荐
- EasyUI combobox下拉列表实现搜索过滤(模糊匹配)
项目中的某个下拉列表长达200多个项,这么巨大的数量一个一个找眼镜都得看花,于是就得整了个搜索功能.看网上别人帖子有只能前缀匹配的方案,但只能前缀匹配的话用起来也不是很方便.于是就记录一下模糊匹配的方 ...
- Apache之——多虚拟主机多站点配置的两种实现方案
Apache中配置多主机多站点,可以通过两种方式实现: 将同一个域名的不同端口映射到不同的虚拟主机,不同端口映射到不同的站点: 将同一个端口映射成不同的域名,不同的域名映射到不同的站点. 我们只需要修 ...
- Oracle_InstantClient 及PL/SQL Developer工具的安装
一.下载 InstantClient 地址: http://www.oracle.com/technology/software/tech/oci/instantclient/index.html i ...
- ORACLE 的CONNECT BY、START WITH,CONNECT_BY_ROOT、CONNECT_BY_ISLEAF、SYS_CONNECT_BY_PATH,LEVEL的使用(Hierarchical query-层次查询)
如果表中存在层次数据,则可以使用层次化查询子句查询出表中行记录之间的层次关系基本语法: START WITH <condition1> CONNECT BY [ NOCYCLE ] < ...
- java高并发系列 - 第23天:JUC中原子类,一篇就够了
这是java高并发系列第23篇文章,环境:jdk1.8. 本文主要内容 JUC中的原子类介绍 介绍基本类型原子类 介绍数组类型原子类 介绍引用类型原子类 介绍对象属性修改相关原子类 预备知识 JUC中 ...
- Java模拟并解决缓存穿透
什么叫做缓存穿透 缓存穿透只会发生在高并发的时候,就是当有10000个并发进行查询数据的时候,我们一般都会先去redis里面查询进行数据,但是如果redis里面没有这个数据的时候,那么这10000个并 ...
- Python 之父的解析器系列之三:生成一个 PEG 解析器
原题 | Generating a PEG Parser 作者 | Guido van Rossum(Python之父) 译者 | 豌豆花下猫("Python猫"公众号作者) 声明 ...
- python3从入门到精通之数据类型,布尔类型介绍
数据的类型 为了更充分的利用内存空间以及更有效率的管理内存,变量是有不同的类型的. Number(数字) int(整型) float(浮点型) complex(复数) bool(布尔) String( ...
- docker配置国内镜像地址
docker的官方镜像站被大天朝强了,今天发现阿里有镜像加速这个功能,目前好像是在公测中,废话不多说,接下来告诉你怎么操作. 点击进入阿里镜像库 https://cr.console.aliyun.c ...
- 手写Struts,带你深入源码中心解析
个人剖析,不喜勿喷 扫码关注公众号,不定期更新干活 在此申明本博文并非原创,原文:http://blog.csdn.net/lenotang/article/details/3336623,本文章是在 ...