【LG4169】[Violet]天使玩偶/SJY摆棋子

题面

洛谷

题解

至于\(cdq\)分治的解法,以前写过

\(kdTree\)的解法好像还\(sb\)一些

就是记一下子树的横、纵坐标最值然后求一下点到矩形得到距离

之后再剪枝即可

为什么不吸氧还是跑不过啊

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <algorithm>
using namespace std;
inline int gi() {
register int data = 0, w = 1;
register char ch = 0;
while (!isdigit(ch) && ch != '-') ch = getchar();
if (ch == '-') w = -1, ch = getchar();
while (isdigit(ch)) data = 10 * data + ch - '0', ch = getchar();
return w * data;
}
void chkmin(int &x, int y) { if (x > y) x = y; }
void chkmax(int &x, int y) { if (x < y) x = y; }
const int MAX_N = 1e6 + 5;
const double alpha = 0.75;
const int INF = 1e9;
struct Point { int x[2]; } p[MAX_N];
struct Node {
int mn[2], mx[2], ls, rs, size;
Point tp;
} t[MAX_N];
int N, M, rt, cur, top, WD, ans, rub[MAX_N];
bool operator < (const Point &l, const Point &r) { return l.x[WD] < r.x[WD]; }
int newnode() {
if (top) return rub[top--];
else return ++cur;
}
void pushup(int o) {
int ls = t[o].ls, rs = t[o].rs;
for (int i = 0; i <= 1; i++) {
t[o].mn[i] = t[o].mx[i] = t[o].tp.x[i];
if (ls) chkmin(t[o].mn[i], t[ls].mn[i]), chkmax(t[o].mx[i], t[ls].mx[i]);
if (rs) chkmin(t[o].mn[i], t[rs].mn[i]), chkmax(t[o].mx[i], t[rs].mx[i]);
}
t[o].size = t[ls].size + t[rs].size + 1;
}
int build(int l, int r, int wd) {
if (l > r) return 0;
int o = newnode(), mid = (l + r) >> 1;
WD = wd, nth_element(&p[l], &p[mid], &p[r + 1]), t[o].tp = p[mid];
t[o].ls = build(l, mid - 1, wd ^ 1), t[o].rs = build(mid + 1, r, wd ^ 1);
return pushup(o), o;
}
void pia(int o, int num) {
if (t[o].ls) pia(t[o].ls, num);
p[num + t[t[o].ls].size + 1] = t[o].tp, rub[++top] = o;
if (t[o].rs) pia(t[o].rs, num + t[t[o].ls].size + 1);
}
void check(int &o, int wd) {
if (alpha * t[o].size < t[t[o].ls].size || alpha * t[o].size < t[t[o].rs].size)
pia(o, 0), o = build(1, t[o].size, wd);
}
void insert(int &o, int wd, Point tmp) {
if (!o) return (void)(o = newnode(), t[o].tp = tmp, t[o].ls = t[o].rs = 0, pushup(o));
if (t[o].tp.x[wd] < tmp.x[wd]) insert(t[o].r
91s, wd ^ 1, tmp);
else insert(t[o].ls, wd ^ 1, tmp);
pushup(o), check(o, wd);
}
int getdis(int o, Point tmp) {
int res = 0;
for (int i = 0; i <= 1; i++) res += max(0, tmp.x[i] - t[o].mx[i]) + max(0, t[o].mn[i] - tmp.x[i]);
return res;
}
int dist(Point a, Point b) { return abs(a.x[0] - b.x[0]) + abs(a.x[1] - b.x[1]); }
void query(int o, Point tmp) {
ans = min(ans, dist(tmp, t[o].tp));
int dl = INF, dr = INF;
if (t[o].ls) dl = getdis(t[o].ls, tmp);
if (t[o].rs) dr = getdis(t[o].rs, tmp);
if (dl < dr) {
if (dl < ans) query(t[o].ls, tmp);
if (dr < ans) query(t[o].rs, tmp);
} else {
if (dr < ans) query(t[o].rs, tmp);
if (dl < ans) query(t[o].ls, tmp);
}
} int main () {
N = gi(), M = gi();
for (int i = 1; i <= N; i++) p[i].x[0] = gi(), p[i].x[1] = gi();
rt = build(1, N, 0);
while (M--) {
Point tmp;
int op = gi(); tmp.x[0] = gi(), tmp.x[1] = gi();
if (op == 1) insert(rt, 0, tmp);
else ans = INF, query(rt, tmp), printf("%d\n", ans);
}
return 0;
}

【LG4169】[Violet]天使玩偶/SJY摆棋子的更多相关文章

  1. LG4169 [Violet]天使玩偶/SJY摆棋子

    题意 Ayu 在七年前曾经收到过一个天使玩偶,当时她把它当作时间囊埋在了地下.而七年后 的今天,Ayu 却忘了她把天使玩偶埋在了哪里,所以她决定仅凭一点模糊的记忆来寻找它. 我们把 Ayu 生活的小镇 ...

  2. bzoj2716/2648 / P4169 [Violet]天使玩偶/SJY摆棋子

    P4169 [Violet]天使玩偶/SJY摆棋子 k-d tree 模板 找了好几天才发现输出优化错了....真是zz...... 当子树非常不平衡时,就用替罪羊树的思想,拍扁重建. luogu有个 ...

  3. 洛谷 P4169 [Violet]天使玩偶/SJY摆棋子 解题报告

    P4169 [Violet]天使玩偶/SJY摆棋子 题目描述 \(Ayu\)在七年前曾经收到过一个天使玩偶,当时她把它当作时间囊埋在了地下.而七年后 的今天,\(Ayu\) 却忘了她把天使玩偶埋在了哪 ...

  4. luoguP4169 [Violet]天使玩偶/SJY摆棋子 K-Dtree

    P4169 [Violet]天使玩偶/SJY摆棋子 链接 luogu 思路 luogu以前用CDQ一直过不去. bzoj还是卡时过去的. 今天终于用k-dtree给过了. 代码 #include &l ...

  5. 洛谷P4169 [Violet]天使玩偶/SJY摆棋子(CDQ分治)

    [Violet]天使玩偶/SJY摆棋子 题目传送门 解题思路 用CDQ分治开了氧气跑过. 将输入给的顺序作为第一维的时间,x为第二维,y为第三维.对于距离一个询问(ax,ay),将询问分为四块,左上, ...

  6. [Violet]天使玩偶/SJY摆棋子 [cdq分治]

    P4169 [Violet]天使玩偶/SJY摆棋子 求离 \((x,y)\) 最近点的距离 距离的定义是 \(|x1-x2|+|y1-y2|\) 直接cdq 4次 考虑左上右上左下右下就可以了-略微卡 ...

  7. P4169 [Violet]天使玩偶/SJY摆棋子

    题目背景 感谢@浮尘ii 提供的一组hack数据 题目描述 Ayu 在七年前曾经收到过一个天使玩偶,当时她把它当作时间囊埋在了地下.而七年后 的今天,Ayu 却忘了她把天使玩偶埋在了哪里,所以她决定仅 ...

  8. 洛谷P4169 [Violet]天使玩偶/SJY摆棋子

    %%%神仙\(SJY\) 题目大意: 一个二维平面,有两种操作: \(1.\)增加一个点\((x,y)\) \(2.\)询问距离\((x,y)\)曼哈顿最近的一个点有多远 \(n,m\le 300 0 ...

  9. Luogu P4169 [Violet]天使玩偶/SJY摆棋子

    传送门 二维平面修改+查询,cdq分治可以解决. 求关于某个点曼哈顿距离(x,y坐标)最近的点——dis(A,B) = |Ax-Bx|+|Ay-By| 但是如何去掉绝对值呢? 查看题解发现假设所有的点 ...

随机推荐

  1. BZOJ 1059 矩阵游戏 二分图匹配

    题目链接: https://www.lydsy.com/JudgeOnline/problem.php?id=1059 题目大意: 小Q是一个非常聪明的孩子,除了国际象棋,他还很喜欢玩一个电脑益智游戏 ...

  2. BeautifulSoup使用注意事项

    BeautifulSoup使用注意事项 BeautifulSoup是一个可以从HTML或XML文件中提取数据的Python库.它能够通过你喜欢的转换器实现惯用的文档导航,查找,修改文档的方式.Beau ...

  3. 异步模式:Callbacks, Promises & Async/Await

    [译]异步JavaScript的演变史:从回调到Promises再到Async/Await https://www.i-programmer.info/programming/theory/8864- ...

  4. Hadoop学习之路(二十)MapReduce求TopN

    前言 在Hadoop中,排序是MapReduce的灵魂,MapTask和ReduceTask均会对数据按Key排序,这个操作是MR框架的默认行为,不管你的业务逻辑上是否需要这一操作. 技术点 MapR ...

  5. myeclipse2014黑色主题风格设置

    http://jingyan.baidu.com/article/915fc41494db8451384b2043.html?st=2&os=0&bd_page_type=1& ...

  6. CentOS 7.0 防火墙操作

    CentOS 7.0默认使用的是firewall作为防火墙,之前版本是使用iptables.所以在CentOS 7执行下面命令是无法查看防火墙状态的. [root@localhost ~]# serv ...

  7. dispatch 之 常见函数小结

    你好2019!一起努力呀! 直奔主题 1.dispatch_barrier_async VS  dispatch_barrier_sync Barrier blocks only behave spe ...

  8. Angular7教程-03-Angular常用操作(上)

    本节来介绍angular中的操作以及TypeScript语法的简单介绍.关于TypeScript语法的更为详细的内容,打算在整个angular教程结束后再单独介绍. 0. 安装所需要的插件及配置插件 ...

  9. 添加一个js扩展方法

    String.prototype.repeatify=String.prototype.repeatify || function(times){ var str=''; for(var i=0;i& ...

  10. C++练习 | 在递增序列中查找最后一个小于等于指定数的元素

    #include <iostream> using namespace std; int mid,l0; int solve(int a1[],int l,int r,int x) { & ...