【CDQ分治】【模板】三维偏序(陌上花开)
P3810 【模板】三维偏序(陌上花开) - 洛谷 | 计算机科学教育新生态 (luogu.com.cn)
#include <bits/stdc++.h>
using namespace std;
using i64 = long long;
template<typename T>
struct BIT {
#ifndef lowbit
#define lowbit(x) (x & (-x));
#endif
int n;
vector<T> t;
BIT () {}
BIT (int _n): n(_n) { t.resize(_n + 1); }
BIT (int _n, vector<T>& a): n(_n) {
t.resize(_n + 1);
for (int i = 1; i <= n; ++ i) {
t[i] += a[i];
int j = i + lowbit(i);
if (j <= n) t[j] += t[i];
}
}
//单点修改
void update(int i, T x) {
while (i <= n) {
t[i] += x;
i += lowbit(i);
}
}
//区间查询
T sum(int i) {
T ans = 0;
while (i > 0) {
ans += t[i];
i -= lowbit(i);
}
return ans;
}
T query(int i, int j) {
return sum(j) - sum(i - 1);
}
//区间修改则存入差分数组,[l, r] + k则update(x,k),update(y+1,-k)
//单点查询则直接求前缀和sum(x)
//求逆序对
/*
iota(d.begin(), d.end(), 0);
stable_sort(d.begin(), d.end(), [&](int x, int y) {
return a[x] < a[y];
});去重排序
BIT<i64> tree(n);
i64 ans = 0;
for (int i = 1; i <= n; i ++) {
tree.update(d[i], 1);
ans += i - tree.sum(d[i]);
}
*/
};
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int n, k;
cin >> n >> k;
vector<array<int, 3>> a(n);
for (auto &[x, y, z] : a)
cin >> x >> y >> z;
sort(a.begin(), a.end());
vector<array<int, 5>> A;
for (int i = 0; i < n; i ++) {
int j = i;
while (j + 1 < n && a[j + 1] == a[j]) {
j ++;
}
A.push_back({a[j][0], a[j][1], a[j][2], j - i + 1, 0});
i = j;
}
BIT<i64> bit(k);
auto cdq = [&](auto && self, int l, int r)->void{
if (l == r)
return ;
int mid = l + r >> 1;
self(self, l, mid);
self(self, mid + 1, r);
sort(A.begin() + l, A.begin() + mid + 1, [](auto x, auto y) {
if (x[1] == y[1]) return x[2] < y[2];
return x[1] < y[1];
});
sort(A.begin() + mid + 1, A.begin() + r + 1, [](auto x, auto y) {
if (x[1] == y[1]) return x[2] < y[2];
return x[1] < y[1];
});
int i = l, j = mid + 1;
while (j <= r) {
while (i <= mid && A[i][1] <= A[j][1]) {
bit.update(A[i][2], A[i][3]);
i ++;
}
A[j][4] += bit.sum(A[j][2]);
j ++;
}
for (int k = l; k < i; k ++)
bit.update(A[k][2], -A[k][3]);
};
cdq(cdq, 0, A.size() - 1);
vector<i64> f(n + 1);
for (auto &[a, b, c, cnt, d] : A) {
f[d + cnt - 1] += cnt;
}
for (int i = 0; i < n; i ++)
cout << f[i] << "\n";
return 0;
}
【CDQ分治】【模板】三维偏序(陌上花开)的更多相关文章
- 并不对劲的cdq分治解三维偏序
为了反驳隔壁很对劲的太刀流,并不对劲的片手流决定与之针锋相对,先一步发表cdq分治解三维偏序. 很对劲的太刀流在这里-> 参照一.二维偏序的方法,会发现一位偏序就是直接排序,可以看成通过排序使 ...
- cdq分治解决三维偏序
问题背景 在三维坐标系中有n个点,坐标为(xi,yi,zi). 定义一个点A比一个点B小,当且仅当xA<=xB,yA<=yB,zA<=zB.问对于每个点,有多少个点比它小.(n< ...
- 【算法学习】【洛谷】cdq分治 & P3810 三维偏序
cdq是何许人也?请参看这篇:https://wenku.baidu.com/view/3b913556fd0a79563d1e7245.html. 在这篇论文中,cdq提出了对修改/询问型问题(Mo ...
- hdu5618(cdq分治求三维偏序)
题意:给n(n<=100000)个点,坐标为(xi,yi,zi)(1<=xi,yi,zi<=100000),定义一个点A比一个点B小,当且仅当xA<=xB,yA<=yB, ...
- BZOJ 3262: 陌上花开 (cdq分治,三维偏序)
#include <iostream> #include <stdio.h> #include <algorithm> using namespace std; c ...
- SPOJ:Another Longest Increasing Subsequence Problem(CDQ分治求三维偏序)
Given a sequence of N pairs of integers, find the length of the longest increasing subsequence of it ...
- HDU4742 CDQ分治,三维LIS
HDU4742 CDQ分治,三维LIS 传送门:http://acm.hdu.edu.cn/showproblem.php?pid=4742 题意: 每个球都有三个属性值x,y,z,要求最长的lis的 ...
- 洛谷 P3810 【模板】三维偏序(陌上花开) (cdq分治模板)
在solve(L,R)中,需要先分治solve两个子区间,再计算左边区间修改对右边区间询问的贡献. 注意,计算额外的贡献时,两子区间各自内部的顺序变得不再重要(不管怎么样左边区间的都发生在右边之前), ...
- CDQ解决一些三维偏序的问题
本来几天前就该记录的东西,硬生生被我拖到了现在,太懒了... 在cdq学习时,二维偏序已经解决了,无非就是先sort使第一维有序,然后再用cdq或者数据结构处理第二维.而三维偏序的时候呢,大佬的做法好 ...
- CDQ分治(三维偏序集)
排序,三关键字 去重 归并排序+树状数组 #include<bits/stdc++.h> using namespace std; #define re register int cons ...
随机推荐
- 报错 ERR !npicode ELIFECYCLE dev: wue-cli-service serve
在系统变量 Path 里面加上:%SystemRoot%\system32,关掉终端,重新启动项目.
- 测试网络的小工具WinMTR
ping网络的小工具 搜集了两个版本中文版和英文版 中文版---- WinMTR中文版.rarhttps://www.aliyundrive.com/s/bZqmokL5dTt提取码: k6v7 英文 ...
- WPF Canvas在Image 图像上绘图,自适应缩放.
效果如图 实现了绘图,自适应缩放 核心代码如下 <Window.InputBindings> <KeyBinding Key="Z" Modifiers=&quo ...
- python-pytest:多进程、多线程执行用例
准备工作: Python Package pytest-parallel-0.0.10: windows版本只能是0.0.10 pytest-xdist 这两个包都是基于pytest基础上,实现多进程 ...
- 在Xcode编写我们的quick help
程序环境 Xcode 13.2.1 "写代码要经常写注释."虽然很多人不以为然,但是在使用Xcode的时候,我们在写注释的时候稍微注意一下格式的话,Xcode会给我们带来额外的正反 ...
- Wireshark找不到接口
在管理员权限下的命令行窗口输入net start npf即可. 注意是管理员权限下的,否则会拒绝访问.
- 关于docker-compose up -d 出现超时情况处理
由于要搭建一个ctf平台,用docker一键搭建是出现超时情况 用了很多办法,换源,等之类的一样没办法,似乎它就是只能用官方那个一样很怪. 只能用一种笨办法来处理了,一个个pull. 打个比如: 打开 ...
- Spring定时任务和@Async注解异步调用
Spring定时任务 1.@Scheduled注解方式 使用方式 @Scheduled的使用方式十分简单,首先在项目启动类添加注解@EnableScheduled. 编写定时任务方法,方法上添加注解@ ...
- VSCode如何设置Vue前端的debug调试
vscode在调试vue.代码时,如何进行debug? 1.安装Chrome Debug插件. 2.在launch.json中,将url修改成你前端项目的路径: 1 { 2 // Use Intell ...
- css-渐变简约的登录设计
代码如下 <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF- ...