传送门

题意:

坐标轴上有\(n\)个机器人,每个机器人带有属性\(x,r,q\),分别表示位置、可视半径以及智商。

现在定义智商相近为两个机器人的智商差的绝对值不超过$K。

现在问有多少对机器人,他们在互相的可视范围内并且智商相近。

思路:

一开始没注意到互相在对面的可视范围内,以为是主席树模板题。。。

  • 注意到\(K\leq 20\),所以总的有用的智商值为\(40*n\)个。
  • 那么我们可以采用动态开点的方法,对于每个智商值,储存存在的区间的位置,这样可以节约空间。
  • 对于互相在对方可视范围内,可以先按\(r\)从大到小排序,然后依次处理,当前位置为\(x_i\),那么在\([x_i-r_i,x_i+r_i\)内的机器人都是合法的。
  • 那么对于每个机器人,暴力每个智商值,查询在对应区间范围内的机器人个数即可。

时间复杂度约为\(O(nklogn)\),空间复杂度也因为动态开点优化到了\(O(nlogn)\)。

代码如下:

/*
* Author: heyuhhh
* Created Time: 2019/11/12 20:42:29
*/
#include <bits/stdc++.h>
#define MP make_pair
#define fi first
#define se second
#define sz(x) (int)(x).size()
#define all(x) (x).begin(), (x).end()
#define INF 0x3f3f3f3f
#ifdef Local
#define dbg(args...) do { cout << #args << " -> "; err(args); } while (0)
void err() { std::cout << '\n'; }
template<typename T, typename...Args>
void err(T a, Args...args) { std::cout << a << ' '; err(args...); }
#else
#define dbg(...)
#endif
void pt() {std::cout << '\n'; }
template<typename T, typename...Args>
void pt(T a, Args...args) {std::cout << a << ' '; pt(args...); }
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
//head
const int N = 1e5 + 25; int n, k;
struct node{
int x, r, q;
bool operator < (const node &A) const{
return r > A.r;
}
}a[N]; unordered_map <int, int> mp;
int tot;
int rt[N * 35], ls[N * 35], rs[N * 35], sum[N * 35]; void upd(int &o, int l, int r, int p, int v) {
if(!o) o = ++tot;
if(l == r) {
sum[o] += v;
return;
}
int mid = (l + r) >> 1;
if(p <= mid) upd(ls[o], l, mid, p, v);
else upd(rs[o], mid + 1, r, p, v);
sum[o] = sum[ls[o]] + sum[rs[o]];
} int query(int o, int l, int r, int L, int R) {
if(!o) return 0;
if(L <= l && r <= R) {
return sum[o];
}
int res = 0, mid = (l + r) >> 1;
if(L <= mid) res += query(ls[o], l, mid, L, R);
if(R > mid) res += query(rs[o], mid + 1, r, L, R);
return res;
} void run(){
for(int i = 1; i <= n; i++) cin >> a[i].x >> a[i].r >> a[i].q;
sort(a + 1, a + n + 1);
ll ans = 0;
for(int i = 1; i <= n; i++) {
int l = max(0, a[i].x - a[i].r);
int r = a[i].x + a[i].r;
int L = max(0, a[i].q - k);
int R = a[i].q + k;
for(int j = L; j <= R; j++) {
if(mp.find(j) == mp.end()) continue;
ans += query(rt[mp[j]], 0, INF, l, r);
}
if(mp.find(a[i].q) == mp.end()) mp[a[i].q] = ++tot;
upd(rt[mp[a[i].q]], 0, INF, a[i].x, 1);
}
cout << ans << '\n';
} int main() {
ios::sync_with_stdio(false);
cin.tie(0); cout.tie(0);
cout << fixed << setprecision(20);
while(cin >> n >> k) run();
return 0;
}

【cf1046】A. AI robots(动态开点线段树)的更多相关文章

  1. CF1045G AI robots(动态开点线段树)

    题意 火星上有$N$个机器人排成一行,第$i$个机器人的位置为$x_{i}$,视野为$r_{i}$,智商为$q_{i}$.我们认为第$i$个机器人可以看到的位置是$[x_{i}-r_{i},x_{i} ...

  2. [2016湖南长沙培训Day4][前鬼后鬼的守护 chen] (动态开点线段树+中位数 or 动规 or 贪心+堆优化)

    题目大意 给定一个长度为n的正整数序列,令修改一个数的代价为修改前后两个数的绝对值之差,求用最小代价将序列转换为不减序列. 其中,n满足小于500000,序列中的正整数小于10^9 题解(引自mzx神 ...

  3. [bzoj 3531][SDOI2014]旅行(树链剖分+动态开点线段树)

    题目:http://www.lydsy.com:808/JudgeOnline/problem.php?id=3531 分析: 对于每个颜色(颜色<=10^5)都建立一颗线段树 什么!那么不是M ...

  4. 【BZOJ-4636】蒟蒻的数列 动态开点线段树 ||(离散化) + 标记永久化

    4636: 蒟蒻的数列 Time Limit: 30 Sec  Memory Limit: 256 MBSubmit: 247  Solved: 113[Submit][Status][Discuss ...

  5. codeforces 893F - Physical Education Lessons 动态开点线段树合并

    https://codeforces.com/contest/893/problem/F 题意: 给一个有根树, 多次查询,每次查询对于$x$i点的子树中,距离$x$小于等于$k$的所有点中权值最小的 ...

  6. codeforces 915E - Physical Education Lessons 动态开点线段树

    题意: 最大$10^9$的区间, $3*10^5$次区间修改,每次操作后求整个区间的和 题解: 裸的动态开点线段树,计算清楚数据范围是关键... 经过尝试 $2*10^7$会$MLE$ $10^7$会 ...

  7. CF915E Physical Education Lessons 动态开点线段树

    题目链接 CF915E Physical Education Lessons 题解 动态开点线段树 代码 /* 动态开点线段树 */ #include<cstdio> #include&l ...

  8. 洛谷P3313 [SDOI2014]旅行(树链剖分 动态开节点线段树)

    题意 题目链接 Sol 树链剖分板子 + 动态开节点线段树板子 #include<bits/stdc++.h> #define Pair pair<int, int> #def ...

  9. NOIP2017 列队——动态开点线段树

    Description: Sylvia 是一个热爱学习的女♂孩子. 前段时间,Sylvia 参加了学校的军训.众所周知,军训的时候需要站方阵. Sylvia 所在的方阵中有n×m名学生,方阵的行数为  ...

  10. 洛谷P3120 [USACO15FEB]牛跳房子(动态开节点线段树)

    题意 题目链接 Sol \(f[i][j]\)表示前\(i\)行\(j\)列的贡献,转移的时候枚举从哪里转移而来,复杂度\(O(n^4)\) 然后考虑每一行的贡献,动态开节点线段树维护一下每种颜色的答 ...

随机推荐

  1. Linux数据库的创建 导入导出 以及一些基本指令

    首先linux 下查看mysql相关目录 查看 mysql 的安装路径 执行查询 SQL mysql>show variables like '%dir%'; datadir 就是数据路径 确定 ...

  2. TensorFlow从1到2(十三)图片风格迁移

    风格迁移 <从锅炉工到AI专家(8)>中我们介绍了一个"图片风格迁移"的例子.因为所引用的作品中使用了TensorFlow 1.x的代码,算法也相对复杂,所以文中没有仔 ...

  3. 三、ITK的dcm图像读写

    一.主要功能 1.读取单张dcm图像 2.写入单张dcm图像 3.图像调整之后以.jpg格式写入 4.调整之后重新以.dcm格式写入 二.代码 #include "itkImageFileR ...

  4. ZOJ 3778 Talented Chief

    http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3778 题目 某人做菜很厉害,一分钟能同时完成最多m个菜的一道工序,输入菜的 ...

  5. Codeforces Round #602 (Div. 2, based on Technocup 2020 Elimination Round 3) E. Arson In Berland Forest 二分 前缀和

    E. Arson In Berland Forest The Berland Forest can be represented as an infinite cell plane. Every ce ...

  6. Codeforces Round #599 (Div. 1) A. Tile Painting 数论

    C. Tile Painting Ujan has been lazy lately, but now has decided to bring his yard to good shape. Fir ...

  7. 【Linux】LVM操作添加新硬盘

    目录 1.查看当前硬盘及分区情况 2.初始化/dev/sdb为PV(physical volume) 3.PV加入至VG组. 4.创建lv 5.格式化逻辑分区 6.挂载硬盘/data 7.迁移zabb ...

  8. 图解servlet

    You can see the following illustration to better understand the lifecycle of the Servlet. When the r ...

  9. PI对于两个SAP客户端通道的了解

    你把你的报文放到ESR的MM里面试一下就知道了 日期格式之类的,可能有转换的你要输入2019-05-13这种 OA到PI不选,都是到一个系统,由PI再来分流 如果你要做成由OA来选的,就要参考类似于  ...

  10. java基础(11):接口、多态

    1. 接口 1.1 接口概念 接口是功能的集合,同样可看做是一种数据类型,是比抽象类更为抽象的”类”. 接口只描述所应该具备的方法,并没有具体实现,具体的实现由接口的实现类(相当于接口的子类)来完成. ...