[BZOJ 3262]陌上开花
今天写了到偏序问题,发现博主真的是个傻X
以前的写法
/**************************************************************
Problem: 3262
User: MiEcoku
Language: C++
Result: Accepted
Time:3416 ms
Memory:6296 kb
****************************************************************/
#include <cstdio>
#include <algorithm>
using namespace std;
#define mid ( l + r >> 1)
#define lowbit(x) ( x & -x)
struct GG {
int x, y, z, id, w;
void get () {
scanf("%d%d%d", &x, &y, &z);
}
}a[], b[];
], k;
; ; x -= lowbit(x)) ans += dis[x]; return ans; }
void add(int x, int y) { for ( ; x <= k; x += lowbit(x)) dis[x] += y; }
bool cmp1(GG a, GG b) { return a.x < b.x || (a.x == b.x && a.y < b.y) || (a.x == b.x && a.y == b.y && a.z < b.z); }
bool cmp2(GG a, GG b) { return a.y < b.y || (a.y == b.y && a.z < b.z); }
];
void cdq(int l, int r) {
, r);
sort(b+l, b+mid+, cmp2); sort(b+mid+, b+r+, cmp2);
;
for ( int i = l; i <= mid; i ++) {
while( b[q].y < b[i].y && q <= r) ans[b[q].id] += query(b[q].z), q ++;
add(b[i].z, b[i].w);
}
while( q <= r) ans[b[q].id] += query(b[q].z), q ++;
for ( int i = l; i <= mid; i ++) add(b[i].z, -b[i].w);
}
];
int main() {
scanf("%d%d", &_, &k);
; i <= _; i ++) a[i].get();
sort( a+, a++_, cmp1);
; i <= _; i ++) {
cnt ++;
].x || a[i].y != a[i+].y || a[i].z != a[i+].z)
b[++ n] = a[i], b[n].w = cnt, cnt = , b[n].id = n;
}
cdq(, n);
; i <= n; i ++)
d[ans[b[i].id]+b[i].w-] += b[i].w;
; i < _; i ++) printf("%d\n", d[i]);
}
可以看出这是个n * logn^2 的算法,这也是博主在大多数地方看到的写法,结果傻叉的以为cdq就是n * long n ^ 2 的算法
实际上可不可以更快?
void cdq(int l, int r) {
, r);
sort(b+l, b+mid+, cmp2); sort(b+mid+, b+r+, cmp2);
;
for ( int i = l; i <= mid; i ++) {
while( b[q].y < b[i].y && q <= r) ans[b[q].id] += query(b[q].z), q ++;
add(b[i].z, b[i].w);
}
while( q <= r) ans[b[q].id] += query(b[q].z), q ++;
for ( int i = l; i <= mid; i ++) add(b[i].z, -b[i].w);
}
可以看出,在cdq内部排了个序,然而真的有这个必要吗? 答案是否定的
我们都知道归并排序,可以看出,内部这个排序无非是想要将第二位排序
但在内部这个循环内就已经排序,所以那个排序可以省略
我们新开个数组
void cdq(int l, int r) {
, r);
, cnt = ;
for ( int i = l; i <= mid; i ++) {
while( b[q].y < b[i].y && q <= r) ans[b[q].id] += query(b[q].z), T[++ cnt] = b[q], q ++;
add(b[i].z, b[i].w); T[++ cnt] = b[i];
}
while( q <= r) ans[b[q].id] += query(b[q].z), T[++ cnt] = b[q], q ++;
for ( int i = l; i <= mid; i ++) add(b[i].z, -b[i].w);
cnt = ;
for ( int i = l; i <= r; ++ i) b[i] = T[++ cnt];
}
这样,我们就省略了内部的一个排序问题,做到n * long n
还能再快不?
能!
for ( int i = l; i <= mid; i ++) add(b[i].z, -b[i].w);
对于这句话,我们能不能将它优化掉?
我们考虑加入一个时间戳 tim
int query(int x, int K) {
;
; x -= lowbit(x)) if( mark[x] == K) ans += dis[x];
return ans;
}
void add(int x, int y, int K) {
for ( ; x <= k; x += lowbit(x)) if( mark[x] == K) dis[x] += y;
else mark[x] = K, dis[x] = y;
}
然后对树状数组进行点修改
这样我们就将常数再次优化了下
上总代码
/**************************************************************
Problem: 3262
User: MiEcoku
Language: C++
Result: Accepted
Time:1428 ms
Memory:9028 kb
****************************************************************/
/**************************************************************
Problem: 3262
User: MiEcoku
Language: C++
Result: Accepted
Time:3416 ms
Memory:6296 kb
****************************************************************/
#include <cstdio>
#include <algorithm>
using namespace std;
#define mid ( l + r >> 1)
#define lowbit(x) ( x & -x)
struct GG {
int x, y, z, id, w;
void get () {
scanf("%d%d%d", &x, &y, &z);
}
}a[], b[], T[];
], k, tim, mark[];
int query(int x, int K) {
;
; x -= lowbit(x)) if( mark[x] == K) ans += dis[x];
return ans;
}
void add(int x, int y, int K) {
for ( ; x <= k; x += lowbit(x)) if( mark[x] == K) dis[x] += y;
else mark[x] = K, dis[x] = y;
}
bool cmp1(GG a, GG b) { return a.x < b.x || (a.x == b.x && a.y < b.y) || (a.x == b.x && a.y == b.y && a.z < b.z); }
bool cmp2(GG a, GG b) { return a.y < b.y || (a.y == b.y && a.z < b.z); }
];
void cdq(int l, int r) {
, r);
, cnt = ; ++ tim;
for ( int i = l; i <= mid; i ++) {
while( b[q].y < b[i].y && q <= r) ans[b[q].id] += query(b[q].z, tim), T[++ cnt] = b[q], q ++;
add(b[i].z, b[i].w, tim); T[++ cnt] = b[i];
}
while( q <= r) ans[b[q].id] += query(b[q].z, tim), T[++ cnt] = b[q], q ++;
cnt = ;
for ( int i = l; i <= r; ++ i) b[i] = T[++ cnt];
}
];
int main() {
scanf("%d%d", &_, &k);
; i <= _; i ++) a[i].get();
sort( a+, a++_, cmp1);
; i <= _; i ++) {
cnt ++;
].x || a[i].y != a[i+].y || a[i].z != a[i+].z)
b[++ n] = a[i], b[n].w = cnt, cnt = , b[n].id = n;
}
cdq(, n);
; i <= n; i ++)
d[ans[b[i].id]+b[i].w-] += b[i].w;
; i < _; i ++) printf("%d\n", d[i]);
}
博主因为傻逼不知道这么写然后考试写的KT-tree被卡了

[BZOJ 3262]陌上开花的更多相关文章
- bzoj 3262 陌上花开 - CDQ分治 - 树状数组
Description 有n朵花,每朵花有三个属性:花形(s).颜色(c).气味(m),又三个整数表示.现要对每朵花评级,一朵花的级别是它拥有的美丽能超过的花的数量.定义一朵花A比另一朵花B要美丽,当 ...
- BZOJ.3262.陌上花开([模板]CDQ分治 三维偏序)
题目链接 BZOJ3262 洛谷P3810 /* 5904kb 872ms 对于相邻x,y,z相同的元素要进行去重,并记录次数算入贡献(它们之间产生的答案是一样的,但不去重会..) */ #inclu ...
- Luogu 3810 & BZOJ 3262 陌上花开/三维偏序 | CDQ分治
Luogu 3810 & BZOJ 3263 陌上花开/三维偏序 | CDQ分治 题面 \(n\)个元素,每个元素有三个值:\(a_i\), \(b_i\) 和 \(c_i\).定义一个元素的 ...
- 【BZOJ 3262】 3262: 陌上花开 (CDQ分治)
3262: 陌上花开 Description 有n朵花,每朵花有三个属性:花形(s).颜色(c).气味(m),又三个整数表示.现要对每朵花评级,一朵花的级别是它拥有的美丽能超过的花的数量.定义一朵花A ...
- BZOJ 3262 陌上花开 ——CDQ分治
[题目分析] 多维问题,我们可以按照其中一维排序,然后把这一维抽象的改为时间. 然后剩下两维,就像简单题那样,排序一维,树状数组一维,按照时间分治即可. 挺有套路的一种算法. 时间的抽象很巧妙. 同种 ...
- bzoj 3262 陌上花开
本质是一个三维偏序,一位排序后cdq分治,一维在子函数里排序,一维用树状数组维护. 把三维相等的合并到一个里面. #include<iostream> #include<cstdio ...
- BZOJ 3262 陌上花开 CDQ分治
= =原来复杂度还是nlog^2(n) Orz 被喷了 #include<cstdio> #include<cstdlib> #include<algorithm> ...
- BZOJ 3262: 陌上花开 [CDQ分治 三维偏序]
Description 有n朵花,每朵花有三个属性:花形(s).颜色(c).气味(m),又三个整数表示.现要对每朵花评级,一朵花的级别是它拥有的美丽能超过的花的数量.定义一朵花A比另一朵花B要美丽,当 ...
- 【刷题】BZOJ 3262 [HNOI2008]GT考试
Description 阿申准备报名参加GT考试,准考证号为N位数X1X2....Xn(0<=Xi<=9),他不希望准考证号上出现不吉利的数字. 他的不吉利数学A1A2...Am(0< ...
随机推荐
- JDK的安装和环境配置
安装环境:网上下载的JDK一般都包含JRE在里面,安装JDK完成后会提示接着装JRE,如果没提示再去下载另装如:jdk_8u101_windows_x64_8.0.1010.13 配置环境:新建系统变 ...
- /error处理
1 BasicErrorController 1.1 简述 SpringMVC框架在出现错误时有一个默认的错误请求 /error:出现异常之后执行/error请求之前框架会判断出现异常的请求类型,然后 ...
- 697. Degree of an Array 频率最高元素的最小覆盖子数组
[抄题]: Given a non-empty array of non-negative integers nums, the degree of this array is defined as ...
- js数值和字符串比较的规则
1.数值和字符串比较时 a.若字符串为数字字符串,则将字符串转为数字,再比较 b.若字符串不为数字字符串,则直接返回false,因为这里把字符串转为了NaN, 数字与NaN比较,都返回false
- sql去除重复记录 且保留id最小的 没用
第一步:查询重复记录 SELECT * FROM TableName WHERE RepeatFiled IN ( SELECT RepeatFiled FROM TableName ...
- Mac notes
1. Mac应用数据存放位置 ~/Library/Application Support/ 比如sublime text的应用数据~/Library/Application Support/Subli ...
- 关于pycharm字体大小的调整
我们平常编写pyhton 可以用sublime eclipse 但是eclipse在后期需要安装很多插件,这很是麻烦,为了避免这种麻烦,我们采用pycharm来编写,但是刚装上的该软件 不建议同学们进 ...
- Adorner的收集
Adorners Overview https://docs.microsoft.com/en-us/dotnet/framework/wpf/controls/adorners-overview ' ...
- Floyd-Warshall求图中任意两点的最短路径
原创 除了DFS和BFS求图中最短路径的方法,算法Floyd-Warshall也可以求图中任意两点的最短路径. 从图中任取两点A.B,A到B的最短路径无非只有两种情况: 1:A直接到B这条路径即是最短 ...
- delphi 金额大小写转换函数
{*------------------------------------------------ 金额大小写转换函数 @author 王云盼 @version V1506.01 在delphi7测 ...