传送门: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分治的更多相关文章

  1. BZOJ3262: 陌上花开(三维偏序,CDQ分治)

    Description 有n朵花,每朵花有三个属性:花形(s).颜色(c).气味(m),用三个整数表示. 现在要对每朵花评级,一朵花的级别是它拥有的美丽能超过的花的数量. 定义一朵花A比另一朵花B要美 ...

  2. 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 ...

  3. hdu5618 (三维偏序,cdq分治)

    给定空间中的n个点,问每个点有多少个点小于等于自己. 先来分析简单的二维的情况,那么只要将x坐标排序,那么这样的问题就可以划分为两个子问题,,这样的分治有一个特点,即前一个子问题的解决是独立的,而后一 ...

  4. P3810 陌上花开 CDQ分治

    陌上花开 CDQ分治 传送门:https://www.luogu.org/problemnew/show/P3810 题意: \[ 有n 个元素,第 i 个元素有 a_i. b_i. c_i 三个属性 ...

  5. 洛谷P3810 陌上花开 CDQ分治(三维偏序)

    好,这是一道三维偏序的模板题 当然没那么简单..... 首先谴责洛谷一下:可怜的陌上花开的题面被无情的消灭了: 这么好听的名字#(滑稽) 那么我们看了题面后就发现:这就是一个三维偏序.只不过ans不加 ...

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

    Description 有n朵花,每朵花有三个属性:花形(s).颜色(c).气味(m),又三个整数表示.现要对每朵花评级,一朵花的级别是它拥有的美丽能超过的花的数量.定义一朵花A比另一朵花B要美丽,当 ...

  7. 【BZOJ-3262】陌上花开 CDQ分治(3维偏序)

    3262: 陌上花开 Time Limit: 20 Sec  Memory Limit: 256 MBSubmit: 1439  Solved: 648[Submit][Status][Discuss ...

  8. N维偏序:cdq分治

    cdq(陈丹琦)分治,是一种类似二分的算法.基本思想同分治: 递归,把大问题划分成若干个结构相同的子问题,直到(L==R): 处理左区间[L,mid]对右区间[mid+1,R]的影响: 合并. 它可以 ...

  9. bzoj3262陌上花开 cdq分治

    3262: 陌上花开 Time Limit: 20 Sec  Memory Limit: 256 MBSubmit: 2794  Solved: 1250[Submit][Status][Discus ...

  10. bzoj3262: 陌上花开(cdq分治+树状数组)

    3262: 陌上花开 题目:传送门 题解: %%%cdq分治 很强大的一个暴力...感觉比分块高级多了 这道题目就是一个十分经典的三维偏序的例题: 一维直接暴力排序x 二维用csq维护y 三维用树状数 ...

随机推荐

  1. 在工作中常用到的SQL

    前言 只有光头才能变强. 文本已收录至我的GitHub仓库,欢迎Star:https://github.com/ZhongFuCheng3y/3y 最近在公司做了几张报表,还记得刚开始要做报表的时候都 ...

  2. yum源使用报错

    CentOS系统yum源使用报错:Error: Cannot retrieve repository metadata (repomd.xml) for repository: rpmforge. 服 ...

  3. Java 求字符串中出现频率最高字符

    前段时间接触的这个题目,大体理解了,还有些小地方仍待进一步品味,暂且记下. import java.util.ArrayList; import java.util.Arrays; import ja ...

  4. codeforces 327 B. Hungry Sequence

    题目链接 题目就是让你输出n个数的序列,要保证该序列是递增的,并且第i个数的前面不能保护它的约数,我直接先对前100000的素数打表,然后输出前n个,so easy. //cf 191 B #incl ...

  5. mule优缺点和MEL

    优点1.开源 https://github.com/mulesoft/mule2.丰富的connector ,可以通过不同的形式来连接各个系统JMS.Web Service.JDBC.HTTP等3.c ...

  6. Currency Exchange POJ1860

    Description Several currency exchange points are working in our city. Let us suppose that each point ...

  7. JVM解剖乐园

    1.JVM锁粗化和循环原文标题:JVM Anatomy Quark #1: Lock Coarsening and Loops 众所周知Hotsport编译器会进行JVM锁粗化和优化,它将相邻的锁区块 ...

  8. 监控JVM

    WAS配置visualVM 在was控制台:找到应用程序服务器--java和进程管理--进程定义--JAVA虚拟机/通用JVM 参数 ,对应英文Application servers > ser ...

  9. LoRaWAN Server开源项目部署

    1,安装MQTT broker,Redis,PostgreSQL sudo apt install mosquitto mosquitto-clients redis-server redis-too ...

  10. pyinstaller打包django项目成exe以及遇到的一些问题

    1. 环境 环境为:win7 + Python3.6 + Django (2.1.10) + PyInstaller (3.5) 注意: 这里的PyInstaller,笔者是直接使用pip安装,中间会 ...