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

相关文章

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

算法复杂度 —— 数据结构前言、算法效率、时间复杂度、空间复杂度、常见复杂度对比、复杂度算法题(旋转数组)

目录 一、数据结构前言 1、数据结构 2、算法 3、学习方法 二、 算法效率 引入概念:算法复杂度  三、时间复杂度 1、大O的渐进表示法 2、时间复杂度计算示例  四、空间复杂度 计算示例:空间复杂度 五、常见复杂度对比 六、复杂度算法题(旋转数组) 1、思路1 2、思路2 3、思路3 一、数据结构前言 1、数据结构         数据结构(D

005:VTK世界坐标系中的相机和物体

VTK医学图像处理---世界坐标系中的相机和物体 左侧是成像结果                                                    右侧是世界坐标系中的相机与被观察物体 目录 VTK医学图像处理---世界坐标系中的相机和物体 简介 1 在三维空间中添加坐标系 2 世界坐标系中的相机 3 世界坐标系中vtkImageData的参数 总结:

点云数据常见的坐标系有哪些,如何进行转换?

文章目录 一、点云坐标系分类1. 世界坐标系2. 相机坐标系3. 极坐标系4. 笛卡尔坐标系(直角坐标系):5. 传感器坐标系6. 地理坐标系 二、坐标系转换方法1. 地理坐标系与投影坐标系之间的转换2. 投影坐标系与局部坐标系之间的转换3. 局部坐标系与3D模型坐标系之间的转换4. 相机坐标系与其他坐标系之间的转换5. 传感器坐标系与其他坐标系之间的转换 三、坐标系转换工具 一

UVa 10820 Send a Table (Farey数列欧拉函数求和)

这里先说一下欧拉函数的求法 先说一下筛选素数的方法 void Get_Prime(){ /*筛选素数法*/for(int i = 0; i < N; i++) vis[i] = 1;vis[0] = vis[1] = 0;for(int i = 2; i * i < N; i++)if(vis[i]){for(int j = i * i; j < N; j += i)vis[j] =

计算几何之向量旋转

实际做题中我们可能会遇到很多有关及计算几何的问题,其中有一类问题就是向量的旋转问题,下面我们来具体探讨一下有关旋转的问题。 首先我们先把问题简化一下,我们先研究一个点绕另一个点旋转一定角度的问题。已知A点坐标(x1,y1),B点坐标(x2,y2),我们需要求得A点绕着B点旋转θ度后的位置。 A点绕B点旋转θ角度后得到的点,问题是我们要如何才能得到A' 点的坐标。(向逆时针方向旋转角度正,