BZOJ 2989 数列 —— kd-tree + 旋转坐标系

2023-11-02 10:32

本文主要是介绍BZOJ 2989 数列 —— kd-tree + 旋转坐标系,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

题目链接:点我啊╭(╯^╰)╮

题目大意:

在这里插入图片描述

解题思路:

    将 ( i , a [ i ] ) (i,a[i]) i,a[i] 视为一个点,查询即为曼哈顿距离 ≤ k \le k k 的点数
    考虑用 k d − t r e e kd-tree kdtree 维护,但查询项是一个以 ( i , a [ i ] ) (i,a[i]) i,a[i] 为中心的菱形
    这样单次查询的复杂度无法保证(但是我测了一下只用了500多ms)

    考虑将坐标系旋转 45 ° 45° 45°,菱形就变成了正方形
    只需要将点 ( x , y ) (x,y) x,y 变成 ( x − y , x + y ) (x-y,x+y) xy,x+y
    旋转之后,原坐标系的曼哈顿距离就是新坐标系的切比雪夫距离

    这里只需要求一个正方形内的点数即可
    测了一下确实快了很多
    交题链接

不旋转坐标系:

#include<bits/stdc++.h>
#define rint register int
#define deb(x) cerr<<#x<<" = "<<(x)<<'\n';
using namespace std;
typedef long long ll;
typedef pair <int,int> pii;
const int maxn = 2e5 + 5;
int n, q, rt, tot, now, g[maxn], ans;
int sta[maxn], top;
struct node{int pla[2];
} a[maxn];
struct tree{int mx[2], mn[2], sz, ls, rs;node place;
} t[maxn]; inline int New(){if(top) return sta[top--];return ++tot; 
}
inline bool cmp(const node &A, const node &B){return A.pla[now] < B.pla[now];
}inline void update(int p){for(int i=0; i<=1; i++){t[p].mx[i] = t[p].mn[i] = t[p].place.pla[i];if(t[p].ls) t[p].mx[i] = max(t[p].mx[i], t[t[p].ls].mx[i]), \t[p].mn[i] = min(t[p].mn[i], t[t[p].ls].mn[i]);if(t[p].rs) t[p].mx[i] = max(t[p].mx[i], t[t[p].rs].mx[i]), \t[p].mn[i] = min(t[p].mn[i], t[t[p].rs].mn[i]);}t[p].sz = t[t[p].ls].sz + t[t[p].rs].sz + 1;
}inline int build(int l, int r, int opt){if(l > r) return 0;int x = New(), mid = l + r >> 1; now = opt;nth_element(a+l, a+mid, a+r+1, cmp); t[x].place = a[mid];t[x].ls = build(l, mid-1, opt^1);t[x].rs = build(mid+1, r, opt^1);update(x); return x;
}inline void pia(int p, int cnt){if(t[p].ls) pia(t[p].ls, cnt);a[cnt+t[t[p].ls].sz+1] = t[p].place, sta[++top] = p;if(t[p].rs) pia(t[p].rs, cnt+t[t[p].ls].sz+1);
}inline void check(int &p, int opt){if(t[p].sz*0.75<max(t[t[p].ls].sz, t[t[p].rs].sz))pia(p, 0), p = build(1, t[p].sz, opt);
}inline void insert(node ret, int &p, int opt){if(!p){p = New(); t[p].place = ret;t[p].ls = t[p].rs = 0;update(p); return;}if(ret.pla[opt] <= t[p].place.pla[opt]) insert(ret, t[p].ls, opt^1);else insert(ret, t[p].rs, opt^1);update(p); check(p, opt);
}inline int ck(node ret, int p, int k){int mx = max(abs(ret.pla[0] - t[p].mn[0]), abs(ret.pla[0] - t[p].mx[0])) + \max(abs(ret.pla[1] - t[p].mn[1]), abs(ret.pla[1] - t[p].mx[1]));int mn = max(0, ret.pla[0] - t[p].mx[0]) + max(0, t[p].mn[0] - ret.pla[0]) + max(0, ret.pla[1] - t[p].mx[1]) + max(0, t[p].mn[1] - ret.pla[1]);if(mx <= k) return 1;if(mn > k) return -1;return 0;
}inline int query(node ret, int p, int k){if(!p) return 0;int q = ck(ret, p, k);if(q == -1) return 0;if(q == 1) return t[p].sz;int res = 0;if(abs(t[p].place.pla[0]-ret.pla[0]) + \abs(t[p].place.pla[1]-ret.pla[1]) <= k)res++;res += query(ret, t[p].ls, k);res += query(ret, t[p].rs, k);return res;
} signed main() {scanf("%d%d", &n, &q);for(int i=1; i<=n; i++){scanf("%d", g+i);a[i].pla[0] = i;a[i].pla[1] = g[i];}rt = build(1, n, 0);while(q--){char s[15]; int x, k; node ins;scanf("%s%d%d", s, &x, &k);ins.pla[0] = x, ins.pla[1] = k;if(s[0] == 'M') g[x] = k, insert(ins, rt, 0);else {ins.pla[1] = g[x];printf("%d\n", query(ins, rt, k));}}
}

旋转坐标系:

#include<bits/stdc++.h>
#define rint register int
#define deb(x) cerr<<#x<<" = "<<(x)<<'\n';
using namespace std;
typedef long long ll;
typedef pair <int,int> pii;
const int maxn = 2e5 + 5;
int n, q, rt, tot, now, g[maxn], ans;
int sta[maxn], top;
struct node{int pla[2];
} a[maxn];
struct tree{int mx[2], mn[2], sz, ls, rs;node place;
} t[maxn]; inline int New(){if(top) return sta[top--];return ++tot; 
}
inline bool cmp(const node &A, const node &B){return A.pla[now] < B.pla[now];
}inline void update(int p){for(int i=0; i<=1; i++){t[p].mx[i] = t[p].mn[i] = t[p].place.pla[i];if(t[p].ls) t[p].mx[i] = max(t[p].mx[i], t[t[p].ls].mx[i]), \t[p].mn[i] = min(t[p].mn[i], t[t[p].ls].mn[i]);if(t[p].rs) t[p].mx[i] = max(t[p].mx[i], t[t[p].rs].mx[i]), \t[p].mn[i] = min(t[p].mn[i], t[t[p].rs].mn[i]);}t[p].sz = t[t[p].ls].sz + t[t[p].rs].sz + 1;
}inline int build(int l, int r, int opt){if(l > r) return 0;int x = New(), mid = l + r >> 1; now = opt;nth_element(a+l, a+mid, a+r+1, cmp); t[x].place = a[mid];t[x].ls = build(l, mid-1, opt^1);t[x].rs = build(mid+1, r, opt^1);update(x); return x;
}inline void pia(int p, int cnt){if(t[p].ls) pia(t[p].ls, cnt);a[cnt+t[t[p].ls].sz+1] = t[p].place, sta[++top] = p;if(t[p].rs) pia(t[p].rs, cnt+t[t[p].ls].sz+1);
}inline void check(int &p, int opt){if(t[p].sz*0.75<max(t[t[p].ls].sz, t[t[p].rs].sz))pia(p, 0), p = build(1, t[p].sz, opt);
}inline void insert(node ret, int &p, int opt){if(!p){p = New(); t[p].place = ret;t[p].ls = t[p].rs = 0;update(p); return;}if(ret.pla[opt] <= t[p].place.pla[opt]) insert(ret, t[p].ls, opt^1);else insert(ret, t[p].rs, opt^1);update(p); check(p, opt);
}inline int ck(int p, int x1, int y1, int x2, int y2){if(t[p].mn[0]>x2 || t[p].mx[0]<x1) return -1;if(t[p].mn[1]>y2 || t[p].mx[1]<y1) return -1;if(t[p].mn[0]>=x1 && t[p].mx[0]<=x2 && t[p].mn[1]>=y1 && t[p].mx[1]<=y2) return 1;return 0;
}inline int query(int p, int x1, int y1, int x2, int y2){if(!p) return 0;int q = ck(p, x1, y1, x2, y2);if(q == -1) return 0;if(q == 1) return t[p].sz;int res = 0;if(t[p].place.pla[0]>=x1 && t[p].place.pla[0]<=x2 && \t[p].place.pla[1]>=y1 && t[p].place.pla[1]<=y2) res++;res += query(t[p].ls, x1, y1, x2, y2);res += query(t[p].rs, x1, y1, x2, y2);return res;
} signed main() {scanf("%d%d", &n, &q);for(int i=1; i<=n; i++){scanf("%d", g+i);a[i].pla[0] = i - g[i];a[i].pla[1] = i + g[i];}rt = build(1, n, 0);while(q--){char s[15]; int x, k; node ins;scanf("%s%d%d", s, &x, &k);ins.pla[0] = x - k, ins.pla[1] = x + k;if(s[0] == 'M') g[x] = k, insert(ins, rt, 0);else printf("%d\n", query(rt, x-g[x]-k, x+g[x]-k, x-g[x]+k, x+g[x]+k));}
}

这篇关于BZOJ 2989 数列 —— kd-tree + 旋转坐标系的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/330001

相关文章

基于 HTML5 Canvas 实现图片旋转与下载功能(完整代码展示)

《基于HTML5Canvas实现图片旋转与下载功能(完整代码展示)》本文将深入剖析一段基于HTML5Canvas的代码,该代码实现了图片的旋转(90度和180度)以及旋转后图片的下载... 目录一、引言二、html 结构分析三、css 样式分析四、JavaScript 功能实现一、引言在 Web 开发中,

基于Python实现一个Windows Tree命令工具

《基于Python实现一个WindowsTree命令工具》今天想要在Windows平台的CMD命令终端窗口中使用像Linux下的tree命令,打印一下目录结构层级树,然而还真有tree命令,但是发现... 目录引言实现代码使用说明可用选项示例用法功能特点添加到环境变量方法一:创建批处理文件并添加到PATH1

使用animation.css库快速实现CSS3旋转动画效果

《使用animation.css库快速实现CSS3旋转动画效果》随着Web技术的不断发展,动画效果已经成为了网页设计中不可或缺的一部分,本文将深入探讨animation.css的工作原理,如何使用以及... 目录1. css3动画技术简介2. animation.css库介绍2.1 animation.cs

css实现图片旋转功能

《css实现图片旋转功能》:本文主要介绍了四种CSS变换效果:图片旋转90度、水平翻转、垂直翻转,并附带了相应的代码示例,详细内容请阅读本文,希望能对你有所帮助... 一 css实现图片旋转90度.icon{ -moz-transform:rotate(-90deg); -webkit-transfo

Qt QWidget实现图片旋转动画

《QtQWidget实现图片旋转动画》这篇文章主要为大家详细介绍了如何使用了Qt和QWidget实现图片旋转动画效果,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下... 一、效果展示二、源码分享本例程通过QGraphicsView实现svg格式图片旋转。.hpjavascript

poj 2187 凸包or旋转qia壳法

题意: 给n(50000)个点,求这些点与点之间距离最大的距离。 解析: 先求凸包然后暴力。 或者旋转卡壳大法。 代码: #include <iostream>#include <cstdio>#include <cstdlib>#include <algorithm>#include <cstring>#include <cmath>#include <s

Android 10.0 mtk平板camera2横屏预览旋转90度横屏拍照图片旋转90度功能实现

1.前言 在10.0的系统rom定制化开发中,在进行一些平板等默认横屏的设备开发的过程中,需要在进入camera2的 时候,默认预览图像也是需要横屏显示的,在上一篇已经实现了横屏预览功能,然后发现横屏预览后,拍照保存的图片 依然是竖屏的,所以说同样需要将图片也保存为横屏图标了,所以就需要看下mtk的camera2的相关横屏保存图片功能, 如何实现实现横屏保存图片功能 如图所示: 2.mtk

树(Tree)——《啊哈!算法》

树 什么是树?树是一种特殊的图,不包含回路的连通无向树。 正因为树有着“不包含回路”这个特点,所以树就被赋予了很多特性。 一棵树中的任意两个结点有且仅有唯一的一条路径连通。一棵树如果有n个结点,那么它一定恰好有n-1条边。在一棵树中加一条边将会构成一个回路。 一棵树有且只有一个根结点。 没有父结点的结点称为根结点(祖先)。没有子结点的结点称为叶结点。如果一个结点既不是根结点也不是叶

二维旋转公式

二维旋转公式 ros的tf工具包可以很方便的实现任意坐标系之间的坐标转换。但是,如果只是想简单的测试想法,而又不想编写过于庞杂的代码,考虑自己写二维旋转的函数。而与二维旋转问题对偶的另一个问题便是二维坐标系旋转变换。这两个问题的形式基本一样,只是旋转的角度相差一个负号。就是这个容易搞混,所以做个笔记,以备查用。 1. 二维旋转公式(算法) 而(此文只针对二维)旋转则是表示某一坐标点 ( x

226 Invert Binary Tree

//226 Invert Binary Tree//算法思路:主要使用递归算法public class Solution {public TreeNode invertTree(TreeNode root) {//1 出口 空节点if (root==null)return null;//2 递归 调用自己TreeNode left = root.left;TreeNode right = ro