题目链接

传送门

思路

这个题在\(BZOJ\)上有个二维平面的版本(\(BZOJ2716\)天使玩偶),不过是权限题因此就不附带链接了,我也只是在算法进阶指南上看到过,那个题的写法是\(CDQ\),然后比赛开始半个小时我就开始写\(CDQ\),\(T\)了之后冷静分析发现复杂度我少算了个\(log\),\(CDQ\)写这题的复杂度是\(nlog^3(n)\),然后就没思路了。

赛后看\(qls\)说用三维\(bit\)可以过后试了一下\(T\)了,然后发现\(qls\)的代码当时跑了\(937ms\)应该是卡过去的,感觉是\(vector\)常数大,但是想不出除\(vector\)的其他动态开全局数组大小的方法,最后疯狂交在\(T\)了一页后成功卡过去了,下面贴一下三维\(bit\)的代码和\(CDQ\)写法(对拍了很久貌似没问题,就当过了吧~)的代码。

这两份代码的主体思路就是拆绝对值符号,然后查询时(拿\(x_1\leq x2,y_1\leq y2,z_1\leq z_2\)举例,下标为\(2\)的是查询的点)\(|x_2-x_1|+|y_2-y_1|+|z_2-z_1|=(x_2+y_2+z_2)-(x_1+y_1+z_1)\),然后就是要查询\(x_1+y_1+z_1\)的最大值用树状数组维护即可,其他情况分别移动坐标即可。

update:发现题解里面贴的那份代码的三维\(bit\)写法贼牛逼,贴的代码就改成看了那个队的代码之后写的了。

代码

//三维bit
#include <set>
#include <map>
#include <deque>
#include <queue>
#include <stack>
#include <cmath>
#include <ctime>
#include <bitset>
#include <cstdio>
#include <string>
#include <vector>
#include <cassert>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std; typedef long long LL;
typedef pair<LL, LL> pLL;
typedef pair<LL, int> pLi;
typedef pair<int, LL> pil;;
typedef pair<int, int> pii;
typedef unsigned long long uLL; #define lson rt<<1
#define rson rt<<1|1
#define lowbit(x) x&(-x)
#define name2str(name) (#name)
#define bug printf("*********\n")
#define debug(x) cout<<#x"=["<<x<<"]" <<endl
#define FIN freopen("/home/dillonh/CLionProjects/Dillonh/in.txt","r",stdin)
#define IO ios::sync_with_stdio(false),cin.tie(0) const double eps = 1e-8;
const int mod = 1000000007;
const int maxn = 800000 + 7;
const double pi = acos(-1);
const int inf = 0x3f3f3f3f;
const LL INF = 0x3f3f3f3f3f3f3f3fLL; int n, m, h, q, op, x, y, z; struct BIT {
int a[maxn]; int gethash(int x, int y, int z) {
return x * h * m + y * h + z;
} void init() {
for(int i = 0; i <= n; ++i) {
for(int j = 0; j <= m; ++j) {
for(int k = 0; k <= h; ++k) {
a[gethash(i, j, k)] = 0;
}
}
}
} void update(int x, int y, int z) {
for(int i = x; i <= n; i += lowbit(i)) {
for(int j = y; j <= m; j += lowbit(j)) {
for(int k = z; k <= h; k += lowbit(k)) {
int num = gethash(i, j, k);
a[num] = max(a[num], x + y + z);
}
}
}
} int query(int x, int y, int z) {
int ans = 0;
for(int i = x; i > 0; i -= lowbit(i)) {
for(int j = y; j > 0; j -= lowbit(j)) {
for(int k = z; k > 0; k -= lowbit(k)) {
ans = max(ans, a[gethash(i, j, k)]);
}
}
}
if(ans == 0) return inf;
return x + y + z - ans;
}
}bit[8]; int main() {
scanf("%d%d%d%d", &n, &m, &h, &q);
n += 1, m += 1, h += 1;
for(int i = 0; i < 8; ++i) bit[i].init();
for(int i = 1; i <= q; ++i) {
scanf("%d%d%d%d", &op, &x, &y, &z);
if(op == 1) {
bit[1].update(x, y, z);
bit[2].update(x, y, h - z);
bit[3].update(x, m - y, z);
bit[4].update(n - x, y, z);
bit[5].update(n - x, m - y, z);
bit[6].update(n - x, y, h - z);
bit[7].update(x, m - y, h - z);
bit[0].update(n - x, m - y, h - z);
} else {
int ans = inf;
ans = min(ans, bit[1].query(x, y, z));
ans = min(ans, bit[2].query(x, y, h - z));
ans = min(ans, bit[3].query(x, m - y, z));
ans = min(ans, bit[4].query(n - x, y, z));
ans = min(ans, bit[5].query(n - x, m - y, z));
ans = min(ans, bit[6].query(n - x, y, h - z));
ans = min(ans, bit[7].query(x, m - y, h - z));
ans = min(ans, bit[0].query(n - x, m - y, h - z));
printf("%d\n", ans);
}
}
return 0;
}
//CDQ
#include <set>
#include <map>
#include <deque>
#include <queue>
#include <stack>
#include <cmath>
#include <ctime>
#include <bitset>
#include <cstdio>
#include <string>
#include <vector>
#include <cassert>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std; typedef long long LL;
typedef pair<LL, LL> pLL;
typedef pair<LL, int> pLi;
typedef pair<int, LL> pil;;
typedef pair<int, int> pii;
typedef unsigned long long uLL; #define lson rt<<1
#define rson rt<<1|1
#define lowbit(x) x&(-x)
#define name2str(name) (#name)
#define bug printf("*********\n")
#define debug(x) cout<<#x"=["<<x<<"]" <<endl
#define FIN freopen("/home/dillonh/CLionProjects/Dillonh/in.txt","r",stdin)
#define IO ios::sync_with_stdio(false),cin.tie(0) const double eps = 1e-8;
const int mod = 1000000007;
const int maxn = 100001;
const double pi = acos(-1);
const int inf = 0x3f3f3f3f;
const LL INF = 0x3f3f3f3f3f3f3f3fLL; int n, m, h, q, op, x, y, z;
int tree[maxn], ans[maxn]; struct node {
int id, x, y, z, op, val, flag;
bool operator < (const node& a) const {
return id < a.id;
}
}a[maxn], tmp[maxn], pp[maxn]; void add(int x, int val) {
while(x <= h) {
tree[x] = max(tree[x], val);
x += lowbit(x);
}
} int query(int x) {
int ans = 0;
while(x) {
ans = max(ans, tree[x]);
x -= lowbit(x);
}
if(ans == 0) ans = -inf;
return ans;
} void del(int x) {
while(x <= h + 1) {
tree[x] = 0;
x += lowbit(x);
}
} void cdq2(int l, int r) {
if(l == r) return;
int mid = (l + r) >> 1;
cdq2(l, mid), cdq2(mid + 1, r);
int ls = l, rs = mid + 1, cnt = l;
for(; rs <= r; ++rs) {
for(; ls <= mid && tmp[ls].y <= tmp[rs].y; ++ls) {
if(tmp[ls].op == 1 && tmp[ls].flag) {
add(tmp[ls].z, tmp[ls].val);
}
}
if(!tmp[rs].flag && tmp[rs].op == 2) ans[tmp[rs].id] = min(ans[tmp[rs].id], tmp[rs].val - query(tmp[rs].z));
}
--ls;
while(ls >= l) {
if(tmp[ls].op == 1 && tmp[ls].flag) {
del(tmp[ls].z);
}
--ls;
}
ls = l, rs = mid + 1;
while(ls <= mid && rs <= r) {
if(tmp[ls].y < tmp[rs].y) {
pp[cnt++] = tmp[ls++];
} else if(tmp[ls].y > tmp[rs].y) {
pp[cnt++] = tmp[rs++];
} else if(tmp[ls].z <= tmp[rs].z) {
pp[cnt++] = tmp[ls++];
} else {
pp[cnt++] = tmp[rs++];
}
}
while(ls <= mid) pp[cnt++] = tmp[ls++];
while(rs <= r) pp[cnt++] = tmp[rs++];
for(int i = l; i <= r; ++i) tmp[i] = pp[i];
} void cdq1(int l, int r) {
if(l == r) return;
int mid = (l + r) >> 1;
cdq1(l, mid), cdq1(mid + 1, r);
int ls = l, rs = mid + 1, cnt = l;
while(ls <= mid && rs <= r) {
if(a[ls].x < a[rs].x) {
tmp[cnt] = a[ls++];
tmp[cnt++].flag = 1;
} else if(a[ls].x > a[rs].x) {
tmp[cnt] = a[rs++];
tmp[cnt++].flag = 0;
} else if(a[ls].y < a[rs].y) {
tmp[cnt] = a[ls++];
tmp[cnt++].flag = 1;
} else if(a[ls].y > a[rs].y) {
tmp[cnt] = a[rs++];
tmp[cnt++].flag = 0;
} else if(a[ls].z <= a[rs].z) {
tmp[cnt] = a[ls++];
tmp[cnt++].flag = 1;
} else {
tmp[cnt] = a[rs++];
tmp[cnt++].flag = 0;
}
}
while(ls <= mid) {
tmp[cnt] = a[ls++];
tmp[cnt++].flag = 1;
}
while(rs <= r) {
tmp[cnt] = a[rs++];
tmp[cnt++].flag = 0;
}
for(int i = l; i <= r; ++i) a[i] = tmp[i];
cdq2(l, r);
} int main() {
#ifndef ONLINE_JUDGE
FIN;
#endif
scanf("%d%d%d%d", &n, &m, &h, &q);
n += 1, m += 1, h += 1;
for(int i = 1; i <= q; ++i) {
scanf("%d%d%d%d", &op, &x, &y, &z);
a[i] = {i, x, y, z, op, 0, 0};
}
//x,y,z
for(int i = 1; i <= q; ++i) {
ans[i] = inf;
a[i].val = a[i].x + a[i].y + a[i].z, a[i].flag = 0;
}
cdq1(1, q); //x,y,-z
for(int i = 1; i <= q; ++i) {
a[i].z = h - a[i].z;
a[i].val = a[i].x + a[i].y + a[i].z, a[i].flag = 0;
}
sort(a + 1, a + q + 1);
cdq1(1, q); //x,-y,z
for(int i = 1; i <= q; ++i) {
a[i].z = h - a[i].z;
a[i].y = m - a[i].y;
a[i].val = a[i].x + a[i].y + a[i].z, a[i].flag = 0;
}
sort(a + 1, a + q + 1);
cdq1(1, q); //-x,y,z
for(int i = 1; i <= q; ++i) {
a[i].y = m - a[i].y;
a[i].x = n - a[i].x;
a[i].val = a[i].x + a[i].y + a[i].z, a[i].flag = 0;
}
sort(a + 1, a + q + 1);
cdq1(1, q); //-x,-y,z
for(int i = 1; i <= q; ++i) {
a[i].y = m - a[i].y;
a[i].val = a[i].x + a[i].y + a[i].z, a[i].flag = 0;
}
sort(a + 1, a + q + 1);
cdq1(1, q); //-x,y,-z
for(int i = 1; i <= q; ++i) {
a[i].y = m - a[i].y;
a[i].z = h - a[i].z;
a[i].val = a[i].x + a[i].y + a[i].z, a[i].flag = 0;
}
sort(a + 1, a + q + 1);
cdq1(1, q); //x,-y,-z
for(int i = 1; i <= q; ++i) {
a[i].x = n - a[i].x;
a[i].y = m - a[i].y;
a[i].val = a[i].x + a[i].y + a[i].z, a[i].flag = 0;
}
sort(a + 1, a + q + 1);
cdq1(1, q); //-x,-y,-z
for(int i = 1; i <= q; ++i) {
a[i].x = n - a[i].x;
a[i].val = a[i].x + a[i].y + a[i].z, a[i].flag = 0;
}
sort(a + 1, a + q + 1);
cdq1(1, q); sort(a + 1, a + q + 1);
for(int i = 1; i <= q; ++i) {
if(a[i].op == 2) {
printf("%d\n", ans[i]);
}
}
return 0;
}

Distance(2019年牛客多校第八场D题+CDQ+树状数组)的更多相关文章

  1. Explorer(2019年牛客多校第八场E题+线段树+可撤销并查集)

    题目链接 传送门 题意 给你一张无向图,每条边\(u_i,v_i\)的权值范围为\([L_i,R_i]\),要经过这条边的条件是你的容量要在\([L_i,R_i]\),现在问你你有多少种容量使得你可以 ...

  2. 2019牛客多校第七场 F Energy stones 树状数组+算贡献转化模拟

    Energy stones 题意 有n块石头,每块有初始能量E[i],每秒石头会增长能量L[i],石头的能量上限是C[i],现有m次时刻,每次会把[s[i],t[i]]的石头的能量吸干,问最后得到了多 ...

  3. 2019牛客多校第八场 F题 Flowers 计算几何+线段树

    2019牛客多校第八场 F题 Flowers 先枚举出三角形内部的点D. 下面所说的旋转没有指明逆时针还是顺时针则是指逆时针旋转. 固定内部点的答案的获取 anti(A)anti(A)anti(A)或 ...

  4. 2020牛客多校第八场K题

    __int128(例题:2020牛客多校第八场K题) 题意: 有n道菜,第i道菜的利润为\(a_i\),且有\(b_i\)盘.你要按照下列要求给顾客上菜. 1.每位顾客至少有一道菜 2.给顾客上菜时, ...

  5. 2019年牛客多校第四场 B题xor(线段树+线性基交)

    题目链接 传送门 题意 给你\(n\)个基底,求\([l,r]\)内的每个基底是否都能异或出\(x\). 思路 线性基交板子题,但是一直没看懂咋求,先偷一份咖啡鸡板子写篇博客吧~ 线性基交学习博客:传 ...

  6. Palindrome Mouse(2019年牛客多校第六场C题+回文树+树状数组)

    目录 题目链接 题意 思路 代码 题目链接 传送门 题意 问\(s\)串中所有本质不同的回文子串中有多少对回文子串满足\(a\)是\(b\)的子串. 思路 参考代码:传送门 本质不同的回文子串肯定是要 ...

  7. generator 1(2019年牛客多校第五场B题+十进制矩阵快速幂)

    目录 题目链接 思路 代码 题目链接 传送门 思路 十进制矩阵快速幂. 代码 #include <set> #include <map> #include <deque& ...

  8. Find the median(2019年牛客多校第七场E题+左闭右开线段树)

    题目链接 传送门 题意 每次往集合里面添加一段连续区间的数,然后询问当前集合内的中位数. 思路 思路很好想,但是卡内存. 当时写的动态开点线段树没卡过去,赛后机房大佬用动态开点过了,\(tql\). ...

  9. 2019年牛客多校第三场 F题Planting Trees(单调队列)

    题目链接 传送门 题意 给你一个\(n\times n\)的矩形,要你求出一个面积最大的矩形使得这个矩形内的最大值减最小值小于等于\(M\). 思路 单调队列滚动窗口. 比赛的时候我的想法是先枚举长度 ...

随机推荐

  1. docker for mac的JSON配置文件中的hosts项修改后无法生效

    docker for mac的JSON配置文件中的hosts项修改后无法生效 docker 2.1k 次浏览 问题对人有帮助,内容完整,我也想知道答案0问题没有实际价值,缺少关键内容,没有改进余地 增 ...

  2. [Gamma]阶段测试报告

    后端测试 我们进行了覆盖性测试,覆盖率达到77%. Beta阶段发现的Bug 项目显示的图片错误 无法使用搜索框 发布实验室项目的按钮点击无法跳转 连续点击发帖按钮可能发出多个相同的帖子 不需要点击我 ...

  3. GCN(Graph Convolutional Network)的简单公式推导

    第一步:从前一个隐藏层到后一个隐藏层,对结点进行特征变换 第二步:对第一步进行具体实现 第三步:对邻接矩阵进行归一化(行之和为1) 邻接矩阵A的归一化,可以通过度矩阵D来实现(即通过D^-1*A来实现 ...

  4. 新博客 https://k8gege.org

    新博客 https://k8gege.org 于2019/12/3启用,忘了发 由于博客园长期被Google误报屏蔽,导致Firefox/Chrome等浏览器无法访问博客 发现将被Google误报的文 ...

  5. 在电脑上部署网站lls 浏览器上访问

    在win7 上安装,首先是要进入lls中进行设置,这是第一种方法: 通过图形化的操作方式进行设置, 这个界面中的东西都比较重要,电脑出现问题的话一般在这都可以进行调整,程序和功能可以用来管理你安装的软 ...

  6. 解决新版本R3.6.0不能加载devtools包问题

    首先是看到下面这个文章想试着练习一下,结果第一步就卡住了,无法加载devtools包,繁体字都冒出来了......汗!(没有截图,但过程痛苦不堪~) https://www.sohu.com/a/12 ...

  7. 【1】【leetcode-17】电话号码的字母组合

    给定一个仅包含数字 2-9 的字符串,返回所有它能表示的字母组合. 给出数字到字母的映射如下(与电话按键相同).注意 1 不对应任何字母. 示例: 输入:"23"输出:[" ...

  8. 使用DbVisualizer 10.0.20 查询ES中的索引时需要注意的事项

    查询前5条数据 光标停在某一个查询结果框中,左下角会显示该字段的类型 查询类型是text的字段使用单引号,使用双引号查询会报错

  9. php 获取当前IP地址

    function getIP() { return isset($_SERVER["HTTP_X_FORWARDED_FOR"])?$_SERVER["HTTP_X_FO ...

  10. win10企业版|激|活|码

    使用下面的激活码升级成企业版 NPPR9-FWDCX-D2C8J-H872K-2YT43 激活后,如果右下角出现未激活的白色字体用cmd命令,管理员窗口运行下面的命令 win10企业版用户请依次输入: ...